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