Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sfx2/viewfrm.hxx>
21 : #include <sfx2/dispatch.hxx>
22 :
23 : #include <cmdid.h>
24 : #include <swmodule.hxx>
25 : #include <view.hxx>
26 : #include <edtwin.hxx>
27 : #include <vcl/layout.hxx>
28 : #include <vcl/svapp.hxx>
29 : #include <mailmergechildwindow.hxx>
30 : #include <../../../ui/dbui/mmoutputpage.hxx>
31 : #include <mmconfigitem.hxx>
32 : #include <comphelper/processfactory.hxx>
33 : #include <comphelper/string.hxx>
34 : #include "com/sun/star/mail/XSmtpService.hpp"
35 : #include "com/sun/star/mail/MailServiceType.hpp"
36 : #include <rtl/ref.hxx>
37 : #include <maildispatcher.hxx>
38 : #include <imaildsplistener.hxx>
39 : #include <swunohelper.hxx>
40 : #include <svtools/svmedit.hxx>
41 : #include <vcl/msgbox.hxx>
42 : #include <vcl/image.hxx>
43 : #include <mailmergechildwindow.hrc>
44 : #include <dbui.hrc>
45 : #include <helpid.h>
46 :
47 : using namespace ::com::sun::star;
48 :
49 0 : SFX_IMPL_FLOATINGWINDOW( SwMailMergeChildWindow, FN_MAILMERGE_CHILDWINDOW )
50 :
51 0 : SwMailMergeChildWindow::SwMailMergeChildWindow( Window* _pParent,
52 : sal_uInt16 nId,
53 : SfxBindings* pBindings,
54 : SfxChildWinInfo* pInfo ) :
55 0 : SfxChildWindow( _pParent, nId )
56 : {
57 0 : pWindow = new SwMailMergeChildWin( pBindings, this, _pParent);
58 :
59 0 : if (!pInfo->aSize.Width() || !pInfo->aSize.Height())
60 : {
61 0 : SwView* pActiveView = ::GetActiveView();
62 0 : if(pActiveView)
63 : {
64 0 : const SwEditWin &rEditWin = pActiveView->GetEditWin();
65 0 : pWindow->SetPosPixel(rEditWin.OutputToScreenPixel(Point(0, 0)));
66 : }
67 : else
68 0 : pWindow->SetPosPixel(_pParent->OutputToScreenPixel(Point(0, 0)));
69 0 : pInfo->aPos = pWindow->GetPosPixel();
70 0 : pInfo->aSize = pWindow->GetSizePixel();
71 : }
72 :
73 0 : ((SwMailMergeChildWin *)pWindow)->Initialize(pInfo);
74 0 : pWindow->Show();
75 0 : }
76 :
77 0 : SwMailMergeChildWin::SwMailMergeChildWin( SfxBindings* _pBindings,
78 : SfxChildWindow* pChild,
79 : Window *pParent) :
80 : SfxFloatingWindow(_pBindings, pChild, pParent, SW_RES(DLG_MAILMERGECHILD)),
81 0 : m_aBackTB(this, SW_RES( TB_BACK ))
82 : {
83 0 : m_aBackTB.SetSelectHdl(LINK(this, SwMailMergeChildWin, BackHdl));
84 0 : sal_uInt16 nIResId = ILIST_TBX;
85 0 : ResId aResId( nIResId, *pSwResMgr );
86 0 : ImageList aIList(aResId);
87 0 : FreeResource();
88 :
89 0 : m_aBackTB.SetItemImage( 1, aIList.GetImage(FN_SHOW_ROOT) );
90 0 : m_aBackTB.SetButtonType( BUTTON_SYMBOLTEXT );
91 0 : Size aSz = m_aBackTB.CalcWindowSizePixel(1);
92 0 : m_aBackTB.SetPosSizePixel( Point(), aSz );
93 0 : SetOutputSizePixel(aSz);
94 0 : m_aBackTB.Show();
95 0 : }
96 :
97 0 : SwMailMergeChildWin::~SwMailMergeChildWin()
98 : {
99 0 : }
100 :
101 0 : IMPL_LINK_NOARG(SwMailMergeChildWin, BackHdl)
102 : {
103 0 : GetBindings().GetDispatcher()->Execute(FN_MAILMERGE_WIZARD, SFX_CALLMODE_ASYNCHRON);
104 0 : return 0;
105 : }
106 :
107 0 : void SwMailMergeChildWin::FillInfo(SfxChildWinInfo& rInfo) const
108 : {
109 0 : SfxFloatingWindow::FillInfo(rInfo);
110 0 : rInfo.aWinState = OString();
111 0 : rInfo.bVisible = false;
112 0 : }
113 :
114 : struct SwSendMailDialog_Impl
115 : {
116 : friend class SwSendMailDialog;
117 : ::osl::Mutex aDescriptorMutex;
118 :
119 : ::std::vector< SwMailDescriptor > aDescriptors;
120 : sal_uInt32 nCurrentDescriptor;
121 : sal_uInt32 nDocumentCount;
122 : ::rtl::Reference< MailDispatcher > xMailDispatcher;
123 : ::rtl::Reference< IMailDispatcherListener> xMailListener;
124 : uno::Reference< mail::XMailService > xConnectedMailService;
125 : uno::Reference< mail::XMailService > xConnectedInMailService;
126 : Timer aRemoveTimer;
127 :
128 0 : SwSendMailDialog_Impl() :
129 : nCurrentDescriptor(0),
130 0 : nDocumentCount(0)
131 : {
132 0 : aRemoveTimer.SetTimeout(500);
133 0 : }
134 :
135 0 : ~SwSendMailDialog_Impl()
136 0 : {
137 : // Shutdown must be called when the last reference to the
138 : // mail dispatcher will be released in order to force a
139 : // shutdown of the mail dispatcher thread.
140 : // 'join' with the mail dispatcher thread leads to a
141 : // deadlock (SolarMutex).
142 0 : if( xMailDispatcher.is() && !xMailDispatcher->isShutdownRequested() )
143 0 : xMailDispatcher->shutdown();
144 0 : }
145 : const SwMailDescriptor* GetNextDescriptor();
146 : };
147 :
148 0 : const SwMailDescriptor* SwSendMailDialog_Impl::GetNextDescriptor()
149 : {
150 0 : ::osl::MutexGuard aGuard(aDescriptorMutex);
151 0 : if(nCurrentDescriptor < aDescriptors.size())
152 : {
153 0 : ++nCurrentDescriptor;
154 0 : return &aDescriptors[nCurrentDescriptor - 1];
155 : }
156 0 : return 0;
157 : }
158 :
159 : class SwMailDispatcherListener_Impl : public IMailDispatcherListener
160 : {
161 : SwSendMailDialog* m_pSendMailDialog;
162 :
163 : public:
164 : SwMailDispatcherListener_Impl(SwSendMailDialog& rParentDlg);
165 : virtual ~SwMailDispatcherListener_Impl();
166 :
167 : virtual void started(::rtl::Reference<MailDispatcher> xMailDispatcher) SAL_OVERRIDE;
168 : virtual void stopped(::rtl::Reference<MailDispatcher> xMailDispatcher) SAL_OVERRIDE;
169 : virtual void idle(::rtl::Reference<MailDispatcher> xMailDispatcher) SAL_OVERRIDE;
170 : virtual void mailDelivered(::rtl::Reference<MailDispatcher> xMailDispatcher,
171 : uno::Reference< mail::XMailMessage> xMailMessage) SAL_OVERRIDE;
172 : virtual void mailDeliveryError(::rtl::Reference<MailDispatcher> xMailDispatcher,
173 : uno::Reference< mail::XMailMessage> xMailMessage, const OUString& sErrorMessage) SAL_OVERRIDE;
174 :
175 : static void DeleteAttachments( uno::Reference< mail::XMailMessage >& xMessage );
176 : };
177 :
178 0 : SwMailDispatcherListener_Impl::SwMailDispatcherListener_Impl(SwSendMailDialog& rParentDlg) :
179 0 : m_pSendMailDialog(&rParentDlg)
180 : {
181 0 : }
182 :
183 0 : SwMailDispatcherListener_Impl::~SwMailDispatcherListener_Impl()
184 : {
185 0 : }
186 :
187 0 : void SwMailDispatcherListener_Impl::started(::rtl::Reference<MailDispatcher> /*xMailDispatcher*/)
188 : {
189 0 : }
190 :
191 0 : void SwMailDispatcherListener_Impl::stopped(
192 : ::rtl::Reference<MailDispatcher> /*xMailDispatcher*/)
193 : {
194 0 : }
195 :
196 0 : void SwMailDispatcherListener_Impl::idle(::rtl::Reference<MailDispatcher> /*xMailDispatcher*/)
197 : {
198 0 : SolarMutexGuard aGuard;
199 0 : m_pSendMailDialog->AllMailsSent();
200 0 : }
201 :
202 0 : void SwMailDispatcherListener_Impl::mailDelivered(
203 : ::rtl::Reference<MailDispatcher> /*xMailDispatcher*/,
204 : uno::Reference< mail::XMailMessage> xMailMessage)
205 : {
206 0 : SolarMutexGuard aGuard;
207 0 : m_pSendMailDialog->DocumentSent( xMailMessage, true, 0 );
208 0 : DeleteAttachments( xMailMessage );
209 0 : }
210 :
211 0 : void SwMailDispatcherListener_Impl::mailDeliveryError(
212 : ::rtl::Reference<MailDispatcher> /*xMailDispatcher*/,
213 : uno::Reference< mail::XMailMessage> xMailMessage,
214 : const OUString& sErrorMessage)
215 : {
216 0 : SolarMutexGuard aGuard;
217 0 : m_pSendMailDialog->DocumentSent( xMailMessage, false, &sErrorMessage );
218 0 : DeleteAttachments( xMailMessage );
219 0 : }
220 :
221 0 : void SwMailDispatcherListener_Impl::DeleteAttachments( uno::Reference< mail::XMailMessage >& xMessage )
222 : {
223 0 : uno::Sequence< mail::MailAttachment > aAttachments = xMessage->getAttachments();
224 :
225 0 : for(sal_Int32 nFile = 0; nFile < aAttachments.getLength(); ++nFile)
226 : {
227 : try
228 : {
229 0 : uno::Reference< beans::XPropertySet > xTransferableProperties( aAttachments[nFile].Data, uno::UNO_QUERY_THROW);
230 0 : if( xTransferableProperties.is() )
231 : {
232 0 : OUString sURL;
233 0 : xTransferableProperties->getPropertyValue("URL") >>= sURL;
234 0 : if(!sURL.isEmpty())
235 0 : SWUnoHelper::UCB_DeleteFile( sURL );
236 0 : }
237 : }
238 0 : catch (const uno::Exception&)
239 : {
240 : }
241 0 : }
242 0 : }
243 :
244 0 : class SwSendWarningBox_Impl : public MessageDialog
245 : {
246 : VclMultiLineEdit *m_pDetailED;
247 : public:
248 : SwSendWarningBox_Impl(Window* pParent, const OUString& rDetails);
249 : };
250 :
251 0 : SwSendWarningBox_Impl::SwSendWarningBox_Impl(Window* pParent, const OUString& rDetails)
252 0 : : MessageDialog(pParent, "WarnEmailDialog", "modules/swriter/ui/warnemaildialog.ui")
253 : {
254 0 : get(m_pDetailED, "errors");
255 0 : m_pDetailED->SetMaxTextWidth(80 * m_pDetailED->approximate_char_width());
256 0 : m_pDetailED->set_width_request(80 * m_pDetailED->approximate_char_width());
257 0 : m_pDetailED->set_height_request(8 * m_pDetailED->GetTextHeight());
258 0 : m_pDetailED->SetText(rDetails);
259 0 : }
260 :
261 : #define ITEMID_TASK 1
262 : #define ITEMID_STATUS 2
263 :
264 0 : SwSendMailDialog::SwSendMailDialog(Window *pParent, SwMailMergeConfigItem& rConfigItem) :
265 : ModelessDialog /*SfxModalDialog*/(pParent, SW_RES(DLG_MM_SENDMAILS)),
266 : m_aStatusFL( this, SW_RES( FL_STATUS )),
267 : m_aStatusFT( this, SW_RES( FT_STATUS1 )),
268 : m_aTransferStatusFL( this, SW_RES( FL_TRANSFERSTATUS )),
269 : m_aTransferStatusFT( this, SW_RES( FT_TRANSFERSTATUS )),
270 : m_PausedFI(this, SW_RES( FI_PAUSED )),
271 : m_aProgressBar( this, SW_RES( PB_PROGRESS )),
272 : m_aErrorStatusFT( this, SW_RES( FT_ERRORSTATUS )),
273 : m_aDetailsPB( this, SW_RES( PB_DETAILS )),
274 : m_aStatusHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER ),
275 : m_aStatusLB( this, SW_RES( LB_STATUS )),
276 : m_aSeparatorFL( this, SW_RES( FL_SEPARATOR )),
277 : m_aStopPB( this, SW_RES( PB_STOP )),
278 : m_aClosePB( this, SW_RES( PB_CLOSE )),
279 : m_sMore(m_aDetailsPB.GetText()),
280 : m_sLess(SW_RES(ST_LESS)),
281 : m_sContinue(SW_RES( ST_CONTINUE )),
282 : m_sStop(m_aStopPB.GetText()),
283 : m_sSend(SW_RES(ST_SEND)),
284 : m_sTransferStatus(m_aTransferStatusFT.GetText()),
285 : m_sErrorStatus( m_aErrorStatusFT.GetText()),
286 : m_sSendingTo( SW_RES(ST_SENDINGTO )),
287 : m_sCompleted( SW_RES(ST_COMPLETED )),
288 : m_sFailed( SW_RES(ST_FAILED )),
289 : m_sTerminateQuery( SW_RES( ST_TERMINATEQUERY )),
290 : m_bCancel(false),
291 : m_bDesctructionEnabled(false),
292 : m_aImageList( SW_RES( ILIST ) ),
293 0 : m_pImpl(new SwSendMailDialog_Impl),
294 : m_pConfigItem(&rConfigItem),
295 : m_nSendCount(0),
296 0 : m_nErrorCount(0)
297 : {
298 0 : m_nStatusHeight = m_aSeparatorFL.GetPosPixel().Y() - m_aStatusLB.GetPosPixel().Y();
299 0 : OUString sTask(SW_RES(ST_TASK));
300 0 : OUString sStatus(SW_RES(ST_STATUS));
301 0 : m_aStatusLB.SetHelpId(HID_MM_SENDMAILS_STATUSLB);
302 :
303 0 : FreeResource();
304 :
305 0 : m_aDetailsPB.SetClickHdl(LINK( this, SwSendMailDialog, DetailsHdl_Impl));
306 0 : m_aStopPB.SetClickHdl(LINK( this, SwSendMailDialog, StopHdl_Impl));
307 0 : m_aClosePB.SetClickHdl(LINK( this, SwSendMailDialog, CloseHdl_Impl));
308 :
309 0 : Size aLBSize(m_aStatusLB.GetSizePixel());
310 0 : m_aStatusHB.SetSizePixel(aLBSize);
311 0 : Size aHeadSize(m_aStatusHB.CalcWindowSizePixel());
312 0 : aHeadSize.Width() = aLBSize.Width();
313 0 : m_aStatusHB.SetSizePixel(aHeadSize);
314 0 : Point aLBPos(m_aStatusLB.GetPosPixel());
315 0 : m_aStatusHB.SetPosPixel(aLBPos);
316 0 : aLBPos.Y() += aHeadSize.Height();
317 0 : aLBSize.Height() -= aHeadSize.Height();
318 0 : m_aStatusLB.SetPosSizePixel(aLBPos, aLBSize);
319 :
320 0 : Size aSz(m_aStatusHB.GetOutputSizePixel());
321 0 : long nPos1 = aSz.Width()/3 * 2;
322 0 : long nPos2 = aSz.Width()/3;
323 : m_aStatusHB.InsertItem( ITEMID_TASK, sTask,
324 : nPos1,
325 0 : HIB_LEFT | HIB_VCENTER );
326 : m_aStatusHB.InsertItem( ITEMID_STATUS, sStatus,
327 : nPos2,
328 0 : HIB_LEFT | HIB_VCENTER );
329 0 : m_aStatusHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
330 0 : m_aStatusHB.Show();
331 :
332 0 : m_aStatusLB.SetHelpId(HID_MM_MAILSTATUS_TLB);
333 0 : static long nTabs[] = {3, 0, nPos1, aSz.Width() };
334 0 : m_aStatusLB.SetStyle( m_aStatusLB.GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
335 0 : m_aStatusLB.SetSelectionMode( SINGLE_SELECTION );
336 0 : m_aStatusLB.SetTabs(&nTabs[0], MAP_PIXEL);
337 0 : m_aStatusLB.SetSpaceBetweenEntries(3);
338 :
339 0 : DetailsHdl_Impl( &m_aDetailsPB );
340 0 : UpdateTransferStatus();
341 0 : }
342 :
343 0 : SwSendMailDialog::~SwSendMailDialog()
344 : {
345 0 : if(m_pImpl->xMailDispatcher.is())
346 : {
347 : try
348 : {
349 0 : if(m_pImpl->xMailDispatcher->isStarted())
350 0 : m_pImpl->xMailDispatcher->stop();
351 0 : if(m_pImpl->xConnectedMailService.is() && m_pImpl->xConnectedMailService->isConnected())
352 0 : m_pImpl->xConnectedMailService->disconnect();
353 0 : if(m_pImpl->xConnectedInMailService.is() && m_pImpl->xConnectedInMailService->isConnected())
354 0 : m_pImpl->xConnectedInMailService->disconnect();
355 :
356 : uno::Reference<mail::XMailMessage> xMessage =
357 0 : m_pImpl->xMailDispatcher->dequeueMailMessage();
358 0 : while(xMessage.is())
359 : {
360 0 : SwMailDispatcherListener_Impl::DeleteAttachments( xMessage );
361 0 : xMessage = m_pImpl->xMailDispatcher->dequeueMailMessage();
362 0 : }
363 : }
364 0 : catch (const uno::Exception&)
365 : {
366 : }
367 : }
368 0 : delete m_pImpl;
369 0 : }
370 :
371 0 : void SwSendMailDialog::AddDocument( SwMailDescriptor& rDesc )
372 : {
373 0 : ::osl::MutexGuard aGuard(m_pImpl->aDescriptorMutex);
374 0 : m_pImpl->aDescriptors.push_back(rDesc);
375 : // if the dialog is already running then continue sending of documents
376 0 : if(m_pImpl->xMailDispatcher.is())
377 : {
378 0 : IterateMails();
379 0 : }
380 :
381 0 : }
382 :
383 0 : void SwSendMailDialog::SetDocumentCount( sal_Int32 nAllDocuments )
384 : {
385 0 : m_pImpl->nDocumentCount = nAllDocuments;
386 0 : UpdateTransferStatus();
387 0 : }
388 :
389 0 : static void lcl_Move(Control& rCtrl, long nYOffset)
390 : {
391 0 : Point aPos(rCtrl.GetPosPixel());
392 0 : aPos.Y() += nYOffset;
393 0 : rCtrl.SetPosPixel(aPos);
394 0 : }
395 :
396 0 : IMPL_LINK_NOARG(SwSendMailDialog, DetailsHdl_Impl)
397 : {
398 0 : long nMove = 0;
399 0 : if(m_aStatusLB.IsVisible())
400 : {
401 0 : m_aStatusLB.Hide();
402 0 : m_aStatusHB.Hide();
403 0 : nMove = - m_nStatusHeight;
404 0 : m_aDetailsPB.SetText(m_sMore);
405 : }
406 : else
407 : {
408 0 : m_aStatusLB.Show();
409 0 : m_aStatusHB.Show();
410 0 : nMove = m_nStatusHeight;
411 0 : m_aDetailsPB.SetText(m_sLess);
412 : }
413 0 : lcl_Move(m_aSeparatorFL, nMove);
414 0 : lcl_Move(m_aStopPB, nMove);
415 0 : lcl_Move(m_aClosePB, nMove);
416 0 : Size aDlgSize = GetSizePixel(); aDlgSize.Height() += nMove; SetSizePixel(aDlgSize);
417 :
418 0 : return 0;
419 : }
420 :
421 0 : IMPL_LINK( SwSendMailDialog, StopHdl_Impl, PushButton*, pButton )
422 : {
423 0 : m_bCancel = true;
424 0 : if(m_pImpl->xMailDispatcher.is())
425 : {
426 0 : if(m_pImpl->xMailDispatcher->isStarted())
427 : {
428 0 : m_pImpl->xMailDispatcher->stop();
429 0 : pButton->SetText(m_sContinue);
430 0 : m_PausedFI.Show();
431 : }
432 : else
433 : {
434 0 : m_pImpl->xMailDispatcher->start();
435 0 : pButton->SetText(m_sStop);
436 0 : m_PausedFI.Show(false);
437 : }
438 : }
439 0 : return 0;
440 : }
441 :
442 0 : IMPL_LINK_NOARG(SwSendMailDialog, CloseHdl_Impl)
443 : {
444 0 : ModelessDialog::Show( false );
445 0 : return 0;
446 : }
447 :
448 0 : IMPL_STATIC_LINK_NOINSTANCE( SwSendMailDialog, StartSendMails, SwSendMailDialog*, pDialog )
449 : {
450 0 : pDialog->SendMails();
451 0 : return 0;
452 : }
453 :
454 0 : IMPL_STATIC_LINK( SwSendMailDialog, RemoveThis, Timer*, pTimer )
455 : {
456 0 : if( pThis->m_pImpl->xMailDispatcher.is() )
457 : {
458 0 : if(pThis->m_pImpl->xMailDispatcher->isStarted())
459 0 : pThis->m_pImpl->xMailDispatcher->stop();
460 0 : if(!pThis->m_pImpl->xMailDispatcher->isShutdownRequested())
461 0 : pThis->m_pImpl->xMailDispatcher->shutdown();
462 : }
463 :
464 0 : if( pThis->m_bDesctructionEnabled &&
465 0 : (!pThis->m_pImpl->xMailDispatcher.is() ||
466 0 : !pThis->m_pImpl->xMailDispatcher->isRunning()))
467 : {
468 0 : delete pThis;
469 : }
470 : else
471 : {
472 0 : pTimer->Start();
473 : }
474 0 : return 0;
475 : }
476 :
477 0 : IMPL_STATIC_LINK_NOINSTANCE( SwSendMailDialog, StopSendMails, SwSendMailDialog*, pDialog )
478 : {
479 0 : if(pDialog->m_pImpl->xMailDispatcher.is() &&
480 0 : pDialog->m_pImpl->xMailDispatcher->isStarted())
481 : {
482 0 : pDialog->m_pImpl->xMailDispatcher->stop();
483 0 : pDialog->m_aStopPB.SetText(pDialog->m_sContinue);
484 0 : pDialog->m_PausedFI.Show();
485 : }
486 0 : return 0;
487 : }
488 :
489 0 : void SwSendMailDialog::SendMails()
490 : {
491 0 : if(!m_pConfigItem)
492 : {
493 : OSL_FAIL("config item not set");
494 0 : return;
495 : }
496 0 : EnterWait();
497 : //get a mail server connection
498 : uno::Reference< mail::XSmtpService > xSmtpServer =
499 : SwMailMergeHelper::ConnectToSmtpServer( *m_pConfigItem,
500 : m_pImpl->xConnectedInMailService,
501 0 : aEmptyOUStr, aEmptyOUStr, this );
502 0 : bool bIsLoggedIn = xSmtpServer.is() && xSmtpServer->isConnected();
503 0 : LeaveWait();
504 0 : if(!bIsLoggedIn)
505 : {
506 : OSL_FAIL("create error message");
507 0 : return;
508 : }
509 0 : m_pImpl->xMailDispatcher.set( new MailDispatcher(xSmtpServer));
510 0 : IterateMails();
511 0 : m_pImpl->xMailListener = new SwMailDispatcherListener_Impl(*this);
512 0 : m_pImpl->xMailDispatcher->addListener(m_pImpl->xMailListener);
513 0 : if(!m_bCancel)
514 : {
515 0 : m_pImpl->xMailDispatcher->start();
516 0 : }
517 : }
518 :
519 0 : void SwSendMailDialog::IterateMails()
520 : {
521 0 : const SwMailDescriptor* pCurrentMailDescriptor = m_pImpl->GetNextDescriptor();
522 0 : while( pCurrentMailDescriptor )
523 : {
524 0 : if(!SwMailMergeHelper::CheckMailAddress( pCurrentMailDescriptor->sEMail ))
525 : {
526 0 : Image aInsertImg = m_aImageList.GetImage( FN_FORMULA_CANCEL );
527 :
528 0 : OUString sMessage = m_sSendingTo;
529 0 : OUString sTmp(pCurrentMailDescriptor->sEMail);
530 0 : sTmp += "\t";
531 0 : sTmp += m_sFailed;
532 0 : m_aStatusLB.InsertEntry( sMessage.replaceFirst("%1", sTmp), aInsertImg, aInsertImg);
533 0 : ++m_nSendCount;
534 0 : ++m_nErrorCount;
535 0 : UpdateTransferStatus( );
536 0 : pCurrentMailDescriptor = m_pImpl->GetNextDescriptor();
537 0 : continue;
538 : }
539 0 : SwMailMessage* pMessage = new SwMailMessage;
540 0 : uno::Reference< mail::XMailMessage > xMessage = pMessage;
541 0 : if(m_pConfigItem->IsMailReplyTo())
542 0 : pMessage->setReplyToAddress(m_pConfigItem->GetMailReplyTo());
543 0 : pMessage->addRecipient( pCurrentMailDescriptor->sEMail );
544 0 : pMessage->SetSenderName( m_pConfigItem->GetMailDisplayName() );
545 0 : pMessage->SetSenderAddress( m_pConfigItem->GetMailAddress() );
546 0 : if(!pCurrentMailDescriptor->sAttachmentURL.isEmpty())
547 : {
548 0 : mail::MailAttachment aAttach;
549 0 : aAttach.Data =
550 : new SwMailTransferable(
551 : pCurrentMailDescriptor->sAttachmentURL,
552 : pCurrentMailDescriptor->sAttachmentName,
553 0 : pCurrentMailDescriptor->sMimeType );
554 0 : aAttach.ReadableName = pCurrentMailDescriptor->sAttachmentName;
555 0 : pMessage->addAttachment( aAttach );
556 : }
557 0 : pMessage->setSubject( pCurrentMailDescriptor->sSubject );
558 : uno::Reference< datatransfer::XTransferable> xBody =
559 : new SwMailTransferable(
560 : pCurrentMailDescriptor->sBodyContent,
561 0 : pCurrentMailDescriptor->sBodyMimeType);
562 0 : pMessage->setBody( xBody );
563 :
564 : //CC and BCC are tokenized by ';'
565 0 : if(!pCurrentMailDescriptor->sCC.isEmpty())
566 : {
567 0 : OUString sTokens( pCurrentMailDescriptor->sCC );
568 0 : sal_uInt16 nTokens = comphelper::string::getTokenCount(sTokens, ';');
569 0 : sal_Int32 nPos = 0;
570 0 : for( sal_uInt16 nToken = 0; nToken < nTokens; ++nToken)
571 : {
572 0 : OUString sTmp = sTokens.getToken( 0, ';', nPos);
573 0 : if( !sTmp.isEmpty() )
574 0 : pMessage->addCcRecipient( sTmp );
575 0 : }
576 : }
577 0 : if(!pCurrentMailDescriptor->sBCC.isEmpty())
578 : {
579 0 : OUString sTokens( pCurrentMailDescriptor->sBCC );
580 0 : sal_uInt16 nTokens = comphelper::string::getTokenCount(sTokens, ';');
581 0 : sal_Int32 nPos = 0;
582 0 : for( sal_uInt16 nToken = 0; nToken < nTokens; ++nToken)
583 : {
584 0 : OUString sTmp = sTokens.getToken( 0, ';', nPos);
585 0 : if( !sTmp.isEmpty() )
586 0 : pMessage->addBccRecipient( sTmp );
587 0 : }
588 : }
589 0 : m_pImpl->xMailDispatcher->enqueueMailMessage( xMessage );
590 0 : pCurrentMailDescriptor = m_pImpl->GetNextDescriptor();
591 0 : }
592 0 : UpdateTransferStatus();
593 0 : }
594 :
595 0 : void SwSendMailDialog::ShowDialog()
596 : {
597 : Application::PostUserEvent( STATIC_LINK( this, SwSendMailDialog,
598 0 : StartSendMails ), this );
599 0 : ModelessDialog::Show();
600 0 : }
601 :
602 0 : void SwSendMailDialog::StateChanged( StateChangedType nStateChange )
603 : {
604 0 : ModelessDialog::StateChanged( nStateChange );
605 0 : if(STATE_CHANGE_VISIBLE == nStateChange && !IsVisible())
606 : {
607 : m_pImpl->aRemoveTimer.SetTimeoutHdl( STATIC_LINK( this, SwSendMailDialog,
608 0 : RemoveThis ) );
609 0 : m_pImpl->aRemoveTimer.Start();
610 : }
611 0 : }
612 :
613 0 : void SwSendMailDialog::DocumentSent( uno::Reference< mail::XMailMessage> xMessage,
614 : bool bResult,
615 : const OUString* pError )
616 : {
617 : //sending should stop on send errors
618 0 : if(pError &&
619 0 : m_pImpl->xMailDispatcher.is() && m_pImpl->xMailDispatcher->isStarted())
620 : {
621 : Application::PostUserEvent( STATIC_LINK( this, SwSendMailDialog,
622 0 : StopSendMails ), this );
623 : }
624 0 : Image aInsertImg = m_aImageList.GetImage( bResult ? FN_FORMULA_APPLY : FN_FORMULA_CANCEL );
625 :
626 0 : OUString sMessage = m_sSendingTo;
627 0 : OUString sTmp(xMessage->getRecipients()[0]);
628 0 : sTmp += "\t";
629 0 : sTmp += bResult ? m_sCompleted : m_sFailed;
630 0 : m_aStatusLB.InsertEntry( sMessage.replaceFirst("%1", sTmp), aInsertImg, aInsertImg);
631 0 : ++m_nSendCount;
632 0 : if(!bResult)
633 0 : ++m_nErrorCount;
634 :
635 0 : UpdateTransferStatus( );
636 :
637 0 : if (pError)
638 : {
639 0 : SwSendWarningBox_Impl* pDlg = new SwSendWarningBox_Impl(0, *pError);
640 0 : pDlg->Execute();
641 0 : delete pDlg;
642 0 : }
643 0 : }
644 :
645 0 : void SwSendMailDialog::UpdateTransferStatus()
646 : {
647 0 : OUString sStatus( m_sTransferStatus );
648 0 : sStatus = sStatus.replaceFirst("%1", OUString::number(m_nSendCount) );
649 0 : sStatus = sStatus.replaceFirst("%2", OUString::number(m_pImpl->aDescriptors.size()));
650 0 : m_aTransferStatusFT.SetText(sStatus);
651 :
652 0 : sStatus = m_sErrorStatus.replaceFirst("%1", OUString::number(m_nErrorCount) );
653 0 : m_aErrorStatusFT.SetText(sStatus);
654 :
655 0 : if(m_pImpl->aDescriptors.size())
656 0 : m_aProgressBar.SetValue((sal_uInt16)(m_nSendCount * 100 / m_pImpl->aDescriptors.size()));
657 : else
658 0 : m_aProgressBar.SetValue(0);
659 0 : }
660 :
661 0 : void SwSendMailDialog::AllMailsSent()
662 : {
663 0 : m_aStopPB.Enable(false);
664 0 : }
665 :
666 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|