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_java.h>
21 #include "ConnectionPage.hxx"
22 #include <core_resource.hxx>
23 #include <IItemSetHelper.hxx>
24 #include <strings.hrc>
27 #include <jvmaccess/virtualmachine.hxx>
29 #include <svl/itemset.hxx>
30 #include <svl/stritem.hxx>
31 #include <svl/eitem.hxx>
32 #include <dsitems.hxx>
34 #include <sqlmessage.hxx>
35 #include <svl/filenotation.hxx>
36 #include <com/sun/star/ucb/XProgressHandler.hpp>
37 #include <connectivity/CommonTools.hxx>
38 #include <o3tl/string_view.hxx>
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::ucb
;
44 using namespace ::com::sun::star::ui::dialogs
;
45 using namespace ::com::sun::star::sdbc
;
46 using namespace ::com::sun::star::beans
;
47 using namespace ::com::sun::star::lang
;
48 using namespace ::com::sun::star::container
;
49 using namespace ::dbtools
;
50 using namespace ::svt
;
52 std::unique_ptr
<SfxTabPage
> OConnectionTabPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
* _rAttrSet
)
54 return std::make_unique
<OConnectionTabPage
>(pPage
, pController
, *_rAttrSet
);
58 OConnectionTabPage::OConnectionTabPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
& _rCoreAttrs
)
59 : OConnectionHelper(pPage
, pController
, "dbaccess/ui/connectionpage.ui", "ConnectionPage", _rCoreAttrs
)
60 , m_xFL2(m_xBuilder
->weld_label("userlabel"))
61 , m_xUserNameLabel(m_xBuilder
->weld_label("userNameLabel"))
62 , m_xUserName(m_xBuilder
->weld_entry("userNameEntry"))
63 , m_xPasswordRequired(m_xBuilder
->weld_check_button("passCheckbutton"))
64 , m_xFL3(m_xBuilder
->weld_label("JDBCLabel"))
65 , m_xJavaDriverLabel(m_xBuilder
->weld_label("javaDriverLabel"))
66 , m_xJavaDriver(m_xBuilder
->weld_entry("driverEntry"))
67 , m_xTestJavaDriver(m_xBuilder
->weld_button("driverButton"))
68 , m_xTestConnection(m_xBuilder
->weld_button("connectionButton"))
70 m_xConnectionURL
->connect_changed(LINK(this, OConnectionTabPage
, OnEditModified
));
71 m_xJavaDriver
->connect_changed(LINK(this, OConnectionTabPage
, OnEditModified
));
72 m_xUserName
->connect_changed(LINK(this, OGenericAdministrationPage
, OnControlEntryModifyHdl
));
73 m_xPasswordRequired
->connect_toggled(LINK(this, OGenericAdministrationPage
, OnControlModifiedButtonClick
));
75 m_xTestConnection
->connect_clicked(LINK(this,OGenericAdministrationPage
,OnTestConnectionButtonClickHdl
));
76 m_xTestJavaDriver
->connect_clicked(LINK(this,OConnectionTabPage
,OnTestJavaClickHdl
));
79 OConnectionTabPage::~OConnectionTabPage()
83 void OConnectionTabPage::implInitControls(const SfxItemSet
& _rSet
, bool _bSaveValue
)
85 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
86 bool bValid
, bReadonly
;
87 getFlags(_rSet
, bValid
, bReadonly
);
89 m_eType
= m_pAdminDialog
->getDatasourceType(_rSet
);
90 OConnectionHelper::implInitControls( _rSet
, _bSaveValue
);
92 ::dbaccess::DATASOURCE_TYPE eType
= m_pCollection
->determineType(m_eType
);
95 case ::dbaccess::DST_DBASE
:
96 m_xFT_Connection
->set_label(DBA_RES(STR_DBASE_PATH_OR_FILE
));
97 m_xConnectionURL
->set_help_id(HID_DSADMIN_DBASE_PATH
);
99 case ::dbaccess::DST_FLAT
:
100 m_xFT_Connection
->set_label(DBA_RES(STR_FLAT_PATH_OR_FILE
));
101 m_xConnectionURL
->set_help_id(HID_DSADMIN_FLAT_PATH
);
103 case ::dbaccess::DST_CALC
:
104 m_xFT_Connection
->set_label(DBA_RES(STR_CALC_PATH_OR_FILE
));
105 m_xConnectionURL
->set_help_id(HID_DSADMIN_CALC_PATH
);
107 case ::dbaccess::DST_WRITER
:
108 m_xFT_Connection
->set_label(DBA_RES(STR_WRITER_PATH_OR_FILE
));
109 m_xConnectionURL
->set_help_id(HID_DSADMIN_WRITER_PATH
);
111 case ::dbaccess::DST_ADO
:
112 m_xFT_Connection
->set_label(DBA_RES(STR_COMMONURL
));
114 case ::dbaccess::DST_MSACCESS
:
115 case ::dbaccess::DST_MSACCESS_2007
:
116 m_xFT_Connection
->set_label(DBA_RES(STR_MSACCESS_MDB_FILE
));
117 m_xConnectionURL
->set_help_id(HID_DSADMIN_MSACCESS_MDB_FILE
);
119 case ::dbaccess::DST_MYSQL_NATIVE
:
120 case ::dbaccess::DST_MYSQL_JDBC
:
121 m_xFT_Connection
->set_label(DBA_RES(STR_MYSQL_DATABASE_NAME
));
122 m_xConnectionURL
->set_help_id( HID_DSADMIN_MYSQL_DATABASE
);
124 case ::dbaccess::DST_ORACLE_JDBC
:
125 m_xFT_Connection
->set_label(DBA_RES(STR_ORACLE_DATABASE_NAME
));
126 m_xConnectionURL
->set_help_id(HID_DSADMIN_ORACLE_DATABASE
);
128 case ::dbaccess::DST_MYSQL_ODBC
:
129 case ::dbaccess::DST_ODBC
:
130 m_xFT_Connection
->set_label(DBA_RES(STR_NAME_OF_ODBC_DATASOURCE
));
131 m_xConnectionURL
->set_help_id( eType
== ::dbaccess::DST_MYSQL_ODBC
? OUString(HID_DSADMIN_MYSQL_ODBC_DATASOURCE
) : OUString(HID_DSADMIN_ODBC_DATASOURCE
));
133 case ::dbaccess::DST_LDAP
:
134 m_xFT_Connection
->set_label(DBA_RES(STR_HOSTNAME
));
135 m_xConnectionURL
->set_help_id( HID_DSADMIN_LDAP_HOSTNAME
);
137 case ::dbaccess::DST_MOZILLA
:
138 m_xFT_Connection
->set_label(DBA_RES(STR_MOZILLA_PROFILE_NAME
));
139 m_xConnectionURL
->set_help_id( HID_DSADMIN_MOZILLA_PROFILE_NAME
);
141 case ::dbaccess::DST_THUNDERBIRD
:
142 m_xFT_Connection
->set_label(DBA_RES(STR_THUNDERBIRD_PROFILE_NAME
));
143 m_xConnectionURL
->set_help_id( HID_DSADMIN_THUNDERBIRD_PROFILE_NAME
);
145 case ::dbaccess::DST_OUTLOOK
:
146 case ::dbaccess::DST_OUTLOOKEXP
:
147 case ::dbaccess::DST_EVOLUTION
:
148 case ::dbaccess::DST_EVOLUTION_GROUPWISE
:
149 case ::dbaccess::DST_EVOLUTION_LDAP
:
150 case ::dbaccess::DST_KAB
:
151 case ::dbaccess::DST_MACAB
:
152 m_xFT_Connection
->set_label(DBA_RES(STR_NO_ADDITIONAL_SETTINGS
));
154 OUString sText
= m_xFT_Connection
->get_label();
155 sText
= sText
.replaceAll("%test",m_xTestConnection
->get_label());
156 sText
= sText
.replaceAll("~","");
157 m_xFT_Connection
->set_label(sText
);
159 m_xConnectionURL
->hide();
161 case ::dbaccess::DST_JDBC
:
163 m_xFT_Connection
->set_label(DBA_RES(STR_COMMONURL
));
167 AuthenticationMode
eAuthMode( DataSourceMetaData::getAuthentication( m_eType
) );
168 bool bShowUserAuthenfication
= ( eAuthMode
!= AuthNone
);
169 bool bShowUser
= ( eAuthMode
== AuthUserPwd
);
171 m_xPB_Connection
->set_help_id(HID_DSADMIN_BROWSECONN
);
172 m_xFL2
->set_visible( bShowUserAuthenfication
);
173 m_xUserNameLabel
->set_visible( bShowUser
&& bShowUserAuthenfication
);
174 m_xUserName
->set_visible( bShowUser
&& bShowUserAuthenfication
);
175 m_xPasswordRequired
->set_visible( bShowUserAuthenfication
);
178 const SfxStringItem
* pUidItem
= _rSet
.GetItem
<SfxStringItem
>(DSID_USER
);
180 const SfxStringItem
* pJdbcDrvItem
= _rSet
.GetItem
<SfxStringItem
>(DSID_JDBCDRIVERCLASS
);
181 const SfxStringItem
* pUrlItem
= _rSet
.GetItem
<SfxStringItem
>(DSID_CONNECTURL
);
182 const SfxBoolItem
* pAllowEmptyPwd
= _rSet
.GetItem
<SfxBoolItem
>(DSID_PASSWORDREQUIRED
);
184 // forward the values to the controls
188 m_xUserName
->set_text(pUidItem
->GetValue());
189 m_xPasswordRequired
->set_active(pAllowEmptyPwd
->GetValue());
191 const OUString
& sUrl
= pUrlItem
->GetValue();
194 const bool bEnableJDBC
= m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
;
195 if ( !pJdbcDrvItem
->GetValue().getLength() )
197 OUString sDefaultJdbcDriverName
= m_pCollection
->getJavaDriverClass(m_eType
);
198 if ( !sDefaultJdbcDriverName
.isEmpty() )
199 m_xJavaDriver
->set_text(sDefaultJdbcDriverName
);
202 m_xJavaDriver
->set_text(pJdbcDrvItem
->GetValue());
204 m_xJavaDriverLabel
->set_visible(bEnableJDBC
);
205 m_xJavaDriver
->set_visible(bEnableJDBC
);
206 m_xTestJavaDriver
->set_visible(bEnableJDBC
);
207 m_xTestJavaDriver
->set_sensitive( !o3tl::trim(m_xJavaDriver
->get_text()).empty() );
208 m_xFL3
->set_visible(bEnableJDBC
);
210 checkTestConnection();
212 m_xUserName
->save_value();
213 m_xConnectionURL
->save_value();
214 m_xJavaDriver
->save_value();
215 m_xPasswordRequired
->save_state();
218 bool OConnectionTabPage::FillItemSet(SfxItemSet
* _rSet
)
220 bool bChangedSomething
= false;
222 if (m_xUserName
->get_value_changed_from_saved())
224 _rSet
->Put(SfxStringItem(DSID_USER
, m_xUserName
->get_text()));
225 _rSet
->Put(SfxStringItem(DSID_PASSWORD
, OUString()));
226 bChangedSomething
= true;
229 fillBool(*_rSet
,m_xPasswordRequired
.get(),DSID_PASSWORDREQUIRED
,false, bChangedSomething
);
231 if ( m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
)
233 fillString(*_rSet
,m_xJavaDriver
.get(), DSID_JDBCDRIVERCLASS
, bChangedSomething
);
236 fillString(*_rSet
,m_xConnectionURL
.get(), DSID_CONNECTURL
, bChangedSomething
);
238 return bChangedSomething
;
240 IMPL_LINK_NOARG(OConnectionTabPage
, OnTestJavaClickHdl
, weld::Button
&, void)
242 OSL_ENSURE(m_pAdminDialog
,"No Admin dialog set! ->GPF");
243 bool bSuccess
= false;
244 #if HAVE_FEATURE_JAVA
247 if ( !o3tl::trim(m_xJavaDriver
->get_text()).empty() )
249 ::rtl::Reference
< jvmaccess::VirtualMachine
> xJVM
= ::connectivity::getJavaVM( m_pAdminDialog
->getORB() );
250 m_xJavaDriver
->set_text(m_xJavaDriver
->get_text().trim()); // fdo#68341
251 bSuccess
= ::connectivity::existsJavaClassByName(xJVM
, o3tl::trim(m_xJavaDriver
->get_text()));
259 TranslateId pMessage
= bSuccess
? STR_JDBCDRIVER_SUCCESS
: STR_JDBCDRIVER_NO_SUCCESS
;
260 const MessageType mt
= bSuccess
? MessageType::Info
: MessageType::Error
;
261 OSQLMessageBox
aMsg(GetFrameWeld(), DBA_RES(pMessage
), OUString(), MessBoxStyle::Ok
| MessBoxStyle::DefaultOk
, mt
);
264 bool OConnectionTabPage::checkTestConnection()
266 OSL_ENSURE(m_pAdminDialog
,"No Admin dialog set! ->GPF");
267 bool bEnableTestConnection
= !m_xConnectionURL
->get_visible() || !m_xConnectionURL
->GetTextNoPrefix().isEmpty();
268 if ( m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
)
269 bEnableTestConnection
= bEnableTestConnection
&& (!o3tl::trim(m_xJavaDriver
->get_text()).empty());
270 m_xTestConnection
->set_sensitive(bEnableTestConnection
);
273 IMPL_LINK(OConnectionTabPage
, OnEditModified
, weld::Entry
&, rEdit
, void)
275 if (&rEdit
== m_xJavaDriver
.get())
276 m_xTestJavaDriver
->set_sensitive( !o3tl::trim(m_xJavaDriver
->get_text()).empty() );
278 checkTestConnection();
279 // tell the listener we were modified
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */