Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cui / source / options / connpooloptions.cxx
blob45ca5d9f3e07a9de06812d4a17603d0522295136
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 .
20 #include <osl/diagnose.h>
21 #include "connpooloptions.hxx"
22 #include <svtools/editbrowsebox.hxx>
23 #include "connpoolsettings.hxx"
24 #include <svl/eitem.hxx>
25 #include <svx/databaseregistrationui.hxx>
26 #include <strings.hrc>
27 #include <dialmgr.hxx>
29 using ::svt::EditBrowseBox;
31 namespace offapp
33 bool ConnectionPoolOptionsPage::isModifiedDriverList() const
35 if (m_aSettings.size() != m_aSavedSettings.size())
36 return true;
38 DriverPoolingSettings::const_iterator aSaved = m_aSavedSettings.begin();
39 for (auto const& currentSetting : m_aSettings)
41 if (currentSetting != *aSaved)
42 return true;
43 ++aSaved;
46 return false;
49 ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& _rAttrSet)
50 : SfxTabPage(pPage, pController, "cui/ui/connpooloptions.ui", "ConnPoolPage", &_rAttrSet)
51 , m_sYes(CuiResId(RID_SVXSTR_YES))
52 , m_sNo(CuiResId(RID_SVXSTR_NO))
53 , m_xEnablePooling(m_xBuilder->weld_check_button("connectionpooling"))
54 , m_xDriversLabel(m_xBuilder->weld_label("driverslabel"))
55 , m_xDriverList(m_xBuilder->weld_tree_view("driverlist"))
56 , m_xDriverLabel(m_xBuilder->weld_label("driverlabel"))
57 , m_xDriver(m_xBuilder->weld_label("driver"))
58 , m_xDriverPoolingEnabled(m_xBuilder->weld_check_button("enablepooling"))
59 , m_xTimeoutLabel(m_xBuilder->weld_label("timeoutlabel"))
60 , m_xTimeout(m_xBuilder->weld_spin_button("timeout"))
62 m_xDriverList->set_size_request(m_xDriverList->get_approximate_digit_width() * 60,
63 m_xDriverList->get_height_rows(15));
64 m_xDriverList->show();
66 std::vector<int> aWidths;
67 aWidths.push_back(m_xDriverList->get_approximate_digit_width() * 50);
68 aWidths.push_back(m_xDriverList->get_approximate_digit_width() * 8);
69 m_xDriverList->set_column_fixed_widths(aWidths);
71 m_xEnablePooling->connect_clicked( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
72 m_xDriverPoolingEnabled->connect_clicked( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
74 m_xDriverList->connect_changed(LINK(this, ConnectionPoolOptionsPage, OnDriverRowChanged));
75 m_xTimeout->connect_value_changed(LINK(this, ConnectionPoolOptionsPage, OnSpinValueChanged));
78 void ConnectionPoolOptionsPage::updateRow(size_t nRow)
80 auto const& currentSetting = m_aSettings[nRow];
81 m_xDriverList->set_text(nRow, currentSetting.sName, 0);
82 if (currentSetting.bEnabled)
84 m_xDriverList->set_text(nRow, m_sYes, 1);
85 m_xDriverList->set_text(nRow, OUString::number(currentSetting.nTimeoutSeconds), 2);
87 else
89 m_xDriverList->set_text(nRow, m_sNo, 1);
90 m_xDriverList->set_text(nRow, "-", 2);
94 void ConnectionPoolOptionsPage::updateCurrentRow()
96 int nRow = m_xDriverList->get_selected_index();
97 if (nRow == -1)
98 return;
99 updateRow(nRow);
102 void ConnectionPoolOptionsPage::UpdateDriverList(const DriverPoolingSettings& _rSettings)
104 m_aSettings = _rSettings;
106 m_xDriverList->freeze();
107 m_xDriverList->clear();
109 for (size_t i = 0; i < m_aSettings.size(); ++i)
111 m_xDriverList->append();
112 updateRow(i);
115 m_xDriverList->thaw();
117 if (!m_aSettings.empty())
119 m_xDriverList->select(0);
120 OnDriverRowChanged(*m_xDriverList);
124 ConnectionPoolOptionsPage::~ConnectionPoolOptionsPage()
128 std::unique_ptr<SfxTabPage> ConnectionPoolOptionsPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* _rAttrSet)
130 return std::make_unique<ConnectionPoolOptionsPage>(pPage, pController, *_rAttrSet);
133 void ConnectionPoolOptionsPage::implInitControls(const SfxItemSet& _rSet)
135 // the enabled flag
136 const SfxBoolItem* pEnabled = _rSet.GetItem<SfxBoolItem>(SID_SB_POOLING_ENABLED);
137 OSL_ENSURE(pEnabled, "ConnectionPoolOptionsPage::implInitControls: missing the Enabled item!");
138 m_xEnablePooling->set_active(pEnabled == nullptr || pEnabled->GetValue());
140 m_xEnablePooling->save_state();
142 // the settings for the single drivers
143 const DriverPoolingSettingsItem* pDriverSettings = _rSet.GetItem<DriverPoolingSettingsItem>(SID_SB_DRIVER_TIMEOUTS);
144 if (pDriverSettings)
145 UpdateDriverList(pDriverSettings->getSettings());
146 else
148 OSL_FAIL("ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
149 UpdateDriverList(DriverPoolingSettings());
151 saveDriverList();
153 // reflect the new settings
154 OnEnabledDisabled(*m_xEnablePooling);
157 IMPL_LINK_NOARG(ConnectionPoolOptionsPage, OnSpinValueChanged, weld::SpinButton&, void)
159 commitTimeoutField();
162 bool ConnectionPoolOptionsPage::FillItemSet(SfxItemSet* _rSet)
164 commitTimeoutField();
166 bool bModified = false;
167 // the enabled flag
168 if (m_xEnablePooling->get_state_changed_from_saved())
170 _rSet->Put(SfxBoolItem(SID_SB_POOLING_ENABLED, m_xEnablePooling->get_active()));
171 bModified = true;
174 // the settings for the single drivers
175 if (isModifiedDriverList())
177 _rSet->Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, m_aSettings));
178 bModified = true;
181 return bModified;
184 void ConnectionPoolOptionsPage::ActivatePage( const SfxItemSet& _rSet)
186 SfxTabPage::ActivatePage(_rSet);
187 implInitControls(_rSet);
190 void ConnectionPoolOptionsPage::Reset(const SfxItemSet* _rSet)
192 implInitControls(*_rSet);
195 IMPL_LINK_NOARG(ConnectionPoolOptionsPage, OnDriverRowChanged, weld::TreeView&, void)
197 const int nDriverPos = m_xDriverList->get_selected_index();
198 bool bValidRow = (nDriverPos != -1);
199 m_xDriverPoolingEnabled->set_sensitive(bValidRow && m_xEnablePooling->get_active());
200 m_xTimeoutLabel->set_sensitive(bValidRow);
201 m_xTimeout->set_sensitive(bValidRow);
203 if (!bValidRow)
204 { // positioned on an invalid row
205 m_xDriver->set_label(OUString());
207 else
209 auto const& currentSetting = m_aSettings[nDriverPos];
210 m_xDriver->set_label(currentSetting.sName);
211 m_xDriverPoolingEnabled->set_active(currentSetting.bEnabled);
212 m_xTimeout->set_value(currentSetting.nTimeoutSeconds);
214 OnEnabledDisabled(*m_xDriverPoolingEnabled);
218 void ConnectionPoolOptionsPage::commitTimeoutField()
220 const int nDriverPos = m_xDriverList->get_selected_index();
221 if (nDriverPos == -1)
222 return;
223 m_aSettings[nDriverPos].nTimeoutSeconds = m_xTimeout->get_value();
224 updateCurrentRow();
227 IMPL_LINK( ConnectionPoolOptionsPage, OnEnabledDisabled, weld::Button&, rCheckBox, void )
229 bool bGloballyEnabled = m_xEnablePooling->get_active();
230 bool bLocalDriverChanged = m_xDriverPoolingEnabled.get() == &rCheckBox;
232 if (m_xEnablePooling.get() == &rCheckBox)
234 m_xDriversLabel->set_sensitive(bGloballyEnabled);
235 m_xDriverList->set_sensitive(bGloballyEnabled);
236 if (!bGloballyEnabled)
237 m_xDriverList->select(-1);
238 m_xDriverLabel->set_sensitive(bGloballyEnabled);
239 m_xDriver->set_sensitive(bGloballyEnabled);
240 m_xDriverPoolingEnabled->set_sensitive(bGloballyEnabled);
242 else
243 OSL_ENSURE(bLocalDriverChanged, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");
245 m_xTimeoutLabel->set_sensitive(bGloballyEnabled && m_xDriverPoolingEnabled->get_active());
246 m_xTimeout->set_sensitive(bGloballyEnabled && m_xDriverPoolingEnabled->get_active());
248 if (bLocalDriverChanged)
250 // update the list
251 const int nDriverPos = m_xDriverList->get_selected_index();
252 if (nDriverPos == -1)
253 return;
254 m_aSettings[nDriverPos].bEnabled = m_xDriverPoolingEnabled->get_active();
255 updateCurrentRow();
259 } // namespace offapp
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */