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