Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
58 :
59 : #include <helpid.h>
60 : #include <unomid.h>
61 :
62 :
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;
74 :
75 : #define ITEMID_NAME 1
76 : #define ITEMID_TABLE 2
77 :
78 : static const char* cUTF8 = "UTF-8";
79 :
80 0 : struct AddressUserData_Impl
81 : {
82 : uno::Reference<XDataSource> xSource;
83 : SharedConnection xConnection;
84 : uno::Reference< XColumnsSupplier> xColumnsSupplier;
85 : uno::Reference< sdbc::XResultSet> xResultSet;
86 : ::rtl::OUString sFilter;
87 : ::rtl::OUString sURL; // data is editable
88 : sal_Int32 nCommandType;
89 : sal_Int32 nTableAndQueryCount;
90 0 : AddressUserData_Impl() :
91 : nCommandType(0),
92 0 : nTableAndQueryCount(-1)
93 0 : {}
94 : };
95 :
96 0 : static ::rtl::OUString lcl_getFlatURL( uno::Reference<beans::XPropertySet>& xSourceProperties )
97 : {
98 0 : ::rtl::OUString sURL;
99 0 : if(xSourceProperties.is())
100 : {
101 0 : rtl::OUString sDBURL;
102 0 : xSourceProperties->getPropertyValue("URL") >>= sDBURL;
103 0 : if(String(sDBURL).SearchAscii("sdbc:flat:") == 0)
104 : {
105 0 : uno::Sequence<OUString> aFilters;
106 0 : xSourceProperties->getPropertyValue("TableFilter") >>= aFilters;
107 0 : uno::Sequence<PropertyValue> aInfo;
108 0 : xSourceProperties->getPropertyValue("Info") >>= aInfo;
109 0 : if(aFilters.getLength() == 1 && aInfo.getLength() )
110 : {
111 0 : ::rtl::OUString sFieldDelim;
112 0 : ::rtl::OUString sStringDelim;
113 0 : ::rtl::OUString sExtension;
114 0 : ::rtl::OUString sCharSet;
115 0 : for(sal_Int32 nInfo = 0; nInfo < aInfo.getLength(); ++nInfo)
116 : {
117 0 : if(aInfo[nInfo].Name == "FieldDelimiter")
118 0 : aInfo[nInfo].Value >>= sFieldDelim;
119 0 : else if(aInfo[nInfo].Name == "StringDelimiter")
120 0 : aInfo[nInfo].Value >>= sStringDelim;
121 0 : else if(aInfo[nInfo].Name == "Extension")
122 0 : aInfo[nInfo].Value >>= sExtension;
123 0 : else if(aInfo[nInfo].Name == "CharSet")
124 0 : aInfo[nInfo].Value >>= sCharSet;
125 : }
126 0 : if(!sCharSet.compareToAscii( cUTF8 ))
127 : {
128 0 : sURL = String(sDBURL).Copy( 10 );
129 : //#i97577# at this point the 'URL' can also be a file name!
130 0 : sURL = URIHelper::SmartRel2Abs( INetURLObject(), sURL );
131 0 : sURL += "/";
132 0 : sURL += aFilters[0];
133 0 : sURL += ".";
134 0 : sURL += sExtension;
135 0 : }
136 0 : }
137 0 : }
138 : }
139 0 : return sURL;
140 : }
141 :
142 0 : SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent) :
143 : SfxModalDialog(pParent, SW_RES(DLG_MM_ADDRESSLISTDIALOG)),
144 : #ifdef MSC
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
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 0 : m_pAddressPage(pParent)
169 : {
170 0 : FreeResource();
171 0 : String sTemp(m_aDescriptionFI.GetText());
172 0 : sTemp.SearchAndReplaceAscii("%1", m_aLoadListPB.GetText());
173 0 : sTemp.SearchAndReplaceAscii("%2", m_aCreateListPB.GetText());
174 0 : m_aDescriptionFI.SetText(sTemp);
175 0 : m_aFilterPB.SetClickHdl( LINK( this, SwAddressListDialog, FilterHdl_Impl ));
176 0 : m_aLoadListPB.SetClickHdl( LINK( this, SwAddressListDialog, LoadHdl_Impl ));
177 0 : m_aCreateListPB.SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
178 0 : m_aEditPB.SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
179 0 : m_aTablePB.SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
180 :
181 0 : Size aLBSize(m_aListLB.GetSizePixel());
182 0 : m_aListHB.SetSizePixel(aLBSize);
183 0 : Size aHeadSize(m_aListHB.CalcWindowSizePixel());
184 0 : aHeadSize.Width() = aLBSize.Width();
185 0 : m_aListHB.SetSizePixel(aHeadSize);
186 0 : Point aLBPos(m_aListLB.GetPosPixel());
187 0 : m_aListHB.SetPosPixel(aLBPos);
188 0 : aLBPos.Y() += aHeadSize.Height();
189 0 : aLBSize.Height() -= aHeadSize.Height();
190 0 : m_aListLB.SetPosSizePixel(aLBPos, aLBSize);
191 :
192 0 : Size aSz(m_aListHB.GetOutputSizePixel());
193 : m_aListHB.InsertItem( ITEMID_NAME, m_sName,
194 0 : aSz.Width()/2,
195 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS/*| HIB_CLICKABLE | HIB_UPARROW */);
196 : m_aListHB.InsertItem( ITEMID_TABLE, m_sTable,
197 0 : aSz.Width()/2,
198 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS /*| HIB_CLICKABLE | HIB_UPARROW */);
199 0 : m_aListHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
200 0 : m_aListHB.Show();
201 :
202 0 : m_aListLB.SetHelpId(HID_MM_ADDRESSLIST_TLB);
203 0 : static long nTabs[] = {2, 0, aSz.Width()/2 };
204 0 : m_aListLB.SetStyle( m_aListLB.GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
205 0 : m_aListLB.SetSelectionMode( SINGLE_SELECTION );
206 0 : m_aListLB.SetTabs(&nTabs[0], MAP_PIXEL);
207 0 : m_aOK.SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
208 :
209 0 : uno::Reference<XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
210 0 : m_xDBContext = DatabaseContext::create(xContext);
211 :
212 0 : SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
213 0 : const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
214 :
215 0 : sal_Bool bEnableEdit = sal_False;
216 0 : sal_Bool bEnableOK = sal_True;
217 0 : m_aListLB.SelectAll( sal_False );
218 :
219 0 : SwDBConfig aDb;
220 0 : ::rtl::OUString sBibliography = aDb.GetBibliographySource().sDataSource;
221 0 : uno::Sequence< ::rtl::OUString> aNames = m_xDBContext->getElementNames();
222 0 : const ::rtl::OUString* pNames = aNames.getConstArray();
223 0 : for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
224 : {
225 0 : if ( pNames[nName] == sBibliography )
226 0 : continue;
227 0 : SvTreeListEntry* pEntry = m_aListLB.InsertEntry(pNames[nName]);
228 0 : AddressUserData_Impl* pUserData = new AddressUserData_Impl();
229 0 : pEntry->SetUserData(pUserData);
230 0 : if(pNames[nName] == rCurrentData.sDataSource)
231 : {
232 0 : m_aListLB.Select(pEntry);
233 0 : m_aListLB.SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
234 0 : pUserData->nCommandType = rCurrentData.nCommandType;
235 0 : pUserData->xSource = rConfigItem.GetSource();
236 0 : pUserData->xConnection = rConfigItem.GetConnection();
237 0 : pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
238 0 : pUserData->xResultSet = rConfigItem.GetResultSet();
239 0 : pUserData->sFilter = rConfigItem.GetFilter();
240 : //is the data source editable (csv, Unicode, single table)
241 0 : uno::Reference<beans::XPropertySet> xSourceProperties;
242 : try
243 : {
244 0 : m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
245 0 : pUserData->sURL = lcl_getFlatURL( xSourceProperties );
246 0 : bEnableEdit = !pUserData->sURL.isEmpty() &&
247 0 : SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
248 0 : !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
249 : }
250 0 : catch (const uno::Exception&)
251 : {
252 0 : bEnableOK = sal_False;
253 : }
254 0 : m_aDBData = rCurrentData;
255 : }
256 : }
257 :
258 0 : m_aOK.Enable(m_aListLB.GetEntryCount()>0 && bEnableOK);
259 0 : m_aEditPB.Enable(bEnableEdit);
260 0 : m_aListLB.SetSelectHdl(LINK(this, SwAddressListDialog, ListBoxSelectHdl_Impl));
261 0 : TableSelectHdl_Impl(NULL);
262 0 : }
263 :
264 0 : SwAddressListDialog::~SwAddressListDialog()
265 : {
266 0 : SvTreeListEntry* pEntry = m_aListLB.First();
267 0 : while(pEntry)
268 : {
269 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
270 0 : delete pUserData;
271 0 : pEntry = m_aListLB.Next( pEntry );
272 : }
273 0 : }
274 :
275 0 : IMPL_LINK_NOARG(SwAddressListDialog, FilterHdl_Impl)
276 : {
277 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
278 0 : uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
279 0 : if(pSelect && xMgr.is())
280 : {
281 0 : String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
282 0 : if ( !sCommand.Len() )
283 0 : return 0;
284 :
285 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
286 0 : if(pUserData->xConnection.is() )
287 : {
288 : try
289 : {
290 0 : uno::Reference<lang::XMultiServiceFactory> xConnectFactory(pUserData->xConnection, UNO_QUERY_THROW);
291 : uno::Reference<XSingleSelectQueryComposer> xComposer(
292 0 : xConnectFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"), UNO_QUERY_THROW);
293 :
294 : uno::Reference<XRowSet> xRowSet(
295 0 : xMgr->createInstance("com.sun.star.sdb.RowSet"), UNO_QUERY);
296 0 : uno::Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
297 0 : xRowProperties->setPropertyValue("DataSourceName",
298 0 : makeAny(OUString(m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1))));
299 0 : xRowProperties->setPropertyValue("Command", makeAny(
300 0 : OUString(sCommand)));
301 0 : xRowProperties->setPropertyValue("CommandType", makeAny(pUserData->nCommandType));
302 0 : xRowProperties->setPropertyValue("ActiveConnection", makeAny(pUserData->xConnection.getTyped()));
303 0 : xRowSet->execute();
304 :
305 0 : ::rtl::OUString sQuery;
306 0 : xRowProperties->getPropertyValue("ActiveCommand")>>= sQuery;
307 0 : xComposer->setQuery(sQuery);
308 0 : if(!pUserData->sFilter.isEmpty())
309 0 : xComposer->setFilter(pUserData->sFilter);
310 :
311 : uno::Reference< XExecutableDialog> xDialog = sdb::FilterDialog::createWithQuery( comphelper::getComponentContext(xMgr),
312 0 : xComposer,xRowSet, uno::Reference<awt::XWindow>() );
313 :
314 0 : if ( RET_OK == xDialog->execute() )
315 : {
316 0 : WaitObject aWO( NULL );
317 0 : pUserData->sFilter = xComposer->getFilter();
318 : }
319 0 : ::comphelper::disposeComponent(xRowSet);
320 : }
321 0 : catch (const Exception&)
322 : {
323 : OSL_FAIL("exception caught in SwAddressListDialog::FilterHdl_Impl");
324 : }
325 0 : }
326 : }
327 0 : return 0;
328 : }
329 :
330 0 : IMPL_LINK_NOARG(SwAddressListDialog, LoadHdl_Impl)
331 : {
332 0 : String sNewSource = SwNewDBMgr::LoadAndRegisterDataSource();
333 0 : if(sNewSource.Len())
334 : {
335 0 : SvTreeListEntry* pNewSource = m_aListLB.InsertEntry(sNewSource);
336 0 : pNewSource->SetUserData(new AddressUserData_Impl());
337 0 : m_aListLB.Select(pNewSource);
338 : }
339 0 : return 0;
340 : }
341 :
342 0 : IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
343 : {
344 0 : String sInputURL;
345 : SwCreateAddressListDialog* pDlg =
346 : new SwCreateAddressListDialog(
347 : pButton,
348 : sInputURL,
349 0 : m_pAddressPage->GetWizard()->GetConfigItem());
350 0 : if(RET_OK == pDlg->Execute())
351 : {
352 : //register the URL a new datasource
353 0 : OUString sURL = pDlg->GetURL();
354 : try
355 : {
356 0 : uno::Reference<XSingleServiceFactory> xFact( m_xDBContext, UNO_QUERY);
357 0 : uno::Reference<XInterface> xNewInstance = xFact->createInstance();
358 0 : INetURLObject aURL( sURL );
359 0 : OUString sNewName = aURL.getBase();
360 : //find a unique name if sNewName already exists
361 0 : OUString sFind(sNewName);
362 0 : sal_Int32 nIndex = 0;
363 0 : while(m_xDBContext->hasByName(sFind))
364 : {
365 0 : sFind = sNewName;
366 0 : sFind += OUString::valueOf(++nIndex);
367 : }
368 0 : uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
369 :
370 0 : OUString sDBURL("sdbc:flat:");
371 : //only the 'path' has to be added
372 0 : INetURLObject aTempURL(aURL);
373 0 : aTempURL.removeSegment();
374 0 : aTempURL.removeFinalSlash();
375 0 : sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
376 0 : Any aAny(&sDBURL, ::getCppuType(&sDBURL));
377 0 : xDataProperties->setPropertyValue("URL", aAny);
378 : //set the filter to the file name without extension
379 0 : uno::Sequence<OUString> aFilters(1);
380 0 : aFilters[0] = sNewName;
381 0 : aAny <<= aFilters;
382 0 : xDataProperties->setPropertyValue("TableFilter", aAny);
383 :
384 0 : uno::Sequence<PropertyValue> aInfo(4);
385 0 : PropertyValue* pInfo = aInfo.getArray();
386 0 : pInfo[0].Name = "FieldDelimiter";
387 0 : pInfo[0].Value <<= OUString('\t');
388 0 : pInfo[1].Name = "StringDelimiter";
389 0 : pInfo[1].Value <<= OUString('"');
390 0 : pInfo[2].Name = "Extension";
391 0 : pInfo[2].Value <<= ::rtl::OUString(aURL.getExtension());//"csv";
392 0 : pInfo[3].Name = "CharSet";
393 0 : pInfo[3].Value <<= rtl::OUString::createFromAscii(cUTF8);
394 0 : aAny <<= aInfo;
395 0 : xDataProperties->setPropertyValue("Info", aAny);
396 :
397 0 : uno::Reference<sdb::XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
398 0 : uno::Reference<frame::XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
399 0 : String sExt = rtl::OUString(".odb");
400 0 : String sTmpName;
401 : {
402 0 : String sHomePath(SvtPathOptions().GetWorkPath());
403 0 : utl::TempFile aTempFile(sFind , &sExt, &sHomePath);
404 0 : aTempFile.EnableKillingFile(sal_True);
405 0 : sTmpName = aTempFile.GetURL();
406 : }
407 0 : xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
408 :
409 :
410 0 : uno::Reference<XNamingService> xNaming(m_xDBContext, UNO_QUERY);
411 0 : xNaming->registerObject( sFind, xNewInstance );
412 : //now insert the new source into the ListBox
413 0 : String sEntry(sFind);
414 0 : sEntry += '\t';
415 0 : sEntry += String(aFilters[0]);
416 0 : m_pCreatedDataSource = m_aListLB.InsertEntry(sEntry);
417 0 : AddressUserData_Impl* pUserData = new AddressUserData_Impl();
418 0 : pUserData->sURL = sURL;
419 0 : m_pCreatedDataSource->SetUserData(pUserData);
420 0 : m_aListLB.Select(m_pCreatedDataSource);
421 0 : m_aCreateListPB.Enable(sal_False);
422 :
423 : }
424 0 : catch (const Exception&)
425 : {
426 0 : }
427 : }
428 0 : delete pDlg;
429 0 : return 0;
430 : }
431 :
432 0 : IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
433 : {
434 0 : SvTreeListEntry* pEntry = m_aListLB.FirstSelected();
435 0 : AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
436 0 : if(pUserData && !pUserData->sURL.isEmpty())
437 : {
438 0 : if(pUserData->xResultSet.is())
439 : {
440 0 : SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
441 0 : if(rConfigItem.GetResultSet() != pUserData->xResultSet)
442 0 : ::comphelper::disposeComponent( pUserData->xResultSet );
443 0 : pUserData->xResultSet = 0;
444 :
445 0 : rConfigItem.DisposeResultSet();
446 : }
447 0 : pUserData->xSource.clear();
448 0 : pUserData->xColumnsSupplier.clear();
449 0 : 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 0 : m_pAddressPage->GetWizard()->GetConfigItem());
456 0 : if(RET_OK == pDlg->Execute())
457 : {
458 : }
459 0 : delete pDlg;
460 : }
461 0 : return 0;
462 : };
463 :
464 0 : IMPL_LINK_NOARG(SwAddressListDialog, ListBoxSelectHdl_Impl)
465 : {
466 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
467 : Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
468 0 : StaticListBoxSelectHdl_Impl ), pSelect );
469 0 : return 0;
470 : }
471 :
472 0 : IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvTreeListEntry*, pSelect)
473 : {
474 : //prevent nested calls of the select handler
475 0 : if(pThis->m_bInSelectHdl)
476 0 : return 0;
477 0 : pThis->EnterWait();
478 0 : pThis->m_bInSelectHdl = true;
479 0 : AddressUserData_Impl* pUserData = 0;
480 0 : if(pSelect)
481 : {
482 0 : String sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
483 0 : if(!sTable.Len())
484 : {
485 0 : pThis->m_aListLB.SetEntryText(pThis->m_sConnecting, pSelect, ITEMID_TABLE - 1);
486 : // allow painting of the new entry
487 0 : pThis->m_aListLB.Window::Invalidate(INVALIDATE_UPDATE);
488 0 : for (sal_uInt16 i = 0; i < 10; i++)
489 0 : Application::Reschedule();
490 : }
491 :
492 0 : pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
493 0 : if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
494 : {
495 : /*
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
502 : *
503 : * Most expedient thing to do is to manually end the parent selection
504 : * here.
505 : */
506 0 : pThis->m_aListLB.EndSelection();
507 0 : pThis->DetectTablesAndQueries(pSelect, !sTable.Len());
508 : }
509 : else
510 : {
511 : //otherwise set the selected db-data
512 0 : pThis->m_aDBData.sDataSource = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
513 0 : pThis->m_aDBData.sCommand = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
514 0 : pThis->m_aDBData.nCommandType = pUserData->nCommandType;
515 0 : pThis->m_aOK.Enable(sal_True);
516 : }
517 0 : sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
518 0 : if(sTable == pThis->m_sConnecting)
519 0 : pThis->m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
520 : }
521 0 : pThis->m_aEditPB.Enable(pUserData && !pUserData->sURL.isEmpty() &&
522 0 : SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
523 0 : !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
524 0 : pThis->m_bInSelectHdl = false;
525 0 : pThis->LeaveWait();
526 0 : return 0;
527 : }
528 :
529 : // detect the number of tables for a data source
530 : // if only one is available then set it at the entry
531 0 : void SwAddressListDialog::DetectTablesAndQueries(
532 : SvTreeListEntry* pSelect,
533 : bool bWidthDialog)
534 : {
535 : try
536 : {
537 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
538 0 : uno::Reference<XCompletedConnection> xComplConnection;
539 0 : if(!pUserData->xConnection.is())
540 : {
541 0 : m_aDBData.sDataSource = m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
542 0 : m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
543 0 : pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
544 :
545 0 : uno::Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
546 0 : uno::Reference< XInteractionHandler > xHandler( InteractionHandler::createWithParent(xContext, 0), UNO_QUERY );
547 0 : pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
548 : }
549 0 : if(pUserData->xConnection.is())
550 : {
551 0 : sal_Int32 nTables = 0;
552 0 : uno::Sequence<rtl::OUString> aTables;
553 0 : uno::Sequence<rtl::OUString> aQueries;
554 0 : uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
555 0 : if(xTSupplier.is())
556 : {
557 0 : uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
558 0 : aTables = xTbls->getElementNames();
559 0 : nTables += aTables.getLength();
560 : }
561 0 : uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
562 0 : if(xQSupplier.is())
563 : {
564 0 : uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
565 0 : aQueries = xQueries->getElementNames();
566 0 : nTables += aQueries.getLength();
567 : }
568 0 : pUserData->nTableAndQueryCount = nTables;
569 0 : if(nTables > 1 && bWidthDialog)
570 : {
571 : //now call the table select dialog - if more than one table exists
572 0 : SwSelectDBTableDialog* pDlg = new SwSelectDBTableDialog(this, pUserData->xConnection);
573 0 : String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
574 0 : if(sTable.Len())
575 0 : pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
576 0 : if(RET_OK == pDlg->Execute())
577 : {
578 : bool bIsTable;
579 0 : m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
580 0 : m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
581 0 : pUserData->nCommandType = m_aDBData.nCommandType;
582 : }
583 0 : delete pDlg;
584 : }
585 0 : else if(nTables == 1)
586 : {
587 0 : if(aTables.getLength())
588 : {
589 0 : m_aDBData.sCommand = aTables[0];
590 0 : m_aDBData.nCommandType = CommandType::TABLE;
591 : }
592 : else
593 : {
594 0 : m_aDBData.sCommand = aQueries[0];
595 0 : m_aDBData.nCommandType = CommandType::QUERY;
596 : }
597 0 : }
598 : }
599 0 : if ( !m_aDBData.sCommand.isEmpty() )
600 : {
601 0 : uno::Reference<beans::XPropertySet> xSourceProperties;
602 0 : m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
603 0 : pUserData->sURL = lcl_getFlatURL( xSourceProperties );
604 :
605 : pUserData->xColumnsSupplier = SwNewDBMgr::GetColumnSupplier(pUserData->xConnection,
606 : m_aDBData.sCommand,
607 : m_aDBData.nCommandType == CommandType::TABLE ?
608 0 : SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY );
609 : //#i97577#
610 0 : if( pUserData->xColumnsSupplier.is() )
611 0 : m_aListLB.SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
612 : else
613 0 : m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
614 : }
615 0 : String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
616 0 : m_aOK.Enable(pSelect && sCommand.Len());
617 0 : m_aFilterPB.Enable( pUserData->xConnection.is() && sCommand.Len() );
618 0 : m_aTablePB.Enable( pUserData->nTableAndQueryCount > 1 );
619 : }
620 0 : catch (const Exception&)
621 : {
622 : OSL_FAIL("exception caught in SwAddressListDialog::DetectTablesAndQueries");
623 0 : m_aOK.Enable( sal_False );
624 : }
625 0 : }
626 :
627 0 : IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
628 : {
629 0 : EnterWait();
630 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
631 0 : if(pSelect)
632 : {
633 0 : 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 0 : String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
637 0 : if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
638 : {
639 0 : DetectTablesAndQueries(pSelect, (pButton != 0) || (!sTable.Len()));
640 0 : }
641 : }
642 :
643 0 : LeaveWait();
644 0 : return 0;
645 : }
646 :
647 0 : IMPL_LINK_NOARG(SwAddressListDialog, OKHdl_Impl)
648 : {
649 0 : EndDialog(sal_True);
650 0 : return 0;
651 : }
652 :
653 0 : uno::Reference< XDataSource> SwAddressListDialog::GetSource()
654 : {
655 0 : uno::Reference< XDataSource> xRet;
656 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
657 0 : if(pSelect)
658 : {
659 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
660 0 : xRet = pUserData->xSource;
661 : }
662 0 : return xRet;
663 :
664 : }
665 :
666 0 : SharedConnection SwAddressListDialog::GetConnection()
667 : {
668 0 : SharedConnection xRet;
669 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
670 0 : if(pSelect)
671 : {
672 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
673 0 : xRet = pUserData->xConnection;
674 : }
675 0 : return xRet;
676 : }
677 :
678 0 : uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
679 : {
680 0 : uno::Reference< XColumnsSupplier> xRet;
681 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
682 0 : if(pSelect)
683 : {
684 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
685 0 : xRet = pUserData->xColumnsSupplier;
686 : }
687 0 : return xRet;
688 : }
689 :
690 0 : ::rtl::OUString SwAddressListDialog::GetFilter()
691 : {
692 0 : ::rtl::OUString sRet;
693 0 : SvTreeListEntry* pSelect = m_aListLB.FirstSelected();
694 0 : if(pSelect)
695 : {
696 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
697 0 : sRet = pUserData->sFilter;
698 : }
699 0 : return sRet;
700 : }
701 :
702 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|