Line data Source code
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 : #ifndef _SWDBMGR_HXX
20 : #define _SWDBMGR_HXX
21 :
22 :
23 : #include <tools/string.hxx>
24 : #include <tools/link.hxx>
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 <boost/ptr_container/ptr_vector.hpp>
33 : namespace com{namespace sun{namespace star{
34 : namespace sdbc{
35 : class XConnection;
36 : class XStatement;
37 : class XDataSource;
38 : class XResultSet;
39 : }
40 : namespace beans{
41 :
42 : class XPropertySet;
43 : struct PropertyValue;
44 : }
45 : namespace sdbcx{
46 : class XColumnsSupplier;
47 : }
48 : namespace util{
49 : class XNumberFormatter;
50 : }
51 : namespace mail{
52 : class XSmtpService;
53 : }
54 : }}}
55 : namespace svx {
56 : class ODataAccessDescriptor;
57 : }
58 :
59 0 : struct SwDBFormatData
60 : {
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;
64 : };
65 :
66 : class SwView;
67 : class SwWrtShell;
68 : class SfxProgress;
69 : class ListBox;
70 : class Button;
71 : class SvNumberFormatter;
72 : class SwDbtoolsClient;
73 : class SwXMailMerge;
74 : class SwMailMergeConfigItem;
75 :
76 : // -----------------------------------------------------------------------
77 :
78 : enum DBMgrOptions
79 : {
80 : DBMGR_MERGE, ///< Data records in fields.
81 : DBMGR_INSERT, ///< Data records in text.
82 : DBMGR_MERGE_MAILMERGE, ///< Print mail merge.
83 : DBMGR_MERGE_MAILING, ///< Send mail merge as email.
84 : DBMGR_MERGE_MAILFILES, ///< Save mail merg as files.
85 : DBMGR_MERGE_DOCUMENTS, ///< Print merged documents.
86 : DBMGR_MERGE_SINGLE_FILE ///< Save merge as single file.
87 : };
88 :
89 : // -----------------------------------------------------------------------
90 :
91 : /*--------------------------------------------------------------------
92 : Administration of (new) logical databases.
93 : --------------------------------------------------------------------*/
94 : #define SW_DB_SELECT_UNKNOWN 0
95 : #define SW_DB_SELECT_TABLE 1
96 : #define SW_DB_SELECT_QUERY 2
97 :
98 0 : struct SwDSParam : public SwDBData
99 : {
100 : com::sun::star::util::Date aNullDate;
101 :
102 : ::com::sun::star::uno::Reference<com::sun::star::util::XNumberFormatter> xFormatter;
103 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection;
104 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XStatement> xStatement;
105 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet> xResultSet;
106 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aSelection;
107 : sal_Bool bScrollable;
108 : sal_Bool bEndOfDB;
109 : sal_Bool bAfterSelection;
110 : long nSelectionIndex;
111 :
112 0 : SwDSParam(const SwDBData& rData) :
113 : SwDBData(rData),
114 : bScrollable(sal_False),
115 : bEndOfDB(sal_False),
116 : bAfterSelection(sal_False),
117 0 : nSelectionIndex(0)
118 0 : {}
119 :
120 0 : SwDSParam(const SwDBData& rData,
121 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& xResSet,
122 : const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rSelection) :
123 : SwDBData(rData),
124 : xResultSet(xResSet),
125 : aSelection(rSelection),
126 : bScrollable(sal_True),
127 : bEndOfDB(sal_False),
128 : bAfterSelection(sal_False),
129 0 : nSelectionIndex(0)
130 0 : {}
131 :
132 0 : void CheckEndOfDB()
133 : {
134 0 : if(bEndOfDB)
135 0 : bAfterSelection = sal_True;
136 0 : }
137 : };
138 : typedef boost::ptr_vector<SwDSParam> SwDSParamArr;
139 :
140 0 : struct SwMergeDescriptor
141 : {
142 : sal_uInt16 nMergeType;
143 : SwWrtShell& rSh;
144 : const ::svx::ODataAccessDescriptor& rDescriptor;
145 : String sSaveToFilter; ///< export filter to save resulting files
146 : String sSaveToFilterOptions;
147 : com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aSaveToFilterData;
148 :
149 : String sSubject;
150 : String sAddressFromColumn;
151 : String sMailBody;
152 : String sAttachmentName;
153 : ::com::sun::star::uno::Sequence< OUString > aCopiesTo;
154 : ::com::sun::star::uno::Sequence< OUString > aBlindCopiesTo;
155 :
156 : ::com::sun::star::uno::Reference< com::sun::star::mail::XSmtpService > xSmtpServer;
157 :
158 : sal_Bool bSendAsHTML;
159 : sal_Bool bSendAsAttachment;
160 :
161 : sal_Bool bPrintAsync;
162 : sal_Bool bCreateSingleFile;
163 :
164 : SwMailMergeConfigItem* pMailMergeConfigItem;
165 :
166 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aPrintOptions;
167 :
168 0 : SwMergeDescriptor( sal_uInt16 nType, SwWrtShell& rShell, ::svx::ODataAccessDescriptor& rDesc ) :
169 : nMergeType(nType),
170 : rSh(rShell),
171 : rDescriptor(rDesc),
172 : bSendAsHTML( sal_True ),
173 : bSendAsAttachment( sal_False ),
174 : bPrintAsync( sal_False ),
175 : bCreateSingleFile( sal_False ),
176 0 : pMailMergeConfigItem(0)
177 0 : {}
178 :
179 : };
180 :
181 : struct SwNewDBMgr_Impl;
182 : class SwConnectionDisposedListener_Impl;
183 : class AbstractMailMergeDlg;
184 :
185 : class SW_DLLPUBLIC SwNewDBMgr
186 : {
187 : friend class SwConnectionDisposedListener_Impl;
188 :
189 : static SwDbtoolsClient* pDbtoolsClient;
190 :
191 : String sEMailAddrFld; ///< Mailing: Column name of email address.
192 : String sSubject; ///< Mailing: Subject
193 : String sAttached; ///< Mailing: Attached Files.
194 : sal_uInt16 nMergeType;
195 : sal_Bool bInitDBFields : 1;
196 : sal_Bool bSingleJobs : 1; ///< Printing job when called from Basic.
197 : sal_Bool bCancel : 1; ///< Mail merge save canceled.
198 :
199 : sal_Bool bInMerge : 1; ///< merge process active
200 : sal_Bool bMergeSilent : 1; ///< suppress display of dialogs/boxes (used when called over API)
201 : sal_Bool bMergeLock : 1; /**< prevent update of database fields while document is
202 : actually printed at the ViewShell */
203 : SwDSParamArr aDataSourceParams;
204 : SwNewDBMgr_Impl* pImpl;
205 : const SwXMailMerge* pMergeEvtSrc; ///< != 0 if mail merge events are to be send
206 :
207 : SW_DLLPRIVATE SwDSParam* FindDSData(const SwDBData& rData, sal_Bool bCreate);
208 : SW_DLLPRIVATE SwDSParam* FindDSConnection(const OUString& rSource, sal_Bool bCreate);
209 :
210 :
211 : SW_DLLPRIVATE DECL_LINK( PrtCancelHdl, Button * );
212 :
213 : /// Insert data record as text into document.
214 : SW_DLLPRIVATE void ImportFromConnection( SwWrtShell* pSh);
215 :
216 : /// Insert a single data record as text into document.
217 : SW_DLLPRIVATE void ImportDBEntry(SwWrtShell* pSh);
218 :
219 : /// merge to file _and_ merge to e-Mail
220 : SW_DLLPRIVATE sal_Bool MergeMailFiles(SwWrtShell* pSh,
221 : const SwMergeDescriptor& rMergeDescriptor );
222 : SW_DLLPRIVATE sal_Bool ToNextRecord(SwDSParam* pParam);
223 :
224 : public:
225 : SwNewDBMgr();
226 : ~SwNewDBMgr();
227 :
228 : /// Current merge type. Cf. DBMgrOptions-enum.
229 : inline sal_uInt16 GetMergeType() const { return nMergeType; }
230 0 : inline void SetMergeType( sal_uInt16 nTyp ) { nMergeType = nTyp; }
231 :
232 : /// MailMergeEvent source
233 0 : const SwXMailMerge * GetMailMergeEvtSrc() const { return pMergeEvtSrc; }
234 0 : void SetMailMergeEvtSrc( const SwXMailMerge *pSrc ) { pMergeEvtSrc = pSrc; }
235 :
236 0 : inline sal_Bool IsMergeSilent() const { return bMergeSilent != 0; }
237 0 : inline void SetMergeSilent( sal_Bool bVal ) { bMergeSilent = bVal; }
238 :
239 : /// Merging of data records into fields.
240 : sal_Bool MergeNew( const SwMergeDescriptor& rMergeDesc );
241 : sal_Bool Merge(SwWrtShell* pSh);
242 :
243 : /// Initialize data fields that lack name of database.
244 0 : inline sal_Bool IsInitDBFields() const { return bInitDBFields; }
245 50 : inline void SetInitDBFields(sal_Bool b) { bInitDBFields = b; }
246 :
247 : /// Print / Save mail merge one by one or all together.
248 : inline sal_Bool IsSingleJobs() const { return bSingleJobs; }
249 : inline void SetSingleJobs(sal_Bool b) { bSingleJobs = b; }
250 :
251 : /// Mailing: Set email data.
252 0 : inline void SetEMailColumn(const String& sColName) { sEMailAddrFld = sColName; }
253 0 : inline void SetSubject(const String& sSbj) { sSubject = sSbj; }
254 0 : inline void SetAttachment(const String& sAtt) { sAttached = sAtt; }
255 :
256 :
257 : /// Fill listbox with all table names of a database.
258 : sal_Bool GetTableNames(ListBox* pListBox, const String& rDBName );
259 :
260 : /// Fill listbox with all column names of a database table.
261 : sal_Bool GetColumnNames(ListBox* pListBox,
262 : const String& rDBName, const String& rTableName, sal_Bool bAppend = sal_False);
263 : sal_Bool GetColumnNames(ListBox* pListBox,
264 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection,
265 : const String& rTableName, sal_Bool bAppend = sal_False);
266 :
267 : sal_uLong GetColumnFmt( ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource> xSource,
268 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection,
269 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xColumn,
270 : SvNumberFormatter* pNFmtr,
271 : long nLanguage );
272 :
273 : sal_uLong GetColumnFmt( const String& rDBName,
274 : const String& rTableName,
275 : const String& rColNm,
276 : SvNumberFormatter* pNFmtr,
277 : long nLanguage );
278 : sal_Int32 GetColumnType( const String& rDBName,
279 : const String& rTableName,
280 : const String& rColNm );
281 :
282 0 : inline sal_Bool IsInMerge() const { return bInMerge; }
283 : void EndMerge();
284 :
285 : void ExecuteFormLetter(SwWrtShell& rSh,
286 : const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& rProperties,
287 : sal_Bool bWithDataSourceBrowser = sal_False);
288 :
289 : void InsertText(SwWrtShell& rSh,
290 : const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& rProperties);
291 :
292 : /// check if a data source is open
293 : sal_Bool IsDataSourceOpen(const String& rDataSource,
294 : const String& rTableOrQuery, sal_Bool bMergeOnly);
295 :
296 : /// open the source while fields are updated - for the calculator only!
297 : sal_Bool OpenDataSource(const String& rDataSource, const String& rTableOrQuery,
298 : sal_Int32 nCommandType = -1, bool bCreate = false);
299 : sal_uInt32 GetSelectedRecordId(const String& rDataSource, const String& rTableOrQuery, sal_Int32 nCommandType = -1);
300 : sal_Bool GetColumnCnt(const String& rSourceName, const String& rTableName,
301 : const String& 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 SwFldMgr and SwDBTreeList */
305 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>
306 : RegisterConnection(OUString& rSource);
307 :
308 0 : const SwDSParam* CreateDSData(const SwDBData& rData)
309 0 : {return FindDSData(rData, sal_True);}
310 0 : const SwDSParamArr& GetDSParamArray() const {return aDataSourceParams;}
311 :
312 :
313 : /// close all data sources - after fields were updated
314 : void CloseAll(sal_Bool bIncludingMerge = sal_True);
315 :
316 : sal_Bool GetMergeColumnCnt(const String& rColumnName, sal_uInt16 nLanguage,
317 : OUString &rResult, double *pNumber, sal_uInt32 *pFormat);
318 : sal_Bool ToNextMergeRecord();
319 : sal_Bool ToNextRecord(const String& rDataSource, const String& rTableOrQuery, sal_Int32 nCommandType = -1);
320 :
321 : sal_Bool ExistsNextRecord()const;
322 : sal_uInt32 GetSelectedRecordId();
323 : sal_Bool ToRecordId(sal_Int32 nSet);
324 :
325 : const SwDBData& GetAddressDBName();
326 :
327 : static String GetDBField(
328 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xColumnProp,
329 : const SwDBFormatData& rDBFormatData,
330 : double *pNumber = NULL);
331 :
332 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>
333 : GetConnection(const String& rDataSource,
334 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource>& rxSource);
335 :
336 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XColumnsSupplier>
337 : GetColumnSupplier(::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>,
338 : const String& rTableOrQuery,
339 : sal_uInt8 eTableOrQuery = SW_DB_SELECT_UNKNOWN);
340 :
341 : static ::com::sun::star::uno::Sequence<OUString> GetExistingDatabaseNames();
342 :
343 : /**
344 : Loads a data source from file and registers it. Returns the registered name.
345 : */
346 : static String LoadAndRegisterDataSource();
347 :
348 : static SwDbtoolsClient& GetDbtoolsClient();
349 : /// has to be called from _FinitUI()
350 : static void RemoveDbtoolsClient();
351 :
352 : /** try to get the data source from the given connection through the XChild interface.
353 : If this is not possible, the data source will be created through its name.
354 : @param _xConnection
355 : The connection which should support the XChild interface. (not a must)
356 : @param _sDataSourceName
357 : The data source name will be used to create the data source when the connection can not be used for it.
358 : @return
359 : The data source.
360 : */
361 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource>
362 : getDataSourceAsParent(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,const OUString& _sDataSourceName);
363 :
364 : /** creates a RowSet, which must be disposed after use.
365 : @param _sDataSourceName
366 : The data source name
367 : @param _sCommand
368 : The command.
369 : @param _nCommandType
370 : The type of the command.
371 : @param _xConnection
372 : The active connection which may be <NULL/>.
373 : @return
374 : The new created RowSet.
375 :
376 : */
377 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>
378 : createCursor( const OUString& _sDataSourceName,
379 : const OUString& _sCommand,
380 : sal_Int32 _nCommandType,
381 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection
382 : );
383 : //merge into one document - returns the number of merged docs
384 : sal_Int32 MergeDocuments( SwMailMergeConfigItem& rMMConfig, SwView& rSourceView );
385 : };
386 :
387 : #endif
388 :
389 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|