1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <config_features.h>
21 #include "ConnectionPage.hxx"
22 #include "dbu_dlg.hrc"
25 #include <jvmaccess/virtualmachine.hxx>
27 #include <svl/itemset.hxx>
28 #include <unotools/pathoptions.hxx>
29 #include <svl/stritem.hxx>
30 #include <svl/eitem.hxx>
31 #include <svl/intitem.hxx>
32 #include <unotools/moduleoptions.hxx>
33 #include "dsitems.hxx"
34 #include "dbaccess_helpid.hrc"
35 #include "localresaccess.hxx"
36 #include <osl/process.h>
37 #include <vcl/msgbox.hxx>
38 #include "dbadmin.hxx"
39 #include <comphelper/types.hxx>
40 #include <vcl/stdtext.hxx>
41 #include "sqlmessage.hxx"
42 #include "odbcconfig.hxx"
43 #include "dsselect.hxx"
44 #include <svl/filenotation.hxx>
45 #include "dbustrings.hrc"
46 #include <com/sun/star/sdbc/XRow.hpp>
47 #include <com/sun/star/awt/XWindow.hpp>
48 #include <com/sun/star/task/XInteractionHandler.hpp>
49 #include <com/sun/star/ucb/XProgressHandler.hpp>
50 #include <com/sun/star/sdbc/XConnection.hpp>
51 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
52 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
53 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
54 #include "UITools.hxx"
55 #include <unotools/localfilehelper.hxx>
56 #include <unotools/ucbhelper.hxx>
57 #include <ucbhelper/commandenvironment.hxx>
58 #include "finteraction.hxx"
59 #include <connectivity/CommonTools.hxx>
60 #include <sfx2/docfilt.hxx>
61 #include "dsnItem.hxx"
63 #define _ADO_DATALINK_BROWSE_
66 #ifdef _ADO_DATALINK_BROWSE_
67 #include <vcl/sysdata.hxx>
68 #include "adodatalinks.hxx"
69 #endif //_ADO_DATALINK_BROWSE_
74 using namespace ::com::sun::star::uno
;
75 using namespace ::com::sun::star::ucb
;
76 using namespace ::com::sun::star::ui::dialogs
;
77 using namespace ::com::sun::star::sdbc
;
78 using namespace ::com::sun::star::beans
;
79 using namespace ::com::sun::star::lang
;
80 using namespace ::com::sun::star::container
;
81 using namespace ::dbtools
;
82 using namespace ::svt
;
84 VclPtr
<SfxTabPage
> OConnectionTabPage::Create( vcl::Window
* pParent
, const SfxItemSet
* _rAttrSet
)
86 return VclPtr
<OConnectionTabPage
>::Create( pParent
, *_rAttrSet
);
90 OConnectionTabPage::OConnectionTabPage(vcl::Window
* pParent
, const SfxItemSet
& _rCoreAttrs
)
91 :OConnectionHelper(pParent
, "ConnectionPage", "dbaccess/ui/connectionpage.ui", _rCoreAttrs
)
93 get(m_pFL2
, "userlabel");
94 get(m_pUserNameLabel
, "userNameLabel");
95 get(m_pUserName
, "userNameEntry");
96 get(m_pPasswordRequired
, "passCheckbutton");
97 get(m_pFL3
, "JDBCLabel");
98 get(m_pJavaDriverLabel
, "javaDriverLabel");
99 get(m_pJavaDriver
, "driverEntry");
100 get(m_pTestJavaDriver
, "driverButton");
101 get(m_pTestConnection
, "connectionButton");
103 m_pConnectionURL
->SetModifyHdl(LINK(this, OConnectionTabPage
, OnEditModified
));
104 m_pJavaDriver
->SetModifyHdl(getControlModifiedLink());
105 m_pJavaDriver
->SetModifyHdl(LINK(this, OConnectionTabPage
, OnEditModified
));
106 m_pUserName
->SetModifyHdl(getControlModifiedLink());
107 m_pPasswordRequired
->SetClickHdl(getControlModifiedLink());
109 m_pTestConnection
->SetClickHdl(LINK(this,OGenericAdministrationPage
,OnTestConnectionClickHdl
));
110 m_pTestJavaDriver
->SetClickHdl(LINK(this,OConnectionTabPage
,OnTestJavaClickHdl
));
113 OConnectionTabPage::~OConnectionTabPage()
118 void OConnectionTabPage::dispose()
121 m_pUserNameLabel
.clear();
123 m_pPasswordRequired
.clear();
125 m_pJavaDriverLabel
.clear();
126 m_pJavaDriver
.clear();
127 m_pTestJavaDriver
.clear();
128 m_pTestConnection
.clear();
129 OConnectionHelper::dispose();
132 void OConnectionTabPage::implInitControls(const SfxItemSet
& _rSet
, bool _bSaveValue
)
134 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
135 bool bValid
, bReadonly
;
136 getFlags(_rSet
, bValid
, bReadonly
);
138 m_eType
= m_pAdminDialog
->getDatasourceType(_rSet
);
139 OConnectionHelper::implInitControls( _rSet
, _bSaveValue
);
141 ::dbaccess::DATASOURCE_TYPE eType
= m_pCollection
->determineType(m_eType
);
144 case ::dbaccess::DST_DBASE
:
145 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_DBASE_PATH_OR_FILE
)));
146 m_pConnectionURL
->SetHelpId(HID_DSADMIN_DBASE_PATH
);
148 case ::dbaccess::DST_FLAT
:
149 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_FLAT_PATH_OR_FILE
)));
150 m_pConnectionURL
->SetHelpId(HID_DSADMIN_FLAT_PATH
);
152 case ::dbaccess::DST_CALC
:
153 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_CALC_PATH_OR_FILE
)));
154 m_pConnectionURL
->SetHelpId(HID_DSADMIN_CALC_PATH
);
156 case ::dbaccess::DST_ADO
:
157 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_COMMONURL
)));
159 case ::dbaccess::DST_MSACCESS
:
160 case ::dbaccess::DST_MSACCESS_2007
:
161 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_MSACCESS_MDB_FILE
)));
162 m_pConnectionURL
->SetHelpId(HID_DSADMIN_MSACCESS_MDB_FILE
);
164 case ::dbaccess::DST_MYSQL_NATIVE
:
165 case ::dbaccess::DST_MYSQL_JDBC
:
166 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_MYSQL_DATABASE_NAME
)));
167 m_pConnectionURL
->SetHelpId( HID_DSADMIN_MYSQL_DATABASE
);
169 case ::dbaccess::DST_ORACLE_JDBC
:
170 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_ORACLE_DATABASE_NAME
)));
171 m_pConnectionURL
->SetHelpId(HID_DSADMIN_ORACLE_DATABASE
);
173 case ::dbaccess::DST_MYSQL_ODBC
:
174 case ::dbaccess::DST_ODBC
:
175 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_NAME_OF_ODBC_DATASOURCE
)));
176 m_pConnectionURL
->SetHelpId( eType
== ::dbaccess::DST_MYSQL_ODBC
? HID_DSADMIN_MYSQL_ODBC_DATASOURCE
: HID_DSADMIN_ODBC_DATASOURCE
);
178 case ::dbaccess::DST_LDAP
:
179 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_HOSTNAME
)));
180 m_pConnectionURL
->SetHelpId( HID_DSADMIN_LDAP_HOSTNAME
);
182 case ::dbaccess::DST_MOZILLA
:
183 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_MOZILLA_PROFILE_NAME
)));
184 m_pConnectionURL
->SetHelpId( HID_DSADMIN_MOZILLA_PROFILE_NAME
);
186 case ::dbaccess::DST_THUNDERBIRD
:
187 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_THUNDERBIRD_PROFILE_NAME
)));
188 m_pConnectionURL
->SetHelpId( HID_DSADMIN_THUNDERBIRD_PROFILE_NAME
);
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_pFT_Connection
->SetText(OUString(ModuleRes(STR_NO_ADDITIONAL_SETTINGS
)));
199 OUString sText
= m_pFT_Connection
->GetText();
200 sText
= sText
.replaceAll("%test",m_pTestConnection
->GetText());
202 sText
= sText
.replaceAll("~",sTemp
);
203 m_pFT_Connection
->SetText(sText
);
205 m_pConnectionURL
->Hide();
207 case ::dbaccess::DST_JDBC
:
209 m_pFT_Connection
->SetText(OUString(ModuleRes(STR_COMMONURL
)));
214 AuthenticationMode
eAuthMode( DataSourceMetaData::getAuthentication( m_eType
) );
215 bool bShowUserAuthenfication
= ( eAuthMode
!= AuthNone
);
216 bool bShowUser
= ( eAuthMode
== AuthUserPwd
);
218 m_pPB_Connection
->SetHelpId(HID_DSADMIN_BROWSECONN
);
219 m_pFL2
->Show( bShowUserAuthenfication
);
220 m_pUserNameLabel
->Show( bShowUser
&& bShowUserAuthenfication
);
221 m_pUserName
->Show( bShowUser
&& bShowUserAuthenfication
);
222 m_pPasswordRequired
->Show( bShowUserAuthenfication
);
223 if ( !bShowUser
&& bShowUserAuthenfication
)
224 m_pPasswordRequired
->SetPosPixel(m_pUserNameLabel
->GetPosPixel());
227 SFX_ITEMSET_GET(_rSet
, pUidItem
, SfxStringItem
, DSID_USER
, true);
229 SFX_ITEMSET_GET(_rSet
, pJdbcDrvItem
, SfxStringItem
, DSID_JDBCDRIVERCLASS
, true);
230 SFX_ITEMSET_GET(_rSet
, pUrlItem
, SfxStringItem
, DSID_CONNECTURL
, true);
231 SFX_ITEMSET_GET(_rSet
, pAllowEmptyPwd
, SfxBoolItem
, DSID_PASSWORDREQUIRED
, true);
233 // forward the values to the controls
236 m_pUserName
->SetText(pUidItem
->GetValue());
237 m_pPasswordRequired
->Check(pAllowEmptyPwd
->GetValue());
239 OUString sUrl
= pUrlItem
->GetValue();
242 const 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_pJavaDriver
->SetText(sDefaultJdbcDriverName
);
249 m_pJavaDriver
->SetModifyFlag();
253 m_pJavaDriver
->SetText(pJdbcDrvItem
->GetValue());
255 m_pJavaDriverLabel
->Show(bEnableJDBC
);
256 m_pJavaDriver
->Show(bEnableJDBC
);
257 m_pTestJavaDriver
->Show(bEnableJDBC
);
258 m_pTestJavaDriver
->Enable( !m_pJavaDriver
->GetText().trim().isEmpty() );
259 m_pFL3
->Show(bEnableJDBC
);
261 checkTestConnection();
263 m_pUserName
->ClearModifyFlag();
264 m_pConnectionURL
->ClearModifyFlag();
265 m_pJavaDriver
->ClearModifyFlag();
269 bool OConnectionTabPage::FillItemSet(SfxItemSet
* _rSet
)
271 bool bChangedSomething
= false;
273 if (m_pUserName
->IsValueChangedFromSaved())
275 _rSet
->Put(SfxStringItem(DSID_USER
, m_pUserName
->GetText()));
276 _rSet
->Put(SfxStringItem(DSID_PASSWORD
, OUString()));
277 bChangedSomething
= true;
280 fillBool(*_rSet
,m_pPasswordRequired
,DSID_PASSWORDREQUIRED
,bChangedSomething
);
282 if ( m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
)
284 fillString(*_rSet
,m_pJavaDriver
, DSID_JDBCDRIVERCLASS
, bChangedSomething
);
287 fillString(*_rSet
,m_pConnectionURL
, DSID_CONNECTURL
, bChangedSomething
);
289 return bChangedSomething
;
291 IMPL_LINK(OConnectionTabPage
, OnTestJavaClickHdl
, PushButton
*, /*_pButton*/)
293 OSL_ENSURE(m_pAdminDialog
,"No Admin dialog set! ->GPF");
294 bool bSuccess
= false;
295 #if HAVE_FEATURE_JAVA
298 if ( !m_pJavaDriver
->GetText().trim().isEmpty() )
300 ::rtl::Reference
< jvmaccess::VirtualMachine
> xJVM
= ::connectivity::getJavaVM( m_pAdminDialog
->getORB() );
301 m_pJavaDriver
->SetText(m_pJavaDriver
->GetText().trim()); // fdo#68341
302 bSuccess
= ::connectivity::existsJavaClassByName(xJVM
,m_pJavaDriver
->GetText().trim());
310 const sal_uInt16 nMessage
= bSuccess
? STR_JDBCDRIVER_SUCCESS
: STR_JDBCDRIVER_NO_SUCCESS
;
311 const OSQLMessageBox::MessageType mt
= bSuccess
? OSQLMessageBox::Info
: OSQLMessageBox::Error
;
312 ScopedVclPtrInstance
< OSQLMessageBox
> aMsg( this, OUString( ModuleRes( nMessage
) ), OUString(), WB_OK
| WB_DEF_OK
, mt
);
316 bool OConnectionTabPage::checkTestConnection()
318 OSL_ENSURE(m_pAdminDialog
,"No Admin dialog set! ->GPF");
319 bool bEnableTestConnection
= !m_pConnectionURL
->IsVisible() || !m_pConnectionURL
->GetTextNoPrefix().isEmpty();
320 if ( m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
)
321 bEnableTestConnection
= bEnableTestConnection
&& (!m_pJavaDriver
->GetText().trim().isEmpty());
322 m_pTestConnection
->Enable(bEnableTestConnection
);
325 IMPL_LINK(OConnectionTabPage
, OnEditModified
, Edit
*, _pEdit
)
327 if ( _pEdit
== m_pJavaDriver
)
328 m_pTestJavaDriver
->Enable( !m_pJavaDriver
->GetText().trim().isEmpty() );
330 checkTestConnection();
331 // tell the listener we were modified
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */