1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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"
37 #ifndef _DBAUI_DSSELECT_HRC_
38 #include "dsselect.hrc"
41 #include "dbu_dlg.hrc"
43 #ifndef _SV_MSGBOX_HXX
44 #include <vcl/msgbox.hxx>
46 #ifndef _DBAUI_LOCALRESACCESS_HXX_
47 #include "localresaccess.hxx"
50 #include <tools/rcid.h>
53 #ifndef _COM_SUN_STAR_SDBCX_XCREATECATALOG_HPP_
54 #include <com/sun/star/sdbcx/XCreateCatalog.hpp>
56 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
57 #include <com/sun/star/beans/XPropertySet.hpp>
59 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSETINFO_HPP_
60 #include <com/sun/star/beans/XPropertySetInfo.hpp>
62 #ifndef _COM_SUN_STAR_UI_DIALOGS_XEXECUTABLEDIALOG_HPP_
63 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
65 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
66 #include <com/sun/star/awt/XWindow.hpp>
68 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
69 #include "dbustrings.hrc"
71 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
72 #include <toolkit/helper/vclunohelper.hxx>
74 #ifndef _COMPHELPER_EXTRACT_HXX_
75 #include <comphelper/extract.hxx>
77 #ifndef _COMPHELPER_TYPES_HXX_
78 #include <comphelper/types.hxx>
80 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
81 #include <comphelper/processfactory.hxx>
83 #ifndef _DBAUI_DATASOURCEITEMS_HXX_
84 #include "dsitems.hxx"
86 #ifndef _SFXSTRITEM_HXX
87 #include <svtools/stritem.hxx>
89 #ifndef _SFXINTITEM_HXX
90 #include <svtools/intitem.hxx>
92 #ifndef _SFXENUMITEM_HXX
93 #include <svtools/eitem.hxx>
95 #ifndef _SFXITEMSET_HXX
96 #include <svtools/itemset.hxx>
99 //.........................................................................
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
))
120 ,m_aCreateAdabasDB (this, ModuleRes(PB_CREATE
))
121 ,m_pOutputSet(_pOutputSet
)
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()));
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
163 m_aManageDatasources
.Show();
164 m_aManageDatasources
.Enable();
165 m_aManageDatasources
.SetClickHdl(LINK(this,ODatasourceSelectDialog
,ManageClickHdl
));
168 m_aDatasource
.SetDoubleClickHdl(LINK(this,ODatasourceSelectDialog
,ListDblClickHdl
));
172 // -----------------------------------------------------------------------
173 ODatasourceSelectDialog::~ODatasourceSelectDialog()
177 // -----------------------------------------------------------------------
178 IMPL_LINK( ODatasourceSelectDialog
, ListDblClickHdl
, ListBox
*, pListBox
)
180 if (pListBox
->GetSelectEntryCount())
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
);
202 // ShowServiceNotAvailableError(this, String(SERVICE_SDB_ADABASCREATIONDIALOG), sal_True);
206 if ( xDialog
->execute() == RET_OK
)
208 Reference
<XPropertySet
> xProp(xDialog
,UNO_QUERY
);
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
))));
242 // -----------------------------------------------------------------------
243 BOOL
ODatasourceSelectDialog::Close()
245 #ifdef HAVE_ODBC_ADMINISTRATION
246 if ( m_pODBCManagement
.get() && m_pODBCManagement
->isRunning() )
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();
268 m_aDatasource
.Disable();
271 m_aManageDatasources
.Disable();
273 OSL_POSTCOND( m_pODBCManagement
->isRunning(), "ODatasourceSelectDialog::ManageClickHdl: success, but not running - you were *fast*!" );
277 IMPL_LINK( ODatasourceSelectDialog
, ManageProcessFinished
, void*, /**/ )
279 StringBag aOdbcDatasources
;
280 OOdbcEnumeration aEnumeration
;
281 aEnumeration
.getDatasourceNames( aOdbcDatasources
);
282 fillListBox( aOdbcDatasources
);
284 m_aDatasource
.Enable();
287 m_aManageDatasources
.Enable();
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();
301 for ( ConstStringBagIterator aDS
= _rDatasources
.begin();
302 aDS
!= _rDatasources
.end();
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 //.........................................................................
320 //.........................................................................