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 .
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 <com/sun/star/util/Date.hpp>
26 #include <swdbdata.hxx>
27 #include <com/sun/star/uno/Reference.h>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <boost/ptr_container/ptr_vector.hpp>
33 namespace com
{namespace sun
{namespace star
{
46 class XColumnsSupplier
;
49 class XNumberFormatter
;
56 class ODataAccessDescriptor
;
61 com::sun::star::util::Date aNullDate
;
62 com::sun::star::uno::Reference
< com::sun::star::util::XNumberFormatter
> xFormatter
;
63 com::sun::star::lang::Locale aLocale
;
75 class SvNumberFormatter
;
77 class SwMailMergeConfigItem
;
84 DBMGR_MERGE
, ///< Data records in fields.
85 DBMGR_MERGE_PRINTER
, ///< Print mail merge.
86 DBMGR_MERGE_EMAIL
, ///< Send mail merge as email.
87 DBMGR_MERGE_FILE
, ///< Save mail merge as files.
88 DBMGR_MERGE_SHELL
///< Create merge doc and keep the doc shell.
91 // Administration of (new) logical databases.
92 #define SW_DB_SELECT_UNKNOWN 0
93 #define SW_DB_SELECT_TABLE 1
94 #define SW_DB_SELECT_QUERY 2
96 struct SwDSParam
: public SwDBData
98 com::sun::star::util::Date aNullDate
;
100 ::com::sun::star::uno::Reference
<com::sun::star::util::XNumberFormatter
> xFormatter
;
101 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> xConnection
;
102 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XStatement
> xStatement
;
103 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XResultSet
> xResultSet
;
104 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> aSelection
;
107 bool bAfterSelection
;
108 long nSelectionIndex
;
110 SwDSParam(const SwDBData
& rData
) :
114 bAfterSelection(false),
118 SwDSParam(const SwDBData
& rData
,
119 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XResultSet
>& xResSet
,
120 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& rSelection
) :
123 aSelection(rSelection
),
126 bAfterSelection(false),
133 bAfterSelection
= true;
136 typedef boost::ptr_vector
<SwDSParam
> SwDSParamArr
;
138 struct SwMergeDescriptor
140 DBManagerOptions nMergeType
;
142 const svx::ODataAccessDescriptor
& rDescriptor
;
143 OUString sSaveToFilter
; ///< export filter to save resulting files
144 OUString sSaveToFilterOptions
;
145 css::uno::Sequence
< css::beans::PropertyValue
> aSaveToFilterData
;
148 OUString sAddressFromColumn
;
150 OUString sAttachmentName
;
151 css::uno::Sequence
< OUString
> aCopiesTo
;
152 css::uno::Sequence
< OUString
> aBlindCopiesTo
;
154 css::uno::Reference
< css::mail::XSmtpService
> xSmtpServer
;
157 bool bSendAsAttachment
;
160 bool bCreateSingleFile
;
161 bool bSubjectIsFilename
;
163 SwMailMergeConfigItem
* pMailMergeConfigItem
;
165 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> aPrintOptions
;
167 SwMergeDescriptor( DBManagerOptions nType
, SwWrtShell
& rShell
, svx::ODataAccessDescriptor
& rDesc
) :
172 bSendAsAttachment( false ),
173 bPrintAsync( false ),
174 bCreateSingleFile( false ),
175 bSubjectIsFilename( false ),
176 pMailMergeConfigItem(0)
181 struct SwDBManager_Impl
;
182 class SwConnectionDisposedListener_Impl
;
183 class AbstractMailMergeDlg
;
185 class SW_DLLPUBLIC SwDBManager
187 friend class SwConnectionDisposedListener_Impl
;
189 OUString sEMailAddrField
; ///< Mailing: Column name of email address.
190 OUString sSubject
; ///< Mailing: Subject
191 OUString sAttached
; ///< Mailing: Attached Files.
192 bool bCancel
; ///< Mail merge canceled.
193 bool bInitDBFields
: 1;
194 bool bSingleJobs
: 1; ///< Printing job when called from Basic.
195 bool bInMerge
: 1; ///< merge process active
196 bool bMergeSilent
: 1; ///< suppress display of dialogs/boxes (used when called over API)
197 bool bMergeLock
: 1; /**< prevent update of database fields while document is
198 actually printed at the SwViewShell */
199 SwDSParamArr aDataSourceParams
;
200 SwDBManager_Impl
* pImpl
;
201 const SwXMailMerge
* pMergeEvtSrc
; ///< != 0 if mail merge events are to be send
203 SAL_DLLPRIVATE SwDSParam
* FindDSData(const SwDBData
& rData
, bool bCreate
);
204 SAL_DLLPRIVATE SwDSParam
* FindDSConnection(const OUString
& rSource
, bool bCreate
);
206 DECL_DLLPRIVATE_LINK( PrtCancelHdl
, Button
* );
208 /// Insert data record as text into document.
209 SAL_DLLPRIVATE
void ImportFromConnection( SwWrtShell
* pSh
);
211 /// Insert a single data record as text into document.
212 SAL_DLLPRIVATE
void ImportDBEntry(SwWrtShell
* pSh
);
214 /// merge to file _and_ merge to e-Mail
215 SAL_DLLPRIVATE
bool MergeMailFiles(SwWrtShell
* pSh
,
216 const SwMergeDescriptor
& rMergeDescriptor
, vcl::Window
* pParent
);
217 SAL_DLLPRIVATE
bool ToNextRecord(SwDSParam
* pParam
);
223 enum DBConnURITypes
{
233 /// MailMergeEvent source
234 const SwXMailMerge
* GetMailMergeEvtSrc() const { return pMergeEvtSrc
; }
235 void SetMailMergeEvtSrc( const SwXMailMerge
*pSrc
) { pMergeEvtSrc
= pSrc
; }
237 inline bool IsMergeSilent() const { return bMergeSilent
; }
238 inline void SetMergeSilent( bool bVal
) { bMergeSilent
= bVal
; }
240 /// Merging of data records into fields.
241 bool MergeNew( const SwMergeDescriptor
& rMergeDesc
, vcl::Window
* pParent
= NULL
);
242 static bool Merge(SwWrtShell
* pSh
);
245 /// Initialize data fields that lack name of database.
246 inline bool IsInitDBFields() const { return bInitDBFields
; }
247 inline void SetInitDBFields(bool b
) { bInitDBFields
= b
; }
249 /// Print / Save mail merge one by one or all together.
250 bool IsSingleJobs() const { return bSingleJobs
; }
251 void SetSingleJobs(bool b
) { bSingleJobs
= b
; }
253 /// Mailing: Set email data.
254 inline void SetEMailColumn(const OUString
& sColName
) { sEMailAddrField
= sColName
; }
255 inline void SetSubject(const OUString
& sSbj
) { sSubject
= sSbj
; }
256 inline void SetAttachment(const OUString
& sAtt
) { sAttached
= sAtt
; }
258 /// Fill listbox with all table names of a database.
259 bool GetTableNames(ListBox
* pListBox
, const OUString
& rDBName
);
261 /// Fill listbox with all column names of a database table.
262 void GetColumnNames(ListBox
* pListBox
,
263 const OUString
& rDBName
, const OUString
& rTableName
, bool bAppend
= false);
264 static void GetColumnNames(ListBox
* pListBox
,
265 css::uno::Reference
< css::sdbc::XConnection
> xConnection
,
266 const OUString
& rTableName
, bool bAppend
= false);
268 static sal_uLong
GetColumnFormat( ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDataSource
> xSource
,
269 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> xConnection
,
270 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> xColumn
,
271 SvNumberFormatter
* pNFormatr
,
274 sal_uLong
GetColumnFormat( const OUString
& rDBName
,
275 const OUString
& rTableName
,
276 const OUString
& rColNm
,
277 SvNumberFormatter
* pNFormatr
,
279 sal_Int32
GetColumnType( const OUString
& rDBName
,
280 const OUString
& rTableName
,
281 const OUString
& rColNm
);
283 inline bool IsInMerge() const { return bInMerge
; }
285 void ExecuteFormLetter(SwWrtShell
& rSh
,
286 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& rProperties
,
287 bool bWithDataSourceBrowser
= false);
289 static void InsertText(SwWrtShell
& rSh
,
290 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& rProperties
);
292 /// check if a data source is open
293 bool IsDataSourceOpen(const OUString
& rDataSource
,
294 const OUString
& rTableOrQuery
, bool bMergeShell
);
296 /// open the source while fields are updated - for the calculator only!
297 bool OpenDataSource(const OUString
& rDataSource
, const OUString
& rTableOrQuery
,
298 sal_Int32 nCommandType
= -1, bool bCreate
= false);
299 sal_uInt32
GetSelectedRecordId(const OUString
& rDataSource
, const OUString
& rTableOrQuery
, sal_Int32 nCommandType
= -1);
300 bool GetColumnCnt(const OUString
& rSourceName
, const OUString
& rTableName
,
301 const OUString
& rColumnName
, sal_uInt32 nAbsRecordId
, long nLanguage
,
302 OUString
& rResult
, double* pNumber
);
303 /** create and store or find an already stored connection to a data source for use
304 in SwFieldMgr and SwDBTreeList */
305 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>
306 RegisterConnection(OUString
& rSource
);
308 const SwDSParam
* CreateDSData(const SwDBData
& rData
)
309 {return FindDSData(rData
, true);}
310 const SwDSParamArr
& GetDSParamArray() const {return aDataSourceParams
;}
312 /// close all data sources - after fields were updated
313 void CloseAll(bool bIncludingMerge
= true);
315 bool GetMergeColumnCnt(const OUString
& rColumnName
, sal_uInt16 nLanguage
,
316 OUString
&rResult
, double *pNumber
);
317 bool FillCalcWithMergeData(SvNumberFormatter
*pDocFormatter
,
318 sal_uInt16 nLanguage
, bool asString
, SwCalc
&aCalc
);
319 bool ToNextMergeRecord();
320 bool ToNextRecord(const OUString
& rDataSource
, const OUString
& rTableOrQuery
, sal_Int32 nCommandType
= -1);
322 bool ExistsNextRecord()const;
323 sal_uInt32
GetSelectedRecordId();
324 bool ToRecordId(sal_Int32 nSet
);
326 static const SwDBData
& GetAddressDBName();
328 static OUString
GetDBField(
329 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> xColumnProp
,
330 const SwDBFormatData
& rDBFormatData
,
331 double *pNumber
= NULL
);
333 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>
334 GetConnection(const OUString
& rDataSource
,
335 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDataSource
>& rxSource
);
337 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbcx::XColumnsSupplier
>
338 GetColumnSupplier(::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>,
339 const OUString
& rTableOrQuery
,
340 sal_uInt8 eTableOrQuery
= SW_DB_SELECT_UNKNOWN
);
342 static ::com::sun::star::uno::Sequence
<OUString
> GetExistingDatabaseNames();
344 static DBConnURITypes
GetDBunoURI(const OUString
&rURI
, ::com::sun::star::uno::Any
&aURLAny
);
347 Loads a data source from file and registers it.
349 This function requires GUI interaction, as it loads the data source from
350 the filename returned by a file picker and additional settings dialog.
351 In case of success it returns the registered name, otherwise an empty string.
353 static OUString
LoadAndRegisterDataSource();
356 Loads a data source from file and registers it.
358 In case of success it returns the registered name, otherwise an empty string.
359 Optionally add a prefix to the registered DB name.
361 static OUString
LoadAndRegisterDataSource(const DBConnURITypes type
, const ::com::sun::star::uno::Any
&rUnoURI
,
362 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> *pSettings
,
363 const OUString
&rURI
, const OUString
*pPrefix
= 0, const OUString
*pDestDir
= 0);
365 Loads a data source from file and registers it.
367 Convenience function, which calls GetDBunoURI and has just one mandatory parameter.
368 In case of success it returns the registered name, otherwise an empty string.
370 static OUString
LoadAndRegisterDataSource(const OUString
& rURI
, const OUString
*pPrefix
= 0, const OUString
*pDestDir
= 0,
371 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> *pSettings
= 0);
373 /// Load the embedded data source of the document and also register it.
374 static void LoadAndRegisterEmbeddedDataSource(const SwDBData
& rData
, const SwDocShell
& rDocShell
);
376 /** try to get the data source from the given connection through the XChild interface.
377 If this is not possible, the data source will be created through its name.
379 The connection which should support the XChild interface. (not a must)
380 @param _sDataSourceName
381 The data source name will be used to create the data source when the connection can not be used for it.
385 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDataSource
>
386 getDataSourceAsParent(const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,const OUString
& _sDataSourceName
);
388 /** creates a RowSet, which must be disposed after use.
389 @param _sDataSourceName
394 The type of the command.
396 The active connection which may be <NULL/>.
398 The new created RowSet.
401 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XResultSet
>
402 createCursor( const OUString
& _sDataSourceName
,
403 const OUString
& _sCommand
,
404 sal_Int32 _nCommandType
,
405 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */