update ooo310-m15
[ooovba.git] / sw / source / ui / dbui / addresslistdialog.cxx
blobaf4bc4edac7a0b712db896a8069e8d55360744f0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: addresslistdialog.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #ifdef SW_DLLIMPLEMENTATION
34 #undef SW_DLLIMPLEMENTATION
35 #endif
36 #include <swtypes.hxx>
37 #include <addresslistdialog.hxx>
38 #include <selectdbtabledialog.hxx>
39 #include <createaddresslistdialog.hxx>
40 #include <mailmergewizard.hxx>
41 #include <mmconfigitem.hxx>
42 #include <mmaddressblockpage.hxx>
43 #ifndef _DBMGR_HXX
44 #include <dbmgr.hxx>
45 #endif
46 #include <dbconfig.hxx>
47 #include <unotools/tempfile.hxx>
48 #include <vcl/msgbox.hxx>
49 #include <vcl/svapp.hxx>
50 #include <tools/urlobj.hxx>
51 #include <comphelper/processfactory.hxx>
52 #include <comphelper/types.hxx>
53 #include <com/sun/star/sdbc/XCloseable.hpp>
54 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
55 #include <com/sun/star/container/XNameAccess.hpp>
56 #include <com/sun/star/uno/XNamingService.hpp>
57 #include <com/sun/star/sdb/XCompletedConnection.hpp>
58 #include <com/sun/star/sdb/CommandType.hpp>
59 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
60 #include <com/sun/star/sdbc/XRowSet.hpp>
61 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSERFACTORY_HPP_
62 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
63 #endif
64 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
65 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
66 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
67 #include <com/sun/star/frame/XStorable.hpp>
68 #include <swunohelper.hxx>
69 #include <vcl/waitobj.hxx>
70 #include <svtools/pathoptions.hxx>
72 #include <addresslistdialog.hrc>
73 #include <dbui.hrc>
75 #include <helpid.h>
76 #include <unomid.h>
79 using namespace ::com::sun::star;
80 using namespace ::com::sun::star::uno;
81 using namespace ::com::sun::star::lang;
82 using namespace ::com::sun::star::container;
83 using namespace ::com::sun::star::sdb;
84 using namespace ::com::sun::star::sdbc;
85 using namespace ::com::sun::star::sdbcx;
86 using namespace ::com::sun::star::task;
87 using namespace ::com::sun::star::beans;
88 using namespace ::com::sun::star::ui::dialogs;
89 using namespace ::rtl;
91 #define ITEMID_NAME 1
92 #define ITEMID_TABLE 2
94 //typedef SharedUNOComponent< XConnection > SharedConnection;
96 static const char* cUTF8 = "UTF-8";
97 /*-- 07.05.2004 14:11:34---------------------------------------------------
99 -----------------------------------------------------------------------*/
100 struct AddressUserData_Impl
102 uno::Reference<XDataSource> xSource;
103 SharedConnection xConnection;
104 uno::Reference< XColumnsSupplier> xColumnsSupplier;
105 uno::Reference< sdbc::XResultSet> xResultSet;
106 ::rtl::OUString sFilter;
107 ::rtl::OUString sURL; // data is editable
108 sal_Int32 nCommandType;
109 sal_Int32 nTableAndQueryCount;
110 AddressUserData_Impl() :
111 nCommandType(0),
112 nTableAndQueryCount(-1)
115 ::rtl::OUString lcl_getFlatURL( uno::Reference<beans::XPropertySet>& xSourceProperties )
117 ::rtl::OUString sURL;
118 if(xSourceProperties.is())
120 rtl::OUString sDBURL;
121 xSourceProperties->getPropertyValue(C2U("URL")) >>= sDBURL;
122 if(String(sDBURL).SearchAscii("sdbc:flat:") == 0)
124 uno::Sequence<OUString> aFilters;
125 xSourceProperties->getPropertyValue(C2U("TableFilter")) >>= aFilters;
126 uno::Sequence<PropertyValue> aInfo;
127 xSourceProperties->getPropertyValue(C2U("Info")) >>= aInfo;
128 if(aFilters.getLength() == 1 && aInfo.getLength() )
130 ::rtl::OUString sFieldDelim;
131 ::rtl::OUString sStringDelim;
132 ::rtl::OUString sExtension;
133 ::rtl::OUString sCharSet;
134 for(sal_Int32 nInfo = 0; nInfo < aInfo.getLength(); ++nInfo)
136 if(aInfo[nInfo].Name == C2U("FieldDelimiter"))
137 aInfo[nInfo].Value >>= sFieldDelim;
138 else if(aInfo[nInfo].Name == C2U("StringDelimiter"))
139 aInfo[nInfo].Value >>= sStringDelim;
140 else if(aInfo[nInfo].Name == C2U("Extension"))
141 aInfo[nInfo].Value >>= sExtension;
142 else if(aInfo[nInfo].Name == C2U("CharSet"))
143 aInfo[nInfo].Value >>= sCharSet;
145 if(!sCharSet.compareToAscii( cUTF8 ))
147 sURL = String(sDBURL).Copy( 10 );
148 sURL += C2U("/");
149 sURL += aFilters[0];
150 sURL += C2U(".");
151 sURL += sExtension;
156 return sURL;
158 /*-- 07.04.2004 16:35:43---------------------------------------------------
160 -----------------------------------------------------------------------*/
161 SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent) :
162 SfxModalDialog(pParent, SW_RES(DLG_MM_ADDRESSLISTDIALOG)),
163 #ifdef MSC
164 #pragma warning (disable : 4355)
165 #endif
166 m_aDescriptionFI( this, SW_RES( FI_DESCRIPTION )),
167 m_aListFT( this, SW_RES( FT_LIST )),
168 m_aListHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER),
169 m_aListLB( this, SW_RES( LB_LIST )),
170 m_aLoadListPB( this, SW_RES( PB_LOADLIST )),
171 m_aCreateListPB(this, SW_RES( PB_CREATELIST )),
172 m_aFilterPB( this, SW_RES( PB_FILTER )),
173 m_aEditPB(this, SW_RES( PB_EDIT )),
174 m_aTablePB(this, SW_RES( PB_TABLE )),
175 m_aSeparatorFL(this, SW_RES( FL_SEPARATOR )),
176 m_aOK( this, SW_RES( PB_OK )),
177 m_aCancel( this, SW_RES( PB_CANCEL )),
178 m_aHelp( this, SW_RES( PB_HELP )),
179 #ifdef MSC
180 #pragma warning (default : 4355)
181 #endif
182 m_sName( SW_RES( ST_NAME )),
183 m_sTable( SW_RES( ST_TABLE )),
184 m_sConnecting( SW_RES( ST_CONNECTING )),
185 m_pCreatedDataSource(0),
186 m_bInSelectHdl(false),
187 m_pAddressPage(pParent)
189 FreeResource();
190 String sTemp(m_aDescriptionFI.GetText());
191 sTemp.SearchAndReplaceAscii("%1", m_aLoadListPB.GetText());
192 sTemp.SearchAndReplaceAscii("%2", m_aCreateListPB.GetText());
193 m_aDescriptionFI.SetText(sTemp);
194 m_aFilterPB.SetClickHdl( LINK( this, SwAddressListDialog, FilterHdl_Impl ));
195 m_aLoadListPB.SetClickHdl( LINK( this, SwAddressListDialog, LoadHdl_Impl ));
196 m_aCreateListPB.SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
197 m_aEditPB.SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
198 m_aTablePB.SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
200 Size aLBSize(m_aListLB.GetSizePixel());
201 m_aListHB.SetSizePixel(aLBSize);
202 Size aHeadSize(m_aListHB.CalcWindowSizePixel());
203 aHeadSize.Width() = aLBSize.Width();
204 m_aListHB.SetSizePixel(aHeadSize);
205 Point aLBPos(m_aListLB.GetPosPixel());
206 m_aListHB.SetPosPixel(aLBPos);
207 aLBPos.Y() += aHeadSize.Height();
208 aLBSize.Height() -= aHeadSize.Height();
209 m_aListLB.SetPosSizePixel(aLBPos, aLBSize);
211 Size aSz(m_aListHB.GetOutputSizePixel());
212 m_aListHB.InsertItem( ITEMID_NAME, m_sName,
213 aSz.Width()/2,
214 HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS/*| HIB_CLICKABLE | HIB_UPARROW */);
215 m_aListHB.InsertItem( ITEMID_TABLE, m_sTable,
216 aSz.Width()/2,
217 HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS /*| HIB_CLICKABLE | HIB_UPARROW */);
218 m_aListHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
219 m_aListHB.Show();
221 m_aListLB.SetHelpId(HID_MM_ADDRESSLIST_TLB);
222 static long nTabs[] = {2, 0, aSz.Width()/2 };
223 m_aListLB.SetWindowBits( WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
224 m_aListLB.SetSelectionMode( SINGLE_SELECTION );
225 m_aListLB.SetTabs(&nTabs[0], MAP_PIXEL);
226 m_aOK.SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
228 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
229 if( xMgr.is() )
231 uno::Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
232 m_xDBContext = uno::Reference<XNameAccess>(xInstance, UNO_QUERY) ;
234 SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
235 const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
237 DBG_ASSERT(m_xDBContext.is(), "service 'com.sun.star.sdb.DatabaseContext' not found!");
238 sal_Bool bEnableEdit = sal_False;
239 sal_Bool bEnableOK = sal_True;
240 m_aListLB.SelectAll( FALSE );
242 if(m_xDBContext.is())
244 SwDBConfig aDb;
245 ::rtl::OUString sBibliography = aDb.GetBibliographySource().sDataSource;
246 uno::Sequence< ::rtl::OUString> aNames = m_xDBContext->getElementNames();
247 const ::rtl::OUString* pNames = aNames.getConstArray();
248 for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
250 if ( pNames[nName] == sBibliography )
251 continue;
252 SvLBoxEntry* pEntry = m_aListLB.InsertEntry(pNames[nName]);
253 AddressUserData_Impl* pUserData = new AddressUserData_Impl();
254 pEntry->SetUserData(pUserData);
255 if(pNames[nName] == rCurrentData.sDataSource)
257 m_aListLB.Select(pEntry);
258 m_aListLB.SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
259 pUserData->nCommandType = rCurrentData.nCommandType;
260 pUserData->xSource = rConfigItem.GetSource();
261 pUserData->xConnection = rConfigItem.GetConnection();
262 pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
263 pUserData->xResultSet = rConfigItem.GetResultSet();
264 pUserData->sFilter = rConfigItem.GetFilter();
265 //is the data source editable (csv, Unicode, single table)
266 uno::Reference<beans::XPropertySet> xSourceProperties;
269 m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
270 pUserData->sURL = lcl_getFlatURL( xSourceProperties );
271 bEnableEdit = pUserData->sURL.getLength() > 0 &&
272 !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
274 catch(const uno::Exception& )
276 bEnableOK = sal_False;
278 m_aDBData = rCurrentData;
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);
287 /*-- 07.04.2004 16:35:43---------------------------------------------------
289 -----------------------------------------------------------------------*/
290 SwAddressListDialog::~SwAddressListDialog()
292 SvLBoxEntry* pEntry = m_aListLB.First();
293 while(pEntry)
295 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
296 delete pUserData;
297 pEntry = m_aListLB.Next( pEntry );
300 /*-- 07.04.2004 16:35:44---------------------------------------------------
302 -----------------------------------------------------------------------*/
303 IMPL_LINK(SwAddressListDialog, FilterHdl_Impl, PushButton*, EMPTYARG)
305 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
306 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
307 if(pSelect && xMgr.is())
309 String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
310 if ( !sCommand.Len() )
311 return 0;
313 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
314 if(pUserData->xConnection.is() )
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);
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;
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;
345 uno::Sequence<Any> aInit(2);
346 aInit[0] <<= aFirst;
347 aInit[1] <<= aSecond;
349 ::rtl::OUString sDialogServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.FilterDialog" ) );
350 uno::Reference< XExecutableDialog> xDialog(
351 xMgr->createInstanceWithArguments( sDialogServiceName, aInit ), UNO_QUERY);
353 if ( RET_OK == xDialog->execute() )
355 WaitObject aWO( NULL );
356 pUserData->sFilter = xComposer->getFilter();
358 ::comphelper::disposeComponent(xRowSet);
360 catch(Exception& )
362 DBG_ERROR("exception caught in SwAddressListDialog::FilterHdl_Impl");
366 return 0;
368 /*-- 07.04.2004 16:35:44---------------------------------------------------
370 -----------------------------------------------------------------------*/
371 IMPL_LINK(SwAddressListDialog, LoadHdl_Impl, PushButton*, EMPTYARG)
373 String sNewSource = SwNewDBMgr::LoadAndRegisterDataSource();
374 if(sNewSource.Len())
376 SvLBoxEntry* pNewSource = m_aListLB.InsertEntry(sNewSource);
377 pNewSource->SetUserData(new AddressUserData_Impl());
378 m_aListLB.Select(pNewSource);
380 return 0;
382 /*-- 07.04.2004 16:35:44---------------------------------------------------
384 -----------------------------------------------------------------------*/
385 IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
387 String sInputURL;
388 SwCreateAddressListDialog* pDlg =
389 new SwCreateAddressListDialog(
390 pButton,
391 sInputURL,
392 m_pAddressPage->GetWizard()->GetConfigItem());
393 if(RET_OK == pDlg->Execute())
395 //register the URL a new datasource
396 OUString sURL = pDlg->GetURL();
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))
408 sFind = sNewName;
409 sFind += OUString::valueOf(++nIndex);
411 uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
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);
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);
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;
445 String sHomePath(SvtPathOptions().GetWorkPath());
446 utl::TempFile aTempFile(sFind , &sExt, &sHomePath);
447 aTempFile.EnableKillingFile(sal_True);
448 sTmpName = aTempFile.GetURL();
450 xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
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(FALSE);
467 catch(Exception& )
471 delete pDlg;
472 return 0;
474 /*-- 22.04.2004 10:30:40---------------------------------------------------
476 -----------------------------------------------------------------------*/
477 IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
479 SvLBoxEntry* pEntry = m_aListLB.FirstSelected();
480 AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
481 if(pUserData && pUserData->sURL.getLength())
483 if(pUserData->xResultSet.is())
485 SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
486 if(rConfigItem.GetResultSet() != pUserData->xResultSet)
487 ::comphelper::disposeComponent( pUserData->xResultSet );
488 pUserData->xResultSet = 0;
490 rConfigItem.DisposeResultSet();
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())
504 delete pDlg;
506 return 0;
508 /*-- 19.04.2004 09:41:05---------------------------------------------------
510 -----------------------------------------------------------------------*/
511 IMPL_LINK(SwAddressListDialog, ListBoxSelectHdl_Impl, SvTabListBox*, EMPTYARG)
513 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
514 Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
515 StaticListBoxSelectHdl_Impl ), pSelect );
516 return 0;
518 IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvLBoxEntry*, pSelect)
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)
528 String sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
529 if(!sTable.Len())
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 (USHORT i = 0; i < 10; i++)
535 Application::Reschedule();
538 pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
539 if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
541 pThis->DetectTablesAndQueries(pSelect, !sTable.Len());
543 else
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);
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);
555 pThis->m_aEditPB.Enable(pUserData && pUserData->sURL.getLength() &&
556 !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
557 pThis->m_bInSelectHdl = false;
558 pThis->LeaveWait();
559 return 0;
562 /*-- 13.05.2004 14:59:25---------------------------------------------------
563 detect the number of tables for a data source
564 if only one is available then set it at the entry
565 -----------------------------------------------------------------------*/
566 void SwAddressListDialog::DetectTablesAndQueries(
567 SvLBoxEntry* pSelect,
568 bool bWidthDialog)
572 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
573 uno::Reference<XCompletedConnection> xComplConnection;
574 if(!pUserData->xConnection.is())
576 m_aDBData.sDataSource = m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
577 m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
578 pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
580 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
581 uno::Reference< XInteractionHandler > xHandler(
582 xMgr->createInstance( C2U( "com.sun.star.sdb.InteractionHandler" )), UNO_QUERY);
583 pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
585 if(pUserData->xConnection.is())
587 sal_Int32 nTables = 0;
588 uno::Sequence<rtl::OUString> aTables;
589 uno::Sequence<rtl::OUString> aQueries;
590 uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
591 if(xTSupplier.is())
593 uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
594 aTables = xTbls->getElementNames();
595 nTables += aTables.getLength();
597 uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
598 if(xQSupplier.is())
600 uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
601 aQueries = xQueries->getElementNames();
602 nTables += aQueries.getLength();
604 pUserData->nTableAndQueryCount = nTables;
605 if(nTables > 1 && bWidthDialog)
607 //now call the table select dialog - if more than one table exists
608 SwSelectDBTableDialog* pDlg = new SwSelectDBTableDialog(this, pUserData->xConnection);
609 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
610 if(sTable.Len())
611 pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
612 if(RET_OK == pDlg->Execute())
614 bool bIsTable;
615 m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
616 m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
617 pUserData->nCommandType = m_aDBData.nCommandType;
619 delete pDlg;
621 else if(nTables == 1)
623 if(aTables.getLength())
625 m_aDBData.sCommand = aTables[0];
626 m_aDBData.nCommandType = CommandType::TABLE;
628 else
630 m_aDBData.sCommand = aQueries[0];
631 m_aDBData.nCommandType = CommandType::QUERY;
635 if ( m_aDBData.sCommand.getLength() )
637 uno::Reference<beans::XPropertySet> xSourceProperties;
638 m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
639 pUserData->sURL = lcl_getFlatURL( xSourceProperties );
641 m_aListLB.SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
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 );
647 String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
648 m_aOK.Enable(pSelect && sCommand.Len());
649 m_aFilterPB.Enable( pUserData->xConnection.is() && sCommand.Len() );
650 m_aTablePB.Enable( pUserData->nTableAndQueryCount > 1 );
652 catch(Exception& )
654 DBG_ERROR("exception caught in SwAddressListDialog::DetectTablesAndQueries");
655 m_aOK.Enable( sal_False );
659 /*-- 13.05.2004 12:55:40---------------------------------------------------
661 -----------------------------------------------------------------------*/
662 IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
664 EnterWait();
665 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
666 if(pSelect)
668 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
669 //only call the table select dialog if tables have not been searched for or there
670 //are more than 1
671 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
672 if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
674 DetectTablesAndQueries(pSelect, (pButton != 0) || (!sTable.Len()));
678 LeaveWait();
679 return 0;
682 /*-- 08.04.2004 14:52:11---------------------------------------------------
684 -----------------------------------------------------------------------*/
685 IMPL_LINK(SwAddressListDialog, OKHdl_Impl, PushButton*, EMPTYARG)
687 EndDialog(TRUE);
688 return 0;
691 /*-- 07.05.2004 14:17:47---------------------------------------------------
693 -----------------------------------------------------------------------*/
694 uno::Reference< XDataSource> SwAddressListDialog::GetSource()
696 uno::Reference< XDataSource> xRet;
697 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
698 if(pSelect)
700 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
701 xRet = pUserData->xSource;
703 return xRet;
706 /*-- 07.05.2004 14:17:48---------------------------------------------------
708 -----------------------------------------------------------------------*/
709 SharedConnection SwAddressListDialog::GetConnection()
711 SharedConnection xRet;
712 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
713 if(pSelect)
715 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
716 xRet = pUserData->xConnection;
718 return xRet;
720 /*-- 07.05.2004 14:17:48---------------------------------------------------
722 -----------------------------------------------------------------------*/
723 uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
725 uno::Reference< XColumnsSupplier> xRet;
726 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
727 if(pSelect)
729 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
730 xRet = pUserData->xColumnsSupplier;
732 return xRet;
734 /*-- 14.05.2004 15:04:09---------------------------------------------------
736 -----------------------------------------------------------------------*/
737 ::rtl::OUString SwAddressListDialog::GetFilter()
739 ::rtl::OUString sRet;
740 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
741 if(pSelect)
743 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
744 sRet = pUserData->sFilter;
746 return sRet;