1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 #ifdef SW_DLLIMPLEMENTATION
31 #undef SW_DLLIMPLEMENTATION
32 #endif
33 #include <swtypes.hxx>
34 #include <addresslistdialog.hxx>
35 #include <selectdbtabledialog.hxx>
36 #include <createaddresslistdialog.hxx>
37 #include <mailmergewizard.hxx>
38 #include <mmconfigitem.hxx>
39 #include <mmaddressblockpage.hxx>
40 #ifndef _DBMGR_HXX
41 #include <dbmgr.hxx>
42 #endif
43 #include <dbconfig.hxx>
44 #include <unotools/tempfile.hxx>
45 #include <vcl/msgbox.hxx>
46 #include <vcl/svapp.hxx>
47 #include <tools/urlobj.hxx>
48 #include <comphelper/processfactory.hxx>
49 #include <comphelper/types.hxx>
50 #include <com/sun/star/sdbc/XCloseable.hpp>
51 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
52 #include <com/sun/star/container/XNameAccess.hpp>
53 #include <com/sun/star/uno/XNamingService.hpp>
54 #include <com/sun/star/sdb/XCompletedConnection.hpp>
55 #include <com/sun/star/sdb/CommandType.hpp>
56 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
57 #include <com/sun/star/sdbc/XRowSet.hpp>
58 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSERFACTORY_HPP_
59 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
60 #endif
61 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
62 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
63 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
64 #include <com/sun/star/frame/XStorable.hpp>
65 #include <swunohelper.hxx>
66 #include <vcl/waitobj.hxx>
67 #include <unotools/pathoptions.hxx>
68 #include <svl/urihelper.hxx>
69 #include <addresslistdialog.hrc>
70 #include <dbui.hrc>
71 
72 #include <helpid.h>
73 #include <unomid.h>
74 
75 
76 using namespace ::com::sun::star;
77 using namespace ::com::sun::star::uno;
78 using namespace ::com::sun::star::lang;
79 using namespace ::com::sun::star::container;
80 using namespace ::com::sun::star::sdb;
81 using namespace ::com::sun::star::sdbc;
82 using namespace ::com::sun::star::sdbcx;
83 using namespace ::com::sun::star::task;
84 using namespace ::com::sun::star::beans;
85 using namespace ::com::sun::star::ui::dialogs;
86 using namespace ::rtl;
87 
88 #define ITEMID_NAME         1
89 #define ITEMID_TABLE        2
90 
91 //typedef SharedUNOComponent< XConnection >   SharedConnection;
92 
93 static const char* cUTF8 = "UTF-8";
94 /*-- 07.05.2004 14:11:34---------------------------------------------------
95 
96   -----------------------------------------------------------------------*/
97 struct AddressUserData_Impl
98 {
99     uno::Reference<XDataSource>             xSource;
100     SharedConnection                        xConnection;
101     uno::Reference< XColumnsSupplier>       xColumnsSupplier;
102     uno::Reference< sdbc::XResultSet>       xResultSet;
103     ::rtl::OUString                    sFilter;
104     ::rtl::OUString                    sURL; // data is editable
105     sal_Int32                          nCommandType;
106     sal_Int32                          nTableAndQueryCount;
107     AddressUserData_Impl() :
108         nCommandType(0),
109         nTableAndQueryCount(-1)
110         {}
111 };
112 ::rtl::OUString lcl_getFlatURL( uno::Reference<beans::XPropertySet>& xSourceProperties )
113 {
114     ::rtl::OUString sURL;
115     if(xSourceProperties.is())
116     {
117         rtl::OUString sDBURL;
118         xSourceProperties->getPropertyValue(C2U("URL")) >>= sDBURL;
119         if(String(sDBURL).SearchAscii("sdbc:flat:") == 0)
120         {
121             uno::Sequence<OUString> aFilters;
122             xSourceProperties->getPropertyValue(C2U("TableFilter")) >>= aFilters;
123             uno::Sequence<PropertyValue> aInfo;
124             xSourceProperties->getPropertyValue(C2U("Info")) >>= aInfo;
125             if(aFilters.getLength() == 1 && aInfo.getLength() )
126             {
127                 ::rtl::OUString sFieldDelim;
128                 ::rtl::OUString sStringDelim;
129                 ::rtl::OUString sExtension;
130                 ::rtl::OUString sCharSet;
131                 for(sal_Int32 nInfo = 0; nInfo < aInfo.getLength(); ++nInfo)
132                 {
133                     if(aInfo[nInfo].Name == C2U("FieldDelimiter"))
134                         aInfo[nInfo].Value >>= sFieldDelim;
135                     else if(aInfo[nInfo].Name == C2U("StringDelimiter"))
136                         aInfo[nInfo].Value >>= sStringDelim;
137                     else if(aInfo[nInfo].Name == C2U("Extension"))
138                         aInfo[nInfo].Value >>= sExtension;
139                     else if(aInfo[nInfo].Name == C2U("CharSet"))
140                         aInfo[nInfo].Value >>= sCharSet;
141                 }
142                 if(!sCharSet.compareToAscii( cUTF8 ))
143                 {
144                     sURL = String(sDBURL).Copy( 10 );
145                     //#i97577# at this point the 'URL' can also be a file name!
146                     sURL = URIHelper::SmartRel2Abs( INetURLObject(), sURL );
147                     sURL += C2U("/");
148                     sURL += aFilters[0];
149                     sURL += C2U(".");
150                     sURL += sExtension;
151                 }
152             }
153         }
154     }
155     return sURL;
156 }
157 /*-- 07.04.2004 16:35:43---------------------------------------------------
158 
159   -----------------------------------------------------------------------*/
160 SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent) :
161     SfxModalDialog(pParent, SW_RES(DLG_MM_ADDRESSLISTDIALOG)),
162 #ifdef MSC
163 #pragma warning (disable : 4355)
164 #endif
165     m_aDescriptionFI( this, SW_RES(  FI_DESCRIPTION    )),
166     m_aListFT( this, SW_RES(         FT_LIST           )),
167     m_aListHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER),
168     m_aListLB( this, SW_RES(         LB_LIST           )),
169     m_aLoadListPB( this, SW_RES(     PB_LOADLIST       )),
170     m_aCreateListPB(this, SW_RES(    PB_CREATELIST     )),
171     m_aFilterPB( this, SW_RES(       PB_FILTER         )),
172     m_aEditPB(this, SW_RES(          PB_EDIT           )),
173     m_aTablePB(this, SW_RES(         PB_TABLE          )),
174     m_aSeparatorFL(this, SW_RES(    FL_SEPARATOR      )),
175     m_aOK( this, SW_RES(             PB_OK             )),
176     m_aCancel( this, SW_RES(         PB_CANCEL         )),
177     m_aHelp( this, SW_RES(           PB_HELP           )),
178 #ifdef MSC
179 #pragma warning (default : 4355)
180 #endif
181     m_sName(        SW_RES( ST_NAME )),
182     m_sTable(       SW_RES( ST_TABLE )),
183     m_sConnecting(  SW_RES( ST_CONNECTING )),
184     m_pCreatedDataSource(0),
185     m_bInSelectHdl(false),
186     m_pAddressPage(pParent)
187 {
188     FreeResource();
189     String sTemp(m_aDescriptionFI.GetText());
190     sTemp.SearchAndReplaceAscii("%1", m_aLoadListPB.GetText());
191     sTemp.SearchAndReplaceAscii("%2", m_aCreateListPB.GetText());
192     m_aDescriptionFI.SetText(sTemp);
193     m_aFilterPB.SetClickHdl( LINK( this, SwAddressListDialog,    FilterHdl_Impl ));
194     m_aLoadListPB.SetClickHdl( LINK( this, SwAddressListDialog,  LoadHdl_Impl ));
195     m_aCreateListPB.SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
196     m_aEditPB.SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
197     m_aTablePB.SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
198 
199     Size aLBSize(m_aListLB.GetSizePixel());
200     m_aListHB.SetSizePixel(aLBSize);
201     Size aHeadSize(m_aListHB.CalcWindowSizePixel());
202     aHeadSize.Width() = aLBSize.Width();
203     m_aListHB.SetSizePixel(aHeadSize);
204     Point aLBPos(m_aListLB.GetPosPixel());
205     m_aListHB.SetPosPixel(aLBPos);
206     aLBPos.Y() += aHeadSize.Height();
207     aLBSize.Height() -= aHeadSize.Height();
208     m_aListLB.SetPosSizePixel(aLBPos, aLBSize);
209 
210     Size aSz(m_aListHB.GetOutputSizePixel());
211     m_aListHB.InsertItem( ITEMID_NAME, m_sName,
212                             aSz.Width()/2,
213                             HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS/*| HIB_CLICKABLE | HIB_UPARROW */);
214     m_aListHB.InsertItem( ITEMID_TABLE, m_sTable,
215                             aSz.Width()/2,
216                             HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS /*| HIB_CLICKABLE | HIB_UPARROW */);
217     m_aListHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
218     m_aListHB.Show();
219 
220     m_aListLB.SetHelpId(HID_MM_ADDRESSLIST_TLB);
221     static long nTabs[] = {2, 0, aSz.Width()/2 };
222     m_aListLB.SetStyle( m_aListLB.GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
223     m_aListLB.SetSelectionMode( SINGLE_SELECTION );
224     m_aListLB.SetTabs(&nTabs[0], MAP_PIXEL);
225     m_aOK.SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
226 
227     uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
228     if( xMgr.is() )
229     {
230         uno::Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
231         m_xDBContext = uno::Reference<XNameAccess>(xInstance, UNO_QUERY) ;
232     }
233     SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
234     const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
235 
236     DBG_ASSERT(m_xDBContext.is(), "service 'com.sun.star.sdb.DatabaseContext' not found!");
237     sal_Bool bEnableEdit = sal_False;
238     sal_Bool bEnableOK = sal_True;
239     m_aListLB.SelectAll( sal_False );
240 
241     if(m_xDBContext.is())
242     {
243         SwDBConfig aDb;
244         ::rtl::OUString sBibliography = aDb.GetBibliographySource().sDataSource;
245         uno::Sequence< ::rtl::OUString> aNames = m_xDBContext->getElementNames();
246         const ::rtl::OUString* pNames = aNames.getConstArray();
247         for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
248         {
249             if ( pNames[nName] == sBibliography )
250                 continue;
251             SvLBoxEntry* pEntry = m_aListLB.InsertEntry(pNames[nName]);
252             AddressUserData_Impl* pUserData = new AddressUserData_Impl();
253             pEntry->SetUserData(pUserData);
254             if(pNames[nName] == rCurrentData.sDataSource)
255             {
256                 m_aListLB.Select(pEntry);
257                 m_aListLB.SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
258                 pUserData->nCommandType = rCurrentData.nCommandType;
259                 pUserData->xSource = rConfigItem.GetSource();
260                 pUserData->xConnection = rConfigItem.GetConnection();
261                 pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
262                 pUserData->xResultSet = rConfigItem.GetResultSet();
263                 pUserData->sFilter = rConfigItem.GetFilter();
264                 //is the data source editable (csv, Unicode, single table)
265                 uno::Reference<beans::XPropertySet> xSourceProperties;
266                 try
267                 {
268                     m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
269                     pUserData->sURL = lcl_getFlatURL( xSourceProperties );
270                     bEnableEdit = pUserData->sURL.getLength() > 0 &&
271                         SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
272                         !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
273                 }
274                 catch(const uno::Exception& )
275                 {
276                     bEnableOK = sal_False;
277                 }
278                 m_aDBData = rCurrentData;
279             }
280         }
281     }
282     m_aOK.Enable(m_aListLB.GetEntryCount()>0 && bEnableOK);
283     m_aEditPB.Enable(bEnableEdit);
284     m_aListLB.SetSelectHdl(LINK(this, SwAddressListDialog, ListBoxSelectHdl_Impl));
285 	TableSelectHdl_Impl(NULL);
286 }
287 /*-- 07.04.2004 16:35:43---------------------------------------------------
288 
289   -----------------------------------------------------------------------*/
290 SwAddressListDialog::~SwAddressListDialog()
291 {
292     SvLBoxEntry* pEntry = m_aListLB.First();
293     while(pEntry)
294     {
295         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
296         delete pUserData;
297         pEntry = m_aListLB.Next( pEntry );
298     }
299 }
300 /*-- 07.04.2004 16:35:44---------------------------------------------------
301 
302   -----------------------------------------------------------------------*/
303 IMPL_LINK(SwAddressListDialog, FilterHdl_Impl, PushButton*, EMPTYARG)
304 {
305     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
306     uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
307     if(pSelect && xMgr.is())
308     {
309         String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
310         if ( !sCommand.Len() )
311             return 0;
312 
313         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
314         if(pUserData->xConnection.is() )
315         {
316             try
317             {
318                 uno::Reference<lang::XMultiServiceFactory> xConnectFactory(pUserData->xConnection, UNO_QUERY_THROW);
319                 uno::Reference<XSingleSelectQueryComposer> xComposer(
320                         xConnectFactory->createInstance(C2U("com.sun.star.sdb.SingleSelectQueryComposer")), UNO_QUERY_THROW);
321 
322                 PropertyValue aSecond;
323                 aSecond.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RowSet" ) );
324                 uno::Reference<XRowSet> xRowSet(
325                         xMgr->createInstance(C2U("com.sun.star.sdb.RowSet")), UNO_QUERY);
326                 uno::Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
327                 xRowProperties->setPropertyValue(C2U("DataSourceName"),
328                         makeAny(OUString(m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1))));
329                 xRowProperties->setPropertyValue(C2U("Command"), makeAny(
330                         OUString(sCommand)));
331                 xRowProperties->setPropertyValue(C2U("CommandType"), makeAny(pUserData->nCommandType));
332                 xRowProperties->setPropertyValue(C2U("ActiveConnection"), makeAny(pUserData->xConnection.getTyped()));
333                 xRowSet->execute();
334                 aSecond.Value <<= xRowSet;
335 
336                 PropertyValue aFirst;
337                 aFirst.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "QueryComposer" ) );
338                 ::rtl::OUString sQuery;
339                 xRowProperties->getPropertyValue(C2U("ActiveCommand"))>>= sQuery;
340                 xComposer->setQuery(sQuery);
341                 if(pUserData->sFilter.getLength())
342                     xComposer->setFilter(pUserData->sFilter);
343                 aFirst.Value <<= xComposer;
344 
345                 uno::Sequence<Any> aInit(2);
346                 aInit[0] <<= aFirst;
347                 aInit[1] <<= aSecond;
348 
349                 ::rtl::OUString sDialogServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.FilterDialog" ) );
350                 uno::Reference< XExecutableDialog> xDialog(
351                         xMgr->createInstanceWithArguments( sDialogServiceName, aInit ), UNO_QUERY);
352 
353                 if ( RET_OK == xDialog->execute() )
354                 {
355                     WaitObject aWO( NULL );
356                     pUserData->sFilter = xComposer->getFilter();
357                 }
358                 ::comphelper::disposeComponent(xRowSet);
359             }
360             catch(Exception& )
361             {
362                 DBG_ERROR("exception caught in SwAddressListDialog::FilterHdl_Impl");
363             }
364         }
365     }
366     return 0;
367 }
368 /*-- 07.04.2004 16:35:44---------------------------------------------------
369 
370   -----------------------------------------------------------------------*/
371 IMPL_LINK(SwAddressListDialog, LoadHdl_Impl,   PushButton*, EMPTYARG)
372 {
373     String sNewSource = SwNewDBMgr::LoadAndRegisterDataSource();
374     if(sNewSource.Len())
375     {
376         SvLBoxEntry* pNewSource = m_aListLB.InsertEntry(sNewSource);
377         pNewSource->SetUserData(new AddressUserData_Impl());
378         m_aListLB.Select(pNewSource);
379     }
380     return 0;
381 }
382 /*-- 07.04.2004 16:35:44---------------------------------------------------
383 
384   -----------------------------------------------------------------------*/
385 IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
386 {
387     String sInputURL;
388     SwCreateAddressListDialog* pDlg =
389             new SwCreateAddressListDialog(
390                     pButton,
391                     sInputURL,
392                     m_pAddressPage->GetWizard()->GetConfigItem());
393     if(RET_OK == pDlg->Execute())
394     {
395         //register the URL a new datasource
396         OUString sURL = pDlg->GetURL();
397         try
398         {
399             uno::Reference<XSingleServiceFactory> xFact( m_xDBContext, UNO_QUERY);
400             uno::Reference<XInterface> xNewInstance = xFact->createInstance();
401             INetURLObject aURL( sURL );
402             OUString sNewName = aURL.getBase();
403             //find a unique name if sNewName already exists
404             OUString sFind(sNewName);
405             sal_Int32 nIndex = 0;
406             while(m_xDBContext->hasByName(sFind))
407             {
408                 sFind = sNewName;
409                 sFind += OUString::valueOf(++nIndex);
410             }
411             uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
412 
413             OUString sDBURL(C2U("sdbc:flat:"));
414             //only the 'path' has to be added
415             INetURLObject aTempURL(aURL);
416             aTempURL.removeSegment();
417             aTempURL.removeFinalSlash();
418             sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
419             Any aAny(&sDBURL, ::getCppuType(&sDBURL));
420             xDataProperties->setPropertyValue(C2U("URL"), aAny);
421             //set the filter to the file name without extension
422             uno::Sequence<OUString> aFilters(1);
423             aFilters[0] = sNewName;
424             aAny <<= aFilters;
425             xDataProperties->setPropertyValue(C2U("TableFilter"), aAny);
426 
427             uno::Sequence<PropertyValue> aInfo(4);
428             PropertyValue* pInfo = aInfo.getArray();
429             pInfo[0].Name = C2U("FieldDelimiter");
430             pInfo[0].Value <<= OUString(String('\t'));
431             pInfo[1].Name = C2U("StringDelimiter");
432             pInfo[1].Value <<= OUString('"');
433             pInfo[2].Name = C2U("Extension");
434             pInfo[2].Value <<= ::rtl::OUString(aURL.getExtension());//C2U("csv");
435             pInfo[3].Name = C2U("CharSet");
436             pInfo[3].Value <<= C2U(cUTF8);
437             aAny <<= aInfo;
438             xDataProperties->setPropertyValue(C2U("Info"), aAny);
439 
440             uno::Reference<sdb::XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
441             uno::Reference<frame::XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
442             String sExt = String::CreateFromAscii(".odb");
443             String sTmpName;
444             {
445                 String sHomePath(SvtPathOptions().GetWorkPath());
446                 utl::TempFile aTempFile(sFind , &sExt, &sHomePath);
447                 aTempFile.EnableKillingFile(sal_True);
448                 sTmpName = aTempFile.GetURL();
449             }
450             xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
451 
452 
453             uno::Reference<XNamingService> xNaming(m_xDBContext, UNO_QUERY);
454             xNaming->registerObject( sFind, xNewInstance );
455             //now insert the new source into the ListBox
456             String sEntry(sFind);
457             sEntry += '\t';
458             sEntry += String(aFilters[0]);
459             m_pCreatedDataSource = m_aListLB.InsertEntry(sEntry);
460             AddressUserData_Impl* pUserData = new AddressUserData_Impl();
461             pUserData->sURL = sURL;
462             m_pCreatedDataSource->SetUserData(pUserData);
463             m_aListLB.Select(m_pCreatedDataSource);
464             m_aCreateListPB.Enable(sal_False);
465 
466         }
467         catch(Exception& )
468         {
469         }
470     }
471     delete pDlg;
472     return 0;
473 }
474 /*-- 22.04.2004 10:30:40---------------------------------------------------
475 
476   -----------------------------------------------------------------------*/
477 IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
478 {
479     SvLBoxEntry* pEntry = m_aListLB.FirstSelected();
480     AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
481     if(pUserData && pUserData->sURL.getLength())
482     {
483         if(pUserData->xResultSet.is())
484         {
485             SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
486             if(rConfigItem.GetResultSet() != pUserData->xResultSet)
487                 ::comphelper::disposeComponent( pUserData->xResultSet );
488             pUserData->xResultSet = 0;
489 
490             rConfigItem.DisposeResultSet();
491         }
492         pUserData->xSource.clear();
493         pUserData->xColumnsSupplier.clear();
494         pUserData->xConnection.clear();
495             // will automatically close if it was the las reference
496         SwCreateAddressListDialog* pDlg =
497                 new SwCreateAddressListDialog(
498                         pButton,
499                         pUserData->sURL,
500                         m_pAddressPage->GetWizard()->GetConfigItem());
501         if(RET_OK == pDlg->Execute())
502         {
503         }
504         delete pDlg;
505     }
506     return 0;
507 };
508 /*-- 19.04.2004 09:41:05---------------------------------------------------
509 
510   -----------------------------------------------------------------------*/
511 IMPL_LINK(SwAddressListDialog, ListBoxSelectHdl_Impl, SvTabListBox*, EMPTYARG)
512 {
513     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
514     Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
515                                                 StaticListBoxSelectHdl_Impl ), pSelect );
516     return 0;
517 }
518 IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvLBoxEntry*, pSelect)
519 {
520     //prevent nested calls of the select handler
521     if(pThis->m_bInSelectHdl)
522         return 0;
523     pThis->EnterWait();
524     pThis->m_bInSelectHdl = true;
525     AddressUserData_Impl* pUserData = 0;
526     if(pSelect)
527     {
528         String sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
529         if(!sTable.Len())
530         {
531             pThis->m_aListLB.SetEntryText(pThis->m_sConnecting, pSelect, ITEMID_TABLE - 1);
532             // allow painting of the new entry
533             pThis->m_aListLB.Window::Invalidate(INVALIDATE_UPDATE);
534             for (sal_uInt16 i = 0; i < 10; i++)
535                 Application::Reschedule();
536         }
537 
538         pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
539         if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
540         {
541             pThis->DetectTablesAndQueries(pSelect, !sTable.Len());
542         }
543         else
544         {
545             //otherwise set the selected db-data
546             pThis->m_aDBData.sDataSource = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
547             pThis->m_aDBData.sCommand = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
548             pThis->m_aDBData.nCommandType = pUserData->nCommandType;
549             pThis->m_aOK.Enable(sal_True);
550         }
551         sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
552         if(sTable == pThis->m_sConnecting)
553            pThis->m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
554     }
555     pThis->m_aEditPB.Enable(pUserData && pUserData->sURL.getLength() &&
556                     SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
557                     !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
558     pThis->m_bInSelectHdl = false;
559     pThis->LeaveWait();
560     return 0;
561 }
562 
563 /*-- 13.05.2004 14:59:25---------------------------------------------------
564     detect the number of tables for a data source
565     if only one is available then set it at the entry
566   -----------------------------------------------------------------------*/
567 void SwAddressListDialog::DetectTablesAndQueries(
568         SvLBoxEntry* pSelect,
569         bool bWidthDialog)
570 {
571     try
572     {
573         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
574         uno::Reference<XCompletedConnection> xComplConnection;
575         if(!pUserData->xConnection.is())
576         {
577             m_aDBData.sDataSource = m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
578             m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
579             pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
580 
581             uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
582             uno::Reference< XInteractionHandler > xHandler(
583                 xMgr->createInstance( C2U( "com.sun.star.task.InteractionHandler" )), UNO_QUERY);
584             pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
585         }
586         if(pUserData->xConnection.is())
587         {
588             sal_Int32 nTables = 0;
589             uno::Sequence<rtl::OUString> aTables;
590             uno::Sequence<rtl::OUString> aQueries;
591             uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
592             if(xTSupplier.is())
593             {
594                 uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
595                 aTables = xTbls->getElementNames();
596                 nTables += aTables.getLength();
597             }
598             uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
599             if(xQSupplier.is())
600             {
601                 uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
602                 aQueries = xQueries->getElementNames();
603                 nTables += aQueries.getLength();
604             }
605             pUserData->nTableAndQueryCount = nTables;
606             if(nTables > 1 && bWidthDialog)
607             {
608                 //now call the table select dialog - if more than one table exists
609                 SwSelectDBTableDialog* pDlg = new SwSelectDBTableDialog(this, pUserData->xConnection);
610                 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
611                 if(sTable.Len())
612                     pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
613                 if(RET_OK == pDlg->Execute())
614                 {
615                     bool bIsTable;
616                     m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
617                     m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
618                     pUserData->nCommandType = m_aDBData.nCommandType;
619                 }
620                 delete pDlg;
621             }
622             else if(nTables == 1)
623             {
624                 if(aTables.getLength())
625                 {
626                     m_aDBData.sCommand = aTables[0];
627                     m_aDBData.nCommandType = CommandType::TABLE;
628                 }
629                 else
630                 {
631                     m_aDBData.sCommand = aQueries[0];
632                     m_aDBData.nCommandType = CommandType::QUERY;
633                 }
634             }
635         }
636         if ( m_aDBData.sCommand.getLength() )
637         {
638             uno::Reference<beans::XPropertySet> xSourceProperties;
639             m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
640             pUserData->sURL = lcl_getFlatURL( xSourceProperties );
641 
642             pUserData->xColumnsSupplier = SwNewDBMgr::GetColumnSupplier(pUserData->xConnection,
643                                     m_aDBData.sCommand,
644                                     m_aDBData.nCommandType == CommandType::TABLE ?
645                                             SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY );
646             //#i97577#
647             if( pUserData->xColumnsSupplier.is() )
648                 m_aListLB.SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
649             else
650                 m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
651         }
652         String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
653         m_aOK.Enable(pSelect && sCommand.Len());
654         m_aFilterPB.Enable( pUserData->xConnection.is() && sCommand.Len() );
655         m_aTablePB.Enable( pUserData->nTableAndQueryCount > 1 );
656     }
657     catch(Exception& )
658     {
659         DBG_ERROR("exception caught in SwAddressListDialog::DetectTablesAndQueries");
660         m_aOK.Enable( sal_False );
661     }
662 }
663 
664 /*-- 13.05.2004 12:55:40---------------------------------------------------
665 
666   -----------------------------------------------------------------------*/
667 IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
668 {
669     EnterWait();
670     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
671     if(pSelect)
672     {
673         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
674         //only call the table select dialog if tables have not been searched for or there
675         //are more than 1
676         String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
677         if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
678         {
679             DetectTablesAndQueries(pSelect, (pButton != 0) || (!sTable.Len()));
680         }
681     }
682 
683     LeaveWait();
684     return 0;
685 }
686 
687 /*-- 08.04.2004 14:52:11---------------------------------------------------
688 
689   -----------------------------------------------------------------------*/
690 IMPL_LINK(SwAddressListDialog, OKHdl_Impl, PushButton*, EMPTYARG)
691 {
692     EndDialog(sal_True);
693     return 0;
694 }
695 
696 /*-- 07.05.2004 14:17:47---------------------------------------------------
697 
698   -----------------------------------------------------------------------*/
699 uno::Reference< XDataSource>  SwAddressListDialog::GetSource()
700 {
701     uno::Reference< XDataSource>  xRet;
702     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
703     if(pSelect)
704     {
705         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
706         xRet = pUserData->xSource;
707     }
708     return xRet;
709 
710 }
711 /*-- 07.05.2004 14:17:48---------------------------------------------------
712 
713   -----------------------------------------------------------------------*/
714 SharedConnection    SwAddressListDialog::GetConnection()
715 {
716     SharedConnection xRet;
717     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
718     if(pSelect)
719     {
720         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
721         xRet = pUserData->xConnection;
722     }
723     return xRet;
724 }
725 /*-- 07.05.2004 14:17:48---------------------------------------------------
726 
727   -----------------------------------------------------------------------*/
728 uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
729 {
730     uno::Reference< XColumnsSupplier> xRet;
731     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
732     if(pSelect)
733     {
734         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
735         xRet = pUserData->xColumnsSupplier;
736     }
737     return xRet;
738 }
739 /*-- 14.05.2004 15:04:09---------------------------------------------------
740 
741   -----------------------------------------------------------------------*/
742 ::rtl::OUString     SwAddressListDialog::GetFilter()
743 {
744     ::rtl::OUString sRet;
745     SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
746     if(pSelect)
747     {
748         AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
749         sRet = pUserData->sFilter;
750     }
751     return sRet;
752 }
753