Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / sw / inc / dbmgr.hxx
blob6d3357333864670703fd67de577e49f975380bff
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #ifndef INCLUDED_SW_INC_DBMGR_HXX
20 #define INCLUDED_SW_INC_DBMGR_HXX
22 #include <rtl/ustring.hxx>
23 #include <tools/link.hxx>
24 #include <i18nlangtag/lang.h>
25 #include <com/sun/star/util/Date.hpp>
26 #include "swdllapi.h"
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>
32 #include <com/sun/star/frame/XStorable.hpp>
33 #include <com/sun/star/embed/XStorage.hpp>
35 #include <memory>
36 #include <vector>
38 namespace com{namespace sun{namespace star{
39 namespace sdbc{
40 class XConnection;
41 class XStatement;
42 class XDataSource;
43 class XResultSet;
45 namespace beans{
47 class XPropertySet;
48 struct PropertyValue;
50 namespace sdbcx{
51 class XColumnsSupplier;
53 namespace util{
54 class XNumberFormatter;
56 namespace mail{
57 class XSmtpService;
59 }}}
60 namespace svx {
61 class ODataAccessDescriptor;
64 struct SwDBFormatData
66 css::util::Date aNullDate;
67 css::uno::Reference< css::util::XNumberFormatter> xFormatter;
68 css::lang::Locale aLocale;
71 namespace vcl {
72 class Window;
75 class SwView;
76 class SwWrtShell;
77 class SfxProgress;
78 class ListBox;
79 class Button;
80 class SvNumberFormatter;
81 class SwXMailMerge;
82 class SwMailMergeConfigItem;
83 class SwCalc;
84 class INetURLObject;
85 class SwDocShell;
87 enum DBManagerOptions
89 DBMGR_MERGE, ///< Data records in fields.
90 DBMGR_MERGE_PRINTER, ///< Print mail merge.
91 DBMGR_MERGE_EMAIL, ///< Send mail merge as email.
92 DBMGR_MERGE_FILE, ///< Save mail merge as files.
93 DBMGR_MERGE_SHELL ///< Create merge doc and keep the doc shell.
96 // Administration of (new) logical databases.
97 enum class SwDBSelect
99 UNKNOWN, TABLE, QUERY
102 struct SwDSParam : public SwDBData
104 css::util::Date aNullDate;
106 css::uno::Reference<css::util::XNumberFormatter> xFormatter;
107 css::uno::Reference< css::sdbc::XConnection> xConnection;
108 css::uno::Reference< css::sdbc::XStatement> xStatement;
109 css::uno::Reference< css::sdbc::XResultSet> xResultSet;
110 css::uno::Sequence< css::uno::Any > aSelection;
111 bool bScrollable;
112 bool bEndOfDB;
113 long nSelectionIndex;
115 SwDSParam(const SwDBData& rData) :
116 SwDBData(rData),
117 bScrollable(false),
118 bEndOfDB(false),
119 nSelectionIndex(0)
122 SwDSParam(const SwDBData& rData,
123 const css::uno::Reference< css::sdbc::XResultSet>& xResSet,
124 const css::uno::Sequence< css::uno::Any >& rSelection) :
125 SwDBData(rData),
126 xResultSet(xResSet),
127 aSelection(rSelection),
128 bScrollable(true),
129 bEndOfDB(false),
130 nSelectionIndex(0)
133 bool HasValidRecord() const
134 { return( !bEndOfDB && xResultSet.is() ); }
137 typedef std::vector<std::unique_ptr<SwDSParam>> SwDSParams_t;
139 struct SwMergeDescriptor
141 const DBManagerOptions nMergeType;
142 SwWrtShell& rSh;
143 const svx::ODataAccessDescriptor& rDescriptor;
146 * Create a single or multiple results
148 * This currently just affects FILE and PRINTER, as EMAIL is always
149 * multiple and SHELL is always single.
151 bool bCreateSingleFile;
154 * @defgroup save Export filter settings
155 * @addtogroup save
156 * @{ */
157 OUString sSaveToFilter;
158 OUString sSaveToFilterOptions;
159 css::uno::Sequence< css::beans::PropertyValue > aSaveToFilterData;
160 /** @} */
163 * @defgroup file Mail merge as File settings
164 * @addtogroup file
165 * @{ */
168 * Basename incl. the path for the generated files.
170 * The final filename will be created by concatenating a number to prevent
171 * overwriting an existing file and the extension based on the filter
172 * settings.
174 OUString sPrefix;
176 * Use the sPrefix as the target filename also overwriting an existing
177 * target file.
179 * Just used for the internal mail merge dialogs as mail merge never
180 * overwrites existing files (see SwDBManager::ExecuteFormLetter).
182 bool bPrefixIsFilename;
183 /** @} */
186 * @defgroup email Mail merge as eMail settings
187 * @addtogroup email
188 * @{ */
189 OUString sSubject;
190 OUString sMailBody;
191 OUString sAttachmentName;
192 css::uno::Sequence< OUString > aCopiesTo;
193 css::uno::Sequence< OUString > aBlindCopiesTo;
194 css::uno::Reference< css::mail::XSmtpService > xSmtpServer;
195 bool bSendAsHTML;
196 bool bSendAsAttachment;
197 /** @} */
200 * @addtogroup file email
201 * @{ */
203 /** DB column to fetch EMail of Filename from
205 OUString sDBcolumn;
207 /** @} */
210 * @defgroup print Mail merge to Printer
211 * @addtogroup print
212 * @{ */
213 css::uno::Sequence< css::beans::PropertyValue > aPrintOptions;
214 /** @} */
216 SwMailMergeConfigItem* pMailMergeConfigItem;
218 SwMergeDescriptor( const DBManagerOptions nType,
219 SwWrtShell& rShell,
220 const svx::ODataAccessDescriptor& rDesc ) :
221 nMergeType(nType),
222 rSh(rShell),
223 rDescriptor(rDesc),
224 bCreateSingleFile( false ),
225 bPrefixIsFilename( false ),
226 bSendAsHTML( true ),
227 bSendAsAttachment( false ),
228 pMailMergeConfigItem( nullptr )
230 if( nType == DBMGR_MERGE_SHELL || nType == DBMGR_MERGE_PRINTER )
231 bCreateSingleFile = true;
235 class AbstractMailMergeDlg;
236 class SwDoc;
238 class SW_DLLPUBLIC SwDBManager
240 struct SwDBManager_Impl;
241 class ConnectionDisposedListener_Impl;
242 class MailDispatcherListener_Impl;
244 enum class MergeStatus
246 OK = 0, CANCEL, ERROR
249 MergeStatus m_aMergeStatus; ///< current / last merge status
250 bool bInitDBFields : 1;
251 bool bInMerge : 1; ///< merge process active
252 bool bMergeSilent : 1; ///< suppress display of dialogs/boxes (used when called over API)
253 bool bMergeLock : 1; /**< prevent update of database fields while document is
254 actually printed at the SwViewShell */
255 SwDSParams_t m_DataSourceParams;
256 std::unique_ptr<SwDBManager_Impl> pImpl;
257 const SwXMailMerge* 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 /// The document that owns this manager.
262 SwDoc* m_pDoc;
264 SAL_DLLPRIVATE SwDSParam* FindDSData(const SwDBData& rData, bool bCreate);
265 SAL_DLLPRIVATE SwDSParam* FindDSConnection(const OUString& rSource, bool bCreate);
267 DECL_DLLPRIVATE_LINK( PrtCancelHdl, Button *, void );
269 /// Insert data record as text into document.
270 SAL_DLLPRIVATE void ImportFromConnection( SwWrtShell* pSh);
272 /// Insert a single data record as text into document.
273 SAL_DLLPRIVATE void ImportDBEntry(SwWrtShell* pSh);
275 /// Run the mail merge for defined modes, except DBMGR_MERGE
276 SAL_DLLPRIVATE bool MergeMailFiles( SwWrtShell* pSh,
277 const SwMergeDescriptor& rMergeDescriptor );
279 SAL_DLLPRIVATE bool ToNextMergeRecord();
280 SAL_DLLPRIVATE bool IsValidMergeRecord() const;
282 SwDBManager(SwDBManager const&) = delete;
283 SwDBManager& operator=(SwDBManager const&) = delete;
285 public:
286 SwDBManager(SwDoc* pDoc);
287 ~SwDBManager();
289 enum DBConnURITypes {
290 DBCONN_UNKNOWN = 0,
291 DBCONN_ODB,
292 DBCONN_CALC,
293 DBCONN_DBASE,
294 DBCONN_FLAT,
295 DBCONN_MSJET,
296 DBCONN_MSACE
299 /// MailMergeEvent source
300 const SwXMailMerge * GetMailMergeEvtSrc() const { return pMergeEvtSrc; }
301 void SetMailMergeEvtSrc( const SwXMailMerge *pSrc ) { pMergeEvtSrc = pSrc; }
303 bool IsMergeSilent() const { return bMergeSilent; }
304 void SetMergeSilent( bool bVal ) { bMergeSilent = bVal; }
306 /// Merging of data records into fields.
307 bool Merge( const SwMergeDescriptor& rMergeDesc );
308 void MergeCancel();
310 bool IsMergeOk() { return MergeStatus::OK == m_aMergeStatus; };
311 bool IsMergeError() { return MergeStatus::ERROR <= m_aMergeStatus; };
313 static std::shared_ptr<SwMailMergeConfigItem> PerformMailMerge(SwView* pView);
315 /// Initialize data fields that lack name of database.
316 bool IsInitDBFields() const { return bInitDBFields; }
317 void SetInitDBFields(bool b) { bInitDBFields = b; }
319 /// Fill listbox with all table names of a database.
320 bool GetTableNames(ListBox* pListBox, const OUString& rDBName );
322 /// Fill listbox with all column names of a database table.
323 void GetColumnNames(ListBox* pListBox,
324 const OUString& rDBName, const OUString& rTableName);
325 static void GetColumnNames(ListBox* pListBox,
326 css::uno::Reference< css::sdbc::XConnection> const & xConnection,
327 const OUString& rTableName);
329 static sal_uLong GetColumnFormat( css::uno::Reference< css::sdbc::XDataSource> const & xSource,
330 css::uno::Reference< css::sdbc::XConnection> const & xConnection,
331 css::uno::Reference< css::beans::XPropertySet> const & xColumn,
332 SvNumberFormatter* pNFormatr,
333 LanguageType nLanguage );
335 sal_uLong GetColumnFormat( const OUString& rDBName,
336 const OUString& rTableName,
337 const OUString& rColNm,
338 SvNumberFormatter* pNFormatr,
339 LanguageType nLanguage );
340 sal_Int32 GetColumnType( const OUString& rDBName,
341 const OUString& rTableName,
342 const OUString& rColNm );
344 bool IsInMerge() const { return bInMerge; }
346 void ExecuteFormLetter(SwWrtShell& rSh,
347 const css::uno::Sequence< css::beans::PropertyValue>& rProperties);
349 static void InsertText(SwWrtShell& rSh,
350 const css::uno::Sequence< css::beans::PropertyValue>& rProperties);
352 /// check if a data source is open
353 bool IsDataSourceOpen(const OUString& rDataSource,
354 const OUString& rTableOrQuery, bool bMergeShell);
356 /// open the source while fields are updated - for the calculator only!
357 bool OpenDataSource(const OUString& rDataSource, const OUString& rTableOrQuery);
358 sal_uInt32 GetSelectedRecordId(const OUString& rDataSource, const OUString& rTableOrQuery, sal_Int32 nCommandType = -1);
359 bool GetColumnCnt(const OUString& rSourceName, const OUString& rTableName,
360 const OUString& rColumnName, sal_uInt32 nAbsRecordId, LanguageType nLanguage,
361 OUString& rResult, double* pNumber);
362 /** create and store or find an already stored connection to a data source for use
363 in SwFieldMgr and SwDBTreeList */
364 css::uno::Reference< css::sdbc::XConnection> const &
365 RegisterConnection(OUString const& rSource);
367 void CreateDSData(const SwDBData& rData)
368 { FindDSData(rData, true); }
369 const SwDSParams_t& GetDSParamArray() const { return m_DataSourceParams; }
371 /// close all data sources - after fields were updated
372 void CloseAll(bool bIncludingMerge = true);
374 bool GetMergeColumnCnt(const OUString& rColumnName, LanguageType nLanguage,
375 OUString &rResult, double *pNumber);
376 bool FillCalcWithMergeData(SvNumberFormatter *pDocFormatter,
377 LanguageType nLanguage, SwCalc &aCalc);
378 bool ToNextRecord(const OUString& rDataSource, const OUString& rTableOrQuery);
380 sal_uInt32 GetSelectedRecordId();
381 bool ToRecordId(sal_Int32 nSet);
383 static const SwDBData& GetAddressDBName();
385 static OUString GetDBField(
386 css::uno::Reference< css::beans::XPropertySet > const & xColumnProp,
387 const SwDBFormatData& rDBFormatData,
388 double *pNumber = nullptr);
390 static css::uno::Reference< css::sdbc::XConnection>
391 GetConnection(const OUString& rDataSource,
392 css::uno::Reference< css::sdbc::XDataSource>& rxSource);
394 static css::uno::Reference< css::sdbcx::XColumnsSupplier>
395 GetColumnSupplier(css::uno::Reference< css::sdbc::XConnection> const & xConnection,
396 const OUString& rTableOrQuery,
397 SwDBSelect eTableOrQuery = SwDBSelect::UNKNOWN);
399 static css::uno::Sequence<OUString> GetExistingDatabaseNames();
401 static DBConnURITypes GetDBunoURI(const OUString &rURI, css::uno::Any &aURLAny);
404 Loads a data source from file and registers it.
406 This function requires GUI interaction, as it loads the data source from
407 the filename returned by a file picker and additional settings dialog.
408 In case of success it returns the registered name, otherwise an empty string.
410 static OUString LoadAndRegisterDataSource(SwDocShell* pDocShell = nullptr);
413 Loads a data source from file and registers it.
415 In case of success it returns the registered name, otherwise an empty string.
416 Optionally add a prefix to the registered DB name.
418 static OUString LoadAndRegisterDataSource(const DBConnURITypes type, const css::uno::Any &rUnoURI,
419 const css::uno::Reference < css::beans::XPropertySet > *pSettings,
420 const OUString &rURI, const OUString *pPrefix, const OUString *pDestDir,
421 SwDocShell* pDocShell = nullptr);
423 Loads a data source from file and registers it.
425 Convenience function, which calls GetDBunoURI and has just one mandatory parameter.
426 In case of success it returns the registered name, otherwise an empty string.
428 static OUString LoadAndRegisterDataSource(const OUString& rURI, const OUString *pPrefix, const OUString *pDestDir,
429 const css::uno::Reference < css::beans::XPropertySet > *pSettings = nullptr);
431 /// Load the embedded data source of the document and also register it.
432 void LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const SwDocShell& rDocShell);
434 /// Unregister a data source.
435 static void RevokeDataSource(const OUString& rName);
437 /** try to get the data source from the given connection through the XChild interface.
438 If this is not possible, the data source will be created through its name.
439 @param _xConnection
440 The connection which should support the XChild interface. (not a must)
441 @param _sDataSourceName
442 The data source name will be used to create the data source when the connection can not be used for it.
443 @return
444 The data source.
446 static css::uno::Reference< css::sdbc::XDataSource>
447 getDataSourceAsParent(const css::uno::Reference< css::sdbc::XConnection>& _xConnection,const OUString& _sDataSourceName);
449 /** creates a RowSet, which must be disposed after use.
450 @param _sDataSourceName
451 The data source name
452 @param _sCommand
453 The command.
454 @param _nCommandType
455 The type of the command.
456 @param _xConnection
457 The active connection which may be <NULL/>.
458 @return
459 The new created RowSet.
462 static css::uno::Reference< css::sdbc::XResultSet>
463 createCursor( const OUString& _sDataSourceName,
464 const OUString& _sCommand,
465 sal_Int32 _nCommandType,
466 const css::uno::Reference< css::sdbc::XConnection>& _xConnection
469 void setEmbeddedName(const OUString& rEmbeddedName, SwDocShell& rDocShell);
470 const OUString& getEmbeddedName() const;
472 static void StoreEmbeddedDataSource(const css::uno::Reference<css::frame::XStorable>& xStorable,
473 const css::uno::Reference<css::embed::XStorage>& xStorage,
474 const OUString& rStreamRelPath,
475 const OUString& rOwnURL);
477 SwDoc* getDoc() const;
478 /// Stop reacting to removed database registrations.
479 void releaseRevokeListener();
482 #endif
484 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */