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 <o3tl/safeint.hxx>
21 #include <osl/diagnose.h>
22 #include "connpooloptions.hxx"
23 #include "connpoolsettings.hxx"
24 #include <svl/eitem.hxx>
25 #include <svx/databaseregistrationui.hxx>
26 #include <strings.hrc>
27 #include <dialmgr.hxx>
28 #include <officecfg/Office/DataAccess.hxx>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 bool ConnectionPoolOptionsPage::isModifiedDriverList() const
35 if (m_aSettings
.size() != m_aSavedSettings
.size())
38 DriverPoolingSettings::const_iterator aSaved
= m_aSavedSettings
.begin();
39 for (auto const& currentSetting
: m_aSettings
)
41 if (currentSetting
!= *aSaved
)
49 ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
& _rAttrSet
)
50 : SfxTabPage(pPage
, pController
, u
"cui/ui/connpooloptions.ui"_ustr
, u
"ConnPoolPage"_ustr
, &_rAttrSet
)
51 , m_sYes(CuiResId(RID_CUISTR_YES
))
52 , m_sNo(CuiResId(RID_CUISTR_NO
))
53 , m_xEnablePooling(m_xBuilder
->weld_check_button(u
"connectionpooling"_ustr
))
54 , m_xEnablePoolingImg(m_xBuilder
->weld_widget(u
"lockconnectionpooling"_ustr
))
55 , m_xDriversLabel(m_xBuilder
->weld_label(u
"driverslabel"_ustr
))
56 , m_xDriverList(m_xBuilder
->weld_tree_view(u
"driverlist"_ustr
))
57 , m_xDriverLabel(m_xBuilder
->weld_label(u
"driverlabel"_ustr
))
58 , m_xDriver(m_xBuilder
->weld_label(u
"driver"_ustr
))
59 , m_xDriverPoolingEnabled(m_xBuilder
->weld_check_button(u
"enablepooling"_ustr
))
60 , m_xDriverPoolingEnabledImg(m_xBuilder
->weld_widget(u
"lockenablepooling"_ustr
))
61 , m_xTimeoutLabel(m_xBuilder
->weld_label(u
"timeoutlabel"_ustr
))
62 , m_xTimeout(m_xBuilder
->weld_spin_button(u
"timeout"_ustr
))
63 , m_xTimeoutImg(m_xBuilder
->weld_widget(u
"locktimeout"_ustr
))
65 m_xDriverList
->set_size_request(m_xDriverList
->get_approximate_digit_width() * 60,
66 m_xDriverList
->get_height_rows(15));
67 m_xDriverList
->show();
69 std::vector
<int> aWidths
71 o3tl::narrowing
<int>(m_xDriverList
->get_approximate_digit_width() * 50),
72 o3tl::narrowing
<int>(m_xDriverList
->get_approximate_digit_width() * 8)
74 m_xDriverList
->set_column_fixed_widths(aWidths
);
76 const css::uno::Reference
< css::uno::XComponentContext
>& xContext(::comphelper::getProcessComponentContext());
77 m_xReadWriteAccess
= css::configuration::ReadWriteAccess::create(xContext
, u
"*"_ustr
);
79 m_xEnablePooling
->connect_toggled( LINK(this, ConnectionPoolOptionsPage
, OnEnabledDisabled
) );
80 m_xDriverPoolingEnabled
->connect_toggled( LINK(this, ConnectionPoolOptionsPage
, OnEnabledDisabled
) );
82 m_xDriverList
->connect_selection_changed(
83 LINK(this, ConnectionPoolOptionsPage
, OnDriverRowChanged
));
84 m_xTimeout
->connect_value_changed(LINK(this, ConnectionPoolOptionsPage
, OnSpinValueChanged
));
87 void ConnectionPoolOptionsPage::updateRow(size_t nRow
)
89 auto const& currentSetting
= m_aSettings
[nRow
];
90 m_xDriverList
->set_text(nRow
, currentSetting
.sName
, 0);
91 if (currentSetting
.bEnabled
)
93 m_xDriverList
->set_text(nRow
, m_sYes
, 1);
94 m_xDriverList
->set_text(nRow
, OUString::number(currentSetting
.nTimeoutSeconds
), 2);
98 m_xDriverList
->set_text(nRow
, m_sNo
, 1);
99 m_xDriverList
->set_text(nRow
, u
"-"_ustr
, 2);
103 void ConnectionPoolOptionsPage::updateCurrentRow()
105 int nRow
= m_xDriverList
->get_selected_index();
111 void ConnectionPoolOptionsPage::UpdateDriverList(const DriverPoolingSettings
& _rSettings
)
113 m_aSettings
= _rSettings
;
115 m_xDriverList
->freeze();
116 m_xDriverList
->clear();
118 for (size_t i
= 0; i
< m_aSettings
.size(); ++i
)
120 m_xDriverList
->append();
124 m_xDriverList
->thaw();
126 if (!m_aSettings
.empty())
128 m_xDriverList
->select(0);
129 OnDriverRowChanged(*m_xDriverList
);
133 ConnectionPoolOptionsPage::~ConnectionPoolOptionsPage()
137 std::unique_ptr
<SfxTabPage
> ConnectionPoolOptionsPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
* _rAttrSet
)
139 return std::make_unique
<ConnectionPoolOptionsPage
>(pPage
, pController
, *_rAttrSet
);
142 void ConnectionPoolOptionsPage::implInitControls(const SfxItemSet
& _rSet
)
145 const SfxBoolItem
* pEnabled
= _rSet
.GetItem
<SfxBoolItem
>(SID_SB_POOLING_ENABLED
);
146 OSL_ENSURE(pEnabled
, "ConnectionPoolOptionsPage::implInitControls: missing the Enabled item!");
147 m_xEnablePooling
->set_active(pEnabled
== nullptr || pEnabled
->GetValue());
148 m_xEnablePooling
->set_sensitive(!officecfg::Office::DataAccess::ConnectionPool::EnablePooling::isReadOnly());
149 m_xEnablePoolingImg
->set_visible(officecfg::Office::DataAccess::ConnectionPool::EnablePooling::isReadOnly());
151 m_xEnablePooling
->save_state();
153 // the settings for the single drivers
154 const DriverPoolingSettingsItem
* pDriverSettings
= _rSet
.GetItem
<DriverPoolingSettingsItem
>(SID_SB_DRIVER_TIMEOUTS
);
156 UpdateDriverList(pDriverSettings
->getSettings());
159 SAL_WARN("cui.options", "ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
160 UpdateDriverList(DriverPoolingSettings());
164 // reflect the new settings
165 OnEnabledDisabled(*m_xEnablePooling
);
168 IMPL_LINK_NOARG(ConnectionPoolOptionsPage
, OnSpinValueChanged
, weld::SpinButton
&, void)
170 commitTimeoutField();
173 OUString
ConnectionPoolOptionsPage::GetAllStrings()
175 OUString sAllStrings
;
176 OUString labels
[] = { u
"label1"_ustr
, u
"driverslabel"_ustr
, u
"driverlabel"_ustr
, u
"timeoutlabel"_ustr
, u
"driver"_ustr
};
178 for (const auto& label
: labels
)
180 if (const auto pString
= m_xBuilder
->weld_label(label
))
181 sAllStrings
+= pString
->get_label() + " ";
184 OUString checkButton
[] = { u
"connectionpooling"_ustr
, u
"enablepooling"_ustr
};
186 for (const auto& check
: checkButton
)
188 if (const auto pString
= m_xBuilder
->weld_check_button(check
))
189 sAllStrings
+= pString
->get_label() + " ";
192 return sAllStrings
.replaceAll("_", "");
195 bool ConnectionPoolOptionsPage::FillItemSet(SfxItemSet
* _rSet
)
197 commitTimeoutField();
199 bool bModified
= false;
201 if (m_xEnablePooling
->get_state_changed_from_saved())
203 _rSet
->Put(SfxBoolItem(SID_SB_POOLING_ENABLED
, m_xEnablePooling
->get_active()));
207 // the settings for the single drivers
208 if (isModifiedDriverList())
210 _rSet
->Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS
, m_aSettings
));
217 void ConnectionPoolOptionsPage::ActivatePage( const SfxItemSet
& _rSet
)
219 SfxTabPage::ActivatePage(_rSet
);
220 implInitControls(_rSet
);
223 void ConnectionPoolOptionsPage::Reset(const SfxItemSet
* _rSet
)
225 implInitControls(*_rSet
);
228 IMPL_LINK_NOARG(ConnectionPoolOptionsPage
, OnDriverRowChanged
, weld::TreeView
&, void)
230 const int nDriverPos
= m_xDriverList
->get_selected_index();
231 bool bValidRow
= (nDriverPos
!= -1);
232 m_xDriverPoolingEnabled
->set_sensitive(bValidRow
&& m_xEnablePooling
->get_active());
233 m_xTimeoutLabel
->set_sensitive(bValidRow
);
234 m_xTimeout
->set_sensitive(bValidRow
);
237 { // positioned on an invalid row
238 m_xDriver
->set_label(OUString());
242 auto const& currentSetting
= m_aSettings
[nDriverPos
];
243 m_xDriver
->set_label(currentSetting
.sName
);
244 m_xDriverPoolingEnabled
->set_active(currentSetting
.bEnabled
);
245 m_xTimeout
->set_value(currentSetting
.nTimeoutSeconds
);
247 OUString aConfigPath
= officecfg::Office::DataAccess::ConnectionPool::DriverSettings::path() + "/" + currentSetting
.sName
;
248 css::beans::Property aProperty
= m_xReadWriteAccess
->getPropertyByHierarchicalName(aConfigPath
+ "/Enable");
249 bool bReadOnly
= (aProperty
.Attributes
& css::beans::PropertyAttribute::READONLY
) != 0;
251 m_xDriverPoolingEnabled
->set_sensitive(!bReadOnly
);
252 m_xDriverPoolingEnabledImg
->set_visible(bReadOnly
);
254 aProperty
= m_xReadWriteAccess
->getPropertyByHierarchicalName(aConfigPath
+ "/Timeout");
255 bReadOnly
= (aProperty
.Attributes
& css::beans::PropertyAttribute::READONLY
) != 0;
257 m_xTimeout
->set_sensitive(!bReadOnly
);
258 m_xTimeoutLabel
->set_sensitive(!bReadOnly
);
259 m_xTimeoutImg
->set_visible(bReadOnly
);
261 OnEnabledDisabled(*m_xDriverPoolingEnabled
);
265 void ConnectionPoolOptionsPage::commitTimeoutField()
267 const int nDriverPos
= m_xDriverList
->get_selected_index();
268 if (nDriverPos
== -1)
270 m_aSettings
[nDriverPos
].nTimeoutSeconds
= m_xTimeout
->get_value();
274 IMPL_LINK( ConnectionPoolOptionsPage
, OnEnabledDisabled
, weld::Toggleable
&, rCheckBox
, void )
276 bool bGloballyEnabled
= m_xEnablePooling
->get_active();
277 bool bLocalDriverChanged
= m_xDriverPoolingEnabled
.get() == &rCheckBox
;
279 if (m_xEnablePooling
.get() == &rCheckBox
)
281 m_xDriversLabel
->set_sensitive(bGloballyEnabled
);
282 m_xDriverList
->set_sensitive(bGloballyEnabled
);
283 if (!bGloballyEnabled
)
284 m_xDriverList
->select(-1);
285 m_xDriverLabel
->set_sensitive(bGloballyEnabled
);
286 m_xDriver
->set_sensitive(bGloballyEnabled
);
287 m_xDriverPoolingEnabled
->set_sensitive(bGloballyEnabled
&& !m_xDriverPoolingEnabledImg
->get_visible());
290 OSL_ENSURE(bLocalDriverChanged
, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");
292 m_xTimeoutLabel
->set_sensitive(bGloballyEnabled
&& m_xDriverPoolingEnabled
->get_active() && !m_xTimeoutImg
->get_visible());
293 m_xTimeout
->set_sensitive(bGloballyEnabled
&& m_xDriverPoolingEnabled
->get_active() && !m_xTimeoutImg
->get_visible());
295 if (bLocalDriverChanged
)
298 const int nDriverPos
= m_xDriverList
->get_selected_index();
299 if (nDriverPos
== -1)
301 m_aSettings
[nDriverPos
].bEnabled
= m_xDriverPoolingEnabled
->get_active();
306 } // namespace offapp
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */