LibreOffice Module sw (master)  1
unomailmerge.cxx
Go to the documentation of this file.
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 <vcl/svapp.hxx>
21 #include <osl/mutex.hxx>
22 #include <svl/itemprop.hxx>
24 #include <unotools/tempfile.hxx>
25 #include <sfx2/app.hxx>
26 #include <sfx2/docfilt.hxx>
27 #include <tools/urlobj.hxx>
29 #include <comphelper/string.hxx>
31 #include <vcl/timer.hxx>
32 #include <com/sun/star/sdb/CommandType.hpp>
33 #include <com/sun/star/text/MailMergeType.hpp>
34 #include <com/sun/star/text/MailMergeEvent.hpp>
35 #include <com/sun/star/text/XMailMergeListener.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/XConnection.hpp>
39 #include <com/sun/star/sdbc/XRowSet.hpp>
40 #include <com/sun/star/frame/Desktop.hpp>
41 #include <com/sun/star/util/XCloseable.hpp>
42 #include <com/sun/star/util/CloseVetoException.hpp>
43 #include <com/sun/star/sdbcx/XRowLocate.hpp>
44 #include <com/sun/star/frame/XStorable.hpp>
45 #include <com/sun/star/mail/XSmtpService.hpp>
46 #include <sfx2/viewfrm.hxx>
47 #include <sfx2/event.hxx>
48 #include <cppuhelper/implbase.hxx>
49 #include <printdata.hxx>
50 #include <swevent.hxx>
51 #include <unomailmerge.hxx>
52 #include <unoprnms.hxx>
53 #include <unomap.hxx>
54 #include <swunohelper.hxx>
55 #include <docsh.hxx>
57 #include <view.hxx>
58 #include <dbmgr.hxx>
59 #include <unotxdoc.hxx>
60 #include <wrtsh.hxx>
61 #include <mmconfigitem.hxx>
62 #include <mailmergehelper.hxx>
63 
64 #include <iodetect.hxx>
65 
66 #include <memory>
67 
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::frame;
70 using namespace ::com::sun::star::uno;
71 using namespace ::com::sun::star::lang;
72 using namespace ::com::sun::star::beans;
73 using namespace ::com::sun::star::text;
74 using namespace SWUnoHelper;
75 
76 typedef ::utl::SharedUNOComponent< XInterface > SharedComponent;
77 
78 static osl::Mutex & GetMailMergeMutex()
79 {
80  static osl::Mutex aMutex;
81  return aMutex;
82 }
83 
84 namespace {
85 
87 {
88  eSuccess, // successfully closed
89  eVetoed, // vetoed, ownership transferred to the vetoing instance
90  eFailed // failed for some unknown reason
91 };
92 
93 }
94 
96  Reference< frame::XModel > const &rxModel,
97  SfxObjectShellRef &rxDocSh )
98 {
99  CloseResult eResult = eSuccess;
100 
101  rxDocSh = nullptr;
102 
106  Reference< util::XCloseable > xClose( rxModel, UNO_QUERY );
107  if (xClose.is())
108  {
109  try
110  {
113  xClose->close( true );
114  }
115  catch (const util::CloseVetoException&)
116  {
119  eResult = eVetoed;
120  }
121  catch (const uno::RuntimeException&)
122  {
123  eResult = eFailed;
124  }
125  }
126  return eResult;
127 }
128 
130 static bool LoadFromURL_impl(
132  SfxObjectShellRef &rxDocSh,
133  const OUString &rURL,
134  bool bClose )
135 {
136  // try to open the document readonly and hidden
137  Reference< frame::XModel > xTmpModel;
138  Sequence < PropertyValue > aArgs( 1 );
139  aArgs[0].Name = "Hidden";
140  aArgs[0].Value <<= true;
141  try
142  {
143  Reference < XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
144  xTmpModel.set( xDesktop->loadComponentFromURL( rURL, "_blank", 0, aArgs ), UNO_QUERY );
145  }
146  catch (const Exception&)
147  {
148  return false;
149  }
150 
151  // try to get the DocShell
152  SwDocShell *pTmpDocShell = nullptr;
153  if (auto pTextDoc = comphelper::getUnoTunnelImplementation<SwXTextDocument>(xTmpModel); pTextDoc)
154  pTmpDocShell = pTextDoc->GetDocShell();
155 
156  bool bRes = false;
157  if (xTmpModel.is() && pTmpDocShell) // everything available?
158  {
159  if (bClose)
160  CloseModelAndDocSh( rxModel, rxDocSh );
161  // set new stuff
162  rxModel = xTmpModel;
163  rxDocSh = pTmpDocShell;
164  bRes = true;
165  }
166  else
167  {
168  // SfxObjectShellRef is ok here, since the document will be explicitly closed
169  SfxObjectShellRef xTmpDocSh = pTmpDocShell;
170  CloseModelAndDocSh( xTmpModel, xTmpDocSh );
171  }
172 
173  return bRes;
174 }
175 
176 namespace
177 {
178  class DelayedFileDeletion : public ::cppu::WeakImplHelper<util::XCloseListener>
179  {
180  protected:
181  ::osl::Mutex m_aMutex;
182  Reference< util::XCloseable > m_xDocument;
183  Timer m_aDeleteTimer;
184  OUString m_sTemporaryFile;
185  sal_Int32 m_nPendingDeleteAttempts;
186 
187  DelayedFileDeletion(DelayedFileDeletion const&) = delete;
188  DelayedFileDeletion& operator=(DelayedFileDeletion const&) = delete;
189 
190  public:
191  DelayedFileDeletion( const Reference< XModel >& _rxModel,
192  const OUString& _rTemporaryFile );
193 
194  protected:
195  virtual ~DelayedFileDeletion( ) override;
196 
197  // XCloseListener
198  virtual void SAL_CALL queryClosing( const EventObject& _rSource, sal_Bool _bGetsOwnership ) override;
199  virtual void SAL_CALL notifyClosing( const EventObject& _rSource ) override;
200 
201  // XEventListener
202  virtual void SAL_CALL disposing( const EventObject& Source ) override;
203 
204  private:
205  void implTakeOwnership( );
206  DECL_LINK( OnTryDeleteFile, Timer*, void );
207  };
208 
209  DelayedFileDeletion::DelayedFileDeletion( const Reference< XModel >& _rxModel, const OUString& _rTemporaryFile )
210  :
211  m_xDocument( _rxModel, UNO_QUERY )
212  ,m_sTemporaryFile( _rTemporaryFile )
213  ,m_nPendingDeleteAttempts( 0 )
214  {
215  osl_atomic_increment( &m_refCount );
216  try
217  {
218  if ( m_xDocument.is() )
219  {
220  m_xDocument->addCloseListener( this );
221  // successfully added -> keep ourself alive
222  acquire();
223  }
224  else {
225  OSL_FAIL("DelayedFileDeletion::DelayedFileDeletion: model is no component!" );
226  }
227  }
228  catch (const Exception&)
229  {
230  OSL_FAIL("DelayedFileDeletion::DelayedFileDeletion: could not register as event listener at the model!" );
231  }
232  osl_atomic_decrement( &m_refCount );
233  }
234 
235  IMPL_LINK_NOARG(DelayedFileDeletion, OnTryDeleteFile, Timer *, void)
236  {
237  ::osl::ClearableMutexGuard aGuard( m_aMutex );
238 
239  bool bSuccess = false;
240  try
241  {
242  bool bDeliverOwnership = ( 0 == m_nPendingDeleteAttempts );
243  // if this is our last attempt, then anybody which vetoes this has to take the consequences
244  // (means take the ownership)
245  m_xDocument->close( bDeliverOwnership );
246  bSuccess = true;
247  }
248  catch (const util::CloseVetoException&)
249  {
250  // somebody vetoed -> next try
251  if ( m_nPendingDeleteAttempts )
252  {
253  // next attempt
254  --m_nPendingDeleteAttempts;
255  m_aDeleteTimer.Start();
256  }
257  else
258  bSuccess = true; // can't do anything here ...
259  }
260  catch (const Exception&)
261  {
262  OSL_FAIL("DelayedFileDeletion::OnTryDeleteFile: caught a strange exception!" );
263  bSuccess = true;
264  // can't do anything here ...
265  }
266 
267  if ( bSuccess )
268  {
269  SWUnoHelper::UCB_DeleteFile( m_sTemporaryFile );
270  aGuard.clear();
271  release(); // this should be our last reference, we should be dead after this
272  }
273  }
274 
275  void DelayedFileDeletion::implTakeOwnership( )
276  {
277  // revoke ourself as listener
278  try
279  {
280  m_xDocument->removeCloseListener( this );
281  }
282  catch (const Exception&)
283  {
284  OSL_FAIL("DelayedFileDeletion::implTakeOwnership: could not revoke the listener!" );
285  }
286 
287  m_aDeleteTimer.SetTimeout( 3000 ); // 3 seconds
288  m_aDeleteTimer.SetInvokeHandler( LINK( this, DelayedFileDeletion, OnTryDeleteFile ) );
289  m_nPendingDeleteAttempts = 3; // try 3 times at most
290  m_aDeleteTimer.Start( );
291  }
292 
293  void SAL_CALL DelayedFileDeletion::queryClosing( const EventObject& , sal_Bool _bGetsOwnership )
294  {
295  ::osl::MutexGuard aGuard( m_aMutex );
296  if ( _bGetsOwnership )
297  implTakeOwnership( );
298 
299  // always veto: We want to take the ownership ourself, as this is the only chance to delete
300  // the temporary file which the model is based on
301  throw util::CloseVetoException( );
302  }
303 
304  void SAL_CALL DelayedFileDeletion::notifyClosing( const EventObject& )
305  {
306  OSL_FAIL("DelayedFileDeletion::notifyClosing: how this?" );
307  // this should not happen:
308  // Either, a foreign instance closes the document, then we should veto this, and take the ownership
309  // Or, we ourself close the document, then we should not be a listener anymore
310  }
311 
312  void SAL_CALL DelayedFileDeletion::disposing( const EventObject& )
313  {
314  OSL_FAIL("DelayedFileDeletion::disposing: how this?" );
315  // this should not happen:
316  // Either, a foreign instance closes the document, then we should veto this, and take the ownership
317  // Or, we ourself close the document, then we should not be a listener anymore
318  }
319 
320  DelayedFileDeletion::~DelayedFileDeletion( )
321  {
322  }
323 }
324 
325 static bool DeleteTmpFile_Impl(
327  SfxObjectShellRef &rxDocSh,
328  const OUString &rTmpFileURL )
329 {
330  bool bRes = false;
331  if (!rTmpFileURL.isEmpty())
332  {
333  bool bDelete = true;
334  if ( eVetoed == CloseModelAndDocSh( rxModel, rxDocSh ) )
335  {
336  // somebody vetoed the closing, and took the ownership of the document
337  // -> ensure that the temporary file is deleted later on
338  new DelayedFileDeletion( rxModel, rTmpFileURL );
339  // note: as soon as #106931# is fixed, the whole DelayedFileDeletion is to be superseded by
340  // a better solution
341  bDelete = false;
342  }
343 
344  rxModel = nullptr;
345  rxDocSh = nullptr; // destroy doc shell
346 
347  if ( bDelete )
348  {
349  if ( !SWUnoHelper::UCB_DeleteFile( rTmpFileURL ) )
350  {
351  new DelayedFileDeletion( rxModel, rTmpFileURL );
352  // same not as above: as soon as #106931#, ...
353  }
354  }
355  else
356  bRes = true; // file will be deleted delayed
357  }
358  return bRes;
359 }
360 
362  m_aEvtListeners ( GetMailMergeMutex() ),
363  m_aMergeListeners ( GetMailMergeMutex() ),
364  m_aPropListeners ( GetMailMergeMutex() ),
365  m_pPropSet( aSwMapProvider.GetPropertySet( PROPERTY_MAP_MAILMERGE ) ),
366  m_nDataCommandType(sdb::CommandType::TABLE),
367  m_nOutputType(MailMergeType::PRINTER),
368  m_bEscapeProcessing(true),
369  m_bSinglePrintJobs(false),
370  m_bFileNameFromColumn(false),
371  m_bSendAsHTML(false),
372  m_bSendAsAttachment(false),
373  m_bSaveAsSingleFile(false),
374  m_bDisposing(false),
375  m_pMgr(nullptr)
376 {
377  // create empty document
378  // like in: SwModule::InsertEnv (appenv.cxx)
379  m_xDocSh = new SwDocShell( SfxObjectCreateMode::STANDARD );
380  m_xDocSh->DoInitNew();
382  SwView *pView = static_cast<SwView*>( pFrame->GetViewShell() );
383  pView->AttrChangedNotify(nullptr); //So that SelectShell is called.
385 }
386 
388 {
389  if (!m_aTmpFileName.isEmpty())
391  else // there was no temporary file in use
392  {
396  if ( eVetoed == CloseModelAndDocSh( m_xModel, m_xDocSh ) )
397  OSL_FAIL("ownership transferred to vetoing object!" );
398 
399  m_xModel = nullptr;
400  m_xDocSh = nullptr; // destroy doc shell
401  }
402 }
403 
404 // Guarantee object consistence in case of an exception
406 {
407 public:
409  : m_pMailMerge(mailmerge)
410  {
411  assert(m_pMailMerge); //mailmerge object missing
412  }
414  {
415  osl::MutexGuard aMgrGuard( GetMailMergeMutex() );
416  m_pMailMerge->m_pMgr = nullptr;
417  }
418 
419 private:
422 
424 };
425 
427  const uno::Sequence< beans::NamedValue >& rArguments )
428 {
429  SolarMutexGuard aGuard;
430  MailMergeExecuteFinalizer aFinalizer(this);
431 
432  // get property values to be used
433  // (use values from the service as default and override them with
434  // the values that are provided as arguments)
435 
436  uno::Sequence< uno::Any > aCurSelection = m_aSelection;
437  uno::Reference< sdbc::XResultSet > xCurResultSet = m_xResultSet;
438  uno::Reference< sdbc::XConnection > xCurConnection = m_xConnection;
439  uno::Reference< frame::XModel > xCurModel = m_xModel;
440  OUString aCurDataSourceName = m_aDataSourceName;
441  OUString aCurDataCommand = m_aDataCommand;
442  OUString aCurFilter = m_aFilter;
443  OUString aCurDocumentURL = m_aDocumentURL;
444  OUString aCurOutputURL = m_aOutputURL;
445  OUString aCurFileNamePrefix = m_aFileNamePrefix;
446  sal_Int32 nCurDataCommandType = m_nDataCommandType;
447  sal_Int16 nCurOutputType = m_nOutputType;
448  bool bCurEscapeProcessing = m_bEscapeProcessing;
449  bool bCurSinglePrintJobs = m_bSinglePrintJobs;
450  bool bCurFileNameFromColumn = m_bFileNameFromColumn;
451 
452  SfxObjectShellRef xCurDocSh = m_xDocSh; // the document
453 
454  for (const beans::NamedValue& rArgument : rArguments)
455  {
456  const OUString &rName = rArgument.Name;
457  const Any &rValue = rArgument.Value;
458 
459  bool bOK = true;
460  if (rName == UNO_NAME_SELECTION)
461  bOK = rValue >>= aCurSelection;
462  else if (rName == UNO_NAME_RESULT_SET)
463  bOK = rValue >>= xCurResultSet;
464  else if (rName == UNO_NAME_CONNECTION)
465  bOK = rValue >>= xCurConnection;
466  else if (rName == UNO_NAME_MODEL)
467  throw PropertyVetoException("Property is read-only: " + rName, static_cast < cppu::OWeakObject * > ( this ) );
468  else if (rName == UNO_NAME_DATA_SOURCE_NAME)
469  bOK = rValue >>= aCurDataSourceName;
470  else if (rName == UNO_NAME_DAD_COMMAND)
471  bOK = rValue >>= aCurDataCommand;
472  else if (rName == UNO_NAME_FILTER)
473  bOK = rValue >>= aCurFilter;
474  else if (rName == UNO_NAME_DOCUMENT_URL)
475  {
476  bOK = rValue >>= aCurDocumentURL;
477  if (!aCurDocumentURL.isEmpty()
478  && !LoadFromURL_impl( xCurModel, xCurDocSh, aCurDocumentURL, false ))
479  throw RuntimeException("Failed to create document from URL: " + aCurDocumentURL, static_cast < cppu::OWeakObject * > ( this ) );
480  }
481  else if (rName == UNO_NAME_OUTPUT_URL)
482  {
483  bOK = rValue >>= aCurOutputURL;
484  if (!aCurOutputURL.isEmpty())
485  {
486  if (!UCB_IsDirectory(aCurOutputURL))
487  throw IllegalArgumentException("URL does not point to a directory: " + aCurOutputURL, static_cast < cppu::OWeakObject * > ( this ), 0 );
488  if (UCB_IsReadOnlyFileName(aCurOutputURL))
489  throw IllegalArgumentException("URL is read-only: " + aCurOutputURL, static_cast < cppu::OWeakObject * > ( this ), 0 );
490  }
491  }
492  else if (rName == UNO_NAME_FILE_NAME_PREFIX)
493  bOK = rValue >>= aCurFileNamePrefix;
494  else if (rName == UNO_NAME_DAD_COMMAND_TYPE)
495  bOK = rValue >>= nCurDataCommandType;
496  else if (rName == UNO_NAME_OUTPUT_TYPE)
497  bOK = rValue >>= nCurOutputType;
498  else if (rName == UNO_NAME_ESCAPE_PROCESSING)
499  bOK = rValue >>= bCurEscapeProcessing;
500  else if (rName == UNO_NAME_SINGLE_PRINT_JOBS)
501  bOK = rValue >>= bCurSinglePrintJobs;
502  else if (rName == UNO_NAME_FILE_NAME_FROM_COLUMN)
503  bOK = rValue >>= bCurFileNameFromColumn;
504  else if (rName == UNO_NAME_SUBJECT)
505  bOK = rValue >>= m_sSubject;
506  else if (rName == UNO_NAME_ADDRESS_FROM_COLUMN)
507  bOK = rValue >>= m_sAddressFromColumn;
508  else if (rName == UNO_NAME_SEND_AS_HTML)
509  bOK = rValue >>= m_bSendAsHTML;
510  else if (rName == UNO_NAME_MAIL_BODY)
511  bOK = rValue >>= m_sMailBody;
512  else if (rName == UNO_NAME_ATTACHMENT_NAME)
513  bOK = rValue >>= m_sAttachmentName;
514  else if (rName == UNO_NAME_ATTACHMENT_FILTER)
515  bOK = rValue >>= m_sAttachmentFilter;
516  else if (rName == UNO_NAME_COPIES_TO)
517  bOK = rValue >>= m_aCopiesTo;
518  else if (rName == UNO_NAME_BLIND_COPIES_TO)
519  bOK = rValue >>= m_aBlindCopiesTo;
520  else if (rName == UNO_NAME_SEND_AS_ATTACHMENT)
521  bOK = rValue >>= m_bSendAsAttachment;
522  else if (rName == UNO_NAME_PRINT_OPTIONS)
523  bOK = rValue >>= m_aPrintSettings;
524  else if (rName == UNO_NAME_SAVE_AS_SINGLE_FILE)
525  bOK = rValue >>= m_bSaveAsSingleFile;
526  else if (rName == UNO_NAME_SAVE_FILTER)
527  bOK = rValue >>= m_sSaveFilter;
528  else if (rName == UNO_NAME_SAVE_FILTER_OPTIONS)
529  bOK = rValue >>= m_sSaveFilterOptions;
530  else if (rName == UNO_NAME_SAVE_FILTER_DATA)
531  bOK = rValue >>= m_aSaveFilterData;
532  else if (rName == UNO_NAME_IN_SERVER_PASSWORD)
533  bOK = rValue >>= m_sInServerPassword;
534  else if (rName == UNO_NAME_OUT_SERVER_PASSWORD)
535  bOK = rValue >>= m_sOutServerPassword;
536  else
537  throw UnknownPropertyException( "Property is unknown: " + rName, static_cast < cppu::OWeakObject * > ( this ) );
538 
539  if (!bOK)
540  throw IllegalArgumentException("Property type mismatch or property not set: " + rName, static_cast < cppu::OWeakObject * > ( this ), 0 );
541  }
542 
543  // need to translate the selection: the API here requires a sequence of bookmarks, but the Merge
544  // method we will call below requires a sequence of indices.
545  if ( aCurSelection.hasElements() )
546  {
547  Sequence< Any > aTranslated( aCurSelection.getLength() );
548 
549  bool bValid = false;
550  Reference< sdbcx::XRowLocate > xRowLocate( xCurResultSet, UNO_QUERY );
551  if ( xRowLocate.is() )
552  {
553  Any* pTranslated = aTranslated.getArray();
554 
555  try
556  {
557  bool bEverythingsFine = true;
558  for ( const Any& rBookmark : std::as_const(aCurSelection) )
559  {
560  bEverythingsFine = xRowLocate->moveToBookmark( rBookmark );
561  if ( !bEverythingsFine )
562  break;
563  *pTranslated <<= xCurResultSet->getRow();
564  ++pTranslated;
565  }
566  if ( bEverythingsFine )
567  bValid = true;
568  }
569  catch (const Exception&)
570  {
571  bValid = false;
572  }
573  }
574 
575  if ( !bValid )
576  {
577  throw IllegalArgumentException(
578  "The current 'Selection' does not describe a valid array of bookmarks, relative to the current 'ResultSet'.",
579  static_cast < cppu::OWeakObject * > ( this ),
580  0
581  );
582  }
583 
584  aCurSelection = aTranslated;
585  }
586 
587  SfxViewFrame* pFrame = SfxViewFrame::GetFirst( xCurDocSh.get(), false);
588  SwView *pView = pFrame ? dynamic_cast<SwView*>( pFrame->GetViewShell() ) : nullptr;
589  if (!pView)
590  throw RuntimeException();
591  SwWrtShell &rSh = *pView->GetWrtShellPtr();
592 
593  // avoid assertion in 'Update' from Sfx by supplying a shell
594  // and thus avoiding the SelectShell call in Writers GetState function
595  // while still in Update of Sfx.
596  // (GetSelection in Update is not allowed)
597  if (!aCurDocumentURL.isEmpty())
598  pView->AttrChangedNotify(nullptr);//So that SelectShell is called.
599 
600  SharedComponent aRowSetDisposeHelper;
601  if (!xCurResultSet.is())
602  {
603  if (aCurDataSourceName.isEmpty() || aCurDataCommand.isEmpty() )
604  {
605  OSL_FAIL("PropertyValues missing or unset");
606  throw IllegalArgumentException("Either the ResultSet or DataSourceName and DataCommand must be set.", static_cast < cppu::OWeakObject * > ( this ), 0 );
607  }
608 
609  // build ResultSet from DataSourceName, DataCommand and DataCommandType
610 
611  Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
612  if (xMgr.is())
613  {
614  Reference< XInterface > xInstance = xMgr->createInstance( "com.sun.star.sdb.RowSet" );
615  aRowSetDisposeHelper.reset( xInstance, SharedComponent::TakeOwnership );
616  Reference< XPropertySet > xRowSetPropSet( xInstance, UNO_QUERY );
617  OSL_ENSURE( xRowSetPropSet.is(), "failed to get XPropertySet interface from RowSet" );
618  if (xRowSetPropSet.is())
619  {
620  if (xCurConnection.is())
621  xRowSetPropSet->setPropertyValue( "ActiveConnection", makeAny( xCurConnection ) );
622  xRowSetPropSet->setPropertyValue( "DataSourceName", makeAny( aCurDataSourceName ) );
623  xRowSetPropSet->setPropertyValue( "Command", makeAny( aCurDataCommand ) );
624  xRowSetPropSet->setPropertyValue( "CommandType", makeAny( nCurDataCommandType ) );
625  xRowSetPropSet->setPropertyValue( "EscapeProcessing", makeAny( bCurEscapeProcessing ) );
626  xRowSetPropSet->setPropertyValue( "ApplyFilter", makeAny( true ) );
627  xRowSetPropSet->setPropertyValue( "Filter", makeAny( aCurFilter ) );
628 
629  Reference< sdbc::XRowSet > xRowSet( xInstance, UNO_QUERY );
630  if (xRowSet.is())
631  xRowSet->execute(); // build ResultSet from properties
632  if( !xCurConnection.is() )
633  xCurConnection.set( xRowSetPropSet->getPropertyValue( "ActiveConnection" ), UNO_QUERY );
634  xCurResultSet = xRowSet;
635  OSL_ENSURE( xCurResultSet.is(), "failed to build ResultSet" );
636  }
637  }
638  }
639 
640  svx::ODataAccessDescriptor aDescriptor;
641  aDescriptor.setDataSource(aCurDataSourceName);
642  aDescriptor[ svx::DataAccessDescriptorProperty::Connection ] <<= xCurConnection;
643  aDescriptor[ svx::DataAccessDescriptorProperty::Command ] <<= aCurDataCommand;
644  aDescriptor[ svx::DataAccessDescriptorProperty::CommandType ] <<= nCurDataCommandType;
645  aDescriptor[ svx::DataAccessDescriptorProperty::EscapeProcessing ] <<= bCurEscapeProcessing;
646  aDescriptor[ svx::DataAccessDescriptorProperty::Cursor ] <<= xCurResultSet;
647  // aDescriptor[ svx::DataAccessDescriptorProperty::ColumnName ] not used
648  // aDescriptor[ svx::DataAccessDescriptorProperty::ColumnObject ] not used
649  aDescriptor[ svx::DataAccessDescriptorProperty::Selection ] <<= aCurSelection;
650 
651  DBManagerOptions nMergeType;
652  switch (nCurOutputType)
653  {
654  case MailMergeType::PRINTER : nMergeType = DBMGR_MERGE_PRINTER; break;
655  case MailMergeType::FILE : nMergeType = DBMGR_MERGE_FILE; break;
656  case MailMergeType::MAIL : nMergeType = DBMGR_MERGE_EMAIL; break;
657  case MailMergeType::SHELL : nMergeType = DBMGR_MERGE_SHELL; break;
658  default:
659  throw IllegalArgumentException("Invalid value of property: OutputType", static_cast < cppu::OWeakObject * > ( this ), 0 );
660  }
661 
662  SwDBManager* pMgr = rSh.GetDBManager();
663  //force layout creation
664  rSh.CalcLayout();
665  OSL_ENSURE( pMgr, "database manager missing" );
666  m_pMgr = pMgr;
667 
668  SwMergeDescriptor aMergeDesc( nMergeType, rSh, aDescriptor );
669 
670  std::unique_ptr< SwMailMergeConfigItem > pMMConfigItem;
671  uno::Reference< mail::XMailService > xInService;
672  switch (nCurOutputType)
673  {
674  case MailMergeType::PRINTER:
675  {
677  SwPrintData aPrtData( rIDDA.getPrintData() );
678  aPrtData.SetPrintSingleJobs( bCurSinglePrintJobs );
679  rIDDA.setPrintData( aPrtData );
680  // #i25686# printing should not be done asynchronously to prevent dangling offices
681  // when mail merge is called as command line macro
682  aMergeDesc.aPrintOptions = m_aPrintSettings;
683  aMergeDesc.bCreateSingleFile = true;
684  }
685  break;
686  case MailMergeType::SHELL:
687  aMergeDesc.bCreateSingleFile = true;
688  pMMConfigItem.reset(new SwMailMergeConfigItem);
689  aMergeDesc.pMailMergeConfigItem = pMMConfigItem.get();
690  break;
691  case MailMergeType::FILE:
692  {
693  INetURLObject aURLObj;
694  aURLObj.SetSmartProtocol( INetProtocol::File );
695 
696  if (!aCurDocumentURL.isEmpty())
697  {
698  // if OutputURL or FileNamePrefix are missing get
699  // them from DocumentURL
700  aURLObj.SetSmartURL( aCurDocumentURL );
701  if (aCurFileNamePrefix.isEmpty())
702  aCurFileNamePrefix = aURLObj.GetBase(); // filename without extension
703  if (aCurOutputURL.isEmpty())
704  {
705  aURLObj.removeSegment();
706  aCurOutputURL = aURLObj.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
707  }
708  }
709  else // default empty document without URL
710  {
711  if (aCurOutputURL.isEmpty())
712  throw RuntimeException("OutputURL is not set and can not be obtained.", static_cast < cppu::OWeakObject * > ( this ) );
713  }
714 
715  aURLObj.SetSmartURL( aCurOutputURL );
716  OUString aPath = aURLObj.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
717 
718  const OUString aDelim( "/" );
719  if (!aPath.isEmpty() && !aPath.endsWith(aDelim))
720  aPath += aDelim;
721  if (bCurFileNameFromColumn)
722  aMergeDesc.sDBcolumn = aCurFileNamePrefix;
723  else
724  {
725  aPath += aCurFileNamePrefix;
726  }
727 
728  aMergeDesc.sPrefix = aPath;
729  aMergeDesc.sSaveToFilter = m_sSaveFilter;
733  }
734  break;
735  case MailMergeType::MAIL:
736  {
737  aMergeDesc.sDBcolumn = m_sAddressFromColumn;
738  if(m_sAddressFromColumn.isEmpty())
739  throw RuntimeException("Mail address column not set.", static_cast < cppu::OWeakObject * > ( this ) );
740  aMergeDesc.sSaveToFilter = m_sAttachmentFilter;
741  aMergeDesc.sSubject = m_sSubject;
742  aMergeDesc.sMailBody = m_sMailBody;
743  aMergeDesc.sAttachmentName = m_sAttachmentName;
744  aMergeDesc.aCopiesTo = m_aCopiesTo;
745  aMergeDesc.aBlindCopiesTo = m_aBlindCopiesTo;
746  aMergeDesc.bSendAsHTML = m_bSendAsHTML;
748 
749  aMergeDesc.bCreateSingleFile = false;
750  pMMConfigItem.reset(new SwMailMergeConfigItem);
751  aMergeDesc.pMailMergeConfigItem = pMMConfigItem.get();
753  *pMMConfigItem,
754  xInService,
756  if( !aMergeDesc.xSmtpServer.is() || !aMergeDesc.xSmtpServer->isConnected())
757  throw RuntimeException("Failed to connect to mail server.", static_cast < cppu::OWeakObject * > ( this ) );
758  }
759  break;
760  }
761 
762  // save document with temporary filename
763  std::shared_ptr<const SfxFilter> pSfxFlt = SwIoSystem::GetFilterOfFormat(
764  FILTER_XML,
765  SwDocShell::Factory().GetFilterContainer() );
766  OUString aExtension(comphelper::string::stripStart(pSfxFlt->GetDefaultExtension(), '*'));
767  utl::TempFile aTempFile( "SwMM", true, &aExtension );
768  m_aTmpFileName = aTempFile.GetURL();
769 
770  Reference< XStorable > xStorable( xCurModel, UNO_QUERY );
771  bool bStoredAsTemporary = false;
772  if ( xStorable.is() )
773  {
774  try
775  {
776  xStorable->storeAsURL( m_aTmpFileName, Sequence< PropertyValue >() );
777  bStoredAsTemporary = true;
778  }
779  catch (const Exception&)
780  {
781  }
782  }
783  if ( !bStoredAsTemporary )
784  throw RuntimeException("Failed to save temporary file.", static_cast < cppu::OWeakObject * > ( this ) );
785 
786  pMgr->SetMergeSilent( true ); // suppress dialogs, message boxes, etc.
787  const SwXMailMerge *pOldSrc = pMgr->GetMailMergeEvtSrc();
788  OSL_ENSURE( !pOldSrc || pOldSrc == this, "Ooops... different event source already set." );
789  pMgr->SetMailMergeEvtSrc( this ); // launch events for listeners
790 
791  SfxGetpApp()->NotifyEvent(SfxEventHint(SfxEventHintId::SwMailMerge, SwDocShell::GetEventName(STR_SW_EVENT_MAIL_MERGE), xCurDocSh.get()));
792  bool bSucc = pMgr->Merge( aMergeDesc );
793  SfxGetpApp()->NotifyEvent(SfxEventHint(SfxEventHintId::SwMailMergeEnd, SwDocShell::GetEventName(STR_SW_EVENT_MAIL_MERGE_END), xCurDocSh.get()));
794 
795  pMgr->SetMailMergeEvtSrc( pOldSrc );
796 
797  if ( xCurModel.get() != m_xModel.get() )
798  { // in case it was a temporary model -> close it, and delete the file
799  DeleteTmpFile_Impl( xCurModel, xCurDocSh, m_aTmpFileName );
800  m_aTmpFileName.clear();
801  }
802  // (in case it wasn't a temporary model, it will be closed in the dtor, at the latest)
803 
804  if (!bSucc)
805  throw Exception("Mail merge failed. Sorry, no further information available.", static_cast < cppu::OWeakObject * > ( this ) );
806 
807  //de-initialize services
808  if(xInService.is() && xInService->isConnected())
809  xInService->disconnect();
810  if(aMergeDesc.xSmtpServer.is() && aMergeDesc.xSmtpServer->isConnected())
811  aMergeDesc.xSmtpServer->disconnect();
812 
813  if (DBMGR_MERGE_SHELL == nMergeType)
814  {
815  return makeAny( aMergeDesc.pMailMergeConfigItem->GetTargetView()->GetDocShell()->GetBaseModel() );
816  }
817  else
818  return makeAny( true );
819 }
820 
821 void SAL_CALL SwXMailMerge::cancel()
822 {
823  // Cancel may be called from a second thread, so this protects from m_pMgr
825  osl::MutexGuard aMgrGuard( GetMailMergeMutex() );
826  if (m_pMgr)
827  m_pMgr->MergeCancel();
828 }
829 
830 void SwXMailMerge::LaunchMailMergeEvent( const MailMergeEvent &rEvt ) const
831 {
832  comphelper::OInterfaceIteratorHelper2 aIt( const_cast<SwXMailMerge *>(this)->m_aMergeListeners );
833  while (aIt.hasMoreElements())
834  {
835  Reference< XMailMergeListener > xRef( aIt.next(), UNO_QUERY );
836  if (xRef.is())
837  xRef->notifyMailMergeEvent( rEvt );
838  }
839 }
840 
841 void SwXMailMerge::launchEvent( const PropertyChangeEvent &rEvt ) const
842 {
843  cppu::OInterfaceContainerHelper *pContainer =
844  m_aPropListeners.getContainer( rEvt.PropertyHandle );
845  if (pContainer)
846  {
847  cppu::OInterfaceIteratorHelper aIt( *pContainer );
848  while (aIt.hasMoreElements())
849  {
850  Reference< XPropertyChangeListener > xRef( aIt.next(), UNO_QUERY );
851  if (xRef.is())
852  xRef->propertyChange( rEvt );
853  }
854  }
855 }
856 
857 uno::Reference< beans::XPropertySetInfo > SAL_CALL SwXMailMerge::getPropertySetInfo( )
858 {
859  SolarMutexGuard aGuard;
860  static Reference< XPropertySetInfo > aRef = m_pPropSet->getPropertySetInfo();
861  return aRef;
862 }
863 
865  const OUString& rPropertyName, const uno::Any& rValue )
866 {
867  SolarMutexGuard aGuard;
868 
869  const SfxItemPropertySimpleEntry* pCur = m_pPropSet->getPropertyMap().getByName( rPropertyName );
870  if (!pCur)
871  throw UnknownPropertyException(rPropertyName);
872  else if (pCur->nFlags & PropertyAttribute::READONLY)
873  throw PropertyVetoException();
874  else
875  {
876  void *pData = nullptr;
877  switch (pCur->nWID)
878  {
879  case WID_SELECTION : pData = &m_aSelection; break;
880  case WID_RESULT_SET : pData = &m_xResultSet; break;
881  case WID_CONNECTION : pData = &m_xConnection; break;
882  case WID_MODEL : pData = &m_xModel; break;
883  case WID_DATA_SOURCE_NAME : pData = &m_aDataSourceName; break;
884  case WID_DATA_COMMAND : pData = &m_aDataCommand; break;
885  case WID_FILTER : pData = &m_aFilter; break;
886  case WID_DOCUMENT_URL : pData = &m_aDocumentURL; break;
887  case WID_OUTPUT_URL : pData = &m_aOutputURL; break;
888  case WID_DATA_COMMAND_TYPE : pData = &m_nDataCommandType; break;
889  case WID_OUTPUT_TYPE : pData = &m_nOutputType; break;
890  case WID_ESCAPE_PROCESSING : pData = &m_bEscapeProcessing; break;
891  case WID_SINGLE_PRINT_JOBS : pData = &m_bSinglePrintJobs; break;
892  case WID_FILE_NAME_FROM_COLUMN : pData = &m_bFileNameFromColumn; break;
893  case WID_FILE_NAME_PREFIX : pData = &m_aFileNamePrefix; break;
894  case WID_MAIL_SUBJECT: pData = &m_sSubject; break;
895  case WID_ADDRESS_FROM_COLUMN: pData = &m_sAddressFromColumn; break;
896  case WID_SEND_AS_HTML: pData = &m_bSendAsHTML; break;
897  case WID_SEND_AS_ATTACHMENT: pData = &m_bSendAsAttachment; break;
898  case WID_MAIL_BODY: pData = &m_sMailBody; break;
899  case WID_ATTACHMENT_NAME: pData = &m_sAttachmentName; break;
900  case WID_ATTACHMENT_FILTER: pData = &m_sAttachmentFilter;break;
901  case WID_PRINT_OPTIONS: pData = &m_aPrintSettings; break;
902  case WID_SAVE_AS_SINGLE_FILE: pData = &m_bSaveAsSingleFile; break;
903  case WID_SAVE_FILTER: pData = &m_sSaveFilter; break;
904  case WID_SAVE_FILTER_OPTIONS: pData = &m_sSaveFilterOptions; break;
905  case WID_SAVE_FILTER_DATA: pData = &m_aSaveFilterData; break;
906  case WID_COPIES_TO: pData = &m_aCopiesTo; break;
907  case WID_BLIND_COPIES_TO: pData = &m_aBlindCopiesTo;break;
908  case WID_IN_SERVER_PASSWORD: pData = &m_sInServerPassword; break;
909  case WID_OUT_SERVER_PASSWORD: pData = &m_sOutServerPassword; break;
910  default :
911  OSL_FAIL("unknown WID");
912  }
913  Any aOld( pData, pCur->aType );
914 
915  bool bChanged = false;
916  bool bOK = true;
917  if (aOld != rValue)
918  {
919  if (pData == &m_aSelection)
920  bOK = rValue >>= m_aSelection;
921  else if (pData == &m_xResultSet)
922  bOK = rValue >>= m_xResultSet;
923  else if (pData == &m_xConnection)
924  bOK = rValue >>= m_xConnection;
925  else if (pData == &m_xModel)
926  bOK = rValue >>= m_xModel;
927  else if (pData == &m_aDataSourceName)
928  bOK = rValue >>= m_aDataSourceName;
929  else if (pData == &m_aDataCommand)
930  bOK = rValue >>= m_aDataCommand;
931  else if (pData == &m_aFilter)
932  bOK = rValue >>= m_aFilter;
933  else if (pData == &m_aDocumentURL)
934  {
935  OUString aText;
936  bOK = rValue >>= aText;
937  if (!aText.isEmpty()
938  && !LoadFromURL_impl( m_xModel, m_xDocSh, aText, true ))
939  throw RuntimeException("Failed to create document from URL: " + aText, static_cast < cppu::OWeakObject * > ( this ) );
940  m_aDocumentURL = aText;
941  }
942  else if (pData == &m_aOutputURL)
943  {
944  OUString aText;
945  bOK = rValue >>= aText;
946  if (!aText.isEmpty())
947  {
948  if (!UCB_IsDirectory(aText))
949  throw IllegalArgumentException("URL does not point to a directory: " + aText, static_cast < cppu::OWeakObject * > ( this ), 0 );
950  if (UCB_IsReadOnlyFileName(aText))
951  throw IllegalArgumentException("URL is read-only: " + aText, static_cast < cppu::OWeakObject * > ( this ), 0 );
952  }
953  m_aOutputURL = aText;
954  }
955  else if (pData == &m_nDataCommandType)
956  bOK = rValue >>= m_nDataCommandType;
957  else if (pData == &m_nOutputType)
958  bOK = rValue >>= m_nOutputType;
959  else if (pData == &m_bEscapeProcessing)
960  bOK = rValue >>= m_bEscapeProcessing;
961  else if (pData == &m_bSinglePrintJobs)
962  bOK = rValue >>= m_bSinglePrintJobs;
963  else if (pData == &m_bFileNameFromColumn)
964  bOK = rValue >>= m_bFileNameFromColumn;
965  else if (pData == &m_aFileNamePrefix)
966  bOK = rValue >>= m_aFileNamePrefix;
967  else if (pData == &m_sSubject)
968  bOK = rValue >>= m_sSubject;
969  else if (pData == &m_sAddressFromColumn)
970  bOK = rValue >>= m_sAddressFromColumn;
971  else if (pData == &m_bSendAsHTML)
972  bOK = rValue >>= m_bSendAsHTML;
973  else if (pData == &m_bSendAsAttachment)
974  bOK = rValue >>= m_bSendAsAttachment;
975  else if (pData == &m_sMailBody)
976  bOK = rValue >>= m_sMailBody;
977  else if (pData == &m_sAttachmentName)
978  bOK = rValue >>= m_sAttachmentName;
979  else if (pData == &m_sAttachmentFilter)
980  bOK = rValue >>= m_sAttachmentFilter;
981  else if (pData == &m_aPrintSettings)
982  bOK = rValue >>= m_aPrintSettings;
983  else if (pData == &m_bSaveAsSingleFile)
984  bOK = rValue >>= m_bSaveAsSingleFile;
985  else if (pData == &m_sSaveFilter)
986  bOK = rValue >>= m_sSaveFilter;
987  else if (pData == &m_sSaveFilterOptions)
988  bOK = rValue >>= m_sSaveFilterOptions;
989  else if (pData == &m_aSaveFilterData)
990  bOK = rValue >>= m_aSaveFilterData;
991  else if (pData == &m_aCopiesTo)
992  bOK = rValue >>= m_aCopiesTo;
993  else if (pData == &m_aBlindCopiesTo)
994  bOK = rValue >>= m_aBlindCopiesTo;
995  else if(pData == &m_sInServerPassword)
996  bOK = rValue >>= m_sInServerPassword;
997  else if(pData == &m_sOutServerPassword)
998  bOK = rValue >>= m_sOutServerPassword;
999  else {
1000  OSL_FAIL("invalid pointer" );
1001  }
1002  OSL_ENSURE( bOK, "set value failed" );
1003  bChanged = true;
1004  }
1005  if (!bOK)
1006  throw IllegalArgumentException("Property type mismatch or property not set: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ), 0 );
1007 
1008  if (bChanged)
1009  {
1010  PropertyChangeEvent aChgEvt( static_cast<XPropertySet *>(this), rPropertyName,
1011  false, pCur->nWID, aOld, rValue );
1012  launchEvent( aChgEvt );
1013  }
1014  }
1015 }
1016 
1018  const OUString& rPropertyName )
1019 {
1020  SolarMutexGuard aGuard;
1021 
1022  Any aRet;
1023 
1024  const SfxItemPropertySimpleEntry* pCur = m_pPropSet->getPropertyMap().getByName( rPropertyName );
1025  if (!pCur)
1026  throw UnknownPropertyException(rPropertyName);
1027 
1028  switch (pCur->nWID)
1029  {
1030  case WID_SELECTION : aRet <<= m_aSelection; break;
1031  case WID_RESULT_SET : aRet <<= m_xResultSet; break;
1032  case WID_CONNECTION : aRet <<= m_xConnection; break;
1033  case WID_MODEL : aRet <<= m_xModel; break;
1034  case WID_DATA_SOURCE_NAME : aRet <<= m_aDataSourceName; break;
1035  case WID_DATA_COMMAND : aRet <<= m_aDataCommand; break;
1036  case WID_FILTER : aRet <<= m_aFilter; break;
1037  case WID_DOCUMENT_URL : aRet <<= m_aDocumentURL; break;
1038  case WID_OUTPUT_URL : aRet <<= m_aOutputURL; break;
1039  case WID_DATA_COMMAND_TYPE : aRet <<= m_nDataCommandType; break;
1040  case WID_OUTPUT_TYPE : aRet <<= m_nOutputType; break;
1041  case WID_ESCAPE_PROCESSING : aRet <<= m_bEscapeProcessing; break;
1042  case WID_SINGLE_PRINT_JOBS : aRet <<= m_bSinglePrintJobs; break;
1043  case WID_FILE_NAME_FROM_COLUMN : aRet <<= m_bFileNameFromColumn; break;
1044  case WID_FILE_NAME_PREFIX : aRet <<= m_aFileNamePrefix; break;
1045  case WID_MAIL_SUBJECT: aRet <<= m_sSubject; break;
1046  case WID_ADDRESS_FROM_COLUMN: aRet <<= m_sAddressFromColumn; break;
1047  case WID_SEND_AS_HTML: aRet <<= m_bSendAsHTML; break;
1048  case WID_SEND_AS_ATTACHMENT: aRet <<= m_bSendAsAttachment; break;
1049  case WID_MAIL_BODY: aRet <<= m_sMailBody; break;
1050  case WID_ATTACHMENT_NAME: aRet <<= m_sAttachmentName; break;
1051  case WID_ATTACHMENT_FILTER: aRet <<= m_sAttachmentFilter;break;
1052  case WID_PRINT_OPTIONS: aRet <<= m_aPrintSettings; break;
1053  case WID_SAVE_AS_SINGLE_FILE: aRet <<= m_bSaveAsSingleFile; break;
1054  case WID_SAVE_FILTER: aRet <<= m_sSaveFilter; break;
1055  case WID_SAVE_FILTER_OPTIONS: aRet <<= m_sSaveFilterOptions; break;
1056  case WID_SAVE_FILTER_DATA: aRet <<= m_aSaveFilterData; break;
1057  case WID_COPIES_TO: aRet <<= m_aCopiesTo; break;
1058  case WID_BLIND_COPIES_TO: aRet <<= m_aBlindCopiesTo;break;
1059  case WID_IN_SERVER_PASSWORD: aRet <<= m_sInServerPassword; break;
1060  case WID_OUT_SERVER_PASSWORD: aRet <<= m_sOutServerPassword; break;
1061  default :
1062  OSL_FAIL("unknown WID");
1063  }
1064 
1065  return aRet;
1066 }
1067 
1069  const OUString& rPropertyName,
1070  const uno::Reference< beans::XPropertyChangeListener >& rListener )
1071 {
1072  SolarMutexGuard aGuard;
1073  if (!m_bDisposing && rListener.is())
1074  {
1075  const SfxItemPropertySimpleEntry* pCur = m_pPropSet->getPropertyMap().getByName( rPropertyName );
1076  if (!pCur)
1077  throw UnknownPropertyException(rPropertyName);
1078  m_aPropListeners.addInterface( pCur->nWID, rListener );
1079  }
1080 }
1081 
1083  const OUString& rPropertyName,
1084  const uno::Reference< beans::XPropertyChangeListener >& rListener )
1085 {
1086  SolarMutexGuard aGuard;
1087  if (!m_bDisposing && rListener.is())
1088  {
1089  const SfxItemPropertySimpleEntry* pCur = m_pPropSet->getPropertyMap().getByName( rPropertyName );
1090  if (!pCur)
1091  throw UnknownPropertyException(rPropertyName);
1092  m_aPropListeners.removeInterface( pCur->nWID, rListener );
1093  }
1094 }
1095 
1097  const OUString& /*rPropertyName*/,
1098  const uno::Reference< beans::XVetoableChangeListener >& /*rListener*/ )
1099 {
1100  // no vetoable property, thus no support for vetoable change listeners
1101  OSL_FAIL("not implemented");
1102 }
1103 
1105  const OUString& /*rPropertyName*/,
1106  const uno::Reference< beans::XVetoableChangeListener >& /*rListener*/ )
1107 {
1108  // no vetoable property, thus no support for vetoable change listeners
1109  OSL_FAIL("not implemented");
1110 }
1111 
1112 void SAL_CALL SwXMailMerge::dispose()
1113 {
1114  SolarMutexGuard aGuard;
1115 
1116  if (!m_bDisposing)
1117  {
1118  m_bDisposing = true;
1119 
1120  EventObject aEvtObj( static_cast<XPropertySet *>(this) );
1121  m_aEvtListeners.disposeAndClear( aEvtObj );
1123  m_aPropListeners.disposeAndClear( aEvtObj );
1124  }
1125 }
1126 
1128  const Reference< XEventListener >& rxListener )
1129 {
1130  SolarMutexGuard aGuard;
1131  if (!m_bDisposing && rxListener.is())
1132  m_aEvtListeners.addInterface( rxListener );
1133 }
1134 
1136  const Reference< XEventListener >& rxListener )
1137 {
1138  SolarMutexGuard aGuard;
1139  if (!m_bDisposing && rxListener.is())
1140  m_aEvtListeners.removeInterface( rxListener );
1141 }
1142 
1144  const uno::Reference< XMailMergeListener >& rxListener )
1145 {
1146  SolarMutexGuard aGuard;
1147  if (!m_bDisposing && rxListener.is())
1148  m_aMergeListeners.addInterface( rxListener );
1149 }
1150 
1152  const uno::Reference< XMailMergeListener >& rxListener )
1153 {
1154  SolarMutexGuard aGuard;
1155  if (!m_bDisposing && rxListener.is())
1156  m_aMergeListeners.removeInterface( rxListener );
1157 }
1158 
1160 {
1161  return "SwXMailMerge";
1162 }
1163 
1164 sal_Bool SAL_CALL SwXMailMerge::supportsService( const OUString& rServiceName )
1165 {
1166  return cppu::supportsService(this, rServiceName);
1167 }
1168 
1169 uno::Sequence< OUString > SAL_CALL SwXMailMerge::getSupportedServiceNames()
1170 {
1171  return { "com.sun.star.text.MailMerge", "com.sun.star.sdb.DataAccessDescriptor" };
1172 }
1173 
1174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString m_aDocumentURL
virtual void SAL_CALL removeMailMergeEventListener(const css::uno::Reference< css::text::XMailMergeListener > &xListener) override
bool bCreateSingleFile
Create a single or multiple results.
Definition: dbmgr.hxx:149
const SwXMailMerge * GetMailMergeEvtSrc() const
MailMergeEvent source.
Definition: dbmgr.hxx:296
void MergeCancel()
Definition: dbmgr.cxx:1720
exports com.sun.star. sdb
uno::Reference< mail::XSmtpService > ConnectToSmtpServer(SwMailMergeConfigItem const &rConfigItem, uno::Reference< mail::XMailService > &rxInMailService, const OUString &rInMailServerPassword, const OUString &rOutMailServerPassword, weld::Window *pDialogParentWindow)
void SetMailMergeEvtSrc(const SwXMailMerge *pSrc)
Definition: dbmgr.hxx:297
#define WID_DATA_COMMAND_TYPE
Definition: unomap.hxx:263
OUString m_sAttachmentName
osl::Mutex m_aMutex
#define UNO_NAME_OUTPUT_TYPE
Definition: unoprnms.hxx:721
SwMailMergeConfigItem * pMailMergeConfigItem
Definition: dbmgr.hxx:218
OUString m_sOutServerPassword
void setDataSource(const OUString &_sDataSourceNameOrLocation)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
#define UNO_NAME_SUBJECT
Definition: unoprnms.hxx:750
std::unique_ptr< ContentProperties > pData
static CloseResult CloseModelAndDocSh(Reference< frame::XModel > const &rxModel, SfxObjectShellRef &rxDocSh)
Reference< XOfficeDatabaseDocument > m_xDocument
const SfxItemPropertySet * m_pPropSet
OUString m_sSaveFilter
#define WID_OUT_SERVER_PASSWORD
Definition: unomap.hxx:282
#define UNO_NAME_CONNECTION
Definition: unoprnms.hxx:715
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
bool UCB_IsReadOnlyFileName(const OUString &rURL)
#define WID_ATTACHMENT_NAME
Definition: unomap.hxx:274
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static bool LoadFromURL_impl(Reference< frame::XModel > &rxModel, SfxObjectShellRef &rxDocSh, const OUString &rURL, bool bClose)
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
css::uno::Reference< css::mail::XSmtpService > xSmtpServer
Definition: dbmgr.hxx:192
OUString m_sAddressFromColumn
#define UNO_NAME_DATA_SOURCE_NAME
Definition: unoprnms.hxx:717
#define WID_SINGLE_PRINT_JOBS
Definition: unomap.hxx:266
#define WID_PRINT_OPTIONS
Definition: unomap.hxx:276
void disposeAndClear(const css::lang::EventObject &rEvt)
bool bSendAsAttachment
Definition: dbmgr.hxx:194
sal_Int16 m_nOutputType
eSuccess
OUString m_sMailBody
#define UNO_NAME_BLIND_COPIES_TO
Definition: unoprnms.hxx:761
virtual css::uno::Any SAL_CALL execute(const css::uno::Sequence< css::beans::NamedValue > &Arguments) override
#define WID_FILE_NAME_FROM_COLUMN
Definition: unomap.hxx:267
css::uno::Reference< css::frame::XModel > m_xModel
css::uno::Reference< css::frame::XModel > GetModel() const
bool UCB_DeleteFile(const OUString &rURL)
Definition: swunohelper.cxx:58
sal_Int32 removeInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
#define UNO_NAME_ESCAPE_PROCESSING
Definition: unoprnms.hxx:722
#define WID_SELECTION
Definition: unomap.hxx:254
css::uno::XInterface *SAL_CALL next()
css::uno::Sequence< css::beans::PropertyValue > aSaveToFilterData
Definition: dbmgr.hxx:157
css::uno::Reference< css::beans::XPropertySetInfo > const & getPropertySetInfo() const
#define UNO_NAME_DOCUMENT_URL
Definition: unoprnms.hxx:719
virtual ~SwXMailMerge() override
#define UNO_NAME_SAVE_FILTER_DATA
Definition: unoprnms.hxx:765
SfxApplication * SfxGetpApp()
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
#define UNO_NAME_MODEL
Definition: unoprnms.hxx:716
OUString GetBase() const
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
OUString m_sAttachmentFilter
css::uno::Any const & rValue
bool m_bSendAsAttachment
constexpr auto SFX_INTERFACE_NONE
#define WID_RESULT_SET
Definition: unomap.hxx:255
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
#define WID_COPIES_TO
Definition: unomap.hxx:279
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
Print mail merge.
Definition: dbmgr.hxx:88
#define UNO_NAME_RESULT_SET
Definition: unoprnms.hxx:714
#define UNO_NAME_MAIL_BODY
Definition: unoprnms.hxx:754
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
#define UNO_NAME_SAVE_FILTER_OPTIONS
Definition: unoprnms.hxx:764
#define UNO_NAME_COPIES_TO
Definition: unoprnms.hxx:760
Mutex aMutex
DBManagerOptions
Definition: dbmgr.hxx:85
Send mail merge as email.
Definition: dbmgr.hxx:89
#define WID_FILTER
Definition: unomap.hxx:260
#define UNO_NAME_ATTACHMENT_FILTER
Definition: unoprnms.hxx:756
OUString sSaveToFilter
Definition: dbmgr.hxx:155
sal_Int32 SAL_CALL removeInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
void launchEvent(const css::beans::PropertyChangeEvent &rEvt) const
Create merge doc and keep the doc shell.
Definition: dbmgr.hxx:91
css::uno::Reference< css::frame::XModel > GetBaseModel() const
OUString sMailBody
Definition: dbmgr.hxx:188
virtual void SAL_CALL addMailMergeEventListener(const css::uno::Reference< css::text::XMailMergeListener > &xListener) override
css::uno::Sequence< css::beans::PropertyValue > m_aSaveFilterData
#define WID_SEND_AS_HTML
Definition: unomap.hxx:271
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
#define UNO_NAME_SAVE_FILTER
Definition: unoprnms.hxx:759
css::uno::Reference< css::sdbc::XConnection > m_xConnection
OUString m_sSubject
bool Merge(const SwMergeDescriptor &rMergeDesc)
Merging of data records into fields.
Definition: dbmgr.cxx:420
SwDBManager * GetDBManager() const
For evaluation of DB fields (new DB-manager).
Definition: edfld.cxx:337
OUString sPrefix
Basename incl.
Definition: dbmgr.hxx:172
const IDocumentDeviceAccess & getIDocumentDeviceAccess() const
Provides access to the document device interface.
Definition: viewsh.cxx:2585
OUString sSaveToFilterOptions
Definition: dbmgr.hxx:156
T * get() const
static SW_DLLPUBLIC std::shared_ptr< const SfxFilter > GetFilterOfFormat(const OUString &rFormat, const SfxFilterContainer *pCnt=nullptr)
find for an internal format name the corresponding filter entry
Definition: iodetect.cxx:67
#define WID_SAVE_FILTER
Definition: unomap.hxx:278
#define STR_SW_EVENT_MAIL_MERGE_END
Definition: swevent.hxx:29
sal_Int32 m_nDataCommandType
#define WID_ESCAPE_PROCESSING
Definition: unomap.hxx:265
OUString sSubject
Definition: dbmgr.hxx:187
OUString m_aFileNamePrefix
virtual OUString SAL_CALL getImplementationName() override
virtual const SwPrintData & getPrintData() const =0
Returns the PrintData.
#define WID_DOCUMENT_URL
Definition: unomap.hxx:261
OUString const & GetURL() const
void SetSmartProtocol(INetProtocol eTheSmartScheme)
void SAL_CALL disposeAndClear(const css::lang::EventObject &rEvt)
#define UNO_NAME_PRINT_OPTIONS
Definition: unoprnms.hxx:757
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
static osl::Mutex & GetMailMergeMutex()
void SetMergeSilent(bool bVal)
Definition: dbmgr.hxx:300
OUString m_aDataSourceName
#define UNO_NAME_SINGLE_PRINT_JOBS
Definition: unoprnms.hxx:723
OUString sAttachmentName
Definition: dbmgr.hxx:189
#define UNO_NAME_SEND_AS_HTML
Definition: unoprnms.hxx:752
#define WID_SEND_AS_ATTACHMENT
Definition: unomap.hxx:272
#define UNO_NAME_FILE_NAME_PREFIX
Definition: unoprnms.hxx:725
#define WID_SAVE_AS_SINGLE_FILE
Definition: unomap.hxx:277
void NotifyEvent(const SfxEventHint &rEvent, bool bSynchron=true)
OUString m_aOutputURL
unsigned char sal_Bool
SfxObjectShellRef m_xDocSh
const SfxItemPropertyMap & getPropertyMap() const
#define WID_MAIL_BODY
Definition: unomap.hxx:273
void LaunchMailMergeEvent(const css::text::MailMergeEvent &rData) const
bool m_bEscapeProcessing
#define WID_BLIND_COPIES_TO
Definition: unomap.hxx:280
virtual void SAL_CALL dispose() override
css::uno::Sequence< OUString > m_aCopiesTo
bool UCB_IsDirectory(const OUString &rURL)
const SfxItemPropertySimpleEntry * getByName(const OUString &rName) const
SfxViewShell * GetViewShell() const
#define WID_OUTPUT_TYPE
Definition: unomap.hxx:264
CloseResult
css::uno::Sequence< css::beans::PropertyValue > m_aPrintSettings
SwDocShell * GetDocShell()
Definition: view.cxx:1108
#define UNO_NAME_ATTACHMENT_NAME
Definition: unoprnms.hxx:755
#define WID_SAVE_FILTER_DATA
Definition: unomap.hxx:284
#define WID_SAVE_FILTER_OPTIONS
Definition: unomap.hxx:283
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
Provides access to the formatting devices of a document.
MailMergeExecuteFinalizer(SwXMailMerge *mailmerge)
#define WID_DATA_COMMAND
Definition: unomap.hxx:259
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
sal_Int32 SAL_CALL addInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &r)
#define WID_MODEL
Definition: unomap.hxx:257
#define WID_IN_SERVER_PASSWORD
Definition: unomap.hxx:281
Reference< XMultiServiceFactory > getProcessServiceFactory()
css::uno::Sequence< OUString > aCopiesTo
Definition: dbmgr.hxx:190
#define UNO_NAME_SAVE_AS_SINGLE_FILE
Definition: unoprnms.hxx:758
css::uno::Sequence< css::beans::PropertyValue > aPrintOptions
Definition: dbmgr.hxx:215
void SetPrintSingleJobs(bool b)
Definition: printdata.hxx:164
::utl::SharedUNOComponent< XInterface > SharedComponent
MailMergeExecuteFinalizer & operator=(MailMergeExecuteFinalizer const &)=delete
Save mail merge as files.
Definition: dbmgr.hxx:90
static SfxViewFrame * LoadHiddenDocument(SfxObjectShell const &i_rDoc, SfxInterfaceId i_nViewId)
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
OUString m_sInServerPassword
#define STR_SW_EVENT_MAIL_MERGE
Definition: swevent.hxx:28
#define FILTER_XML
XML filter.
Definition: iodetect.hxx:35
#define UNO_NAME_OUTPUT_URL
Definition: unoprnms.hxx:720
#define WID_DATA_SOURCE_NAME
Definition: unomap.hxx:258
bool m_bSinglePrintJobs
#define UNO_NAME_FILTER
Definition: unoprnms.hxx:718
#define WID_FILE_NAME_PREFIX
Definition: unomap.hxx:268
bool DoInitNew(SfxMedium *pMedium=nullptr)
OUString m_sSaveFilterOptions
virtual void setPrintData(const SwPrintData &rPrtData)=0
Sets the PrintData.
comphelper::OInterfaceContainerHelper2 m_aMergeListeners
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
css::uno::Sequence< OUString > m_aBlindCopiesTo
css::uno::Sequence< OUString > aBlindCopiesTo
Definition: dbmgr.hxx:191
Reference< XComponentContext > getProcessComponentContext()
SwUnoPropertyMapProvider aSwMapProvider
Definition: unomap1.cxx:89
OString stripStart(const OString &rIn, char c)
#define WID_CONNECTION
Definition: unomap.hxx:256
TABLE
OUString sDBcolumn
DB column to fetch EMail of Filename from.
Definition: dbmgr.hxx:203
OUString m_aDataCommand
#define UNO_NAME_DAD_COMMAND
Definition: unoprnms.hxx:727
OUString m_aTmpFileName
static OUString GetEventName(sal_Int32 nId)
Definition: docsh.cxx:1314
virtual void CalcLayout() override
To enable set up of StartActions and EndActions.
Definition: edws.cxx:108
OUString m_aFilter
static SfxViewFrame * GetFirst(const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
#define WID_ADDRESS_FROM_COLUMN
Definition: unomap.hxx:270
#define UNO_NAME_OUT_SERVER_PASSWORD
Definition: unoprnms.hxx:763
#define WID_OUTPUT_URL
Definition: unomap.hxx:262
OPropertyListenerContainerHelper m_aPropListeners
css::uno::XInterface * next()
void reset(const css::uno::Reference< INTERFACE > &_rxComponent, AssignmentMode _eMode=TakeOwnership)
#define UNO_NAME_SEND_AS_ATTACHMENT
Definition: unoprnms.hxx:753
virtual void SAL_CALL cancel() override
bool m_bSaveAsSingleFile
#define WID_ATTACHMENT_FILTER
Definition: unomap.hxx:275
#define WID_MAIL_SUBJECT
Definition: unomap.hxx:269
css::uno::Reference< css::sdbc::XResultSet > m_xResultSet
css::uno::Sequence< css::uno::Any > m_aSelection
OInterfaceContainerHelper *SAL_CALL getContainer(const key &) const
#define UNO_NAME_IN_SERVER_PASSWORD
Definition: unoprnms.hxx:762
comphelper::OInterfaceContainerHelper2 m_aEvtListeners
#define UNO_NAME_SELECTION
Definition: unoprnms.hxx:713
#define UNO_NAME_ADDRESS_FROM_COLUMN
Definition: unoprnms.hxx:751
#define PROPERTY_MAP_MAILMERGE
Definition: unomap.hxx:115
Definition: view.hxx:144
#define UNO_NAME_FILE_NAME_FROM_COLUMN
Definition: unoprnms.hxx:724
IMPL_LINK_NOARG(FormFieldButton, FieldPopupModeEndHdl, FloatingWindow *, void)
static bool DeleteTmpFile_Impl(Reference< frame::XModel > &rxModel, SfxObjectShellRef &rxDocSh, const OUString &rTmpFileURL)
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
bool SetSmartURL(OUString const &rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
bool m_bFileNameFromColumn
bool SAL_CALL hasMoreElements() const
SwDBManager * m_pMgr
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
#define UNO_NAME_DAD_COMMAND_TYPE
Definition: unoprnms.hxx:728
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)