Update ooo320-m1
[ooovba.git] / sw / source / ui / dbui / addresslistdialog.cxx
blobea75d3f5584e9dc4a254d639805a360b05ab2e1e
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>
71 #include <svtools/urihelper.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 //#i97577# at this point the 'URL' can also be a file name!
149 sURL = URIHelper::SmartRel2Abs( INetURLObject(), sURL );
150 sURL += C2U("/");
151 sURL += aFilters[0];
152 sURL += C2U(".");
153 sURL += sExtension;
158 return sURL;
160 /*-- 07.04.2004 16:35:43---------------------------------------------------
162 -----------------------------------------------------------------------*/
163 SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent) :
164 SfxModalDialog(pParent, SW_RES(DLG_MM_ADDRESSLISTDIALOG)),
165 #ifdef MSC
166 #pragma warning (disable : 4355)
167 #endif
168 m_aDescriptionFI( this, SW_RES( FI_DESCRIPTION )),
169 m_aListFT( this, SW_RES( FT_LIST )),
170 m_aListHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER),
171 m_aListLB( this, SW_RES( LB_LIST )),
172 m_aLoadListPB( this, SW_RES( PB_LOADLIST )),
173 m_aCreateListPB(this, SW_RES( PB_CREATELIST )),
174 m_aFilterPB( this, SW_RES( PB_FILTER )),
175 m_aEditPB(this, SW_RES( PB_EDIT )),
176 m_aTablePB(this, SW_RES( PB_TABLE )),
177 m_aSeparatorFL(this, SW_RES( FL_SEPARATOR )),
178 m_aOK( this, SW_RES( PB_OK )),
179 m_aCancel( this, SW_RES( PB_CANCEL )),
180 m_aHelp( this, SW_RES( PB_HELP )),
181 #ifdef MSC
182 #pragma warning (default : 4355)
183 #endif
184 m_sName( SW_RES( ST_NAME )),
185 m_sTable( SW_RES( ST_TABLE )),
186 m_sConnecting( SW_RES( ST_CONNECTING )),
187 m_pCreatedDataSource(0),
188 m_bInSelectHdl(false),
189 m_pAddressPage(pParent)
191 FreeResource();
192 String sTemp(m_aDescriptionFI.GetText());
193 sTemp.SearchAndReplaceAscii("%1", m_aLoadListPB.GetText());
194 sTemp.SearchAndReplaceAscii("%2", m_aCreateListPB.GetText());
195 m_aDescriptionFI.SetText(sTemp);
196 m_aFilterPB.SetClickHdl( LINK( this, SwAddressListDialog, FilterHdl_Impl ));
197 m_aLoadListPB.SetClickHdl( LINK( this, SwAddressListDialog, LoadHdl_Impl ));
198 m_aCreateListPB.SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
199 m_aEditPB.SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
200 m_aTablePB.SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
202 Size aLBSize(m_aListLB.GetSizePixel());
203 m_aListHB.SetSizePixel(aLBSize);
204 Size aHeadSize(m_aListHB.CalcWindowSizePixel());
205 aHeadSize.Width() = aLBSize.Width();
206 m_aListHB.SetSizePixel(aHeadSize);
207 Point aLBPos(m_aListLB.GetPosPixel());
208 m_aListHB.SetPosPixel(aLBPos);
209 aLBPos.Y() += aHeadSize.Height();
210 aLBSize.Height() -= aHeadSize.Height();
211 m_aListLB.SetPosSizePixel(aLBPos, aLBSize);
213 Size aSz(m_aListHB.GetOutputSizePixel());
214 m_aListHB.InsertItem( ITEMID_NAME, m_sName,
215 aSz.Width()/2,
216 HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS/*| HIB_CLICKABLE | HIB_UPARROW */);
217 m_aListHB.InsertItem( ITEMID_TABLE, m_sTable,
218 aSz.Width()/2,
219 HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS /*| HIB_CLICKABLE | HIB_UPARROW */);
220 m_aListHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
221 m_aListHB.Show();
223 m_aListLB.SetHelpId(HID_MM_ADDRESSLIST_TLB);
224 static long nTabs[] = {2, 0, aSz.Width()/2 };
225 m_aListLB.SetWindowBits( WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
226 m_aListLB.SetSelectionMode( SINGLE_SELECTION );
227 m_aListLB.SetTabs(&nTabs[0], MAP_PIXEL);
228 m_aOK.SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
230 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
231 if( xMgr.is() )
233 uno::Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
234 m_xDBContext = uno::Reference<XNameAccess>(xInstance, UNO_QUERY) ;
236 SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
237 const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
239 DBG_ASSERT(m_xDBContext.is(), "service 'com.sun.star.sdb.DatabaseContext' not found!");
240 sal_Bool bEnableEdit = sal_False;
241 sal_Bool bEnableOK = sal_True;
242 m_aListLB.SelectAll( FALSE );
244 if(m_xDBContext.is())
246 SwDBConfig aDb;
247 ::rtl::OUString sBibliography = aDb.GetBibliographySource().sDataSource;
248 uno::Sequence< ::rtl::OUString> aNames = m_xDBContext->getElementNames();
249 const ::rtl::OUString* pNames = aNames.getConstArray();
250 for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
252 if ( pNames[nName] == sBibliography )
253 continue;
254 SvLBoxEntry* pEntry = m_aListLB.InsertEntry(pNames[nName]);
255 AddressUserData_Impl* pUserData = new AddressUserData_Impl();
256 pEntry->SetUserData(pUserData);
257 if(pNames[nName] == rCurrentData.sDataSource)
259 m_aListLB.Select(pEntry);
260 m_aListLB.SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
261 pUserData->nCommandType = rCurrentData.nCommandType;
262 pUserData->xSource = rConfigItem.GetSource();
263 pUserData->xConnection = rConfigItem.GetConnection();
264 pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
265 pUserData->xResultSet = rConfigItem.GetResultSet();
266 pUserData->sFilter = rConfigItem.GetFilter();
267 //is the data source editable (csv, Unicode, single table)
268 uno::Reference<beans::XPropertySet> xSourceProperties;
271 m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
272 pUserData->sURL = lcl_getFlatURL( xSourceProperties );
273 bEnableEdit = pUserData->sURL.getLength() > 0 &&
274 SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
275 !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
277 catch(const uno::Exception& )
279 bEnableOK = sal_False;
281 m_aDBData = rCurrentData;
285 m_aOK.Enable(m_aListLB.GetEntryCount()>0 && bEnableOK);
286 m_aEditPB.Enable(bEnableEdit);
287 m_aListLB.SetSelectHdl(LINK(this, SwAddressListDialog, ListBoxSelectHdl_Impl));
288 TableSelectHdl_Impl(NULL);
290 /*-- 07.04.2004 16:35:43---------------------------------------------------
292 -----------------------------------------------------------------------*/
293 SwAddressListDialog::~SwAddressListDialog()
295 SvLBoxEntry* pEntry = m_aListLB.First();
296 while(pEntry)
298 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
299 delete pUserData;
300 pEntry = m_aListLB.Next( pEntry );
303 /*-- 07.04.2004 16:35:44---------------------------------------------------
305 -----------------------------------------------------------------------*/
306 IMPL_LINK(SwAddressListDialog, FilterHdl_Impl, PushButton*, EMPTYARG)
308 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
309 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
310 if(pSelect && xMgr.is())
312 String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
313 if ( !sCommand.Len() )
314 return 0;
316 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
317 if(pUserData->xConnection.is() )
321 uno::Reference<lang::XMultiServiceFactory> xConnectFactory(pUserData->xConnection, UNO_QUERY_THROW);
322 uno::Reference<XSingleSelectQueryComposer> xComposer(
323 xConnectFactory->createInstance(C2U("com.sun.star.sdb.SingleSelectQueryComposer")), UNO_QUERY_THROW);
325 PropertyValue aSecond;
326 aSecond.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RowSet" ) );
327 uno::Reference<XRowSet> xRowSet(
328 xMgr->createInstance(C2U("com.sun.star.sdb.RowSet")), UNO_QUERY);
329 uno::Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
330 xRowProperties->setPropertyValue(C2U("DataSourceName"),
331 makeAny(OUString(m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1))));
332 xRowProperties->setPropertyValue(C2U("Command"), makeAny(
333 OUString(sCommand)));
334 xRowProperties->setPropertyValue(C2U("CommandType"), makeAny(pUserData->nCommandType));
335 xRowProperties->setPropertyValue(C2U("ActiveConnection"), makeAny(pUserData->xConnection.getTyped()));
336 xRowSet->execute();
337 aSecond.Value <<= xRowSet;
339 PropertyValue aFirst;
340 aFirst.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "QueryComposer" ) );
341 ::rtl::OUString sQuery;
342 xRowProperties->getPropertyValue(C2U("ActiveCommand"))>>= sQuery;
343 xComposer->setQuery(sQuery);
344 if(pUserData->sFilter.getLength())
345 xComposer->setFilter(pUserData->sFilter);
346 aFirst.Value <<= xComposer;
348 uno::Sequence<Any> aInit(2);
349 aInit[0] <<= aFirst;
350 aInit[1] <<= aSecond;
352 ::rtl::OUString sDialogServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.FilterDialog" ) );
353 uno::Reference< XExecutableDialog> xDialog(
354 xMgr->createInstanceWithArguments( sDialogServiceName, aInit ), UNO_QUERY);
356 if ( RET_OK == xDialog->execute() )
358 WaitObject aWO( NULL );
359 pUserData->sFilter = xComposer->getFilter();
361 ::comphelper::disposeComponent(xRowSet);
363 catch(Exception& )
365 DBG_ERROR("exception caught in SwAddressListDialog::FilterHdl_Impl");
369 return 0;
371 /*-- 07.04.2004 16:35:44---------------------------------------------------
373 -----------------------------------------------------------------------*/
374 IMPL_LINK(SwAddressListDialog, LoadHdl_Impl, PushButton*, EMPTYARG)
376 String sNewSource = SwNewDBMgr::LoadAndRegisterDataSource();
377 if(sNewSource.Len())
379 SvLBoxEntry* pNewSource = m_aListLB.InsertEntry(sNewSource);
380 pNewSource->SetUserData(new AddressUserData_Impl());
381 m_aListLB.Select(pNewSource);
383 return 0;
385 /*-- 07.04.2004 16:35:44---------------------------------------------------
387 -----------------------------------------------------------------------*/
388 IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
390 String sInputURL;
391 SwCreateAddressListDialog* pDlg =
392 new SwCreateAddressListDialog(
393 pButton,
394 sInputURL,
395 m_pAddressPage->GetWizard()->GetConfigItem());
396 if(RET_OK == pDlg->Execute())
398 //register the URL a new datasource
399 OUString sURL = pDlg->GetURL();
402 uno::Reference<XSingleServiceFactory> xFact( m_xDBContext, UNO_QUERY);
403 uno::Reference<XInterface> xNewInstance = xFact->createInstance();
404 INetURLObject aURL( sURL );
405 OUString sNewName = aURL.getBase();
406 //find a unique name if sNewName already exists
407 OUString sFind(sNewName);
408 sal_Int32 nIndex = 0;
409 while(m_xDBContext->hasByName(sFind))
411 sFind = sNewName;
412 sFind += OUString::valueOf(++nIndex);
414 uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
416 OUString sDBURL(C2U("sdbc:flat:"));
417 //only the 'path' has to be added
418 INetURLObject aTempURL(aURL);
419 aTempURL.removeSegment();
420 aTempURL.removeFinalSlash();
421 sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
422 Any aAny(&sDBURL, ::getCppuType(&sDBURL));
423 xDataProperties->setPropertyValue(C2U("URL"), aAny);
424 //set the filter to the file name without extension
425 uno::Sequence<OUString> aFilters(1);
426 aFilters[0] = sNewName;
427 aAny <<= aFilters;
428 xDataProperties->setPropertyValue(C2U("TableFilter"), aAny);
430 uno::Sequence<PropertyValue> aInfo(4);
431 PropertyValue* pInfo = aInfo.getArray();
432 pInfo[0].Name = C2U("FieldDelimiter");
433 pInfo[0].Value <<= OUString(String('\t'));
434 pInfo[1].Name = C2U("StringDelimiter");
435 pInfo[1].Value <<= OUString('"');
436 pInfo[2].Name = C2U("Extension");
437 pInfo[2].Value <<= ::rtl::OUString(aURL.getExtension());//C2U("csv");
438 pInfo[3].Name = C2U("CharSet");
439 pInfo[3].Value <<= C2U(cUTF8);
440 aAny <<= aInfo;
441 xDataProperties->setPropertyValue(C2U("Info"), aAny);
443 uno::Reference<sdb::XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
444 uno::Reference<frame::XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
445 String sExt = String::CreateFromAscii(".odb");
446 String sTmpName;
448 String sHomePath(SvtPathOptions().GetWorkPath());
449 utl::TempFile aTempFile(sFind , &sExt, &sHomePath);
450 aTempFile.EnableKillingFile(sal_True);
451 sTmpName = aTempFile.GetURL();
453 xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
456 uno::Reference<XNamingService> xNaming(m_xDBContext, UNO_QUERY);
457 xNaming->registerObject( sFind, xNewInstance );
458 //now insert the new source into the ListBox
459 String sEntry(sFind);
460 sEntry += '\t';
461 sEntry += String(aFilters[0]);
462 m_pCreatedDataSource = m_aListLB.InsertEntry(sEntry);
463 AddressUserData_Impl* pUserData = new AddressUserData_Impl();
464 pUserData->sURL = sURL;
465 m_pCreatedDataSource->SetUserData(pUserData);
466 m_aListLB.Select(m_pCreatedDataSource);
467 m_aCreateListPB.Enable(FALSE);
470 catch(Exception& )
474 delete pDlg;
475 return 0;
477 /*-- 22.04.2004 10:30:40---------------------------------------------------
479 -----------------------------------------------------------------------*/
480 IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
482 SvLBoxEntry* pEntry = m_aListLB.FirstSelected();
483 AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
484 if(pUserData && pUserData->sURL.getLength())
486 if(pUserData->xResultSet.is())
488 SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
489 if(rConfigItem.GetResultSet() != pUserData->xResultSet)
490 ::comphelper::disposeComponent( pUserData->xResultSet );
491 pUserData->xResultSet = 0;
493 rConfigItem.DisposeResultSet();
495 pUserData->xSource.clear();
496 pUserData->xColumnsSupplier.clear();
497 pUserData->xConnection.clear();
498 // will automatically close if it was the las reference
499 SwCreateAddressListDialog* pDlg =
500 new SwCreateAddressListDialog(
501 pButton,
502 pUserData->sURL,
503 m_pAddressPage->GetWizard()->GetConfigItem());
504 if(RET_OK == pDlg->Execute())
507 delete pDlg;
509 return 0;
511 /*-- 19.04.2004 09:41:05---------------------------------------------------
513 -----------------------------------------------------------------------*/
514 IMPL_LINK(SwAddressListDialog, ListBoxSelectHdl_Impl, SvTabListBox*, EMPTYARG)
516 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
517 Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
518 StaticListBoxSelectHdl_Impl ), pSelect );
519 return 0;
521 IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvLBoxEntry*, pSelect)
523 //prevent nested calls of the select handler
524 if(pThis->m_bInSelectHdl)
525 return 0;
526 pThis->EnterWait();
527 pThis->m_bInSelectHdl = true;
528 AddressUserData_Impl* pUserData = 0;
529 if(pSelect)
531 String sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
532 if(!sTable.Len())
534 pThis->m_aListLB.SetEntryText(pThis->m_sConnecting, pSelect, ITEMID_TABLE - 1);
535 // allow painting of the new entry
536 pThis->m_aListLB.Window::Invalidate(INVALIDATE_UPDATE);
537 for (USHORT i = 0; i < 10; i++)
538 Application::Reschedule();
541 pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
542 if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
544 pThis->DetectTablesAndQueries(pSelect, !sTable.Len());
546 else
548 //otherwise set the selected db-data
549 pThis->m_aDBData.sDataSource = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
550 pThis->m_aDBData.sCommand = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
551 pThis->m_aDBData.nCommandType = pUserData->nCommandType;
552 pThis->m_aOK.Enable(sal_True);
554 sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
555 if(sTable == pThis->m_sConnecting)
556 pThis->m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
558 pThis->m_aEditPB.Enable(pUserData && pUserData->sURL.getLength() &&
559 SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
560 !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
561 pThis->m_bInSelectHdl = false;
562 pThis->LeaveWait();
563 return 0;
566 /*-- 13.05.2004 14:59:25---------------------------------------------------
567 detect the number of tables for a data source
568 if only one is available then set it at the entry
569 -----------------------------------------------------------------------*/
570 void SwAddressListDialog::DetectTablesAndQueries(
571 SvLBoxEntry* pSelect,
572 bool bWidthDialog)
576 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
577 uno::Reference<XCompletedConnection> xComplConnection;
578 if(!pUserData->xConnection.is())
580 m_aDBData.sDataSource = m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
581 m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
582 pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
584 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
585 uno::Reference< XInteractionHandler > xHandler(
586 xMgr->createInstance( C2U( "com.sun.star.sdb.InteractionHandler" )), UNO_QUERY);
587 pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
589 if(pUserData->xConnection.is())
591 sal_Int32 nTables = 0;
592 uno::Sequence<rtl::OUString> aTables;
593 uno::Sequence<rtl::OUString> aQueries;
594 uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
595 if(xTSupplier.is())
597 uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
598 aTables = xTbls->getElementNames();
599 nTables += aTables.getLength();
601 uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
602 if(xQSupplier.is())
604 uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
605 aQueries = xQueries->getElementNames();
606 nTables += aQueries.getLength();
608 pUserData->nTableAndQueryCount = nTables;
609 if(nTables > 1 && bWidthDialog)
611 //now call the table select dialog - if more than one table exists
612 SwSelectDBTableDialog* pDlg = new SwSelectDBTableDialog(this, pUserData->xConnection);
613 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
614 if(sTable.Len())
615 pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
616 if(RET_OK == pDlg->Execute())
618 bool bIsTable;
619 m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
620 m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
621 pUserData->nCommandType = m_aDBData.nCommandType;
623 delete pDlg;
625 else if(nTables == 1)
627 if(aTables.getLength())
629 m_aDBData.sCommand = aTables[0];
630 m_aDBData.nCommandType = CommandType::TABLE;
632 else
634 m_aDBData.sCommand = aQueries[0];
635 m_aDBData.nCommandType = CommandType::QUERY;
639 if ( m_aDBData.sCommand.getLength() )
641 uno::Reference<beans::XPropertySet> xSourceProperties;
642 m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
643 pUserData->sURL = lcl_getFlatURL( xSourceProperties );
645 pUserData->xColumnsSupplier = SwNewDBMgr::GetColumnSupplier(pUserData->xConnection,
646 m_aDBData.sCommand,
647 m_aDBData.nCommandType == CommandType::TABLE ?
648 SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY );
649 //#i97577#
650 if( pUserData->xColumnsSupplier.is() )
651 m_aListLB.SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
652 else
653 m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
655 String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
656 m_aOK.Enable(pSelect && sCommand.Len());
657 m_aFilterPB.Enable( pUserData->xConnection.is() && sCommand.Len() );
658 m_aTablePB.Enable( pUserData->nTableAndQueryCount > 1 );
660 catch(Exception& )
662 DBG_ERROR("exception caught in SwAddressListDialog::DetectTablesAndQueries");
663 m_aOK.Enable( sal_False );
667 /*-- 13.05.2004 12:55:40---------------------------------------------------
669 -----------------------------------------------------------------------*/
670 IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
672 EnterWait();
673 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
674 if(pSelect)
676 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
677 //only call the table select dialog if tables have not been searched for or there
678 //are more than 1
679 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
680 if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
682 DetectTablesAndQueries(pSelect, (pButton != 0) || (!sTable.Len()));
686 LeaveWait();
687 return 0;
690 /*-- 08.04.2004 14:52:11---------------------------------------------------
692 -----------------------------------------------------------------------*/
693 IMPL_LINK(SwAddressListDialog, OKHdl_Impl, PushButton*, EMPTYARG)
695 EndDialog(TRUE);
696 return 0;
699 /*-- 07.05.2004 14:17:47---------------------------------------------------
701 -----------------------------------------------------------------------*/
702 uno::Reference< XDataSource> SwAddressListDialog::GetSource()
704 uno::Reference< XDataSource> xRet;
705 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
706 if(pSelect)
708 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
709 xRet = pUserData->xSource;
711 return xRet;
714 /*-- 07.05.2004 14:17:48---------------------------------------------------
716 -----------------------------------------------------------------------*/
717 SharedConnection SwAddressListDialog::GetConnection()
719 SharedConnection xRet;
720 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
721 if(pSelect)
723 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
724 xRet = pUserData->xConnection;
726 return xRet;
728 /*-- 07.05.2004 14:17:48---------------------------------------------------
730 -----------------------------------------------------------------------*/
731 uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
733 uno::Reference< XColumnsSupplier> xRet;
734 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
735 if(pSelect)
737 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
738 xRet = pUserData->xColumnsSupplier;
740 return xRet;
742 /*-- 14.05.2004 15:04:09---------------------------------------------------
744 -----------------------------------------------------------------------*/
745 ::rtl::OUString SwAddressListDialog::GetFilter()
747 ::rtl::OUString sRet;
748 SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
749 if(pSelect)
751 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
752 sRet = pUserData->sFilter;
754 return sRet;