merge the formfield patch from ooo-build
[ooovba.git] / shell / source / win32 / simplemail / smplmailclient.cxx
blobffdc8d4f5d7510ffb6c32a166be6d1418d4be302
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: smplmailclient.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
33 #include <osl/diagnose.h>
34 #include <osl/process.h>
35 #include <rtl/bootstrap.hxx>
36 #include "smplmailclient.hxx"
37 #include "smplmailmsg.hxx"
38 #include <com/sun/star/system/SimpleMailClientFlags.hpp>
39 #include <osl/file.hxx>
41 #define WIN32_LEAN_AND_MEAN
42 #if defined _MSC_VER
43 #pragma warning(push, 1)
44 #endif
45 #include <windows.h>
46 #include <mapi.h>
47 #if defined _MSC_VER
48 #pragma warning(pop)
49 #endif
51 #include <process.h>
52 #include <vector>
54 using css::uno::Reference;
55 using css::uno::Exception;
56 using css::uno::RuntimeException;
57 using css::uno::Sequence;
58 using css::lang::IllegalArgumentException;
60 using css::system::XSimpleMailClient;
61 using css::system::XSimpleMailMessage;
62 using css::system::SimpleMailClientFlags::NO_USER_INTERFACE;
63 using css::system::SimpleMailClientFlags::NO_LOGON_DIALOG;
65 typedef std::vector<rtl::OUString> StringList_t;
66 typedef StringList_t::const_iterator StringListIterator_t;
68 const rtl::OUString TO = rtl::OUString::createFromAscii("--to");
69 const rtl::OUString CC = rtl::OUString::createFromAscii("--cc");
70 const rtl::OUString BCC = rtl::OUString::createFromAscii("--bcc");
71 const rtl::OUString FROM = rtl::OUString::createFromAscii("--from");
72 const rtl::OUString SUBJECT = rtl::OUString::createFromAscii("--subject");
73 const rtl::OUString BODY = rtl::OUString::createFromAscii("--body");
74 const rtl::OUString ATTACH = rtl::OUString::createFromAscii("--attach");
75 const rtl::OUString FLAG_MAPI_DIALOG = rtl::OUString::createFromAscii("--mapi-dialog");
76 const rtl::OUString FLAG_MAPI_LOGON_UI = rtl::OUString::createFromAscii("--mapi-logon-ui");
78 namespace /* private */
80 /** @internal
81 look if an alternative program is configured
82 which should be used as senddoc executable */
83 rtl::OUString getAlternativeSenddocUrl()
85 rtl::OUString altSenddocUrl;
86 HKEY hkey;
87 LONG lret = RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\OpenOffice.org\\SendAsEMailClient", &hkey);
88 if (lret == ERROR_SUCCESS)
90 wchar_t buff[MAX_PATH];
91 LONG sz = sizeof(buff);
92 lret = RegQueryValueW(hkey, NULL, buff, &sz);
93 if (lret == ERROR_SUCCESS)
95 osl::FileBase::getFileURLFromSystemPath(reinterpret_cast<const sal_Unicode*>(buff), altSenddocUrl);
97 RegCloseKey(hkey);
99 return altSenddocUrl;
103 Returns the absolute file Url of the senddoc executable.
105 @returns
106 the absolute file Url of the senddoc executable. In case
107 of an error an empty string will be returned.
109 rtl::OUString getSenddocUrl()
111 rtl::OUString senddocUrl = getAlternativeSenddocUrl();
113 if (senddocUrl.getLength() == 0)
115 senddocUrl = rtl::OUString(
116 RTL_CONSTASCII_USTRINGPARAM(
117 "$OOO_BASE_DIR/program/senddoc.exe"));
118 rtl::Bootstrap::expandMacros(senddocUrl); //TODO: detect failure
120 return senddocUrl;
124 Execute Senddoc.exe which a MAPI wrapper.
126 @param rCommandArgs
127 [in] the arguments to be passed to Senddoc.exe
129 @returns
130 <TRUE/> on success.
132 bool executeSenddoc(const StringList_t& rCommandArgs)
134 rtl::OUString senddocUrl = getSenddocUrl();
135 if (senddocUrl.getLength() == 0)
136 return false;
138 oslProcess proc;
139 oslProcessError err = osl_Process_E_Unknown;
141 /* for efficiency reasons we are using a 'bad' cast here
142 as a vector or rtl::OUStrings is nothing else than
143 an array of pointers to rtl_uString's */
144 err = osl_executeProcess(
145 senddocUrl.pData,
146 (rtl_uString**)&rCommandArgs[0],
147 rCommandArgs.size(),
148 osl_Process_WAIT | osl_Process_DETACHED,
149 NULL,
150 NULL,
151 NULL,
153 &proc);
155 if (err != osl_Process_E_None)
156 return false;
158 oslProcessInfo procInfo;
159 procInfo.Size = sizeof(oslProcessInfo);
160 osl_getProcessInfo(proc, osl_Process_EXITCODE, &procInfo);
161 osl_freeProcessHandle(proc);
162 return (procInfo.Code == SUCCESS_SUCCESS);
164 } // namespace private
166 Reference<XSimpleMailMessage> SAL_CALL CSmplMailClient::createSimpleMailMessage()
167 throw (RuntimeException)
169 return Reference<XSimpleMailMessage>(new CSmplMailMsg());
173 Assemble a command line for SendDoc.exe out of the members
174 of the supplied SimpleMailMessage.
176 @param xSimpleMailMessage
177 [in] the mail message.
179 @param aFlags
180 [in] different flags to be used with the simple mail service.
182 @param rCommandArgs
183 [in|out] a buffer for the command line arguments. The buffer
184 is assumed to be empty.
186 @throws com::sun::star::lang::IllegalArgumentException
187 if an invalid file URL has been detected in the attachment list.
189 void CSmplMailClient::assembleCommandLine(
190 const Reference<XSimpleMailMessage>& xSimpleMailMessage,
191 sal_Int32 aFlag, StringList_t& rCommandArgs)
193 OSL_ENSURE(rCommandArgs.size() == 0, "Provided command argument buffer not empty");
195 rtl::OUString to = xSimpleMailMessage->getRecipient();
196 if (to.getLength() > 0)
198 rCommandArgs.push_back(TO);
199 rCommandArgs.push_back(to);
202 Sequence<rtl::OUString> ccRecipients = xSimpleMailMessage->getCcRecipient();
203 for (int i = 0; i < ccRecipients.getLength(); i++)
205 rCommandArgs.push_back(CC);
206 rCommandArgs.push_back(ccRecipients[i]);
209 Sequence<rtl::OUString> bccRecipients = xSimpleMailMessage->getBccRecipient();
210 for (int i = 0; i < bccRecipients.getLength(); i++)
212 rCommandArgs.push_back(BCC);
213 rCommandArgs.push_back(bccRecipients[i]);
216 rtl::OUString from = xSimpleMailMessage->getOriginator();
217 if (from.getLength() > 0)
219 rCommandArgs.push_back(FROM);
220 rCommandArgs.push_back(from);
223 rtl::OUString subject = xSimpleMailMessage->getSubject();
224 if (subject.getLength() > 0)
226 rCommandArgs.push_back(SUBJECT);
227 rCommandArgs.push_back(subject);
230 Sequence<rtl::OUString> attachments = xSimpleMailMessage->getAttachement();
231 for (int i = 0; i < attachments.getLength(); i++)
233 rtl::OUString sysPath;
234 osl::FileBase::RC err = osl::FileBase::getSystemPathFromFileURL(attachments[i], sysPath);
235 if (err != osl::FileBase::E_None)
236 throw IllegalArgumentException(
237 rtl::OUString::createFromAscii("Invalid attachment file URL"),
238 static_cast<XSimpleMailClient*>(this),
241 rCommandArgs.push_back(ATTACH);
242 rCommandArgs.push_back(sysPath);
245 if (!(aFlag & NO_USER_INTERFACE))
246 rCommandArgs.push_back(FLAG_MAPI_DIALOG);
248 if (!(aFlag & NO_LOGON_DIALOG))
249 rCommandArgs.push_back(FLAG_MAPI_LOGON_UI);
252 void SAL_CALL CSmplMailClient::sendSimpleMailMessage(
253 const Reference<XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag)
254 throw (IllegalArgumentException, Exception, RuntimeException)
256 validateParameter(xSimpleMailMessage, aFlag);
258 StringList_t senddocParams;
259 assembleCommandLine(xSimpleMailMessage, aFlag, senddocParams);
261 if (!executeSenddoc(senddocParams))
262 throw Exception(
263 rtl::OUString::createFromAscii("Send email failed"),
264 static_cast<XSimpleMailClient*>(this));
267 void CSmplMailClient::validateParameter(
268 const Reference<XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag )
270 if (!xSimpleMailMessage.is())
271 throw IllegalArgumentException(
272 rtl::OUString::createFromAscii("Empty mail message reference"),
273 static_cast<XSimpleMailClient*>(this),
276 // #93077#
277 OSL_ENSURE(!(aFlag & NO_LOGON_DIALOG), "Flag NO_LOGON_DIALOG has currently no effect");
279 // check the flags, the allowed range is 0 - (2^n - 1)
280 if (aFlag < 0 || aFlag > 3)
281 throw IllegalArgumentException(
282 rtl::OUString::createFromAscii("Invalid flag value"),
283 static_cast<XSimpleMailClient*>(this),
286 // check if a recipient is specified of the flags NO_USER_INTERFACE is specified
287 if ((aFlag & NO_USER_INTERFACE) && !xSimpleMailMessage->getRecipient().getLength())
288 throw IllegalArgumentException(
289 rtl::OUString::createFromAscii("No recipient specified"),
290 static_cast<XSimpleMailClient*>(this),