1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <rtl/ustring.hxx>
22 #include <tools/solar.h>
23 #include <tools/long.hxx>
24 #include <i18nlangtag/lang.h>
25 #include <com/sun/star/util/Date.hpp>
27 #include "swdbdata.hxx"
28 #include <com/sun/star/uno/Reference.h>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/lang/Locale.hpp>
31 #include <com/sun/star/beans/PropertyValue.hpp>
37 namespace com::sun::star
{
49 class XColumnsSupplier
;
52 class XNumberFormatter
;
57 namespace embed
{ class XStorage
; }
58 namespace frame
{ class XStorable
; }
61 class ODataAccessDescriptor
;
66 css::util::Date aNullDate
;
67 css::uno::Reference
< css::util::XNumberFormatter
> xFormatter
;
68 css::lang::Locale aLocale
;
79 class SvNumberFormatter
;
81 class SwMailMergeConfigItem
;
88 DBMGR_MERGE
, ///< Data records in fields.
89 DBMGR_MERGE_PRINTER
, ///< Print mail merge.
90 DBMGR_MERGE_EMAIL
, ///< Send mail merge as email.
91 DBMGR_MERGE_FILE
, ///< Save mail merge as files.
92 DBMGR_MERGE_SHELL
///< Create merge doc and keep the doc shell.
95 // Administration of (new) logical databases.
101 struct SwDSParam
: public SwDBData
103 css::util::Date aNullDate
;
105 css::uno::Reference
<css::util::XNumberFormatter
> xFormatter
;
106 css::uno::Reference
< css::sdbc::XConnection
> xConnection
;
107 css::uno::Reference
< css::sdbc::XStatement
> xStatement
;
108 css::uno::Reference
< css::sdbc::XResultSet
> xResultSet
;
109 css::uno::Sequence
< css::uno::Any
> aSelection
;
112 tools::Long nSelectionIndex
;
114 SwDSParam(const SwDBData
& rData
) :
121 SwDSParam(const SwDBData
& rData
,
122 css::uno::Reference
< css::sdbc::XResultSet
> xResSet
,
123 const css::uno::Sequence
< css::uno::Any
>& rSelection
) :
125 xResultSet(std::move(xResSet
)),
126 aSelection(rSelection
),
132 bool HasValidRecord() const
133 { return( !bEndOfDB
&& xResultSet
.is() ); }
136 typedef std::vector
<std::unique_ptr
<SwDSParam
>> SwDSParams_t
;
138 struct SwMergeDescriptor
140 const DBManagerOptions nMergeType
;
142 const svx::ODataAccessDescriptor
& rDescriptor
;
145 * Create a single or multiple results
147 * This currently just affects FILE, as EMAIL is always
148 * multiple and SHELL and PRINTER are always single.
150 bool bCreateSingleFile
;
153 * @defgroup save Export filter settings
156 OUString sSaveToFilter
;
157 OUString sSaveToFilterOptions
;
158 css::uno::Sequence
< css::beans::PropertyValue
> aSaveToFilterData
;
162 * @defgroup file Mail merge as File settings
167 * Basename incl. the path for the generated files.
169 * The final filename will be created by concatenating a number to prevent
170 * overwriting an existing file and the extension based on the filter
175 * Use the sPrefix as the target filename also overwriting an existing
178 * Just used for the internal mail merge dialogs as mail merge never
179 * overwrites existing files (see SwDBManager::ExecuteFormLetter).
181 bool bPrefixIsFilename
;
185 * @defgroup email Mail merge as eMail settings
190 OUString sAttachmentName
;
191 css::uno::Sequence
< OUString
> aCopiesTo
;
192 css::uno::Sequence
< OUString
> aBlindCopiesTo
;
193 css::uno::Reference
< css::mail::XSmtpService
> xSmtpServer
;
195 bool bSendAsAttachment
;
199 * @addtogroup file email
202 /** DB column to fetch EMail of Filename from
206 /** DB column to fetch password
208 OUString sDBPasswordColumn
;
213 * @defgroup print Mail merge to Printer
216 css::uno::Sequence
< css::beans::PropertyValue
> aPrintOptions
;
219 SwMailMergeConfigItem
* pMailMergeConfigItem
;
221 SwMergeDescriptor( const DBManagerOptions nType
,
223 const svx::ODataAccessDescriptor
& rDesc
) :
227 bCreateSingleFile( false ),
228 bPrefixIsFilename( false ),
230 bSendAsAttachment( false ),
231 pMailMergeConfigItem( nullptr )
233 if( nType
== DBMGR_MERGE_SHELL
|| nType
== DBMGR_MERGE_PRINTER
)
234 bCreateSingleFile
= true;
242 struct SwDBManager_Impl
;
243 class ConnectionDisposedListener_Impl
;
244 class MailDispatcherListener_Impl
;
246 enum class MergeStatus
248 Ok
= 0, Cancel
, Error
251 MergeStatus m_aMergeStatus
; ///< current / last merge status
252 bool m_bInitDBFields
: 1;
253 bool m_bInMerge
: 1; ///< merge process active
254 bool m_bMergeSilent
: 1; ///< suppress display of dialogs/boxes (used when called over API)
255 SwDSParams_t m_DataSourceParams
;
256 std::unique_ptr
<SwDBManager_Impl
> m_pImpl
;
257 const SwXMailMerge
* m_pMergeEvtSrc
; ///< != 0 if mail merge events are to be send
258 /// Name of the embedded database that's included in the current document.
259 OUString m_sEmbeddedName
;
261 /// Store last registrations to revoke or commit
262 static std::vector
<std::pair
<SwDocShell
*, OUString
>> s_aUncommittedRegistrations
;
264 /// Not used connections.
265 std::vector
<OUString
> m_aNotUsedConnections
;
267 /// Set connection as used.
268 void SetAsUsed(const OUString
& rName
);
270 /// The document that owns this manager.
273 SwDSParam
* FindDSData(const SwDBData
& rData
, bool bCreate
);
274 SwDSParam
* FindDSConnection(const OUString
& rSource
, bool bCreate
);
276 /// Insert data record as text into document.
277 void ImportFromConnection( SwWrtShell
* pSh
);
279 /// Insert a single data record as text into document.
280 void ImportDBEntry(SwWrtShell
* pSh
);
282 /// Run the mail merge for defined modes, except DBMGR_MERGE
283 bool MergeMailFiles( SwWrtShell
* pSh
,
284 const SwMergeDescriptor
& rMergeDescriptor
);
286 bool ToNextMergeRecord();
287 bool IsValidMergeRecord() const;
290 SwDBManager(SwDBManager
const&) = delete;
291 SwDBManager
& operator=(SwDBManager
const&) = delete;
294 SwDBManager(SwDoc
* pDoc
);
297 /// MailMergeEvent source
298 const SwXMailMerge
* GetMailMergeEvtSrc() const { return m_pMergeEvtSrc
; }
299 void SetMailMergeEvtSrc( const SwXMailMerge
*pSrc
) { m_pMergeEvtSrc
= pSrc
; }
301 bool IsMergeSilent() const { return m_bMergeSilent
; }
302 void SetMergeSilent( bool bVal
) { m_bMergeSilent
= bVal
; }
304 /// Merging of data records into fields.
305 bool Merge( const SwMergeDescriptor
& rMergeDesc
);
308 bool IsMergeOk() const { return MergeStatus::Ok
== m_aMergeStatus
; }
309 bool IsMergeError() const { return MergeStatus::Error
<= m_aMergeStatus
; }
311 SW_DLLPUBLIC
static std::shared_ptr
<SwMailMergeConfigItem
> PerformMailMerge(SwView
const * pView
);
313 /// Initialize data fields that lack name of database.
314 bool IsInitDBFields() const { return m_bInitDBFields
; }
315 void SetInitDBFields(bool b
) { m_bInitDBFields
= b
; }
317 /// Fill listbox with all table names of a database.
318 SW_DLLPUBLIC
bool GetTableNames(weld::ComboBox
& rBox
, const OUString
& rDBName
);
320 /// Fill listbox with all column names of a database table.
321 SW_DLLPUBLIC
void GetColumnNames(weld::ComboBox
& rBox
,
322 const OUString
& rDBName
, const OUString
& rTableName
);
323 SW_DLLPUBLIC
static void GetColumnNames(weld::ComboBox
& rBox
,
324 css::uno::Reference
< css::sdbc::XConnection
> const & xConnection
,
325 const OUString
& rTableName
);
327 static sal_uInt32
GetColumnFormat( css::uno::Reference
< css::sdbc::XDataSource
> const & xSource
,
328 css::uno::Reference
< css::sdbc::XConnection
> const & xConnection
,
329 css::uno::Reference
< css::beans::XPropertySet
> const & xColumn
,
330 SvNumberFormatter
* pNFormatr
,
331 LanguageType nLanguage
);
333 sal_uInt32
GetColumnFormat( const OUString
& rDBName
,
334 const OUString
& rTableName
,
335 const OUString
& rColNm
,
336 SvNumberFormatter
* pNFormatr
,
337 LanguageType nLanguage
);
338 sal_Int32
GetColumnType( const OUString
& rDBName
,
339 const OUString
& rTableName
,
340 const OUString
& rColNm
);
342 bool IsInMerge() const { return m_bInMerge
; }
344 void ExecuteFormLetter(SwWrtShell
& rSh
,
345 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
);
347 static void InsertText(SwWrtShell
& rSh
,
348 const css::uno::Sequence
< css::beans::PropertyValue
>& rProperties
);
350 /// check if a data source is open
351 bool IsDataSourceOpen(const OUString
& rDataSource
,
352 const OUString
& rTableOrQuery
, bool bMergeShell
);
354 /// open the source while fields are updated - for the calculator only!
355 bool OpenDataSource(const OUString
& rDataSource
, const OUString
& rTableOrQuery
);
356 sal_uInt32
GetSelectedRecordId(const OUString
& rDataSource
, const OUString
& rTableOrQuery
, sal_Int32 nCommandType
= -1);
357 bool GetColumnCnt(const OUString
& rSourceName
, const OUString
& rTableName
,
358 const OUString
& rColumnName
, sal_uInt32 nAbsRecordId
, LanguageType nLanguage
,
359 OUString
& rResult
, double* pNumber
);
360 /** create and store or find an already stored connection to a data source for use
361 in SwFieldMgr and SwDBTreeList */
362 css::uno::Reference
< css::sdbc::XConnection
> const &
363 RegisterConnection(OUString
const& rSource
);
365 void CreateDSData(const SwDBData
& rData
)
366 { FindDSData(rData
, true); }
367 const SwDSParams_t
& GetDSParamArray() const { return m_DataSourceParams
; }
369 /// close all data sources - after fields were updated
370 void CloseAll(bool bIncludingMerge
= true);
372 bool GetMergeColumnCnt(const OUString
& rColumnName
, LanguageType nLanguage
,
373 OUString
&rResult
, double *pNumber
);
374 bool FillCalcWithMergeData(SvNumberFormatter
*pDocFormatter
,
375 LanguageType nLanguage
, SwCalc
&aCalc
);
376 void ToNextRecord(const OUString
& rDataSource
, const OUString
& rTableOrQuery
);
378 sal_uInt32
GetSelectedRecordId();
379 bool ToRecordId(sal_Int32 nSet
);
381 static const SwDBData
& GetAddressDBName();
383 SW_DLLPUBLIC
static OUString
GetDBField(
384 css::uno::Reference
< css::beans::XPropertySet
> const & xColumnProp
,
385 const SwDBFormatData
& rDBFormatData
,
386 double *pNumber
= nullptr);
388 static css::uno::Reference
< css::sdbc::XConnection
>
389 GetConnection(const OUString
& rDataSource
,
390 css::uno::Reference
< css::sdbc::XDataSource
>& rxSource
,
391 const SwView
* pView
);
393 SW_DLLPUBLIC
static css::uno::Reference
< css::sdbcx::XColumnsSupplier
>
394 GetColumnSupplier(css::uno::Reference
< css::sdbc::XConnection
> const & xConnection
,
395 const OUString
& rTableOrQuery
,
396 SwDBSelect eTableOrQuery
= SwDBSelect::UNKNOWN
);
398 SW_DLLPUBLIC
static css::uno::Sequence
<OUString
> GetExistingDatabaseNames();
401 Loads a data source from file and registers it.
403 This function requires GUI interaction, as it loads the data source from
404 the filename returned by a file picker and additional settings dialog.
405 In case of success it returns the registered name, otherwise an empty string.
407 SW_DLLPUBLIC
static OUString
LoadAndRegisterDataSource(weld::Window
* pParent
, SwDocShell
* pDocShell
= nullptr);
410 Loads a data source from file and registers it.
412 Convenience function, which calls GetDBunoURI and has just one mandatory parameter.
413 In case of success it returns the registered name, otherwise an empty string.
415 SW_DLLPUBLIC
static OUString
LoadAndRegisterDataSource(std::u16string_view rURI
, const OUString
*pDestDir
);
417 /// Load the embedded data source of the document and also register it.
418 void LoadAndRegisterEmbeddedDataSource(const SwDBData
& rData
, const SwDocShell
& rDocShell
);
420 /// Unregister a data source.
421 SW_DLLPUBLIC
static void RevokeDataSource(const OUString
& rName
);
423 /** try to get the data source from the given connection through the XChild interface.
424 If this is not possible, the data source will be created through its name.
426 The connection which should support the XChild interface. (not a must)
427 @param _sDataSourceName
428 The data source name will be used to create the data source when the connection can not be used for it.
432 static css::uno::Reference
< css::sdbc::XDataSource
>
433 getDataSourceAsParent(const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,const OUString
& _sDataSourceName
);
435 /** creates a RowSet, which must be disposed after use.
436 @param _sDataSourceName
441 The type of the command.
443 The active connection which may be <NULL/>.
445 The new created RowSet.
448 SW_DLLPUBLIC
static css::uno::Reference
< css::sdbc::XResultSet
>
449 createCursor(const OUString
& _sDataSourceName
,
450 const OUString
& _sCommand
,
451 sal_Int32 _nCommandType
,
452 const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,
453 const SwView
* pView
);
455 void setEmbeddedName(const OUString
& rEmbeddedName
, SwDocShell
& rDocShell
);
456 const OUString
& getEmbeddedName() const;
458 // rOwnURL should be taken using INetURLObject::GetMainURL(INetURLObject::DecodeMechanism::NONE)
459 static void StoreEmbeddedDataSource(const css::uno::Reference
<css::frame::XStorable
>& xStorable
,
460 const css::uno::Reference
<css::embed::XStorage
>& xStorage
,
461 const OUString
& rStreamRelPath
,
462 const OUString
& rOwnURL
, bool bCopyTo
= false);
464 SwDoc
* getDoc() const;
465 /// Stop reacting to removed database registrations.
466 void releaseRevokeListener();
468 /// Revoke not committed registrations in case of mail merge cancel
469 SW_DLLPUBLIC
void RevokeLastRegistrations();
471 /// Accept not committed registrations
472 SW_DLLPUBLIC
void CommitLastRegistrations();
474 /// Remove not used connections.
475 void RevokeNotUsedConnections();
480 enum class DBConnURIType
491 DBConnURIType SW_DLLPUBLIC
GetDBunoType(const INetURLObject
&rURL
);
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */