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/simptabl.hxx>
36 : #include <svtools/treelistentry.hxx>
37 : #include <com/sun/star/sdbc/XCloseable.hpp>
38 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 : #include <com/sun/star/container/XNameAccess.hpp>
40 : #include <com/sun/star/uno/XNamingService.hpp>
41 : #include <com/sun/star/sdb/DatabaseContext.hpp>
42 : #include <com/sun/star/sdb/XCompletedConnection.hpp>
43 : #include <com/sun/star/sdb/CommandType.hpp>
44 : #include <com/sun/star/sdb/FilterDialog.hpp>
45 : #include <com/sun/star/sdb/XDocumentDataSource.hpp>
46 : #include <com/sun/star/sdbc/XRowSet.hpp>
47 : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
48 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
49 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
50 : #include <com/sun/star/task/InteractionHandler.hpp>
51 : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
52 : #include <com/sun/star/frame/XStorable.hpp>
53 : #include <swunohelper.hxx>
54 : #include <vcl/waitobj.hxx>
55 : #include <unotools/pathoptions.hxx>
56 : #include <svl/urihelper.hxx>
57 : #include <dbui.hrc>
58 :
59 : #include <helpid.h>
60 : #include <unomid.h>
61 :
62 : #include <boost/scoped_ptr.hpp>
63 :
64 : using namespace ::com::sun::star;
65 : using namespace ::com::sun::star::uno;
66 : using namespace ::com::sun::star::lang;
67 : using namespace ::com::sun::star::container;
68 : using namespace ::com::sun::star::sdb;
69 : using namespace ::com::sun::star::sdbc;
70 : using namespace ::com::sun::star::sdbcx;
71 : using namespace ::com::sun::star::task;
72 : using namespace ::com::sun::star::beans;
73 : using namespace ::com::sun::star::ui::dialogs;
74 :
75 : #define ITEMID_NAME 1
76 : #define ITEMID_TABLE 2
77 :
78 0 : struct AddressUserData_Impl
79 : {
80 : uno::Reference<XDataSource> xSource;
81 : SharedConnection xConnection;
82 : uno::Reference< XColumnsSupplier> xColumnsSupplier;
83 : uno::Reference< sdbc::XResultSet> xResultSet;
84 : OUString sFilter;
85 : OUString sURL; // data is editable
86 : sal_Int32 nCommandType;
87 : sal_Int32 nTableAndQueryCount;
88 0 : AddressUserData_Impl() :
89 : nCommandType(0),
90 0 : nTableAndQueryCount(-1)
91 0 : {}
92 : };
93 :
94 0 : static OUString lcl_getFlatURL( uno::Reference<beans::XPropertySet>& xSourceProperties )
95 : {
96 0 : if(xSourceProperties.is())
97 : {
98 0 : OUString sDBURL;
99 0 : xSourceProperties->getPropertyValue("URL") >>= sDBURL;
100 0 : if (sDBURL.startsWith("sdbc:flat:"))
101 : {
102 0 : uno::Sequence<OUString> aFilters;
103 0 : xSourceProperties->getPropertyValue("TableFilter") >>= aFilters;
104 0 : uno::Sequence<PropertyValue> aInfo;
105 0 : xSourceProperties->getPropertyValue("Info") >>= aInfo;
106 0 : if(aFilters.getLength() == 1 && aInfo.getLength() )
107 : {
108 0 : OUString sFieldDelim;
109 0 : OUString sStringDelim;
110 0 : OUString sExtension;
111 0 : OUString sCharSet;
112 0 : for(sal_Int32 nInfo = 0; nInfo < aInfo.getLength(); ++nInfo)
113 : {
114 0 : if(aInfo[nInfo].Name == "FieldDelimiter")
115 0 : aInfo[nInfo].Value >>= sFieldDelim;
116 0 : else if(aInfo[nInfo].Name == "StringDelimiter")
117 0 : aInfo[nInfo].Value >>= sStringDelim;
118 0 : else if(aInfo[nInfo].Name == "Extension")
119 0 : aInfo[nInfo].Value >>= sExtension;
120 0 : else if(aInfo[nInfo].Name == "CharSet")
121 0 : aInfo[nInfo].Value >>= sCharSet;
122 : }
123 0 : if (sCharSet=="UTF-8")
124 : {
125 : //#i97577# at this point the 'URL' can also be a file name!
126 : return URIHelper::SmartRel2Abs( INetURLObject(), sDBURL.copy(10) )
127 0 : + "/" + aFilters[0] + "." + sExtension;
128 0 : }
129 0 : }
130 0 : }
131 : }
132 0 : return OUString();
133 : }
134 :
135 0 : class SwAddrSourceLB : public SvSimpleTable
136 : {
137 : public:
138 0 : SwAddrSourceLB(SvSimpleTableContainer& rParent, WinBits nBits = 0)
139 0 : : SvSimpleTable(rParent, nBits)
140 : {
141 0 : }
142 : virtual void Resize() SAL_OVERRIDE;
143 : void setColSizes();
144 : };
145 :
146 0 : void SwAddrSourceLB::Resize()
147 : {
148 0 : SvSimpleTable::Resize();
149 0 : setColSizes();
150 0 : }
151 :
152 0 : void SwAddrSourceLB::setColSizes()
153 : {
154 0 : HeaderBar &rHB = GetTheHeaderBar();
155 0 : if (rHB.GetItemCount() < 2)
156 0 : return;
157 :
158 0 : long nWidth = rHB.GetSizePixel().Width();
159 :
160 0 : long nTabs[] = { 2, 0, nWidth/2 };
161 :
162 0 : SvSimpleTable::SetTabs(&nTabs[0], MAP_PIXEL);
163 : }
164 :
165 0 : SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent)
166 : : SfxModalDialog(pParent, "SelectAddressDialog",
167 : "modules/swriter/ui/selectaddressdialog.ui")
168 :
169 : ,
170 :
171 : m_pCreatedDataSource(0),
172 : m_bInSelectHdl(false),
173 0 : m_pAddressPage(pParent)
174 : {
175 0 : get(m_pDescriptionFI, "desc");
176 0 : get(m_pLoadListPB, "add");
177 0 : get(m_pCreateListPB, "create");
178 0 : get(m_pFilterPB, "filter");
179 0 : get(m_pEditPB, "edit");
180 0 : get(m_pTablePB, "changetable");
181 0 : get(m_pOK, "ok");
182 :
183 0 : m_sName = get<FixedText>("name")->GetText();
184 0 : m_sTable = get<FixedText>("table")->GetText();
185 0 : m_sConnecting = get<FixedText>("connecting")->GetText();
186 :
187 0 : const OUString sTemp(m_pDescriptionFI->GetText()
188 0 : .replaceFirst("%1", m_pLoadListPB->GetText())
189 0 : .replaceFirst("%2", m_pCreateListPB->GetText()));
190 0 : m_pDescriptionFI->SetText(sTemp);
191 0 : m_pFilterPB->SetClickHdl( LINK( this, SwAddressListDialog, FilterHdl_Impl ));
192 0 : m_pLoadListPB->SetClickHdl( LINK( this, SwAddressListDialog, LoadHdl_Impl ));
193 0 : m_pCreateListPB->SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
194 0 : m_pEditPB->SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
195 0 : m_pTablePB->SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
196 :
197 0 : SvSimpleTableContainer *pHeaderTreeContainer = get<SvSimpleTableContainer>("sources");
198 0 : Size aSize = pHeaderTreeContainer->LogicToPixel(Size(182 , 102), MAP_APPFONT);
199 0 : pHeaderTreeContainer->set_width_request(aSize.Width());
200 0 : pHeaderTreeContainer->set_height_request(aSize.Height());
201 0 : m_pListLB = new SwAddrSourceLB(*pHeaderTreeContainer);
202 :
203 0 : m_pListLB->InsertHeaderEntry(m_sName + "\t" + m_sTable);
204 0 : m_pListLB->setColSizes();
205 :
206 0 : m_pListLB->SetStyle( m_pListLB->GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
207 0 : m_pListLB->SetSelectionMode( SINGLE_SELECTION );
208 0 : m_pOK->SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
209 :
210 0 : uno::Reference<XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
211 0 : m_xDBContext = DatabaseContext::create(xContext);
212 :
213 0 : SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
214 0 : const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
215 :
216 0 : bool bEnableEdit = false;
217 0 : bool bEnableOK = true;
218 0 : m_pListLB->SelectAll( false );
219 :
220 0 : SwDBConfig aDb;
221 0 : const OUString sBibliography = aDb.GetBibliographySource().sDataSource;
222 0 : uno::Sequence< OUString> aNames = m_xDBContext->getElementNames();
223 0 : const OUString* pNames = aNames.getConstArray();
224 0 : for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
225 : {
226 0 : if ( pNames[nName] == sBibliography )
227 0 : continue;
228 0 : SvTreeListEntry* pEntry = m_pListLB->InsertEntry(pNames[nName]);
229 0 : AddressUserData_Impl* pUserData = new AddressUserData_Impl();
230 0 : pEntry->SetUserData(pUserData);
231 0 : if(pNames[nName] == rCurrentData.sDataSource)
232 : {
233 0 : m_pListLB->Select(pEntry);
234 0 : m_pListLB->SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
235 0 : pUserData->nCommandType = rCurrentData.nCommandType;
236 0 : pUserData->xSource = rConfigItem.GetSource();
237 0 : pUserData->xConnection = rConfigItem.GetConnection();
238 0 : pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
239 0 : pUserData->xResultSet = rConfigItem.GetResultSet();
240 0 : pUserData->sFilter = rConfigItem.GetFilter();
241 : //is the data source editable (csv, Unicode, single table)
242 0 : uno::Reference<beans::XPropertySet> xSourceProperties;
243 : try
244 : {
245 0 : m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
246 0 : pUserData->sURL = lcl_getFlatURL( xSourceProperties );
247 0 : bEnableEdit = !pUserData->sURL.isEmpty() &&
248 0 : SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
249 0 : !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
250 : }
251 0 : catch (const uno::Exception&)
252 : {
253 0 : bEnableOK = false;
254 : }
255 0 : m_aDBData = rCurrentData;
256 : }
257 : }
258 :
259 0 : m_pOK->Enable(m_pListLB->GetEntryCount()>0 && bEnableOK);
260 0 : m_pEditPB->Enable(bEnableEdit);
261 0 : m_pListLB->SetSelectHdl(LINK(this, SwAddressListDialog, ListBoxSelectHdl_Impl));
262 0 : TableSelectHdl_Impl(NULL);
263 0 : }
264 :
265 0 : SwAddressListDialog::~SwAddressListDialog()
266 : {
267 0 : SvTreeListEntry* pEntry = m_pListLB->First();
268 0 : while(pEntry)
269 : {
270 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
271 0 : delete pUserData;
272 0 : pEntry = m_pListLB->Next( pEntry );
273 : }
274 0 : delete m_pListLB;
275 0 : }
276 :
277 0 : IMPL_LINK_NOARG(SwAddressListDialog, FilterHdl_Impl)
278 : {
279 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
280 0 : uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
281 0 : if(pSelect)
282 : {
283 0 : const OUString sCommand = m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1);
284 0 : if (sCommand.isEmpty())
285 0 : return 0;
286 :
287 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
288 0 : if(pUserData->xConnection.is() )
289 : {
290 : try
291 : {
292 0 : uno::Reference<lang::XMultiServiceFactory> xConnectFactory(pUserData->xConnection, UNO_QUERY_THROW);
293 : uno::Reference<XSingleSelectQueryComposer> xComposer(
294 0 : xConnectFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"), UNO_QUERY_THROW);
295 :
296 : uno::Reference<XRowSet> xRowSet(
297 0 : xMgr->createInstance("com.sun.star.sdb.RowSet"), UNO_QUERY);
298 0 : uno::Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
299 0 : xRowProperties->setPropertyValue("DataSourceName",
300 0 : makeAny(m_pListLB->GetEntryText(pSelect, ITEMID_NAME - 1)));
301 0 : xRowProperties->setPropertyValue("Command", makeAny(sCommand));
302 0 : xRowProperties->setPropertyValue("CommandType", makeAny(pUserData->nCommandType));
303 0 : xRowProperties->setPropertyValue("ActiveConnection", makeAny(pUserData->xConnection.getTyped()));
304 0 : xRowSet->execute();
305 :
306 0 : OUString sQuery;
307 0 : xRowProperties->getPropertyValue("ActiveCommand")>>= sQuery;
308 0 : xComposer->setQuery(sQuery);
309 0 : if(!pUserData->sFilter.isEmpty())
310 0 : xComposer->setFilter(pUserData->sFilter);
311 :
312 : uno::Reference< XExecutableDialog> xDialog = sdb::FilterDialog::createWithQuery( comphelper::getComponentContext(xMgr),
313 0 : xComposer,xRowSet, uno::Reference<awt::XWindow>() );
314 :
315 0 : if ( RET_OK == xDialog->execute() )
316 : {
317 0 : WaitObject aWO( NULL );
318 0 : pUserData->sFilter = xComposer->getFilter();
319 : }
320 0 : ::comphelper::disposeComponent(xRowSet);
321 : }
322 0 : catch (const Exception&)
323 : {
324 : OSL_FAIL("exception caught in SwAddressListDialog::FilterHdl_Impl");
325 : }
326 0 : }
327 : }
328 0 : return 0;
329 : }
330 :
331 0 : IMPL_LINK_NOARG(SwAddressListDialog, LoadHdl_Impl)
332 : {
333 0 : const OUString sNewSource = SwDBManager::LoadAndRegisterDataSource();
334 0 : if(!sNewSource.isEmpty())
335 : {
336 0 : SvTreeListEntry* pNewSource = m_pListLB->InsertEntry(sNewSource);
337 0 : pNewSource->SetUserData(new AddressUserData_Impl());
338 0 : m_pListLB->Select(pNewSource);
339 : }
340 0 : return 0;
341 : }
342 :
343 0 : IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
344 : {
345 0 : OUString sInputURL;
346 : boost::scoped_ptr<SwCreateAddressListDialog> pDlg(
347 : new SwCreateAddressListDialog(
348 : pButton,
349 : sInputURL,
350 0 : m_pAddressPage->GetWizard()->GetConfigItem()));
351 0 : if(RET_OK == pDlg->Execute())
352 : {
353 : //register the URL a new datasource
354 0 : const OUString sURL = pDlg->GetURL();
355 : try
356 : {
357 0 : uno::Reference<XSingleServiceFactory> xFact( m_xDBContext, UNO_QUERY);
358 0 : uno::Reference<XInterface> xNewInstance = xFact->createInstance();
359 0 : INetURLObject aURL( sURL );
360 0 : const OUString sNewName = aURL.getBase();
361 : //find a unique name if sNewName already exists
362 0 : OUString sFind(sNewName);
363 0 : sal_Int32 nIndex = 0;
364 0 : while(m_xDBContext->hasByName(sFind))
365 : {
366 0 : sFind = sNewName + OUString::number(++nIndex);
367 : }
368 0 : uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
369 :
370 : //only the 'path' has to be added
371 0 : INetURLObject aTempURL(aURL);
372 0 : aTempURL.removeSegment();
373 0 : aTempURL.removeFinalSlash();
374 0 : const OUString sDBURL("sdbc:flat:" + aTempURL.GetMainURL(INetURLObject::NO_DECODE));
375 0 : Any aAny(&sDBURL, ::getCppuType(&sDBURL));
376 0 : xDataProperties->setPropertyValue("URL", aAny);
377 : //set the filter to the file name without extension
378 0 : uno::Sequence<OUString> aFilters(1);
379 0 : aFilters[0] = sNewName;
380 0 : aAny <<= aFilters;
381 0 : xDataProperties->setPropertyValue("TableFilter", aAny);
382 :
383 0 : uno::Sequence<PropertyValue> aInfo(4);
384 0 : PropertyValue* pInfo = aInfo.getArray();
385 0 : pInfo[0].Name = "FieldDelimiter";
386 0 : pInfo[0].Value <<= OUString('\t');
387 0 : pInfo[1].Name = "StringDelimiter";
388 0 : pInfo[1].Value <<= OUString('"');
389 0 : pInfo[2].Name = "Extension";
390 0 : pInfo[2].Value <<= aURL.getExtension();//"csv";
391 0 : pInfo[3].Name = "CharSet";
392 0 : pInfo[3].Value <<= OUString("UTF-8");
393 0 : aAny <<= aInfo;
394 0 : xDataProperties->setPropertyValue("Info", aAny);
395 :
396 0 : uno::Reference<sdb::XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
397 0 : uno::Reference<frame::XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
398 0 : OUString const sExt(".odb");
399 0 : OUString sTmpName;
400 : {
401 0 : OUString sHomePath(SvtPathOptions().GetWorkPath());
402 0 : utl::TempFile aTempFile(sFind, true, &sExt, &sHomePath);
403 0 : aTempFile.EnableKillingFile(true);
404 0 : sTmpName = aTempFile.GetURL();
405 : }
406 0 : xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
407 :
408 0 : uno::Reference<XNamingService> xNaming(m_xDBContext, UNO_QUERY);
409 0 : xNaming->registerObject( sFind, xNewInstance );
410 : //now insert the new source into the ListBox
411 0 : m_pCreatedDataSource = m_pListLB->InsertEntry(sFind + "\t" + aFilters[0]);
412 0 : AddressUserData_Impl* pUserData = new AddressUserData_Impl();
413 0 : pUserData->sURL = sURL;
414 0 : m_pCreatedDataSource->SetUserData(pUserData);
415 0 : m_pListLB->Select(m_pCreatedDataSource);
416 0 : m_pCreateListPB->Enable(false);
417 :
418 : }
419 0 : catch (const Exception&)
420 : {
421 0 : }
422 : }
423 0 : return 0;
424 : }
425 :
426 0 : IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
427 : {
428 0 : SvTreeListEntry* pEntry = m_pListLB->FirstSelected();
429 0 : AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
430 0 : if(pUserData && !pUserData->sURL.isEmpty())
431 : {
432 0 : if(pUserData->xResultSet.is())
433 : {
434 0 : SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
435 0 : if(rConfigItem.GetResultSet() != pUserData->xResultSet)
436 0 : ::comphelper::disposeComponent( pUserData->xResultSet );
437 0 : pUserData->xResultSet = 0;
438 :
439 0 : rConfigItem.DisposeResultSet();
440 : }
441 0 : pUserData->xSource.clear();
442 0 : pUserData->xColumnsSupplier.clear();
443 0 : pUserData->xConnection.clear();
444 : // will automatically close if it was the las reference
445 : boost::scoped_ptr<SwCreateAddressListDialog> pDlg(
446 : new SwCreateAddressListDialog(
447 : pButton,
448 : pUserData->sURL,
449 0 : m_pAddressPage->GetWizard()->GetConfigItem()));
450 0 : if(RET_OK == pDlg->Execute())
451 : {
452 0 : }
453 : }
454 0 : return 0;
455 : };
456 :
457 0 : IMPL_LINK_NOARG(SwAddressListDialog, ListBoxSelectHdl_Impl)
458 : {
459 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
460 : Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
461 0 : StaticListBoxSelectHdl_Impl ), pSelect );
462 0 : return 0;
463 : }
464 :
465 0 : IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvTreeListEntry*, pSelect)
466 : {
467 : //prevent nested calls of the select handler
468 0 : if(pThis->m_bInSelectHdl)
469 0 : return 0;
470 0 : pThis->EnterWait();
471 0 : pThis->m_bInSelectHdl = true;
472 0 : AddressUserData_Impl* pUserData = 0;
473 0 : if(pSelect)
474 : {
475 0 : const OUString sTable(pThis->m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1));
476 0 : if(sTable.isEmpty())
477 : {
478 0 : pThis->m_pListLB->SetEntryText(pThis->m_sConnecting, pSelect, ITEMID_TABLE - 1);
479 : // allow painting of the new entry
480 0 : pThis->m_pListLB->Window::Invalidate(INVALIDATE_UPDATE);
481 0 : for (int i = 0; i < 10; ++i)
482 0 : Application::Reschedule();
483 : }
484 :
485 0 : pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
486 0 : if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
487 : {
488 : /*
489 : * We're a callback from a selection from a list box, which takes
490 : * place on mouse down before mouse up. The next dialog also has a
491 : * list box. Spawning it means this list box doesn't get the mouse
492 : * down event. So it sticks on "making selection" mode. So if you
493 : * cancel the next dialog and just move the mouse out of this entry
494 : * and back then the dialog pops up again, without requiring a click
495 : *
496 : * Most expedient thing to do is to manually end the parent selection
497 : * here.
498 : */
499 0 : pThis->m_pListLB->EndSelection();
500 0 : pThis->DetectTablesAndQueries(pSelect, sTable.isEmpty());
501 : }
502 : else
503 : {
504 : //otherwise set the selected db-data
505 0 : pThis->m_aDBData.sDataSource = pThis->m_pListLB->GetEntryText(pSelect, ITEMID_NAME - 1);
506 0 : pThis->m_aDBData.sCommand = pThis->m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1);
507 0 : pThis->m_aDBData.nCommandType = pUserData->nCommandType;
508 0 : pThis->m_pOK->Enable(true);
509 : }
510 0 : if(pThis->m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1) == pThis->m_sConnecting)
511 0 : pThis->m_pListLB->SetEntryText(OUString(), pSelect, ITEMID_TABLE - 1);
512 : }
513 0 : pThis->m_pEditPB->Enable(pUserData && !pUserData->sURL.isEmpty() &&
514 0 : SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
515 0 : !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
516 0 : pThis->m_bInSelectHdl = false;
517 0 : pThis->LeaveWait();
518 0 : return 0;
519 : }
520 :
521 : // detect the number of tables for a data source
522 : // if only one is available then set it at the entry
523 0 : void SwAddressListDialog::DetectTablesAndQueries(
524 : SvTreeListEntry* pSelect,
525 : bool bWidthDialog)
526 : {
527 : try
528 : {
529 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
530 0 : uno::Reference<XCompletedConnection> xComplConnection;
531 0 : if(!pUserData->xConnection.is())
532 : {
533 0 : m_aDBData.sDataSource = m_pListLB->GetEntryText(pSelect, ITEMID_NAME - 1);
534 0 : m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
535 0 : pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
536 :
537 0 : uno::Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
538 0 : uno::Reference< XInteractionHandler > xHandler( InteractionHandler::createWithParent(xContext, 0), UNO_QUERY );
539 0 : pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
540 : }
541 0 : if(pUserData->xConnection.is())
542 : {
543 0 : sal_Int32 nTables = 0;
544 0 : uno::Sequence<OUString> aTables;
545 0 : uno::Sequence<OUString> aQueries;
546 0 : uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
547 0 : if(xTSupplier.is())
548 : {
549 0 : uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
550 0 : aTables = xTbls->getElementNames();
551 0 : nTables += aTables.getLength();
552 : }
553 0 : uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
554 0 : if(xQSupplier.is())
555 : {
556 0 : uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
557 0 : aQueries = xQueries->getElementNames();
558 0 : nTables += aQueries.getLength();
559 : }
560 0 : pUserData->nTableAndQueryCount = nTables;
561 0 : if(nTables > 1 && bWidthDialog)
562 : {
563 : //now call the table select dialog - if more than one table exists
564 0 : boost::scoped_ptr<SwSelectDBTableDialog> pDlg(new SwSelectDBTableDialog(this, pUserData->xConnection));
565 0 : const OUString sTable = m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1);
566 0 : if(!sTable.isEmpty())
567 0 : pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
568 0 : if(RET_OK == pDlg->Execute())
569 : {
570 : bool bIsTable;
571 0 : m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
572 0 : m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
573 0 : pUserData->nCommandType = m_aDBData.nCommandType;
574 0 : }
575 : }
576 0 : else if(nTables == 1)
577 : {
578 0 : if(aTables.getLength())
579 : {
580 0 : m_aDBData.sCommand = aTables[0];
581 0 : m_aDBData.nCommandType = CommandType::TABLE;
582 : }
583 : else
584 : {
585 0 : m_aDBData.sCommand = aQueries[0];
586 0 : m_aDBData.nCommandType = CommandType::QUERY;
587 : }
588 0 : }
589 : }
590 0 : if ( !m_aDBData.sCommand.isEmpty() )
591 : {
592 0 : uno::Reference<beans::XPropertySet> xSourceProperties;
593 0 : m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
594 0 : pUserData->sURL = lcl_getFlatURL( xSourceProperties );
595 :
596 0 : pUserData->xColumnsSupplier = SwDBManager::GetColumnSupplier(pUserData->xConnection,
597 : m_aDBData.sCommand,
598 0 : m_aDBData.nCommandType == CommandType::TABLE ?
599 0 : SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY );
600 : //#i97577#
601 0 : if( pUserData->xColumnsSupplier.is() )
602 0 : m_pListLB->SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
603 : else
604 0 : m_pListLB->SetEntryText(OUString(), pSelect, ITEMID_TABLE - 1);
605 : }
606 0 : const OUString sCommand = m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1);
607 0 : m_pOK->Enable(pSelect && !sCommand.isEmpty());
608 0 : m_pFilterPB->Enable( pUserData->xConnection.is() && !sCommand.isEmpty() );
609 0 : m_pTablePB->Enable( pUserData->nTableAndQueryCount > 1 );
610 : }
611 0 : catch (const Exception&)
612 : {
613 : OSL_FAIL("exception caught in SwAddressListDialog::DetectTablesAndQueries");
614 0 : m_pOK->Enable( false );
615 : }
616 0 : }
617 :
618 0 : IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
619 : {
620 0 : EnterWait();
621 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
622 0 : if(pSelect)
623 : {
624 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
625 : //only call the table select dialog if tables have not been searched for or there
626 : //are more than 1
627 0 : const OUString sTable = m_pListLB->GetEntryText(pSelect, ITEMID_TABLE - 1);
628 0 : if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
629 : {
630 0 : DetectTablesAndQueries(pSelect, (pButton != 0) || sTable.isEmpty());
631 0 : }
632 : }
633 :
634 0 : LeaveWait();
635 0 : return 0;
636 : }
637 :
638 0 : IMPL_LINK_NOARG(SwAddressListDialog, OKHdl_Impl)
639 : {
640 0 : EndDialog(sal_True);
641 0 : return 0;
642 : }
643 :
644 0 : uno::Reference< XDataSource> SwAddressListDialog::GetSource()
645 : {
646 0 : uno::Reference< XDataSource> xRet;
647 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
648 0 : if(pSelect)
649 : {
650 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
651 0 : xRet = pUserData->xSource;
652 : }
653 0 : return xRet;
654 :
655 : }
656 :
657 0 : SharedConnection SwAddressListDialog::GetConnection()
658 : {
659 0 : SharedConnection xRet;
660 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
661 0 : if(pSelect)
662 : {
663 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
664 0 : xRet = pUserData->xConnection;
665 : }
666 0 : return xRet;
667 : }
668 :
669 0 : uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
670 : {
671 0 : uno::Reference< XColumnsSupplier> xRet;
672 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
673 0 : if(pSelect)
674 : {
675 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
676 0 : xRet = pUserData->xColumnsSupplier;
677 : }
678 0 : return xRet;
679 : }
680 :
681 0 : OUString SwAddressListDialog::GetFilter()
682 : {
683 0 : SvTreeListEntry* pSelect = m_pListLB->FirstSelected();
684 0 : if(pSelect)
685 : {
686 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
687 0 : return pUserData->sFilter;
688 : }
689 0 : return OUString();
690 0 : }
691 :
692 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|