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