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