Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <swtypes.hxx>
30 : : #include <addresslistdialog.hxx>
31 : : #include <selectdbtabledialog.hxx>
32 : : #include <createaddresslistdialog.hxx>
33 : : #include <mailmergewizard.hxx>
34 : : #include <mmconfigitem.hxx>
35 : : #include <mmaddressblockpage.hxx>
36 : : #include <dbmgr.hxx>
37 : : #include <dbconfig.hxx>
38 : : #include <unotools/tempfile.hxx>
39 : : #include <vcl/msgbox.hxx>
40 : : #include <vcl/svapp.hxx>
41 : : #include <tools/urlobj.hxx>
42 : : #include <comphelper/processfactory.hxx>
43 : : #include <comphelper/types.hxx>
44 : : #include <com/sun/star/sdbc/XCloseable.hpp>
45 : : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46 : : #include <com/sun/star/container/XNameAccess.hpp>
47 : : #include <com/sun/star/uno/XNamingService.hpp>
48 : : #include <com/sun/star/sdb/XCompletedConnection.hpp>
49 : : #include <com/sun/star/sdb/CommandType.hpp>
50 : : #include <com/sun/star/sdb/XDocumentDataSource.hpp>
51 : : #include <com/sun/star/sdbc/XRowSet.hpp>
52 : : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
53 : : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
54 : : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
55 : : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
56 : : #include <com/sun/star/frame/XStorable.hpp>
57 : : #include <swunohelper.hxx>
58 : : #include <vcl/waitobj.hxx>
59 : : #include <unotools/pathoptions.hxx>
60 : : #include <svl/urihelper.hxx>
61 : : #include <addresslistdialog.hrc>
62 : : #include <dbui.hrc>
63 : :
64 : : #include <helpid.h>
65 : : #include <unomid.h>
66 : :
67 : :
68 : : using namespace ::com::sun::star;
69 : : using namespace ::com::sun::star::uno;
70 : : using namespace ::com::sun::star::lang;
71 : : using namespace ::com::sun::star::container;
72 : : using namespace ::com::sun::star::sdb;
73 : : using namespace ::com::sun::star::sdbc;
74 : : using namespace ::com::sun::star::sdbcx;
75 : : using namespace ::com::sun::star::task;
76 : : using namespace ::com::sun::star::beans;
77 : : using namespace ::com::sun::star::ui::dialogs;
78 : : using namespace ::rtl;
79 : :
80 : : #define ITEMID_NAME 1
81 : : #define ITEMID_TABLE 2
82 : :
83 : : static const char* cUTF8 = "UTF-8";
84 : :
85 : 0 : struct AddressUserData_Impl
86 : : {
87 : : uno::Reference<XDataSource> xSource;
88 : : SharedConnection xConnection;
89 : : uno::Reference< XColumnsSupplier> xColumnsSupplier;
90 : : uno::Reference< sdbc::XResultSet> xResultSet;
91 : : ::rtl::OUString sFilter;
92 : : ::rtl::OUString sURL; // data is editable
93 : : sal_Int32 nCommandType;
94 : : sal_Int32 nTableAndQueryCount;
95 : 0 : AddressUserData_Impl() :
96 : : nCommandType(0),
97 : 0 : nTableAndQueryCount(-1)
98 : 0 : {}
99 : : };
100 : :
101 : 0 : ::rtl::OUString lcl_getFlatURL( uno::Reference<beans::XPropertySet>& xSourceProperties )
102 : : {
103 : 0 : ::rtl::OUString sURL;
104 : 0 : if(xSourceProperties.is())
105 : : {
106 : 0 : rtl::OUString sDBURL;
107 : 0 : xSourceProperties->getPropertyValue(C2U("URL")) >>= sDBURL;
108 : 0 : if(String(sDBURL).SearchAscii("sdbc:flat:") == 0)
109 : : {
110 : 0 : uno::Sequence<OUString> aFilters;
111 : 0 : xSourceProperties->getPropertyValue(C2U("TableFilter")) >>= aFilters;
112 : 0 : uno::Sequence<PropertyValue> aInfo;
113 : 0 : xSourceProperties->getPropertyValue(C2U("Info")) >>= aInfo;
114 : 0 : if(aFilters.getLength() == 1 && aInfo.getLength() )
115 : : {
116 : 0 : ::rtl::OUString sFieldDelim;
117 : 0 : ::rtl::OUString sStringDelim;
118 : 0 : ::rtl::OUString sExtension;
119 : 0 : ::rtl::OUString sCharSet;
120 : 0 : for(sal_Int32 nInfo = 0; nInfo < aInfo.getLength(); ++nInfo)
121 : : {
122 : 0 : if(aInfo[nInfo].Name == C2U("FieldDelimiter"))
123 : 0 : aInfo[nInfo].Value >>= sFieldDelim;
124 : 0 : else if(aInfo[nInfo].Name == C2U("StringDelimiter"))
125 : 0 : aInfo[nInfo].Value >>= sStringDelim;
126 : 0 : else if(aInfo[nInfo].Name == C2U("Extension"))
127 : 0 : aInfo[nInfo].Value >>= sExtension;
128 : 0 : else if(aInfo[nInfo].Name == C2U("CharSet"))
129 : 0 : aInfo[nInfo].Value >>= sCharSet;
130 : : }
131 : 0 : if(!sCharSet.compareToAscii( cUTF8 ))
132 : : {
133 : 0 : sURL = String(sDBURL).Copy( 10 );
134 : : //#i97577# at this point the 'URL' can also be a file name!
135 : 0 : sURL = URIHelper::SmartRel2Abs( INetURLObject(), sURL );
136 : 0 : sURL += C2U("/");
137 : 0 : sURL += aFilters[0];
138 : 0 : sURL += C2U(".");
139 : 0 : sURL += sExtension;
140 : 0 : }
141 : 0 : }
142 : 0 : }
143 : : }
144 : 0 : return sURL;
145 : : }
146 : :
147 : 0 : SwAddressListDialog::SwAddressListDialog(SwMailMergeAddressBlockPage* pParent) :
148 : : SfxModalDialog(pParent, SW_RES(DLG_MM_ADDRESSLISTDIALOG)),
149 : : #ifdef MSC
150 : : #pragma warning (disable : 4355)
151 : : #endif
152 : : m_aDescriptionFI( this, SW_RES( FI_DESCRIPTION )),
153 : : m_aListFT( this, SW_RES( FT_LIST )),
154 : : m_aListHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER),
155 : : m_aListLB( this, SW_RES( LB_LIST )),
156 : : m_aLoadListPB( this, SW_RES( PB_LOADLIST )),
157 : : m_aCreateListPB(this, SW_RES( PB_CREATELIST )),
158 : : m_aFilterPB( this, SW_RES( PB_FILTER )),
159 : : m_aEditPB(this, SW_RES( PB_EDIT )),
160 : : m_aTablePB(this, SW_RES( PB_TABLE )),
161 : : m_aSeparatorFL(this, SW_RES( FL_SEPARATOR )),
162 : : m_aOK( this, SW_RES( PB_OK )),
163 : : m_aCancel( this, SW_RES( PB_CANCEL )),
164 : : m_aHelp( this, SW_RES( PB_HELP )),
165 : : #ifdef MSC
166 : : #pragma warning (default : 4355)
167 : : #endif
168 : : m_sName( SW_RES( ST_NAME )),
169 : : m_sTable( SW_RES( ST_TABLE )),
170 : : m_sConnecting( SW_RES( ST_CONNECTING )),
171 : : m_pCreatedDataSource(0),
172 : : m_bInSelectHdl(false),
173 : 0 : m_pAddressPage(pParent)
174 : : {
175 : 0 : FreeResource();
176 : 0 : String sTemp(m_aDescriptionFI.GetText());
177 : 0 : sTemp.SearchAndReplaceAscii("%1", m_aLoadListPB.GetText());
178 : 0 : sTemp.SearchAndReplaceAscii("%2", m_aCreateListPB.GetText());
179 : 0 : m_aDescriptionFI.SetText(sTemp);
180 : 0 : m_aFilterPB.SetClickHdl( LINK( this, SwAddressListDialog, FilterHdl_Impl ));
181 : 0 : m_aLoadListPB.SetClickHdl( LINK( this, SwAddressListDialog, LoadHdl_Impl ));
182 : 0 : m_aCreateListPB.SetClickHdl( LINK( this, SwAddressListDialog,CreateHdl_Impl ));
183 : 0 : m_aEditPB.SetClickHdl(LINK( this, SwAddressListDialog, EditHdl_Impl));
184 : 0 : m_aTablePB.SetClickHdl(LINK( this, SwAddressListDialog, TableSelectHdl_Impl));
185 : :
186 : 0 : Size aLBSize(m_aListLB.GetSizePixel());
187 : 0 : m_aListHB.SetSizePixel(aLBSize);
188 : 0 : Size aHeadSize(m_aListHB.CalcWindowSizePixel());
189 : 0 : aHeadSize.Width() = aLBSize.Width();
190 : 0 : m_aListHB.SetSizePixel(aHeadSize);
191 : 0 : Point aLBPos(m_aListLB.GetPosPixel());
192 : 0 : m_aListHB.SetPosPixel(aLBPos);
193 : 0 : aLBPos.Y() += aHeadSize.Height();
194 : 0 : aLBSize.Height() -= aHeadSize.Height();
195 : 0 : m_aListLB.SetPosSizePixel(aLBPos, aLBSize);
196 : :
197 : 0 : Size aSz(m_aListHB.GetOutputSizePixel());
198 : : m_aListHB.InsertItem( ITEMID_NAME, m_sName,
199 : 0 : aSz.Width()/2,
200 : 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS/*| HIB_CLICKABLE | HIB_UPARROW */);
201 : : m_aListHB.InsertItem( ITEMID_TABLE, m_sTable,
202 : 0 : aSz.Width()/2,
203 : 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXED | HIB_FIXEDPOS /*| HIB_CLICKABLE | HIB_UPARROW */);
204 : 0 : m_aListHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
205 : 0 : m_aListHB.Show();
206 : :
207 : 0 : m_aListLB.SetHelpId(HID_MM_ADDRESSLIST_TLB);
208 : 0 : static long nTabs[] = {2, 0, aSz.Width()/2 };
209 : 0 : m_aListLB.SetStyle( m_aListLB.GetStyle() | WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP );
210 : 0 : m_aListLB.SetSelectionMode( SINGLE_SELECTION );
211 : 0 : m_aListLB.SetTabs(&nTabs[0], MAP_PIXEL);
212 : 0 : m_aOK.SetClickHdl( LINK( this, SwAddressListDialog, OKHdl_Impl));
213 : :
214 : 0 : uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
215 : 0 : if( xMgr.is() )
216 : : {
217 : 0 : uno::Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
218 : 0 : m_xDBContext = uno::Reference<XNameAccess>(xInstance, UNO_QUERY) ;
219 : : }
220 : 0 : SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
221 : 0 : const SwDBData& rCurrentData = rConfigItem.GetCurrentDBData();
222 : :
223 : : OSL_ENSURE(m_xDBContext.is(), "service 'com.sun.star.sdb.DatabaseContext' not found!");
224 : 0 : sal_Bool bEnableEdit = sal_False;
225 : 0 : sal_Bool bEnableOK = sal_True;
226 : 0 : m_aListLB.SelectAll( sal_False );
227 : :
228 : 0 : if(m_xDBContext.is())
229 : : {
230 : 0 : SwDBConfig aDb;
231 : 0 : ::rtl::OUString sBibliography = aDb.GetBibliographySource().sDataSource;
232 : 0 : uno::Sequence< ::rtl::OUString> aNames = m_xDBContext->getElementNames();
233 : 0 : const ::rtl::OUString* pNames = aNames.getConstArray();
234 : 0 : for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
235 : : {
236 : 0 : if ( pNames[nName] == sBibliography )
237 : 0 : continue;
238 : 0 : SvLBoxEntry* pEntry = m_aListLB.InsertEntry(pNames[nName]);
239 : 0 : AddressUserData_Impl* pUserData = new AddressUserData_Impl();
240 : 0 : pEntry->SetUserData(pUserData);
241 : 0 : if(pNames[nName] == rCurrentData.sDataSource)
242 : : {
243 : 0 : m_aListLB.Select(pEntry);
244 : 0 : m_aListLB.SetEntryText(rCurrentData.sCommand, pEntry, ITEMID_TABLE - 1);
245 : 0 : pUserData->nCommandType = rCurrentData.nCommandType;
246 : 0 : pUserData->xSource = rConfigItem.GetSource();
247 : 0 : pUserData->xConnection = rConfigItem.GetConnection();
248 : 0 : pUserData->xColumnsSupplier = rConfigItem.GetColumnsSupplier();
249 : 0 : pUserData->xResultSet = rConfigItem.GetResultSet();
250 : 0 : pUserData->sFilter = rConfigItem.GetFilter();
251 : : //is the data source editable (csv, Unicode, single table)
252 : 0 : uno::Reference<beans::XPropertySet> xSourceProperties;
253 : : try
254 : : {
255 : 0 : m_xDBContext->getByName(pNames[nName]) >>= xSourceProperties;
256 : 0 : pUserData->sURL = lcl_getFlatURL( xSourceProperties );
257 : 0 : bEnableEdit = !pUserData->sURL.isEmpty() &&
258 : 0 : SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
259 : 0 : !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL );
260 : : }
261 : 0 : catch (const uno::Exception&)
262 : : {
263 : 0 : bEnableOK = sal_False;
264 : : }
265 : 0 : m_aDBData = rCurrentData;
266 : : }
267 : 0 : }
268 : : }
269 : 0 : m_aOK.Enable(m_aListLB.GetEntryCount()>0 && bEnableOK);
270 : 0 : m_aEditPB.Enable(bEnableEdit);
271 : 0 : m_aListLB.SetSelectHdl(LINK(this, SwAddressListDialog, ListBoxSelectHdl_Impl));
272 : 0 : TableSelectHdl_Impl(NULL);
273 : 0 : }
274 : :
275 : 0 : SwAddressListDialog::~SwAddressListDialog()
276 : : {
277 : 0 : SvLBoxEntry* pEntry = m_aListLB.First();
278 : 0 : while(pEntry)
279 : : {
280 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pEntry->GetUserData());
281 : 0 : delete pUserData;
282 : 0 : pEntry = m_aListLB.Next( pEntry );
283 : : }
284 : 0 : }
285 : :
286 : 0 : IMPL_LINK_NOARG(SwAddressListDialog, FilterHdl_Impl)
287 : : {
288 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
289 : 0 : uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
290 : 0 : if(pSelect && xMgr.is())
291 : : {
292 : 0 : String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
293 : 0 : if ( !sCommand.Len() )
294 : 0 : return 0;
295 : :
296 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
297 : 0 : if(pUserData->xConnection.is() )
298 : : {
299 : : try
300 : : {
301 : 0 : uno::Reference<lang::XMultiServiceFactory> xConnectFactory(pUserData->xConnection, UNO_QUERY_THROW);
302 : : uno::Reference<XSingleSelectQueryComposer> xComposer(
303 : 0 : xConnectFactory->createInstance(C2U("com.sun.star.sdb.SingleSelectQueryComposer")), UNO_QUERY_THROW);
304 : :
305 : 0 : PropertyValue aSecond;
306 : 0 : aSecond.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RowSet" ) );
307 : : uno::Reference<XRowSet> xRowSet(
308 : 0 : xMgr->createInstance(C2U("com.sun.star.sdb.RowSet")), UNO_QUERY);
309 : 0 : uno::Reference<XPropertySet> xRowProperties(xRowSet, UNO_QUERY);
310 : 0 : xRowProperties->setPropertyValue(C2U("DataSourceName"),
311 : 0 : makeAny(OUString(m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1))));
312 : 0 : xRowProperties->setPropertyValue(C2U("Command"), makeAny(
313 : 0 : OUString(sCommand)));
314 : 0 : xRowProperties->setPropertyValue(C2U("CommandType"), makeAny(pUserData->nCommandType));
315 : 0 : xRowProperties->setPropertyValue(C2U("ActiveConnection"), makeAny(pUserData->xConnection.getTyped()));
316 : 0 : xRowSet->execute();
317 : 0 : aSecond.Value <<= xRowSet;
318 : :
319 : 0 : PropertyValue aFirst;
320 : 0 : aFirst.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "QueryComposer" ) );
321 : 0 : ::rtl::OUString sQuery;
322 : 0 : xRowProperties->getPropertyValue(C2U("ActiveCommand"))>>= sQuery;
323 : 0 : xComposer->setQuery(sQuery);
324 : 0 : if(!pUserData->sFilter.isEmpty())
325 : 0 : xComposer->setFilter(pUserData->sFilter);
326 : 0 : aFirst.Value <<= xComposer;
327 : :
328 : 0 : uno::Sequence<Any> aInit(2);
329 : 0 : aInit[0] <<= aFirst;
330 : 0 : aInit[1] <<= aSecond;
331 : :
332 : 0 : ::rtl::OUString sDialogServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.FilterDialog" ) );
333 : : uno::Reference< XExecutableDialog> xDialog(
334 : 0 : xMgr->createInstanceWithArguments( sDialogServiceName, aInit ), UNO_QUERY);
335 : :
336 : 0 : if ( RET_OK == xDialog->execute() )
337 : : {
338 : 0 : WaitObject aWO( NULL );
339 : 0 : pUserData->sFilter = xComposer->getFilter();
340 : : }
341 : 0 : ::comphelper::disposeComponent(xRowSet);
342 : : }
343 : 0 : catch (const Exception&)
344 : : {
345 : : OSL_FAIL("exception caught in SwAddressListDialog::FilterHdl_Impl");
346 : : }
347 : 0 : }
348 : : }
349 : 0 : return 0;
350 : : }
351 : :
352 : 0 : IMPL_LINK_NOARG(SwAddressListDialog, LoadHdl_Impl)
353 : : {
354 : 0 : String sNewSource = SwNewDBMgr::LoadAndRegisterDataSource();
355 : 0 : if(sNewSource.Len())
356 : : {
357 : 0 : SvLBoxEntry* pNewSource = m_aListLB.InsertEntry(sNewSource);
358 : 0 : pNewSource->SetUserData(new AddressUserData_Impl());
359 : 0 : m_aListLB.Select(pNewSource);
360 : : }
361 : 0 : return 0;
362 : : }
363 : :
364 : 0 : IMPL_LINK(SwAddressListDialog, CreateHdl_Impl, PushButton*, pButton)
365 : : {
366 : 0 : String sInputURL;
367 : : SwCreateAddressListDialog* pDlg =
368 : : new SwCreateAddressListDialog(
369 : : pButton,
370 : : sInputURL,
371 : 0 : m_pAddressPage->GetWizard()->GetConfigItem());
372 : 0 : if(RET_OK == pDlg->Execute())
373 : : {
374 : : //register the URL a new datasource
375 : 0 : OUString sURL = pDlg->GetURL();
376 : : try
377 : : {
378 : 0 : uno::Reference<XSingleServiceFactory> xFact( m_xDBContext, UNO_QUERY);
379 : 0 : uno::Reference<XInterface> xNewInstance = xFact->createInstance();
380 : 0 : INetURLObject aURL( sURL );
381 : 0 : OUString sNewName = aURL.getBase();
382 : : //find a unique name if sNewName already exists
383 : 0 : OUString sFind(sNewName);
384 : 0 : sal_Int32 nIndex = 0;
385 : 0 : while(m_xDBContext->hasByName(sFind))
386 : : {
387 : 0 : sFind = sNewName;
388 : 0 : sFind += OUString::valueOf(++nIndex);
389 : : }
390 : 0 : uno::Reference<XPropertySet> xDataProperties(xNewInstance, UNO_QUERY);
391 : :
392 : 0 : OUString sDBURL(C2U("sdbc:flat:"));
393 : : //only the 'path' has to be added
394 : 0 : INetURLObject aTempURL(aURL);
395 : 0 : aTempURL.removeSegment();
396 : 0 : aTempURL.removeFinalSlash();
397 : 0 : sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE);
398 : 0 : Any aAny(&sDBURL, ::getCppuType(&sDBURL));
399 : 0 : xDataProperties->setPropertyValue(C2U("URL"), aAny);
400 : : //set the filter to the file name without extension
401 : 0 : uno::Sequence<OUString> aFilters(1);
402 : 0 : aFilters[0] = sNewName;
403 : 0 : aAny <<= aFilters;
404 : 0 : xDataProperties->setPropertyValue(C2U("TableFilter"), aAny);
405 : :
406 : 0 : uno::Sequence<PropertyValue> aInfo(4);
407 : 0 : PropertyValue* pInfo = aInfo.getArray();
408 : 0 : pInfo[0].Name = C2U("FieldDelimiter");
409 : 0 : pInfo[0].Value <<= OUString('\t');
410 : 0 : pInfo[1].Name = C2U("StringDelimiter");
411 : 0 : pInfo[1].Value <<= OUString('"');
412 : 0 : pInfo[2].Name = C2U("Extension");
413 : 0 : pInfo[2].Value <<= ::rtl::OUString(aURL.getExtension());//C2U("csv");
414 : 0 : pInfo[3].Name = C2U("CharSet");
415 : 0 : pInfo[3].Value <<= rtl::OUString::createFromAscii(cUTF8);
416 : 0 : aAny <<= aInfo;
417 : 0 : xDataProperties->setPropertyValue(C2U("Info"), aAny);
418 : :
419 : 0 : uno::Reference<sdb::XDocumentDataSource> xDS(xNewInstance, UNO_QUERY_THROW);
420 : 0 : uno::Reference<frame::XStorable> xStore(xDS->getDatabaseDocument(), UNO_QUERY_THROW);
421 : 0 : String sExt = rtl::OUString(".odb");
422 : 0 : String sTmpName;
423 : : {
424 : 0 : String sHomePath(SvtPathOptions().GetWorkPath());
425 : 0 : utl::TempFile aTempFile(sFind , &sExt, &sHomePath);
426 : 0 : aTempFile.EnableKillingFile(sal_True);
427 : 0 : sTmpName = aTempFile.GetURL();
428 : : }
429 : 0 : xStore->storeAsURL(sTmpName, Sequence< PropertyValue >());
430 : :
431 : :
432 : 0 : uno::Reference<XNamingService> xNaming(m_xDBContext, UNO_QUERY);
433 : 0 : xNaming->registerObject( sFind, xNewInstance );
434 : : //now insert the new source into the ListBox
435 : 0 : String sEntry(sFind);
436 : 0 : sEntry += '\t';
437 : 0 : sEntry += String(aFilters[0]);
438 : 0 : m_pCreatedDataSource = m_aListLB.InsertEntry(sEntry);
439 : 0 : AddressUserData_Impl* pUserData = new AddressUserData_Impl();
440 : 0 : pUserData->sURL = sURL;
441 : 0 : m_pCreatedDataSource->SetUserData(pUserData);
442 : 0 : m_aListLB.Select(m_pCreatedDataSource);
443 : 0 : m_aCreateListPB.Enable(sal_False);
444 : :
445 : : }
446 : 0 : catch (const Exception&)
447 : : {
448 : 0 : }
449 : : }
450 : 0 : delete pDlg;
451 : 0 : return 0;
452 : : }
453 : :
454 : 0 : IMPL_LINK(SwAddressListDialog, EditHdl_Impl, PushButton*, pButton)
455 : : {
456 : 0 : SvLBoxEntry* pEntry = m_aListLB.FirstSelected();
457 : 0 : AddressUserData_Impl* pUserData = pEntry ? static_cast<AddressUserData_Impl*>(pEntry->GetUserData()) : 0;
458 : 0 : if(pUserData && !pUserData->sURL.isEmpty())
459 : : {
460 : 0 : if(pUserData->xResultSet.is())
461 : : {
462 : 0 : SwMailMergeConfigItem& rConfigItem = m_pAddressPage->GetWizard()->GetConfigItem();
463 : 0 : if(rConfigItem.GetResultSet() != pUserData->xResultSet)
464 : 0 : ::comphelper::disposeComponent( pUserData->xResultSet );
465 : 0 : pUserData->xResultSet = 0;
466 : :
467 : 0 : rConfigItem.DisposeResultSet();
468 : : }
469 : 0 : pUserData->xSource.clear();
470 : 0 : pUserData->xColumnsSupplier.clear();
471 : 0 : pUserData->xConnection.clear();
472 : : // will automatically close if it was the las reference
473 : : SwCreateAddressListDialog* pDlg =
474 : : new SwCreateAddressListDialog(
475 : : pButton,
476 : : pUserData->sURL,
477 : 0 : m_pAddressPage->GetWizard()->GetConfigItem());
478 : 0 : if(RET_OK == pDlg->Execute())
479 : : {
480 : : }
481 : 0 : delete pDlg;
482 : : }
483 : 0 : return 0;
484 : : };
485 : :
486 : 0 : IMPL_LINK_NOARG(SwAddressListDialog, ListBoxSelectHdl_Impl)
487 : : {
488 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
489 : : Application::PostUserEvent( STATIC_LINK( this, SwAddressListDialog,
490 : 0 : StaticListBoxSelectHdl_Impl ), pSelect );
491 : 0 : return 0;
492 : : }
493 : :
494 : 0 : IMPL_STATIC_LINK(SwAddressListDialog, StaticListBoxSelectHdl_Impl, SvLBoxEntry*, pSelect)
495 : : {
496 : : //prevent nested calls of the select handler
497 : 0 : if(pThis->m_bInSelectHdl)
498 : 0 : return 0;
499 : 0 : pThis->EnterWait();
500 : 0 : pThis->m_bInSelectHdl = true;
501 : 0 : AddressUserData_Impl* pUserData = 0;
502 : 0 : if(pSelect)
503 : : {
504 : 0 : String sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
505 : 0 : if(!sTable.Len())
506 : : {
507 : 0 : pThis->m_aListLB.SetEntryText(pThis->m_sConnecting, pSelect, ITEMID_TABLE - 1);
508 : : // allow painting of the new entry
509 : 0 : pThis->m_aListLB.Window::Invalidate(INVALIDATE_UPDATE);
510 : 0 : for (sal_uInt16 i = 0; i < 10; i++)
511 : 0 : Application::Reschedule();
512 : : }
513 : :
514 : 0 : pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
515 : 0 : if(pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
516 : : {
517 : : /*
518 : : * We're a callback from a selection from a list box, which takes
519 : : * place on mouse down before mouse up. The next dialog also has a
520 : : * list box. Spawning it means this list box doesn't get the mouse
521 : : * down event. So it sticks on "making selection" mode. So if you
522 : : * cancel the next dialog and just move the mouse out of this entry
523 : : * and back then the dialog pops up again, without requiring a click
524 : : *
525 : : * Most expedient thing to do is to manually end the parent selection
526 : : * here.
527 : : */
528 : 0 : pThis->m_aListLB.EndSelection();
529 : 0 : pThis->DetectTablesAndQueries(pSelect, !sTable.Len());
530 : : }
531 : : else
532 : : {
533 : : //otherwise set the selected db-data
534 : 0 : pThis->m_aDBData.sDataSource = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
535 : 0 : pThis->m_aDBData.sCommand = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
536 : 0 : pThis->m_aDBData.nCommandType = pUserData->nCommandType;
537 : 0 : pThis->m_aOK.Enable(sal_True);
538 : : }
539 : 0 : sTable = pThis->m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
540 : 0 : if(sTable == pThis->m_sConnecting)
541 : 0 : pThis->m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
542 : : }
543 : 0 : pThis->m_aEditPB.Enable(pUserData && !pUserData->sURL.isEmpty() &&
544 : 0 : SWUnoHelper::UCB_IsFile( pUserData->sURL ) && //#i97577#
545 : 0 : !SWUnoHelper::UCB_IsReadOnlyFileName( pUserData->sURL ) );
546 : 0 : pThis->m_bInSelectHdl = false;
547 : 0 : pThis->LeaveWait();
548 : 0 : return 0;
549 : : }
550 : :
551 : : // detect the number of tables for a data source
552 : : // if only one is available then set it at the entry
553 : 0 : void SwAddressListDialog::DetectTablesAndQueries(
554 : : SvLBoxEntry* pSelect,
555 : : bool bWidthDialog)
556 : : {
557 : : try
558 : : {
559 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
560 : 0 : uno::Reference<XCompletedConnection> xComplConnection;
561 : 0 : if(!pUserData->xConnection.is())
562 : : {
563 : 0 : m_aDBData.sDataSource = m_aListLB.GetEntryText(pSelect, ITEMID_NAME - 1);
564 : 0 : m_xDBContext->getByName(m_aDBData.sDataSource) >>= xComplConnection;
565 : 0 : pUserData->xSource = uno::Reference<XDataSource>(xComplConnection, UNO_QUERY);
566 : :
567 : 0 : uno::Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
568 : : uno::Reference< XInteractionHandler > xHandler(
569 : 0 : xMgr->createInstance( C2U( "com.sun.star.task.InteractionHandler" )), UNO_QUERY);
570 : 0 : pUserData->xConnection = SharedConnection( xComplConnection->connectWithCompletion( xHandler ) );
571 : : }
572 : 0 : if(pUserData->xConnection.is())
573 : : {
574 : 0 : sal_Int32 nTables = 0;
575 : 0 : uno::Sequence<rtl::OUString> aTables;
576 : 0 : uno::Sequence<rtl::OUString> aQueries;
577 : 0 : uno::Reference<XTablesSupplier> xTSupplier(pUserData->xConnection, UNO_QUERY);
578 : 0 : if(xTSupplier.is())
579 : : {
580 : 0 : uno::Reference<XNameAccess> xTbls = xTSupplier->getTables();
581 : 0 : aTables = xTbls->getElementNames();
582 : 0 : nTables += aTables.getLength();
583 : : }
584 : 0 : uno::Reference<XQueriesSupplier> xQSupplier(pUserData->xConnection, UNO_QUERY);
585 : 0 : if(xQSupplier.is())
586 : : {
587 : 0 : uno::Reference<XNameAccess> xQueries = xQSupplier->getQueries();
588 : 0 : aQueries = xQueries->getElementNames();
589 : 0 : nTables += aQueries.getLength();
590 : : }
591 : 0 : pUserData->nTableAndQueryCount = nTables;
592 : 0 : if(nTables > 1 && bWidthDialog)
593 : : {
594 : : //now call the table select dialog - if more than one table exists
595 : 0 : SwSelectDBTableDialog* pDlg = new SwSelectDBTableDialog(this, pUserData->xConnection);
596 : 0 : String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
597 : 0 : if(sTable.Len())
598 : 0 : pDlg->SetSelectedTable(sTable, pUserData->nCommandType == CommandType::TABLE);
599 : 0 : if(RET_OK == pDlg->Execute())
600 : : {
601 : : bool bIsTable;
602 : 0 : m_aDBData.sCommand = pDlg->GetSelectedTable(bIsTable);
603 : 0 : m_aDBData.nCommandType = bIsTable ? CommandType::TABLE : CommandType::QUERY;
604 : 0 : pUserData->nCommandType = m_aDBData.nCommandType;
605 : : }
606 : 0 : delete pDlg;
607 : : }
608 : 0 : else if(nTables == 1)
609 : : {
610 : 0 : if(aTables.getLength())
611 : : {
612 : 0 : m_aDBData.sCommand = aTables[0];
613 : 0 : m_aDBData.nCommandType = CommandType::TABLE;
614 : : }
615 : : else
616 : : {
617 : 0 : m_aDBData.sCommand = aQueries[0];
618 : 0 : m_aDBData.nCommandType = CommandType::QUERY;
619 : : }
620 : 0 : }
621 : : }
622 : 0 : if ( !m_aDBData.sCommand.isEmpty() )
623 : : {
624 : 0 : uno::Reference<beans::XPropertySet> xSourceProperties;
625 : 0 : m_xDBContext->getByName(m_aDBData.sDataSource) >>= xSourceProperties;
626 : 0 : pUserData->sURL = lcl_getFlatURL( xSourceProperties );
627 : :
628 : : pUserData->xColumnsSupplier = SwNewDBMgr::GetColumnSupplier(pUserData->xConnection,
629 : : m_aDBData.sCommand,
630 : : m_aDBData.nCommandType == CommandType::TABLE ?
631 : 0 : SW_DB_SELECT_TABLE : SW_DB_SELECT_QUERY );
632 : : //#i97577#
633 : 0 : if( pUserData->xColumnsSupplier.is() )
634 : 0 : m_aListLB.SetEntryText(m_aDBData.sCommand, pSelect, ITEMID_TABLE - 1);
635 : : else
636 : 0 : m_aListLB.SetEntryText(String(), pSelect, ITEMID_TABLE - 1);
637 : : }
638 : 0 : String sCommand = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
639 : 0 : m_aOK.Enable(pSelect && sCommand.Len());
640 : 0 : m_aFilterPB.Enable( pUserData->xConnection.is() && sCommand.Len() );
641 : 0 : m_aTablePB.Enable( pUserData->nTableAndQueryCount > 1 );
642 : : }
643 : 0 : catch (const Exception&)
644 : : {
645 : : OSL_FAIL("exception caught in SwAddressListDialog::DetectTablesAndQueries");
646 : 0 : m_aOK.Enable( sal_False );
647 : : }
648 : 0 : }
649 : :
650 : 0 : IMPL_LINK(SwAddressListDialog, TableSelectHdl_Impl, PushButton*, pButton)
651 : : {
652 : 0 : EnterWait();
653 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
654 : 0 : if(pSelect)
655 : : {
656 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
657 : : //only call the table select dialog if tables have not been searched for or there
658 : : //are more than 1
659 : 0 : String sTable = m_aListLB.GetEntryText(pSelect, ITEMID_TABLE - 1);
660 : 0 : if( pUserData->nTableAndQueryCount > 1 || pUserData->nTableAndQueryCount == -1)
661 : : {
662 : 0 : DetectTablesAndQueries(pSelect, (pButton != 0) || (!sTable.Len()));
663 : 0 : }
664 : : }
665 : :
666 : 0 : LeaveWait();
667 : 0 : return 0;
668 : : }
669 : :
670 : 0 : IMPL_LINK_NOARG(SwAddressListDialog, OKHdl_Impl)
671 : : {
672 : 0 : EndDialog(sal_True);
673 : 0 : return 0;
674 : : }
675 : :
676 : 0 : uno::Reference< XDataSource> SwAddressListDialog::GetSource()
677 : : {
678 : 0 : uno::Reference< XDataSource> xRet;
679 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
680 : 0 : if(pSelect)
681 : : {
682 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
683 : 0 : xRet = pUserData->xSource;
684 : : }
685 : 0 : return xRet;
686 : :
687 : : }
688 : :
689 : 0 : SharedConnection SwAddressListDialog::GetConnection()
690 : : {
691 : 0 : SharedConnection xRet;
692 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
693 : 0 : if(pSelect)
694 : : {
695 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
696 : 0 : xRet = pUserData->xConnection;
697 : : }
698 : 0 : return xRet;
699 : : }
700 : :
701 : 0 : uno::Reference< XColumnsSupplier> SwAddressListDialog::GetColumnsSupplier()
702 : : {
703 : 0 : uno::Reference< XColumnsSupplier> xRet;
704 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
705 : 0 : if(pSelect)
706 : : {
707 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
708 : 0 : xRet = pUserData->xColumnsSupplier;
709 : : }
710 : 0 : return xRet;
711 : : }
712 : :
713 : 0 : ::rtl::OUString SwAddressListDialog::GetFilter()
714 : : {
715 : 0 : ::rtl::OUString sRet;
716 : 0 : SvLBoxEntry* pSelect = m_aListLB.FirstSelected();
717 : 0 : if(pSelect)
718 : : {
719 : 0 : AddressUserData_Impl* pUserData = static_cast<AddressUserData_Impl*>(pSelect->GetUserData());
720 : 0 : sRet = pUserData->sFilter;
721 : : }
722 : 0 : return sRet;
723 : : }
724 : :
725 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|