update credits
[LibreOffice.git] / sw / source / ui / dbui / addresslistdialog.cxx
blob35c0b6f07071fec4a1622b35b91a210da7302954
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <swtypes.hxx>
21 #include <addresslistdialog.hxx>
22 #include <selectdbtabledialog.hxx>
23 #include <createaddresslistdialog.hxx>
24 #include <mailmergewizard.hxx>
25 #include <mmconfigitem.hxx>
26 #include <mmaddressblockpage.hxx>
27 #include <dbmgr.hxx>
28 #include <dbconfig.hxx>
29 #include <unotools/tempfile.hxx>
30 #include <vcl/msgbox.hxx>
31 #include <vcl/svapp.hxx>
32 #include <tools/urlobj.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <comphelper/types.hxx>
35 #include "svtools/treelistentry.hxx"
36 #include <com/sun/star/sdbc/XCloseable.hpp>
37 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
39 #include <com/sun/star/uno/XNamingService.hpp>
40 #include <com/sun/star/sdb/DatabaseContext.hpp>
41 #include <com/sun/star/sdb/XCompletedConnection.hpp>
42 #include <com/sun/star/sdb/CommandType.hpp>
43 #include <com/sun/star/sdb/FilterDialog.hpp>
44 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
45 #include <com/sun/star/sdbc/XRowSet.hpp>
46 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
47 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
48 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
49 #include <com/sun/star/task/InteractionHandler.hpp>
50 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
51 #include <com/sun/star/frame/XStorable.hpp>
52 #include <swunohelper.hxx>
53 #include <vcl/waitobj.hxx>
54 #include <unotools/pathoptions.hxx>
55 #include <svl/urihelper.hxx>
56 #include <addresslistdialog.hrc>
57 #include <dbui.hrc>
59 #include <helpid.h>
60 #include <unomid.h>
63 using namespace ::com::sun::star;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::container;
67 using namespace ::com::sun::star::sdb;
68 using namespace ::com::sun::star::sdbc;
69 using namespace ::com::sun::star::sdbcx;
70 using namespace ::com::sun::star::task;
71 using namespace ::com::sun::star::beans;
72 using namespace ::com::sun::star::ui::dialogs;
73 using namespace ::rtl;
75 #define ITEMID_NAME 1
76 #define ITEMID_TABLE 2
78 static const char* cUTF8 = "UTF-8";
80 struct AddressUserData_Impl
82 uno::Reference<XDataSource> xSource;
83 SharedConnection xConnection;
84 uno::Reference< XColumnsSupplier> xColumnsSupplier;
85 uno::Reference< sdbc::XResultSet> xResultSet;
86 OUString sFilter;
87 OUString sURL; // data is editable
88 sal_Int32 nCommandType;
89 sal_Int32 nTableAndQueryCount;
90 AddressUserData_Impl() :
91 nCommandType(0),
92 nTableAndQueryCount(-1)
96 static OUString lcl_getFlatURL( uno::Reference<beans::XPropertySet>& xSourceProperties )
98 OUString sURL;
99 if(xSourceProperties.is())
101 OUString sDBURL;
102 xSourceProperties->getPropertyValue("URL") >>= sDBURL;
103 if(String(sDBURL).SearchAscii("sdbc:flat:") == 0)
105 uno::Sequence<OUString> aFilters;
106 xSourceProperties->getPropertyValue("TableFilter") >>= aFilters;
107 uno::Sequence<PropertyValue> aInfo;
108 xSourceProperties->getPropertyValue("Info") >>= aInfo;
109 if(aFilters.getLength() == 1 && aInfo.getLength() )
111 OUString sFieldDelim;
112 OUString sStringDelim;
113 OUString sExtension;
114 OUString sCharSet;
115 for(sal_Int32 nInfo = 0; nInfo < aInfo.getLength(); ++nInfo)
117 if(aInfo[nInfo].Name == "FieldDelimiter")
118 aInfo[nInfo].Value >>= sFieldDelim;
119 else if(aInfo[nInfo].Name == "StringDelimiter")
120 aInfo[nInfo].Value >>= sStringDelim;
121 else if(aInfo[nInfo].Name == "Extension")
122 aInfo[nInfo].Value >>= sExtension;
123 else if(aInfo[nInfo].Name == "CharSet")
124 aInfo[nInfo].Value >>= sCharSet;
126 if(!sCharSet.compareToAscii( cUTF8 ))
128 sURL = String(sDBURL).Copy( 10 );
129 //#i97577# at this point the 'URL' can also be a file name!
130 sURL = URIHelper::SmartRel2Abs( INetURLObject(), sURL );
131 sURL += "/";
132 sURL += aFilters[0];
133 sURL += ".";
134 sURL += sExtension;
139 return sURL;
142 SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent) :
143 SfxModalDialog(pParent, SW_RES(DLG_MM_ADDRESSLISTDIALOG)),
144 #ifdef _MSC_VER
145 #pragma warning (disable : 4355)
146 #endif
147 m_aDescriptionFI( this, SW_RES( FI_DESCRIPTION )),
148 m_aListFT( this, SW_RES( FT_LIST )),
149 m_aListHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER),
150 m_aListLB( this, SW_RES( LB_LIST )),
151 m_aLoadListPB( this, SW_RES( PB_LOADLIST )),
152 m_aCreateListPB(this, SW_RES( PB_CREATELIST )),
153 m_aFilterPB( this, SW_RES( PB_FILTER )),
154 m_aEditPB(this, SW_RES( PB_EDIT )),
155 m_aTablePB(this, SW_RES( PB_TABLE )),
156 m_aSeparatorFL(this, SW_RES( FL_SEPARATOR )),
157 m_aOK( this, SW_RES( PB_OK )),
158 m_aCancel( this, SW_RES( PB_CANCEL )),
159 m_aHelp( this, SW_RES( PB_HELP )),
160 #ifdef _MSC_VER
161 #pragma warning (default : 4355)
162 #endif
163 m_sName( SW_RES( ST_NAME )),
164 m_sTable( SW_RES( ST_TABLE )),
165 m_sConnecting( SW_RES( ST_CONNECTING )),
166 m_pCreatedDataSource(0),
167 m_bInSelectHdl(false),
168 m_pAddressPage(pParent)
170 FreeResource();
171 String sTemp(m_aDescriptionFI.GetText());
172 sTemp.SearchAndReplaceAscii("%1", m_aLoadListPB.GetText());
173 sTemp.SearchAndReplaceAscii("%2", m_aCreateListPB.GetText());
174 m_aDescriptionFI.SetText(sTemp);
175 m_aFilterPB.SetClickHdl( LINK( this, SwAddressListDialog, FilterHdl_Impl ));
176 m_aLoadListPB.SetClickHdl( LINK( this, SwAddressListDialog, LoadHdl_Impl ));
177 m_aCreateListPB.SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
178 m_aEditPB.SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
179 m_aTablePB.SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
181 Size aLBSize(m_aListLB.GetSizePixel());
182 m_aListHB.SetSizePixel(aLBSize);
183 Size aHeadSize(m_aListHB.CalcWindowSizePixel());
184 aHeadSize.Width() = aLBSize.Width();
185 m_aListHB.SetSizePixel(aHeadSize);
186 Point aLBPos(m_aListLB.GetPosPixel());
187 m_aListHB.SetPosPixel(aLBPos);
188 aLBPos.Y() += aHeadSize.Height();
189 aLBSize.Height() -= aHeadSize.Height();
190 m_aListLB.SetPosSizePixel(aLBPos, aLBSize);
192 Size aSz(m_aListHB.GetOutputSizePixel());
193 m_aListHB.InsertItem( ITEMID_NAME, m_sName,
194 aSz.Width()/2,
195 HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS/*| HIB_CLICKABLE | HIB_UPARROW */);
196 m_aListHB.InsertItem( ITEMID_TABLE, m_sTable,
197 aSz.Width()/2,
198 HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS /*| HIB_CLICKABLE | HIB_UPARROW */);
199 m_aListHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
200 m_aListHB.Show();
202 m_aListLB.SetHelpId(HID_MM_ADDRESSLIST_TLB);
203 static long nTabs[] = {2, 0, aSz.Width()/2 };
204 m_aListLB.SetStyle( m_aListLB.GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
205 m_aListLB.SetSelectionMode( SINGLE_SELECTION );
206 m_aListLB.SetTabs(&nTabs[0], MAP_PIXEL);
207 m_aOK.SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
209 uno::Reference<XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
210 m_xDBContext = DatabaseContext::create(xContext);
212 SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
213 const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
215 sal_Bool bEnableEdit = sal_False;
216 sal_Bool bEnableOK = sal_True;
217 m_aListLB.SelectAll( sal_False );
219 SwDBConfig aDb;
220 OUString sBibliography = aDb.GetBibliographySource().sDataSource;
221 uno::Sequence< OUString> aNames = m_xDBContext->getElementNames();
222 const OUString* pNames = aNames.getConstArray();
223 for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
225 if ( pNames[nName] == sBibliography )
226 continue;
227 SvTreeListEntry* pEntry = m_aListLB.InsertEntry(pNames[nName]);
228 AddressUserData_Impl* pUserData = new AddressUserData_Impl();
229 pEntry->SetUserData(pUserData);
230 if(pNames[nName] == rCurrentData.sDataSource)
232 m_aListLB.Select(pEntry);
233 m_aListLB.SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
234 pUserData->nCommandType = rCurrentData.nCommandType;
235 pUserData->xSource = rConfigItem.GetSource();
236 pUserData->xConnection = rConfigItem.GetConnection();
237 pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
238 pUserData->xResultSet = rConfigItem.GetResultSet();
239 pUserData->sFilter = rConfigItem.GetFilter();
240 //is the data source editable (csv, Unicode, single table)
241 uno::Reference<beans::XPropertySet> xSourceProperties;
244 m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
245 pUserData->sURL = lcl_getFlatURL( xSourceProperties );
246 bEnableEdit = !pUserData->sURL.isEmpty() &&
247 SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
248 !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
250 catch (const uno::Exception&)
252 bEnableOK = sal_False;
254 m_aDBData = rCurrentData;
258 m_aOK.Enable(m_aListLB.GetEntryCount()>0 && bEnableOK);
259 m_aEditPB.Enable(bEnableEdit);
260 m_aListLB.SetSelectHdl(LINK(this, SwAddressListDialog, ListBoxSelectHdl_Impl));
261 TableSelectHdl_Impl(NULL);
264 SwAddressListDialog::~SwAddressListDialog()
266 SvTreeListEntry* pEntry = m_aListLB.First();
267 while(pEntry)
269 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
270 delete pUserData;
271 pEntry = m_aListLB.Next( pEntry );
275 IMPL_LINK_NOARG(SwAddressListDialog, FilterHdl_Impl)
277 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
278 uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
279 if(pSelect)
281 String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
282 if ( !sCommand.Len() )
283 return 0;
285 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
286 if(pUserData->xConnection.is() )
290 uno::Reference<lang::XMultiServiceFactory> xConnectFactory(pUserData->xConnection, UNO_QUERY_THROW);
291 uno::Reference<XSingleSelectQueryComposer> xComposer(
292 xConnectFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"), UNO_QUERY_THROW);
294 uno::Reference<XRowSet> xRowSet(
295 xMgr->createInstance("com.sun.star.sdb.RowSet"), UNO_QUERY);
296 uno::Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
297 xRowProperties->setPropertyValue("DataSourceName",
298 makeAny(OUString(m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1))));
299 xRowProperties->setPropertyValue("Command", makeAny(
300 OUString(sCommand)));
301 xRowProperties->setPropertyValue("CommandType", makeAny(pUserData->nCommandType));
302 xRowProperties->setPropertyValue("ActiveConnection", makeAny(pUserData->xConnection.getTyped()));
303 xRowSet->execute();
305 OUString sQuery;
306 xRowProperties->getPropertyValue("ActiveCommand")>>= sQuery;
307 xComposer->setQuery(sQuery);
308 if(!pUserData->sFilter.isEmpty())
309 xComposer->setFilter(pUserData->sFilter);
311 uno::Reference< XExecutableDialog> xDialog = sdb::FilterDialog::createWithQuery( comphelper::getComponentContext(xMgr),
312 xComposer,xRowSet, uno::Reference<awt::XWindow>() );
314 if ( RET_OK == xDialog->execute() )
316 WaitObject aWO( NULL );
317 pUserData->sFilter = xComposer->getFilter();
319 ::comphelper::disposeComponent(xRowSet);
321 catch (const Exception&)
323 OSL_FAIL("exception caught in SwAddressListDialog::FilterHdl_Impl");
327 return 0;
330 IMPL_LINK_NOARG(SwAddressListDialog, LoadHdl_Impl)
332 String sNewSource = SwNewDBMgr::LoadAndRegisterDataSource();
333 if(sNewSource.Len())
335 SvTreeListEntry* pNewSource = m_aListLB.InsertEntry(sNewSource);
336 pNewSource->SetUserData(new AddressUserData_Impl());
337 m_aListLB.Select(pNewSource);
339 return 0;
342 IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
344 String sInputURL;
345 SwCreateAddressListDialog* pDlg =
346 new SwCreateAddressListDialog(
347 pButton,
348 sInputURL,
349 m_pAddressPage->GetWizard()->GetConfigItem());
350 if(RET_OK == pDlg->Execute())
352 //register the URL a new datasource
353 OUString sURL = pDlg->GetURL();
356 uno::Reference<XSingleServiceFactory> xFact( m_xDBContext, UNO_QUERY);
357 uno::Reference<XInterface> xNewInstance = xFact->createInstance();
358 INetURLObject aURL( sURL );
359 OUString sNewName = aURL.getBase();
360 //find a unique name if sNewName already exists
361 OUString sFind(sNewName);
362 sal_Int32 nIndex = 0;
363 while(m_xDBContext->hasByName(sFind))
365 sFind = sNewName;
366 sFind += OUString::valueOf(++nIndex);
368 uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
370 OUString sDBURL("sdbc:flat:");
371 //only the 'path' has to be added
372 INetURLObject aTempURL(aURL);
373 aTempURL.removeSegment();
374 aTempURL.removeFinalSlash();
375 sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
376 Any aAny(&sDBURL, ::getCppuType(&sDBURL));
377 xDataProperties->setPropertyValue("URL", aAny);
378 //set the filter to the file name without extension
379 uno::Sequence<OUString> aFilters(1);
380 aFilters[0] = sNewName;
381 aAny <<= aFilters;
382 xDataProperties->setPropertyValue("TableFilter", aAny);
384 uno::Sequence<PropertyValue> aInfo(4);
385 PropertyValue* pInfo = aInfo.getArray();
386 pInfo[0].Name = "FieldDelimiter";
387 pInfo[0].Value <<= OUString('\t');
388 pInfo[1].Name = "StringDelimiter";
389 pInfo[1].Value <<= OUString('"');
390 pInfo[2].Name = "Extension";
391 pInfo[2].Value <<= OUString(aURL.getExtension());//"csv";
392 pInfo[3].Name = "CharSet";
393 pInfo[3].Value <<= OUString::createFromAscii(cUTF8);
394 aAny <<= aInfo;
395 xDataProperties->setPropertyValue("Info", aAny);
397 uno::Reference<sdb::XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
398 uno::Reference<frame::XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
399 String sExt = OUString(".odb");
400 String sTmpName;
402 String sHomePath(SvtPathOptions().GetWorkPath());
403 utl::TempFile aTempFile(sFind , &sExt, &sHomePath);
404 aTempFile.EnableKillingFile(sal_True);
405 sTmpName = aTempFile.GetURL();
407 xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
410 uno::Reference<XNamingService> xNaming(m_xDBContext, UNO_QUERY);
411 xNaming->registerObject( sFind, xNewInstance );
412 //now insert the new source into the ListBox
413 String sEntry(sFind);
414 sEntry += '\t';
415 sEntry += String(aFilters[0]);
416 m_pCreatedDataSource = m_aListLB.InsertEntry(sEntry);
417 AddressUserData_Impl* pUserData = new AddressUserData_Impl();
418 pUserData->sURL = sURL;
419 m_pCreatedDataSource->SetUserData(pUserData);
420 m_aListLB.Select(m_pCreatedDataSource);
421 m_aCreateListPB.Enable(sal_False);
424 catch (const Exception&)
428 delete pDlg;
429 return 0;
432 IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
434 SvTreeListEntry* pEntry = m_aListLB.FirstSelected();
435 AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
436 if(pUserData && !pUserData->sURL.isEmpty())
438 if(pUserData->xResultSet.is())
440 SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
441 if(rConfigItem.GetResultSet() != pUserData->xResultSet)
442 ::comphelper::disposeComponent( pUserData->xResultSet );
443 pUserData->xResultSet = 0;
445 rConfigItem.DisposeResultSet();
447 pUserData->xSource.clear();
448 pUserData->xColumnsSupplier.clear();
449 pUserData->xConnection.clear();
450 // will automatically close if it was the las reference
451 SwCreateAddressListDialog* pDlg =
452 new SwCreateAddressListDialog(
453 pButton,
454 pUserData->sURL,
455 m_pAddressPage->GetWizard()->GetConfigItem());
456 if(RET_OK == pDlg->Execute())
459 delete pDlg;
461 return 0;
464 IMPL_LINK_NOARG(SwAddressListDialog, ListBoxSelectHdl_Impl)
466 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
467 Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
468 StaticListBoxSelectHdl_Impl ), pSelect );
469 return 0;
472 IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvTreeListEntry*, pSelect)
474 //prevent nested calls of the select handler
475 if(pThis->m_bInSelectHdl)
476 return 0;
477 pThis->EnterWait();
478 pThis->m_bInSelectHdl = true;
479 AddressUserData_Impl* pUserData = 0;
480 if(pSelect)
482 String sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
483 if(!sTable.Len())
485 pThis->m_aListLB.SetEntryText(pThis->m_sConnecting, pSelect, ITEMID_TABLE - 1);
486 // allow painting of the new entry
487 pThis->m_aListLB.Window::Invalidate(INVALIDATE_UPDATE);
488 for (sal_uInt16 i = 0; i < 10; i++)
489 Application::Reschedule();
492 pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
493 if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
496 * We're a callback from a selection from a list box, which takes
497 * place on mouse down before mouse up. The next dialog also has a
498 * list box. Spawning it means this list box doesn't get the mouse
499 * down event. So it sticks on "making selection" mode. So if you
500 * cancel the next dialog and just move the mouse out of this entry
501 * and back then the dialog pops up again, without requiring a click
503 * Most expedient thing to do is to manually end the parent selection
504 * here.
506 pThis->m_aListLB.EndSelection();
507 pThis->DetectTablesAndQueries(pSelect, !sTable.Len());
509 else
511 //otherwise set the selected db-data
512 pThis->m_aDBData.sDataSource = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
513 pThis->m_aDBData.sCommand = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
514 pThis->m_aDBData.nCommandType = pUserData->nCommandType;
515 pThis->m_aOK.Enable(sal_True);
517 sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
518 if(sTable == pThis->m_sConnecting)
519 pThis->m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
521 pThis->m_aEditPB.Enable(pUserData && !pUserData->sURL.isEmpty() &&
522 SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
523 !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
524 pThis->m_bInSelectHdl = false;
525 pThis->LeaveWait();
526 return 0;
529 // detect the number of tables for a data source
530 // if only one is available then set it at the entry
531 void SwAddressListDialog::DetectTablesAndQueries(
532 SvTreeListEntry* pSelect,
533 bool bWidthDialog)
537 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
538 uno::Reference<XCompletedConnection> xComplConnection;
539 if(!pUserData->xConnection.is())
541 m_aDBData.sDataSource = m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
542 m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
543 pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
545 uno::Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
546 uno::Reference< XInteractionHandler > xHandler( InteractionHandler::createWithParent(xContext, 0), UNO_QUERY );
547 pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
549 if(pUserData->xConnection.is())
551 sal_Int32 nTables = 0;
552 uno::Sequence<OUString> aTables;
553 uno::Sequence<OUString> aQueries;
554 uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
555 if(xTSupplier.is())
557 uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
558 aTables = xTbls->getElementNames();
559 nTables += aTables.getLength();
561 uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
562 if(xQSupplier.is())
564 uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
565 aQueries = xQueries->getElementNames();
566 nTables += aQueries.getLength();
568 pUserData->nTableAndQueryCount = nTables;
569 if(nTables > 1 && bWidthDialog)
571 //now call the table select dialog - if more than one table exists
572 SwSelectDBTableDialog* pDlg = new SwSelectDBTableDialog(this, pUserData->xConnection);
573 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
574 if(sTable.Len())
575 pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
576 if(RET_OK == pDlg->Execute())
578 bool bIsTable;
579 m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
580 m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
581 pUserData->nCommandType = m_aDBData.nCommandType;
583 delete pDlg;
585 else if(nTables == 1)
587 if(aTables.getLength())
589 m_aDBData.sCommand = aTables[0];
590 m_aDBData.nCommandType = CommandType::TABLE;
592 else
594 m_aDBData.sCommand = aQueries[0];
595 m_aDBData.nCommandType = CommandType::QUERY;
599 if ( !m_aDBData.sCommand.isEmpty() )
601 uno::Reference<beans::XPropertySet> xSourceProperties;
602 m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
603 pUserData->sURL = lcl_getFlatURL( xSourceProperties );
605 pUserData->xColumnsSupplier = SwNewDBMgr::GetColumnSupplier(pUserData->xConnection,
606 m_aDBData.sCommand,
607 m_aDBData.nCommandType == CommandType::TABLE ?
608 SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY );
609 //#i97577#
610 if( pUserData->xColumnsSupplier.is() )
611 m_aListLB.SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
612 else
613 m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
615 String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
616 m_aOK.Enable(pSelect && sCommand.Len());
617 m_aFilterPB.Enable( pUserData->xConnection.is() && sCommand.Len() );
618 m_aTablePB.Enable( pUserData->nTableAndQueryCount > 1 );
620 catch (const Exception&)
622 OSL_FAIL("exception caught in SwAddressListDialog::DetectTablesAndQueries");
623 m_aOK.Enable( sal_False );
627 IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
629 EnterWait();
630 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
631 if(pSelect)
633 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
634 //only call the table select dialog if tables have not been searched for or there
635 //are more than 1
636 String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
637 if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
639 DetectTablesAndQueries(pSelect, (pButton != 0) || (!sTable.Len()));
643 LeaveWait();
644 return 0;
647 IMPL_LINK_NOARG(SwAddressListDialog, OKHdl_Impl)
649 EndDialog(sal_True);
650 return 0;
653 uno::Reference< XDataSource> SwAddressListDialog::GetSource()
655 uno::Reference< XDataSource> xRet;
656 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
657 if(pSelect)
659 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
660 xRet = pUserData->xSource;
662 return xRet;
666 SharedConnection SwAddressListDialog::GetConnection()
668 SharedConnection xRet;
669 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
670 if(pSelect)
672 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
673 xRet = pUserData->xConnection;
675 return xRet;
678 uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
680 uno::Reference< XColumnsSupplier> xRet;
681 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
682 if(pSelect)
684 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
685 xRet = pUserData->xColumnsSupplier;
687 return xRet;
690 OUString SwAddressListDialog::GetFilter()
692 OUString sRet;
693 SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
694 if(pSelect)
696 AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
697 sRet = pUserData->sFilter;
699 return sRet;
702 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */