1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
31 #include <tools/string.hxx>
32 #include <tools/link.hxx>
33 #include <svl/svarray.hxx>
34 #include <com/sun/star/util/Date.hpp>
36 #include <swdbdata.hxx>
37 #include <com/sun/star/uno/Reference.h>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <com/sun/star/lang/Locale.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 namespace com
{namespace sun
{namespace star
{
54 class XColumnsSupplier
;
57 class XNumberFormatter
;
64 class ODataAccessDescriptor
;
69 com::sun::star::util::Date aNullDate
;
70 com::sun::star::uno::Reference
< com::sun::star::util::XNumberFormatter
> xFormatter
;
71 com::sun::star::lang::Locale aLocale
;
80 class SvNumberFormatter
;
81 class SwDbtoolsClient
;
83 class SwMailMergeConfigItem
;
85 // -----------------------------------------------------------------------
89 DBMGR_MERGE
, // Datensaetze in Felder
90 DBMGR_INSERT
, // Datensaetze in Text
91 DBMGR_MERGE_MAILMERGE
, // Serienbriefe drucken
92 DBMGR_MERGE_MAILING
, // Serienbriefe als email versenden
93 DBMGR_MERGE_MAILFILES
, // Serienbriefe als Datei(en) speichern
94 DBMGR_MERGE_DOCUMENTS
, // Print merged documents
95 DBMGR_MERGE_SINGLE_FILE
// save merge as single file
98 // -----------------------------------------------------------------------
100 /*--------------------------------------------------------------------
101 Beschreibung: (neue) Logische Datenbanken verwalten
102 --------------------------------------------------------------------*/
103 #define SW_DB_SELECT_UNKNOWN 0
104 #define SW_DB_SELECT_TABLE 1
105 #define SW_DB_SELECT_QUERY 2
107 struct SwDSParam
: public SwDBData
109 com::sun::star::util::Date aNullDate
;
111 ::com::sun::star::uno::Reference
<com::sun::star::util::XNumberFormatter
> xFormatter
;
112 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> xConnection
;
113 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XStatement
> xStatement
;
114 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XResultSet
> xResultSet
;
115 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> aSelection
;
118 BOOL bAfterSelection
;
119 long nSelectionIndex
;
121 SwDSParam(const SwDBData
& rData
) :
125 bAfterSelection(FALSE
),
129 SwDSParam(const SwDBData
& rData
,
130 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XResultSet
>& xResSet
,
131 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& rSelection
) :
134 aSelection(rSelection
),
137 bAfterSelection(FALSE
),
144 bAfterSelection
= TRUE
;
147 typedef SwDSParam
* SwDSParamPtr
;
148 SV_DECL_PTRARR_DEL(SwDSParamArr
, SwDSParamPtr
, 0, 5)
150 struct SwMergeDescriptor
154 const ::svx::ODataAccessDescriptor
& rDescriptor
;
155 String sSaveToFilter
; //export filter to save resulting files
156 String sSaveToFilterOptions
; //
157 com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> aSaveToFilterData
;
160 String sAddressFromColumn
;
162 String sAttachmentName
;
163 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aCopiesTo
;
164 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aBlindCopiesTo
;
166 ::com::sun::star::uno::Reference
< com::sun::star::mail::XSmtpService
> xSmtpServer
;
168 sal_Bool bSendAsHTML
;
169 sal_Bool bSendAsAttachment
;
171 sal_Bool bPrintAsync
;
172 sal_Bool bCreateSingleFile
;
174 SwMailMergeConfigItem
* pMailMergeConfigItem
;
176 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> aPrintOptions
;
178 SwMergeDescriptor( USHORT nType
, SwWrtShell
& rShell
, ::svx::ODataAccessDescriptor
& rDesc
) :
182 bSendAsHTML( sal_True
),
183 bSendAsAttachment( sal_False
),
184 bPrintAsync( sal_False
),
185 bCreateSingleFile( sal_False
),
186 pMailMergeConfigItem(0)
191 struct SwNewDBMgr_Impl
;
192 class SwConnectionDisposedListener_Impl
;
193 class AbstractMailMergeDlg
;
195 class SW_DLLPUBLIC SwNewDBMgr
197 friend class SwConnectionDisposedListener_Impl
;
199 static SwDbtoolsClient
* pDbtoolsClient
;
201 String sEMailAddrFld
; // Mailing: Spaltenname der E-Mail Adresse
202 String sSubject
; // Mailing: Subject
203 String sAttached
; // Mailing: Attachte Files
205 BOOL bInitDBFields
: 1; // TRUE: Datenbank an Feldern beim Mergen
206 BOOL bSingleJobs
: 1; // Einzelne Druckjobs bei Aufruf aus Basic
207 BOOL bCancel
: 1; // Serienbrief-Save abgebrochen
209 BOOL bInMerge
: 1; //merge process active
210 BOOL bMergeSilent
: 1; // suppress display of dialogs/boxes (used when called over API)
211 BOOL bMergeLock
: 1; // prevent update of database fields while document is
212 // actually printed at the ViewShell
213 SwDSParamArr aDataSourceParams
;
214 SwNewDBMgr_Impl
* pImpl
;
215 const SwXMailMerge
* pMergeEvtSrc
; // != 0 if mail merge events are to be send
217 SW_DLLPRIVATE SwDSParam
* FindDSData(const SwDBData
& rData
, BOOL bCreate
);
218 SW_DLLPRIVATE SwDSParam
* FindDSConnection(const ::rtl::OUString
& rSource
, BOOL bCreate
);
221 SW_DLLPRIVATE
DECL_LINK( PrtCancelHdl
, Button
* );
223 // Datensaetze als Text ins Dokument einfuegen
224 SW_DLLPRIVATE
void ImportFromConnection( SwWrtShell
* pSh
);
226 // Einzelnen Datensatz als Text ins Dokument einfuegen
227 SW_DLLPRIVATE
void ImportDBEntry(SwWrtShell
* pSh
);
229 // merge to file _and_ merge to e-Mail
230 SW_DLLPRIVATE BOOL
MergeMailFiles(SwWrtShell
* pSh
,
231 const SwMergeDescriptor
& rMergeDescriptor
);
232 SW_DLLPRIVATE BOOL
ToNextRecord(SwDSParam
* pParam
);
238 // Art des aktellen Mergens. Siehe DBMgrOptions-enum
239 inline USHORT
GetMergeType() const { return nMergeType
; }
240 inline void SetMergeType( USHORT nTyp
) { nMergeType
= nTyp
; }
242 // MailMergeEvent source
243 const SwXMailMerge
* GetMailMergeEvtSrc() const { return pMergeEvtSrc
; }
244 void SetMailMergeEvtSrc( const SwXMailMerge
*pSrc
) { pMergeEvtSrc
= pSrc
; }
246 inline BOOL
IsMergeSilent() const { return bMergeSilent
!= 0; }
247 inline void SetMergeSilent( BOOL bVal
) { bMergeSilent
= bVal
; }
249 // Mischen von Datensaetzen in Felder
250 BOOL
MergeNew( const SwMergeDescriptor
& rMergeDesc
);
251 BOOL
Merge(SwWrtShell
* pSh
);
253 // Datenbankfelder mit fehlendem Datenbankname initialisieren
254 inline BOOL
IsInitDBFields() const { return bInitDBFields
; }
255 inline void SetInitDBFields(BOOL b
) { bInitDBFields
= b
; }
257 // Serienbriefe einzelnd oder alle zusammen drucken/speichern
258 inline BOOL
IsSingleJobs() const { return bSingleJobs
; }
259 inline void SetSingleJobs(BOOL b
) { bSingleJobs
= b
; }
262 // email-Daten setzen
263 inline void SetEMailColumn(const String
& sColName
) { sEMailAddrFld
= sColName
; }
264 inline void SetSubject(const String
& sSbj
) { sSubject
= sSbj
; }
265 inline void SetAttachment(const String
& sAtt
) { sAttached
= sAtt
; }
268 // Listbox mit allen Tabellennamen einer Datenbank fuellen
269 BOOL
GetTableNames(ListBox
* pListBox
, const String
& rDBName
);
271 // Listbox mit allen Spaltennamen einer Datenbanktabelle fuellen
272 BOOL
GetColumnNames(ListBox
* pListBox
,
273 const String
& rDBName
, const String
& rTableName
, BOOL bAppend
= FALSE
);
274 BOOL
GetColumnNames(ListBox
* pListBox
,
275 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> xConnection
,
276 const String
& rTableName
, BOOL bAppend
= FALSE
);
278 ULONG
GetColumnFmt( ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDataSource
> xSource
,
279 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> xConnection
,
280 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> xColumn
,
281 SvNumberFormatter
* pNFmtr
,
284 ULONG
GetColumnFmt( const String
& rDBName
,
285 const String
& rTableName
,
286 const String
& rColNm
,
287 SvNumberFormatter
* pNFmtr
,
289 sal_Int32
GetColumnType( const String
& rDBName
,
290 const String
& rTableName
,
291 const String
& rColNm
);
293 inline BOOL
IsInMerge() const { return bInMerge
; }
296 void ExecuteFormLetter(SwWrtShell
& rSh
,
297 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& rProperties
,
298 BOOL bWithDataSourceBrowser
= FALSE
);
300 void InsertText(SwWrtShell
& rSh
,
301 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& rProperties
);
303 // check if a data source is open
304 BOOL
IsDataSourceOpen(const String
& rDataSource
,
305 const String
& rTableOrQuery
, sal_Bool bMergeOnly
);
307 // open the source while fields are updated - for the calculator only!
308 BOOL
OpenDataSource(const String
& rDataSource
, const String
& rTableOrQuery
,
309 sal_Int32 nCommandType
= -1, bool bCreate
= false);
310 sal_uInt32
GetSelectedRecordId(const String
& rDataSource
, const String
& rTableOrQuery
, sal_Int32 nCommandType
= -1);
311 BOOL
GetColumnCnt(const String
& rSourceName
, const String
& rTableName
,
312 const String
& rColumnName
, sal_uInt32 nAbsRecordId
, long nLanguage
,
313 String
& rResult
, double* pNumber
);
314 //create and store or find an already stored connection to a data source for use
315 //in SwFldMgr and SwDBTreeList
316 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>
317 RegisterConnection(::rtl::OUString
& rSource
);
319 const SwDSParam
* CreateDSData(const SwDBData
& rData
)
320 {return FindDSData(rData
, TRUE
);}
321 const SwDSParamArr
& GetDSParamArray() const {return aDataSourceParams
;}
324 //close all data sources - after fields were updated
325 void CloseAll(BOOL bIncludingMerge
= TRUE
);
327 BOOL
GetMergeColumnCnt(const String
& rColumnName
, USHORT nLanguage
,
328 String
&rResult
, double *pNumber
, sal_uInt32
*pFormat
);
329 BOOL
ToNextMergeRecord();
330 BOOL
ToNextRecord(const String
& rDataSource
, const String
& rTableOrQuery
, sal_Int32 nCommandType
= -1);
332 BOOL
ExistsNextRecord()const;
333 sal_uInt32
GetSelectedRecordId();
334 sal_Bool
ToRecordId(sal_Int32 nSet
);
336 const SwDBData
& GetAddressDBName();
338 static String
GetDBField(
339 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> xColumnProp
,
340 const SwDBFormatData
& rDBFormatData
,
341 double *pNumber
= NULL
);
343 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>
344 GetConnection(const String
& rDataSource
,
345 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDataSource
>& rxSource
);
347 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbcx::XColumnsSupplier
>
348 GetColumnSupplier(::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>,
349 const String
& rTableOrQuery
,
350 BYTE eTableOrQuery
= SW_DB_SELECT_UNKNOWN
);
352 static ::com::sun::star::uno::Sequence
<rtl::OUString
> GetExistingDatabaseNames();
355 Loads a data source from file and registers it. Returns the registered name.
357 static String
LoadAndRegisterDataSource();
359 static SwDbtoolsClient
& GetDbtoolsClient();
360 // has to be called from _FinitUI()
361 static void RemoveDbtoolsClient();
363 /** try to get the data source from the given connection through the XChild interface.
364 If this is not possible, the data source will be created through its name.
366 The connection which should support the XChild interface. (not a must)
367 @param _sDataSourceName
368 The data source name will be used to create the data source when the connection can not be used for it.
372 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDataSource
>
373 getDataSourceAsParent(const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,const ::rtl::OUString
& _sDataSourceName
);
375 /** creates a RowSet, which must be disposed after use.
376 @param _sDataSourceName
381 The type of the command.
383 The active connection which may be <NULL/>.
385 The new created RowSet.
388 static ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XResultSet
>
389 createCursor( const ::rtl::OUString
& _sDataSourceName
,
390 const ::rtl::OUString
& _sCommand
,
391 sal_Int32 _nCommandType
,
392 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
394 //merge into one document - returns the number of merged docs
395 sal_Int32
MergeDocuments( SwMailMergeConfigItem
& rMMConfig
, SwView
& rSourceView
);