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