xref: /trunk/main/dbaccess/source/ui/dlg/dsselect.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_dbaccess.hxx"
30 
31 #ifndef _DBAUI_DSSELECT_HXX_
32 #include "dsselect.hxx"
33 #endif
34 #ifndef _DBAUI_DSSELECT_HRC_
35 #include "dsselect.hrc"
36 #endif
37 #ifndef _DBU_DLG_HRC_
38 #include "dbu_dlg.hrc"
39 #endif
40 #ifndef _SV_MSGBOX_HXX
41 #include <vcl/msgbox.hxx>
42 #endif
43 #ifndef _DBAUI_LOCALRESACCESS_HXX_
44 #include "localresaccess.hxx"
45 #endif
46 #ifndef _TOOLS_RCID_H
47 #include <tools/rcid.h>
48 #endif
49 
50 #ifndef _COM_SUN_STAR_SDBCX_XCREATECATALOG_HPP_
51 #include <com/sun/star/sdbcx/XCreateCatalog.hpp>
52 #endif
53 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
54 #include <com/sun/star/beans/XPropertySet.hpp>
55 #endif
56 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSETINFO_HPP_
57 #include <com/sun/star/beans/XPropertySetInfo.hpp>
58 #endif
59 #ifndef _COM_SUN_STAR_UI_DIALOGS_XEXECUTABLEDIALOG_HPP_
60 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
61 #endif
62 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
63 #include <com/sun/star/awt/XWindow.hpp>
64 #endif
65 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
66 #include "dbustrings.hrc"
67 #endif
68 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
69 #include <toolkit/helper/vclunohelper.hxx>
70 #endif
71 #ifndef _COMPHELPER_EXTRACT_HXX_
72 #include <comphelper/extract.hxx>
73 #endif
74 #ifndef _COMPHELPER_TYPES_HXX_
75 #include <comphelper/types.hxx>
76 #endif
77 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
78 #include <comphelper/processfactory.hxx>
79 #endif
80 #ifndef _DBAUI_DATASOURCEITEMS_HXX_
81 #include "dsitems.hxx"
82 #endif
83 #ifndef _SFXSTRITEM_HXX
84 #include <svl/stritem.hxx>
85 #endif
86 #ifndef _SFXINTITEM_HXX
87 #include <svl/intitem.hxx>
88 #endif
89 #ifndef _SFXENUMITEM_HXX
90 #include <svl/eitem.hxx>
91 #endif
92 #ifndef _SFXITEMSET_HXX
93 #include <svl/itemset.hxx>
94 #endif
95 
96 //.........................................................................
97 namespace dbaui
98 {
99 //.........................................................................
100 using namespace ::com::sun::star::uno;
101 using namespace ::com::sun::star::beans;
102 using namespace ::com::sun::star::sdbc;
103 using namespace ::com::sun::star::sdbcx;
104 using namespace ::com::sun::star::ui::dialogs;
105 using namespace ::comphelper;
106 //==================================================================
107 ODatasourceSelectDialog::ODatasourceSelectDialog(Window* _pParent, const StringBag& _rDatasources, bool _bAdabas,SfxItemSet* _pOutputSet)
108      :ModalDialog(_pParent, ModuleRes(DLG_DATASOURCE_SELECTION))
109      ,m_aDescription        (this, ModuleRes(FT_DESCRIPTION))
110      ,m_aDatasource         (this, ModuleRes(LB_DATASOURCE))
111      ,m_aOk                 (this, ModuleRes(PB_OK))
112      ,m_aCancel             (this, ModuleRes(PB_CANCEL))
113      ,m_aHelp               (this, ModuleRes(PB_HELP))
114 #ifdef HAVE_ODBC_ADMINISTRATION
115      ,m_aManageDatasources  (this, ModuleRes(PB_MANAGE))
116 #endif
117      ,m_aCreateAdabasDB     (this, ModuleRes(PB_CREATE))
118      ,m_pOutputSet(_pOutputSet)
119 {
120     if ( _bAdabas )
121     {   // set a new title (indicating that we're browsing local data sources only)
122         SetText(ModuleRes(STR_LOCAL_DATASOURCES));
123         m_aDescription.SetText(ModuleRes(STR_DESCRIPTION2));
124 
125         m_aCreateAdabasDB.Show();
126         m_aCreateAdabasDB.SetClickHdl(LINK(this,ODatasourceSelectDialog,CreateDBClickHdl));
127 
128         // resize the dialog a little bit, 'cause Adabas data source names are usually somewhat shorter
129         // than ODBC ones are
130 
131         // shrink the listbox
132         Size aOldSize = m_aDatasource.GetSizePixel();
133         Size aNewSize(3 * aOldSize.Width() / 4, aOldSize.Height());
134         m_aDatasource.SetSizePixel(aNewSize);
135 
136         sal_Int32 nLostPixels = aOldSize.Width() - aNewSize.Width();
137 
138         // shrink the fixed text
139         aOldSize = m_aDescription.GetSizePixel();
140         m_aDescription.SetSizePixel(Size(aOldSize.Width() - nLostPixels, aOldSize.Height()));
141 
142         // move the buttons
143         PushButton* pButtons[] = { &m_aOk, &m_aCancel, &m_aHelp ,&m_aCreateAdabasDB};
144         for (size_t i=0; i<sizeof(pButtons)/sizeof(pButtons[0]); ++i)
145         {
146             Point aOldPos = pButtons[i]->GetPosPixel();
147             pButtons[i]->SetPosPixel(Point(aOldPos.X() - nLostPixels, aOldPos.Y()));
148         }
149 
150         // resize the dialog itself
151         aOldSize = GetSizePixel();
152         SetSizePixel(Size(aOldSize.Width() - nLostPixels, aOldSize.Height()));
153     }
154 
155     fillListBox(_rDatasources);
156 #ifdef HAVE_ODBC_ADMINISTRATION
157     // allow ODBC datasource managenment
158     if (  !_bAdabas )
159     {
160         m_aManageDatasources.Show();
161         m_aManageDatasources.Enable();
162         m_aManageDatasources.SetClickHdl(LINK(this,ODatasourceSelectDialog,ManageClickHdl));
163     }
164 #endif
165     m_aDatasource.SetDoubleClickHdl(LINK(this,ODatasourceSelectDialog,ListDblClickHdl));
166     FreeResource();
167 }
168 
169 // -----------------------------------------------------------------------
170 ODatasourceSelectDialog::~ODatasourceSelectDialog()
171 {
172 }
173 
174 // -----------------------------------------------------------------------
175 IMPL_LINK( ODatasourceSelectDialog, ListDblClickHdl, ListBox *, pListBox )
176 {
177     if (pListBox->GetSelectEntryCount())
178         EndDialog(RET_OK);
179     return 0;
180 }
181 // -----------------------------------------------------------------------
182 IMPL_LINK( ODatasourceSelectDialog, CreateDBClickHdl, PushButton*, /*pButton*/ )
183 {
184     try
185     {
186         OSL_ENSURE(m_pOutputSet,"No itemset given!");
187         Reference< ::com::sun::star::lang::XMultiServiceFactory > xORB = ::comphelper::getProcessServiceFactory();
188         Reference<XCreateCatalog> xCatalog(xORB->createInstance(SERVICE_EXTENDED_ADABAS_DRIVER),UNO_QUERY);
189         if ( xCatalog.is() && m_pOutputSet )
190         {
191             Sequence< Any > aArgs(2);
192             aArgs[0] <<= PropertyValue(::rtl::OUString::createFromAscii("CreateCatalog"), 0,makeAny(xCatalog) , PropertyState_DIRECT_VALUE);
193             aArgs[1] <<= PropertyValue(PROPERTY_PARENTWINDOW, 0, makeAny(VCLUnoHelper::GetInterface(this)), PropertyState_DIRECT_VALUE);
194 
195             Reference< XExecutableDialog > xDialog(
196                 xORB->createInstanceWithArguments(SERVICE_SDB_ADABASCREATIONDIALOG, aArgs), UNO_QUERY);
197             if (!xDialog.is())
198             {
199                 //  ShowServiceNotAvailableError(this, String(SERVICE_SDB_ADABASCREATIONDIALOG), sal_True);
200                 return 0L;
201             }
202 
203             if ( xDialog->execute() == RET_OK )
204             {
205                 Reference<XPropertySet> xProp(xDialog,UNO_QUERY);
206                 if(xProp.is())
207                 {
208                     Reference<XPropertySetInfo> xPropInfo(xProp->getPropertySetInfo());
209                     if(xPropInfo->hasPropertyByName(PROPERTY_DATABASENAME))
210                     {
211                         String sDatabaseName;
212                         sDatabaseName = String(::comphelper::getString(xProp->getPropertyValue(PROPERTY_DATABASENAME)));
213                         m_aDatasource.SelectEntryPos(m_aDatasource.InsertEntry( sDatabaseName ));
214 
215                     }
216                     if ( xPropInfo->hasPropertyByName(PROPERTY_CONTROLUSER) )
217                         m_pOutputSet->Put(SfxStringItem(DSID_CONN_CTRLUSER, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_CONTROLUSER))));
218                     if ( xPropInfo->hasPropertyByName(PROPERTY_CONTROLPASSWORD) )
219                         m_pOutputSet->Put(SfxStringItem(DSID_CONN_CTRLPWD, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_CONTROLPASSWORD))));
220                     if ( xPropInfo->hasPropertyByName(PROPERTY_USER) )
221                         m_pOutputSet->Put(SfxStringItem(DSID_USER, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_USER))));
222                     if ( xPropInfo->hasPropertyByName(PROPERTY_PASSWORD) )
223                     {
224                         m_pOutputSet->Put(SfxStringItem(DSID_PASSWORD, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_PASSWORD))));
225                         m_pOutputSet->Put(SfxBoolItem(DSID_PASSWORDREQUIRED, sal_True));
226                     }
227                     if ( xPropInfo->hasPropertyByName(PROPERTY_CACHESIZE) )
228                         m_pOutputSet->Put(SfxInt32Item(DSID_CONN_CACHESIZE, ::comphelper::getINT32(xProp->getPropertyValue(PROPERTY_CACHESIZE))));
229                 }
230             }
231         }
232     }
233     catch(Exception&)
234     {
235     }
236     return 0L;
237 }
238 
239 // -----------------------------------------------------------------------
240 sal_Bool ODatasourceSelectDialog::Close()
241 {
242 #ifdef HAVE_ODBC_ADMINISTRATION
243     if ( m_pODBCManagement.get() && m_pODBCManagement->isRunning() )
244         return sal_False;
245 #endif
246 
247     return ModalDialog::Close();
248 }
249 
250 // -----------------------------------------------------------------------
251 #ifdef HAVE_ODBC_ADMINISTRATION
252 IMPL_LINK( ODatasourceSelectDialog, ManageClickHdl, PushButton*, EMPTYARG )
253 {
254     if ( !m_pODBCManagement.get() )
255         m_pODBCManagement.reset( new OOdbcManagement( LINK( this, ODatasourceSelectDialog, ManageProcessFinished ) ) );
256 
257     if ( !m_pODBCManagement->manageDataSources_async() )
258     {
259         // TODO: error message
260         m_aDatasource.GrabFocus();
261         m_aManageDatasources.Disable();
262         return 1L;
263     }
264 
265     m_aDatasource.Disable();
266     m_aOk.Disable();
267     m_aCancel.Disable();
268     m_aManageDatasources.Disable();
269 
270     OSL_POSTCOND( m_pODBCManagement->isRunning(), "ODatasourceSelectDialog::ManageClickHdl: success, but not running - you were *fast*!" );
271     return 0L;
272 }
273 
274 IMPL_LINK( ODatasourceSelectDialog, ManageProcessFinished, void*, /**/ )
275 {
276     StringBag aOdbcDatasources;
277     OOdbcEnumeration aEnumeration;
278     aEnumeration.getDatasourceNames( aOdbcDatasources );
279     fillListBox( aOdbcDatasources );
280 
281     m_aDatasource.Enable();
282     m_aOk.Enable();
283     m_aCancel.Enable();
284     m_aManageDatasources.Enable();
285 
286     return 0L;
287 }
288 
289 #endif
290 // -----------------------------------------------------------------------------
291 void ODatasourceSelectDialog::fillListBox(const StringBag& _rDatasources)
292 {
293     ::rtl::OUString sSelected;
294     if (m_aDatasource.GetEntryCount())
295          sSelected = m_aDatasource.GetSelectEntry();
296     m_aDatasource.Clear();
297     // fill the list
298     for (   ConstStringBagIterator aDS = _rDatasources.begin();
299             aDS != _rDatasources.end();
300             ++aDS
301         )
302     {
303         m_aDatasource.InsertEntry( *aDS );
304     }
305 
306     if (m_aDatasource.GetEntryCount())
307     {
308         if (sSelected.getLength())
309             m_aDatasource.SelectEntry(sSelected);
310         else        // select the first entry
311             m_aDatasource.SelectEntryPos(0);
312     }
313 }
314 
315 //.........................................................................
316 }   // namespace dbaui
317 //.........................................................................
318 
319