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