Update ooo320-m1
[ooovba.git] / dbaccess / source / ui / dlg / dsselect.cxx
blob5d2ec36272170c6bfb59f8d43a3070c63e242f88
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dsselect.cxx,v $
10 * $Revision: 1.23.68.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _DBAUI_DSSELECT_HXX_
35 #include "dsselect.hxx"
36 #endif
37 #ifndef _DBAUI_DSSELECT_HRC_
38 #include "dsselect.hrc"
39 #endif
40 #ifndef _DBU_DLG_HRC_
41 #include "dbu_dlg.hrc"
42 #endif
43 #ifndef _SV_MSGBOX_HXX
44 #include <vcl/msgbox.hxx>
45 #endif
46 #ifndef _DBAUI_LOCALRESACCESS_HXX_
47 #include "localresaccess.hxx"
48 #endif
49 #ifndef _TOOLS_RCID_H
50 #include <tools/rcid.h>
51 #endif
53 #ifndef _COM_SUN_STAR_SDBCX_XCREATECATALOG_HPP_
54 #include <com/sun/star/sdbcx/XCreateCatalog.hpp>
55 #endif
56 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
57 #include <com/sun/star/beans/XPropertySet.hpp>
58 #endif
59 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSETINFO_HPP_
60 #include <com/sun/star/beans/XPropertySetInfo.hpp>
61 #endif
62 #ifndef _COM_SUN_STAR_UI_DIALOGS_XEXECUTABLEDIALOG_HPP_
63 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
64 #endif
65 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
66 #include <com/sun/star/awt/XWindow.hpp>
67 #endif
68 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
69 #include "dbustrings.hrc"
70 #endif
71 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
72 #include <toolkit/helper/vclunohelper.hxx>
73 #endif
74 #ifndef _COMPHELPER_EXTRACT_HXX_
75 #include <comphelper/extract.hxx>
76 #endif
77 #ifndef _COMPHELPER_TYPES_HXX_
78 #include <comphelper/types.hxx>
79 #endif
80 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
81 #include <comphelper/processfactory.hxx>
82 #endif
83 #ifndef _DBAUI_DATASOURCEITEMS_HXX_
84 #include "dsitems.hxx"
85 #endif
86 #ifndef _SFXSTRITEM_HXX
87 #include <svtools/stritem.hxx>
88 #endif
89 #ifndef _SFXINTITEM_HXX
90 #include <svtools/intitem.hxx>
91 #endif
92 #ifndef _SFXENUMITEM_HXX
93 #include <svtools/eitem.hxx>
94 #endif
95 #ifndef _SFXITEMSET_HXX
96 #include <svtools/itemset.hxx>
97 #endif
99 //.........................................................................
100 namespace dbaui
102 //.........................................................................
103 using namespace ::com::sun::star::uno;
104 using namespace ::com::sun::star::beans;
105 using namespace ::com::sun::star::sdbc;
106 using namespace ::com::sun::star::sdbcx;
107 using namespace ::com::sun::star::ui::dialogs;
108 using namespace ::comphelper;
109 //==================================================================
110 ODatasourceSelectDialog::ODatasourceSelectDialog(Window* _pParent, const StringBag& _rDatasources, bool _bAdabas,SfxItemSet* _pOutputSet)
111 :ModalDialog(_pParent, ModuleRes(DLG_DATASOURCE_SELECTION))
112 ,m_aDescription (this, ModuleRes(FT_DESCRIPTION))
113 ,m_aDatasource (this, ModuleRes(LB_DATASOURCE))
114 ,m_aOk (this, ModuleRes(PB_OK))
115 ,m_aCancel (this, ModuleRes(PB_CANCEL))
116 ,m_aHelp (this, ModuleRes(PB_HELP))
117 #ifdef HAVE_ODBC_ADMINISTRATION
118 ,m_aManageDatasources (this, ModuleRes(PB_MANAGE))
119 #endif
120 ,m_aCreateAdabasDB (this, ModuleRes(PB_CREATE))
121 ,m_pOutputSet(_pOutputSet)
123 if ( _bAdabas )
124 { // set a new title (indicating that we're browsing local data sources only)
125 SetText(ModuleRes(STR_LOCAL_DATASOURCES));
126 m_aDescription.SetText(ModuleRes(STR_DESCRIPTION2));
128 m_aCreateAdabasDB.Show();
129 m_aCreateAdabasDB.SetClickHdl(LINK(this,ODatasourceSelectDialog,CreateDBClickHdl));
131 // resize the dialog a little bit, 'cause Adabas data source names are usually somewhat shorter
132 // than ODBC ones are
134 // shrink the listbox
135 Size aOldSize = m_aDatasource.GetSizePixel();
136 Size aNewSize(3 * aOldSize.Width() / 4, aOldSize.Height());
137 m_aDatasource.SetSizePixel(aNewSize);
139 sal_Int32 nLostPixels = aOldSize.Width() - aNewSize.Width();
141 // shrink the fixed text
142 aOldSize = m_aDescription.GetSizePixel();
143 m_aDescription.SetSizePixel(Size(aOldSize.Width() - nLostPixels, aOldSize.Height()));
145 // move the buttons
146 PushButton* pButtons[] = { &m_aOk, &m_aCancel, &m_aHelp ,&m_aCreateAdabasDB};
147 for (size_t i=0; i<sizeof(pButtons)/sizeof(pButtons[0]); ++i)
149 Point aOldPos = pButtons[i]->GetPosPixel();
150 pButtons[i]->SetPosPixel(Point(aOldPos.X() - nLostPixels, aOldPos.Y()));
153 // resize the dialog itself
154 aOldSize = GetSizePixel();
155 SetSizePixel(Size(aOldSize.Width() - nLostPixels, aOldSize.Height()));
158 fillListBox(_rDatasources);
159 #ifdef HAVE_ODBC_ADMINISTRATION
160 // allow ODBC datasource managenment
161 if ( !_bAdabas )
163 m_aManageDatasources.Show();
164 m_aManageDatasources.Enable();
165 m_aManageDatasources.SetClickHdl(LINK(this,ODatasourceSelectDialog,ManageClickHdl));
167 #endif
168 m_aDatasource.SetDoubleClickHdl(LINK(this,ODatasourceSelectDialog,ListDblClickHdl));
169 FreeResource();
172 // -----------------------------------------------------------------------
173 ODatasourceSelectDialog::~ODatasourceSelectDialog()
177 // -----------------------------------------------------------------------
178 IMPL_LINK( ODatasourceSelectDialog, ListDblClickHdl, ListBox *, pListBox )
180 if (pListBox->GetSelectEntryCount())
181 EndDialog(RET_OK);
182 return 0;
184 // -----------------------------------------------------------------------
185 IMPL_LINK( ODatasourceSelectDialog, CreateDBClickHdl, PushButton*, /*pButton*/ )
189 OSL_ENSURE(m_pOutputSet,"No itemset given!");
190 Reference< ::com::sun::star::lang::XMultiServiceFactory > xORB = ::comphelper::getProcessServiceFactory();
191 Reference<XCreateCatalog> xCatalog(xORB->createInstance(SERVICE_EXTENDED_ADABAS_DRIVER),UNO_QUERY);
192 if ( xCatalog.is() && m_pOutputSet )
194 Sequence< Any > aArgs(2);
195 aArgs[0] <<= PropertyValue(::rtl::OUString::createFromAscii("CreateCatalog"), 0,makeAny(xCatalog) , PropertyState_DIRECT_VALUE);
196 aArgs[1] <<= PropertyValue(PROPERTY_PARENTWINDOW, 0, makeAny(VCLUnoHelper::GetInterface(this)), PropertyState_DIRECT_VALUE);
198 Reference< XExecutableDialog > xDialog(
199 xORB->createInstanceWithArguments(SERVICE_SDB_ADABASCREATIONDIALOG, aArgs), UNO_QUERY);
200 if (!xDialog.is())
202 // ShowServiceNotAvailableError(this, String(SERVICE_SDB_ADABASCREATIONDIALOG), sal_True);
203 return 0L;
206 if ( xDialog->execute() == RET_OK )
208 Reference<XPropertySet> xProp(xDialog,UNO_QUERY);
209 if(xProp.is())
211 Reference<XPropertySetInfo> xPropInfo(xProp->getPropertySetInfo());
212 if(xPropInfo->hasPropertyByName(PROPERTY_DATABASENAME))
214 String sDatabaseName;
215 sDatabaseName = String(::comphelper::getString(xProp->getPropertyValue(PROPERTY_DATABASENAME)));
216 m_aDatasource.SelectEntryPos(m_aDatasource.InsertEntry( sDatabaseName ));
219 if ( xPropInfo->hasPropertyByName(PROPERTY_CONTROLUSER) )
220 m_pOutputSet->Put(SfxStringItem(DSID_CONN_CTRLUSER, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_CONTROLUSER))));
221 if ( xPropInfo->hasPropertyByName(PROPERTY_CONTROLPASSWORD) )
222 m_pOutputSet->Put(SfxStringItem(DSID_CONN_CTRLPWD, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_CONTROLPASSWORD))));
223 if ( xPropInfo->hasPropertyByName(PROPERTY_USER) )
224 m_pOutputSet->Put(SfxStringItem(DSID_USER, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_USER))));
225 if ( xPropInfo->hasPropertyByName(PROPERTY_PASSWORD) )
227 m_pOutputSet->Put(SfxStringItem(DSID_PASSWORD, ::comphelper::getString(xProp->getPropertyValue(PROPERTY_PASSWORD))));
228 m_pOutputSet->Put(SfxBoolItem(DSID_PASSWORDREQUIRED, TRUE));
230 if ( xPropInfo->hasPropertyByName(PROPERTY_CACHESIZE) )
231 m_pOutputSet->Put(SfxInt32Item(DSID_CONN_CACHESIZE, ::comphelper::getINT32(xProp->getPropertyValue(PROPERTY_CACHESIZE))));
236 catch(Exception&)
239 return 0L;
242 // -----------------------------------------------------------------------
243 BOOL ODatasourceSelectDialog::Close()
245 #ifdef HAVE_ODBC_ADMINISTRATION
246 if ( m_pODBCManagement.get() && m_pODBCManagement->isRunning() )
247 return FALSE;
248 #endif
250 return ModalDialog::Close();
253 // -----------------------------------------------------------------------
254 #ifdef HAVE_ODBC_ADMINISTRATION
255 IMPL_LINK( ODatasourceSelectDialog, ManageClickHdl, PushButton*, EMPTYARG )
257 if ( !m_pODBCManagement.get() )
258 m_pODBCManagement.reset( new OOdbcManagement( LINK( this, ODatasourceSelectDialog, ManageProcessFinished ) ) );
260 if ( !m_pODBCManagement->manageDataSources_async() )
262 // TODO: error message
263 m_aDatasource.GrabFocus();
264 m_aManageDatasources.Disable();
265 return 1L;
268 m_aDatasource.Disable();
269 m_aOk.Disable();
270 m_aCancel.Disable();
271 m_aManageDatasources.Disable();
273 OSL_POSTCOND( m_pODBCManagement->isRunning(), "ODatasourceSelectDialog::ManageClickHdl: success, but not running - you were *fast*!" );
274 return 0L;
277 IMPL_LINK( ODatasourceSelectDialog, ManageProcessFinished, void*, /**/ )
279 StringBag aOdbcDatasources;
280 OOdbcEnumeration aEnumeration;
281 aEnumeration.getDatasourceNames( aOdbcDatasources );
282 fillListBox( aOdbcDatasources );
284 m_aDatasource.Enable();
285 m_aOk.Enable();
286 m_aCancel.Enable();
287 m_aManageDatasources.Enable();
289 return 0L;
292 #endif
293 // -----------------------------------------------------------------------------
294 void ODatasourceSelectDialog::fillListBox(const StringBag& _rDatasources)
296 ::rtl::OUString sSelected;
297 if (m_aDatasource.GetEntryCount())
298 sSelected = m_aDatasource.GetSelectEntry();
299 m_aDatasource.Clear();
300 // fill the list
301 for ( ConstStringBagIterator aDS = _rDatasources.begin();
302 aDS != _rDatasources.end();
303 ++aDS
306 m_aDatasource.InsertEntry( *aDS );
309 if (m_aDatasource.GetEntryCount())
311 if (sSelected.getLength())
312 m_aDatasource.SelectEntry(sSelected);
313 else // select the first entry
314 m_aDatasource.SelectEntryPos(0);
318 //.........................................................................
319 } // namespace dbaui
320 //.........................................................................