bump product version to 4.1.6.2
[LibreOffice.git] / dbaccess / source / ui / dlg / ConnectionPage.cxx
blob77504d9d3d03d79d9a83a9a8e473a160851dc5b1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "ConnectionPage.hxx"
22 #include "ConnectionPage.hrc"
23 #include "dbu_dlg.hrc"
24 #include "dsmeta.hxx"
25 #ifdef SOLAR_JAVA
26 #include <jvmaccess/virtualmachine.hxx>
27 #endif
28 #include <svl/itemset.hxx>
29 #include <unotools/pathoptions.hxx>
30 #include <svl/stritem.hxx>
31 #include <svl/eitem.hxx>
32 #include <svl/intitem.hxx>
33 #include <unotools/moduleoptions.hxx>
34 #include "dsitems.hxx"
35 #include "dbaccess_helpid.hrc"
36 #include "localresaccess.hxx"
37 #include <osl/process.h>
38 #include <vcl/msgbox.hxx>
39 #include "dbadmin.hxx"
40 #include <comphelper/types.hxx>
41 #include <vcl/stdtext.hxx>
42 #include "sqlmessage.hxx"
43 #include "odbcconfig.hxx"
44 #include "dsselect.hxx"
45 #include <svl/filenotation.hxx>
46 #include "dbustrings.hrc"
47 #include <com/sun/star/sdbc/XRow.hpp>
48 #include <com/sun/star/awt/XWindow.hpp>
49 #include <com/sun/star/task/XInteractionHandler.hpp>
50 #include <com/sun/star/ucb/XProgressHandler.hpp>
51 #include <com/sun/star/sdbc/XConnection.hpp>
52 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
53 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
54 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
55 #include "UITools.hxx"
56 #include <unotools/localfilehelper.hxx>
57 #include <unotools/ucbhelper.hxx>
58 #include <ucbhelper/commandenvironment.hxx>
59 #include "finteraction.hxx"
60 #include <connectivity/CommonTools.hxx>
61 #include <sfx2/docfilt.hxx>
62 #include "dsnItem.hxx"
63 #if defined(WNT)
64 #define _ADO_DATALINK_BROWSE_
65 #endif
67 #ifdef _ADO_DATALINK_BROWSE_
68 #include <vcl/sysdata.hxx>
69 #include "adodatalinks.hxx"
70 #endif //_ADO_DATALINK_BROWSE_
72 #include "AutoControls.hrc"
74 //.........................................................................
75 namespace dbaui
77 //.........................................................................
78 using namespace ::com::sun::star::uno;
79 using namespace ::com::sun::star::ucb;
80 using namespace ::com::sun::star::ui::dialogs;
81 using namespace ::com::sun::star::sdbc;
82 using namespace ::com::sun::star::beans;
83 using namespace ::com::sun::star::lang;
84 using namespace ::com::sun::star::container;
85 using namespace ::dbtools;
86 using namespace ::svt;
88 SfxTabPage* OConnectionTabPage::Create( Window* pParent, const SfxItemSet& _rAttrSet )
90 return ( new OConnectionTabPage( pParent, _rAttrSet ) );
92 //========================================================================
93 //= OConnectionTabPage
94 //========================================================================
95 DBG_NAME(OConnectionTabPage)
96 OConnectionTabPage::OConnectionTabPage(Window* pParent, const SfxItemSet& _rCoreAttrs)
97 :OConnectionHelper(pParent, ModuleRes(PAGE_CONNECTION), _rCoreAttrs)
98 ,m_aFL1(this, ModuleRes(FL_SEPARATOR1))
99 ,m_aFL2(this, ModuleRes(FL_SEPARATOR2))
100 ,m_aUserNameLabel(this, ModuleRes(FT_USERNAME))
101 ,m_aUserName(this, ModuleRes(ET_USERNAME))
102 ,m_aPasswordRequired(this, ModuleRes(CB_PASSWORD_REQUIRED))
103 ,m_aFL3(this, ModuleRes(FL_SEPARATOR3))
104 ,m_aJavaDriverLabel(this, ModuleRes(FT_JDBCDRIVERCLASS))
105 ,m_aJavaDriver(this, ModuleRes(ET_JDBCDRIVERCLASS))
106 ,m_aTestJavaDriver(this, ModuleRes(PB_TESTDRIVERCLASS))
107 ,m_aTestConnection(this, ModuleRes(PB_TESTCONNECTION))
109 DBG_CTOR(OConnectionTabPage,NULL);
110 m_aConnectionURL.SetModifyHdl(LINK(this, OConnectionTabPage, OnEditModified));
111 m_aJavaDriver.SetModifyHdl(getControlModifiedLink());
112 m_aJavaDriver.SetModifyHdl(LINK(this, OConnectionTabPage, OnEditModified));
113 m_aUserName.SetModifyHdl(getControlModifiedLink());
114 m_aPasswordRequired.SetClickHdl(getControlModifiedLink());
116 m_aTestConnection.SetClickHdl(LINK(this,OGenericAdministrationPage,OnTestConnectionClickHdl));
117 m_aTestJavaDriver.SetClickHdl(LINK(this,OConnectionTabPage,OnTestJavaClickHdl));
119 FreeResource();
121 LayoutHelper::fitSizeRightAligned( m_aTestConnection );
124 // -----------------------------------------------------------------------
125 OConnectionTabPage::~OConnectionTabPage()
127 DBG_DTOR(OConnectionTabPage,NULL);
130 // -----------------------------------------------------------------------
131 void OConnectionTabPage::implInitControls(const SfxItemSet& _rSet, sal_Bool _bSaveValue)
133 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
134 sal_Bool bValid, bReadonly;
135 getFlags(_rSet, bValid, bReadonly);
137 m_eType = m_pAdminDialog->getDatasourceType(_rSet);
138 OConnectionHelper::implInitControls( _rSet, _bSaveValue);
140 LocalResourceAccess aLocRes( PAGE_CONNECTION, RSC_TABPAGE );
141 ::dbaccess::DATASOURCE_TYPE eType = m_pCollection->determineType(m_eType);
142 switch( eType )
144 case ::dbaccess::DST_DBASE:
145 m_aFT_Connection.SetText(OUString(ModuleRes(STR_DBASE_PATH_OR_FILE)));
146 m_aConnectionURL.SetHelpId(HID_DSADMIN_DBASE_PATH);
147 break;
148 case ::dbaccess::DST_FLAT:
149 m_aFT_Connection.SetText(OUString(ModuleRes(STR_FLAT_PATH_OR_FILE)));
150 m_aConnectionURL.SetHelpId(HID_DSADMIN_FLAT_PATH);
151 break;
152 case ::dbaccess::DST_CALC:
153 m_aFT_Connection.SetText(OUString(ModuleRes(STR_CALC_PATH_OR_FILE)));
154 m_aConnectionURL.SetHelpId(HID_DSADMIN_CALC_PATH);
155 break;
156 case ::dbaccess::DST_ADO:
157 m_aFT_Connection.SetText(OUString(ModuleRes(STR_COMMONURL)));
158 break;
159 case ::dbaccess::DST_MSACCESS:
160 case ::dbaccess::DST_MSACCESS_2007:
161 m_aFT_Connection.SetText(OUString(ModuleRes(STR_MSACCESS_MDB_FILE)));
162 m_aConnectionURL.SetHelpId(HID_DSADMIN_MSACCESS_MDB_FILE);
163 break;
164 case ::dbaccess::DST_MYSQL_NATIVE:
165 case ::dbaccess::DST_MYSQL_JDBC:
166 m_aFT_Connection.SetText(OUString(ModuleRes(STR_MYSQL_DATABASE_NAME)));
167 m_aConnectionURL.SetHelpId( HID_DSADMIN_MYSQL_DATABASE );
168 break;
169 case ::dbaccess::DST_ORACLE_JDBC:
170 m_aFT_Connection.SetText(OUString(ModuleRes(STR_ORACLE_DATABASE_NAME)));
171 m_aConnectionURL.SetHelpId(HID_DSADMIN_ORACLE_DATABASE);
172 break;
173 case ::dbaccess::DST_MYSQL_ODBC:
174 case ::dbaccess::DST_ODBC:
175 m_aFT_Connection.SetText(OUString(ModuleRes(STR_NAME_OF_ODBC_DATASOURCE)));
176 m_aConnectionURL.SetHelpId( eType == ::dbaccess::DST_MYSQL_ODBC ? HID_DSADMIN_MYSQL_ODBC_DATASOURCE : HID_DSADMIN_ODBC_DATASOURCE);
177 break;
178 case ::dbaccess::DST_LDAP:
179 m_aFT_Connection.SetText(OUString(ModuleRes(STR_HOSTNAME)));
180 m_aConnectionURL.SetHelpId( HID_DSADMIN_LDAP_HOSTNAME );
181 break;
182 case ::dbaccess::DST_MOZILLA:
183 m_aFT_Connection.SetText(OUString(ModuleRes(STR_MOZILLA_PROFILE_NAME)));
184 m_aConnectionURL.SetHelpId( HID_DSADMIN_MOZILLA_PROFILE_NAME );
185 break;
186 case ::dbaccess::DST_THUNDERBIRD:
187 m_aFT_Connection.SetText(OUString(ModuleRes(STR_THUNDERBIRD_PROFILE_NAME)));
188 m_aConnectionURL.SetHelpId( HID_DSADMIN_THUNDERBIRD_PROFILE_NAME );
189 break;
190 case ::dbaccess::DST_OUTLOOK:
191 case ::dbaccess::DST_OUTLOOKEXP:
192 case ::dbaccess::DST_EVOLUTION:
193 case ::dbaccess::DST_EVOLUTION_GROUPWISE:
194 case ::dbaccess::DST_EVOLUTION_LDAP:
195 case ::dbaccess::DST_KAB:
196 case ::dbaccess::DST_MACAB:
197 m_aFT_Connection.SetText(OUString(ModuleRes(STR_NO_ADDITIONAL_SETTINGS)));
199 OUString sText = m_aFT_Connection.GetText();
200 sText = sText.replaceAll("%test",m_aTestConnection.GetText());
201 OUString sTemp;
202 sText = sText.replaceAll("~",sTemp);
203 m_aFT_Connection.SetText(sText);
205 m_aConnectionURL.Hide();
206 break;
207 case ::dbaccess::DST_JDBC:
208 default:
209 m_aFT_Connection.SetText(OUString(ModuleRes(STR_COMMONURL)));
210 break;
214 AuthenticationMode eAuthMode( DataSourceMetaData::getAuthentication( m_eType ) );
215 bool bShowUserAuthenfication = ( eAuthMode != AuthNone );
216 bool bShowUser = ( eAuthMode == AuthUserPwd );
218 m_aPB_Connection.SetHelpId(HID_DSADMIN_BROWSECONN);
219 m_aFL2.Show( bShowUserAuthenfication );
220 m_aUserNameLabel.Show( bShowUser && bShowUserAuthenfication );
221 m_aUserName.Show( bShowUser && bShowUserAuthenfication );
222 m_aPasswordRequired.Show( bShowUserAuthenfication );
223 if ( !bShowUser && bShowUserAuthenfication )
224 m_aPasswordRequired.SetPosPixel(m_aUserNameLabel.GetPosPixel());
226 // collect the items
227 SFX_ITEMSET_GET(_rSet, pUidItem, SfxStringItem, DSID_USER, sal_True);
229 SFX_ITEMSET_GET(_rSet, pJdbcDrvItem, SfxStringItem, DSID_JDBCDRIVERCLASS, sal_True);
230 SFX_ITEMSET_GET(_rSet, pUrlItem, SfxStringItem, DSID_CONNECTURL, sal_True);
231 SFX_ITEMSET_GET(_rSet, pAllowEmptyPwd, SfxBoolItem, DSID_PASSWORDREQUIRED, sal_True);
233 // forward the values to the controls
234 if ( bValid )
236 m_aUserName.SetText(pUidItem->GetValue());
237 m_aPasswordRequired.Check(pAllowEmptyPwd->GetValue());
239 OUString sUrl = pUrlItem->GetValue();
240 setURL( sUrl );
242 const sal_Bool bEnableJDBC = m_pCollection->determineType(m_eType) == ::dbaccess::DST_JDBC;
243 if ( !pJdbcDrvItem->GetValue().getLength() )
245 OUString sDefaultJdbcDriverName = m_pCollection->getJavaDriverClass(m_eType);
246 if ( !sDefaultJdbcDriverName.isEmpty() )
248 m_aJavaDriver.SetText(sDefaultJdbcDriverName);
249 m_aJavaDriver.SetModifyFlag();
252 else
253 m_aJavaDriver.SetText(pJdbcDrvItem->GetValue());
255 m_aJavaDriverLabel.Show(bEnableJDBC);
256 m_aJavaDriver.Show(bEnableJDBC);
257 m_aTestJavaDriver.Show(bEnableJDBC);
258 m_aTestJavaDriver.Enable( !m_aJavaDriver.GetText().isEmpty() );
259 m_aFL3.Show(bEnableJDBC);
261 checkTestConnection();
263 m_aUserName.ClearModifyFlag();
264 m_aConnectionURL.ClearModifyFlag();
265 m_aJavaDriver.ClearModifyFlag();
268 // -----------------------------------------------------------------------
269 void OConnectionTabPage::fillWindows(::std::vector< ISaveValueWrapper* >& _rControlList)
271 _rControlList.push_back(new ODisableWrapper<FixedLine>(&m_aFL1));
273 _rControlList.push_back(new ODisableWrapper<FixedLine>(&m_aFL2));
274 _rControlList.push_back(new ODisableWrapper<FixedText>(&m_aJavaDriverLabel));
275 _rControlList.push_back(new ODisableWrapper<PushButton>(&m_aTestJavaDriver));
277 _rControlList.push_back(new ODisableWrapper<FixedLine>(&m_aFL3));
278 _rControlList.push_back(new ODisableWrapper<FixedText>(&m_aUserNameLabel));
279 _rControlList.push_back(new ODisableWrapper<PushButton>(&m_aTestConnection));
280 OConnectionHelper::fillWindows(_rControlList);
283 // -----------------------------------------------------------------------
284 void OConnectionTabPage::fillControls(::std::vector< ISaveValueWrapper* >& _rControlList)
286 _rControlList.push_back(new OSaveValueWrapper<Edit>(&m_aJavaDriver));
287 _rControlList.push_back(new OSaveValueWrapper<Edit>(&m_aUserName));
288 _rControlList.push_back(new OSaveValueWrapper<CheckBox>(&m_aPasswordRequired));
289 OConnectionHelper::fillControls(_rControlList);
292 // -----------------------------------------------------------------------
293 sal_Bool OConnectionTabPage::FillItemSet(SfxItemSet& _rSet)
295 sal_Bool bChangedSomething = sal_False;
297 if (m_aUserName.GetText() != m_aUserName.GetSavedValue())
299 _rSet.Put(SfxStringItem(DSID_USER, m_aUserName.GetText()));
300 _rSet.Put(SfxStringItem(DSID_PASSWORD, OUString()));
301 bChangedSomething = sal_True;
304 fillBool(_rSet,&m_aPasswordRequired,DSID_PASSWORDREQUIRED,bChangedSomething);
306 if ( m_pCollection->determineType(m_eType) == ::dbaccess::DST_JDBC )
308 fillString(_rSet,&m_aJavaDriver, DSID_JDBCDRIVERCLASS, bChangedSomething);
311 fillString(_rSet,&m_aConnectionURL, DSID_CONNECTURL, bChangedSomething);
313 return bChangedSomething;
315 // -----------------------------------------------------------------------
316 IMPL_LINK(OConnectionTabPage, OnTestJavaClickHdl, PushButton*, /*_pButton*/)
318 OSL_ENSURE(m_pAdminDialog,"No Admin dialog set! ->GPF");
319 sal_Bool bSuccess = sal_False;
320 #ifdef SOLAR_JAVA
323 if ( !m_aJavaDriver.GetText().isEmpty() )
325 ::rtl::Reference< jvmaccess::VirtualMachine > xJVM = ::connectivity::getJavaVM( m_pAdminDialog->getORB() );
326 bSuccess = ::connectivity::existsJavaClassByName(xJVM,m_aJavaDriver.GetText());
329 catch(Exception&)
332 #endif
334 const sal_uInt16 nMessage = bSuccess ? STR_JDBCDRIVER_SUCCESS : STR_JDBCDRIVER_NO_SUCCESS;
335 const OSQLMessageBox::MessageType mt = bSuccess ? OSQLMessageBox::Info : OSQLMessageBox::Error;
336 OSQLMessageBox aMsg( this, OUString( ModuleRes( nMessage ) ), OUString(), WB_OK | WB_DEF_OK, mt );
337 aMsg.Execute();
338 return 0L;
340 // -----------------------------------------------------------------------
341 bool OConnectionTabPage::checkTestConnection()
343 OSL_ENSURE(m_pAdminDialog,"No Admin dialog set! ->GPF");
344 sal_Bool bEnableTestConnection = !m_aConnectionURL.IsVisible() || (m_aConnectionURL.GetTextNoPrefix().Len() != 0);
345 if ( m_pCollection->determineType(m_eType) == ::dbaccess::DST_JDBC )
346 bEnableTestConnection = bEnableTestConnection && (!m_aJavaDriver.GetText().isEmpty());
347 m_aTestConnection.Enable(bEnableTestConnection);
348 return true;
350 // -----------------------------------------------------------------------
351 IMPL_LINK(OConnectionTabPage, OnEditModified, Edit*, _pEdit)
353 if ( _pEdit == &m_aJavaDriver )
354 m_aTestJavaDriver.Enable( !m_aJavaDriver.GetText().isEmpty() );
356 checkTestConnection();
357 // tell the listener we were modified
358 callModifiedHdl();
359 return 0L;
361 //.........................................................................
362 } // namespace dbaui
363 //.........................................................................
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */