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 INCLUDED_SW_INC_DBMGR_HXX
20 : #define INCLUDED_SW_INC_DBMGR_HXX
21 :
22 : #include <rtl/ustring.hxx>
23 : #include <tools/link.hxx>
24 : #include <com/sun/star/util/Date.hpp>
25 : #include "swdllapi.h"
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 <com/sun/star/frame/XStorable.hpp>
32 : #include <com/sun/star/embed/XStorage.hpp>
33 : #include <boost/ptr_container/ptr_vector.hpp>
34 :
35 : namespace com{namespace sun{namespace star{
36 : namespace sdbc{
37 : class XConnection;
38 : class XStatement;
39 : class XDataSource;
40 : class XResultSet;
41 : }
42 : namespace beans{
43 :
44 : class XPropertySet;
45 : struct PropertyValue;
46 : }
47 : namespace sdbcx{
48 : class XColumnsSupplier;
49 : }
50 : namespace util{
51 : class XNumberFormatter;
52 : }
53 : namespace mail{
54 : class XSmtpService;
55 : }
56 : }}}
57 : namespace svx {
58 : class ODataAccessDescriptor;
59 : }
60 :
61 1240 : struct SwDBFormatData
62 : {
63 : com::sun::star::util::Date aNullDate;
64 : com::sun::star::uno::Reference< com::sun::star::util::XNumberFormatter> xFormatter;
65 : com::sun::star::lang::Locale aLocale;
66 : };
67 :
68 : namespace vcl {
69 : class Window;
70 : }
71 :
72 : class SwView;
73 : class SwWrtShell;
74 : class SfxProgress;
75 : class ListBox;
76 : class Button;
77 : class SvNumberFormatter;
78 : class SwXMailMerge;
79 : class SwMailMergeConfigItem;
80 : class SwCalc;
81 : class INetURLObject;
82 : class SwDocShell;
83 :
84 : enum DBManagerOptions
85 : {
86 : DBMGR_MERGE, ///< Data records in fields.
87 : DBMGR_MERGE_PRINTER, ///< Print mail merge.
88 : DBMGR_MERGE_EMAIL, ///< Send mail merge as email.
89 : DBMGR_MERGE_FILE, ///< Save mail merge as files.
90 : DBMGR_MERGE_SHELL ///< Create merge doc and keep the doc shell.
91 : };
92 :
93 : // Administration of (new) logical databases.
94 : enum class SwDBSelect
95 : {
96 : UNKNOWN, TABLE, QUERY
97 : };
98 :
99 16 : struct SwDSParam : public SwDBData
100 : {
101 : com::sun::star::util::Date aNullDate;
102 :
103 : ::com::sun::star::uno::Reference<com::sun::star::util::XNumberFormatter> xFormatter;
104 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection;
105 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XStatement> xStatement;
106 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet> xResultSet;
107 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aSelection;
108 : bool bScrollable;
109 : bool bEndOfDB;
110 : bool bAfterSelection;
111 : long nSelectionIndex;
112 :
113 0 : SwDSParam(const SwDBData& rData) :
114 : SwDBData(rData),
115 : bScrollable(false),
116 : bEndOfDB(false),
117 : bAfterSelection(false),
118 0 : nSelectionIndex(0)
119 0 : {}
120 :
121 8 : SwDSParam(const SwDBData& rData,
122 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& xResSet,
123 : const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rSelection) :
124 : SwDBData(rData),
125 : xResultSet(xResSet),
126 : aSelection(rSelection),
127 : bScrollable(true),
128 : bEndOfDB(false),
129 : bAfterSelection(false),
130 8 : nSelectionIndex(0)
131 8 : {}
132 :
133 73 : void CheckEndOfDB()
134 : {
135 73 : if(bEndOfDB)
136 9 : bAfterSelection = true;
137 73 : }
138 : };
139 : typedef boost::ptr_vector<SwDSParam> SwDSParamArr;
140 :
141 8 : struct SwMergeDescriptor
142 : {
143 : DBManagerOptions nMergeType;
144 : SwWrtShell& rSh;
145 : const svx::ODataAccessDescriptor& rDescriptor;
146 : OUString sSaveToFilter; ///< export filter to save resulting files
147 : OUString sSaveToFilterOptions;
148 : css::uno::Sequence< css::beans::PropertyValue > aSaveToFilterData;
149 :
150 : OUString sSubject;
151 : OUString sAddressFromColumn;
152 : OUString sMailBody;
153 : OUString sAttachmentName;
154 : css::uno::Sequence< OUString > aCopiesTo;
155 : css::uno::Sequence< OUString > aBlindCopiesTo;
156 :
157 : css::uno::Reference< css::mail::XSmtpService > xSmtpServer;
158 :
159 : bool bSendAsHTML;
160 : bool bSendAsAttachment;
161 :
162 : bool bPrintAsync;
163 : bool bCreateSingleFile;
164 : bool bSubjectIsFilename;
165 :
166 : SwMailMergeConfigItem* pMailMergeConfigItem;
167 :
168 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aPrintOptions;
169 :
170 8 : SwMergeDescriptor( DBManagerOptions nType, SwWrtShell& rShell, svx::ODataAccessDescriptor& rDesc ) :
171 : nMergeType(nType),
172 : rSh(rShell),
173 : rDescriptor(rDesc),
174 : bSendAsHTML( true ),
175 : bSendAsAttachment( false ),
176 : bPrintAsync( false ),
177 : bCreateSingleFile( false ),
178 : bSubjectIsFilename( false ),
179 8 : pMailMergeConfigItem(0)
180 8 : {}
181 :
182 : };
183 :
184 : struct SwDBManager_Impl;
185 : class SwConnectionDisposedListener_Impl;
186 : class AbstractMailMergeDlg;
187 : class SwDoc;
188 :
189 : class SW_DLLPUBLIC SwDBManager
190 : {
191 : friend class SwConnectionDisposedListener_Impl;
192 :
193 : OUString sEMailAddrField; ///< Mailing: Column name of email address.
194 : OUString sSubject; ///< Mailing: Subject
195 : OUString sAttached; ///< Mailing: Attached Files.
196 : bool bCancel; ///< Mail merge canceled.
197 : bool bInitDBFields : 1;
198 : bool bSingleJobs : 1; ///< Printing job when called from Basic.
199 : bool bInMerge : 1; ///< merge process active
200 : bool bMergeSilent : 1; ///< suppress display of dialogs/boxes (used when called over API)
201 : bool bMergeLock : 1; /**< prevent update of database fields while document is
202 : actually printed at the SwViewShell */
203 : SwDSParamArr aDataSourceParams;
204 : SwDBManager_Impl* pImpl;
205 : const SwXMailMerge* pMergeEvtSrc; ///< != 0 if mail merge events are to be send
206 : /// Name of the embedded database that's included in the current document.
207 : OUString m_sEmbeddedName;
208 :
209 : /// The document that owns this manager.
210 : SwDoc* m_pDoc;
211 :
212 : SAL_DLLPRIVATE SwDSParam* FindDSData(const SwDBData& rData, bool bCreate);
213 : SAL_DLLPRIVATE SwDSParam* FindDSConnection(const OUString& rSource, bool bCreate);
214 :
215 : DECL_DLLPRIVATE_LINK( PrtCancelHdl, Button * );
216 :
217 : /// Insert data record as text into document.
218 : SAL_DLLPRIVATE void ImportFromConnection( SwWrtShell* pSh);
219 :
220 : /// Insert a single data record as text into document.
221 : SAL_DLLPRIVATE void ImportDBEntry(SwWrtShell* pSh);
222 :
223 : /// merge to file _and_ merge to e-Mail
224 : SAL_DLLPRIVATE bool MergeMailFiles(SwWrtShell* pSh,
225 : const SwMergeDescriptor& rMergeDescriptor, vcl::Window* pParent );
226 : SAL_DLLPRIVATE bool ToNextRecord(SwDSParam* pParam);
227 :
228 : public:
229 : SwDBManager(SwDoc* pDoc);
230 : ~SwDBManager();
231 :
232 : enum DBConnURITypes {
233 : DBCONN_UNKNOWN = 0,
234 : DBCONN_ODB,
235 : DBCONN_CALC,
236 : DBCONN_DBASE,
237 : DBCONN_FLAT,
238 : DBCONN_MSJET,
239 : DBCONN_MSACE
240 : };
241 :
242 : /// MailMergeEvent source
243 73 : const SwXMailMerge * GetMailMergeEvtSrc() const { return pMergeEvtSrc; }
244 16 : void SetMailMergeEvtSrc( const SwXMailMerge *pSrc ) { pMergeEvtSrc = pSrc; }
245 :
246 81 : inline bool IsMergeSilent() const { return bMergeSilent; }
247 8 : inline void SetMergeSilent( bool bVal ) { bMergeSilent = bVal; }
248 :
249 : /// Merging of data records into fields.
250 : bool MergeNew( const SwMergeDescriptor& rMergeDesc, vcl::Window* pParent = NULL );
251 : static bool Merge(SwWrtShell* pSh);
252 : void MergeCancel();
253 :
254 : /// Initialize data fields that lack name of database.
255 8 : inline bool IsInitDBFields() const { return bInitDBFields; }
256 125 : inline void SetInitDBFields(bool b) { bInitDBFields = b; }
257 :
258 : /// Print / Save mail merge one by one or all together.
259 : bool IsSingleJobs() const { return bSingleJobs; }
260 : void SetSingleJobs(bool b) { bSingleJobs = b; }
261 :
262 : /// Mailing: Set email data.
263 3 : inline void SetEMailColumn(const OUString& sColName) { sEMailAddrField = sColName; }
264 3 : inline void SetSubject(const OUString& sSbj) { sSubject = sSbj; }
265 : inline void SetAttachment(const OUString& sAtt) { sAttached = sAtt; }
266 :
267 : /// Fill listbox with all table names of a database.
268 : bool GetTableNames(ListBox* pListBox, const OUString& rDBName );
269 :
270 : /// Fill listbox with all column names of a database table.
271 : void GetColumnNames(ListBox* pListBox,
272 : const OUString& rDBName, const OUString& rTableName, bool bAppend = false);
273 : static void GetColumnNames(ListBox* pListBox,
274 : css::uno::Reference< css::sdbc::XConnection> xConnection,
275 : const OUString& rTableName, bool bAppend = false);
276 :
277 : static sal_uLong GetColumnFormat( ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource> xSource,
278 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection,
279 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xColumn,
280 : SvNumberFormatter* pNFormatr,
281 : long nLanguage );
282 :
283 : sal_uLong GetColumnFormat( const OUString& rDBName,
284 : const OUString& rTableName,
285 : const OUString& rColNm,
286 : SvNumberFormatter* pNFormatr,
287 : long nLanguage );
288 : sal_Int32 GetColumnType( const OUString& rDBName,
289 : const OUString& rTableName,
290 : const OUString& rColNm );
291 :
292 0 : inline bool IsInMerge() const { return bInMerge; }
293 :
294 : void ExecuteFormLetter(SwWrtShell& rSh,
295 : const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& rProperties,
296 : bool bWithDataSourceBrowser = false);
297 :
298 : static void InsertText(SwWrtShell& rSh,
299 : const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& rProperties);
300 :
301 : /// check if a data source is open
302 : bool IsDataSourceOpen(const OUString& rDataSource,
303 : const OUString& rTableOrQuery, bool bMergeShell);
304 :
305 : /// open the source while fields are updated - for the calculator only!
306 : bool OpenDataSource(const OUString& rDataSource, const OUString& rTableOrQuery,
307 : sal_Int32 nCommandType = -1, bool bCreate = false);
308 : sal_uInt32 GetSelectedRecordId(const OUString& rDataSource, const OUString& rTableOrQuery, sal_Int32 nCommandType = -1);
309 : bool GetColumnCnt(const OUString& rSourceName, const OUString& rTableName,
310 : const OUString& rColumnName, sal_uInt32 nAbsRecordId, long nLanguage,
311 : OUString& rResult, double* pNumber);
312 : /** create and store or find an already stored connection to a data source for use
313 : in SwFieldMgr and SwDBTreeList */
314 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>
315 : RegisterConnection(OUString& rSource);
316 :
317 0 : const SwDSParam* CreateDSData(const SwDBData& rData)
318 0 : {return FindDSData(rData, true);}
319 0 : const SwDSParamArr& GetDSParamArray() const {return aDataSourceParams;}
320 :
321 : /// close all data sources - after fields were updated
322 : void CloseAll(bool bIncludingMerge = true);
323 :
324 : bool GetMergeColumnCnt(const OUString& rColumnName, sal_uInt16 nLanguage,
325 : OUString &rResult, double *pNumber);
326 : bool FillCalcWithMergeData(SvNumberFormatter *pDocFormatter,
327 : sal_uInt16 nLanguage, bool asString, SwCalc &aCalc);
328 : bool ToNextMergeRecord();
329 : bool ToNextRecord(const OUString& rDataSource, const OUString& rTableOrQuery, sal_Int32 nCommandType = -1);
330 :
331 : bool ExistsNextRecord()const;
332 : sal_uInt32 GetSelectedRecordId();
333 : bool ToRecordId(sal_Int32 nSet);
334 :
335 : static const SwDBData& GetAddressDBName();
336 :
337 : static OUString GetDBField(
338 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xColumnProp,
339 : const SwDBFormatData& rDBFormatData,
340 : double *pNumber = NULL);
341 :
342 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>
343 : GetConnection(const OUString& rDataSource,
344 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource>& rxSource);
345 :
346 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XColumnsSupplier>
347 : GetColumnSupplier(::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>,
348 : const OUString& rTableOrQuery,
349 : SwDBSelect eTableOrQuery = SwDBSelect::UNKNOWN);
350 :
351 : static ::com::sun::star::uno::Sequence<OUString> GetExistingDatabaseNames();
352 :
353 : static DBConnURITypes GetDBunoURI(const OUString &rURI, ::com::sun::star::uno::Any &aURLAny);
354 :
355 : /**
356 : Loads a data source from file and registers it.
357 :
358 : This function requires GUI interaction, as it loads the data source from
359 : the filename returned by a file picker and additional settings dialog.
360 : In case of success it returns the registered name, otherwise an empty string.
361 : */
362 : static OUString LoadAndRegisterDataSource(SwDocShell* pDocShell = 0);
363 :
364 : /**
365 : Loads a data source from file and registers it.
366 :
367 : In case of success it returns the registered name, otherwise an empty string.
368 : Optionally add a prefix to the registered DB name.
369 : */
370 : static OUString LoadAndRegisterDataSource(const DBConnURITypes type, const ::com::sun::star::uno::Any &rUnoURI,
371 : const ::com::sun::star::uno::Reference < ::com::sun::star::beans::XPropertySet > *pSettings,
372 : const OUString &rURI, const OUString *pPrefix = 0, const OUString *pDestDir = 0,
373 : SwDocShell* pDocShell = 0);
374 : /**
375 : Loads a data source from file and registers it.
376 :
377 : Convenience function, which calls GetDBunoURI and has just one mandatory parameter.
378 : In case of success it returns the registered name, otherwise an empty string.
379 : */
380 : static OUString LoadAndRegisterDataSource(const OUString& rURI, const OUString *pPrefix = 0, const OUString *pDestDir = 0,
381 : const ::com::sun::star::uno::Reference < ::com::sun::star::beans::XPropertySet > *pSettings = 0);
382 :
383 : /// Load the embedded data source of the document and also register it.
384 : void LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const SwDocShell& rDocShell);
385 :
386 : /// Unregister a data source.
387 : static void RevokeDataSource(const OUString& rName);
388 :
389 : /** try to get the data source from the given connection through the XChild interface.
390 : If this is not possible, the data source will be created through its name.
391 : @param _xConnection
392 : The connection which should support the XChild interface. (not a must)
393 : @param _sDataSourceName
394 : The data source name will be used to create the data source when the connection can not be used for it.
395 : @return
396 : The data source.
397 : */
398 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource>
399 : getDataSourceAsParent(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,const OUString& _sDataSourceName);
400 :
401 : /** creates a RowSet, which must be disposed after use.
402 : @param _sDataSourceName
403 : The data source name
404 : @param _sCommand
405 : The command.
406 : @param _nCommandType
407 : The type of the command.
408 : @param _xConnection
409 : The active connection which may be <NULL/>.
410 : @return
411 : The new created RowSet.
412 :
413 : */
414 : static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>
415 : createCursor( const OUString& _sDataSourceName,
416 : const OUString& _sCommand,
417 : sal_Int32 _nCommandType,
418 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection
419 : );
420 :
421 : void setEmbeddedName(const OUString& rEmbeddedName, SwDocShell& rDocShell);
422 : OUString getEmbeddedName() const;
423 :
424 : static void StoreEmbeddedDataSource(const css::uno::Reference<css::frame::XStorable>& xStorable,
425 : const css::uno::Reference<css::embed::XStorage>& xStorage,
426 : const OUString& rStreamRelPath,
427 : const OUString& rOwnURL);
428 :
429 : SwDoc* getDoc() const;
430 : /// Stop reacting to removed database registrations.
431 : void releaseRevokeListener();
432 : };
433 :
434 : #endif
435 :
436 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|