1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 
32 #include <sfx2/viewfrm.hxx>
33 #include <sfx2/dispatch.hxx>
34 
35 #include <cmdid.h>
36 #include <swmodule.hxx>
37 #include <view.hxx>
38 #include <edtwin.hxx>
39 #include <vcl/svapp.hxx>
40 #include <mailmergechildwindow.hxx>
41 #include <mmoutputpage.hxx>
42 #include <mmconfigitem.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include "com/sun/star/mail/XSmtpService.hpp"
45 #include "com/sun/star/mail/MailServiceType.hpp"
46 #include <rtl/ref.hxx>
47 #include <maildispatcher.hxx>
48 #include <imaildsplistener.hxx>
49 #include <swunohelper.hxx>
50 #include <svtools/svmedit.hxx>
51 #include <vcl/msgbox.hxx>
52 #include <vcl/image.hxx>
53 #include <mailmergechildwindow.hrc>
54 #include <dbui.hrc>
55 #include <helpid.h>
56 
57 using namespace ::com::sun::star;
58 
59 SFX_IMPL_FLOATINGWINDOW( SwMailMergeChildWindow, FN_MAILMERGE_CHILDWINDOW )
60 //SFX_IMPL_FLOATINGWINDOW( SwSendMailChildWindow, FN_MAILMERGE_SENDMAIL_CHILDWINDOW )
61 
62 /*------------------------------------------------------------------------
63 	Beschreibung:
64 ------------------------------------------------------------------------*/
65 
66 SwMailMergeChildWindow::SwMailMergeChildWindow( Window* _pParent,
67 								sal_uInt16 nId,
68 								SfxBindings* pBindings,
69 								SfxChildWinInfo* pInfo ) :
70                                 SfxChildWindow( _pParent, nId )
71 {
72     pWindow = new SwMailMergeChildWin( pBindings, this, _pParent);
73 
74 	if (!pInfo->aSize.Width() || !pInfo->aSize.Height())
75 	{
76         SwView* pActiveView = ::GetActiveView();
77         if(pActiveView)
78         {
79             const SwEditWin &rEditWin = pActiveView->GetEditWin();
80             pWindow->SetPosPixel(rEditWin.OutputToScreenPixel(Point(0, 0)));
81         }
82         else
83             pWindow->SetPosPixel(_pParent->OutputToScreenPixel(Point(0, 0)));
84 		pInfo->aPos = pWindow->GetPosPixel();
85 		pInfo->aSize = pWindow->GetSizePixel();
86 	}
87 
88     ((SwMailMergeChildWin *)pWindow)->Initialize(pInfo);
89 	pWindow->Show();
90 }
91 
92 /*------------------------------------------------------------------------
93 	Beschreibung:
94 ------------------------------------------------------------------------*/
95 
96 SwMailMergeChildWin::SwMailMergeChildWin( SfxBindings* _pBindings,
97 							SfxChildWindow* pChild,
98 							Window *pParent) :
99     SfxFloatingWindow(_pBindings, pChild, pParent, SW_RES(DLG_MAILMERGECHILD)),
100     m_aBackTB(this, SW_RES( TB_BACK ))
101 {
102     m_aBackTB.SetSelectHdl(LINK(this, SwMailMergeChildWin, BackHdl));
103     sal_uInt16 nIResId =  GetSettings().GetStyleSettings().GetHighContrastMode() ?
104 		ILIST_TBX_HC : ILIST_TBX;
105     ResId aResId( nIResId, *pSwResMgr );
106     ImageList aIList(aResId);
107     FreeResource();
108 
109     m_aBackTB.SetItemImage( 1, aIList.GetImage(FN_SHOW_ROOT) );
110     m_aBackTB.SetButtonType( BUTTON_SYMBOLTEXT );
111     Size aSz = m_aBackTB.CalcWindowSizePixel(1);
112     m_aBackTB.SetPosSizePixel( Point(), aSz );
113     SetOutputSizePixel(aSz);
114     m_aBackTB.Show();
115 }
116 
117 /*------------------------------------------------------------------------
118 	Beschreibung:
119 ------------------------------------------------------------------------*/
120 
121 SwMailMergeChildWin::~SwMailMergeChildWin()
122 {
123 }
124 
125 /*--------------------------------------------------------------------
126 	Beschreibung:
127  --------------------------------------------------------------------*/
128 
129 IMPL_LINK( SwMailMergeChildWin, BackHdl, ToolBox *, EMPTYARG )
130 {
131     GetBindings().GetDispatcher()->Execute(FN_MAILMERGE_WIZARD, SFX_CALLMODE_ASYNCHRON);
132 	return 0;
133 }
134 
135 /*-- 26.05.2004 10:12:44---------------------------------------------------
136 
137   -----------------------------------------------------------------------*/
138 void    SwMailMergeChildWin::FillInfo(SfxChildWinInfo& rInfo) const
139 {
140     SfxFloatingWindow::FillInfo(rInfo);
141     rInfo.aWinState.Erase();
142     rInfo.bVisible = sal_False;
143 }
144 /*-- 21.05.2004 14:07:37---------------------------------------------------
145 
146   -----------------------------------------------------------------------*/
147 struct SwSendMailDialog_Impl
148 {
149     friend class SwSendMailDialog;
150     ::osl::Mutex                                aDescriptorMutex;
151 
152     ::std::vector< SwMailDescriptor >           aDescriptors;
153     sal_uInt32                                  nCurrentDescriptor;
154     sal_uInt32                                  nDocumentCount;
155     ::rtl::Reference< MailDispatcher >          xMailDispatcher;
156     ::rtl::Reference< IMailDispatcherListener>  xMailListener;
157     uno::Reference< mail::XMailService >        xConnectedMailService;
158     uno::Reference< mail::XMailService >        xConnectedInMailService;
159     Timer                                       aRemoveTimer;
160 
161 
162     SwSendMailDialog_Impl() :
163         nCurrentDescriptor(0),
164         nDocumentCount(0)
165              {
166                 aRemoveTimer.SetTimeout(500);
167              }
168 
169     ~SwSendMailDialog_Impl()
170     {
171         // Shutdown must be called when the last reference to the
172         // mail dispatcher will be released in order to force a
173         // shutdown of the mail dispatcher thread.
174         // 'join' with the mail dispatcher thread leads to a
175         // deadlock (SolarMutex).
176         if( xMailDispatcher.is() && !xMailDispatcher->isShutdownRequested() )
177             xMailDispatcher->shutdown();
178     }
179     const SwMailDescriptor* GetNextDescriptor();
180 };
181 
182 const SwMailDescriptor* SwSendMailDialog_Impl::GetNextDescriptor()
183 {
184     ::osl::MutexGuard aGuard(aDescriptorMutex);
185     if(nCurrentDescriptor < aDescriptors.size())
186     {
187         ++nCurrentDescriptor;
188         return &aDescriptors[nCurrentDescriptor - 1];
189     }
190     return 0;
191 }
192 
193 /*-- 23.06.2004 10:19:55---------------------------------------------------
194 
195   -----------------------------------------------------------------------*/
196 class SwMailDispatcherListener_Impl : public IMailDispatcherListener
197 {
198     SwSendMailDialog* m_pSendMailDialog;
199 
200 public:
201     SwMailDispatcherListener_Impl(SwSendMailDialog& rParentDlg);
202     ~SwMailDispatcherListener_Impl();
203 
204     virtual void started(::rtl::Reference<MailDispatcher> xMailDispatcher);
205     virtual void stopped(::rtl::Reference<MailDispatcher> xMailDispatcher);
206     virtual void idle(::rtl::Reference<MailDispatcher> xMailDispatcher);
207     virtual void mailDelivered(::rtl::Reference<MailDispatcher> xMailDispatcher,
208                 uno::Reference< mail::XMailMessage> xMailMessage);
209     virtual void mailDeliveryError(::rtl::Reference<MailDispatcher> xMailDispatcher,
210                 uno::Reference< mail::XMailMessage> xMailMessage, const rtl::OUString& sErrorMessage);
211 
212     static void DeleteAttachments( uno::Reference< mail::XMailMessage >& xMessage );
213 };
214 
215 /*-- 23.06.2004 10:04:48---------------------------------------------------
216 
217   -----------------------------------------------------------------------*/
218 SwMailDispatcherListener_Impl::SwMailDispatcherListener_Impl(SwSendMailDialog& rParentDlg) :
219     m_pSendMailDialog(&rParentDlg)
220 {
221 }
222 /*-- 23.06.2004 10:04:48---------------------------------------------------
223 
224   -----------------------------------------------------------------------*/
225 SwMailDispatcherListener_Impl::~SwMailDispatcherListener_Impl()
226 {
227 }
228 /*-- 23.06.2004 10:04:49---------------------------------------------------
229 
230   -----------------------------------------------------------------------*/
231 void SwMailDispatcherListener_Impl::started(::rtl::Reference<MailDispatcher> /*xMailDispatcher*/)
232 {
233 }
234 /*-- 23.06.2004 10:04:49---------------------------------------------------
235 
236   -----------------------------------------------------------------------*/
237 void SwMailDispatcherListener_Impl::stopped(
238                         ::rtl::Reference<MailDispatcher> /*xMailDispatcher*/)
239 {
240 }
241 /*-- 23.06.2004 10:04:49---------------------------------------------------
242 
243   -----------------------------------------------------------------------*/
244 void SwMailDispatcherListener_Impl::idle(::rtl::Reference<MailDispatcher> /*xMailDispatcher*/)
245 {
246     vos::OGuard aGuard(Application::GetSolarMutex());
247     m_pSendMailDialog->AllMailsSent();
248 }
249 /*-- 23.06.2004 10:04:50---------------------------------------------------
250 
251   -----------------------------------------------------------------------*/
252 void SwMailDispatcherListener_Impl::mailDelivered(
253                         ::rtl::Reference<MailDispatcher> /*xMailDispatcher*/,
254                         uno::Reference< mail::XMailMessage> xMailMessage)
255 {
256     vos::OGuard aGuard(Application::GetSolarMutex());
257     m_pSendMailDialog->DocumentSent( xMailMessage, true, 0 );
258     DeleteAttachments( xMailMessage );
259 }
260 /*-- 23.06.2004 10:04:51---------------------------------------------------
261 
262   -----------------------------------------------------------------------*/
263 void SwMailDispatcherListener_Impl::mailDeliveryError(
264                 ::rtl::Reference<MailDispatcher> /*xMailDispatcher*/,
265                 uno::Reference< mail::XMailMessage> xMailMessage,
266                 const rtl::OUString& sErrorMessage)
267 {
268     vos::OGuard aGuard(Application::GetSolarMutex());
269     m_pSendMailDialog->DocumentSent( xMailMessage, false, &sErrorMessage );
270     DeleteAttachments( xMailMessage );
271 }
272 /*-- 23.06.2004 12:30:39---------------------------------------------------
273 
274   -----------------------------------------------------------------------*/
275 void SwMailDispatcherListener_Impl::DeleteAttachments( uno::Reference< mail::XMailMessage >& xMessage )
276 {
277     uno::Sequence< mail::MailAttachment > aAttachments = xMessage->getAttachments();
278 
279     for(sal_Int32 nFile = 0; nFile < aAttachments.getLength(); ++nFile)
280     {
281         try
282         {
283             uno::Reference< beans::XPropertySet > xTransferableProperties( aAttachments[nFile].Data, uno::UNO_QUERY_THROW);
284             if( xTransferableProperties.is() )
285             {
286                 ::rtl::OUString sURL;
287                 xTransferableProperties->getPropertyValue( ::rtl::OUString::createFromAscii("URL") ) >>= sURL;
288                 if(sURL.getLength())
289                     SWUnoHelper::UCB_DeleteFile( sURL );
290             }
291         }
292         catch( const uno::Exception& rEx )
293         {
294             (void)rEx;
295         }
296     }
297 }
298 /*-- 07.07.2004 13:45:51---------------------------------------------------
299 
300   -----------------------------------------------------------------------*/
301 class SwSendWarningBox_Impl : public ModalDialog
302 {
303     FixedImage      aWarningImageIM;
304     FixedInfo       aWarningFI;
305     FixedText       aDetailFT;
306     MultiLineEdit   aDetailED;
307     FixedLine       aSeparatorFL;
308     OKButton        aOKPB;
309 
310 public:
311     SwSendWarningBox_Impl(Window* pParent, const String& rDetails);
312     ~SwSendWarningBox_Impl();
313 };
314 /*-- 07.07.2004 13:52:41---------------------------------------------------
315 
316   -----------------------------------------------------------------------*/
317 SwSendWarningBox_Impl::SwSendWarningBox_Impl(Window* pParent, const String& rDetails) :
318     ModalDialog(pParent, SW_RES( DLG_MM_SENDWARNING )),
319     aWarningImageIM(this, SW_RES( IM_WARNING         )),
320     aWarningFI(     this, SW_RES( FI_WARNING         )),
321     aDetailFT(      this, SW_RES( FT_DETAILS         )),
322     aDetailED(      this, SW_RES( ED_DETAILS         )),
323     aSeparatorFL(   this, SW_RES( FL_SEPARATOR       )),
324     aOKPB(          this, SW_RES(PB_OK))
325 {
326     FreeResource();
327     aWarningImageIM.SetImage(WarningBox::GetStandardImage());
328     aDetailED.SetText(rDetails);
329 }
330 /*-- 07.07.2004 13:52:41---------------------------------------------------
331 
332   -----------------------------------------------------------------------*/
333 SwSendWarningBox_Impl::~SwSendWarningBox_Impl()
334 {
335 }
336 
337 /*-- 21.05.2004 14:10:39---------------------------------------------------
338 
339   -----------------------------------------------------------------------*/
340 #define ITEMID_TASK     1
341 #define ITEMID_STATUS   2
342 
343 SwSendMailDialog::SwSendMailDialog(Window *pParent, SwMailMergeConfigItem& rConfigItem) :
344     ModelessDialog /*SfxModalDialog*/(pParent, SW_RES(DLG_MM_SENDMAILS)),
345 #ifdef MSC
346 #pragma warning (disable : 4355)
347 #endif
348     m_aStatusFL( this, SW_RES(             FL_STATUS             )),
349     m_aStatusFT( this, SW_RES(             FT_STATUS1            )),
350     m_aTransferStatusFL( this, SW_RES(     FL_TRANSFERSTATUS     )),
351     m_aTransferStatusFT( this, SW_RES(     FT_TRANSFERSTATUS     )),
352     m_PausedFI(this, SW_RES(               FI_PAUSED             )),
353     m_aProgressBar( this,      SW_RES(     PB_PROGRESS           )),
354     m_aErrorStatusFT( this, SW_RES(        FT_ERRORSTATUS        )),
355     m_aDetailsPB( this, SW_RES(            PB_DETAILS            )),
356     m_aStatusHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER         ),
357     m_aStatusLB( this, SW_RES(             LB_STATUS             )),
358     m_aSeparatorFL( this, SW_RES(          FL_SEPARATOR          )),
359     m_aStopPB( this, SW_RES(               PB_STOP               )),
360     m_aClosePB( this, SW_RES(              PB_CLOSE              )),
361 #ifdef MSC
362 #pragma warning (default : 4355)
363 #endif
364     m_sMore(m_aDetailsPB.GetText()),
365     m_sLess(SW_RES(ST_LESS)),
366     m_sContinue(SW_RES( ST_CONTINUE )),
367     m_sStop(m_aStopPB.GetText()),
368     m_sSend(SW_RES(ST_SEND)),
369     m_sTransferStatus(m_aTransferStatusFT.GetText()),
370     m_sErrorStatus(   m_aErrorStatusFT.GetText()),
371     m_sSendingTo(   SW_RES(ST_SENDINGTO )),
372     m_sCompleted(   SW_RES(ST_COMPLETED )),
373     m_sFailed(      SW_RES(ST_FAILED     )),
374     m_sTerminateQuery( SW_RES( ST_TERMINATEQUERY )),
375     m_bCancel(false),
376     m_bDesctructionEnabled(false),
377     m_aImageList( SW_RES( ILIST ) ),
378     m_aImageListHC( SW_RES( ILIST_HC ) ),
379     m_pImpl(new SwSendMailDialog_Impl),
380     m_pConfigItem(&rConfigItem),
381     m_nSendCount(0),
382     m_nErrorCount(0)
383 {
384     m_nStatusHeight =  m_aSeparatorFL.GetPosPixel().Y() - m_aStatusLB.GetPosPixel().Y();
385     String sTask(SW_RES(ST_TASK));
386     String sStatus(SW_RES(ST_STATUS));
387     m_aStatusLB.SetHelpId(HID_MM_SENDMAILS_STATUSLB);
388 
389     FreeResource();
390 
391     m_aDetailsPB.SetClickHdl(LINK( this, SwSendMailDialog, DetailsHdl_Impl));
392     m_aStopPB.SetClickHdl(LINK( this, SwSendMailDialog, StopHdl_Impl));
393     m_aClosePB.SetClickHdl(LINK( this, SwSendMailDialog, CloseHdl_Impl));
394 
395 
396     Size aLBSize(m_aStatusLB.GetSizePixel());
397     m_aStatusHB.SetSizePixel(aLBSize);
398     Size aHeadSize(m_aStatusHB.CalcWindowSizePixel());
399     aHeadSize.Width() = aLBSize.Width();
400     m_aStatusHB.SetSizePixel(aHeadSize);
401     Point aLBPos(m_aStatusLB.GetPosPixel());
402     m_aStatusHB.SetPosPixel(aLBPos);
403     aLBPos.Y() += aHeadSize.Height();
404     aLBSize.Height() -= aHeadSize.Height();
405     m_aStatusLB.SetPosSizePixel(aLBPos, aLBSize);
406 
407     Size aSz(m_aStatusHB.GetOutputSizePixel());
408     long nPos1 = aSz.Width()/3 * 2;
409     long nPos2 = aSz.Width()/3;
410     m_aStatusHB.InsertItem( ITEMID_TASK, sTask,
411                             nPos1,
412                             HIB_LEFT | HIB_VCENTER );
413     m_aStatusHB.InsertItem( ITEMID_STATUS, sStatus,
414                             nPos2,
415                             HIB_LEFT | HIB_VCENTER );
416     m_aStatusHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
417     m_aStatusHB.Show();
418 
419     m_aStatusLB.SetHelpId(HID_MM_MAILSTATUS_TLB);
420     static long nTabs[] = {3, 0, nPos1, aSz.Width() };
421     m_aStatusLB.SetStyle( m_aStatusLB.GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
422     m_aStatusLB.SetSelectionMode( SINGLE_SELECTION );
423     m_aStatusLB.SetTabs(&nTabs[0], MAP_PIXEL);
424     m_aStatusLB.SetSpaceBetweenEntries(3);
425 
426     DetailsHdl_Impl( &m_aDetailsPB );
427     UpdateTransferStatus();
428 }
429 /*-- 21.05.2004 14:10:39---------------------------------------------------
430 
431   -----------------------------------------------------------------------*/
432 SwSendMailDialog::~SwSendMailDialog()
433 {
434     if(m_pImpl->xMailDispatcher.is())
435     {
436         try
437         {
438             if(m_pImpl->xMailDispatcher->isStarted())
439                 m_pImpl->xMailDispatcher->stop();
440             if(m_pImpl->xConnectedMailService.is() && m_pImpl->xConnectedMailService->isConnected())
441                 m_pImpl->xConnectedMailService->disconnect();
442             if(m_pImpl->xConnectedInMailService.is() && m_pImpl->xConnectedInMailService->isConnected())
443                 m_pImpl->xConnectedInMailService->disconnect();
444 
445             uno::Reference<mail::XMailMessage> xMessage =
446                     m_pImpl->xMailDispatcher->dequeueMailMessage();
447             while(xMessage.is())
448             {
449                 SwMailDispatcherListener_Impl::DeleteAttachments( xMessage );
450                 xMessage = m_pImpl->xMailDispatcher->dequeueMailMessage();
451             }
452         }
453         catch(const uno::Exception&)
454         {
455         }
456     }
457     delete m_pImpl;
458 }
459 /*-- 21.05.2004 14:10:40---------------------------------------------------
460 
461   -----------------------------------------------------------------------*/
462 void SwSendMailDialog::AddDocument( SwMailDescriptor& rDesc )
463 {
464     ::osl::MutexGuard aGuard(m_pImpl->aDescriptorMutex);
465     m_pImpl->aDescriptors.push_back(rDesc);
466     // if the dialog is already running then continue sending of documents
467     if(m_pImpl->xMailDispatcher.is())
468     {
469         IterateMails();
470     }
471 
472 }
473 /*-- 31.01.2005 08:59:35---------------------------------------------------
474 
475   -----------------------------------------------------------------------*/
476 void SwSendMailDialog::SetDocumentCount( sal_Int32 nAllDocuments )
477 {
478     m_pImpl->nDocumentCount = nAllDocuments;
479     UpdateTransferStatus();
480 }
481 /*-- 21.05.2004 14:10:40---------------------------------------------------
482 
483   -----------------------------------------------------------------------*/
484 void lcl_Move(Control& rCtrl, long nYOffset)
485 {
486     Point aPos(rCtrl.GetPosPixel());
487     aPos.Y() += nYOffset;
488     rCtrl.SetPosPixel(aPos);
489 }
490 /*-- 21.05.2004 14:10:40---------------------------------------------------
491 
492   -----------------------------------------------------------------------*/
493 IMPL_LINK( SwSendMailDialog, DetailsHdl_Impl, PushButton*, EMPTYARG )
494 {
495     long nMove = 0;
496     if(m_aStatusLB.IsVisible())
497     {
498         m_aStatusLB.Hide();
499         m_aStatusHB.Hide();
500         nMove = - m_nStatusHeight;
501         m_aDetailsPB.SetText(m_sMore);
502     }
503     else
504     {
505         m_aStatusLB.Show();
506         m_aStatusHB.Show();
507         nMove = m_nStatusHeight;
508         m_aDetailsPB.SetText(m_sLess);
509     }
510     lcl_Move(m_aSeparatorFL, nMove);
511     lcl_Move(m_aStopPB, nMove);
512     lcl_Move(m_aClosePB, nMove);
513     Size aDlgSize = GetSizePixel(); aDlgSize.Height() += nMove; SetSizePixel(aDlgSize);
514 
515     return 0;
516 }
517 /*-- 21.05.2004 14:10:40---------------------------------------------------
518 
519   -----------------------------------------------------------------------*/
520 IMPL_LINK( SwSendMailDialog, StopHdl_Impl, PushButton*, pButton )
521 {
522     m_bCancel = true;
523     if(m_pImpl->xMailDispatcher.is())
524     {
525         if(m_pImpl->xMailDispatcher->isStarted())
526         {
527             m_pImpl->xMailDispatcher->stop();
528             pButton->SetText(m_sContinue);
529             m_PausedFI.Show();
530         }
531         else
532         {
533             m_pImpl->xMailDispatcher->start();
534             pButton->SetText(m_sStop);
535             m_PausedFI.Show(sal_False);
536         }
537     }
538     return 0;
539 }
540 /*-- 21.05.2004 14:10:40---------------------------------------------------
541 
542   -----------------------------------------------------------------------*/
543 IMPL_LINK( SwSendMailDialog, CloseHdl_Impl, PushButton*, EMPTYARG )
544 {
545     ModelessDialog::Show( sal_False );
546     return 0;
547 }
548 /*-- 14.06.2004 09:48:30---------------------------------------------------
549 
550   -----------------------------------------------------------------------*/
551 IMPL_STATIC_LINK_NOINSTANCE( SwSendMailDialog, StartSendMails, SwSendMailDialog*, pDialog )
552 {
553     pDialog->SendMails();
554     return 0;
555 }
556 /*-- 14.06.2004 09:48:30---------------------------------------------------
557 
558   -----------------------------------------------------------------------*/
559 IMPL_STATIC_LINK( SwSendMailDialog, RemoveThis, Timer*, pTimer )
560 {
561     if( pThis->m_pImpl->xMailDispatcher.is() )
562     {
563         if(pThis->m_pImpl->xMailDispatcher->isStarted())
564             pThis->m_pImpl->xMailDispatcher->stop();
565         if(!pThis->m_pImpl->xMailDispatcher->isShutdownRequested())
566             pThis->m_pImpl->xMailDispatcher->shutdown();
567     }
568 
569     if( pThis->m_bDesctructionEnabled &&
570             (!pThis->m_pImpl->xMailDispatcher.is() ||
571                     !pThis->m_pImpl->xMailDispatcher->isRunning()))
572     {
573         delete pThis;
574     }
575     else
576     {
577         pTimer->Start();
578     }
579     return 0;
580 }
581 /*-- 07.07.2004 14:34:05---------------------------------------------------
582 
583   -----------------------------------------------------------------------*/
584 IMPL_STATIC_LINK_NOINSTANCE( SwSendMailDialog, StopSendMails, SwSendMailDialog*, pDialog )
585 {
586     if(pDialog->m_pImpl->xMailDispatcher.is() &&
587         pDialog->m_pImpl->xMailDispatcher->isStarted())
588     {
589         pDialog->m_pImpl->xMailDispatcher->stop();
590         pDialog->m_aStopPB.SetText(pDialog->m_sContinue);
591         pDialog->m_PausedFI.Show();
592     }
593     return 0;
594 }
595 /*-- 14.06.2004 10:38:04---------------------------------------------------
596 
597   -----------------------------------------------------------------------*/
598 void  SwSendMailDialog::SendMails()
599 {
600     if(!m_pConfigItem)
601     {
602         DBG_ERROR("config item not set");
603         return;
604     }
605     String sErrorMessage;
606     bool bIsLoggedIn = false;
607     EnterWait();
608     //get a mail server connection
609     uno::Reference< mail::XSmtpService > xSmtpServer =
610                 SwMailMergeHelper::ConnectToSmtpServer( *m_pConfigItem,
611                                             m_pImpl->xConnectedInMailService,
612                                             aEmptyStr, aEmptyStr, this );
613     bIsLoggedIn = xSmtpServer.is() && xSmtpServer->isConnected();
614     LeaveWait();
615     if(!bIsLoggedIn)
616     {
617         DBG_ERROR("create error message");
618         return;
619     }
620     m_pImpl->xMailDispatcher.set( new MailDispatcher(xSmtpServer));
621     IterateMails();
622     m_pImpl->xMailListener = new SwMailDispatcherListener_Impl(*this);
623     m_pImpl->xMailDispatcher->addListener(m_pImpl->xMailListener);
624     if(!m_bCancel)
625     {
626         m_pImpl->xMailDispatcher->start();
627     }
628 }
629 /* -----------------28.01.2005 10:17-----------------
630 
631  --------------------------------------------------*/
632 void  SwSendMailDialog::IterateMails()
633 {
634     const SwMailDescriptor* pCurrentMailDescriptor = m_pImpl->GetNextDescriptor();
635     while( pCurrentMailDescriptor )
636     {
637         if(!SwMailMergeHelper::CheckMailAddress( pCurrentMailDescriptor->sEMail ))
638         {
639             ImageList& rImgLst = GetSettings().GetStyleSettings().GetHighContrastMode() ?
640                                         m_aImageListHC : m_aImageList;
641             Image aInsertImg =   rImgLst.GetImage( FN_FORMULA_CANCEL );
642 
643             String sMessage = m_sSendingTo;
644             String sTmp(pCurrentMailDescriptor->sEMail);
645             sTmp += '\t';
646             sTmp += m_sFailed;
647             sMessage.SearchAndReplaceAscii("%1", sTmp);
648             m_aStatusLB.InsertEntry( sMessage, aInsertImg, aInsertImg);
649             ++m_nSendCount;
650             ++m_nErrorCount;
651             UpdateTransferStatus( );
652             pCurrentMailDescriptor = m_pImpl->GetNextDescriptor();
653             continue;
654         }
655         SwMailMessage* pMessage = 0;
656         uno::Reference< mail::XMailMessage > xMessage = pMessage = new SwMailMessage;
657         if(m_pConfigItem->IsMailReplyTo())
658             pMessage->setReplyToAddress(m_pConfigItem->GetMailReplyTo());
659         pMessage->addRecipient( pCurrentMailDescriptor->sEMail );
660         pMessage->SetSenderName( m_pConfigItem->GetMailDisplayName() );
661         pMessage->SetSenderAddress( m_pConfigItem->GetMailAddress() );
662         if(pCurrentMailDescriptor->sAttachmentURL.getLength())
663         {
664             mail::MailAttachment aAttach;
665             aAttach.Data =
666                     new SwMailTransferable(
667                         pCurrentMailDescriptor->sAttachmentURL,
668                         pCurrentMailDescriptor->sAttachmentName,
669                         pCurrentMailDescriptor->sMimeType );
670             aAttach.ReadableName = pCurrentMailDescriptor->sAttachmentName;
671             pMessage->addAttachment( aAttach );
672         }
673         pMessage->setSubject( pCurrentMailDescriptor->sSubject );
674         uno::Reference< datatransfer::XTransferable> xBody =
675                     new SwMailTransferable(
676                         pCurrentMailDescriptor->sBodyContent,
677                         pCurrentMailDescriptor->sBodyMimeType);
678         pMessage->setBody( xBody );
679 
680         //CC and BCC are tokenized by ';'
681         if(pCurrentMailDescriptor->sCC.getLength())
682         {
683             String sTokens( pCurrentMailDescriptor->sCC );
684             sal_uInt16 nTokens = sTokens.GetTokenCount( ';' );
685             xub_StrLen nPos = 0;
686             for( sal_uInt16 nToken = 0; nToken < nTokens; ++nToken)
687             {
688                 String sTmp = sTokens.GetToken( 0, ';', nPos);
689                 if( sTmp.Len() )
690                     pMessage->addCcRecipient( sTmp );
691             }
692         }
693         if(pCurrentMailDescriptor->sBCC.getLength())
694         {
695             String sTokens( pCurrentMailDescriptor->sBCC );
696             sal_uInt16 nTokens = sTokens.GetTokenCount( ';' );
697             xub_StrLen nPos = 0;
698             for( sal_uInt16 nToken = 0; nToken < nTokens; ++nToken)
699             {
700                 String sTmp = sTokens.GetToken( 0, ';', nPos);
701                 if( sTmp.Len() )
702                     pMessage->addBccRecipient( sTmp );
703             }
704         }
705         m_pImpl->xMailDispatcher->enqueueMailMessage( xMessage );
706         pCurrentMailDescriptor = m_pImpl->GetNextDescriptor();
707     }
708     UpdateTransferStatus();
709 }
710 /*-- 27.08.2004 10:50:17---------------------------------------------------
711 
712   -----------------------------------------------------------------------*/
713 void SwSendMailDialog::ShowDialog()
714 {
715     Application::PostUserEvent( STATIC_LINK( this, SwSendMailDialog,
716                                                 StartSendMails ), this );
717     ModelessDialog::Show();
718 }
719 /*-- 27.08.2004 10:50:17---------------------------------------------------
720 
721   -----------------------------------------------------------------------*/
722 void  SwSendMailDialog::StateChanged( StateChangedType nStateChange )
723 {
724     ModelessDialog::StateChanged( nStateChange );
725     if(STATE_CHANGE_VISIBLE == nStateChange && !IsVisible())
726     {
727         m_pImpl->aRemoveTimer.SetTimeoutHdl( STATIC_LINK( this, SwSendMailDialog,
728                                                     RemoveThis ) );
729         m_pImpl->aRemoveTimer.Start();
730     }
731 }
732 
733 /*-- 23.06.2004 10:14:22---------------------------------------------------
734 
735   -----------------------------------------------------------------------*/
736 void SwSendMailDialog::DocumentSent( uno::Reference< mail::XMailMessage> xMessage,
737                                         bool bResult,
738                                         const ::rtl::OUString* pError )
739 {
740     //sending should stop on send errors
741     if(pError &&
742         m_pImpl->xMailDispatcher.is() && m_pImpl->xMailDispatcher->isStarted())
743     {
744         Application::PostUserEvent( STATIC_LINK( this, SwSendMailDialog,
745                                                     StopSendMails ), this );
746     }
747     ImageList& rImgLst = GetSettings().GetStyleSettings().GetHighContrastMode() ?
748                                 m_aImageListHC : m_aImageList;
749     Image aInsertImg =   rImgLst.GetImage( bResult ? FN_FORMULA_APPLY : FN_FORMULA_CANCEL );
750 
751     String sMessage = m_sSendingTo;
752     String sTmp(xMessage->getRecipients()[0]);
753     sTmp += '\t';
754     sTmp += bResult ? m_sCompleted : m_sFailed;
755     sMessage.SearchAndReplaceAscii("%1", sTmp);
756     m_aStatusLB.InsertEntry( sMessage, aInsertImg, aInsertImg);
757     ++m_nSendCount;
758     if(!bResult)
759         ++m_nErrorCount;
760 
761     UpdateTransferStatus( );
762     if(pError)
763     {
764         SwSendWarningBox_Impl* pDlg = new SwSendWarningBox_Impl(0, *pError);
765         pDlg->Execute();
766         delete pDlg;
767     }
768 }
769 /*-- 23.06.2004 11:25:31---------------------------------------------------
770 
771   -----------------------------------------------------------------------*/
772 void SwSendMailDialog::UpdateTransferStatus()
773 {
774     String sStatus( m_sTransferStatus );
775     sStatus.SearchAndReplaceAscii("%1", String::CreateFromInt32(m_nSendCount) );
776     sStatus.SearchAndReplaceAscii("%2", String::CreateFromInt32(m_pImpl->nDocumentCount));
777     m_aTransferStatusFT.SetText(sStatus);
778 
779     sStatus = m_sErrorStatus;
780     sStatus.SearchAndReplaceAscii("%1", String::CreateFromInt32(m_nErrorCount) );
781     m_aErrorStatusFT.SetText(sStatus);
782 
783     if(m_pImpl->aDescriptors.size())
784         m_aProgressBar.SetValue((sal_uInt16)(m_nSendCount * 100 / m_pImpl->aDescriptors.size()));
785     else
786         m_aProgressBar.SetValue(0);
787 }
788 /*-- 23.06.2004 11:18:50---------------------------------------------------
789 
790   -----------------------------------------------------------------------*/
791 void SwSendMailDialog::AllMailsSent()
792 {
793     m_aStopPB.Enable(sal_False);
794 }
795 
796 
797