20#include <config_folders.h>
22#include <osl/diagnose.h>
23#include <osl/process.h>
24#include <rtl/bootstrap.hxx>
27#include <com/sun/star/system/SimpleMailClientFlags.hpp>
28#include <com/sun/star/system/XSimpleMailMessage2.hpp>
29#include <osl/file.hxx>
36#define WIN32_LEAN_AND_MEAN
39#if defined GetTempPath
46using css::uno::UNO_QUERY;
47using css::uno::Reference;
48using css::uno::Exception;
49using css::uno::Sequence;
50using css::lang::IllegalArgumentException;
52using css::system::XSimpleMailClient;
53using css::system::XSimpleMailMessage;
54using css::system::XSimpleMailMessage2;
55using css::system::SimpleMailClientFlags::NO_USER_INTERFACE;
56using css::system::SimpleMailClientFlags::NO_LOGON_DIALOG;
63 OUString getAlternativeSenddocUrl()
65 OUString altSenddocUrl;
67 LONG lret = RegOpenKeyW(HKEY_CURRENT_USER, L
"Software\\LibreOffice\\SendAsEMailClient", &hkey);
68 if (lret == ERROR_SUCCESS)
71 LONG sz =
sizeof(buff);
72 lret = RegQueryValueW(hkey,
nullptr, buff, &sz);
73 if (lret == ERROR_SUCCESS)
75 osl::FileBase::getFileURLFromSystemPath(OUString(o3tl::toU(buff)), altSenddocUrl);
89 OUString getSenddocUrl()
91 OUString senddocUrl = getAlternativeSenddocUrl();
93 if (senddocUrl.isEmpty())
95 senddocUrl =
"$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER
"/senddoc.exe";
96 rtl::Bootstrap::expandMacros(senddocUrl);
110 bool executeSenddoc(
const std::vector<OUString>& rCommandArgs,
bool bWait)
112 OUString senddocUrl = getSenddocUrl();
113 if (senddocUrl.getLength() == 0)
116 oslProcessOption nProcOption = osl_Process_DETACHED | (bWait ? osl_Process_WAIT : 0);
123 oslProcessError
err = osl_executeProcess(
125 const_cast<rtl_uString**
>(
reinterpret_cast<rtl_uString *
const *
>(rCommandArgs.data())),
134 if (
err != osl_Process_E_None)
140 oslProcessInfo procInfo;
141 procInfo.Size =
sizeof(oslProcessInfo);
142 osl_getProcessInfo(proc, osl_Process_EXITCODE, &procInfo);
143 osl_freeProcessHandle(proc);
144 return (procInfo.Code == SUCCESS_SUCCESS);
150 return Reference<XSimpleMailMessage>(
new CSmplMailMsg());
159OUString InitBaseTempDirURL()
165 if (aRetURL.isEmpty())
167 osl::File::getTempDirURL(aRetURL);
169 if (aRetURL.endsWith(
"/"))
170 aRetURL = aRetURL.copy(0, aRetURL.getLength() - 1);
175const OUString& GetBaseTempDirURL()
177 static const OUString aRetURL(InitBaseTempDirURL());
193 maAttachmentFiles.emplace_back(std::make_unique<utl::TempFileNamed>(&GetBaseTempDirURL()));
197 OUString sCorrectedOrigAttachURL(sOrigAttachURL);
200 osl::FileBase::getFileURLFromSystemPath(sCorrectedOrigAttachURL, sCorrectedOrigAttachURL);
201 if (osl::File::copy(sCorrectedOrigAttachURL, sNewAttachmentURL) == osl::FileBase::RC::E_None)
213 sNewAttachmentURL = sOrigAttachURL;
217 return sNewAttachmentURL;
225 pTempFile->EnableKillingFile(
false);
248 const Reference<XSimpleMailMessage>& xSimpleMailMessage,
249 sal_Int32 aFlag, std::vector<OUString>& rCommandArgs)
251 OSL_ENSURE(rCommandArgs.empty(),
"Provided command argument buffer not empty");
253 Reference<XSimpleMailMessage2> xMessage( xSimpleMailMessage, UNO_QUERY );
256 OUString body = xMessage->getBody();
257 if (body.getLength()>0)
259 rCommandArgs.push_back(
"--body");
260 rCommandArgs.push_back(body);
264 OUString
to = xSimpleMailMessage->getRecipient();
265 if (
to.getLength() > 0)
267 rCommandArgs.push_back(
"--to");
268 rCommandArgs.push_back(
to);
271 const Sequence<OUString> ccRecipients = xSimpleMailMessage->getCcRecipient();
272 for (OUString
const & s : ccRecipients)
274 rCommandArgs.push_back(
"--cc");
275 rCommandArgs.push_back(s);
278 const Sequence<OUString> bccRecipients = xSimpleMailMessage->getBccRecipient();
279 for (OUString
const & s : bccRecipients)
281 rCommandArgs.push_back(
"--bcc");
282 rCommandArgs.push_back(s);
285 OUString
from = xSimpleMailMessage->getOriginator();
286 if (
from.getLength() > 0)
288 rCommandArgs.push_back(
"--from");
289 rCommandArgs.push_back(
from);
292 OUString subject = xSimpleMailMessage->getSubject();
293 if (subject.getLength() > 0)
295 rCommandArgs.push_back(
"--subject");
296 rCommandArgs.push_back(subject);
299 auto const attachments = xSimpleMailMessage->getAttachement();
300 for (
const auto& attachment : attachments)
303 bool nodelete =
false;
306 osl::FileBase::RC
err = osl::FileBase::getSystemPathFromFileURL(sTempFileURL, sysPath);
307 if (
err != osl::FileBase::E_None)
308 throw IllegalArgumentException(
309 "Invalid attachment file URL",
310 static_cast<XSimpleMailClient*
>(
this),
313 rCommandArgs.push_back(
"--attach");
314 rCommandArgs.push_back(sysPath);
317 rCommandArgs.push_back(
"--attach-name");
321 rCommandArgs.push_back(
"--nodelete");
324 if (!(aFlag & NO_USER_INTERFACE))
325 rCommandArgs.push_back(
"--mapi-dialog");
327 if (!(aFlag & NO_LOGON_DIALOG))
328 rCommandArgs.push_back(
"--mapi-logon-ui");
330 rCommandArgs.push_back(
"--langtag");
331 rCommandArgs.push_back(
SvtSysLocale().GetUILanguageTag().getBcp47());
333 rtl::Bootstrap aBootstrap;
334 OUString sBootstrapPath;
335 aBootstrap.getIniName(sBootstrapPath);
336 if (!sBootstrapPath.isEmpty())
338 rCommandArgs.push_back(
"--bootstrap");
339 rCommandArgs.push_back(sBootstrapPath);
345 const Reference<XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag)
349 std::vector<OUString> senddocParams;
352 const bool bWait = aFlag & NO_USER_INTERFACE;
353 if (!executeSenddoc(senddocParams, bWait))
356 static_cast<XSimpleMailClient*
>(
this));
363 const Reference<XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag )
365 if (!xSimpleMailMessage.is())
366 throw IllegalArgumentException(
367 "Empty mail message reference",
368 static_cast<XSimpleMailClient*
>(
this),
371 OSL_ENSURE(!(aFlag & NO_LOGON_DIALOG),
"Flag NO_LOGON_DIALOG has currently no effect");
374 if (aFlag < 0 || aFlag > 3)
375 throw IllegalArgumentException(
376 "Invalid flag value",
377 static_cast<XSimpleMailClient*
>(
this),
381 if ((aFlag & NO_USER_INTERFACE) && !xSimpleMailMessage->getRecipient().getLength())
382 throw IllegalArgumentException(
383 "No recipient specified",
384 static_cast<XSimpleMailClient*
>(
this),
OUString CopyAttachment(const OUString &sOrigAttachURL, OUString &sUserVisibleName, bool &nodelete)
std::vector< std::unique_ptr< utl::TempFileNamed > > maAttachmentFiles
virtual void SAL_CALL sendSimpleMailMessage(const css::uno::Reference< css::system::XSimpleMailMessage > &xSimpleMailMessage, sal_Int32 aFlag) override
void ReleaseAttachments()
virtual css::uno::Reference< css::system::XSimpleMailMessage > SAL_CALL createSimpleMailMessage() override
void assembleCommandLine(const css::uno::Reference< css::system::XSimpleMailMessage > &xSimpleMailMessage, sal_Int32 aFlag, std::vector< OUString > &rCommandArgs)
Assemble a command line for SendDoc.exe out of the members of the supplied SimpleMailMessage.
void validateParameter(const css::uno::Reference< css::system::XSimpleMailMessage > &xSimpleMailMessage, sal_Int32 aFlag)
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
const OUString & GetTempPath() const