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::sdbc
;
45 std::unique_ptr
<SfxTabPage
> OConnectionTabPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
* _rAttrSet
)
47 return std::make_unique
<OConnectionTabPage
>(pPage
, pController
, *_rAttrSet
);
51 OConnectionTabPage::OConnectionTabPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
& _rCoreAttrs
)
52 : OConnectionHelper(pPage
, pController
, u
"dbaccess/ui/connectionpage.ui"_ustr
, u
"ConnectionPage"_ustr
, _rCoreAttrs
)
53 , m_xFL2(m_xBuilder
->weld_label(u
"userlabel"_ustr
))
54 , m_xUserNameLabel(m_xBuilder
->weld_label(u
"userNameLabel"_ustr
))
55 , m_xUserName(m_xBuilder
->weld_entry(u
"userNameEntry"_ustr
))
56 , m_xPasswordRequired(m_xBuilder
->weld_check_button(u
"passCheckbutton"_ustr
))
57 , m_xFL3(m_xBuilder
->weld_label(u
"JDBCLabel"_ustr
))
58 , m_xJavaDriverLabel(m_xBuilder
->weld_label(u
"javaDriverLabel"_ustr
))
59 , m_xJavaDriver(m_xBuilder
->weld_entry(u
"driverEntry"_ustr
))
60 , m_xTestJavaDriver(m_xBuilder
->weld_button(u
"driverButton"_ustr
))
61 , m_xTestConnection(m_xBuilder
->weld_button(u
"connectionButton"_ustr
))
63 m_xConnectionURL
->connect_changed(LINK(this, OConnectionTabPage
, OnEditModified
));
64 m_xJavaDriver
->connect_changed(LINK(this, OConnectionTabPage
, OnEditModified
));
65 m_xUserName
->connect_changed(LINK(this, OGenericAdministrationPage
, OnControlEntryModifyHdl
));
66 m_xPasswordRequired
->connect_toggled(LINK(this, OGenericAdministrationPage
, OnControlModifiedButtonClick
));
68 m_xTestConnection
->connect_clicked(LINK(this,OGenericAdministrationPage
,OnTestConnectionButtonClickHdl
));
69 m_xTestJavaDriver
->connect_clicked(LINK(this,OConnectionTabPage
,OnTestJavaClickHdl
));
72 OConnectionTabPage::~OConnectionTabPage()
76 void OConnectionTabPage::implInitControls(const SfxItemSet
& _rSet
, bool _bSaveValue
)
78 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
79 bool bValid
, bReadonly
;
80 getFlags(_rSet
, bValid
, bReadonly
);
82 m_eType
= m_pAdminDialog
->getDatasourceType(_rSet
);
83 OConnectionHelper::implInitControls( _rSet
, _bSaveValue
);
85 ::dbaccess::DATASOURCE_TYPE eType
= m_pCollection
->determineType(m_eType
);
88 case ::dbaccess::DST_DBASE
:
89 m_xFT_Connection
->set_label(DBA_RES(STR_DBASE_PATH_OR_FILE
));
90 m_xConnectionURL
->set_help_id(HID_DSADMIN_DBASE_PATH
);
92 case ::dbaccess::DST_FLAT
:
93 m_xFT_Connection
->set_label(DBA_RES(STR_FLAT_PATH_OR_FILE
));
94 m_xConnectionURL
->set_help_id(HID_DSADMIN_FLAT_PATH
);
96 case ::dbaccess::DST_CALC
:
97 m_xFT_Connection
->set_label(DBA_RES(STR_CALC_PATH_OR_FILE
));
98 m_xConnectionURL
->set_help_id(HID_DSADMIN_CALC_PATH
);
100 case ::dbaccess::DST_WRITER
:
101 m_xFT_Connection
->set_label(DBA_RES(STR_WRITER_PATH_OR_FILE
));
102 m_xConnectionURL
->set_help_id(HID_DSADMIN_WRITER_PATH
);
104 case ::dbaccess::DST_MSACCESS
:
105 m_xFT_Connection
->set_label(DBA_RES(STR_MSACCESS_MDB_FILE
));
106 m_xConnectionURL
->set_help_id(HID_DSADMIN_MSACCESS_MDB_FILE
);
108 case ::dbaccess::DST_MYSQL_NATIVE
:
109 case ::dbaccess::DST_MYSQL_JDBC
:
110 m_xFT_Connection
->set_label(DBA_RES(STR_MYSQL_DATABASE_NAME
));
111 m_xConnectionURL
->set_help_id( HID_DSADMIN_MYSQL_DATABASE
);
113 case ::dbaccess::DST_ORACLE_JDBC
:
114 m_xFT_Connection
->set_label(DBA_RES(STR_ORACLE_DATABASE_NAME
));
115 m_xConnectionURL
->set_help_id(HID_DSADMIN_ORACLE_DATABASE
);
117 case ::dbaccess::DST_MYSQL_ODBC
:
118 case ::dbaccess::DST_ODBC
:
119 m_xFT_Connection
->set_label(DBA_RES(STR_NAME_OF_ODBC_DATASOURCE
));
120 m_xConnectionURL
->set_help_id( eType
== ::dbaccess::DST_MYSQL_ODBC
? HID_DSADMIN_MYSQL_ODBC_DATASOURCE
: HID_DSADMIN_ODBC_DATASOURCE
);
122 case ::dbaccess::DST_LDAP
:
123 m_xFT_Connection
->set_label(DBA_RES(STR_HOSTNAME
));
124 m_xConnectionURL
->set_help_id( HID_DSADMIN_LDAP_HOSTNAME
);
126 case ::dbaccess::DST_MOZILLA
:
127 m_xFT_Connection
->set_label(DBA_RES(STR_MOZILLA_PROFILE_NAME
));
128 m_xConnectionURL
->set_help_id( HID_DSADMIN_MOZILLA_PROFILE_NAME
);
130 case ::dbaccess::DST_THUNDERBIRD
:
131 m_xFT_Connection
->set_label(DBA_RES(STR_THUNDERBIRD_PROFILE_NAME
));
132 m_xConnectionURL
->set_help_id( HID_DSADMIN_THUNDERBIRD_PROFILE_NAME
);
134 case ::dbaccess::DST_OUTLOOK
:
135 case ::dbaccess::DST_OUTLOOKEXP
:
136 case ::dbaccess::DST_EVOLUTION
:
137 case ::dbaccess::DST_EVOLUTION_GROUPWISE
:
138 case ::dbaccess::DST_EVOLUTION_LDAP
:
139 case ::dbaccess::DST_KAB
:
140 case ::dbaccess::DST_MACAB
:
141 m_xFT_Connection
->set_label(DBA_RES(STR_NO_ADDITIONAL_SETTINGS
));
143 OUString sText
= m_xFT_Connection
->get_label();
144 sText
= sText
.replaceAll("%test",m_xTestConnection
->get_label());
145 sText
= sText
.replaceAll("~","");
146 m_xFT_Connection
->set_label(sText
);
148 m_xConnectionURL
->hide();
150 case ::dbaccess::DST_ADO
:
151 case ::dbaccess::DST_JDBC
:
153 m_xFT_Connection
->set_label(DBA_RES(STR_COMMONURL
));
157 AuthenticationMode
eAuthMode( DataSourceMetaData::getAuthentication( m_eType
) );
158 bool bShowUserAuthenfication
= ( eAuthMode
!= AuthNone
);
159 bool bShowUser
= ( eAuthMode
== AuthUserPwd
);
161 m_xPB_Connection
->set_help_id(HID_DSADMIN_BROWSECONN
);
162 m_xFL2
->set_visible( bShowUserAuthenfication
);
163 m_xUserNameLabel
->set_visible( bShowUser
&& bShowUserAuthenfication
);
164 m_xUserName
->set_visible( bShowUser
&& bShowUserAuthenfication
);
165 m_xPasswordRequired
->set_visible( bShowUserAuthenfication
);
168 const SfxStringItem
* pUidItem
= _rSet
.GetItem
<SfxStringItem
>(DSID_USER
);
170 const SfxStringItem
* pJdbcDrvItem
= _rSet
.GetItem
<SfxStringItem
>(DSID_JDBCDRIVERCLASS
);
171 const SfxStringItem
* pUrlItem
= _rSet
.GetItem
<SfxStringItem
>(DSID_CONNECTURL
);
172 const SfxBoolItem
* pAllowEmptyPwd
= _rSet
.GetItem
<SfxBoolItem
>(DSID_PASSWORDREQUIRED
);
174 // forward the values to the controls
178 m_xUserName
->set_text(pUidItem
->GetValue());
179 m_xPasswordRequired
->set_active(pAllowEmptyPwd
->GetValue());
181 const OUString
& sUrl
= pUrlItem
->GetValue();
184 const bool bEnableJDBC
= m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
;
185 if ( !pJdbcDrvItem
->GetValue().getLength() )
187 OUString sDefaultJdbcDriverName
= m_pCollection
->getJavaDriverClass(m_eType
);
188 if ( !sDefaultJdbcDriverName
.isEmpty() )
189 m_xJavaDriver
->set_text(sDefaultJdbcDriverName
);
192 m_xJavaDriver
->set_text(pJdbcDrvItem
->GetValue());
194 m_xJavaDriverLabel
->set_visible(bEnableJDBC
);
195 m_xJavaDriver
->set_visible(bEnableJDBC
);
196 m_xTestJavaDriver
->set_visible(bEnableJDBC
);
197 m_xTestJavaDriver
->set_sensitive( !o3tl::trim(m_xJavaDriver
->get_text()).empty() );
198 m_xFL3
->set_visible(bEnableJDBC
);
200 checkTestConnection();
202 m_xUserName
->save_value();
203 m_xConnectionURL
->save_value();
204 m_xJavaDriver
->save_value();
205 m_xPasswordRequired
->save_state();
208 bool OConnectionTabPage::FillItemSet(SfxItemSet
* _rSet
)
210 bool bChangedSomething
= false;
212 if (m_xUserName
->get_value_changed_from_saved())
214 _rSet
->Put(SfxStringItem(DSID_USER
, m_xUserName
->get_text()));
215 _rSet
->Put(SfxStringItem(DSID_PASSWORD
, OUString()));
216 bChangedSomething
= true;
219 fillBool(*_rSet
,m_xPasswordRequired
.get(),DSID_PASSWORDREQUIRED
,false, bChangedSomething
);
221 if ( m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
)
223 fillString(*_rSet
,m_xJavaDriver
.get(), DSID_JDBCDRIVERCLASS
, bChangedSomething
);
226 fillString(*_rSet
,m_xConnectionURL
.get(), DSID_CONNECTURL
, bChangedSomething
);
228 return bChangedSomething
;
230 IMPL_LINK_NOARG(OConnectionTabPage
, OnTestJavaClickHdl
, weld::Button
&, void)
232 assert(m_pAdminDialog
&& "No Admin dialog set! ->GPF");
233 bool bSuccess
= false;
234 #if HAVE_FEATURE_JAVA
237 if ( !o3tl::trim(m_xJavaDriver
->get_text()).empty() )
239 ::rtl::Reference
< jvmaccess::VirtualMachine
> xJVM
= ::connectivity::getJavaVM( m_pAdminDialog
->getORB() );
240 m_xJavaDriver
->set_text(m_xJavaDriver
->get_text().trim()); // fdo#68341
241 bSuccess
= ::connectivity::existsJavaClassByName(xJVM
, o3tl::trim(m_xJavaDriver
->get_text()));
249 TranslateId pMessage
= bSuccess
? STR_JDBCDRIVER_SUCCESS
: STR_JDBCDRIVER_NO_SUCCESS
;
250 const MessageType mt
= bSuccess
? MessageType::Info
: MessageType::Error
;
251 OSQLMessageBox
aMsg(GetFrameWeld(), DBA_RES(pMessage
), OUString(), MessBoxStyle::Ok
| MessBoxStyle::DefaultOk
, mt
);
254 bool OConnectionTabPage::checkTestConnection()
256 assert(m_pAdminDialog
&& "No Admin dialog set! ->GPF");
257 bool bEnableTestConnection
= !m_xConnectionURL
->get_visible() || !m_xConnectionURL
->GetTextNoPrefix().isEmpty();
258 if ( m_pCollection
->determineType(m_eType
) == ::dbaccess::DST_JDBC
)
259 bEnableTestConnection
= bEnableTestConnection
&& (!o3tl::trim(m_xJavaDriver
->get_text()).empty());
260 m_xTestConnection
->set_sensitive(bEnableTestConnection
);
263 IMPL_LINK(OConnectionTabPage
, OnEditModified
, weld::Entry
&, rEdit
, void)
265 if (&rEdit
== m_xJavaDriver
.get())
266 m_xTestJavaDriver
->set_sensitive( !o3tl::trim(m_xJavaDriver
->get_text()).empty() );
268 checkTestConnection();
269 // tell the listener we were modified
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */