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 "commonpagesdbp.hxx"
21 : #include "dbpresid.hrc"
22 : #include "componentmodule.hxx"
23 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 : #include <com/sun/star/sdb/XCompletedConnection.hpp>
25 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
26 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
27 : #include <com/sun/star/sdbc/XConnection.hpp>
28 : #include <com/sun/star/sdb/SQLContext.hpp>
29 : #include <com/sun/star/sdbc/SQLWarning.hpp>
30 : #include <com/sun/star/sdb/CommandType.hpp>
31 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
32 : #include <tools/debug.hxx>
33 : #include <svtools/localresaccess.hxx>
34 : #include <comphelper/interaction.hxx>
35 : #include <connectivity/dbtools.hxx>
36 : #include <vcl/stdtext.hxx>
37 : #include <vcl/waitobj.hxx>
38 : #include <sfx2/docfilt.hxx>
39 : #include <unotools/pathoptions.hxx>
40 : #include <sfx2/filedlghelper.hxx>
41 : #include <svl/filenotation.hxx>
42 : //.........................................................................
43 : namespace dbp
44 : {
45 : //.........................................................................
46 :
47 : using namespace ::com::sun::star;
48 : using namespace ::com::sun::star::uno;
49 : using namespace ::com::sun::star::lang;
50 : using namespace ::com::sun::star::container;
51 : using namespace ::com::sun::star::sdb;
52 : using namespace ::com::sun::star::sdbc;
53 : using namespace ::com::sun::star::sdbcx;
54 : using namespace ::com::sun::star::task;
55 : using namespace ::comphelper;
56 :
57 : //=====================================================================
58 : //= OTableSelectionPage
59 : //=====================================================================
60 : //---------------------------------------------------------------------
61 0 : OTableSelectionPage::OTableSelectionPage(OControlWizard* _pParent)
62 : :OControlWizardPage(_pParent, ModuleRes(RID_PAGE_TABLESELECTION))
63 : ,m_aData (this, ModuleRes(FL_DATA))
64 : ,m_aExplanation (this, ModuleRes(FT_EXPLANATION))
65 : ,m_aDatasourceLabel (this, ModuleRes(FT_DATASOURCE))
66 : ,m_aDatasource (this, ModuleRes(LB_DATASOURCE))
67 : ,m_aSearchDatabase (this, ModuleRes(PB_FORMDATASOURCE))
68 : ,m_aTableLabel (this, ModuleRes(FT_TABLE))
69 0 : ,m_aTable (this, ModuleRes(LB_TABLE))
70 : {
71 0 : FreeResource();
72 :
73 0 : implCollectDatasource();
74 :
75 0 : m_aDatasource.SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
76 0 : m_aTable.SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
77 0 : m_aTable.SetDoubleClickHdl(LINK(this, OTableSelectionPage, OnListboxDoubleClicked));
78 0 : m_aSearchDatabase.SetClickHdl(LINK(this, OTableSelectionPage, OnSearchClicked));
79 :
80 0 : m_aDatasource.SetDropDownLineCount(10);
81 0 : }
82 :
83 : //---------------------------------------------------------------------
84 0 : void OTableSelectionPage::ActivatePage()
85 : {
86 0 : OControlWizardPage::ActivatePage();
87 0 : m_aDatasource.GrabFocus();
88 0 : }
89 :
90 : //---------------------------------------------------------------------
91 0 : bool OTableSelectionPage::canAdvance() const
92 : {
93 0 : if (!OControlWizardPage::canAdvance())
94 0 : return false;
95 :
96 0 : if (0 == m_aDatasource.GetSelectEntryCount())
97 0 : return false;
98 :
99 0 : if (0 == m_aTable.GetSelectEntryCount())
100 0 : return false;
101 :
102 0 : return sal_True;
103 : }
104 :
105 : //---------------------------------------------------------------------
106 0 : void OTableSelectionPage::initializePage()
107 : {
108 0 : OControlWizardPage::initializePage();
109 :
110 0 : const OControlWizardContext& rContext = getContext();
111 : try
112 : {
113 0 : ::rtl::OUString sDataSourceName;
114 0 : rContext.xForm->getPropertyValue(::rtl::OUString("DataSourceName")) >>= sDataSourceName;
115 :
116 0 : Reference< XConnection > xConnection;
117 0 : bool bEmbedded = ::dbtools::isEmbeddedInDatabase( rContext.xForm, xConnection );
118 0 : if ( bEmbedded )
119 : {
120 0 : m_aDatasource.Hide();
121 0 : m_aDatasourceLabel.Hide();
122 0 : m_aSearchDatabase.Hide();
123 0 : m_aTableLabel.SetPosPixel(m_aDatasourceLabel.GetPosPixel());
124 0 : m_aTable.SetPosPixel(m_aDatasource.GetPosPixel());
125 0 : m_aDatasource.InsertEntry(sDataSourceName);
126 : }
127 0 : m_aDatasource.SelectEntry(sDataSourceName);
128 :
129 0 : implFillTables(xConnection);
130 :
131 0 : ::rtl::OUString sCommand;
132 0 : OSL_VERIFY( rContext.xForm->getPropertyValue( ::rtl::OUString("Command") ) >>= sCommand );
133 0 : sal_Int32 nCommandType = CommandType::TABLE;
134 0 : OSL_VERIFY( rContext.xForm->getPropertyValue( ::rtl::OUString("CommandType") ) >>= nCommandType );
135 :
136 : // search the entry of the given type with the given name
137 0 : for ( sal_uInt16 nLookup = 0; nLookup < m_aTable.GetEntryCount(); ++nLookup )
138 : {
139 0 : if (sCommand.equals(m_aTable.GetEntry(nLookup)))
140 : {
141 0 : if ( reinterpret_cast< sal_IntPtr >( m_aTable.GetEntryData( nLookup ) ) == nCommandType )
142 : {
143 0 : m_aTable.SelectEntryPos( nLookup );
144 0 : break;
145 : }
146 : }
147 0 : }
148 : }
149 0 : catch(const Exception&)
150 : {
151 : OSL_FAIL("OTableSelectionPage::initializePage: caught an exception!");
152 : }
153 0 : }
154 :
155 : //---------------------------------------------------------------------
156 0 : sal_Bool OTableSelectionPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
157 : {
158 0 : if (!OControlWizardPage::commitPage(_eReason))
159 0 : return sal_False;
160 :
161 0 : const OControlWizardContext& rContext = getContext();
162 : try
163 : {
164 0 : Reference< XConnection > xOldConn;
165 0 : if ( !rContext.bEmbedded )
166 : {
167 0 : xOldConn = getFormConnection();
168 :
169 0 : ::rtl::OUString sDataSource = m_aDatasource.GetSelectEntry();
170 0 : rContext.xForm->setPropertyValue( ::rtl::OUString("DataSourceName"), makeAny( sDataSource ) );
171 : }
172 0 : ::rtl::OUString sCommand = m_aTable.GetSelectEntry();
173 0 : sal_Int32 nCommandType = reinterpret_cast< sal_IntPtr >( m_aTable.GetEntryData( m_aTable.GetSelectEntryPos() ) );
174 :
175 0 : rContext.xForm->setPropertyValue( ::rtl::OUString("Command"), makeAny( sCommand ) );
176 0 : rContext.xForm->setPropertyValue( ::rtl::OUString("CommandType"), makeAny( nCommandType ) );
177 :
178 0 : if ( !rContext.bEmbedded )
179 0 : setFormConnection( xOldConn, sal_False );
180 :
181 0 : if (!updateContext())
182 0 : return sal_False;
183 : }
184 0 : catch(const Exception&)
185 : {
186 : OSL_FAIL("OTableSelectionPage::commitPage: caught an exception!");
187 : }
188 :
189 0 : return sal_True;
190 : }
191 :
192 : //---------------------------------------------------------------------
193 0 : IMPL_LINK( OTableSelectionPage, OnSearchClicked, PushButton*, /*_pButton*/ )
194 : {
195 : ::sfx2::FileDialogHelper aFileDlg(
196 0 : ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION, 0);
197 0 : aFileDlg.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
198 :
199 0 : const SfxFilter* pFilter = SfxFilter::GetFilterByName(rtl::OUString("StarOffice XML (Base)"));
200 : OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
201 0 : if ( pFilter )
202 : {
203 0 : aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
204 : }
205 :
206 0 : if (0 == aFileDlg.Execute())
207 : {
208 0 : String sDataSourceName = aFileDlg.GetPath();
209 0 : ::svt::OFileNotation aFileNotation(sDataSourceName);
210 0 : sDataSourceName = aFileNotation.get(::svt::OFileNotation::N_SYSTEM);
211 0 : m_aDatasource.InsertEntry(sDataSourceName);
212 0 : m_aDatasource.SelectEntry(sDataSourceName);
213 0 : LINK(this, OTableSelectionPage, OnListboxSelection).Call(&m_aDatasource);
214 : }
215 0 : return 0L;
216 : }
217 : //---------------------------------------------------------------------
218 0 : IMPL_LINK( OTableSelectionPage, OnListboxDoubleClicked, ListBox*, _pBox )
219 : {
220 0 : if (_pBox->GetSelectEntryCount())
221 0 : getDialog()->travelNext();
222 0 : return 0L;
223 : }
224 :
225 : //---------------------------------------------------------------------
226 0 : IMPL_LINK( OTableSelectionPage, OnListboxSelection, ListBox*, _pBox )
227 : {
228 0 : if (&m_aDatasource == _pBox)
229 : { // new data source selected
230 0 : implFillTables();
231 : }
232 : else
233 : {
234 : }
235 :
236 0 : updateDialogTravelUI();
237 :
238 0 : return 0L;
239 : }
240 :
241 : //---------------------------------------------------------------------
242 : namespace
243 : {
244 0 : void lcl_fillEntries( ListBox& _rListBox, const Sequence< ::rtl::OUString >& _rNames, const Image& _rImage, sal_Int32 _nCommandType )
245 : {
246 0 : const ::rtl::OUString* pNames = _rNames.getConstArray();
247 0 : const ::rtl::OUString* pNamesEnd = _rNames.getConstArray() + _rNames.getLength();
248 0 : sal_uInt16 nPos = 0;
249 0 : while ( pNames != pNamesEnd )
250 : {
251 0 : nPos = _rListBox.InsertEntry( *pNames++, _rImage );
252 0 : _rListBox.SetEntryData( nPos, reinterpret_cast< void* >( _nCommandType ) );
253 : }
254 0 : }
255 : }
256 :
257 : //---------------------------------------------------------------------
258 0 : void OTableSelectionPage::implFillTables(const Reference< XConnection >& _rxConn)
259 : {
260 0 : m_aTable.Clear();
261 :
262 0 : WaitObject aWaitCursor(this);
263 :
264 : // will be the table tables of the selected data source
265 0 : Sequence< ::rtl::OUString > aTableNames;
266 0 : Sequence< ::rtl::OUString > aQueryNames;
267 :
268 : // connect to the data source
269 0 : Any aSQLException;
270 0 : Reference< XConnection > xConn = _rxConn;
271 0 : if ( !xConn.is() )
272 : {
273 0 : if (!m_xDSContext.is())
274 : return;
275 : // connect to the data source
276 : try
277 : {
278 0 : ::rtl::OUString sCurrentDatasource = m_aDatasource.GetSelectEntry();
279 0 : if (!sCurrentDatasource.isEmpty())
280 : {
281 : // obtain the DS object
282 0 : Reference< XCompletedConnection > xDatasource;
283 : // check if I know this one otherwise transform it into a file URL
284 0 : if ( !m_xDSContext->hasByName(sCurrentDatasource) )
285 : {
286 0 : ::svt::OFileNotation aFileNotation(sCurrentDatasource);
287 0 : sCurrentDatasource = aFileNotation.get(::svt::OFileNotation::N_URL);
288 : }
289 :
290 0 : if (m_xDSContext->getByName(sCurrentDatasource) >>= xDatasource)
291 : { // connect
292 : // get the default SDB interaction handler
293 0 : Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(this);
294 0 : if (!xHandler.is() )
295 : return;
296 0 : xConn = xDatasource->connectWithCompletion(xHandler);
297 0 : setFormConnection( xConn );
298 : }
299 : else
300 : {
301 : OSL_FAIL("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
302 0 : }
303 0 : }
304 : }
305 0 : catch(const SQLContext& e) { aSQLException <<= e; }
306 0 : catch(const SQLWarning& e) { aSQLException <<= e; }
307 0 : catch(const SQLException& e) { aSQLException <<= e; }
308 0 : catch (const Exception&)
309 : {
310 : OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
311 : }
312 : }
313 :
314 : // will be the table tables of the selected data source
315 0 : if ( xConn.is() )
316 : {
317 : try
318 : {
319 : // get the tables
320 0 : Reference< XTablesSupplier > xSupplTables(xConn, UNO_QUERY);
321 0 : if ( xSupplTables.is() )
322 : {
323 0 : Reference< XNameAccess > xTables(xSupplTables->getTables(), UNO_QUERY);
324 0 : if (xTables.is())
325 0 : aTableNames = xTables->getElementNames();
326 : }
327 :
328 : // and the queries
329 0 : Reference< XQueriesSupplier > xSuppQueries( xConn, UNO_QUERY );
330 0 : if ( xSuppQueries.is() )
331 : {
332 0 : Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_QUERY );
333 0 : if ( xQueries.is() )
334 0 : aQueryNames = xQueries->getElementNames();
335 0 : }
336 : }
337 0 : catch(const SQLContext& e) { aSQLException <<= e; }
338 0 : catch(const SQLWarning& e) { aSQLException <<= e; }
339 0 : catch(const SQLException& e) { aSQLException <<= e; }
340 0 : catch (const Exception&)
341 : {
342 : OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
343 : }
344 : }
345 :
346 :
347 0 : if ( aSQLException.hasValue() )
348 : { // an SQLException (or derivee) was thrown ...
349 0 : Reference< XInteractionRequest > xRequest = new OInteractionRequest(aSQLException);
350 : try
351 : {
352 : // get the default SDB interaction handler
353 0 : Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(this);
354 0 : if ( xHandler.is() )
355 0 : xHandler->handle(xRequest);
356 : }
357 0 : catch(const Exception&) { }
358 0 : return;
359 : }
360 :
361 0 : Image aTableImage, aQueryImage;
362 : {
363 0 : ::svt::OLocalResourceAccess aLocalResAccess( ModuleRes( RID_PAGE_TABLESELECTION ), RSC_TABPAGE );
364 :
365 0 : aTableImage = Image( ModuleRes( IMG_TABLE ) );
366 0 : aQueryImage = Image( ModuleRes( IMG_QUERY ) );
367 : }
368 0 : lcl_fillEntries( m_aTable, aTableNames, aTableImage, CommandType::TABLE );
369 0 : lcl_fillEntries( m_aTable, aQueryNames, aQueryImage, CommandType::QUERY );
370 : }
371 :
372 : //---------------------------------------------------------------------
373 0 : void OTableSelectionPage::implCollectDatasource()
374 : {
375 : try
376 : {
377 0 : m_xDSContext = getContext().xDatasourceContext;
378 0 : if (m_xDSContext.is())
379 0 : fillListBox(m_aDatasource, m_xDSContext->getElementNames());
380 : }
381 0 : catch (const Exception&)
382 : {
383 : OSL_FAIL("OTableSelectionPage::implCollectDatasource: could not collect the data source names!");
384 : }
385 0 : }
386 :
387 : //=====================================================================
388 : //= OMaybeListSelectionPage
389 : //=====================================================================
390 : //---------------------------------------------------------------------
391 0 : OMaybeListSelectionPage::OMaybeListSelectionPage( OControlWizard* _pParent, const ResId& _rId )
392 : :OControlWizardPage(_pParent, _rId)
393 : ,m_pYes(NULL)
394 : ,m_pNo(NULL)
395 0 : ,m_pList(NULL)
396 : {
397 0 : }
398 :
399 : //---------------------------------------------------------------------
400 0 : void OMaybeListSelectionPage::announceControls(RadioButton& _rYesButton, RadioButton& _rNoButton, ListBox& _rSelection)
401 : {
402 0 : m_pYes = &_rYesButton;
403 0 : m_pNo = &_rNoButton;
404 0 : m_pList = &_rSelection;
405 :
406 0 : m_pYes->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
407 0 : m_pNo->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
408 0 : implEnableWindows();
409 0 : }
410 :
411 : //---------------------------------------------------------------------
412 0 : IMPL_LINK( OMaybeListSelectionPage, OnRadioSelected, RadioButton*, /*NOTINTERESTEDIN*/ )
413 : {
414 0 : implEnableWindows();
415 0 : return 0L;
416 : }
417 :
418 : //---------------------------------------------------------------------
419 0 : void OMaybeListSelectionPage::implInitialize(const String& _rSelection)
420 : {
421 : DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::implInitialize: no controls announced!");
422 0 : sal_Bool bIsSelection = (0 != _rSelection.Len());
423 0 : m_pYes->Check(bIsSelection);
424 0 : m_pNo->Check(!bIsSelection);
425 0 : m_pList->Enable(bIsSelection);
426 :
427 0 : m_pList->SelectEntry(bIsSelection ? _rSelection : String());
428 0 : }
429 :
430 : //---------------------------------------------------------------------
431 0 : void OMaybeListSelectionPage::implCommit(String& _rSelection)
432 : {
433 0 : _rSelection = m_pYes->IsChecked() ? m_pList->GetSelectEntry() : String();
434 0 : }
435 :
436 : //---------------------------------------------------------------------
437 0 : void OMaybeListSelectionPage::implEnableWindows()
438 : {
439 0 : m_pList->Enable(m_pYes->IsChecked());
440 0 : }
441 :
442 : //---------------------------------------------------------------------
443 0 : void OMaybeListSelectionPage::ActivatePage()
444 : {
445 0 : OControlWizardPage::ActivatePage();
446 :
447 : DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::ActivatePage: no controls announced!");
448 0 : if (m_pYes->IsChecked())
449 0 : m_pList->GrabFocus();
450 : else
451 0 : m_pNo->GrabFocus();
452 0 : }
453 :
454 : //=====================================================================
455 : //= ODBFieldPage
456 : //=====================================================================
457 : //---------------------------------------------------------------------
458 0 : ODBFieldPage::ODBFieldPage( OControlWizard* _pParent )
459 : :OMaybeListSelectionPage(_pParent, ModuleRes(RID_PAGE_OPTION_DBFIELD))
460 : ,m_aFrame (this, ModuleRes(FL_DATABASEFIELD_EXPL))
461 : ,m_aDescription (this, ModuleRes(FT_DATABASEFIELD_EXPL))
462 : ,m_aQuestion (this, ModuleRes(FT_DATABASEFIELD_QUEST))
463 : ,m_aStoreYes (this, ModuleRes(RB_STOREINFIELD_YES))
464 : ,m_aStoreNo (this, ModuleRes(LB_STOREINFIELD))
465 0 : ,m_aStoreWhere (this, ModuleRes(RB_STOREINFIELD_NO))
466 : {
467 0 : FreeResource();
468 0 : announceControls(m_aStoreYes, m_aStoreNo, m_aStoreWhere);
469 0 : m_aStoreWhere.SetDropDownLineCount(10);
470 0 : }
471 :
472 : //---------------------------------------------------------------------
473 0 : void ODBFieldPage::initializePage()
474 : {
475 0 : OMaybeListSelectionPage::initializePage();
476 :
477 : // fill the fields page
478 0 : fillListBox(m_aStoreWhere, getContext().aFieldNames);
479 :
480 0 : implInitialize(getDBFieldSetting());
481 0 : }
482 :
483 : //---------------------------------------------------------------------
484 0 : sal_Bool ODBFieldPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
485 : {
486 0 : if (!OMaybeListSelectionPage::commitPage(_eReason))
487 0 : return sal_False;
488 :
489 0 : implCommit(getDBFieldSetting());
490 :
491 0 : return sal_True;
492 : }
493 :
494 : //.........................................................................
495 : } // namespace dbp
496 : //.........................................................................
497 :
498 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|