xref: /aoo41x/main/sw/source/ui/dbui/dbmgr.cxx (revision 8ef2f12b)
1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10efeef26fSAndrew Rist  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12efeef26fSAndrew Rist  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19efeef26fSAndrew Rist  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
2643f0f119SHerbert Dürr 
27cdf0e10cSrcweir #include <cstdarg>
2843f0f119SHerbert Dürr #include <cstdio>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <unotxdoc.hxx>
31cdf0e10cSrcweir #include <com/sun/star/text/NotePrintMode.hpp>
32cdf0e10cSrcweir #include <sfx2/app.hxx>
33cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
34cdf0e10cSrcweir #include <com/sun/star/sdb/XDocumentDataSource.hpp>
35cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XEventListener.hpp>
38cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatter.hpp>
39cdf0e10cSrcweir #include <com/sun/star/sdb/XCompletedConnection.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdb/XCompletedExecution.hpp>
41cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
42cdf0e10cSrcweir #include <com/sun/star/text/MailMergeEvent.hpp>
43cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
45cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
46cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
47cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
48cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
49cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
50cdf0e10cSrcweir #include <sfx2/fcontnr.hxx>
51cdf0e10cSrcweir #include <sfx2/filedlghelper.hxx>
52cdf0e10cSrcweir #include <sfx2/viewfrm.hxx>
53cdf0e10cSrcweir #include <dbconfig.hxx>
54cdf0e10cSrcweir #include <swdbtoolsclient.hxx>
55cdf0e10cSrcweir #include <pagedesc.hxx>
56cdf0e10cSrcweir #include <vcl/lstbox.hxx>
57cdf0e10cSrcweir #include <unotools/tempfile.hxx>
58cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
59cdf0e10cSrcweir #include <svl/urihelper.hxx>
60cdf0e10cSrcweir #define _SVSTDARR_STRINGSDTOR
61cdf0e10cSrcweir #include <svl/svstdarr.hxx>
62cdf0e10cSrcweir #include <svl/zforlist.hxx>
63cdf0e10cSrcweir #include <svl/zformat.hxx>
64cdf0e10cSrcweir #include <svl/stritem.hxx>
65cdf0e10cSrcweir #include <svl/eitem.hxx>
66cdf0e10cSrcweir #include <vcl/oldprintadaptor.hxx>
67cdf0e10cSrcweir #include <sfx2/docfile.hxx>
68cdf0e10cSrcweir #include <sfx2/progress.hxx>
69cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
70cdf0e10cSrcweir #include <svl/mailenum.hxx>
71cdf0e10cSrcweir #include <cmdid.h>
72cdf0e10cSrcweir #include <swmodule.hxx>
73cdf0e10cSrcweir #include <view.hxx>
74cdf0e10cSrcweir #include <docsh.hxx>
75cdf0e10cSrcweir #include <edtwin.hxx>
76cdf0e10cSrcweir #include <wrtsh.hxx>
77cdf0e10cSrcweir #include <fldbas.hxx>
78cdf0e10cSrcweir #include <initui.hxx>
79cdf0e10cSrcweir #include <swundo.hxx>
80cdf0e10cSrcweir #include <flddat.hxx>
81cdf0e10cSrcweir #include <modcfg.hxx>
82cdf0e10cSrcweir #include <shellio.hxx>
83cdf0e10cSrcweir #include <dbui.hxx>
84cdf0e10cSrcweir #include <dbmgr.hxx>
85cdf0e10cSrcweir #include <doc.hxx>
86cdf0e10cSrcweir #include <swwait.hxx>
87cdf0e10cSrcweir #include <swunohelper.hxx>
88cdf0e10cSrcweir #include <dbui.hrc>
89cdf0e10cSrcweir #include <globals.hrc>
90cdf0e10cSrcweir #include <statstr.hrc>
91cdf0e10cSrcweir #include <mmconfigitem.hxx>
92cdf0e10cSrcweir #include <sfx2/request.hxx>
93cdf0e10cSrcweir #include <hintids.hxx>
94cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
95cdf0e10cSrcweir #include <com/sun/star/sdbc/XRowSet.hpp>
96cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
97cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
98cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
99cdf0e10cSrcweir #include <com/sun/star/sdb/XColumn.hpp>
100cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
101cdf0e10cSrcweir #include <com/sun/star/sdbc/ResultSetType.hpp>
102cdf0e10cSrcweir #include <com/sun/star/mail/MailAttachment.hpp>
103cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
104cdf0e10cSrcweir #include <comphelper/types.hxx>
105cdf0e10cSrcweir #include <comphelper/property.hxx>
106cdf0e10cSrcweir #include <mailmergehelper.hxx>
107cdf0e10cSrcweir #include <maildispatcher.hxx>
108cdf0e10cSrcweir #include <svtools/htmlcfg.hxx>
109cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
110cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
111cdf0e10cSrcweir #include <editeng/langitem.hxx>
112cdf0e10cSrcweir #include <svl/numuno.hxx>
113cdf0e10cSrcweir 
114cdf0e10cSrcweir #include <unomailmerge.hxx>
115cdf0e10cSrcweir #include <sfx2/event.hxx>
116cdf0e10cSrcweir #include <vcl/msgbox.hxx>
117cdf0e10cSrcweir #include <svx/dataaccessdescriptor.hxx>
118cdf0e10cSrcweir #include <vos/mutex.hxx>
119cdf0e10cSrcweir #include <rtl/textenc.h>
120cdf0e10cSrcweir #include <ndindex.hxx>
121cdf0e10cSrcweir #include <pam.hxx>
122cdf0e10cSrcweir #include <swcrsr.hxx>
123cdf0e10cSrcweir #include <swevent.hxx>
124cdf0e10cSrcweir #include <osl/file.hxx>
125cdf0e10cSrcweir #include <swabstdlg.hxx>
126cdf0e10cSrcweir #include <fmthdft.hxx>
127cdf0e10cSrcweir #include <dbui.hrc>
128cdf0e10cSrcweir #include <envelp.hrc>
129cdf0e10cSrcweir #include <memory>
130cdf0e10cSrcweir #include <vector>
131cdf0e10cSrcweir #include <unomid.h>
132cdf0e10cSrcweir #include <section.hxx>
133cdf0e10cSrcweir 
134cdf0e10cSrcweir using namespace ::osl;
135cdf0e10cSrcweir using namespace ::svx;
136cdf0e10cSrcweir using namespace ::com::sun::star;
137cdf0e10cSrcweir using namespace ::com::sun::star::text;
138cdf0e10cSrcweir using namespace ::com::sun::star::uno;
139cdf0e10cSrcweir using namespace ::com::sun::star::container;
140cdf0e10cSrcweir using namespace ::com::sun::star::frame;
141cdf0e10cSrcweir using namespace ::com::sun::star::lang;
142cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
143cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
144cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
145cdf0e10cSrcweir using namespace ::com::sun::star::beans;
146cdf0e10cSrcweir using namespace ::com::sun::star::util;
147cdf0e10cSrcweir using namespace ::com::sun::star::task;
148cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir #define DB_SEP_SPACE  	0
151cdf0e10cSrcweir #define DB_SEP_TAB  	1
152cdf0e10cSrcweir #define DB_SEP_RETURN   2
153cdf0e10cSrcweir #define DB_SEP_NEWLINE  3
154cdf0e10cSrcweir 
155cdf0e10cSrcweir SV_IMPL_PTRARR(SwDSParamArr, SwDSParamPtr);
156cdf0e10cSrcweir const sal_Char cCursor[] = "Cursor";
157cdf0e10cSrcweir const sal_Char cCommand[] = "Command";
158cdf0e10cSrcweir const sal_Char cCommandType[] = "CommandType";
159cdf0e10cSrcweir const sal_Char cDataSourceName[] = "DataSourceName";
160cdf0e10cSrcweir const sal_Char cSelection[] = "Selection";
161cdf0e10cSrcweir const sal_Char cActiveConnection[] = "ActiveConnection";
162cdf0e10cSrcweir 
163cdf0e10cSrcweir // -----------------------------------------------------------------------------
164cdf0e10cSrcweir // Use nameless namespace to avoid to rubbish the global namespace
165cdf0e10cSrcweir // -----------------------------------------------------------------------------
166cdf0e10cSrcweir namespace
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 
lcl_getCountFromResultSet(sal_Int32 & rCount,const uno::Reference<XResultSet> & xResultSet)169cdf0e10cSrcweir bool lcl_getCountFromResultSet( sal_Int32& rCount, const uno::Reference<XResultSet>& xResultSet )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     uno::Reference<XPropertySet> xPrSet(xResultSet, UNO_QUERY);
172cdf0e10cSrcweir     if(xPrSet.is())
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         try
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             sal_Bool bFinal = sal_False;
177cdf0e10cSrcweir             Any aFinal = xPrSet->getPropertyValue(C2U("IsRowCountFinal"));
178cdf0e10cSrcweir             aFinal >>= bFinal;
179cdf0e10cSrcweir             if(!bFinal)
180cdf0e10cSrcweir             {
181cdf0e10cSrcweir                 xResultSet->last();
182cdf0e10cSrcweir                 xResultSet->first();
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir             Any aCount = xPrSet->getPropertyValue(C2U("RowCount"));
185cdf0e10cSrcweir             if( aCount >>= rCount )
186cdf0e10cSrcweir                 return true;
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir         catch(Exception&)
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir     return false;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir // #122799# copy compatibility options
lcl_CopyCompatibilityOptions(SwWrtShell & rSourceShell,SwWrtShell & rTargetShell)195cdf0e10cSrcweir void lcl_CopyCompatibilityOptions( SwWrtShell& rSourceShell, SwWrtShell& rTargetShell)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     IDocumentSettingAccess* pIDsa = rSourceShell.getIDocumentSettingAccess();
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     rTargetShell.SetParaSpaceMax( pIDsa->get(IDocumentSettingAccess::PARA_SPACE_MAX));
200cdf0e10cSrcweir     rTargetShell.SetParaSpaceMaxAtPages(pIDsa->get(IDocumentSettingAccess::PARA_SPACE_MAX_AT_PAGES));
201cdf0e10cSrcweir     rTargetShell.SetTabCompat( pIDsa->get(IDocumentSettingAccess::TAB_COMPAT));
202cdf0e10cSrcweir     rTargetShell.SetAddExtLeading( pIDsa->get(IDocumentSettingAccess::ADD_EXT_LEADING));
203cdf0e10cSrcweir     rTargetShell.SetUseVirDev( pIDsa->get(IDocumentSettingAccess::USE_VIRTUAL_DEVICE));
204cdf0e10cSrcweir     rTargetShell.SetAddParaSpacingToTableCells( pIDsa->get(IDocumentSettingAccess::ADD_PARA_SPACING_TO_TABLE_CELLS));
205cdf0e10cSrcweir     rTargetShell.SetUseFormerLineSpacing( pIDsa->get(IDocumentSettingAccess::OLD_LINE_SPACING));
206cdf0e10cSrcweir     rTargetShell.SetUseFormerObjectPositioning( pIDsa->get(IDocumentSettingAccess::USE_FORMER_OBJECT_POS));
207cdf0e10cSrcweir     rTargetShell.SetConsiderWrapOnObjPos( pIDsa->get(IDocumentSettingAccess::CONSIDER_WRAP_ON_OBJECT_POSITION));
208cdf0e10cSrcweir     rTargetShell.SetUseFormerTextWrapping( pIDsa->get(IDocumentSettingAccess::USE_FORMER_TEXT_WRAPPING));
209cdf0e10cSrcweir }
210cdf0e10cSrcweir }
211cdf0e10cSrcweir /* -----------------09.12.2002 12:35-----------------
212cdf0e10cSrcweir  *
213cdf0e10cSrcweir  * --------------------------------------------------*/
214cdf0e10cSrcweir 
215cdf0e10cSrcweir class SwConnectionDisposedListener_Impl : public cppu::WeakImplHelper1
216cdf0e10cSrcweir < lang::XEventListener >
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     SwNewDBMgr&     rDBMgr;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     virtual void SAL_CALL disposing( const EventObject& Source ) throw (RuntimeException);
221cdf0e10cSrcweir public:
222cdf0e10cSrcweir     SwConnectionDisposedListener_Impl(SwNewDBMgr& rMgr);
223cdf0e10cSrcweir     ~SwConnectionDisposedListener_Impl();
224cdf0e10cSrcweir 
225cdf0e10cSrcweir };
226cdf0e10cSrcweir // -----------------------------------------------------------------------------
227cdf0e10cSrcweir struct SwNewDBMgr_Impl
228cdf0e10cSrcweir {
229cdf0e10cSrcweir     SwDSParam*          pMergeData;
230cdf0e10cSrcweir     AbstractMailMergeDlg*     pMergeDialog;
231cdf0e10cSrcweir     uno::Reference<lang::XEventListener> xDisposeListener;
232cdf0e10cSrcweir 
SwNewDBMgr_ImplSwNewDBMgr_Impl233cdf0e10cSrcweir     SwNewDBMgr_Impl(SwNewDBMgr& rDBMgr)
234cdf0e10cSrcweir        :pMergeData(0)
235cdf0e10cSrcweir        ,pMergeDialog(0)
236cdf0e10cSrcweir        ,xDisposeListener(new SwConnectionDisposedListener_Impl(rDBMgr))
237cdf0e10cSrcweir         {}
238cdf0e10cSrcweir };
239cdf0e10cSrcweir /*-- 24.10.2003 15:54:18---------------------------------------------------
240cdf0e10cSrcweir 
241cdf0e10cSrcweir   -----------------------------------------------------------------------*/
lcl_InitNumberFormatter(SwDSParam & rParam,uno::Reference<XDataSource> xSource)242cdf0e10cSrcweir void lcl_InitNumberFormatter(SwDSParam& rParam, uno::Reference<XDataSource> xSource)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     uno::Reference<XMultiServiceFactory> xMgr = ::comphelper::getProcessServiceFactory();
245cdf0e10cSrcweir     if( xMgr.is() )
246cdf0e10cSrcweir     {
247cdf0e10cSrcweir         uno::Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.util.NumberFormatter" ));
248cdf0e10cSrcweir         rParam.xFormatter = uno::Reference<util::XNumberFormatter>(xInstance, UNO_QUERY) ;
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir     if(!xSource.is())
251cdf0e10cSrcweir         xSource = SwNewDBMgr::getDataSourceAsParent(rParam.xConnection, rParam.sDataSource);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     uno::Reference<XPropertySet> xSourceProps(xSource, UNO_QUERY);
254cdf0e10cSrcweir     if(xSourceProps.is())
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         Any aFormats = xSourceProps->getPropertyValue(C2U("NumberFormatsSupplier"));
257cdf0e10cSrcweir         if(aFormats.hasValue())
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             uno::Reference<XNumberFormatsSupplier> xSuppl;
260cdf0e10cSrcweir             aFormats >>= xSuppl;
261cdf0e10cSrcweir             if(xSuppl.is())
262cdf0e10cSrcweir             {
263cdf0e10cSrcweir                 uno::Reference< XPropertySet > xSettings = xSuppl->getNumberFormatSettings();
264cdf0e10cSrcweir                 Any aNull = xSettings->getPropertyValue(C2U("NullDate"));
265cdf0e10cSrcweir                 aNull >>= rParam.aNullDate;
266cdf0e10cSrcweir 				if(rParam.xFormatter.is())
267cdf0e10cSrcweir 					rParam.xFormatter->attachNumberFormatsSupplier(xSuppl);
268cdf0e10cSrcweir             }
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir     }
271cdf0e10cSrcweir }
272cdf0e10cSrcweir /* -----------------------------17.07.00 17:04--------------------------------
273cdf0e10cSrcweir 
274cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
lcl_MoveAbsolute(SwDSParam * pParam,long nAbsPos)275cdf0e10cSrcweir sal_Bool lcl_MoveAbsolute(SwDSParam* pParam, long nAbsPos)
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
278cdf0e10cSrcweir 	try
279cdf0e10cSrcweir 	{
280cdf0e10cSrcweir 		if(pParam->bScrollable)
281cdf0e10cSrcweir 		{
282cdf0e10cSrcweir 			bRet = pParam->xResultSet->absolute( nAbsPos );
283cdf0e10cSrcweir 		}
284cdf0e10cSrcweir 		else
285cdf0e10cSrcweir 		{
286cdf0e10cSrcweir             DBG_ERROR("no absolute positioning available");
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir 	}
289cdf0e10cSrcweir 	catch(Exception aExcept)
290cdf0e10cSrcweir 	{
291cdf0e10cSrcweir 	}
292cdf0e10cSrcweir 	return bRet;
293cdf0e10cSrcweir }
294cdf0e10cSrcweir /* -----------------------------17.07.00 17:23--------------------------------
295cdf0e10cSrcweir 
296cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
lcl_GetColumnCnt(SwDSParam * pParam,const String & rColumnName,long nLanguage,String & rResult,double * pNumber)297cdf0e10cSrcweir sal_Bool lcl_GetColumnCnt(SwDSParam* pParam,
298cdf0e10cSrcweir 	const String& rColumnName, long nLanguage, String& rResult, double* pNumber)
299cdf0e10cSrcweir {
300cdf0e10cSrcweir     uno::Reference< XColumnsSupplier > xColsSupp( pParam->xResultSet, UNO_QUERY );
301cdf0e10cSrcweir     uno::Reference<XNameAccess> xCols;
302cdf0e10cSrcweir     try
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         xCols = xColsSupp->getColumns();
305cdf0e10cSrcweir     }
306cdf0e10cSrcweir     catch( lang::DisposedException& )
307cdf0e10cSrcweir     {
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir 	if(!xCols.is() || !xCols->hasByName(rColumnName))
310cdf0e10cSrcweir 		return sal_False;
311cdf0e10cSrcweir 	Any aCol = xCols->getByName(rColumnName);
312cdf0e10cSrcweir     uno::Reference< XPropertySet > xColumnProps;
313cdf0e10cSrcweir     aCol >>= xColumnProps;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	SwDBFormatData aFormatData;
316cdf0e10cSrcweir     if(!pParam->xFormatter.is())
317cdf0e10cSrcweir 	{
318cdf0e10cSrcweir         uno::Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(
319cdf0e10cSrcweir 									pParam->xConnection,pParam->sDataSource);
320cdf0e10cSrcweir         lcl_InitNumberFormatter(*pParam, xSource );
321cdf0e10cSrcweir 	}
322cdf0e10cSrcweir     aFormatData.aNullDate = pParam->aNullDate;
323cdf0e10cSrcweir 	aFormatData.xFormatter = pParam->xFormatter;
324cdf0e10cSrcweir 
325cdf0e10cSrcweir     MsLangId::convertLanguageToLocale( (LanguageType)nLanguage, aFormatData.aLocale );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 	rResult = SwNewDBMgr::GetDBField( xColumnProps, aFormatData, pNumber);
328cdf0e10cSrcweir 	return sal_True;
329cdf0e10cSrcweir };
330cdf0e10cSrcweir /*--------------------------------------------------------------------
331cdf0e10cSrcweir 	Beschreibung: Daten importieren
332cdf0e10cSrcweir  --------------------------------------------------------------------*/
MergeNew(const SwMergeDescriptor & rMergeDesc)333cdf0e10cSrcweir sal_Bool SwNewDBMgr::MergeNew(const SwMergeDescriptor& rMergeDesc )
334cdf0e10cSrcweir {
335cdf0e10cSrcweir     SetMergeType( rMergeDesc.nMergeType );
336cdf0e10cSrcweir 
337cdf0e10cSrcweir     DBG_ASSERT(!bInMerge && !pImpl->pMergeData, "merge already activated!");
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     SwDBData aData;
340cdf0e10cSrcweir     aData.nCommandType = CommandType::TABLE;
341cdf0e10cSrcweir     uno::Reference<XResultSet>  xResSet;
342cdf0e10cSrcweir     Sequence<Any> aSelection;
343cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     aData.sDataSource = rMergeDesc.rDescriptor.getDataSource();
346cdf0e10cSrcweir     rMergeDesc.rDescriptor[daCommand]      >>= aData.sCommand;
347cdf0e10cSrcweir     rMergeDesc.rDescriptor[daCommandType]  >>= aData.nCommandType;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     if ( rMergeDesc.rDescriptor.has(daCursor) )
350cdf0e10cSrcweir         rMergeDesc.rDescriptor[daCursor] >>= xResSet;
351cdf0e10cSrcweir     if ( rMergeDesc.rDescriptor.has(daSelection) )
352cdf0e10cSrcweir         rMergeDesc.rDescriptor[daSelection] >>= aSelection;
353cdf0e10cSrcweir     if ( rMergeDesc.rDescriptor.has(daConnection) )
354cdf0e10cSrcweir         rMergeDesc.rDescriptor[daConnection] >>= xConnection;
355cdf0e10cSrcweir 
356cdf0e10cSrcweir     if(!aData.sDataSource.getLength() || !aData.sCommand.getLength() || !xResSet.is())
357cdf0e10cSrcweir 	{
358cdf0e10cSrcweir 		return sal_False;
359cdf0e10cSrcweir 	}
360cdf0e10cSrcweir 
361cdf0e10cSrcweir     pImpl->pMergeData = new SwDSParam(aData, xResSet, aSelection);
362cdf0e10cSrcweir     SwDSParam*  pTemp = FindDSData(aData, sal_False);
363cdf0e10cSrcweir 	if(pTemp)
364cdf0e10cSrcweir         *pTemp = *pImpl->pMergeData;
365cdf0e10cSrcweir 	else
366cdf0e10cSrcweir 	{
367cdf0e10cSrcweir         //#94779# calls from the calculator may have added a connection with an invalid commandtype
368cdf0e10cSrcweir         //"real" data base connections added here have to re-use the already available
369cdf0e10cSrcweir         //DSData and set the correct CommandType
370cdf0e10cSrcweir         SwDBData aTempData(aData);
371cdf0e10cSrcweir 		aData.nCommandType = -1;
372cdf0e10cSrcweir 		pTemp = FindDSData(aData, sal_False);
373cdf0e10cSrcweir 		if(pTemp)
374cdf0e10cSrcweir 			*pTemp = *pImpl->pMergeData;
375cdf0e10cSrcweir 		else
376cdf0e10cSrcweir         {
377cdf0e10cSrcweir 			SwDSParam* pInsert = new SwDSParam(*pImpl->pMergeData);
378cdf0e10cSrcweir 			aDataSourceParams.Insert(pInsert, aDataSourceParams.Count());
379cdf0e10cSrcweir 			try
380cdf0e10cSrcweir 			{
381cdf0e10cSrcweir                 uno::Reference<XComponent> xComponent(pInsert->xConnection, UNO_QUERY);
382cdf0e10cSrcweir 				if(xComponent.is())
383cdf0e10cSrcweir 					xComponent->addEventListener(pImpl->xDisposeListener);
384cdf0e10cSrcweir 			}
385cdf0e10cSrcweir 			catch(Exception&)
386cdf0e10cSrcweir 			{
387cdf0e10cSrcweir 			}
388cdf0e10cSrcweir         }
389cdf0e10cSrcweir 	}
390cdf0e10cSrcweir     if(!pImpl->pMergeData->xConnection.is())
391cdf0e10cSrcweir         pImpl->pMergeData->xConnection = xConnection;
392cdf0e10cSrcweir     // add an XEventListener
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 	try{
395cdf0e10cSrcweir 		//set to start position
396cdf0e10cSrcweir         if(pImpl->pMergeData->aSelection.getLength())
397cdf0e10cSrcweir 		{
398cdf0e10cSrcweir 			sal_Int32 nPos = 0;
399cdf0e10cSrcweir 			pImpl->pMergeData->aSelection.getConstArray()[ pImpl->pMergeData->nSelectionIndex++ ] >>= nPos;
400cdf0e10cSrcweir             pImpl->pMergeData->bEndOfDB = !pImpl->pMergeData->xResultSet->absolute( nPos );
401cdf0e10cSrcweir             pImpl->pMergeData->CheckEndOfDB();
402cdf0e10cSrcweir             if(pImpl->pMergeData->nSelectionIndex >= pImpl->pMergeData->aSelection.getLength())
403cdf0e10cSrcweir                 pImpl->pMergeData->bEndOfDB = sal_True;
404cdf0e10cSrcweir 		}
405cdf0e10cSrcweir 		else
406cdf0e10cSrcweir 		{
407cdf0e10cSrcweir             pImpl->pMergeData->bEndOfDB = !pImpl->pMergeData->xResultSet->first();
408cdf0e10cSrcweir             pImpl->pMergeData->CheckEndOfDB();
409cdf0e10cSrcweir 		}
410cdf0e10cSrcweir 	}
411cdf0e10cSrcweir     catch(Exception&)
412cdf0e10cSrcweir 	{
413cdf0e10cSrcweir         pImpl->pMergeData->bEndOfDB = sal_True;
414cdf0e10cSrcweir         pImpl->pMergeData->CheckEndOfDB();
415cdf0e10cSrcweir 		DBG_ERROR("exception in MergeNew()");
416cdf0e10cSrcweir 	}
417cdf0e10cSrcweir 
418cdf0e10cSrcweir     uno::Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,aData.sDataSource);
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 	lcl_InitNumberFormatter(*pImpl->pMergeData, xSource);
421cdf0e10cSrcweir 
422cdf0e10cSrcweir     rMergeDesc.rSh.ChgDBData(aData);
423cdf0e10cSrcweir 	bInMerge = sal_True;
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 	if (IsInitDBFields())
426cdf0e10cSrcweir 	{
427cdf0e10cSrcweir 		// Bei Datenbankfeldern ohne DB-Name DB-Name von Dok einsetzen
428cdf0e10cSrcweir 		SvStringsDtor aDBNames(1, 1);
429cdf0e10cSrcweir 		aDBNames.Insert( new String(), 0);
430cdf0e10cSrcweir         SwDBData aInsertData = rMergeDesc.rSh.GetDBData();
431cdf0e10cSrcweir         String sDBName = aInsertData.sDataSource;
432cdf0e10cSrcweir 		sDBName += DB_DELIM;
433cdf0e10cSrcweir         sDBName += (String)aInsertData.sCommand;
434cdf0e10cSrcweir 		sDBName += DB_DELIM;
435cdf0e10cSrcweir         sDBName += String::CreateFromInt32(aInsertData.nCommandType);
436cdf0e10cSrcweir         rMergeDesc.rSh.ChangeDBFields( aDBNames, sDBName);
437cdf0e10cSrcweir 		SetInitDBFields(sal_False);
438cdf0e10cSrcweir 	}
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 	sal_Bool bRet = sal_True;
441cdf0e10cSrcweir     switch(rMergeDesc.nMergeType)
442cdf0e10cSrcweir 	{
443cdf0e10cSrcweir 		case DBMGR_MERGE:
444cdf0e10cSrcweir             bRet = Merge(&rMergeDesc.rSh);   // Mischen
445cdf0e10cSrcweir 			break;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         case DBMGR_MERGE_MAILMERGE: // printing merge from 'old' merge dialog or from UNO-component
448cdf0e10cSrcweir 		case DBMGR_MERGE_MAILING:
449cdf0e10cSrcweir         case DBMGR_MERGE_MAILFILES:
450cdf0e10cSrcweir         case DBMGR_MERGE_SINGLE_FILE:
451cdf0e10cSrcweir             // save files and send them as e-Mail if required
452cdf0e10cSrcweir             bRet = MergeMailFiles(&rMergeDesc.rSh,
453cdf0e10cSrcweir                     rMergeDesc);
454cdf0e10cSrcweir 			break;
455cdf0e10cSrcweir 
456cdf0e10cSrcweir 		default:    	// Einfuegen der selektierten Eintraege
457cdf0e10cSrcweir 						// (war: InsertRecord)
458cdf0e10cSrcweir             ImportFromConnection(&rMergeDesc.rSh);
459cdf0e10cSrcweir 			break;
460cdf0e10cSrcweir 	}
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 	EndMerge();
463cdf0e10cSrcweir 	return bRet;
464cdf0e10cSrcweir }
465cdf0e10cSrcweir 
466cdf0e10cSrcweir /*--------------------------------------------------------------------
467cdf0e10cSrcweir 	Beschreibung: Daten importieren
468cdf0e10cSrcweir  --------------------------------------------------------------------*/
469cdf0e10cSrcweir 
470cdf0e10cSrcweir 
Merge(SwWrtShell * pSh)471cdf0e10cSrcweir sal_Bool SwNewDBMgr::Merge(SwWrtShell* pSh)
472cdf0e10cSrcweir {
473cdf0e10cSrcweir 	pSh->StartAllAction();
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	pSh->ViewShell::UpdateFlds(sal_True);
476cdf0e10cSrcweir 	pSh->SetModified();
477cdf0e10cSrcweir 
478cdf0e10cSrcweir 	pSh->EndAllAction();
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 	return sal_True;
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir /*--------------------------------------------------------------------
484cdf0e10cSrcweir 	Beschreibung:
485cdf0e10cSrcweir  --------------------------------------------------------------------*/
ImportFromConnection(SwWrtShell * pSh)486cdf0e10cSrcweir void SwNewDBMgr::ImportFromConnection(	SwWrtShell* pSh )
487cdf0e10cSrcweir {
488cdf0e10cSrcweir     if(pImpl->pMergeData && !pImpl->pMergeData->bEndOfDB)
489cdf0e10cSrcweir 	{
490cdf0e10cSrcweir 		{
491cdf0e10cSrcweir 			pSh->StartAllAction();
492cdf0e10cSrcweir             pSh->StartUndo(UNDO_EMPTY);
493cdf0e10cSrcweir 			sal_Bool bGroupUndo(pSh->DoesGroupUndo());
494cdf0e10cSrcweir 			pSh->DoGroupUndo(sal_False);
495cdf0e10cSrcweir 
496cdf0e10cSrcweir 			if( pSh->HasSelection() )
497cdf0e10cSrcweir 				pSh->DelRight();
498cdf0e10cSrcweir 
499cdf0e10cSrcweir 			SwWait *pWait = 0;
500cdf0e10cSrcweir 
501cdf0e10cSrcweir 			{
502cdf0e10cSrcweir 				sal_uLong i = 0;
503cdf0e10cSrcweir 				do {
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 					ImportDBEntry(pSh);
506cdf0e10cSrcweir 					if( 10 == ++i )
507*8ef2f12bSOliver-Rainer Wittmann 						pWait = new SwWait( *pSh->GetView().GetDocShell(), true );
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 				} while(ToNextMergeRecord());
510cdf0e10cSrcweir 			}
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 			pSh->DoGroupUndo(bGroupUndo);
513cdf0e10cSrcweir             pSh->EndUndo(UNDO_EMPTY);
514cdf0e10cSrcweir 			pSh->EndAllAction();
515cdf0e10cSrcweir 			delete pWait;
516cdf0e10cSrcweir 		}
517cdf0e10cSrcweir 	}
518cdf0e10cSrcweir }
519cdf0e10cSrcweir /*-----------------24.02.97 10.30-------------------
520cdf0e10cSrcweir 
521cdf0e10cSrcweir --------------------------------------------------*/
522cdf0e10cSrcweir 
lcl_FindColumn(const String & sFormatStr,sal_uInt16 & nUsedPos,sal_uInt8 & nSeparator)523cdf0e10cSrcweir String 	lcl_FindColumn(const String& sFormatStr,sal_uInt16  &nUsedPos, sal_uInt8 &nSeparator)
524cdf0e10cSrcweir {
525cdf0e10cSrcweir 	String sReturn;
526cdf0e10cSrcweir 	sal_uInt16 nLen = sFormatStr.Len();
527cdf0e10cSrcweir 	nSeparator = 0xff;
528cdf0e10cSrcweir 	while(nUsedPos < nLen && nSeparator == 0xff)
529cdf0e10cSrcweir 	{
530cdf0e10cSrcweir 		sal_Unicode cAkt = sFormatStr.GetChar(nUsedPos);
531cdf0e10cSrcweir 		switch(cAkt)
532cdf0e10cSrcweir 		{
533cdf0e10cSrcweir 			case ',':
534cdf0e10cSrcweir 				nSeparator = DB_SEP_SPACE;
535cdf0e10cSrcweir 			break;
536cdf0e10cSrcweir 			case ';':
537cdf0e10cSrcweir 				nSeparator = DB_SEP_RETURN;
538cdf0e10cSrcweir 			break;
539cdf0e10cSrcweir 			case ':':
540cdf0e10cSrcweir 				nSeparator = DB_SEP_TAB;
541cdf0e10cSrcweir 			break;
542cdf0e10cSrcweir 			case '#':
543cdf0e10cSrcweir 				nSeparator = DB_SEP_NEWLINE;
544cdf0e10cSrcweir 			break;
545cdf0e10cSrcweir 			default:
546cdf0e10cSrcweir 				sReturn += cAkt;
547cdf0e10cSrcweir 		}
548cdf0e10cSrcweir 		nUsedPos++;
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 	}
551cdf0e10cSrcweir 	return sReturn;
552cdf0e10cSrcweir }
553cdf0e10cSrcweir 
554cdf0e10cSrcweir /*--------------------------------------------------------------------
555cdf0e10cSrcweir 	Beschreibung:
556cdf0e10cSrcweir  --------------------------------------------------------------------*/
ImportDBEntry(SwWrtShell * pSh)557cdf0e10cSrcweir void SwNewDBMgr::ImportDBEntry(SwWrtShell* pSh)
558cdf0e10cSrcweir {
559cdf0e10cSrcweir     if(pImpl->pMergeData && !pImpl->pMergeData->bEndOfDB)
560cdf0e10cSrcweir 	{
561cdf0e10cSrcweir         uno::Reference< XColumnsSupplier > xColsSupp( pImpl->pMergeData->xResultSet, UNO_QUERY );
562cdf0e10cSrcweir         uno::Reference<XNameAccess> xCols = xColsSupp->getColumns();
563cdf0e10cSrcweir         String sFormatStr;
564cdf0e10cSrcweir 		sal_uInt16 nFmtLen = sFormatStr.Len();
565cdf0e10cSrcweir 		if( nFmtLen )
566cdf0e10cSrcweir 		{
567cdf0e10cSrcweir 			const char cSpace = ' ';
568cdf0e10cSrcweir 			const char cTab = '\t';
569cdf0e10cSrcweir 			sal_uInt16 nUsedPos = 0;
570cdf0e10cSrcweir 			sal_uInt8	nSeparator;
571cdf0e10cSrcweir 			String sColumn = lcl_FindColumn(sFormatStr, nUsedPos, nSeparator);
572cdf0e10cSrcweir 			while( sColumn.Len() )
573cdf0e10cSrcweir 			{
574cdf0e10cSrcweir 				if(!xCols->hasByName(sColumn))
575cdf0e10cSrcweir 					return;
576cdf0e10cSrcweir 				Any aCol = xCols->getByName(sColumn);
577cdf0e10cSrcweir                 uno::Reference< XPropertySet > xColumnProp;
578cdf0e10cSrcweir                 aCol >>= xColumnProp;
579cdf0e10cSrcweir 				if(xColumnProp.is())
580cdf0e10cSrcweir 				{
581cdf0e10cSrcweir 					SwDBFormatData aDBFormat;
582cdf0e10cSrcweir 					String sInsert = GetDBField( xColumnProp,	aDBFormat);
583cdf0e10cSrcweir 					if( DB_SEP_SPACE == nSeparator )
584cdf0e10cSrcweir 							sInsert += cSpace;
585cdf0e10cSrcweir 					else if( DB_SEP_TAB == nSeparator)
586cdf0e10cSrcweir 							sInsert += cTab;
587cdf0e10cSrcweir 					pSh->Insert(sInsert);
588cdf0e10cSrcweir 					if( DB_SEP_RETURN == nSeparator)
589cdf0e10cSrcweir 						pSh->SplitNode();
590cdf0e10cSrcweir 					else if(DB_SEP_NEWLINE == nSeparator)
591cdf0e10cSrcweir 							pSh->InsertLineBreak();
592cdf0e10cSrcweir 				}
593cdf0e10cSrcweir 				else
594cdf0e10cSrcweir 				{
595cdf0e10cSrcweir 					// Spalte nicht gefunden -> Fehler anzeigen
596cdf0e10cSrcweir 					String sInsert = '?';
597cdf0e10cSrcweir 					sInsert += sColumn;
598cdf0e10cSrcweir 					sInsert += '?';
599cdf0e10cSrcweir 					pSh->Insert(sInsert);
600cdf0e10cSrcweir 				}
601cdf0e10cSrcweir 				sColumn = lcl_FindColumn(sFormatStr, nUsedPos, nSeparator);
602cdf0e10cSrcweir 			}
603cdf0e10cSrcweir 			pSh->SplitNode();
604cdf0e10cSrcweir 		}
605cdf0e10cSrcweir 		else
606cdf0e10cSrcweir 		{
607cdf0e10cSrcweir 			String sStr;
608cdf0e10cSrcweir             Sequence<rtl::OUString> aColNames = xCols->getElementNames();
609cdf0e10cSrcweir             const rtl::OUString* pColNames = aColNames.getConstArray();
610cdf0e10cSrcweir 			long nLength = aColNames.getLength();
611cdf0e10cSrcweir 			for(long i = 0; i < nLength; i++)
612cdf0e10cSrcweir 			{
613cdf0e10cSrcweir 				Any aCol = xCols->getByName(pColNames[i]);
614cdf0e10cSrcweir                 uno::Reference< XPropertySet > xColumnProp;
615cdf0e10cSrcweir                 aCol >>= xColumnProp;
616cdf0e10cSrcweir 				SwDBFormatData aDBFormat;
617cdf0e10cSrcweir 				sStr += GetDBField( xColumnProp, aDBFormat);
618cdf0e10cSrcweir 				if (i < nLength - 1)
619cdf0e10cSrcweir 					sStr += '\t';
620cdf0e10cSrcweir 			}
621cdf0e10cSrcweir             pSh->SwEditShell::Insert2(sStr);
622cdf0e10cSrcweir 			pSh->SwFEShell::SplitNode();	// Zeilenvorschub
623cdf0e10cSrcweir 		}
624cdf0e10cSrcweir 	}
625cdf0e10cSrcweir }
626cdf0e10cSrcweir /*--------------------------------------------------------------------
627cdf0e10cSrcweir 	Beschreibung: Listbox mit Tabellenliste fuellen
628cdf0e10cSrcweir  --------------------------------------------------------------------*/
GetTableNames(ListBox * pListBox,const String & rDBName)629cdf0e10cSrcweir sal_Bool SwNewDBMgr::GetTableNames(ListBox* pListBox, const String& rDBName)
630cdf0e10cSrcweir {
631cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
632cdf0e10cSrcweir     String sOldTableName(pListBox->GetSelectEntry());
633cdf0e10cSrcweir 	pListBox->Clear();
634cdf0e10cSrcweir     SwDSParam* pParam = FindDSConnection(rDBName, sal_False);
635cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
636cdf0e10cSrcweir     if(pParam && pParam->xConnection.is())
637cdf0e10cSrcweir         xConnection = pParam->xConnection;
638cdf0e10cSrcweir     else
639cdf0e10cSrcweir     {
640cdf0e10cSrcweir         rtl::OUString sDBName(rDBName);
641cdf0e10cSrcweir         if ( sDBName.getLength() )
642cdf0e10cSrcweir             xConnection = RegisterConnection( sDBName );
643cdf0e10cSrcweir     }
644cdf0e10cSrcweir 	if(xConnection.is())
645cdf0e10cSrcweir 	{
646cdf0e10cSrcweir         uno::Reference<XTablesSupplier> xTSupplier = uno::Reference<XTablesSupplier>(xConnection, UNO_QUERY);
647cdf0e10cSrcweir 		if(xTSupplier.is())
648cdf0e10cSrcweir 		{
649cdf0e10cSrcweir             uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
650cdf0e10cSrcweir             Sequence<rtl::OUString> aTbls = xTbls->getElementNames();
651cdf0e10cSrcweir             const rtl::OUString* pTbls = aTbls.getConstArray();
652cdf0e10cSrcweir 			for(long i = 0; i < aTbls.getLength(); i++)
653cdf0e10cSrcweir             {
654cdf0e10cSrcweir                 sal_uInt16 nEntry = pListBox->InsertEntry(pTbls[i]);
655cdf0e10cSrcweir                 pListBox->SetEntryData(nEntry, (void*)0);
656cdf0e10cSrcweir             }
657cdf0e10cSrcweir 		}
658cdf0e10cSrcweir         uno::Reference<XQueriesSupplier> xQSupplier = uno::Reference<XQueriesSupplier>(xConnection, UNO_QUERY);
659cdf0e10cSrcweir 		if(xQSupplier.is())
660cdf0e10cSrcweir 		{
661cdf0e10cSrcweir             uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
662cdf0e10cSrcweir             Sequence<rtl::OUString> aQueries = xQueries->getElementNames();
663cdf0e10cSrcweir             const rtl::OUString* pQueries = aQueries.getConstArray();
664cdf0e10cSrcweir 			for(long i = 0; i < aQueries.getLength(); i++)
665cdf0e10cSrcweir             {
666cdf0e10cSrcweir                 sal_uInt16 nEntry = pListBox->InsertEntry(pQueries[i]);
667cdf0e10cSrcweir                 pListBox->SetEntryData(nEntry, (void*)1);
668cdf0e10cSrcweir             }
669cdf0e10cSrcweir         }
670cdf0e10cSrcweir 		if (sOldTableName.Len())
671cdf0e10cSrcweir 			pListBox->SelectEntry(sOldTableName);
672cdf0e10cSrcweir 		bRet = sal_True;
673cdf0e10cSrcweir 	}
674cdf0e10cSrcweir 	return bRet;
675cdf0e10cSrcweir }
676cdf0e10cSrcweir 
677cdf0e10cSrcweir /*--------------------------------------------------------------------
678cdf0e10cSrcweir 	Beschreibung: Listbox mit Spaltennamen einer Datenbank fuellen
679cdf0e10cSrcweir  --------------------------------------------------------------------*/
GetColumnNames(ListBox * pListBox,const String & rDBName,const String & rTableName,sal_Bool bAppend)680cdf0e10cSrcweir sal_Bool SwNewDBMgr::GetColumnNames(ListBox* pListBox,
681cdf0e10cSrcweir 			const String& rDBName, const String& rTableName, sal_Bool bAppend)
682cdf0e10cSrcweir {
683cdf0e10cSrcweir 	if (!bAppend)
684cdf0e10cSrcweir 		pListBox->Clear();
685cdf0e10cSrcweir     SwDBData aData;
686cdf0e10cSrcweir     aData.sDataSource = rDBName;
687cdf0e10cSrcweir     aData.sCommand = rTableName;
688cdf0e10cSrcweir     aData.nCommandType = -1;
689cdf0e10cSrcweir     SwDSParam* pParam = FindDSData(aData, sal_False);
690cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
691cdf0e10cSrcweir     if(pParam && pParam->xConnection.is())
692cdf0e10cSrcweir         xConnection = pParam->xConnection;
693cdf0e10cSrcweir     else
694cdf0e10cSrcweir     {
695cdf0e10cSrcweir         rtl::OUString sDBName(rDBName);
696cdf0e10cSrcweir         xConnection = RegisterConnection( sDBName );
697cdf0e10cSrcweir     }
698cdf0e10cSrcweir     uno::Reference< XColumnsSupplier> xColsSupp = SwNewDBMgr::GetColumnSupplier(xConnection, rTableName);
699cdf0e10cSrcweir 	if(xColsSupp.is())
700cdf0e10cSrcweir 	{
701cdf0e10cSrcweir         uno::Reference<XNameAccess> xCols = xColsSupp->getColumns();
702cdf0e10cSrcweir         const Sequence<rtl::OUString> aColNames = xCols->getElementNames();
703cdf0e10cSrcweir         const rtl::OUString* pColNames = aColNames.getConstArray();
704cdf0e10cSrcweir 		for(int nCol = 0; nCol < aColNames.getLength(); nCol++)
705cdf0e10cSrcweir 		{
706cdf0e10cSrcweir 			pListBox->InsertEntry(pColNames[nCol]);
707cdf0e10cSrcweir 		}
708cdf0e10cSrcweir         ::comphelper::disposeComponent( xColsSupp );
709cdf0e10cSrcweir     }
710cdf0e10cSrcweir     return(sal_True);
711cdf0e10cSrcweir }
712cdf0e10cSrcweir /* -----------------------------08.06.01 15:11--------------------------------
713cdf0e10cSrcweir 
714cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetColumnNames(ListBox * pListBox,uno::Reference<XConnection> xConnection,const String & rTableName,sal_Bool bAppend)715cdf0e10cSrcweir sal_Bool SwNewDBMgr::GetColumnNames(ListBox* pListBox,
716cdf0e10cSrcweir         uno::Reference< XConnection> xConnection,
717cdf0e10cSrcweir         const String& rTableName, sal_Bool bAppend)
718cdf0e10cSrcweir {
719cdf0e10cSrcweir 	if (!bAppend)
720cdf0e10cSrcweir 		pListBox->Clear();
721cdf0e10cSrcweir     uno::Reference< XColumnsSupplier> xColsSupp = SwNewDBMgr::GetColumnSupplier(xConnection, rTableName);
722cdf0e10cSrcweir 	if(xColsSupp.is())
723cdf0e10cSrcweir 	{
724cdf0e10cSrcweir         uno::Reference<XNameAccess> xCols = xColsSupp->getColumns();
725cdf0e10cSrcweir         const Sequence<rtl::OUString> aColNames = xCols->getElementNames();
726cdf0e10cSrcweir         const rtl::OUString* pColNames = aColNames.getConstArray();
727cdf0e10cSrcweir 		for(int nCol = 0; nCol < aColNames.getLength(); nCol++)
728cdf0e10cSrcweir 		{
729cdf0e10cSrcweir 			pListBox->InsertEntry(pColNames[nCol]);
730cdf0e10cSrcweir 		}
731cdf0e10cSrcweir         ::comphelper::disposeComponent( xColsSupp );
732cdf0e10cSrcweir     }
733cdf0e10cSrcweir 	return(sal_True);
734cdf0e10cSrcweir }
735cdf0e10cSrcweir 
736cdf0e10cSrcweir /*--------------------------------------------------------------------
737cdf0e10cSrcweir 	Beschreibung: CTOR
738cdf0e10cSrcweir  --------------------------------------------------------------------*/
739cdf0e10cSrcweir 
SwNewDBMgr()740cdf0e10cSrcweir SwNewDBMgr::SwNewDBMgr() :
741cdf0e10cSrcweir             nMergeType(DBMGR_INSERT),
742cdf0e10cSrcweir             bInitDBFields(sal_False),
743cdf0e10cSrcweir             bInMerge(sal_False),
744cdf0e10cSrcweir             bMergeSilent(sal_False),
745cdf0e10cSrcweir             bMergeLock(sal_False),
746cdf0e10cSrcweir             pImpl(new SwNewDBMgr_Impl(*this)),
747cdf0e10cSrcweir             pMergeEvtSrc(NULL)
748cdf0e10cSrcweir {
749cdf0e10cSrcweir }
750cdf0e10cSrcweir /* -----------------------------18.07.00 08:56--------------------------------
751cdf0e10cSrcweir 
752cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
~SwNewDBMgr()753cdf0e10cSrcweir SwNewDBMgr::~SwNewDBMgr()
754cdf0e10cSrcweir {
755cdf0e10cSrcweir     for(sal_uInt16 nPos = 0; nPos < aDataSourceParams.Count(); nPos++)
756cdf0e10cSrcweir     {
757cdf0e10cSrcweir         SwDSParam* pParam = aDataSourceParams[nPos];
758cdf0e10cSrcweir         if(pParam->xConnection.is())
759cdf0e10cSrcweir         {
760cdf0e10cSrcweir             try
761cdf0e10cSrcweir             {
762cdf0e10cSrcweir                 uno::Reference<XComponent> xComp(pParam->xConnection, UNO_QUERY);
763cdf0e10cSrcweir                 if(xComp.is())
764cdf0e10cSrcweir                     xComp->dispose();
765cdf0e10cSrcweir             }
766cdf0e10cSrcweir             catch(const RuntimeException& )
767cdf0e10cSrcweir             {
768cdf0e10cSrcweir                 //may be disposed already since multiple entries may have used the same connection
769cdf0e10cSrcweir             }
770cdf0e10cSrcweir         }
771cdf0e10cSrcweir     }
772cdf0e10cSrcweir     delete pImpl;
773cdf0e10cSrcweir }
774cdf0e10cSrcweir 
775cdf0e10cSrcweir /*--------------------------------------------------------------------
776cdf0e10cSrcweir 	Beschreibung:	Serienbriefe als einzelne Dokumente speichern
777cdf0e10cSrcweir  --------------------------------------------------------------------*/
lcl_FindUniqueName(SwWrtShell * pTargetShell,const String & rStartingPageDesc,sal_uLong nDocNo)778cdf0e10cSrcweir String lcl_FindUniqueName(SwWrtShell* pTargetShell, const String& rStartingPageDesc, sal_uLong nDocNo )
779cdf0e10cSrcweir {
780cdf0e10cSrcweir     do
781cdf0e10cSrcweir     {
782cdf0e10cSrcweir         String sTest = rStartingPageDesc;
783cdf0e10cSrcweir         sTest += String::CreateFromInt32( nDocNo );
784cdf0e10cSrcweir         if( !pTargetShell->FindPageDescByName( sTest ) )
785cdf0e10cSrcweir             return sTest;
786cdf0e10cSrcweir         ++nDocNo;
787cdf0e10cSrcweir     }while(true);
788cdf0e10cSrcweir }
lcl_CopyDynamicDefaults(const SwDoc & rSource,SwDoc & rTarget)789cdf0e10cSrcweir void lcl_CopyDynamicDefaults( const SwDoc& rSource, SwDoc& rTarget )
790cdf0e10cSrcweir {
791cdf0e10cSrcweir     sal_uInt16 __FAR_DATA aRangeOfDefaults[] = {
792cdf0e10cSrcweir         RES_FRMATR_BEGIN, RES_FRMATR_END-1,
793cdf0e10cSrcweir         RES_CHRATR_BEGIN, RES_CHRATR_END-1,
794cdf0e10cSrcweir         RES_PARATR_BEGIN, RES_PARATR_END-1,
795cdf0e10cSrcweir         // --> OD 2008-02-25 #refactorlists##
796cdf0e10cSrcweir         RES_PARATR_LIST_BEGIN, RES_PARATR_LIST_END-1,
797cdf0e10cSrcweir         // <--
798cdf0e10cSrcweir         RES_UNKNOWNATR_BEGIN, RES_UNKNOWNATR_END-1,
799cdf0e10cSrcweir         0
800cdf0e10cSrcweir     };
801cdf0e10cSrcweir 
802cdf0e10cSrcweir     SfxItemSet aNewDefaults( rTarget.GetAttrPool(), aRangeOfDefaults );
803cdf0e10cSrcweir 
804cdf0e10cSrcweir     sal_uInt16 nWhich;
805cdf0e10cSrcweir     sal_uInt16 nRange = 0;
806cdf0e10cSrcweir     while( aRangeOfDefaults[nRange] != 0)
807cdf0e10cSrcweir     {
808cdf0e10cSrcweir         for( nWhich = aRangeOfDefaults[nRange]; nWhich < aRangeOfDefaults[nRange + 1]; ++nWhich )
809cdf0e10cSrcweir         {
810cdf0e10cSrcweir             const SfxPoolItem& rSourceAttr = rSource.GetDefault( nWhich );
811cdf0e10cSrcweir             if( rSourceAttr != rTarget.GetDefault( nWhich ) )
812cdf0e10cSrcweir                 aNewDefaults.Put( rSourceAttr );
813cdf0e10cSrcweir         }
814cdf0e10cSrcweir         nRange += 2;
815cdf0e10cSrcweir     }
816cdf0e10cSrcweir     if( aNewDefaults.Count() )
817cdf0e10cSrcweir         rTarget.SetDefault( aNewDefaults );
818cdf0e10cSrcweir }
lcl_CopyFollowPageDesc(SwWrtShell & rTargetShell,const SwPageDesc & rSourcePageDesc,const SwPageDesc & rTargetPageDesc,const sal_uLong nDocNo)819cdf0e10cSrcweir void lcl_CopyFollowPageDesc(
820cdf0e10cSrcweir                             SwWrtShell& rTargetShell,
821cdf0e10cSrcweir                             const SwPageDesc& rSourcePageDesc,
822cdf0e10cSrcweir                             const SwPageDesc& rTargetPageDesc,
823cdf0e10cSrcweir                             const sal_uLong nDocNo )
824cdf0e10cSrcweir {
825cdf0e10cSrcweir     //now copy the follow page desc, too
826cdf0e10cSrcweir     const SwPageDesc* pFollowPageDesc = rSourcePageDesc.GetFollow();
827cdf0e10cSrcweir     String sFollowPageDesc = pFollowPageDesc->GetName();
828cdf0e10cSrcweir     if( sFollowPageDesc != rSourcePageDesc.GetName() )
829cdf0e10cSrcweir     {
830cdf0e10cSrcweir         SwDoc* pTargetDoc = rTargetShell.GetDoc();
831cdf0e10cSrcweir         String sNewFollowPageDesc = lcl_FindUniqueName(&rTargetShell, sFollowPageDesc, nDocNo );
832cdf0e10cSrcweir         sal_uInt16 nNewDesc = pTargetDoc->MakePageDesc( sNewFollowPageDesc );
833cdf0e10cSrcweir         SwPageDesc& rTargetFollowPageDesc = pTargetDoc->_GetPageDesc( nNewDesc );
834cdf0e10cSrcweir 
835cdf0e10cSrcweir         pTargetDoc->CopyPageDesc( *pFollowPageDesc, rTargetFollowPageDesc, sal_False );
836cdf0e10cSrcweir         SwPageDesc aDesc( rTargetPageDesc );
837cdf0e10cSrcweir         aDesc.SetFollow( &rTargetFollowPageDesc );
838cdf0e10cSrcweir         pTargetDoc->ChgPageDesc( rTargetPageDesc.GetName(), aDesc );
839cdf0e10cSrcweir     }
840cdf0e10cSrcweir }
841cdf0e10cSrcweir 
lcl_RemoveSectionLinks(SwWrtShell & rWorkShell)842cdf0e10cSrcweir void lcl_RemoveSectionLinks( SwWrtShell& rWorkShell )
843cdf0e10cSrcweir {
844cdf0e10cSrcweir     //reset all links of the sections of synchronized labels
845cdf0e10cSrcweir     sal_uInt16 nSections = rWorkShell.GetSectionFmtCount();
846cdf0e10cSrcweir     for( sal_uInt16 nSection = 0; nSection < nSections; ++nSection )
847cdf0e10cSrcweir     {
848cdf0e10cSrcweir         SwSectionData aSectionData( *rWorkShell.GetSectionFmt( nSection ).GetSection() );
849cdf0e10cSrcweir         if( aSectionData.GetType() == FILE_LINK_SECTION )
850cdf0e10cSrcweir         {
851cdf0e10cSrcweir             aSectionData.SetType( CONTENT_SECTION );
852cdf0e10cSrcweir             aSectionData.SetLinkFileName( String() );
853cdf0e10cSrcweir             rWorkShell.UpdateSection( nSection, aSectionData );
854cdf0e10cSrcweir         }
855cdf0e10cSrcweir     }
856cdf0e10cSrcweir     rWorkShell.SetLabelDoc( sal_False );
857cdf0e10cSrcweir }
858cdf0e10cSrcweir 
MergeMailFiles(SwWrtShell * pSourceShell,const SwMergeDescriptor & rMergeDescriptor)859cdf0e10cSrcweir sal_Bool SwNewDBMgr::MergeMailFiles(SwWrtShell* pSourceShell,
860cdf0e10cSrcweir         const SwMergeDescriptor& rMergeDescriptor)
861cdf0e10cSrcweir {
862cdf0e10cSrcweir 	//check if the doc is synchronized and contains at least one linked section
863cdf0e10cSrcweir     sal_Bool bSynchronizedDoc = pSourceShell->IsLabelDoc() && pSourceShell->GetSectionFmtCount() > 1;
864cdf0e10cSrcweir 	sal_Bool bLoop = sal_True;
865cdf0e10cSrcweir     sal_Bool bEMail = rMergeDescriptor.nMergeType == DBMGR_MERGE_MAILING;
866cdf0e10cSrcweir     const bool bAsSingleFile = rMergeDescriptor.nMergeType == DBMGR_MERGE_SINGLE_FILE;
867cdf0e10cSrcweir 
868cdf0e10cSrcweir     ::rtl::Reference< MailDispatcher >          xMailDispatcher;
869cdf0e10cSrcweir     ::rtl::OUString sBodyMimeType;
870cdf0e10cSrcweir     rtl_TextEncoding eEncoding = ::gsl_getSystemTextEncoding();
871cdf0e10cSrcweir 
872cdf0e10cSrcweir     if(bEMail)
873cdf0e10cSrcweir     {
874cdf0e10cSrcweir         xMailDispatcher.set( new MailDispatcher(rMergeDescriptor.xSmtpServer));
875cdf0e10cSrcweir         if(!rMergeDescriptor.bSendAsAttachment && rMergeDescriptor.bSendAsHTML)
876cdf0e10cSrcweir         {
877cdf0e10cSrcweir             sBodyMimeType = ::rtl::OUString::createFromAscii("text/html; charset=");
878cdf0e10cSrcweir             sBodyMimeType += ::rtl::OUString::createFromAscii(
879cdf0e10cSrcweir                                 rtl_getBestMimeCharsetFromTextEncoding( eEncoding ));
880cdf0e10cSrcweir             SvxHtmlOptions* pHtmlOptions = SvxHtmlOptions::Get();
881cdf0e10cSrcweir             eEncoding = pHtmlOptions->GetTextEncoding();
882cdf0e10cSrcweir         }
883cdf0e10cSrcweir         else
884cdf0e10cSrcweir             sBodyMimeType =
885cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("text/plain; charset=UTF-8; format=flowed");
886cdf0e10cSrcweir     }
887cdf0e10cSrcweir 
888cdf0e10cSrcweir     uno::Reference< XPropertySet > xColumnProp;
889cdf0e10cSrcweir 	{
890cdf0e10cSrcweir 		sal_Bool bColumnName = sEMailAddrFld.Len() > 0;
891cdf0e10cSrcweir 
892cdf0e10cSrcweir 		if (bColumnName)
893cdf0e10cSrcweir 		{
894cdf0e10cSrcweir             uno::Reference< XColumnsSupplier > xColsSupp( pImpl->pMergeData->xResultSet, UNO_QUERY );
895cdf0e10cSrcweir             uno::Reference<XNameAccess> xCols = xColsSupp->getColumns();
896cdf0e10cSrcweir 			if(!xCols->hasByName(sEMailAddrFld))
897cdf0e10cSrcweir 				return sal_False;
898cdf0e10cSrcweir 			Any aCol = xCols->getByName(sEMailAddrFld);
899cdf0e10cSrcweir             aCol >>= xColumnProp;
900cdf0e10cSrcweir 		}
901cdf0e10cSrcweir 
902cdf0e10cSrcweir         SfxDispatcher* pSfxDispatcher = pSourceShell->GetView().GetViewFrame()->GetDispatcher();
903cdf0e10cSrcweir         SwDocShell* pSourrceDocSh = pSourceShell->GetView().GetDocShell();
904cdf0e10cSrcweir         pSfxDispatcher->Execute( pSourrceDocSh->HasName() ? SID_SAVEDOC : SID_SAVEASDOC, SFX_CALLMODE_SYNCHRON|SFX_CALLMODE_RECORD);
905cdf0e10cSrcweir         // has document been saved successfully?
906cdf0e10cSrcweir         if( !pSourrceDocSh->IsModified() )
907cdf0e10cSrcweir 		{
908cdf0e10cSrcweir             SfxMedium* pOrig = pSourceShell->GetView().GetDocShell()->GetMedium();
909cdf0e10cSrcweir             String sSourceDocumentURL(pOrig->GetURLObject().GetMainURL( INetURLObject::NO_DECODE ));
910cdf0e10cSrcweir 			const SfxFilter* pSfxFlt = SwIoSystem::GetFileFilter(
911cdf0e10cSrcweir                                                     sSourceDocumentURL, ::aEmptyStr );
912cdf0e10cSrcweir             const SfxFilter* pStoreToFilter = pSfxFlt;
913cdf0e10cSrcweir             SfxFilterContainer* pFilterContainer = SwDocShell::Factory().GetFilterContainer();
914cdf0e10cSrcweir             const String* pStoreToFilterOptions = 0;
915cdf0e10cSrcweir             // if a save_to filter is set then use it - otherwise use the default
916cdf0e10cSrcweir             if( bEMail && !rMergeDescriptor.bSendAsAttachment )
917cdf0e10cSrcweir             {
918cdf0e10cSrcweir                 String sExtension( String::CreateFromAscii(
919cdf0e10cSrcweir                         rMergeDescriptor.bSendAsHTML ? "html" : "txt" ));
920cdf0e10cSrcweir                 pStoreToFilter = pFilterContainer->GetFilter4Extension(sExtension, SFX_FILTER_EXPORT);
921cdf0e10cSrcweir             }
922cdf0e10cSrcweir             else if( rMergeDescriptor.sSaveToFilter.Len())
923cdf0e10cSrcweir             {
924cdf0e10cSrcweir                 const SfxFilter* pFilter =
925cdf0e10cSrcweir                         pFilterContainer->GetFilter4FilterName( rMergeDescriptor.sSaveToFilter );
926cdf0e10cSrcweir                 if(pFilter)
927cdf0e10cSrcweir                 {
928cdf0e10cSrcweir                     pStoreToFilter = pFilter;
929cdf0e10cSrcweir                     if(rMergeDescriptor.sSaveToFilterOptions.Len())
930cdf0e10cSrcweir                         pStoreToFilterOptions = &rMergeDescriptor.sSaveToFilterOptions;
931cdf0e10cSrcweir                 }
932cdf0e10cSrcweir             }
933cdf0e10cSrcweir 			bCancel = sal_False;
934cdf0e10cSrcweir 
935cdf0e10cSrcweir             // in case of creating a single resulting file this has to be created here
936cdf0e10cSrcweir             SwWrtShell* pTargetShell = 0;
937cdf0e10cSrcweir 
938cdf0e10cSrcweir             // the shell will be explicitly closed at the end of the method, but it is
939cdf0e10cSrcweir             // still more safe to use SfxObjectShellLock here
940cdf0e10cSrcweir             SfxObjectShellLock xTargetDocShell;
941cdf0e10cSrcweir 
942cdf0e10cSrcweir             SwView* pTargetView = 0;
943cdf0e10cSrcweir             std::auto_ptr< utl::TempFile > aTempFile;
944cdf0e10cSrcweir             String sModifiedStartingPageDesc;
945cdf0e10cSrcweir             String sStartingPageDesc;
946cdf0e10cSrcweir             sal_uInt16 nStartingPageNo = 0;
947cdf0e10cSrcweir             bool bPageStylesWithHeaderFooter = false;
948cdf0e10cSrcweir             if(bAsSingleFile || rMergeDescriptor.bCreateSingleFile)
949cdf0e10cSrcweir             {
950cdf0e10cSrcweir                 // create a target docshell to put the merged document into
951cdf0e10cSrcweir                 xTargetDocShell = new SwDocShell( SFX_CREATE_MODE_STANDARD );
952cdf0e10cSrcweir                 xTargetDocShell->DoInitNew( 0 );
953cdf0e10cSrcweir                 SfxViewFrame* pTargetFrame = SfxViewFrame::LoadHiddenDocument( *xTargetDocShell, 0 );
954cdf0e10cSrcweir 
955cdf0e10cSrcweir                 pTargetView = static_cast<SwView*>( pTargetFrame->GetViewShell() );
956cdf0e10cSrcweir 
957cdf0e10cSrcweir                 //initiate SelectShell() to create sub shells
958cdf0e10cSrcweir                 pTargetView->AttrChangedNotify( &pTargetView->GetWrtShell() );
959cdf0e10cSrcweir                 pTargetShell = pTargetView->GetWrtShellPtr();
960cdf0e10cSrcweir                 //copy the styles from the source to the target document
961cdf0e10cSrcweir                 SwgReaderOption aOpt;
962cdf0e10cSrcweir                 aOpt.SetTxtFmts( sal_True );
963cdf0e10cSrcweir                 aOpt.SetFrmFmts( sal_True );
964cdf0e10cSrcweir                 aOpt.SetPageDescs( sal_True );
965cdf0e10cSrcweir                 aOpt.SetNumRules( sal_True );
966cdf0e10cSrcweir                 aOpt.SetMerge( sal_False );
967cdf0e10cSrcweir                 pTargetView->GetDocShell()->LoadStylesFromFile(
968cdf0e10cSrcweir                         sSourceDocumentURL, aOpt, sal_True );
969cdf0e10cSrcweir                 //determine the page style and number used at the start of the source document
970cdf0e10cSrcweir                 pSourceShell->SttEndDoc(sal_True);
971cdf0e10cSrcweir                 nStartingPageNo = pSourceShell->GetVirtPageNum();
972cdf0e10cSrcweir                 sStartingPageDesc = sModifiedStartingPageDesc = pSourceShell->GetPageDesc(
973cdf0e10cSrcweir                                             pSourceShell->GetCurPageDesc()).GetName();
974cdf0e10cSrcweir                 // #122799# copy compatibility options
975cdf0e10cSrcweir                 lcl_CopyCompatibilityOptions( *pSourceShell, *pTargetShell);
976cdf0e10cSrcweir                 // #72821# copy dynamic defaults
977cdf0e10cSrcweir                 lcl_CopyDynamicDefaults( *pSourceShell->GetDoc(), *pTargetShell->GetDoc() );
978cdf0e10cSrcweir                 // #i72517#
979cdf0e10cSrcweir                 const SwPageDesc* pSourcePageDesc = pSourceShell->FindPageDescByName( sStartingPageDesc );
980cdf0e10cSrcweir                 const SwFrmFmt& rMaster = pSourcePageDesc->GetMaster();
981cdf0e10cSrcweir                 bPageStylesWithHeaderFooter = rMaster.GetHeader().IsActive()  ||
982cdf0e10cSrcweir                                                 rMaster.GetFooter().IsActive();
983cdf0e10cSrcweir 
984cdf0e10cSrcweir             }
985cdf0e10cSrcweir 
986cdf0e10cSrcweir             PrintMonitor aPrtMonDlg(&pSourceShell->GetView().GetEditWin(), PrintMonitor::MONITOR_TYPE_PRINT);
987cdf0e10cSrcweir             aPrtMonDlg.aDocName.SetText(pSourceShell->GetView().GetDocShell()->GetTitle(22));
988cdf0e10cSrcweir 
989cdf0e10cSrcweir 			aPrtMonDlg.aCancel.SetClickHdl(LINK(this, SwNewDBMgr, PrtCancelHdl));
990cdf0e10cSrcweir             if (!IsMergeSilent())
991cdf0e10cSrcweir                 aPrtMonDlg.Show();
992cdf0e10cSrcweir 
993cdf0e10cSrcweir 			// Progress, um KeyInputs zu unterbinden
994cdf0e10cSrcweir             SfxProgress aProgress(pSourrceDocSh, ::aEmptyStr, 1);
995cdf0e10cSrcweir 
996cdf0e10cSrcweir 			// Alle Dispatcher sperren
997cdf0e10cSrcweir             SfxViewFrame* pViewFrm = SfxViewFrame::GetFirst(pSourrceDocSh);
998cdf0e10cSrcweir 			while (pViewFrm)
999cdf0e10cSrcweir 			{
1000cdf0e10cSrcweir 				pViewFrm->GetDispatcher()->Lock(sal_True);
1001cdf0e10cSrcweir                 pViewFrm = SfxViewFrame::GetNext(*pViewFrm, pSourrceDocSh);
1002cdf0e10cSrcweir 			}
1003cdf0e10cSrcweir 			sal_uLong nDocNo = 1;
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir             long nStartRow, nEndRow;
1006cdf0e10cSrcweir             // collect temporary files
1007cdf0e10cSrcweir             ::std::vector< String> aFilesToRemove;
1008cdf0e10cSrcweir             do
1009cdf0e10cSrcweir             {
1010cdf0e10cSrcweir                 nStartRow = pImpl->pMergeData ? pImpl->pMergeData->xResultSet->getRow() : 0;
1011cdf0e10cSrcweir                 {
1012cdf0e10cSrcweir 					String sPath(sSubject);
1013cdf0e10cSrcweir 
1014cdf0e10cSrcweir                     String sAddress;
1015cdf0e10cSrcweir                     if( !bEMail && bColumnName )
1016cdf0e10cSrcweir 					{
1017cdf0e10cSrcweir 						SwDBFormatData aDBFormat;
1018cdf0e10cSrcweir 						aDBFormat.xFormatter = pImpl->pMergeData->xFormatter;
1019cdf0e10cSrcweir 						aDBFormat.aNullDate = pImpl->pMergeData->aNullDate;
1020cdf0e10cSrcweir 						sAddress = GetDBField( xColumnProp,	aDBFormat);
1021cdf0e10cSrcweir 						if (!sAddress.Len())
1022cdf0e10cSrcweir 							sAddress = '_';
1023cdf0e10cSrcweir 						sPath += sAddress;
1024cdf0e10cSrcweir 					}
1025cdf0e10cSrcweir 
1026cdf0e10cSrcweir                     // create a new temporary file name - only done once in case of bCreateSingleFile
1027cdf0e10cSrcweir                     if( 1 == nDocNo || (!rMergeDescriptor.bCreateSingleFile && !bAsSingleFile) )
1028cdf0e10cSrcweir                     {
1029cdf0e10cSrcweir                         INetURLObject aEntry(sPath);
1030cdf0e10cSrcweir                         String sLeading;
1031cdf0e10cSrcweir                         //#i97667# if the name is from a database field then it will be used _as is_
1032cdf0e10cSrcweir                         if( sAddress.Len() )
1033cdf0e10cSrcweir                             sLeading = sAddress;
1034cdf0e10cSrcweir                         else
1035cdf0e10cSrcweir                             sLeading = aEntry.GetBase();
1036cdf0e10cSrcweir                         aEntry.removeSegment();
1037cdf0e10cSrcweir                         sPath = aEntry.GetMainURL( INetURLObject::NO_DECODE );
1038cdf0e10cSrcweir                         String sExt( pStoreToFilter->GetDefaultExtension() );
1039cdf0e10cSrcweir                         sExt.EraseLeadingChars('*');
1040cdf0e10cSrcweir                         aTempFile = std::auto_ptr< utl::TempFile >(
1041cdf0e10cSrcweir                                 new utl::TempFile(sLeading,&sExt,&sPath ));
1042cdf0e10cSrcweir                         if( bAsSingleFile )
1043cdf0e10cSrcweir                             aTempFile->EnableKillingFile();
1044cdf0e10cSrcweir                     }
1045cdf0e10cSrcweir 
1046cdf0e10cSrcweir                     if( !aTempFile->IsValid() )
1047cdf0e10cSrcweir 					{
1048cdf0e10cSrcweir 						ErrorHandler::HandleError( ERRCODE_IO_NOTSUPPORTED );
1049cdf0e10cSrcweir 						bLoop = sal_False;
1050cdf0e10cSrcweir 						bCancel = sal_True;
1051cdf0e10cSrcweir 					}
1052cdf0e10cSrcweir 					else
1053cdf0e10cSrcweir 					{
1054cdf0e10cSrcweir                         INetURLObject aTempFileURL(aTempFile->GetURL());
1055cdf0e10cSrcweir                         aPrtMonDlg.aPrinter.SetText( aTempFileURL.GetBase() );
1056cdf0e10cSrcweir 						String sStat(SW_RES(STR_STATSTR_LETTER));	// Brief
1057cdf0e10cSrcweir 						sStat += ' ';
1058cdf0e10cSrcweir 						sStat += String::CreateFromInt32( nDocNo );
1059cdf0e10cSrcweir 						aPrtMonDlg.aPrintInfo.SetText(sStat);
1060cdf0e10cSrcweir 
1061cdf0e10cSrcweir 						// Rechenzeit fuer Save-Monitor:
1062cdf0e10cSrcweir 						for (sal_uInt16 i = 0; i < 10; i++)
1063cdf0e10cSrcweir 							Application::Reschedule();
1064cdf0e10cSrcweir 
1065cdf0e10cSrcweir 						// Create and save new document
1066cdf0e10cSrcweir                         // The SfxObjectShell will be closed explicitly later but it is more safe to use SfxObjectShellLock here
1067cdf0e10cSrcweir                         SfxObjectShellLock xWorkDocSh( new SwDocShell( SFX_CREATE_MODE_INTERNAL ));
1068cdf0e10cSrcweir                         SfxMedium* pWorkMed = new SfxMedium( sSourceDocumentURL, STREAM_STD_READ, sal_True );
1069cdf0e10cSrcweir                         pWorkMed->SetFilter( pSfxFlt );
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir                         if (xWorkDocSh->DoLoad(pWorkMed))
1072cdf0e10cSrcweir 						{
1073cdf0e10cSrcweir                             //create a view frame for the document
1074cdf0e10cSrcweir                             SfxViewFrame* pWorkFrame = SfxViewFrame::LoadHiddenDocument( *xWorkDocSh, 0 );
1075cdf0e10cSrcweir                             //request the layout calculation
1076cdf0e10cSrcweir                             SwWrtShell& rWorkShell =
1077cdf0e10cSrcweir                                     static_cast< SwView* >(pWorkFrame->GetViewShell())->GetWrtShell();
1078cdf0e10cSrcweir                             rWorkShell.CalcLayout();
1079cdf0e10cSrcweir                             SwDoc* pWorkDoc = ((SwDocShell*)(&xWorkDocSh))->GetDoc();
1080cdf0e10cSrcweir                             SwNewDBMgr* pOldDBMgr = pWorkDoc->GetNewDBMgr();
1081cdf0e10cSrcweir                             pWorkDoc->SetNewDBMgr( this );
1082cdf0e10cSrcweir                             SFX_APP()->NotifyEvent(SfxEventHint(SW_EVENT_FIELD_MERGE, SwDocShell::GetEventName(STR_SW_EVENT_FIELD_MERGE), xWorkDocSh));
1083cdf0e10cSrcweir                             pWorkDoc->UpdateFlds(NULL, false);
1084cdf0e10cSrcweir                             SFX_APP()->NotifyEvent(SfxEventHint(SW_EVENT_FIELD_MERGE_FINISHED, SwDocShell::GetEventName(STR_SW_EVENT_FIELD_MERGE_FINISHED), xWorkDocSh));
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir 							// alle versteckten Felder/Bereiche entfernen
1087cdf0e10cSrcweir                             pWorkDoc->RemoveInvisibleContent();
1088cdf0e10cSrcweir 
1089cdf0e10cSrcweir                             // launch MailMergeEvent if required
1090cdf0e10cSrcweir                             const SwXMailMerge *pEvtSrc = GetMailMergeEvtSrc();
1091cdf0e10cSrcweir                             if(pEvtSrc)
1092cdf0e10cSrcweir                             {
1093cdf0e10cSrcweir                                 uno::Reference< XInterface > xRef( (XMailMergeBroadcaster *) pEvtSrc );
1094cdf0e10cSrcweir                                 text::MailMergeEvent aEvt( xRef, xWorkDocSh->GetModel() );
1095cdf0e10cSrcweir                                 pEvtSrc->LaunchMailMergeEvent( aEvt );
1096cdf0e10cSrcweir                             }
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir                             if(rMergeDescriptor.bCreateSingleFile || bAsSingleFile )
1099cdf0e10cSrcweir                             {
1100cdf0e10cSrcweir                                 DBG_ASSERT( pTargetShell, "no target shell available!" );
1101cdf0e10cSrcweir                                 // copy created file into the target document
1102cdf0e10cSrcweir                                 rWorkShell.ConvertFieldsToText();
1103cdf0e10cSrcweir                                 rWorkShell.SetNumberingRestart();
1104cdf0e10cSrcweir                                 if( bSynchronizedDoc )
1105cdf0e10cSrcweir                                 {
1106cdf0e10cSrcweir                                     lcl_RemoveSectionLinks( rWorkShell );
1107cdf0e10cSrcweir                                 }
1108cdf0e10cSrcweir 
1109cdf0e10cSrcweir                                 // insert the document into the target document
1110cdf0e10cSrcweir                                 rWorkShell.SttEndDoc(sal_False);
1111cdf0e10cSrcweir                                 rWorkShell.SttEndDoc(sal_True);
1112cdf0e10cSrcweir                                 rWorkShell.SelAll();
1113cdf0e10cSrcweir                                 pTargetShell->SwCrsrShell::SttEndDoc( sal_False );
1114cdf0e10cSrcweir                                 //#i72517# the headers and footers are still those from the source - update in case of fields inside header/footer
1115cdf0e10cSrcweir                                 if( !nDocNo && bPageStylesWithHeaderFooter )
1116cdf0e10cSrcweir                                     pTargetShell->GetView().GetDocShell()->_LoadStyles( *rWorkShell.GetView().GetDocShell(), sal_True );
1117cdf0e10cSrcweir                                 //#i72517# put the styles to the target document
1118cdf0e10cSrcweir                                 //if the source uses headers or footers each new copy need to copy a new page styles
1119cdf0e10cSrcweir                                 if(bPageStylesWithHeaderFooter)
1120cdf0e10cSrcweir                                 {
1121cdf0e10cSrcweir                                     //create a new pagestyle
1122cdf0e10cSrcweir                                     //copy the pagedesc from the current document to the new document and change the name of the to-be-applied style
1123cdf0e10cSrcweir 
1124cdf0e10cSrcweir                                     SwDoc* pTargetDoc = pTargetShell->GetDoc();
1125cdf0e10cSrcweir                                     SwPageDesc* pSourcePageDesc = rWorkShell.FindPageDescByName( sStartingPageDesc );
1126cdf0e10cSrcweir                                     String sNewPageDescName = lcl_FindUniqueName(pTargetShell, sStartingPageDesc, nDocNo );
1127cdf0e10cSrcweir                                     pTargetDoc->MakePageDesc( sNewPageDescName );
1128cdf0e10cSrcweir                                     SwPageDesc* pTargetPageDesc = pTargetShell->FindPageDescByName( sNewPageDescName );
1129cdf0e10cSrcweir                                     if(pSourcePageDesc && pTargetPageDesc)
1130cdf0e10cSrcweir                                     {
1131cdf0e10cSrcweir                                         pTargetDoc->CopyPageDesc( *pSourcePageDesc, *pTargetPageDesc, sal_False );
1132cdf0e10cSrcweir                                         sModifiedStartingPageDesc = sNewPageDescName;
1133cdf0e10cSrcweir                                         lcl_CopyFollowPageDesc( *pTargetShell, *pSourcePageDesc, *pTargetPageDesc, nDocNo );
1134cdf0e10cSrcweir                                     }
1135cdf0e10cSrcweir                                 }
1136cdf0e10cSrcweir 
1137cdf0e10cSrcweir                                 if(nDocNo > 1)
1138cdf0e10cSrcweir                                     pTargetShell->InsertPageBreak( &sModifiedStartingPageDesc, nStartingPageNo );
1139cdf0e10cSrcweir                                 else
1140cdf0e10cSrcweir                                     pTargetShell->SetPageStyle(sModifiedStartingPageDesc);
1141cdf0e10cSrcweir                                 DBG_ASSERT(!pTargetShell->GetTableFmt(),"target document ends with a table - paragraph should be appended");
1142cdf0e10cSrcweir                                 //#i51359# add a second paragraph in case there's only one
1143cdf0e10cSrcweir                                 {
1144cdf0e10cSrcweir                                     SwNodeIndex aIdx( pWorkDoc->GetNodes().GetEndOfExtras(), 2 );
1145cdf0e10cSrcweir                                     SwPosition aTestPos( aIdx );
1146cdf0e10cSrcweir                                     SwCursor aTestCrsr(aTestPos,0,false);
1147cdf0e10cSrcweir                                     if(!aTestCrsr.MovePara(fnParaNext, fnParaStart))
1148cdf0e10cSrcweir                                     {
1149cdf0e10cSrcweir                                         //append a paragraph
1150cdf0e10cSrcweir                                         pWorkDoc->AppendTxtNode( aTestPos );
1151cdf0e10cSrcweir                                     }
1152cdf0e10cSrcweir                                 }
1153cdf0e10cSrcweir                                 pTargetShell->Paste( rWorkShell.GetDoc(), sal_True );
1154cdf0e10cSrcweir 
1155cdf0e10cSrcweir                                 //convert fields in page styles (header/footer - has to be done after the first document has been pasted
1156cdf0e10cSrcweir                                 if(1 == nDocNo)
1157cdf0e10cSrcweir                                 {
1158cdf0e10cSrcweir                                     pTargetShell->CalcLayout();
1159cdf0e10cSrcweir                                     pTargetShell->ConvertFieldsToText();
1160cdf0e10cSrcweir                                 }
1161cdf0e10cSrcweir                             }
1162cdf0e10cSrcweir                             else
1163cdf0e10cSrcweir                             {
1164cdf0e10cSrcweir                                 String sFileURL =  aTempFileURL.GetMainURL( INetURLObject::NO_DECODE );
1165cdf0e10cSrcweir                                 SfxMedium* pDstMed = new SfxMedium(
1166cdf0e10cSrcweir                                     sFileURL,
1167cdf0e10cSrcweir                                     STREAM_STD_READWRITE, sal_True );
1168cdf0e10cSrcweir                                 pDstMed->SetFilter( pStoreToFilter );
1169cdf0e10cSrcweir                                 if(pDstMed->GetItemSet())
1170cdf0e10cSrcweir                                 {
1171cdf0e10cSrcweir                                     if(pStoreToFilterOptions )
1172cdf0e10cSrcweir                                         pDstMed->GetItemSet()->Put(SfxStringItem(SID_FILE_FILTEROPTIONS, *pStoreToFilterOptions));
1173cdf0e10cSrcweir                                     if(rMergeDescriptor.aSaveToFilterData.getLength())
1174cdf0e10cSrcweir                                         pDstMed->GetItemSet()->Put(SfxUsrAnyItem(SID_FILTER_DATA, makeAny(rMergeDescriptor.aSaveToFilterData)));
1175cdf0e10cSrcweir                                 }
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir                                 //convert fields to text if we are exporting to PDF
1178cdf0e10cSrcweir                                 //this prevents a second merge while updating the fields in SwXTextDocument::getRendererCount()
1179cdf0e10cSrcweir                                 if( pStoreToFilter && pStoreToFilter->GetFilterName().EqualsAscii("writer_pdf_Export"))
1180cdf0e10cSrcweir                                     rWorkShell.ConvertFieldsToText();
1181cdf0e10cSrcweir                                 xWorkDocSh->DoSaveAs(*pDstMed);
1182cdf0e10cSrcweir                                 xWorkDocSh->DoSaveCompleted(pDstMed);
1183cdf0e10cSrcweir                                 if( xWorkDocSh->GetError() )
1184cdf0e10cSrcweir                                 {
1185cdf0e10cSrcweir                                     // error message ??
1186cdf0e10cSrcweir                                     ErrorHandler::HandleError( xWorkDocSh->GetError() );
1187cdf0e10cSrcweir                                     bCancel = sal_True;
1188cdf0e10cSrcweir                                     bLoop = sal_False;
1189cdf0e10cSrcweir                                 }
1190cdf0e10cSrcweir                                 if( bEMail )
1191cdf0e10cSrcweir                                 {
1192cdf0e10cSrcweir                                     SwDBFormatData aDBFormat;
1193cdf0e10cSrcweir                                     aDBFormat.xFormatter = pImpl->pMergeData->xFormatter;
1194cdf0e10cSrcweir                                     aDBFormat.aNullDate = pImpl->pMergeData->aNullDate;
1195cdf0e10cSrcweir                                     String sMailAddress = GetDBField( xColumnProp, aDBFormat);
1196cdf0e10cSrcweir                                     if(!SwMailMergeHelper::CheckMailAddress( sMailAddress ))
1197cdf0e10cSrcweir                                     {
1198cdf0e10cSrcweir                                         DBG_ERROR("invalid e-Mail address in database column");
1199cdf0e10cSrcweir                                     }
1200cdf0e10cSrcweir                                     else
1201cdf0e10cSrcweir                                     {
1202cdf0e10cSrcweir                                         SwMailMessage* pMessage = 0;
1203cdf0e10cSrcweir                                         uno::Reference< mail::XMailMessage > xMessage =
1204cdf0e10cSrcweir                                                                 pMessage = new SwMailMessage;
1205cdf0e10cSrcweir                                         if(rMergeDescriptor.pMailMergeConfigItem->IsMailReplyTo())
1206cdf0e10cSrcweir                                             pMessage->setReplyToAddress(rMergeDescriptor.pMailMergeConfigItem->GetMailReplyTo());
1207cdf0e10cSrcweir                                         pMessage->addRecipient( sMailAddress );
1208cdf0e10cSrcweir                                         pMessage->SetSenderAddress( rMergeDescriptor.pMailMergeConfigItem->GetMailAddress() );
1209cdf0e10cSrcweir                                         ::rtl::OUString sBody;
1210cdf0e10cSrcweir                                         if(rMergeDescriptor.bSendAsAttachment)
1211cdf0e10cSrcweir                                         {
1212cdf0e10cSrcweir                                             sBody = rMergeDescriptor.sMailBody;
1213cdf0e10cSrcweir                                             mail::MailAttachment aAttach;
1214cdf0e10cSrcweir                                             aAttach.Data = new SwMailTransferable(
1215cdf0e10cSrcweir                                                     sFileURL,
1216cdf0e10cSrcweir                                                     rMergeDescriptor.sAttachmentName,
1217cdf0e10cSrcweir                                                     pStoreToFilter->GetMimeType());
1218cdf0e10cSrcweir                                             aAttach.ReadableName = rMergeDescriptor.sAttachmentName;
1219cdf0e10cSrcweir                                             pMessage->addAttachment( aAttach );
1220cdf0e10cSrcweir                                         }
1221cdf0e10cSrcweir                                         else
1222cdf0e10cSrcweir                                         {
1223cdf0e10cSrcweir                                             {
1224cdf0e10cSrcweir                                                 //read in the temporary file and use it as mail body
1225cdf0e10cSrcweir                                                 SfxMedium aMedium( sFileURL, STREAM_READ, sal_True);
1226cdf0e10cSrcweir                                                 SvStream* pInStream = aMedium.GetInStream();
1227cdf0e10cSrcweir                                                 DBG_ASSERT(pInStream, "no output file created?");
1228cdf0e10cSrcweir                                                 if(pInStream)
1229cdf0e10cSrcweir                                                 {
1230cdf0e10cSrcweir                                                     pInStream->SetStreamCharSet( eEncoding );
1231cdf0e10cSrcweir                                                     ByteString sLine;
1232cdf0e10cSrcweir                                                     sal_Bool bDone = pInStream->ReadLine( sLine );
1233cdf0e10cSrcweir                                                     while ( bDone )
1234cdf0e10cSrcweir                                                     {
1235cdf0e10cSrcweir                                                         sBody += String(sLine, eEncoding);
1236cdf0e10cSrcweir                                                         sBody += ::rtl::OUString('\n');
1237cdf0e10cSrcweir                                                         bDone = pInStream->ReadLine( sLine );
1238cdf0e10cSrcweir                                                     }
1239cdf0e10cSrcweir                                                 }
1240cdf0e10cSrcweir                                             }
1241cdf0e10cSrcweir                                         }
1242cdf0e10cSrcweir                                         pMessage->setSubject( rMergeDescriptor.sSubject );
1243cdf0e10cSrcweir                                         uno::Reference< datatransfer::XTransferable> xBody =
1244cdf0e10cSrcweir                                                     new SwMailTransferable(
1245cdf0e10cSrcweir                                                         sBody,
1246cdf0e10cSrcweir                                                         sBodyMimeType);
1247cdf0e10cSrcweir                                         pMessage->setBody( xBody );
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir                                         if(rMergeDescriptor.aCopiesTo.getLength())
1250cdf0e10cSrcweir                                         {
1251cdf0e10cSrcweir                                             const ::rtl::OUString* pCopies = rMergeDescriptor.aCopiesTo.getConstArray();
1252cdf0e10cSrcweir                                             for( sal_Int32 nToken = 0; nToken < rMergeDescriptor.aCopiesTo.getLength(); ++nToken)
1253cdf0e10cSrcweir                                                 pMessage->addCcRecipient( pCopies[nToken] );
1254cdf0e10cSrcweir                                         }
1255cdf0e10cSrcweir                                         if(rMergeDescriptor.aBlindCopiesTo.getLength())
1256cdf0e10cSrcweir                                         {
1257cdf0e10cSrcweir                                             const ::rtl::OUString* pCopies = rMergeDescriptor.aBlindCopiesTo.getConstArray();
1258cdf0e10cSrcweir                                             for( sal_Int32 nToken = 0; nToken < rMergeDescriptor.aBlindCopiesTo.getLength(); ++nToken)
1259cdf0e10cSrcweir                                                 pMessage->addBccRecipient( pCopies[nToken] );
1260cdf0e10cSrcweir                                         }
1261cdf0e10cSrcweir                                         xMailDispatcher->enqueueMailMessage( xMessage );
1262cdf0e10cSrcweir                                         if(!xMailDispatcher->isStarted())
1263cdf0e10cSrcweir                                                 xMailDispatcher->start();
1264cdf0e10cSrcweir                                         //schedule for removal
1265cdf0e10cSrcweir                                         aFilesToRemove.push_back(sFileURL);
1266cdf0e10cSrcweir                                     }
1267cdf0e10cSrcweir                                 }
1268cdf0e10cSrcweir                             }
1269cdf0e10cSrcweir                             pWorkDoc->SetNewDBMgr( pOldDBMgr );
1270cdf0e10cSrcweir 						}
1271cdf0e10cSrcweir                         xWorkDocSh->DoClose();
1272cdf0e10cSrcweir 					}
1273cdf0e10cSrcweir 				}
1274cdf0e10cSrcweir                 nDocNo++;
1275cdf0e10cSrcweir                 nEndRow = pImpl->pMergeData ? pImpl->pMergeData->xResultSet->getRow() : 0;
1276cdf0e10cSrcweir             } while( !bCancel &&
1277cdf0e10cSrcweir                 (bSynchronizedDoc && (nStartRow != nEndRow)? ExistsNextRecord() : ToNextMergeRecord()));
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir             aPrtMonDlg.Show( sal_False );
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir             // save the single output document
1282cdf0e10cSrcweir             if(rMergeDescriptor.bCreateSingleFile || bAsSingleFile)
1283cdf0e10cSrcweir             {
1284cdf0e10cSrcweir                 if( rMergeDescriptor.nMergeType != DBMGR_MERGE_MAILMERGE )
1285cdf0e10cSrcweir                 {
1286cdf0e10cSrcweir                     DBG_ASSERT( aTempFile.get(), "Temporary file not available" );
1287cdf0e10cSrcweir                     INetURLObject aTempFileURL(bAsSingleFile ? sSubject : aTempFile->GetURL());
1288cdf0e10cSrcweir                     SfxMedium* pDstMed = new SfxMedium(
1289cdf0e10cSrcweir                         aTempFileURL.GetMainURL( INetURLObject::NO_DECODE ),
1290cdf0e10cSrcweir                         STREAM_STD_READWRITE, sal_True );
1291cdf0e10cSrcweir                     pDstMed->SetFilter( pStoreToFilter );
1292cdf0e10cSrcweir                     if(pDstMed->GetItemSet())
1293cdf0e10cSrcweir                     {
1294cdf0e10cSrcweir                         if(pStoreToFilterOptions )
1295cdf0e10cSrcweir                             pDstMed->GetItemSet()->Put(SfxStringItem(SID_FILE_FILTEROPTIONS, *pStoreToFilterOptions));
1296cdf0e10cSrcweir                         if(rMergeDescriptor.aSaveToFilterData.getLength())
1297cdf0e10cSrcweir                             pDstMed->GetItemSet()->Put(SfxUsrAnyItem(SID_FILTER_DATA, makeAny(rMergeDescriptor.aSaveToFilterData)));
1298cdf0e10cSrcweir                     }
1299cdf0e10cSrcweir 
1300cdf0e10cSrcweir                     xTargetDocShell->DoSaveAs(*pDstMed);
1301cdf0e10cSrcweir                     xTargetDocShell->DoSaveCompleted(pDstMed);
1302cdf0e10cSrcweir                     if( xTargetDocShell->GetError() )
1303cdf0e10cSrcweir                     {
1304cdf0e10cSrcweir                         // error message ??
1305cdf0e10cSrcweir                         ErrorHandler::HandleError( xTargetDocShell->GetError() );
1306cdf0e10cSrcweir                         bLoop = sal_False;
1307cdf0e10cSrcweir                     }
1308cdf0e10cSrcweir                 }
1309cdf0e10cSrcweir                 else if( pTargetView ) // must be available!
1310cdf0e10cSrcweir                 {
1311cdf0e10cSrcweir                     //print the target document
1312cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
1313cdf0e10cSrcweir                     sal_Bool  _bVal;
1314cdf0e10cSrcweir                     sal_Int16 _nVal;
1315cdf0e10cSrcweir                     rtl::OUString  _sVal;
1316cdf0e10cSrcweir                     const beans::PropertyValue* pDbgPrintOptions = rMergeDescriptor.aPrintOptions.getConstArray();
1317cdf0e10cSrcweir                     for( sal_Int32 nOption = 0; nOption < rMergeDescriptor.aPrintOptions.getLength(); ++nOption)
1318cdf0e10cSrcweir                     {
1319cdf0e10cSrcweir                         rtl::OUString aName( pDbgPrintOptions[nOption].Name );
1320cdf0e10cSrcweir                         uno::Any aVal( pDbgPrintOptions[nOption].Value );
1321cdf0e10cSrcweir                         aVal >>= _bVal;
1322cdf0e10cSrcweir                         aVal >>= _nVal;
1323cdf0e10cSrcweir                         aVal >>= _sVal;
1324cdf0e10cSrcweir                     }
1325cdf0e10cSrcweir         #endif
1326cdf0e10cSrcweir                     // printing should be done synchronously otherwise the document
1327cdf0e10cSrcweir                     // might already become invalid during the process
1328cdf0e10cSrcweir                     uno::Sequence< beans::PropertyValue > aOptions( rMergeDescriptor.aPrintOptions );
1329cdf0e10cSrcweir                     const sal_Int32 nOpts = aOptions.getLength();
1330cdf0e10cSrcweir                     aOptions.realloc( nOpts + 1 );
1331cdf0e10cSrcweir                     aOptions[ nOpts ].Name = rtl::OUString::createFromAscii( "Wait" );
1332cdf0e10cSrcweir                     aOptions[ nOpts ].Value <<= sal_True ;
1333cdf0e10cSrcweir //                    aPrintArgs.Put(SfxBoolItem(FN_QRY_MERGE, sal_True) );
1334cdf0e10cSrcweir //                    // #i52629# aynchronous printing should only be done in silent mode - otherwise
1335cdf0e10cSrcweir //                    // the printer dialog does not come up
1336cdf0e10cSrcweir //                    aPrintArgs.Put( SfxBoolItem( SID_ASYNCHRON, rMergeDescriptor.bPrintAsync ));
1337cdf0e10cSrcweir                     // move print options
1338cdf0e10cSrcweir                     const beans::PropertyValue* pPrintOptions = rMergeDescriptor.aPrintOptions.getConstArray();
1339cdf0e10cSrcweir                     for( sal_Int32 nOption = 0; nOption < rMergeDescriptor.aPrintOptions.getLength(); ++nOption)
1340cdf0e10cSrcweir                     {
1341cdf0e10cSrcweir                         if( pPrintOptions[nOption].Name.equalsAscii( "CopyCount" )
1342cdf0e10cSrcweir                             ||( pPrintOptions[nOption].Name.equalsAscii( "FileName" ))
1343cdf0e10cSrcweir                             ||( pPrintOptions[nOption].Name.equalsAscii( "Collate" ))
1344cdf0e10cSrcweir                             ||( pPrintOptions[nOption].Name.equalsAscii( "Pages" ))
1345cdf0e10cSrcweir                             ||( pPrintOptions[nOption].Name.equalsAscii( "Wait" )))
1346cdf0e10cSrcweir                         {
1347cdf0e10cSrcweir                             aOptions.realloc( nOpts + 1 );
1348cdf0e10cSrcweir                             aOptions[ nOpts ].Name = pPrintOptions[nOption].Name;
1349cdf0e10cSrcweir                             aOptions[ nOpts ].Value = pPrintOptions[nOption].Value ;
1350cdf0e10cSrcweir                         }
1351cdf0e10cSrcweir                     }
1352cdf0e10cSrcweir 
1353cdf0e10cSrcweir //                    const SwModuleOptions * pModOpt = SW_MOD()->GetModuleConfig();
1354cdf0e10cSrcweir //                    if (pModOpt->IsSinglePrintJob())
1355cdf0e10cSrcweir //                    {
1356cdf0e10cSrcweir //                    }
1357cdf0e10cSrcweir //                    else
1358cdf0e10cSrcweir //                    {
1359cdf0e10cSrcweir                         pTargetView->ExecPrint( aOptions, IsMergeSilent(), rMergeDescriptor.bPrintAsync );
1360cdf0e10cSrcweir //                    }
1361cdf0e10cSrcweir                 }
1362cdf0e10cSrcweir                 xTargetDocShell->DoClose();
1363cdf0e10cSrcweir             }
1364cdf0e10cSrcweir 
1365cdf0e10cSrcweir             //remove the temporary files
1366cdf0e10cSrcweir             ::std::vector<String>::iterator aFileIter;
1367cdf0e10cSrcweir             for(aFileIter = aFilesToRemove.begin();
1368cdf0e10cSrcweir                         aFileIter != aFilesToRemove.end(); aFileIter++)
1369cdf0e10cSrcweir                 SWUnoHelper::UCB_DeleteFile( *aFileIter );
1370cdf0e10cSrcweir 
1371cdf0e10cSrcweir             // Alle Dispatcher freigeben
1372cdf0e10cSrcweir             pViewFrm = SfxViewFrame::GetFirst(pSourrceDocSh);
1373cdf0e10cSrcweir 			while (pViewFrm)
1374cdf0e10cSrcweir 			{
1375cdf0e10cSrcweir 				pViewFrm->GetDispatcher()->Lock(sal_False);
1376cdf0e10cSrcweir                 pViewFrm = SfxViewFrame::GetNext(*pViewFrm, pSourrceDocSh);
1377cdf0e10cSrcweir 			}
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir             SW_MOD()->SetView(&pSourceShell->GetView());
1380cdf0e10cSrcweir 		}
1381cdf0e10cSrcweir 
1382cdf0e10cSrcweir 		nMergeType = DBMGR_INSERT;
1383cdf0e10cSrcweir 	}
1384cdf0e10cSrcweir 
1385cdf0e10cSrcweir     if(bEMail)
1386cdf0e10cSrcweir     {
1387cdf0e10cSrcweir         xMailDispatcher->stop();
1388cdf0e10cSrcweir         xMailDispatcher->shutdown();
1389cdf0e10cSrcweir 
1390cdf0e10cSrcweir     }
1391cdf0e10cSrcweir 
1392cdf0e10cSrcweir     return bLoop;
1393cdf0e10cSrcweir }
1394cdf0e10cSrcweir 
1395cdf0e10cSrcweir /*--------------------------------------------------------------------
1396cdf0e10cSrcweir 	Beschreibung:
1397cdf0e10cSrcweir   --------------------------------------------------------------------*/
1398cdf0e10cSrcweir 
IMPL_LINK_INLINE_START(SwNewDBMgr,PrtCancelHdl,Button *,pButton)1399cdf0e10cSrcweir IMPL_LINK_INLINE_START( SwNewDBMgr, PrtCancelHdl, Button *, pButton )
1400cdf0e10cSrcweir {
1401cdf0e10cSrcweir 	pButton->GetParent()->Hide();
1402cdf0e10cSrcweir 	bCancel = sal_True;
1403cdf0e10cSrcweir 	return 0;
1404cdf0e10cSrcweir }
IMPL_LINK_INLINE_END(SwNewDBMgr,PrtCancelHdl,Button *,pButton)1405cdf0e10cSrcweir IMPL_LINK_INLINE_END( SwNewDBMgr, PrtCancelHdl, Button *, pButton )
1406cdf0e10cSrcweir 
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir /*--------------------------------------------------------------------
1409cdf0e10cSrcweir 	Beschreibung: Numberformat der Spalte ermitteln und ggfs. in
1410cdf0e10cSrcweir 					den uebergebenen Formatter uebertragen
1411cdf0e10cSrcweir   --------------------------------------------------------------------*/
1412cdf0e10cSrcweir 
1413cdf0e10cSrcweir sal_uLong SwNewDBMgr::GetColumnFmt( const String& rDBName,
1414cdf0e10cSrcweir 								const String& rTableName,
1415cdf0e10cSrcweir 								const String& rColNm,
1416cdf0e10cSrcweir 								SvNumberFormatter* pNFmtr,
1417cdf0e10cSrcweir 								long nLanguage )
1418cdf0e10cSrcweir {
1419cdf0e10cSrcweir 	sal_uLong nRet = 0;
1420cdf0e10cSrcweir 	if(pNFmtr)
1421cdf0e10cSrcweir 	{
1422cdf0e10cSrcweir         uno::Reference< XDataSource> xSource;
1423cdf0e10cSrcweir         uno::Reference< XConnection> xConnection;
1424cdf0e10cSrcweir         sal_Bool bUseMergeData = sal_False;
1425cdf0e10cSrcweir         uno::Reference< XColumnsSupplier> xColsSupp;
1426cdf0e10cSrcweir         bool bDisposeConnection = false;
1427cdf0e10cSrcweir         if(pImpl->pMergeData &&
1428cdf0e10cSrcweir             pImpl->pMergeData->sDataSource.equals(rDBName) && pImpl->pMergeData->sCommand.equals(rTableName))
1429cdf0e10cSrcweir 		{
1430cdf0e10cSrcweir             xConnection = pImpl->pMergeData->xConnection;
1431cdf0e10cSrcweir             xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,rDBName);
1432cdf0e10cSrcweir             bUseMergeData = sal_True;
1433cdf0e10cSrcweir             xColsSupp = xColsSupp.query( pImpl->pMergeData->xResultSet );
1434cdf0e10cSrcweir 		}
1435cdf0e10cSrcweir         if(!xConnection.is())
1436cdf0e10cSrcweir         {
1437cdf0e10cSrcweir             SwDBData aData;
1438cdf0e10cSrcweir             aData.sDataSource = rDBName;
1439cdf0e10cSrcweir             aData.sCommand = rTableName;
1440cdf0e10cSrcweir             aData.nCommandType = -1;
1441cdf0e10cSrcweir             SwDSParam* pParam = FindDSData(aData, sal_False);
1442cdf0e10cSrcweir             if(pParam && pParam->xConnection.is())
1443cdf0e10cSrcweir             {
1444cdf0e10cSrcweir                 xConnection = pParam->xConnection;
1445cdf0e10cSrcweir                 xColsSupp = xColsSupp.query( pParam->xResultSet );
1446cdf0e10cSrcweir             }
1447cdf0e10cSrcweir             else
1448cdf0e10cSrcweir             {
1449cdf0e10cSrcweir                 rtl::OUString sDBName(rDBName);
1450cdf0e10cSrcweir                 xConnection = RegisterConnection( sDBName );
1451cdf0e10cSrcweir                 bDisposeConnection = true;
1452cdf0e10cSrcweir             }
1453cdf0e10cSrcweir             if(bUseMergeData)
1454cdf0e10cSrcweir                 pImpl->pMergeData->xConnection = xConnection;
1455cdf0e10cSrcweir         }
1456cdf0e10cSrcweir         bool bDispose = !xColsSupp.is();
1457cdf0e10cSrcweir         if(bDispose)
1458cdf0e10cSrcweir         {
1459cdf0e10cSrcweir             xColsSupp = SwNewDBMgr::GetColumnSupplier(xConnection, rTableName);
1460cdf0e10cSrcweir         }
1461cdf0e10cSrcweir 		if(xColsSupp.is())
1462cdf0e10cSrcweir 		{
1463cdf0e10cSrcweir             uno::Reference<XNameAccess> xCols;
1464cdf0e10cSrcweir             try
1465cdf0e10cSrcweir             {
1466cdf0e10cSrcweir                 xCols = xColsSupp->getColumns();
1467cdf0e10cSrcweir             }
1468cdf0e10cSrcweir             catch(Exception&)
1469cdf0e10cSrcweir             {
1470cdf0e10cSrcweir                 DBG_ERROR("Exception in getColumns()");
1471cdf0e10cSrcweir             }
1472cdf0e10cSrcweir             if(!xCols.is() || !xCols->hasByName(rColNm))
1473cdf0e10cSrcweir 				return nRet;
1474cdf0e10cSrcweir 			Any aCol = xCols->getByName(rColNm);
1475cdf0e10cSrcweir             uno::Reference< XPropertySet > xColumn;
1476cdf0e10cSrcweir             aCol >>= xColumn;
1477cdf0e10cSrcweir             nRet = GetColumnFmt(xSource, xConnection, xColumn, pNFmtr, nLanguage);
1478cdf0e10cSrcweir             if(bDispose)
1479cdf0e10cSrcweir             {
1480cdf0e10cSrcweir                 ::comphelper::disposeComponent( xColsSupp );
1481cdf0e10cSrcweir             }
1482cdf0e10cSrcweir             if(bDisposeConnection)
1483cdf0e10cSrcweir             {
1484cdf0e10cSrcweir                 ::comphelper::disposeComponent( xConnection );
1485cdf0e10cSrcweir             }
1486cdf0e10cSrcweir         }
1487cdf0e10cSrcweir         else
1488cdf0e10cSrcweir 			nRet = pNFmtr->GetFormatIndex( NF_NUMBER_STANDARD, LANGUAGE_SYSTEM );
1489cdf0e10cSrcweir 	}
1490cdf0e10cSrcweir 	return nRet;
1491cdf0e10cSrcweir }
1492cdf0e10cSrcweir /* -----------------------------07.06.01 15:43--------------------------------
1493cdf0e10cSrcweir 
1494cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetColumnFmt(uno::Reference<XDataSource> xSource,uno::Reference<XConnection> xConnection,uno::Reference<XPropertySet> xColumn,SvNumberFormatter * pNFmtr,long nLanguage)1495cdf0e10cSrcweir sal_uLong SwNewDBMgr::GetColumnFmt( uno::Reference< XDataSource> xSource,
1496cdf0e10cSrcweir                         uno::Reference< XConnection> xConnection,
1497cdf0e10cSrcweir                         uno::Reference< XPropertySet> xColumn,
1498cdf0e10cSrcweir 						SvNumberFormatter* pNFmtr,
1499cdf0e10cSrcweir                         long nLanguage )
1500cdf0e10cSrcweir {
1501cdf0e10cSrcweir 	//JP 12.01.99: ggfs. das NumberFormat im Doc setzen
1502cdf0e10cSrcweir 	sal_uLong nRet = 0;
1503cdf0e10cSrcweir 
1504cdf0e10cSrcweir     if(!xSource.is())
1505cdf0e10cSrcweir     {
1506cdf0e10cSrcweir         uno::Reference<XChild> xChild(xConnection, UNO_QUERY);
1507cdf0e10cSrcweir         if ( xChild.is() )
1508cdf0e10cSrcweir             xSource = uno::Reference<XDataSource>(xChild->getParent(), UNO_QUERY);
1509cdf0e10cSrcweir     }
1510cdf0e10cSrcweir     if(xSource.is() && xConnection.is() && xColumn.is() && pNFmtr)
1511cdf0e10cSrcweir 	{
1512cdf0e10cSrcweir 		SvNumberFormatsSupplierObj* pNumFmt = new SvNumberFormatsSupplierObj( pNFmtr );
1513cdf0e10cSrcweir         uno::Reference< util::XNumberFormatsSupplier >  xDocNumFmtsSupplier = pNumFmt;
1514cdf0e10cSrcweir         uno::Reference< XNumberFormats > xDocNumberFormats = xDocNumFmtsSupplier->getNumberFormats();
1515cdf0e10cSrcweir         uno::Reference< XNumberFormatTypes > xDocNumberFormatTypes(xDocNumberFormats, UNO_QUERY);
1516cdf0e10cSrcweir 
1517cdf0e10cSrcweir 		Locale aLocale( MsLangId::convertLanguageToLocale( (LanguageType)nLanguage ));
1518cdf0e10cSrcweir 
1519cdf0e10cSrcweir 		//get the number formatter of the data source
1520cdf0e10cSrcweir         uno::Reference<XPropertySet> xSourceProps(xSource, UNO_QUERY);
1521cdf0e10cSrcweir         uno::Reference< XNumberFormats > xNumberFormats;
1522cdf0e10cSrcweir 		if(xSourceProps.is())
1523cdf0e10cSrcweir 		{
1524cdf0e10cSrcweir 			Any aFormats = xSourceProps->getPropertyValue(C2U("NumberFormatsSupplier"));
1525cdf0e10cSrcweir 			if(aFormats.hasValue())
1526cdf0e10cSrcweir 			{
1527cdf0e10cSrcweir                 uno::Reference<XNumberFormatsSupplier> xSuppl;
1528cdf0e10cSrcweir                 aFormats >>= xSuppl;
1529cdf0e10cSrcweir 				if(xSuppl.is())
1530cdf0e10cSrcweir 				{
1531cdf0e10cSrcweir 					xNumberFormats = xSuppl->getNumberFormats();
1532cdf0e10cSrcweir 				}
1533cdf0e10cSrcweir 			}
1534cdf0e10cSrcweir 		}
1535cdf0e10cSrcweir         bool bUseDefault = true;
1536cdf0e10cSrcweir         try
1537cdf0e10cSrcweir         {
1538cdf0e10cSrcweir             Any aFormatKey = xColumn->getPropertyValue(C2U("FormatKey"));
1539cdf0e10cSrcweir             if(aFormatKey.hasValue())
1540cdf0e10cSrcweir             {
1541cdf0e10cSrcweir                 sal_Int32 nFmt = 0;
1542cdf0e10cSrcweir                 aFormatKey >>= nFmt;
1543cdf0e10cSrcweir                 if(xNumberFormats.is())
1544cdf0e10cSrcweir                 {
1545cdf0e10cSrcweir                     try
1546cdf0e10cSrcweir                     {
1547cdf0e10cSrcweir                         uno::Reference<XPropertySet> xNumProps = xNumberFormats->getByKey( nFmt );
1548cdf0e10cSrcweir                         Any aFormatString = xNumProps->getPropertyValue(C2U("FormatString"));
1549cdf0e10cSrcweir                         Any aLocaleVal = xNumProps->getPropertyValue(C2U("Locale"));
1550cdf0e10cSrcweir                         rtl::OUString sFormat;
1551cdf0e10cSrcweir                         aFormatString >>= sFormat;
1552cdf0e10cSrcweir                         lang::Locale aLoc;
1553cdf0e10cSrcweir                         aLocaleVal >>= aLoc;
1554cdf0e10cSrcweir                         nFmt = xDocNumberFormats->queryKey( sFormat, aLoc, sal_False );
1555cdf0e10cSrcweir                         if(NUMBERFORMAT_ENTRY_NOT_FOUND == sal::static_int_cast< sal_uInt32, sal_Int32>(nFmt))
1556cdf0e10cSrcweir                             nFmt = xDocNumberFormats->addNew( sFormat, aLoc );
1557cdf0e10cSrcweir                         nRet = nFmt;
1558cdf0e10cSrcweir                         bUseDefault = false;
1559cdf0e10cSrcweir                     }
1560cdf0e10cSrcweir                     catch(const Exception&)
1561cdf0e10cSrcweir                     {
1562cdf0e10cSrcweir                         DBG_ERROR("illegal number format key");
1563cdf0e10cSrcweir                     }
1564cdf0e10cSrcweir                 }
1565cdf0e10cSrcweir             }
1566cdf0e10cSrcweir         }
1567cdf0e10cSrcweir         catch( const Exception& )
1568cdf0e10cSrcweir         {
1569cdf0e10cSrcweir             DBG_ERROR("no FormatKey property found");
1570cdf0e10cSrcweir         }
1571cdf0e10cSrcweir         if(bUseDefault)
1572cdf0e10cSrcweir             nRet = SwNewDBMgr::GetDbtoolsClient().getDefaultNumberFormat(xColumn, xDocNumberFormatTypes,  aLocale);
1573cdf0e10cSrcweir 	}
1574cdf0e10cSrcweir 	return nRet;
1575cdf0e10cSrcweir }
1576cdf0e10cSrcweir 
1577cdf0e10cSrcweir /* -----------------------------17.07.00 09:47--------------------------------
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetColumnType(const String & rDBName,const String & rTableName,const String & rColNm)1580cdf0e10cSrcweir sal_Int32 SwNewDBMgr::GetColumnType( const String& rDBName,
1581cdf0e10cSrcweir 						  const String& rTableName,
1582cdf0e10cSrcweir 						  const String& rColNm )
1583cdf0e10cSrcweir {
1584cdf0e10cSrcweir 	sal_Int32 nRet = DataType::SQLNULL;
1585cdf0e10cSrcweir     SwDBData aData;
1586cdf0e10cSrcweir     aData.sDataSource = rDBName;
1587cdf0e10cSrcweir     aData.sCommand = rTableName;
1588cdf0e10cSrcweir     aData.nCommandType = -1;
1589cdf0e10cSrcweir     SwDSParam* pParam = FindDSData(aData, sal_False);
1590cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
1591cdf0e10cSrcweir     uno::Reference< XColumnsSupplier > xColsSupp;
1592cdf0e10cSrcweir     bool bDispose = false;
1593cdf0e10cSrcweir     if(pParam && pParam->xConnection.is())
1594cdf0e10cSrcweir     {
1595cdf0e10cSrcweir         xConnection = pParam->xConnection;
1596cdf0e10cSrcweir         xColsSupp = uno::Reference< XColumnsSupplier >( pParam->xResultSet, UNO_QUERY );
1597cdf0e10cSrcweir     }
1598cdf0e10cSrcweir     else
1599cdf0e10cSrcweir     {
1600cdf0e10cSrcweir         rtl::OUString sDBName(rDBName);
1601cdf0e10cSrcweir         xConnection = RegisterConnection( sDBName );
1602cdf0e10cSrcweir     }
1603cdf0e10cSrcweir     if( !xColsSupp.is() )
1604cdf0e10cSrcweir     {
1605cdf0e10cSrcweir         xColsSupp = SwNewDBMgr::GetColumnSupplier(xConnection, rTableName);
1606cdf0e10cSrcweir         bDispose = true;
1607cdf0e10cSrcweir     }
1608cdf0e10cSrcweir 	if(xColsSupp.is())
1609cdf0e10cSrcweir 	{
1610cdf0e10cSrcweir         uno::Reference<XNameAccess> xCols = xColsSupp->getColumns();
1611cdf0e10cSrcweir 		if(xCols->hasByName(rColNm))
1612cdf0e10cSrcweir 		{
1613cdf0e10cSrcweir 			Any aCol = xCols->getByName(rColNm);
1614cdf0e10cSrcweir             uno::Reference<XPropertySet> xCol;
1615cdf0e10cSrcweir             aCol >>= xCol;
1616cdf0e10cSrcweir 			Any aType = xCol->getPropertyValue(C2S("Type"));
1617cdf0e10cSrcweir 			aType >>= nRet;
1618cdf0e10cSrcweir 		}
1619cdf0e10cSrcweir         if(bDispose)
1620cdf0e10cSrcweir             ::comphelper::disposeComponent( xColsSupp );
1621cdf0e10cSrcweir     }
1622cdf0e10cSrcweir 	return nRet;
1623cdf0e10cSrcweir }
1624cdf0e10cSrcweir 
1625cdf0e10cSrcweir /* -----------------------------03.07.00 17:12--------------------------------
1626cdf0e10cSrcweir 
1627cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetConnection(const String & rDataSource,uno::Reference<XDataSource> & rxSource)1628cdf0e10cSrcweir uno::Reference< sdbc::XConnection> SwNewDBMgr::GetConnection(const String& rDataSource,
1629cdf0e10cSrcweir                                                     uno::Reference<XDataSource>& rxSource)
1630cdf0e10cSrcweir {
1631cdf0e10cSrcweir 	Reference< sdbc::XConnection> xConnection;
1632cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
1633cdf0e10cSrcweir 	try
1634cdf0e10cSrcweir 	{
1635cdf0e10cSrcweir 		Reference<XCompletedConnection> xComplConnection(SwNewDBMgr::GetDbtoolsClient().getDataSource(rDataSource, xMgr),UNO_QUERY);
1636cdf0e10cSrcweir 		if ( xComplConnection.is() )
1637cdf0e10cSrcweir 		{
1638cdf0e10cSrcweir 			rxSource.set(xComplConnection,UNO_QUERY);
1639cdf0e10cSrcweir 			Reference< XInteractionHandler > xHandler(
1640cdf0e10cSrcweir 					xMgr->createInstance( C2U( "com.sun.star.task.InteractionHandler" )), UNO_QUERY);
1641cdf0e10cSrcweir 				xConnection = xComplConnection->connectWithCompletion( xHandler );
1642cdf0e10cSrcweir 		}
1643cdf0e10cSrcweir 	}
1644cdf0e10cSrcweir 	catch(Exception&) {}
1645cdf0e10cSrcweir 
1646cdf0e10cSrcweir 	return xConnection;
1647cdf0e10cSrcweir }
1648cdf0e10cSrcweir /* -----------------------------03.07.00 17:12--------------------------------
1649cdf0e10cSrcweir 
1650cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetColumnSupplier(uno::Reference<sdbc::XConnection> xConnection,const String & rTableOrQuery,sal_uInt8 eTableOrQuery)1651cdf0e10cSrcweir uno::Reference< sdbcx::XColumnsSupplier> SwNewDBMgr::GetColumnSupplier(uno::Reference<sdbc::XConnection> xConnection,
1652cdf0e10cSrcweir 									const String& rTableOrQuery,
1653cdf0e10cSrcweir 									sal_uInt8	eTableOrQuery)
1654cdf0e10cSrcweir {
1655cdf0e10cSrcweir     Reference< sdbcx::XColumnsSupplier> xRet;
1656cdf0e10cSrcweir     try
1657cdf0e10cSrcweir     {
1658cdf0e10cSrcweir         if(eTableOrQuery == SW_DB_SELECT_UNKNOWN)
1659cdf0e10cSrcweir         {
1660cdf0e10cSrcweir             //search for a table with the given command name
1661cdf0e10cSrcweir             Reference<XTablesSupplier> xTSupplier = Reference<XTablesSupplier>(xConnection, UNO_QUERY);
1662cdf0e10cSrcweir             if(xTSupplier.is())
1663cdf0e10cSrcweir             {
1664cdf0e10cSrcweir                 Reference<XNameAccess> xTbls = xTSupplier->getTables();
1665cdf0e10cSrcweir                 eTableOrQuery = xTbls->hasByName(rTableOrQuery) ?
1666cdf0e10cSrcweir                             SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY;
1667cdf0e10cSrcweir             }
1668cdf0e10cSrcweir         }
1669cdf0e10cSrcweir         sal_Int32 nCommandType = SW_DB_SELECT_TABLE == eTableOrQuery ?
1670cdf0e10cSrcweir                 CommandType::TABLE : CommandType::QUERY;
1671cdf0e10cSrcweir         Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
1672cdf0e10cSrcweir         Reference<XRowSet> xRowSet(
1673cdf0e10cSrcweir                 xMgr->createInstance(C2U("com.sun.star.sdb.RowSet")), UNO_QUERY);
1674cdf0e10cSrcweir 
1675cdf0e10cSrcweir         ::rtl::OUString sDataSource;
1676cdf0e10cSrcweir         Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection, sDataSource);
1677cdf0e10cSrcweir         Reference<XPropertySet> xSourceProperties(xSource, UNO_QUERY);
1678cdf0e10cSrcweir         if(xSourceProperties.is())
1679cdf0e10cSrcweir         {
1680cdf0e10cSrcweir             xSourceProperties->getPropertyValue(C2U("Name")) >>= sDataSource;
1681cdf0e10cSrcweir         }
1682cdf0e10cSrcweir 
1683cdf0e10cSrcweir         Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
1684cdf0e10cSrcweir         xRowProperties->setPropertyValue(C2U("DataSourceName"), makeAny(sDataSource));
1685cdf0e10cSrcweir         xRowProperties->setPropertyValue(C2U("Command"), makeAny(::rtl::OUString(rTableOrQuery)));
1686cdf0e10cSrcweir         xRowProperties->setPropertyValue(C2U("CommandType"), makeAny(nCommandType));
1687cdf0e10cSrcweir         xRowProperties->setPropertyValue(C2U("FetchSize"), makeAny((sal_Int32)10));
1688cdf0e10cSrcweir         xRowProperties->setPropertyValue(C2U("ActiveConnection"), makeAny(xConnection));
1689cdf0e10cSrcweir         xRowSet->execute();
1690cdf0e10cSrcweir         xRet = Reference<XColumnsSupplier>( xRowSet, UNO_QUERY );
1691cdf0e10cSrcweir     }
1692cdf0e10cSrcweir     catch( const uno::Exception& )
1693cdf0e10cSrcweir     {
1694cdf0e10cSrcweir         DBG_ERROR("Exception in SwDBMgr::GetColumnSupplier");
1695cdf0e10cSrcweir     }
1696cdf0e10cSrcweir 
1697cdf0e10cSrcweir     return xRet;
1698cdf0e10cSrcweir }
1699cdf0e10cSrcweir /* -----------------------------05.07.00 13:44--------------------------------
1700cdf0e10cSrcweir 
1701cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetDBField(uno::Reference<XPropertySet> xColumnProps,const SwDBFormatData & rDBFormatData,double * pNumber)1702cdf0e10cSrcweir String SwNewDBMgr::GetDBField(uno::Reference<XPropertySet> xColumnProps,
1703cdf0e10cSrcweir 						const SwDBFormatData& rDBFormatData,
1704cdf0e10cSrcweir 						double* pNumber)
1705cdf0e10cSrcweir {
1706cdf0e10cSrcweir     uno::Reference< XColumn > xColumn(xColumnProps, UNO_QUERY);
1707cdf0e10cSrcweir 	String sRet;
1708cdf0e10cSrcweir 	DBG_ASSERT(xColumn.is(), "SwNewDBMgr::::ImportDBField: illegal arguments");
1709cdf0e10cSrcweir 	if(!xColumn.is())
1710cdf0e10cSrcweir 		return sRet;
1711cdf0e10cSrcweir 
1712cdf0e10cSrcweir 	Any aType = xColumnProps->getPropertyValue(C2U("Type"));
1713cdf0e10cSrcweir 	sal_Int32 eDataType = 0;
1714cdf0e10cSrcweir 	aType >>= eDataType;
1715cdf0e10cSrcweir 	switch(eDataType)
1716cdf0e10cSrcweir 	{
1717cdf0e10cSrcweir 		case DataType::CHAR:
1718cdf0e10cSrcweir 		case DataType::VARCHAR:
1719cdf0e10cSrcweir 		case DataType::LONGVARCHAR:
1720cdf0e10cSrcweir             try
1721cdf0e10cSrcweir             {
1722cdf0e10cSrcweir 			    sRet = xColumn->getString();
1723cdf0e10cSrcweir             }
1724cdf0e10cSrcweir             catch( SQLException& )
1725cdf0e10cSrcweir             {
1726cdf0e10cSrcweir             }
1727cdf0e10cSrcweir 		break;
1728cdf0e10cSrcweir 		case DataType::BIT:
1729cdf0e10cSrcweir 		case DataType::BOOLEAN:
1730cdf0e10cSrcweir 		case DataType::TINYINT:
1731cdf0e10cSrcweir 		case DataType::SMALLINT:
1732cdf0e10cSrcweir 		case DataType::INTEGER:
1733cdf0e10cSrcweir 		case DataType::BIGINT:
1734cdf0e10cSrcweir 		case DataType::FLOAT:
1735cdf0e10cSrcweir 		case DataType::REAL:
1736cdf0e10cSrcweir 		case DataType::DOUBLE:
1737cdf0e10cSrcweir 		case DataType::NUMERIC:
1738cdf0e10cSrcweir 		case DataType::DECIMAL:
1739cdf0e10cSrcweir 		case DataType::DATE:
1740cdf0e10cSrcweir 		case DataType::TIME:
1741cdf0e10cSrcweir 		case DataType::TIMESTAMP:
1742cdf0e10cSrcweir 		{
1743cdf0e10cSrcweir //			::Date aTempDate(rDBFormatData.aNullDate.Day,
1744cdf0e10cSrcweir //				rDBFormatData.aNullDate.Month, rDBFormatData.aNullDate.Year);
1745cdf0e10cSrcweir 
1746cdf0e10cSrcweir 			try
1747cdf0e10cSrcweir 			{
1748cdf0e10cSrcweir                 SwDbtoolsClient& aClient = SwNewDBMgr::GetDbtoolsClient();
1749cdf0e10cSrcweir                 sRet = aClient.getFormattedValue(
1750cdf0e10cSrcweir 					xColumnProps,
1751cdf0e10cSrcweir 					rDBFormatData.xFormatter,
1752cdf0e10cSrcweir 					rDBFormatData.aLocale,
1753cdf0e10cSrcweir 					rDBFormatData.aNullDate);
1754cdf0e10cSrcweir 				if (pNumber)
1755cdf0e10cSrcweir                 {
1756cdf0e10cSrcweir 				    double fVal = xColumn->getDouble();
1757cdf0e10cSrcweir                     if(!xColumn->wasNull())
1758cdf0e10cSrcweir                     {
1759cdf0e10cSrcweir                         *pNumber = fVal;
1760cdf0e10cSrcweir                     }
1761cdf0e10cSrcweir                 }
1762cdf0e10cSrcweir 			}
1763cdf0e10cSrcweir             catch(Exception& )
1764cdf0e10cSrcweir 			{
1765cdf0e10cSrcweir 				DBG_ERROR("exception caught");
1766cdf0e10cSrcweir 			}
1767cdf0e10cSrcweir 
1768cdf0e10cSrcweir 		}
1769cdf0e10cSrcweir 		break;
1770cdf0e10cSrcweir 
1771cdf0e10cSrcweir //		case DataType::BINARY:
1772cdf0e10cSrcweir //		case DataType::VARBINARY:
1773cdf0e10cSrcweir //		case DataType::LONGVARBINARY:
1774cdf0e10cSrcweir //		case DataType::SQLNULL:
1775cdf0e10cSrcweir //		case DataType::OTHER:
1776cdf0e10cSrcweir //		case DataType::OBJECT:
1777cdf0e10cSrcweir //		case DataType::DISTINCT:
1778cdf0e10cSrcweir //		case DataType::STRUCT:
1779cdf0e10cSrcweir //		case DataType::ARRAY:
1780cdf0e10cSrcweir //		case DataType::BLOB:
1781cdf0e10cSrcweir //		case DataType::CLOB:
1782cdf0e10cSrcweir //		case DataType::REF:
1783cdf0e10cSrcweir //		default:
1784cdf0e10cSrcweir 	}
1785cdf0e10cSrcweir //	if (pFormat)
1786cdf0e10cSrcweir //	{
1787cdf0e10cSrcweir //		SFX_ITEMSET_GET(*pCol, pFormatItem, SfxUInt32Item, SBA_DEF_FMTVALUE, sal_True);
1788cdf0e10cSrcweir //		*pFormat = pFormatItem->GetValue();
1789cdf0e10cSrcweir //	}
1790cdf0e10cSrcweir 
1791cdf0e10cSrcweir 	return sRet;
1792cdf0e10cSrcweir }
1793cdf0e10cSrcweir /* -----------------------------06.07.00 14:28--------------------------------
1794cdf0e10cSrcweir 	releases the merge data source table or query after merge is completed
1795cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
EndMerge()1796cdf0e10cSrcweir void	SwNewDBMgr::EndMerge()
1797cdf0e10cSrcweir {
1798cdf0e10cSrcweir 	DBG_ASSERT(bInMerge, "merge is not active");
1799cdf0e10cSrcweir 	bInMerge = sal_False;
1800cdf0e10cSrcweir     delete pImpl->pMergeData;
1801cdf0e10cSrcweir     pImpl->pMergeData = 0;
1802cdf0e10cSrcweir }
1803cdf0e10cSrcweir /* -----------------------------06.07.00 14:28--------------------------------
1804cdf0e10cSrcweir 	checks if a desired data source table or query is open
1805cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
IsDataSourceOpen(const String & rDataSource,const String & rTableOrQuery,sal_Bool bMergeOnly)1806cdf0e10cSrcweir sal_Bool    SwNewDBMgr::IsDataSourceOpen(const String& rDataSource,
1807cdf0e10cSrcweir             const String& rTableOrQuery, sal_Bool bMergeOnly)
1808cdf0e10cSrcweir {
1809cdf0e10cSrcweir     if(pImpl->pMergeData)
1810cdf0e10cSrcweir 	{
1811cdf0e10cSrcweir         return !bMergeLock &&
1812cdf0e10cSrcweir                 ((rDataSource == (String)pImpl->pMergeData->sDataSource &&
1813cdf0e10cSrcweir                     rTableOrQuery == (String)pImpl->pMergeData->sCommand)
1814cdf0e10cSrcweir                     ||(!rDataSource.Len() && !rTableOrQuery.Len()))
1815cdf0e10cSrcweir                     &&
1816cdf0e10cSrcweir                     pImpl->pMergeData->xResultSet.is();
1817cdf0e10cSrcweir 	}
1818cdf0e10cSrcweir     else if(!bMergeOnly)
1819cdf0e10cSrcweir     {
1820cdf0e10cSrcweir         SwDBData aData;
1821cdf0e10cSrcweir         aData.sDataSource = rDataSource;
1822cdf0e10cSrcweir         aData.sCommand = rTableOrQuery;
1823cdf0e10cSrcweir         aData.nCommandType = -1;
1824cdf0e10cSrcweir         SwDSParam* pFound = FindDSData(aData, sal_False);
1825cdf0e10cSrcweir         return (pFound && pFound->xResultSet.is());
1826cdf0e10cSrcweir     }
1827cdf0e10cSrcweir     return sal_False;
1828cdf0e10cSrcweir }
1829cdf0e10cSrcweir /* -----------------------------17.07.00 16:44--------------------------------
1830cdf0e10cSrcweir 	read column data a a specified position
1831cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetColumnCnt(const String & rSourceName,const String & rTableName,const String & rColumnName,sal_uInt32 nAbsRecordId,long nLanguage,String & rResult,double * pNumber)1832cdf0e10cSrcweir sal_Bool SwNewDBMgr::GetColumnCnt(const String& rSourceName, const String& rTableName,
1833cdf0e10cSrcweir 							const String& rColumnName, sal_uInt32 nAbsRecordId,
1834cdf0e10cSrcweir 							long nLanguage,
1835cdf0e10cSrcweir 							String& rResult, double* pNumber)
1836cdf0e10cSrcweir {
1837cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
1838cdf0e10cSrcweir     SwDSParam* pFound = 0;
1839cdf0e10cSrcweir     //check if it's the merge data source
1840cdf0e10cSrcweir     if(pImpl->pMergeData &&
1841cdf0e10cSrcweir         rSourceName == (String)pImpl->pMergeData->sDataSource &&
1842cdf0e10cSrcweir         rTableName == (String)pImpl->pMergeData->sCommand)
1843cdf0e10cSrcweir 	{
1844cdf0e10cSrcweir         pFound = pImpl->pMergeData;
1845cdf0e10cSrcweir     }
1846cdf0e10cSrcweir     else
1847cdf0e10cSrcweir 	{
1848cdf0e10cSrcweir         SwDBData aData;
1849cdf0e10cSrcweir         aData.sDataSource = rSourceName;
1850cdf0e10cSrcweir         aData.sCommand = rTableName;
1851cdf0e10cSrcweir         aData.nCommandType = -1;
1852cdf0e10cSrcweir         pFound = FindDSData(aData, sal_False);
1853cdf0e10cSrcweir     }
1854cdf0e10cSrcweir     //check validity of supplied record Id
1855cdf0e10cSrcweir     if(pFound->aSelection.getLength())
1856cdf0e10cSrcweir     {
1857cdf0e10cSrcweir         //the destination has to be an element of the selection
1858cdf0e10cSrcweir         const Any* pSelection = pFound->aSelection.getConstArray();
1859cdf0e10cSrcweir         sal_Bool bFound = sal_False;
1860cdf0e10cSrcweir         for(sal_Int32 nPos = 0; !bFound && nPos < pFound->aSelection.getLength(); nPos++)
1861cdf0e10cSrcweir         {
1862cdf0e10cSrcweir             sal_Int32 nSelection = 0;
1863cdf0e10cSrcweir             pSelection[nPos] >>= nSelection;
1864cdf0e10cSrcweir             if(nSelection == static_cast<sal_Int32>(nAbsRecordId))
1865cdf0e10cSrcweir                 bFound = sal_True;
1866cdf0e10cSrcweir         }
1867cdf0e10cSrcweir         if(!bFound)
1868cdf0e10cSrcweir             return sal_False;
1869cdf0e10cSrcweir     }
1870cdf0e10cSrcweir     if(pFound && pFound->xResultSet.is() && !pFound->bAfterSelection)
1871cdf0e10cSrcweir     {
1872cdf0e10cSrcweir         sal_Int32 nOldRow = 0;
1873cdf0e10cSrcweir         try
1874cdf0e10cSrcweir         {
1875cdf0e10cSrcweir             nOldRow = pFound->xResultSet->getRow();
1876cdf0e10cSrcweir         }
1877cdf0e10cSrcweir         catch(const Exception& )
1878cdf0e10cSrcweir         {
1879cdf0e10cSrcweir 			return sal_False;
1880cdf0e10cSrcweir         }
1881cdf0e10cSrcweir         //position to the desired index
1882cdf0e10cSrcweir         sal_Bool bMove = sal_True;
1883cdf0e10cSrcweir         if ( nOldRow != static_cast<sal_Int32>(nAbsRecordId) )
1884cdf0e10cSrcweir             bMove = lcl_MoveAbsolute(pFound, nAbsRecordId);
1885cdf0e10cSrcweir         if(bMove)
1886cdf0e10cSrcweir         {
1887cdf0e10cSrcweir             bRet = lcl_GetColumnCnt(pFound, rColumnName, nLanguage, rResult, pNumber);
1888cdf0e10cSrcweir         }
1889cdf0e10cSrcweir         if ( nOldRow != static_cast<sal_Int32>(nAbsRecordId) )
1890cdf0e10cSrcweir             bMove = lcl_MoveAbsolute(pFound, nOldRow);
1891cdf0e10cSrcweir     }
1892cdf0e10cSrcweir 	return bRet;
1893cdf0e10cSrcweir }
1894cdf0e10cSrcweir /* -----------------------------06.07.00 16:47--------------------------------
1895cdf0e10cSrcweir 	reads the column data at the current position
1896cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetMergeColumnCnt(const String & rColumnName,sal_uInt16 nLanguage,String & rResult,double * pNumber,sal_uInt32 *)1897cdf0e10cSrcweir sal_Bool	SwNewDBMgr::GetMergeColumnCnt(const String& rColumnName, sal_uInt16 nLanguage,
1898cdf0e10cSrcweir                                 String &rResult, double *pNumber, sal_uInt32 * /*pFormat*/)
1899cdf0e10cSrcweir {
1900cdf0e10cSrcweir     if(!pImpl->pMergeData || !pImpl->pMergeData->xResultSet.is() || pImpl->pMergeData->bAfterSelection )
1901cdf0e10cSrcweir 	{
1902cdf0e10cSrcweir 		rResult.Erase();
1903cdf0e10cSrcweir 		return sal_False;
1904cdf0e10cSrcweir 	}
1905cdf0e10cSrcweir 
1906cdf0e10cSrcweir     sal_Bool bRet = lcl_GetColumnCnt(pImpl->pMergeData, rColumnName, nLanguage, rResult, pNumber);
1907cdf0e10cSrcweir 	return bRet;
1908cdf0e10cSrcweir }
1909cdf0e10cSrcweir /* -----------------------------07.07.00 14:28--------------------------------
1910cdf0e10cSrcweir 
1911cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
ToNextMergeRecord()1912cdf0e10cSrcweir sal_Bool SwNewDBMgr::ToNextMergeRecord()
1913cdf0e10cSrcweir {
1914cdf0e10cSrcweir     DBG_ASSERT(pImpl->pMergeData && pImpl->pMergeData->xResultSet.is(), "no data source in merge");
1915cdf0e10cSrcweir     return ToNextRecord(pImpl->pMergeData);
1916cdf0e10cSrcweir }
1917cdf0e10cSrcweir /* -----------------------------10.07.01 14:28--------------------------------
1918cdf0e10cSrcweir 
1919cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
ToNextRecord(const String & rDataSource,const String & rCommand,sal_Int32)1920cdf0e10cSrcweir sal_Bool SwNewDBMgr::ToNextRecord(
1921cdf0e10cSrcweir     const String& rDataSource, const String& rCommand, sal_Int32 /*nCommandType*/)
1922cdf0e10cSrcweir {
1923cdf0e10cSrcweir     SwDSParam* pFound = 0;
1924cdf0e10cSrcweir     if(pImpl->pMergeData &&
1925cdf0e10cSrcweir         rDataSource == (String)pImpl->pMergeData->sDataSource &&
1926cdf0e10cSrcweir         rCommand == (String)pImpl->pMergeData->sCommand)
1927cdf0e10cSrcweir         pFound = pImpl->pMergeData;
1928cdf0e10cSrcweir     else
1929cdf0e10cSrcweir     {
1930cdf0e10cSrcweir         SwDBData aData;
1931cdf0e10cSrcweir         aData.sDataSource = rDataSource;
1932cdf0e10cSrcweir         aData.sCommand = rCommand;
1933cdf0e10cSrcweir         aData.nCommandType = -1;
1934cdf0e10cSrcweir         pFound = FindDSData(aData, sal_False);
1935cdf0e10cSrcweir     }
1936cdf0e10cSrcweir     return ToNextRecord(pFound);
1937cdf0e10cSrcweir }
1938cdf0e10cSrcweir /* -----------------------------10.07.01 14:38--------------------------------
1939cdf0e10cSrcweir 
1940cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
ToNextRecord(SwDSParam * pParam)1941cdf0e10cSrcweir sal_Bool SwNewDBMgr::ToNextRecord(SwDSParam* pParam)
1942cdf0e10cSrcweir {
1943cdf0e10cSrcweir     sal_Bool bRet = sal_True;
1944cdf0e10cSrcweir     if(!pParam || !pParam->xResultSet.is() || pParam->bEndOfDB ||
1945cdf0e10cSrcweir 			(pParam->aSelection.getLength() && pParam->aSelection.getLength() <= pParam->nSelectionIndex))
1946cdf0e10cSrcweir 	{
1947cdf0e10cSrcweir         if(pParam)
1948cdf0e10cSrcweir             pParam->CheckEndOfDB();
1949cdf0e10cSrcweir 		return sal_False;
1950cdf0e10cSrcweir 	}
1951cdf0e10cSrcweir 	try
1952cdf0e10cSrcweir 	{
1953cdf0e10cSrcweir         if(pParam->aSelection.getLength())
1954cdf0e10cSrcweir 		{
1955cdf0e10cSrcweir 			sal_Int32 nPos = 0;
1956cdf0e10cSrcweir 			pParam->aSelection.getConstArray()[ pParam->nSelectionIndex++ ] >>= nPos;
1957cdf0e10cSrcweir             pParam->bEndOfDB = !pParam->xResultSet->absolute( nPos );
1958cdf0e10cSrcweir             pParam->CheckEndOfDB();
1959cdf0e10cSrcweir             bRet = !pParam->bEndOfDB;
1960cdf0e10cSrcweir             if(pParam->nSelectionIndex >= pParam->aSelection.getLength())
1961cdf0e10cSrcweir                 pParam->bEndOfDB = sal_True;
1962cdf0e10cSrcweir 		}
1963cdf0e10cSrcweir 		else
1964cdf0e10cSrcweir 		{
1965cdf0e10cSrcweir             sal_Int32 nBefore = pParam->xResultSet->getRow();
1966cdf0e10cSrcweir             pParam->bEndOfDB = !pParam->xResultSet->next();
1967cdf0e10cSrcweir             if( !pParam->bEndOfDB && nBefore == pParam->xResultSet->getRow())
1968cdf0e10cSrcweir             {
1969cdf0e10cSrcweir                 //next returned true but it didn't move
1970cdf0e10cSrcweir                 pParam->bEndOfDB = sal_True;
1971cdf0e10cSrcweir             }
1972cdf0e10cSrcweir 
1973cdf0e10cSrcweir             pParam->CheckEndOfDB();
1974cdf0e10cSrcweir             bRet = !pParam->bEndOfDB;
1975cdf0e10cSrcweir             ++pParam->nSelectionIndex;
1976cdf0e10cSrcweir 		}
1977cdf0e10cSrcweir 	}
1978cdf0e10cSrcweir     catch(Exception&)
1979cdf0e10cSrcweir 	{
1980cdf0e10cSrcweir 	}
1981cdf0e10cSrcweir     return bRet;
1982cdf0e10cSrcweir }
1983cdf0e10cSrcweir 
1984cdf0e10cSrcweir /* -----------------------------13.07.00 17:23--------------------------------
1985cdf0e10cSrcweir 	synchronized labels contain a next record field at their end
1986cdf0e10cSrcweir 	to assure that the next page can be created in mail merge
1987cdf0e10cSrcweir 	the cursor position must be validated
1988cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
ExistsNextRecord() const1989cdf0e10cSrcweir sal_Bool SwNewDBMgr::ExistsNextRecord() const
1990cdf0e10cSrcweir {
1991cdf0e10cSrcweir     return pImpl->pMergeData && !pImpl->pMergeData->bEndOfDB;
1992cdf0e10cSrcweir }
1993cdf0e10cSrcweir /* -----------------------------13.07.00 10:41--------------------------------
1994cdf0e10cSrcweir 
1995cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetSelectedRecordId()1996cdf0e10cSrcweir sal_uInt32 	SwNewDBMgr::GetSelectedRecordId()
1997cdf0e10cSrcweir {
1998cdf0e10cSrcweir 	sal_uInt32 	nRet = 0;
1999cdf0e10cSrcweir     DBG_ASSERT(pImpl->pMergeData && pImpl->pMergeData->xResultSet.is(), "no data source in merge");
2000cdf0e10cSrcweir     if(!pImpl->pMergeData || !pImpl->pMergeData->xResultSet.is())
2001cdf0e10cSrcweir 		return sal_False;
2002cdf0e10cSrcweir 	try
2003cdf0e10cSrcweir 	{
2004cdf0e10cSrcweir         nRet = pImpl->pMergeData->xResultSet->getRow();
2005cdf0e10cSrcweir 	}
2006cdf0e10cSrcweir     catch(Exception& )
2007cdf0e10cSrcweir 	{
2008cdf0e10cSrcweir 	}
2009cdf0e10cSrcweir 	return nRet;
2010cdf0e10cSrcweir }
2011cdf0e10cSrcweir /* -----------------------------13.07.00 10:58--------------------------------
2012cdf0e10cSrcweir 
2013cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
ToRecordId(sal_Int32 nSet)2014cdf0e10cSrcweir sal_Bool SwNewDBMgr::ToRecordId(sal_Int32 nSet)
2015cdf0e10cSrcweir {
2016cdf0e10cSrcweir     DBG_ASSERT(pImpl->pMergeData && pImpl->pMergeData->xResultSet.is(), "no data source in merge");
2017cdf0e10cSrcweir     if(!pImpl->pMergeData || !pImpl->pMergeData->xResultSet.is()|| nSet < 0)
2018cdf0e10cSrcweir 		return sal_False;
2019cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
2020cdf0e10cSrcweir     sal_Int32 nAbsPos = nSet;
2021cdf0e10cSrcweir 
2022cdf0e10cSrcweir 	if(nAbsPos >= 0)
2023cdf0e10cSrcweir 	{
2024cdf0e10cSrcweir         bRet = lcl_MoveAbsolute(pImpl->pMergeData, nAbsPos);
2025cdf0e10cSrcweir         pImpl->pMergeData->bEndOfDB = !bRet;
2026cdf0e10cSrcweir         pImpl->pMergeData->CheckEndOfDB();
2027cdf0e10cSrcweir 	}
2028cdf0e10cSrcweir 	return bRet;
2029cdf0e10cSrcweir }
2030cdf0e10cSrcweir 
2031cdf0e10cSrcweir /* -----------------------------17.07.00 14:17--------------------------------
2032cdf0e10cSrcweir 
2033cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
OpenDataSource(const String & rDataSource,const String & rTableOrQuery,sal_Int32 nCommandType,bool bCreate)2034cdf0e10cSrcweir sal_Bool SwNewDBMgr::OpenDataSource(const String& rDataSource, const String& rTableOrQuery,
2035cdf0e10cSrcweir             sal_Int32 nCommandType, bool bCreate)
2036cdf0e10cSrcweir {
2037cdf0e10cSrcweir     SwDBData aData;
2038cdf0e10cSrcweir     aData.sDataSource = rDataSource;
2039cdf0e10cSrcweir     aData.sCommand = rTableOrQuery;
2040cdf0e10cSrcweir     aData.nCommandType = nCommandType;
2041cdf0e10cSrcweir 
2042cdf0e10cSrcweir     SwDSParam* pFound = FindDSData(aData, sal_True);
2043cdf0e10cSrcweir     uno::Reference< XDataSource> xSource;
2044cdf0e10cSrcweir 	if(pFound->xResultSet.is())
2045cdf0e10cSrcweir 		return sal_True;
2046cdf0e10cSrcweir     SwDSParam* pParam = FindDSConnection(rDataSource, sal_False);
2047cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
2048cdf0e10cSrcweir     if(pParam && pParam->xConnection.is())
2049cdf0e10cSrcweir         pFound->xConnection = pParam->xConnection;
2050cdf0e10cSrcweir     else if(bCreate)
2051cdf0e10cSrcweir     {
2052cdf0e10cSrcweir         rtl::OUString sDataSource(rDataSource);
2053cdf0e10cSrcweir         pFound->xConnection = RegisterConnection( sDataSource );
2054cdf0e10cSrcweir     }
2055cdf0e10cSrcweir 	if(pFound->xConnection.is())
2056cdf0e10cSrcweir 	{
2057cdf0e10cSrcweir 		try
2058cdf0e10cSrcweir 		{
2059cdf0e10cSrcweir             uno::Reference< sdbc::XDatabaseMetaData >  xMetaData = pFound->xConnection->getMetaData();
2060cdf0e10cSrcweir             try
2061cdf0e10cSrcweir             {
2062cdf0e10cSrcweir                 pFound->bScrollable = xMetaData
2063cdf0e10cSrcweir 						->supportsResultSetType((sal_Int32)ResultSetType::SCROLL_INSENSITIVE);
2064cdf0e10cSrcweir             }
2065cdf0e10cSrcweir             catch(Exception&)
2066cdf0e10cSrcweir             {
2067cdf0e10cSrcweir                 //#98373# DB driver may not be ODBC 3.0 compliant
2068cdf0e10cSrcweir                 pFound->bScrollable = sal_True;
2069cdf0e10cSrcweir             }
2070cdf0e10cSrcweir 			pFound->xStatement = pFound->xConnection->createStatement();
2071cdf0e10cSrcweir             rtl::OUString aQuoteChar = xMetaData->getIdentifierQuoteString();
2072cdf0e10cSrcweir             rtl::OUString sStatement(C2U("SELECT * FROM "));
2073cdf0e10cSrcweir             sStatement = C2U("SELECT * FROM ");
2074cdf0e10cSrcweir             sStatement += aQuoteChar;
2075cdf0e10cSrcweir             sStatement += rTableOrQuery;
2076cdf0e10cSrcweir             sStatement += aQuoteChar;
2077cdf0e10cSrcweir             pFound->xResultSet = pFound->xStatement->executeQuery( sStatement );
2078cdf0e10cSrcweir 
2079cdf0e10cSrcweir 			//after executeQuery the cursor must be positioned
2080cdf0e10cSrcweir             pFound->bEndOfDB = !pFound->xResultSet->next();
2081cdf0e10cSrcweir             pFound->bAfterSelection = sal_False;
2082cdf0e10cSrcweir             pFound->CheckEndOfDB();
2083cdf0e10cSrcweir             ++pFound->nSelectionIndex;
2084cdf0e10cSrcweir 		}
2085cdf0e10cSrcweir 		catch (Exception&)
2086cdf0e10cSrcweir 		{
2087cdf0e10cSrcweir 			pFound->xResultSet = 0;
2088cdf0e10cSrcweir 			pFound->xStatement = 0;
2089cdf0e10cSrcweir 			pFound->xConnection = 0;
2090cdf0e10cSrcweir 		}
2091cdf0e10cSrcweir 	}
2092cdf0e10cSrcweir 	return pFound->xResultSet.is();
2093cdf0e10cSrcweir }
2094cdf0e10cSrcweir /* -----------------------------14.08.2001 10:26------------------------------
2095cdf0e10cSrcweir 
2096cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
RegisterConnection(rtl::OUString & rDataSource)2097cdf0e10cSrcweir uno::Reference< XConnection> SwNewDBMgr::RegisterConnection(rtl::OUString& rDataSource)
2098cdf0e10cSrcweir {
2099cdf0e10cSrcweir     SwDSParam* pFound = SwNewDBMgr::FindDSConnection(rDataSource, sal_True);
2100cdf0e10cSrcweir     uno::Reference< XDataSource> xSource;
2101cdf0e10cSrcweir     if(!pFound->xConnection.is())
2102cdf0e10cSrcweir 	{
2103cdf0e10cSrcweir         pFound->xConnection = SwNewDBMgr::GetConnection(rDataSource, xSource );
2104cdf0e10cSrcweir         try
2105cdf0e10cSrcweir         {
2106cdf0e10cSrcweir             uno::Reference<XComponent> xComponent(pFound->xConnection, UNO_QUERY);
2107cdf0e10cSrcweir             if(xComponent.is())
2108cdf0e10cSrcweir                 xComponent->addEventListener(pImpl->xDisposeListener);
2109cdf0e10cSrcweir         }
2110cdf0e10cSrcweir         catch(Exception&)
2111cdf0e10cSrcweir         {
2112cdf0e10cSrcweir         }
2113cdf0e10cSrcweir 	}
2114cdf0e10cSrcweir     return pFound->xConnection;
2115cdf0e10cSrcweir }
2116cdf0e10cSrcweir /* -----------------------------17.07.00 15:55--------------------------------
2117cdf0e10cSrcweir 
2118cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetSelectedRecordId(const String & rDataSource,const String & rTableOrQuery,sal_Int32 nCommandType)2119cdf0e10cSrcweir sal_uInt32      SwNewDBMgr::GetSelectedRecordId(
2120cdf0e10cSrcweir     const String& rDataSource, const String& rTableOrQuery, sal_Int32 nCommandType)
2121cdf0e10cSrcweir {
2122cdf0e10cSrcweir     sal_uInt32 nRet = 0xffffffff;
2123cdf0e10cSrcweir 	//check for merge data source first
2124cdf0e10cSrcweir     if(pImpl->pMergeData && rDataSource == (String)pImpl->pMergeData->sDataSource &&
2125cdf0e10cSrcweir                     rTableOrQuery == (String)pImpl->pMergeData->sCommand &&
2126cdf0e10cSrcweir                     (nCommandType == -1 || nCommandType == pImpl->pMergeData->nCommandType) &&
2127cdf0e10cSrcweir                     pImpl->pMergeData->xResultSet.is())
2128cdf0e10cSrcweir 		nRet = GetSelectedRecordId();
2129cdf0e10cSrcweir 	else
2130cdf0e10cSrcweir 	{
2131cdf0e10cSrcweir         SwDBData aData;
2132cdf0e10cSrcweir         aData.sDataSource = rDataSource;
2133cdf0e10cSrcweir         aData.sCommand = rTableOrQuery;
2134cdf0e10cSrcweir         aData.nCommandType = nCommandType;
2135cdf0e10cSrcweir         SwDSParam* pFound = FindDSData(aData, sal_False);
2136cdf0e10cSrcweir 		if(pFound && pFound->xResultSet.is())
2137cdf0e10cSrcweir 		{
2138cdf0e10cSrcweir 			try
2139cdf0e10cSrcweir 			{	//if a selection array is set the current row at the result set may not be set yet
2140cdf0e10cSrcweir 				if(pFound->aSelection.getLength())
2141cdf0e10cSrcweir 				{
2142cdf0e10cSrcweir 					sal_Int32 nSelIndex = pFound->nSelectionIndex;
2143cdf0e10cSrcweir                     if(nSelIndex >= pFound->aSelection.getLength())
2144cdf0e10cSrcweir 						nSelIndex = pFound->aSelection.getLength() -1;
2145cdf0e10cSrcweir 					pFound->aSelection.getConstArray()[nSelIndex] >>= nRet;
2146cdf0e10cSrcweir 
2147cdf0e10cSrcweir 				}
2148cdf0e10cSrcweir 				else
2149cdf0e10cSrcweir 					nRet = pFound->xResultSet->getRow();
2150cdf0e10cSrcweir 			}
2151cdf0e10cSrcweir 			catch(Exception&){}
2152cdf0e10cSrcweir 		}
2153cdf0e10cSrcweir 	}
2154cdf0e10cSrcweir 	return nRet;
2155cdf0e10cSrcweir }
2156cdf0e10cSrcweir 
2157cdf0e10cSrcweir /* -----------------------------17.07.00 14:18--------------------------------
2158cdf0e10cSrcweir 	close all data sources - after fields were updated
2159cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
CloseAll(sal_Bool bIncludingMerge)2160cdf0e10cSrcweir void	SwNewDBMgr::CloseAll(sal_Bool bIncludingMerge)
2161cdf0e10cSrcweir {
2162cdf0e10cSrcweir     //the only thing done here is to reset the selection index
2163cdf0e10cSrcweir 	//all connections stay open
2164cdf0e10cSrcweir     for(sal_uInt16 nPos = 0; nPos < aDataSourceParams.Count(); nPos++)
2165cdf0e10cSrcweir     {
2166cdf0e10cSrcweir         SwDSParam* pParam = aDataSourceParams[nPos];
2167cdf0e10cSrcweir         if(bIncludingMerge || pParam != pImpl->pMergeData)
2168cdf0e10cSrcweir         {
2169cdf0e10cSrcweir 			pParam->nSelectionIndex = 0;
2170cdf0e10cSrcweir 			pParam->bAfterSelection = sal_False;
2171cdf0e10cSrcweir             pParam->bEndOfDB = sal_False;
2172cdf0e10cSrcweir             try
2173cdf0e10cSrcweir             {
2174cdf0e10cSrcweir                 if(!bInMerge && pParam->xResultSet.is())
2175cdf0e10cSrcweir                     pParam->xResultSet->first();
2176cdf0e10cSrcweir             }
2177cdf0e10cSrcweir             catch(Exception& )
2178cdf0e10cSrcweir             {}
2179cdf0e10cSrcweir         }
2180cdf0e10cSrcweir     }
2181cdf0e10cSrcweir }
2182cdf0e10cSrcweir /* -----------------------------17.07.00 14:54--------------------------------
2183cdf0e10cSrcweir 
2184cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
FindDSData(const SwDBData & rData,sal_Bool bCreate)2185cdf0e10cSrcweir SwDSParam* SwNewDBMgr::FindDSData(const SwDBData& rData, sal_Bool bCreate)
2186cdf0e10cSrcweir {
2187cdf0e10cSrcweir     //prefer merge data if available
2188cdf0e10cSrcweir     if(pImpl->pMergeData && rData.sDataSource == pImpl->pMergeData->sDataSource &&
2189cdf0e10cSrcweir         rData.sCommand == pImpl->pMergeData->sCommand &&
2190cdf0e10cSrcweir         (rData.nCommandType == -1 || rData.nCommandType == pImpl->pMergeData->nCommandType ||
2191cdf0e10cSrcweir         (bCreate && pImpl->pMergeData->nCommandType == -1)))
2192cdf0e10cSrcweir     {
2193cdf0e10cSrcweir          return pImpl->pMergeData;
2194cdf0e10cSrcweir     }
2195cdf0e10cSrcweir 
2196cdf0e10cSrcweir     SwDSParam* pFound = 0;
2197cdf0e10cSrcweir 	for(sal_uInt16 nPos = aDataSourceParams.Count(); nPos; nPos--)
2198cdf0e10cSrcweir 	{
2199cdf0e10cSrcweir 		SwDSParam* pParam = aDataSourceParams[nPos - 1];
2200cdf0e10cSrcweir         if(rData.sDataSource == pParam->sDataSource &&
2201cdf0e10cSrcweir             rData.sCommand == pParam->sCommand &&
2202cdf0e10cSrcweir             (rData.nCommandType == -1 || rData.nCommandType == pParam->nCommandType ||
2203cdf0e10cSrcweir             (bCreate && pParam->nCommandType == -1)))
2204cdf0e10cSrcweir 			{
2205cdf0e10cSrcweir                 //#94779# calls from the calculator may add a connection with an invalid commandtype
2206cdf0e10cSrcweir                 //later added "real" data base connections have to re-use the already available
2207cdf0e10cSrcweir                 //DSData and set the correct CommandType
2208cdf0e10cSrcweir                 if(bCreate && pParam->nCommandType == -1)
2209cdf0e10cSrcweir                     pParam->nCommandType = rData.nCommandType;
2210cdf0e10cSrcweir 				pFound = pParam;
2211cdf0e10cSrcweir 				break;
2212cdf0e10cSrcweir 			}
2213cdf0e10cSrcweir 	}
2214cdf0e10cSrcweir 	if(bCreate)
2215cdf0e10cSrcweir 	{
2216cdf0e10cSrcweir 		if(!pFound)
2217cdf0e10cSrcweir 		{
2218cdf0e10cSrcweir             pFound = new SwDSParam(rData);
2219cdf0e10cSrcweir             aDataSourceParams.Insert(pFound, aDataSourceParams.Count());
2220cdf0e10cSrcweir             try
2221cdf0e10cSrcweir             {
2222cdf0e10cSrcweir                 uno::Reference<XComponent> xComponent(pFound->xConnection, UNO_QUERY);
2223cdf0e10cSrcweir                 if(xComponent.is())
2224cdf0e10cSrcweir                     xComponent->addEventListener(pImpl->xDisposeListener);
2225cdf0e10cSrcweir             }
2226cdf0e10cSrcweir             catch(Exception&)
2227cdf0e10cSrcweir             {
2228cdf0e10cSrcweir             }
2229cdf0e10cSrcweir         }
2230cdf0e10cSrcweir 	}
2231cdf0e10cSrcweir 	return pFound;
2232cdf0e10cSrcweir }
2233cdf0e10cSrcweir /* -----------------------------14.08.2001 10:27------------------------------
2234cdf0e10cSrcweir 
2235cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
2236cdf0e10cSrcweir 
FindDSConnection(const rtl::OUString & rDataSource,sal_Bool bCreate)2237cdf0e10cSrcweir SwDSParam*  SwNewDBMgr::FindDSConnection(const rtl::OUString& rDataSource, sal_Bool bCreate)
2238cdf0e10cSrcweir {
2239cdf0e10cSrcweir     //prefer merge data if available
2240cdf0e10cSrcweir     if(pImpl->pMergeData && rDataSource == pImpl->pMergeData->sDataSource )
2241cdf0e10cSrcweir     {
2242cdf0e10cSrcweir          return pImpl->pMergeData;
2243cdf0e10cSrcweir     }
2244cdf0e10cSrcweir 	SwDSParam* pFound = 0;
2245cdf0e10cSrcweir 	for(sal_uInt16 nPos = 0; nPos < aDataSourceParams.Count(); nPos++)
2246cdf0e10cSrcweir 	{
2247cdf0e10cSrcweir 		SwDSParam* pParam = aDataSourceParams[nPos];
2248cdf0e10cSrcweir         if(rDataSource == pParam->sDataSource)
2249cdf0e10cSrcweir         {
2250cdf0e10cSrcweir             pFound = pParam;
2251cdf0e10cSrcweir             break;
2252cdf0e10cSrcweir         }
2253cdf0e10cSrcweir 	}
2254cdf0e10cSrcweir 	if(bCreate && !pFound)
2255cdf0e10cSrcweir 	{
2256cdf0e10cSrcweir         SwDBData aData;
2257cdf0e10cSrcweir         aData.sDataSource = rDataSource;
2258cdf0e10cSrcweir         pFound = new SwDSParam(aData);
2259cdf0e10cSrcweir 		aDataSourceParams.Insert(pFound, aDataSourceParams.Count());
2260cdf0e10cSrcweir         try
2261cdf0e10cSrcweir         {
2262cdf0e10cSrcweir             uno::Reference<XComponent> xComponent(pFound->xConnection, UNO_QUERY);
2263cdf0e10cSrcweir             if(xComponent.is())
2264cdf0e10cSrcweir                 xComponent->addEventListener(pImpl->xDisposeListener);
2265cdf0e10cSrcweir         }
2266cdf0e10cSrcweir         catch(Exception&)
2267cdf0e10cSrcweir         {
2268cdf0e10cSrcweir         }
2269cdf0e10cSrcweir     }
2270cdf0e10cSrcweir 	return pFound;
2271cdf0e10cSrcweir }
2272cdf0e10cSrcweir 
2273cdf0e10cSrcweir /* -----------------------------17.07.00 14:34--------------------------------
2274cdf0e10cSrcweir 
2275cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetAddressDBName()2276cdf0e10cSrcweir const SwDBData&	SwNewDBMgr::GetAddressDBName()
2277cdf0e10cSrcweir {
2278cdf0e10cSrcweir 	return SW_MOD()->GetDBConfig()->GetAddressSource();
2279cdf0e10cSrcweir }
2280cdf0e10cSrcweir /* -----------------------------18.07.00 13:13--------------------------------
2281cdf0e10cSrcweir 
2282cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
GetExistingDatabaseNames()2283cdf0e10cSrcweir Sequence<rtl::OUString> SwNewDBMgr::GetExistingDatabaseNames()
2284cdf0e10cSrcweir {
2285cdf0e10cSrcweir     uno::Reference<XNameAccess> xDBContext;
2286cdf0e10cSrcweir     uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
2287cdf0e10cSrcweir 	if( xMgr.is() )
2288cdf0e10cSrcweir 	{
2289cdf0e10cSrcweir         uno::Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
2290cdf0e10cSrcweir         xDBContext = uno::Reference<XNameAccess>(xInstance, UNO_QUERY) ;
2291cdf0e10cSrcweir 	}
2292cdf0e10cSrcweir 	if(xDBContext.is())
2293cdf0e10cSrcweir 	{
2294cdf0e10cSrcweir 		return xDBContext->getElementNames();
2295cdf0e10cSrcweir 	}
2296cdf0e10cSrcweir     return Sequence<rtl::OUString>();
2297cdf0e10cSrcweir }
2298cdf0e10cSrcweir /*-- 26.05.2004 14:33:13---------------------------------------------------
2299cdf0e10cSrcweir 
2300cdf0e10cSrcweir   -----------------------------------------------------------------------*/
LoadAndRegisterDataSource()2301cdf0e10cSrcweir String SwNewDBMgr::LoadAndRegisterDataSource()
2302cdf0e10cSrcweir {
2303cdf0e10cSrcweir     sfx2::FileDialogHelper aDlgHelper( TemplateDescription::FILEOPEN_SIMPLE, 0 );
2304cdf0e10cSrcweir     Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
2305cdf0e10cSrcweir 
2306cdf0e10cSrcweir     String sHomePath(SvtPathOptions().GetWorkPath());
2307cdf0e10cSrcweir     aDlgHelper.SetDisplayDirectory( sHomePath );
2308cdf0e10cSrcweir 
2309cdf0e10cSrcweir     Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
2310cdf0e10cSrcweir 
2311cdf0e10cSrcweir     String sFilterAll(SW_RES(STR_FILTER_ALL));
2312cdf0e10cSrcweir     String sFilterAllData(SW_RES(STR_FILTER_ALL_DATA));
2313cdf0e10cSrcweir     String sFilterSXB(SW_RES(STR_FILTER_SXB));
2314cdf0e10cSrcweir     String sFilterSXC(SW_RES(STR_FILTER_SXC));
2315cdf0e10cSrcweir     String sFilterDBF(SW_RES(STR_FILTER_DBF));
2316cdf0e10cSrcweir     String sFilterXLS(SW_RES(STR_FILTER_XLS));
2317cdf0e10cSrcweir     String sFilterTXT(SW_RES(STR_FILTER_TXT));
2318cdf0e10cSrcweir     String sFilterCSV(SW_RES(STR_FILTER_CSV));
2319cdf0e10cSrcweir #ifdef WNT
2320cdf0e10cSrcweir     String sFilterMDB(SW_RES(STR_FILTER_MDB));
2321cdf0e10cSrcweir     String sFilterACCDB(SW_RES(STR_FILTER_ACCDB));
2322cdf0e10cSrcweir #endif
2323cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterAll, C2U("*") );
2324cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterAllData, C2U("*.ods;*.sxc;*.dbf;*.xls;*.txt;*.csv"));
2325cdf0e10cSrcweir 
2326cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterSXB, C2U("*.odb") );
2327cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterSXC, C2U("*.ods;*.sxc") );
2328cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterDBF, C2U("*.dbf") );
2329cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterXLS, C2U("*.xls") );
2330cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterTXT, C2U("*.txt") );
2331cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterCSV, C2U("*.csv") );
2332cdf0e10cSrcweir #ifdef WNT
2333cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterMDB, C2U("*.mdb") );
2334cdf0e10cSrcweir     xFltMgr->appendFilter( sFilterACCDB, C2U("*.accdb") );
2335cdf0e10cSrcweir #endif
2336cdf0e10cSrcweir 
2337cdf0e10cSrcweir     xFltMgr->setCurrentFilter( sFilterAll ) ;
2338cdf0e10cSrcweir     String sFind;
2339cdf0e10cSrcweir     bool bTextConnection = false;
2340cdf0e10cSrcweir     if( ERRCODE_NONE == aDlgHelper.Execute() )
2341cdf0e10cSrcweir     {
2342cdf0e10cSrcweir         String sURL = xFP->getFiles().getConstArray()[0];
2343cdf0e10cSrcweir         //data sources have to be registered depending on their extensions
2344cdf0e10cSrcweir         INetURLObject aURL( sURL );
2345cdf0e10cSrcweir         String sExt( aURL.GetExtension() );
2346cdf0e10cSrcweir         Any aURLAny;
2347cdf0e10cSrcweir         Any aTableFilterAny;
2348cdf0e10cSrcweir         Any aSuppressVersionsAny;
2349cdf0e10cSrcweir         Any aInfoAny;
2350cdf0e10cSrcweir         INetURLObject aTempURL(aURL);
2351cdf0e10cSrcweir         bool bStore = true;
2352cdf0e10cSrcweir         if(sExt.EqualsAscii("odb"))
2353cdf0e10cSrcweir         {
2354cdf0e10cSrcweir             bStore = false;
2355cdf0e10cSrcweir         }
2356cdf0e10cSrcweir         else if(sExt.EqualsIgnoreCaseAscii("sxc")
2357cdf0e10cSrcweir             || sExt.EqualsIgnoreCaseAscii("ods")
2358cdf0e10cSrcweir                 || sExt.EqualsIgnoreCaseAscii("xls"))
2359cdf0e10cSrcweir         {
2360cdf0e10cSrcweir             rtl::OUString sDBURL(C2U("sdbc:calc:"));
2361cdf0e10cSrcweir             sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
2362cdf0e10cSrcweir             aURLAny <<= sDBURL;
2363cdf0e10cSrcweir         }
2364cdf0e10cSrcweir         else if(sExt.EqualsIgnoreCaseAscii("dbf"))
2365cdf0e10cSrcweir         {
2366cdf0e10cSrcweir             aTempURL.removeSegment();
2367cdf0e10cSrcweir             aTempURL.removeFinalSlash();
2368cdf0e10cSrcweir             rtl::OUString sDBURL(C2U("sdbc:dbase:"));
2369cdf0e10cSrcweir             sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
2370cdf0e10cSrcweir             aURLAny <<= sDBURL;
2371cdf0e10cSrcweir             //set the filter to the file name without extension
2372cdf0e10cSrcweir             Sequence<rtl::OUString> aFilters(1);
2373cdf0e10cSrcweir             rtl::OUString sTmp(aURL.getBase());
2374cdf0e10cSrcweir             aFilters[0] = aURL.getBase();
2375cdf0e10cSrcweir             aTableFilterAny <<= aFilters;
2376cdf0e10cSrcweir         }
2377cdf0e10cSrcweir         else if(sExt.EqualsIgnoreCaseAscii("csv") || sExt.EqualsIgnoreCaseAscii("txt"))
2378cdf0e10cSrcweir         {
2379cdf0e10cSrcweir             aTempURL.removeSegment();
2380cdf0e10cSrcweir             aTempURL.removeFinalSlash();
2381cdf0e10cSrcweir             rtl::OUString sDBURL(C2U("sdbc:flat:"));
2382cdf0e10cSrcweir             //only the 'path' has to be added
2383cdf0e10cSrcweir             sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
2384cdf0e10cSrcweir             aURLAny <<= sDBURL;
2385cdf0e10cSrcweir 
2386cdf0e10cSrcweir             bTextConnection = true;
2387cdf0e10cSrcweir             //set the filter to the file name without extension
2388cdf0e10cSrcweir             Sequence<rtl::OUString> aFilters(1);
2389cdf0e10cSrcweir             rtl::OUString sTmp(aURL.getBase());
2390cdf0e10cSrcweir             aFilters[0] = aURL.getBase();
2391cdf0e10cSrcweir             aTableFilterAny <<= aFilters;
2392cdf0e10cSrcweir         }
2393cdf0e10cSrcweir #ifdef WNT
2394cdf0e10cSrcweir         else if(sExt.EqualsIgnoreCaseAscii("mdb"))
2395cdf0e10cSrcweir         {
2396cdf0e10cSrcweir             rtl::OUString sDBURL(C2U("sdbc:ado:access:PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="));
2397cdf0e10cSrcweir             sDBURL += aTempURL.PathToFileName();
2398cdf0e10cSrcweir             aURLAny <<= sDBURL;
2399cdf0e10cSrcweir             aSuppressVersionsAny <<= makeAny(true);
2400cdf0e10cSrcweir         }
2401cdf0e10cSrcweir         else if(sExt.EqualsIgnoreCaseAscii("accdb"))
2402cdf0e10cSrcweir         {
2403cdf0e10cSrcweir             rtl::OUString sDBURL(C2U("sdbc:ado:PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE="));
2404cdf0e10cSrcweir             sDBURL += aTempURL.PathToFileName();
2405cdf0e10cSrcweir             aURLAny <<= sDBURL;
2406cdf0e10cSrcweir             aSuppressVersionsAny <<= makeAny(true);
2407cdf0e10cSrcweir         }
2408cdf0e10cSrcweir #endif
2409cdf0e10cSrcweir         try
2410cdf0e10cSrcweir         {
2411cdf0e10cSrcweir             Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
2412cdf0e10cSrcweir             Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
2413cdf0e10cSrcweir             Reference<XNameAccess> xDBContext(xInstance, UNO_QUERY_THROW);
2414cdf0e10cSrcweir             Reference<XSingleServiceFactory> xFact( xDBContext, UNO_QUERY);
2415cdf0e10cSrcweir 
2416cdf0e10cSrcweir 			String sNewName = INetURLObject::decode( aURL.getName(),
2417cdf0e10cSrcweir 													 INET_HEX_ESCAPE,
2418cdf0e10cSrcweir 													 INetURLObject::DECODE_UNAMBIGUOUS,
2419cdf0e10cSrcweir 													 RTL_TEXTENCODING_UTF8 );
2420cdf0e10cSrcweir             xub_StrLen nExtLen = static_cast< xub_StrLen >(aURL.GetExtension().getLength());
2421cdf0e10cSrcweir             sNewName.Erase( sNewName.Len() - nExtLen - 1, nExtLen + 1 );
2422cdf0e10cSrcweir 
2423cdf0e10cSrcweir             //find a unique name if sNewName already exists
2424cdf0e10cSrcweir             sFind = sNewName;
2425cdf0e10cSrcweir             sal_Int32 nIndex = 0;
2426cdf0e10cSrcweir             while(xDBContext->hasByName(sFind))
2427cdf0e10cSrcweir             {
2428cdf0e10cSrcweir                 sFind = sNewName;
2429cdf0e10cSrcweir                 sFind += String::CreateFromInt32(++nIndex);
2430cdf0e10cSrcweir             }
2431cdf0e10cSrcweir 
2432cdf0e10cSrcweir             Reference<XInterface> xNewInstance;
2433cdf0e10cSrcweir             if(!bStore)
2434cdf0e10cSrcweir             {
2435cdf0e10cSrcweir                 //odb-file
2436cdf0e10cSrcweir                 Any aDataSource = xDBContext->getByName(aTempURL.GetMainURL(INetURLObject::NO_DECODE));
2437cdf0e10cSrcweir                 aDataSource >>= xNewInstance;
2438cdf0e10cSrcweir             }
2439cdf0e10cSrcweir             else
2440cdf0e10cSrcweir             {
2441cdf0e10cSrcweir                 xNewInstance = xFact->createInstance();
2442cdf0e10cSrcweir                 Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
2443cdf0e10cSrcweir 
2444cdf0e10cSrcweir                 if(aURLAny.hasValue())
2445cdf0e10cSrcweir                     xDataProperties->setPropertyValue(C2U("URL"), aURLAny);
2446cdf0e10cSrcweir                 if(aTableFilterAny.hasValue())
2447cdf0e10cSrcweir                     xDataProperties->setPropertyValue(C2U("TableFilter"), aTableFilterAny);
2448cdf0e10cSrcweir                 if(aSuppressVersionsAny.hasValue())
2449cdf0e10cSrcweir                     xDataProperties->setPropertyValue(C2U("SuppressVersionColumns"), aSuppressVersionsAny);
2450cdf0e10cSrcweir                 if(aInfoAny.hasValue())
2451cdf0e10cSrcweir                     xDataProperties->setPropertyValue(C2U("Info"), aInfoAny);
2452cdf0e10cSrcweir 
2453cdf0e10cSrcweir                 if( bTextConnection )
2454cdf0e10cSrcweir                 {
2455cdf0e10cSrcweir                     uno::Reference < ui::dialogs::XExecutableDialog > xSettingsDlg(
2456cdf0e10cSrcweir                                 xMgr->createInstance( C2U( "com.sun.star.sdb.TextConnectionSettings" ) ), uno::UNO_QUERY);
2457cdf0e10cSrcweir                     if( xSettingsDlg->execute() )
2458cdf0e10cSrcweir                     {
2459cdf0e10cSrcweir                         uno::Any aSettings = xDataProperties->getPropertyValue( C2U( "Settings" ) );
2460cdf0e10cSrcweir                         uno::Reference < beans::XPropertySet > xDSSettings;
2461cdf0e10cSrcweir                         aSettings >>= xDSSettings;
2462cdf0e10cSrcweir                         ::comphelper::copyProperties(
2463cdf0e10cSrcweir                             uno::Reference < beans::XPropertySet >( xSettingsDlg, uno::UNO_QUERY ),
2464cdf0e10cSrcweir                             xDSSettings );
2465cdf0e10cSrcweir                         xDSSettings->setPropertyValue( C2U("Extension"), uno::makeAny( ::rtl::OUString( sExt )));
2466cdf0e10cSrcweir                     }
2467cdf0e10cSrcweir                 }
2468cdf0e10cSrcweir 
2469cdf0e10cSrcweir                 Reference<XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
2470cdf0e10cSrcweir                 Reference<XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
2471cdf0e10cSrcweir                 String sOutputExt = String::CreateFromAscii(".odb");
2472cdf0e10cSrcweir                 String sTmpName;
2473cdf0e10cSrcweir                 {
2474cdf0e10cSrcweir                     utl::TempFile aTempFile(sNewName , &sOutputExt, &sHomePath);
2475cdf0e10cSrcweir                     aTempFile.EnableKillingFile(sal_True);
2476cdf0e10cSrcweir                     sTmpName = aTempFile.GetURL();
2477cdf0e10cSrcweir                 }
2478cdf0e10cSrcweir                 xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
2479cdf0e10cSrcweir             }
2480cdf0e10cSrcweir             Reference<XNamingService> xNaming(xDBContext, UNO_QUERY);
2481cdf0e10cSrcweir             xNaming->registerObject( sFind, xNewInstance );
2482cdf0e10cSrcweir 
2483cdf0e10cSrcweir         }
2484cdf0e10cSrcweir         catch(Exception&)
2485cdf0e10cSrcweir         {
2486cdf0e10cSrcweir         }
2487cdf0e10cSrcweir     }
2488cdf0e10cSrcweir     return sFind;
2489cdf0e10cSrcweir 
2490cdf0e10cSrcweir }
2491cdf0e10cSrcweir /* -----------------------------10.11.00 17:10--------------------------------
2492cdf0e10cSrcweir 
2493cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
ExecuteFormLetter(SwWrtShell & rSh,const Sequence<PropertyValue> & rProperties,sal_Bool bWithDataSourceBrowser)2494cdf0e10cSrcweir void SwNewDBMgr::ExecuteFormLetter(	SwWrtShell& rSh,
2495cdf0e10cSrcweir                         const Sequence<PropertyValue>& rProperties,
2496cdf0e10cSrcweir                         sal_Bool bWithDataSourceBrowser)
2497cdf0e10cSrcweir {
2498cdf0e10cSrcweir 	//prevent second call
2499cdf0e10cSrcweir     if(pImpl->pMergeDialog)
2500cdf0e10cSrcweir 		return ;
2501cdf0e10cSrcweir     rtl::OUString sDataSource, sDataTableOrQuery;
2502cdf0e10cSrcweir     Sequence<Any> aSelection;
2503cdf0e10cSrcweir 
250460e513e9SAriel Constenla-Haile 	sal_Int32 nCmdType = CommandType::TABLE;
2505cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
2506cdf0e10cSrcweir 
2507cdf0e10cSrcweir 	ODataAccessDescriptor aDescriptor(rProperties);
2508cdf0e10cSrcweir 	sDataSource = aDescriptor.getDataSource();
2509cdf0e10cSrcweir 	aDescriptor[daCommand]		>>= sDataTableOrQuery;
2510cdf0e10cSrcweir 	aDescriptor[daCommandType]	>>= nCmdType;
2511cdf0e10cSrcweir 
2512cdf0e10cSrcweir 	if ( aDescriptor.has(daSelection) )
2513cdf0e10cSrcweir 		aDescriptor[daSelection] >>= aSelection;
2514cdf0e10cSrcweir 	if ( aDescriptor.has(daConnection) )
2515cdf0e10cSrcweir 		aDescriptor[daConnection] >>= xConnection;
2516cdf0e10cSrcweir 
2517cdf0e10cSrcweir     if(!sDataSource.getLength() || !sDataTableOrQuery.getLength())
2518cdf0e10cSrcweir 	{
2519cdf0e10cSrcweir 		DBG_ERROR("PropertyValues missing or unset");
2520cdf0e10cSrcweir 		return;
2521cdf0e10cSrcweir 	}
2522cdf0e10cSrcweir 
2523cdf0e10cSrcweir     //always create a connection for the dialog and dispose it after the dialog has been closed
2524cdf0e10cSrcweir     SwDSParam* pFound = 0;
2525cdf0e10cSrcweir     if(!xConnection.is())
2526cdf0e10cSrcweir     {
2527cdf0e10cSrcweir         xConnection = SwNewDBMgr::RegisterConnection(sDataSource);
2528cdf0e10cSrcweir         pFound = FindDSConnection(sDataSource, sal_True);
2529cdf0e10cSrcweir     }
2530cdf0e10cSrcweir 	SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
2531cdf0e10cSrcweir     DBG_ASSERT(pFact, "Dialogdiet fail!");
2532cdf0e10cSrcweir     pImpl->pMergeDialog = pFact->CreateMailMergeDlg( DLG_MAILMERGE,
2533cdf0e10cSrcweir 														&rSh.GetView().GetViewFrame()->GetWindow(), rSh,
2534cdf0e10cSrcweir 														sDataSource,
2535cdf0e10cSrcweir 														sDataTableOrQuery,
2536cdf0e10cSrcweir 														nCmdType,
2537cdf0e10cSrcweir 														xConnection,
2538cdf0e10cSrcweir 														bWithDataSourceBrowser ? 0 : &aSelection);
2539cdf0e10cSrcweir     DBG_ASSERT(pImpl->pMergeDialog, "Dialogdiet fail!");
2540cdf0e10cSrcweir     if(pImpl->pMergeDialog->Execute() == RET_OK)
2541cdf0e10cSrcweir 	{
2542cdf0e10cSrcweir 		aDescriptor[daSelection] <<= pImpl->pMergeDialog->GetSelection();
2543cdf0e10cSrcweir 
2544cdf0e10cSrcweir         uno::Reference<XResultSet> xResSet = pImpl->pMergeDialog->GetResultSet();
2545cdf0e10cSrcweir         if(xResSet.is())
2546cdf0e10cSrcweir 			aDescriptor[daCursor] <<= xResSet;
2547cdf0e10cSrcweir 
2548cdf0e10cSrcweir         // SfxObjectShellRef is ok, since there should be no control over the document lifetime here
2549cdf0e10cSrcweir         SfxObjectShellRef xDocShell = rSh.GetView().GetViewFrame()->GetObjectShell();
2550cdf0e10cSrcweir         SFX_APP()->NotifyEvent(SfxEventHint(SW_EVENT_MAIL_MERGE, SwDocShell::GetEventName(STR_SW_EVENT_MAIL_MERGE), xDocShell));
2551cdf0e10cSrcweir         {
2552cdf0e10cSrcweir             //copy rSh to aTempFile
2553cdf0e10cSrcweir             ::rtl::OUString sTempURL;
2554cdf0e10cSrcweir             const SfxFilter *pSfxFlt = SwIoSystem::GetFilterOfFormat(
2555cdf0e10cSrcweir                         String::CreateFromAscii( FILTER_XML ),
2556cdf0e10cSrcweir                         SwDocShell::Factory().GetFilterContainer() );
2557cdf0e10cSrcweir             try
2558cdf0e10cSrcweir             {
2559cdf0e10cSrcweir 
2560cdf0e10cSrcweir                 uno::Sequence< beans::PropertyValue > aValues(1);
2561cdf0e10cSrcweir                 beans::PropertyValue* pValues = aValues.getArray();
2562cdf0e10cSrcweir                 pValues[0].Name = C2U("FilterName");
2563cdf0e10cSrcweir                 pValues[0].Value <<= ::rtl::OUString(pSfxFlt->GetFilterName());
2564cdf0e10cSrcweir                 uno::Reference< frame::XStorable > xStore( xDocShell->GetModel(), uno::UNO_QUERY);
2565cdf0e10cSrcweir                 sTempURL = URIHelper::SmartRel2Abs( INetURLObject(), utl::TempFile::CreateTempName() );
2566cdf0e10cSrcweir                 xStore->storeToURL( sTempURL, aValues );
2567cdf0e10cSrcweir             }
2568cdf0e10cSrcweir             catch( const uno::Exception& rEx )
2569cdf0e10cSrcweir             {
2570cdf0e10cSrcweir                 (void) rEx;
2571cdf0e10cSrcweir             }
2572cdf0e10cSrcweir             if( xDocShell->GetError() )
2573cdf0e10cSrcweir             {
2574cdf0e10cSrcweir                 // error message ??
2575cdf0e10cSrcweir                 ErrorHandler::HandleError( xDocShell->GetError() );
2576cdf0e10cSrcweir             }
2577cdf0e10cSrcweir             else
2578cdf0e10cSrcweir             {
2579cdf0e10cSrcweir                 // the shell will be explicitly closed, but it is more safe to use SfxObjectShellLock here
2580cdf0e10cSrcweir                 // especially for the case that the loading has failed
2581cdf0e10cSrcweir                 SfxObjectShellLock xWorkDocSh( new SwDocShell( SFX_CREATE_MODE_INTERNAL ));
2582cdf0e10cSrcweir                 SfxMedium* pWorkMed = new SfxMedium( sTempURL, STREAM_STD_READ, sal_True );
2583cdf0e10cSrcweir                 pWorkMed->SetFilter( pSfxFlt );
2584cdf0e10cSrcweir                 if( xWorkDocSh->DoLoad(pWorkMed) )
2585cdf0e10cSrcweir                 {
2586cdf0e10cSrcweir                     SfxViewFrame *pFrame = SfxViewFrame::LoadHiddenDocument( *xWorkDocSh, 0 );
2587cdf0e10cSrcweir                     SwView *pView = (SwView*) pFrame->GetViewShell();
2588cdf0e10cSrcweir                     pView->AttrChangedNotify( &pView->GetWrtShell() );//Damit SelectShell gerufen wird.
2589cdf0e10cSrcweir                     //set the current DBMgr
2590cdf0e10cSrcweir                     SwDoc* pWorkDoc = pView->GetWrtShell().GetDoc();
2591cdf0e10cSrcweir                     SwNewDBMgr* pWorkDBMgr = pWorkDoc->GetNewDBMgr();
2592cdf0e10cSrcweir                     pWorkDoc->SetNewDBMgr( this );
2593cdf0e10cSrcweir 
2594cdf0e10cSrcweir                     SwMergeDescriptor aMergeDesc( pImpl->pMergeDialog->GetMergeType(), pView->GetWrtShell(), aDescriptor );
2595cdf0e10cSrcweir                     aMergeDesc.sSaveToFilter = pImpl->pMergeDialog->GetSaveFilter();
2596cdf0e10cSrcweir                     aMergeDesc.bCreateSingleFile = !pImpl->pMergeDialog->IsSaveIndividualDocs();
2597cdf0e10cSrcweir                     if( !aMergeDesc.bCreateSingleFile && pImpl->pMergeDialog->IsGenerateFromDataBase() )
2598cdf0e10cSrcweir                     {
2599cdf0e10cSrcweir                         aMergeDesc.sAddressFromColumn = pImpl->pMergeDialog->GetColumnName();
2600cdf0e10cSrcweir                         aMergeDesc.sSubject = pImpl->pMergeDialog->GetPath();
2601cdf0e10cSrcweir                     }
2602cdf0e10cSrcweir 
2603cdf0e10cSrcweir                     MergeNew(aMergeDesc);
2604cdf0e10cSrcweir 
2605cdf0e10cSrcweir                     pWorkDoc->SetNewDBMgr( pWorkDBMgr );
2606cdf0e10cSrcweir                     //close the temporary file
2607cdf0e10cSrcweir                     uno::Reference< util::XCloseable > xClose( xWorkDocSh->GetModel(), uno::UNO_QUERY );
2608cdf0e10cSrcweir                     if (xClose.is())
2609cdf0e10cSrcweir                     {
2610cdf0e10cSrcweir                         try
2611cdf0e10cSrcweir                         {
2612cdf0e10cSrcweir                             //! 'sal_True' -> transfer ownership to vetoing object if vetoed!
2613cdf0e10cSrcweir                             //! I.e. now that object is responsible for closing the model and doc shell.
2614cdf0e10cSrcweir                             xClose->close( sal_True );
2615cdf0e10cSrcweir                         }
2616cdf0e10cSrcweir                         catch ( const uno::Exception& )
2617cdf0e10cSrcweir                         {
2618cdf0e10cSrcweir                         }
2619cdf0e10cSrcweir                     }
2620cdf0e10cSrcweir                 }
2621cdf0e10cSrcweir             }
2622cdf0e10cSrcweir             //remove the temporary file
2623cdf0e10cSrcweir             SWUnoHelper::UCB_DeleteFile( sTempURL );
2624cdf0e10cSrcweir         }
2625cdf0e10cSrcweir         SFX_APP()->NotifyEvent(SfxEventHint(SW_EVENT_MAIL_MERGE_END, SwDocShell::GetEventName(STR_SW_EVENT_MAIL_MERGE_END), rSh.GetView().GetViewFrame()->GetObjectShell()));
2626cdf0e10cSrcweir 
2627cdf0e10cSrcweir 		// reset the cursor inside
2628cdf0e10cSrcweir 		xResSet = NULL;
2629cdf0e10cSrcweir 		aDescriptor[daCursor] <<= xResSet;
2630cdf0e10cSrcweir 	}
2631cdf0e10cSrcweir     if(pFound)
2632cdf0e10cSrcweir     {
2633cdf0e10cSrcweir         for(sal_uInt16 nPos = 0; nPos < aDataSourceParams.Count(); nPos++)
2634cdf0e10cSrcweir         {
2635cdf0e10cSrcweir             SwDSParam* pParam = aDataSourceParams[nPos];
2636cdf0e10cSrcweir             if(pParam == pFound)
2637cdf0e10cSrcweir             {
2638cdf0e10cSrcweir                 try
2639cdf0e10cSrcweir                 {
2640cdf0e10cSrcweir                     uno::Reference<XComponent> xComp(pParam->xConnection, UNO_QUERY);
2641cdf0e10cSrcweir                     if(xComp.is())
2642cdf0e10cSrcweir                         xComp->dispose();
2643cdf0e10cSrcweir                 }
2644cdf0e10cSrcweir                 catch(const RuntimeException& )
2645cdf0e10cSrcweir                 {
2646cdf0e10cSrcweir                     //may be disposed already since multiple entries may have used the same connection
2647cdf0e10cSrcweir                 }
2648cdf0e10cSrcweir                 break;
2649cdf0e10cSrcweir             }
2650cdf0e10cSrcweir             //pFound doesn't need to be removed/deleted -
2651cdf0e10cSrcweir             //this has been done by the SwConnectionDisposedListener_Impl already
2652cdf0e10cSrcweir         }
2653cdf0e10cSrcweir     }
2654cdf0e10cSrcweir     DELETEZ(pImpl->pMergeDialog);
2655cdf0e10cSrcweir }
2656cdf0e10cSrcweir /* -----------------------------13.11.00 08:20--------------------------------
2657cdf0e10cSrcweir 
2658cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
InsertText(SwWrtShell & rSh,const Sequence<PropertyValue> & rProperties)2659cdf0e10cSrcweir void SwNewDBMgr::InsertText(SwWrtShell& rSh,
2660cdf0e10cSrcweir 						const Sequence< PropertyValue>& rProperties)
2661cdf0e10cSrcweir {
2662cdf0e10cSrcweir     rtl::OUString sDataSource, sDataTableOrQuery;
2663cdf0e10cSrcweir     uno::Reference<XResultSet>  xResSet;
2664cdf0e10cSrcweir     Sequence<Any> aSelection;
2665cdf0e10cSrcweir 	sal_Bool bHasSelectionProperty = sal_False;
2666cdf0e10cSrcweir 	sal_Int32 nSelectionPos = 0;
2667cdf0e10cSrcweir 	sal_Int16 nCmdType = CommandType::TABLE;
2668cdf0e10cSrcweir 	const PropertyValue* pValues = rProperties.getConstArray();
2669cdf0e10cSrcweir     uno::Reference< XConnection> xConnection;
2670cdf0e10cSrcweir     for(sal_Int32 nPos = 0; nPos < rProperties.getLength(); nPos++)
2671cdf0e10cSrcweir 	{
2672cdf0e10cSrcweir         if(pValues[nPos].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(cDataSourceName)))
2673cdf0e10cSrcweir 			pValues[nPos].Value >>= sDataSource;
2674cdf0e10cSrcweir         else if(pValues[nPos].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(cCommand)))
2675cdf0e10cSrcweir 			pValues[nPos].Value >>= sDataTableOrQuery;
2676cdf0e10cSrcweir         else if(pValues[nPos].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(cCursor)))
2677cdf0e10cSrcweir 			pValues[nPos].Value >>= xResSet;
2678cdf0e10cSrcweir         else if(pValues[nPos].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(cSelection)))
2679cdf0e10cSrcweir 		{
2680cdf0e10cSrcweir 			bHasSelectionProperty = sal_True;
2681cdf0e10cSrcweir 			nSelectionPos = nPos;
2682cdf0e10cSrcweir 			pValues[nPos].Value >>= aSelection;
2683cdf0e10cSrcweir 		}
2684cdf0e10cSrcweir         else if(pValues[nPos].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(cCommandType)))
2685cdf0e10cSrcweir 			pValues[nPos].Value >>= nCmdType;
2686cdf0e10cSrcweir         else if(pValues[nPos].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(cActiveConnection)))
2687cdf0e10cSrcweir             pValues[nPos].Value >>= xConnection;
2688cdf0e10cSrcweir     }
2689cdf0e10cSrcweir 	if(!sDataSource.getLength() || !sDataTableOrQuery.getLength() || !xResSet.is())
2690cdf0e10cSrcweir 	{
2691cdf0e10cSrcweir 		DBG_ERROR("PropertyValues missing or unset");
2692cdf0e10cSrcweir 		return;
2693cdf0e10cSrcweir 	}
2694cdf0e10cSrcweir     uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
2695cdf0e10cSrcweir     uno::Reference<XDataSource> xSource;
2696cdf0e10cSrcweir     uno::Reference<XChild> xChild(xConnection, UNO_QUERY);
2697cdf0e10cSrcweir     if(xChild.is())
2698cdf0e10cSrcweir         xSource = uno::Reference<XDataSource>(xChild->getParent(), UNO_QUERY);
2699cdf0e10cSrcweir     if(!xSource.is())
2700cdf0e10cSrcweir         xSource = SwNewDBMgr::GetDbtoolsClient().getDataSource(sDataSource, xMgr);
2701cdf0e10cSrcweir     uno::Reference< XColumnsSupplier > xColSupp( xResSet, UNO_QUERY );
2702cdf0e10cSrcweir 	SwDBData aDBData;
2703cdf0e10cSrcweir 	aDBData.sDataSource = sDataSource;
2704cdf0e10cSrcweir 	aDBData.sCommand = sDataTableOrQuery;
2705cdf0e10cSrcweir 	aDBData.nCommandType = nCmdType;
2706cdf0e10cSrcweir 
2707cdf0e10cSrcweir     SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
2708cdf0e10cSrcweir     DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
2709cdf0e10cSrcweir 
2710cdf0e10cSrcweir 	AbstractSwInsertDBColAutoPilot* pDlg = pFact->CreateSwInsertDBColAutoPilot( rSh.GetView(),
2711cdf0e10cSrcweir 																				xSource,
2712cdf0e10cSrcweir 																				xColSupp,
2713cdf0e10cSrcweir 																				aDBData,
2714cdf0e10cSrcweir                                                                                 DLG_AP_INSERT_DB_SEL );
2715cdf0e10cSrcweir     DBG_ASSERT(pDlg, "Dialogdiet fail!");
2716cdf0e10cSrcweir 	if( RET_OK == pDlg->Execute() )
2717cdf0e10cSrcweir 	{
2718cdf0e10cSrcweir         rtl::OUString sDummy;
2719cdf0e10cSrcweir         if(!xConnection.is())
2720cdf0e10cSrcweir             xConnection = xSource->getConnection(sDummy, sDummy);
2721cdf0e10cSrcweir         try
2722cdf0e10cSrcweir         {
2723cdf0e10cSrcweir             pDlg->DataToDoc( aSelection , xSource, xConnection, xResSet);
2724cdf0e10cSrcweir         }
2725cdf0e10cSrcweir         catch(Exception& )
2726cdf0e10cSrcweir         {
2727cdf0e10cSrcweir             DBG_ERROR("exception caught");
2728cdf0e10cSrcweir         }
2729cdf0e10cSrcweir     }
2730cdf0e10cSrcweir 	delete pDlg;
2731cdf0e10cSrcweir 
2732cdf0e10cSrcweir }
2733cdf0e10cSrcweir /* -----------------------------30.08.2001 12:00------------------------------
2734cdf0e10cSrcweir 
2735cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
2736cdf0e10cSrcweir SwDbtoolsClient* SwNewDBMgr::pDbtoolsClient = NULL;
2737cdf0e10cSrcweir 
GetDbtoolsClient()2738cdf0e10cSrcweir SwDbtoolsClient& SwNewDBMgr::GetDbtoolsClient()
2739cdf0e10cSrcweir {
2740cdf0e10cSrcweir     if ( !pDbtoolsClient )
2741cdf0e10cSrcweir         pDbtoolsClient = new SwDbtoolsClient;
2742cdf0e10cSrcweir     return *pDbtoolsClient;
2743cdf0e10cSrcweir }
2744cdf0e10cSrcweir /* -----------------13.05.2003 15:34-----------------
2745cdf0e10cSrcweir 
2746cdf0e10cSrcweir  --------------------------------------------------*/
RemoveDbtoolsClient()2747cdf0e10cSrcweir void SwNewDBMgr::RemoveDbtoolsClient()
2748cdf0e10cSrcweir {
2749cdf0e10cSrcweir     delete pDbtoolsClient;
2750cdf0e10cSrcweir     pDbtoolsClient = 0;
2751cdf0e10cSrcweir }
2752cdf0e10cSrcweir /* -----------------------------20.08.2002 12:00------------------------------
2753cdf0e10cSrcweir 
2754cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
getDataSourceAsParent(const uno::Reference<XConnection> & _xConnection,const::rtl::OUString & _sDataSourceName)2755cdf0e10cSrcweir uno::Reference<XDataSource> SwNewDBMgr::getDataSourceAsParent(const uno::Reference< XConnection>& _xConnection,const ::rtl::OUString& _sDataSourceName)
2756cdf0e10cSrcweir {
2757cdf0e10cSrcweir     uno::Reference<XDataSource> xSource;
2758cdf0e10cSrcweir 	try
2759cdf0e10cSrcweir 	{
2760cdf0e10cSrcweir         uno::Reference<XChild> xChild(_xConnection, UNO_QUERY);
2761cdf0e10cSrcweir 		if ( xChild.is() )
2762cdf0e10cSrcweir             xSource = uno::Reference<XDataSource>(xChild->getParent(), UNO_QUERY);
2763cdf0e10cSrcweir 		if ( !xSource.is() )
2764cdf0e10cSrcweir 			xSource = SwNewDBMgr::GetDbtoolsClient().getDataSource(_sDataSourceName, ::comphelper::getProcessServiceFactory());
2765cdf0e10cSrcweir 	}
2766cdf0e10cSrcweir 	catch(const Exception&)
2767cdf0e10cSrcweir 	{
2768cdf0e10cSrcweir 		DBG_ERROR("exception in getDataSourceAsParent caught");
2769cdf0e10cSrcweir 	}
2770cdf0e10cSrcweir 	return xSource;
2771cdf0e10cSrcweir }
2772cdf0e10cSrcweir /* -----------------------------20.08.2002 12:00------------------------------
2773cdf0e10cSrcweir 
2774cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
createCursor(const::rtl::OUString & _sDataSourceName,const::rtl::OUString & _sCommand,sal_Int32 _nCommandType,const uno::Reference<XConnection> & _xConnection)2775cdf0e10cSrcweir uno::Reference<XResultSet> SwNewDBMgr::createCursor(const ::rtl::OUString& _sDataSourceName,
2776cdf0e10cSrcweir 									   const ::rtl::OUString& _sCommand,
2777cdf0e10cSrcweir 									   sal_Int32 _nCommandType,
2778cdf0e10cSrcweir                                        const uno::Reference<XConnection>& _xConnection
2779cdf0e10cSrcweir 									  )
2780cdf0e10cSrcweir {
2781cdf0e10cSrcweir     uno::Reference<XResultSet> xResultSet;
2782cdf0e10cSrcweir 	try
2783cdf0e10cSrcweir 	{
2784cdf0e10cSrcweir         uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
2785cdf0e10cSrcweir 		if( xMgr.is() )
2786cdf0e10cSrcweir 		{
2787cdf0e10cSrcweir             uno::Reference<XInterface> xInstance = xMgr->createInstance(
2788cdf0e10cSrcweir 				C2U( "com.sun.star.sdb.RowSet" ));
2789cdf0e10cSrcweir             uno::Reference<XPropertySet> xRowSetPropSet(xInstance, UNO_QUERY);
2790cdf0e10cSrcweir 			if(xRowSetPropSet.is())
2791cdf0e10cSrcweir 			{
2792cdf0e10cSrcweir 				xRowSetPropSet->setPropertyValue(C2U("DataSourceName"), makeAny(_sDataSourceName));
2793cdf0e10cSrcweir 				xRowSetPropSet->setPropertyValue(C2U("ActiveConnection"), makeAny(_xConnection));
2794cdf0e10cSrcweir 				xRowSetPropSet->setPropertyValue(C2U("Command"), makeAny(_sCommand));
2795cdf0e10cSrcweir 				xRowSetPropSet->setPropertyValue(C2U("CommandType"), makeAny(_nCommandType));
2796cdf0e10cSrcweir 
2797cdf0e10cSrcweir                 uno::Reference< XCompletedExecution > xRowSet(xInstance, UNO_QUERY);
2798cdf0e10cSrcweir 
2799cdf0e10cSrcweir 				if ( xRowSet.is() )
2800cdf0e10cSrcweir 				{
2801cdf0e10cSrcweir                     uno::Reference< XInteractionHandler > xHandler(xMgr->createInstance(C2U("com.sun.star.task.InteractionHandler")), UNO_QUERY);
2802cdf0e10cSrcweir 					xRowSet->executeWithCompletion(xHandler);
2803cdf0e10cSrcweir 				}
2804cdf0e10cSrcweir                 xResultSet = uno::Reference<XResultSet>(xRowSet, UNO_QUERY);
2805cdf0e10cSrcweir 			}
2806cdf0e10cSrcweir 		}
2807cdf0e10cSrcweir 	}
2808cdf0e10cSrcweir 	catch(const Exception&)
2809cdf0e10cSrcweir 	{
2810cdf0e10cSrcweir 		DBG_ASSERT(0,"Caught exception while creating a new RowSet!");
2811cdf0e10cSrcweir 	}
2812cdf0e10cSrcweir 	return xResultSet;
2813cdf0e10cSrcweir }
2814cdf0e10cSrcweir /*-- 13.05.2004 16:14:15---------------------------------------------------
2815cdf0e10cSrcweir     merge all data into one resulting document and return the number of
2816cdf0e10cSrcweir     merged documents
2817cdf0e10cSrcweir   -----------------------------------------------------------------------*/
2818cdf0e10cSrcweir 
MergeDocuments(SwMailMergeConfigItem & rMMConfig,SwView & rSourceView)2819cdf0e10cSrcweir sal_Int32 SwNewDBMgr::MergeDocuments( SwMailMergeConfigItem& rMMConfig,
2820cdf0e10cSrcweir                             SwView& rSourceView )
2821cdf0e10cSrcweir {
2822cdf0e10cSrcweir     // check the availability of all data in the config item
2823cdf0e10cSrcweir     uno::Reference< XResultSet> xResultSet = rMMConfig.GetResultSet();
2824cdf0e10cSrcweir     if(!xResultSet.is())
2825cdf0e10cSrcweir         return false;
2826cdf0e10cSrcweir     bInMerge = sal_True;
2827cdf0e10cSrcweir     sal_Int32 nRet  = 0;
2828cdf0e10cSrcweir     pImpl->pMergeData = new SwDSParam(
2829cdf0e10cSrcweir                 rMMConfig.GetCurrentDBData(), xResultSet, rMMConfig.GetSelection());
2830cdf0e10cSrcweir 
2831cdf0e10cSrcweir     try{
2832cdf0e10cSrcweir         //set to start position
2833cdf0e10cSrcweir         if(pImpl->pMergeData->aSelection.getLength())
2834cdf0e10cSrcweir         {
2835cdf0e10cSrcweir             sal_Int32 nPos = 0;
2836cdf0e10cSrcweir             pImpl->pMergeData->aSelection.getConstArray()[ pImpl->pMergeData->nSelectionIndex++ ] >>= nPos;
2837cdf0e10cSrcweir             pImpl->pMergeData->bEndOfDB = !pImpl->pMergeData->xResultSet->absolute( nPos );
2838cdf0e10cSrcweir             pImpl->pMergeData->CheckEndOfDB();
2839cdf0e10cSrcweir             if(pImpl->pMergeData->nSelectionIndex >= pImpl->pMergeData->aSelection.getLength())
2840cdf0e10cSrcweir                 pImpl->pMergeData->bEndOfDB = sal_True;
2841cdf0e10cSrcweir         }
2842cdf0e10cSrcweir         else
2843cdf0e10cSrcweir         {
2844cdf0e10cSrcweir             pImpl->pMergeData->bEndOfDB = !pImpl->pMergeData->xResultSet->first();
2845cdf0e10cSrcweir             pImpl->pMergeData->CheckEndOfDB();
2846cdf0e10cSrcweir         }
2847cdf0e10cSrcweir     }
2848cdf0e10cSrcweir     catch(Exception&)
2849cdf0e10cSrcweir     {
2850cdf0e10cSrcweir         pImpl->pMergeData->bEndOfDB = sal_True;
2851cdf0e10cSrcweir         pImpl->pMergeData->CheckEndOfDB();
2852cdf0e10cSrcweir         DBG_ERROR("exception in MergeNew()");
2853cdf0e10cSrcweir     }
2854cdf0e10cSrcweir 
2855cdf0e10cSrcweir     //bCancel is set from the PrintMonitor
2856cdf0e10cSrcweir     bCancel = sal_False;
2857cdf0e10cSrcweir 
2858cdf0e10cSrcweir     CreateMonitor aMonitorDlg(&rSourceView.GetEditWin());
2859cdf0e10cSrcweir     aMonitorDlg.SetCancelHdl(LINK(this, SwNewDBMgr, PrtCancelHdl));
2860cdf0e10cSrcweir     if (!IsMergeSilent())
2861cdf0e10cSrcweir     {
2862cdf0e10cSrcweir         aMonitorDlg.Show();
2863cdf0e10cSrcweir         aMonitorDlg.Invalidate();
2864cdf0e10cSrcweir         aMonitorDlg.Update();
2865cdf0e10cSrcweir         // the print monitor needs some time to act
2866cdf0e10cSrcweir         for( sal_uInt16 i = 0; i < 25; i++)
2867cdf0e10cSrcweir             Application::Reschedule();
2868cdf0e10cSrcweir     }
2869cdf0e10cSrcweir 
2870cdf0e10cSrcweir     SwWrtShell& rSourceShell = rSourceView.GetWrtShell();
2871cdf0e10cSrcweir     sal_Bool bSynchronizedDoc = rSourceShell.IsLabelDoc() && rSourceShell.GetSectionFmtCount() > 1;
2872cdf0e10cSrcweir     //save the settings of the first
2873cdf0e10cSrcweir     rSourceShell.SttEndDoc(sal_True);
2874cdf0e10cSrcweir     sal_uInt16 nStartingPageNo = rSourceShell.GetVirtPageNum();
2875cdf0e10cSrcweir     String sModifiedStartingPageDesc;
2876cdf0e10cSrcweir     String sStartingPageDesc = sModifiedStartingPageDesc = rSourceShell.GetPageDesc(
2877cdf0e10cSrcweir                                 rSourceShell.GetCurPageDesc()).GetName();
2878cdf0e10cSrcweir 
2879cdf0e10cSrcweir     try
2880cdf0e10cSrcweir     {
2881cdf0e10cSrcweir         // create a target docshell to put the merged document into
2882cdf0e10cSrcweir         SfxObjectShellRef xTargetDocShell( new SwDocShell( SFX_CREATE_MODE_STANDARD ) );
2883cdf0e10cSrcweir         xTargetDocShell->DoInitNew( 0 );
2884cdf0e10cSrcweir         SfxViewFrame* pTargetFrame = SfxViewFrame::LoadHiddenDocument( *xTargetDocShell, 0 );
2885cdf0e10cSrcweir 
2886cdf0e10cSrcweir         //the created window has to be located at the same position as the source window
2887cdf0e10cSrcweir         Window& rTargetWindow = pTargetFrame->GetFrame().GetWindow();
2888cdf0e10cSrcweir         Window& rSourceWindow = rSourceView.GetViewFrame()->GetFrame().GetWindow();
2889cdf0e10cSrcweir         rTargetWindow.SetPosPixel(rSourceWindow.GetPosPixel());
2890cdf0e10cSrcweir 
2891cdf0e10cSrcweir //        pTargetFrame->GetFrame().Appear();
2892cdf0e10cSrcweir         SwView* pTargetView = static_cast<SwView*>( pTargetFrame->GetViewShell() );
2893cdf0e10cSrcweir         rMMConfig.SetTargetView(pTargetView);
2894cdf0e10cSrcweir         //initiate SelectShell() to create sub shells
2895cdf0e10cSrcweir         pTargetView->AttrChangedNotify( &pTargetView->GetWrtShell() );
2896cdf0e10cSrcweir         SwWrtShell* pTargetShell = pTargetView->GetWrtShellPtr();
2897cdf0e10cSrcweir         // #i63806#
2898cdf0e10cSrcweir         const SwPageDesc* pSourcePageDesc = rSourceShell.FindPageDescByName( sStartingPageDesc );
2899cdf0e10cSrcweir         const SwFrmFmt& rMaster = pSourcePageDesc->GetMaster();
2900cdf0e10cSrcweir         bool bPageStylesWithHeaderFooter = rMaster.GetHeader().IsActive()  ||
2901cdf0e10cSrcweir                                         rMaster.GetFooter().IsActive();
2902cdf0e10cSrcweir 
2903cdf0e10cSrcweir 
2904cdf0e10cSrcweir         // #122799# copy compatibility options
2905cdf0e10cSrcweir         lcl_CopyCompatibilityOptions( rSourceShell, *pTargetShell);
2906cdf0e10cSrcweir         // #72821# copy dynamic defaults
2907cdf0e10cSrcweir         lcl_CopyDynamicDefaults( *rSourceShell.GetDoc(), *pTargetShell->GetDoc() );
2908cdf0e10cSrcweir 
2909cdf0e10cSrcweir 
2910cdf0e10cSrcweir         long nStartRow, nEndRow;
2911cdf0e10cSrcweir         sal_uLong nDocNo = 1;
2912cdf0e10cSrcweir         sal_Int32 nDocCount = 0;
2913cdf0e10cSrcweir         if( !IsMergeSilent() && lcl_getCountFromResultSet( nDocCount, pImpl->pMergeData->xResultSet ) )
2914cdf0e10cSrcweir             aMonitorDlg.SetTotalCount( nDocCount );
2915cdf0e10cSrcweir 
2916cdf0e10cSrcweir         do
2917cdf0e10cSrcweir         {
2918cdf0e10cSrcweir             nStartRow = pImpl->pMergeData->xResultSet->getRow();
2919cdf0e10cSrcweir             if (!IsMergeSilent())
2920cdf0e10cSrcweir             {
2921cdf0e10cSrcweir                 aMonitorDlg.SetCurrentPosition( nDocNo );
2922cdf0e10cSrcweir                 aMonitorDlg.Invalidate();
2923cdf0e10cSrcweir                 aMonitorDlg.Update();
2924cdf0e10cSrcweir                 // the print monitor needs some time to act
2925cdf0e10cSrcweir                 for( sal_uInt16 i = 0; i < 25; i++)
2926cdf0e10cSrcweir                     Application::Reschedule();
2927cdf0e10cSrcweir             }
2928cdf0e10cSrcweir 
2929cdf0e10cSrcweir             // copy the source document
2930cdf0e10cSrcweir             // the copy will be closed later, but it is more safe to use SfxObjectShellLock here
2931cdf0e10cSrcweir             SfxObjectShellLock xWorkDocSh;
2932cdf0e10cSrcweir             if(nDocNo == 1 )
2933cdf0e10cSrcweir             {
2934cdf0e10cSrcweir                 uno::Reference< util::XCloneable > xClone( rSourceView.GetDocShell()->GetModel(), uno::UNO_QUERY);
2935cdf0e10cSrcweir                 uno::Reference< lang::XUnoTunnel > xWorkDocShell( xClone->createClone(), uno::UNO_QUERY);
2936cdf0e10cSrcweir                 SwXTextDocument* pWorkModel = reinterpret_cast<SwXTextDocument*>(xWorkDocShell->getSomething(SwXTextDocument::getUnoTunnelId()));
2937cdf0e10cSrcweir                 xWorkDocSh = pWorkModel->GetDocShell();
2938cdf0e10cSrcweir             }
2939cdf0e10cSrcweir             else
2940cdf0e10cSrcweir             {
2941cdf0e10cSrcweir                 xWorkDocSh = rSourceView.GetDocShell()->GetDoc()->CreateCopy(true);
2942cdf0e10cSrcweir             }
2943cdf0e10cSrcweir             //create a ViewFrame
2944cdf0e10cSrcweir             SwView* pWorkView = static_cast< SwView* >( SfxViewFrame::LoadHiddenDocument( *xWorkDocSh, 0 )->GetViewShell() );
2945cdf0e10cSrcweir             SwWrtShell& rWorkShell = pWorkView->GetWrtShell();
2946cdf0e10cSrcweir             pWorkView->AttrChangedNotify( &rWorkShell );//Damit SelectShell gerufen wird.
2947cdf0e10cSrcweir 
2948cdf0e10cSrcweir                 // merge the data
2949cdf0e10cSrcweir                 SwDoc* pWorkDoc = rWorkShell.GetDoc();
2950cdf0e10cSrcweir                 SwNewDBMgr* pWorkDBMgr = pWorkDoc->GetNewDBMgr();
2951cdf0e10cSrcweir                 pWorkDoc->SetNewDBMgr( this );
2952cdf0e10cSrcweir                 pWorkDoc->EmbedAllLinks();
2953cdf0e10cSrcweir                 SwUndoId nLastUndoId(UNDO_EMPTY);
2954cdf0e10cSrcweir                 if (rWorkShell.GetLastUndoInfo(0, & nLastUndoId))
2955cdf0e10cSrcweir                 {
2956cdf0e10cSrcweir                     if (UNDO_UI_DELETE_INVISIBLECNTNT == nLastUndoId)
2957cdf0e10cSrcweir                     {
2958cdf0e10cSrcweir                         rWorkShell.Undo();
2959cdf0e10cSrcweir                     }
2960cdf0e10cSrcweir                 }
2961cdf0e10cSrcweir                 // #i69485# lock fields to prevent access to the result set while calculating layout
2962cdf0e10cSrcweir 	            rWorkShell.LockExpFlds();
2963cdf0e10cSrcweir                 // create a layout
2964cdf0e10cSrcweir                 rWorkShell.CalcLayout();
2965cdf0e10cSrcweir                 rWorkShell.UnlockExpFlds();
2966cdf0e10cSrcweir                 SFX_APP()->NotifyEvent(SfxEventHint(SW_EVENT_FIELD_MERGE, SwDocShell::GetEventName(STR_SW_EVENT_FIELD_MERGE), rWorkShell.GetView().GetViewFrame()->GetObjectShell()));
2967cdf0e10cSrcweir             rWorkShell.ViewShell::UpdateFlds();
2968cdf0e10cSrcweir                 SFX_APP()->NotifyEvent(SfxEventHint(SW_EVENT_FIELD_MERGE_FINISHED, SwDocShell::GetEventName(STR_SW_EVENT_FIELD_MERGE_FINISHED), rWorkShell.GetView().GetViewFrame()->GetObjectShell()));
2969cdf0e10cSrcweir 
2970cdf0e10cSrcweir             // strip invisible content and convert fields to text
2971cdf0e10cSrcweir             rWorkShell.RemoveInvisibleContent();
2972cdf0e10cSrcweir             rWorkShell.ConvertFieldsToText();
2973cdf0e10cSrcweir             rWorkShell.SetNumberingRestart();
2974cdf0e10cSrcweir             if( bSynchronizedDoc )
2975cdf0e10cSrcweir             {
2976cdf0e10cSrcweir                 lcl_RemoveSectionLinks( rWorkShell );
2977cdf0e10cSrcweir             }
2978cdf0e10cSrcweir 
2979cdf0e10cSrcweir             // insert the document into the target document
2980cdf0e10cSrcweir             rWorkShell.SttEndDoc(sal_False);
2981cdf0e10cSrcweir             rWorkShell.SttEndDoc(sal_True);
2982cdf0e10cSrcweir             rWorkShell.SelAll();
2983cdf0e10cSrcweir             pTargetShell->SttEndDoc(sal_False);
2984cdf0e10cSrcweir 
2985cdf0e10cSrcweir             //#i63806# put the styles to the target document
2986cdf0e10cSrcweir             //if the source uses headers or footers each new copy need to copy a new page styles
2987cdf0e10cSrcweir             if(bPageStylesWithHeaderFooter)
2988cdf0e10cSrcweir             {
2989cdf0e10cSrcweir                 //create a new pagestyle
2990cdf0e10cSrcweir                 //copy the pagedesc from the current document to the new document and change the name of the to-be-applied style
2991cdf0e10cSrcweir 
2992cdf0e10cSrcweir                 SwDoc* pTargetDoc = pTargetShell->GetDoc();
2993cdf0e10cSrcweir                 String sNewPageDescName = lcl_FindUniqueName(pTargetShell, sStartingPageDesc, nDocNo );
2994cdf0e10cSrcweir                 pTargetShell->GetDoc()->MakePageDesc( sNewPageDescName );
2995cdf0e10cSrcweir                 SwPageDesc* pTargetPageDesc = pTargetShell->FindPageDescByName( sNewPageDescName );
2996cdf0e10cSrcweir                 const SwPageDesc* pWorkPageDesc = rWorkShell.FindPageDescByName( sStartingPageDesc );
2997cdf0e10cSrcweir 
2998cdf0e10cSrcweir                 if(pWorkPageDesc && pTargetPageDesc)
2999cdf0e10cSrcweir                 {
3000cdf0e10cSrcweir                     pTargetDoc->CopyPageDesc( *pWorkPageDesc, *pTargetPageDesc, sal_False );
3001cdf0e10cSrcweir                     sModifiedStartingPageDesc = sNewPageDescName;
3002cdf0e10cSrcweir                     lcl_CopyFollowPageDesc( *pTargetShell, *pWorkPageDesc, *pTargetPageDesc, nDocNo );
3003cdf0e10cSrcweir                 }
3004cdf0e10cSrcweir             }
3005cdf0e10cSrcweir             if(nDocNo == 1 || bPageStylesWithHeaderFooter)
3006cdf0e10cSrcweir             {
3007cdf0e10cSrcweir                 pTargetView->GetDocShell()->_LoadStyles( *rSourceView.GetDocShell(), sal_True );
3008cdf0e10cSrcweir             }
3009cdf0e10cSrcweir             if(nDocNo > 1)
3010cdf0e10cSrcweir             {
3011cdf0e10cSrcweir                 pTargetShell->InsertPageBreak( &sModifiedStartingPageDesc, nStartingPageNo );
3012cdf0e10cSrcweir             }
3013cdf0e10cSrcweir             else
3014cdf0e10cSrcweir             {
3015cdf0e10cSrcweir                 pTargetShell->SetPageStyle(sModifiedStartingPageDesc);
3016cdf0e10cSrcweir             }
3017cdf0e10cSrcweir             sal_uInt16 nPageCountBefore = pTargetShell->GetPageCnt();
3018cdf0e10cSrcweir             DBG_ASSERT(!pTargetShell->GetTableFmt(),"target document ends with a table - paragraph should be appended");
3019cdf0e10cSrcweir             //#i51359# add a second paragraph in case there's only one
3020cdf0e10cSrcweir             {
3021cdf0e10cSrcweir                 SwNodeIndex aIdx( pWorkDoc->GetNodes().GetEndOfExtras(), 2 );
3022cdf0e10cSrcweir               SwPosition aTestPos( aIdx );
3023cdf0e10cSrcweir               SwCursor aTestCrsr(aTestPos,0,false);
3024cdf0e10cSrcweir                 if(!aTestCrsr.MovePara(fnParaNext, fnParaStart))
3025cdf0e10cSrcweir                 {
3026cdf0e10cSrcweir                     //append a paragraph
3027cdf0e10cSrcweir                     pWorkDoc->AppendTxtNode( aTestPos );
3028cdf0e10cSrcweir                 }
3029cdf0e10cSrcweir             }
3030cdf0e10cSrcweir             pTargetShell->Paste( rWorkShell.GetDoc(), sal_True );
3031cdf0e10cSrcweir             //convert fields in page styles (header/footer - has to be done after the first document has been pasted
3032cdf0e10cSrcweir             if(1 == nDocNo)
3033cdf0e10cSrcweir             {
3034cdf0e10cSrcweir                 pTargetShell->CalcLayout();
3035cdf0e10cSrcweir                 pTargetShell->ConvertFieldsToText();
3036cdf0e10cSrcweir             }
3037cdf0e10cSrcweir             //add the document info to the config item
3038cdf0e10cSrcweir             SwDocMergeInfo aMergeInfo;
3039cdf0e10cSrcweir             aMergeInfo.nStartPageInTarget = nPageCountBefore;
3040cdf0e10cSrcweir             //#i72820# calculate layout to be able to find the correct page index
3041cdf0e10cSrcweir             pTargetShell->CalcLayout();
3042cdf0e10cSrcweir             aMergeInfo.nEndPageInTarget = pTargetShell->GetPageCnt();
3043cdf0e10cSrcweir             aMergeInfo.nDBRow = nStartRow;
3044cdf0e10cSrcweir             rMMConfig.AddMergedDocument( aMergeInfo );
3045cdf0e10cSrcweir             ++nRet;
3046cdf0e10cSrcweir 
3047cdf0e10cSrcweir             // the print monitor needs some time to act
3048cdf0e10cSrcweir             for( sal_uInt16 i = 0; i < 25; i++)
3049cdf0e10cSrcweir                 Application::Reschedule();
3050cdf0e10cSrcweir 
3051cdf0e10cSrcweir             //restore the ole DBMgr
3052cdf0e10cSrcweir             pWorkDoc->SetNewDBMgr( pWorkDBMgr );
3053cdf0e10cSrcweir             //now the temporary document should be closed
3054cdf0e10cSrcweir             SfxObjectShellRef xDocSh(pWorkView->GetDocShell());
3055cdf0e10cSrcweir             xDocSh->DoClose();
3056cdf0e10cSrcweir             nEndRow = pImpl->pMergeData->xResultSet->getRow();
3057cdf0e10cSrcweir             ++nDocNo;
3058cdf0e10cSrcweir         } while( !bCancel &&
3059cdf0e10cSrcweir                 (bSynchronizedDoc && (nStartRow != nEndRow)? ExistsNextRecord() : ToNextMergeRecord()));
3060cdf0e10cSrcweir 
3061cdf0e10cSrcweir         //deselect all, go out of the frame and go to the beginning of the document
3062cdf0e10cSrcweir         Point aPt(LONG_MIN, LONG_MIN);
3063cdf0e10cSrcweir         pTargetShell->SelectObj(aPt, SW_LEAVE_FRAME);
3064cdf0e10cSrcweir         if (pTargetShell->IsSelFrmMode())
3065cdf0e10cSrcweir         {
3066cdf0e10cSrcweir             pTargetShell->UnSelectFrm();
3067cdf0e10cSrcweir             pTargetShell->LeaveSelFrmMode();
3068cdf0e10cSrcweir         }
3069cdf0e10cSrcweir         pTargetShell->EnterStdMode();
3070cdf0e10cSrcweir         pTargetShell->SttDoc();
3071cdf0e10cSrcweir         //
3072cdf0e10cSrcweir     }
3073cdf0e10cSrcweir     catch( Exception& rEx)
3074cdf0e10cSrcweir     {
3075cdf0e10cSrcweir         (void)rEx;
3076cdf0e10cSrcweir         DBG_ERROR("exception caught in SwNewDBMgr::MergeDocuments");
3077cdf0e10cSrcweir     }
3078cdf0e10cSrcweir     DELETEZ(pImpl->pMergeData);
3079cdf0e10cSrcweir     bInMerge = sal_False;
3080cdf0e10cSrcweir     return nRet;
3081cdf0e10cSrcweir }
3082cdf0e10cSrcweir /* -----------------09.12.2002 12:38-----------------
3083cdf0e10cSrcweir  *
3084cdf0e10cSrcweir  * --------------------------------------------------*/
SwConnectionDisposedListener_Impl(SwNewDBMgr & rMgr)3085cdf0e10cSrcweir SwConnectionDisposedListener_Impl::SwConnectionDisposedListener_Impl(SwNewDBMgr& rMgr) :
3086cdf0e10cSrcweir     rDBMgr(rMgr)
3087cdf0e10cSrcweir {};
3088cdf0e10cSrcweir /* -----------------09.12.2002 12:39-----------------
3089cdf0e10cSrcweir  *
3090cdf0e10cSrcweir  * --------------------------------------------------*/
~SwConnectionDisposedListener_Impl()3091cdf0e10cSrcweir SwConnectionDisposedListener_Impl::~SwConnectionDisposedListener_Impl()
3092cdf0e10cSrcweir {};
3093cdf0e10cSrcweir /* -----------------09.12.2002 12:39-----------------
3094cdf0e10cSrcweir  *
3095cdf0e10cSrcweir  * --------------------------------------------------*/
disposing(const EventObject & rSource)3096cdf0e10cSrcweir void SwConnectionDisposedListener_Impl::disposing( const EventObject& rSource )
3097cdf0e10cSrcweir         throw (RuntimeException)
3098cdf0e10cSrcweir {
3099cdf0e10cSrcweir     ::vos::OGuard aGuard(Application::GetSolarMutex());
3100cdf0e10cSrcweir     uno::Reference<XConnection> xSource(rSource.Source, UNO_QUERY);
3101cdf0e10cSrcweir     for(sal_uInt16 nPos = rDBMgr.aDataSourceParams.Count(); nPos; nPos--)
3102cdf0e10cSrcweir     {
3103cdf0e10cSrcweir         SwDSParam* pParam = rDBMgr.aDataSourceParams[nPos - 1];
3104cdf0e10cSrcweir         if(pParam->xConnection.is() &&
3105cdf0e10cSrcweir                 (xSource == pParam->xConnection))
3106cdf0e10cSrcweir         {
3107cdf0e10cSrcweir             rDBMgr.aDataSourceParams.DeleteAndDestroy(nPos - 1);
3108cdf0e10cSrcweir         }
3109cdf0e10cSrcweir     }
3110cdf0e10cSrcweir }
3111