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