summaryrefslogtreecommitdiffstats
path: root/cui/source/options/connpooloptions.cxx
blob: f6321f2252b16709c0e7d400d85a93bab4d8b76e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <o3tl/safeint.hxx>
#include <osl/diagnose.h>
#include "connpooloptions.hxx"
#include "connpoolsettings.hxx"
#include <svl/eitem.hxx>
#include <svx/databaseregistrationui.hxx>
#include <strings.hrc>
#include <dialmgr.hxx>
#include <officecfg/Office/DataAccess.hxx>
#include <com/sun/star/beans/PropertyAttribute.hpp>

namespace offapp
{
    bool ConnectionPoolOptionsPage::isModifiedDriverList() const
    {
        if (m_aSettings.size() != m_aSavedSettings.size())
            return true;

        DriverPoolingSettings::const_iterator aSaved = m_aSavedSettings.begin();
        for (auto const& currentSetting : m_aSettings)
        {
            if (currentSetting != *aSaved)
                return true;
            ++aSaved;
        }

        return false;
    }

    ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& _rAttrSet)
        : SfxTabPage(pPage, pController, "cui/ui/connpooloptions.ui", "ConnPoolPage", &_rAttrSet)
        , m_sYes(CuiResId(RID_CUISTR_YES))
        , m_sNo(CuiResId(RID_CUISTR_NO))
        , m_xEnablePooling(m_xBuilder->weld_check_button("connectionpooling"))
        , m_xEnablePoolingImg(m_xBuilder->weld_widget("lockconnectionpooling"))
        , m_xDriversLabel(m_xBuilder->weld_label("driverslabel"))
        , m_xDriverList(m_xBuilder->weld_tree_view("driverlist"))
        , m_xDriverLabel(m_xBuilder->weld_label("driverlabel"))
        , m_xDriver(m_xBuilder->weld_label("driver"))
        , m_xDriverPoolingEnabled(m_xBuilder->weld_check_button("enablepooling"))
        , m_xDriverPoolingEnabledImg(m_xBuilder->weld_widget("lockenablepooling"))
        , m_xTimeoutLabel(m_xBuilder->weld_label("timeoutlabel"))
        , m_xTimeout(m_xBuilder->weld_spin_button("timeout"))
        , m_xTimeoutImg(m_xBuilder->weld_widget("locktimeout"))
    {
        m_xDriverList->set_size_request(m_xDriverList->get_approximate_digit_width() * 60,
                                        m_xDriverList->get_height_rows(15));
        m_xDriverList->show();

        std::vector<int> aWidths
        {
            o3tl::narrowing<int>(m_xDriverList->get_approximate_digit_width() * 50),
            o3tl::narrowing<int>(m_xDriverList->get_approximate_digit_width() * 8)
        };
        m_xDriverList->set_column_fixed_widths(aWidths);

        css::uno::Reference < css::uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
        m_xReadWriteAccess = css::configuration::ReadWriteAccess::create(xContext, "*");

        m_xEnablePooling->connect_toggled( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
        m_xDriverPoolingEnabled->connect_toggled( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );

        m_xDriverList->connect_changed(LINK(this, ConnectionPoolOptionsPage, OnDriverRowChanged));
        m_xTimeout->connect_value_changed(LINK(this, ConnectionPoolOptionsPage, OnSpinValueChanged));
    }

    void ConnectionPoolOptionsPage::updateRow(size_t nRow)
    {
        auto const& currentSetting = m_aSettings[nRow];
        m_xDriverList->set_text(nRow, currentSetting.sName, 0);
        if (currentSetting.bEnabled)
        {
            m_xDriverList->set_text(nRow, m_sYes, 1);
            m_xDriverList->set_text(nRow, OUString::number(currentSetting.nTimeoutSeconds), 2);
        }
        else
        {
            m_xDriverList->set_text(nRow, m_sNo, 1);
            m_xDriverList->set_text(nRow, "-", 2);
        }
    }

    void ConnectionPoolOptionsPage::updateCurrentRow()
    {
        int nRow = m_xDriverList->get_selected_index();
        if (nRow == -1)
            return;
        updateRow(nRow);
    }

    void ConnectionPoolOptionsPage::UpdateDriverList(const DriverPoolingSettings& _rSettings)
    {
        m_aSettings = _rSettings;

        m_xDriverList->freeze();
        m_xDriverList->clear();

        for (size_t i = 0; i < m_aSettings.size(); ++i)
        {
            m_xDriverList->append();
            updateRow(i);
        }

        m_xDriverList->thaw();

        if (!m_aSettings.empty())
        {
            m_xDriverList->select(0);
            OnDriverRowChanged(*m_xDriverList);
        }
    }

    ConnectionPoolOptionsPage::~ConnectionPoolOptionsPage()
    {
    }

    std::unique_ptr<SfxTabPage> ConnectionPoolOptionsPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* _rAttrSet)
    {
        return std::make_unique<ConnectionPoolOptionsPage>(pPage, pController, *_rAttrSet);
    }

    void ConnectionPoolOptionsPage::implInitControls(const SfxItemSet& _rSet)
    {
        // the enabled flag
        const SfxBoolItem* pEnabled = _rSet.GetItem<SfxBoolItem>(SID_SB_POOLING_ENABLED);
        OSL_ENSURE(pEnabled, "ConnectionPoolOptionsPage::implInitControls: missing the Enabled item!");
        m_xEnablePooling->set_active(pEnabled == nullptr || pEnabled->GetValue());
        m_xEnablePooling->set_sensitive(!officecfg::Office::DataAccess::ConnectionPool::EnablePooling::isReadOnly());
        m_xEnablePoolingImg->set_visible(officecfg::Office::DataAccess::ConnectionPool::EnablePooling::isReadOnly());

        m_xEnablePooling->save_state();

        // the settings for the single drivers
        const DriverPoolingSettingsItem* pDriverSettings = _rSet.GetItem<DriverPoolingSettingsItem>(SID_SB_DRIVER_TIMEOUTS);
        if (pDriverSettings)
            UpdateDriverList(pDriverSettings->getSettings());
        else
        {
            SAL_WARN("cui.options", "ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
            UpdateDriverList(DriverPoolingSettings());
        }
        saveDriverList();

        // reflect the new settings
        OnEnabledDisabled(*m_xEnablePooling);
    }

    IMPL_LINK_NOARG(ConnectionPoolOptionsPage, OnSpinValueChanged, weld::SpinButton&, void)
    {
        commitTimeoutField();
    }

    OUString ConnectionPoolOptionsPage::GetAllStrings()
    {
        OUString sAllStrings;
        OUString labels[] = { "label1", "driverslabel", "driverlabel", "timeoutlabel", "driver" };

        for (const auto& label : labels)
        {
            if (const auto& pString = m_xBuilder->weld_label(label))
                sAllStrings += pString->get_label() + " ";
        }

        OUString checkButton[] = { "connectionpooling", "enablepooling" };

        for (const auto& check : checkButton)
        {
            if (const auto& pString = m_xBuilder->weld_check_button(check))
                sAllStrings += pString->get_label() + " ";
        }

        return sAllStrings.replaceAll("_", "");
    }

    bool ConnectionPoolOptionsPage::FillItemSet(SfxItemSet* _rSet)
    {
        commitTimeoutField();

        bool bModified = false;
        // the enabled flag
        if (m_xEnablePooling->get_state_changed_from_saved())
        {
            _rSet->Put(SfxBoolItem(SID_SB_POOLING_ENABLED, m_xEnablePooling->get_active()));
            bModified = true;
        }

        // the settings for the single drivers
        if (isModifiedDriverList())
        {
            _rSet->Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, m_aSettings));
            bModified = true;
        }

        return bModified;
    }

    void ConnectionPoolOptionsPage::ActivatePage( const SfxItemSet& _rSet)
    {
        SfxTabPage::ActivatePage(_rSet);
        implInitControls(_rSet);
    }

    void ConnectionPoolOptionsPage::Reset(const SfxItemSet* _rSet)
    {
        implInitControls(*_rSet);
    }

    IMPL_LINK_NOARG(ConnectionPoolOptionsPage, OnDriverRowChanged, weld::TreeView&, void)
    {
        const int nDriverPos = m_xDriverList->get_selected_index();
        bool bValidRow = (nDriverPos != -1);
        m_xDriverPoolingEnabled->set_sensitive(bValidRow && m_xEnablePooling->get_active());
        m_xTimeoutLabel->set_sensitive(bValidRow);
        m_xTimeout->set_sensitive(bValidRow);

        if (!bValidRow)
        {   // positioned on an invalid row
            m_xDriver->set_label(OUString());
        }
        else
        {
            auto const& currentSetting = m_aSettings[nDriverPos];
            m_xDriver->set_label(currentSetting.sName);
            m_xDriverPoolingEnabled->set_active(currentSetting.bEnabled);
            m_xTimeout->set_value(currentSetting.nTimeoutSeconds);

            OUString aConfigPath = officecfg::Office::DataAccess::ConnectionPool::DriverSettings::path() + "/" + currentSetting.sName;
            css::beans::Property aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName(aConfigPath + "/Enable");
            bool bReadOnly = (aProperty.Attributes & css::beans::PropertyAttribute::READONLY) != 0;

            m_xDriverPoolingEnabled->set_sensitive(!bReadOnly);
            m_xDriverPoolingEnabledImg->set_visible(bReadOnly);

            aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName(aConfigPath + "/Timeout");
            bReadOnly = (aProperty.Attributes & css::beans::PropertyAttribute::READONLY) != 0;

            m_xTimeout->set_sensitive(!bReadOnly);
            m_xTimeoutLabel->set_sensitive(!bReadOnly);
            m_xTimeoutImg->set_visible(bReadOnly);

            OnEnabledDisabled(*m_xDriverPoolingEnabled);
        }
    }

    void ConnectionPoolOptionsPage::commitTimeoutField()
    {
        const int nDriverPos = m_xDriverList->get_selected_index();
        if (nDriverPos == -1)
            return;
        m_aSettings[nDriverPos].nTimeoutSeconds = m_xTimeout->get_value();
        updateCurrentRow();
    }

    IMPL_LINK( ConnectionPoolOptionsPage, OnEnabledDisabled, weld::Toggleable&, rCheckBox, void )
    {
        bool bGloballyEnabled = m_xEnablePooling->get_active();
        bool bLocalDriverChanged = m_xDriverPoolingEnabled.get() == &rCheckBox;

        if (m_xEnablePooling.get() == &rCheckBox)
        {
            m_xDriversLabel->set_sensitive(bGloballyEnabled);
            m_xDriverList->set_sensitive(bGloballyEnabled);
            if (!bGloballyEnabled)
                m_xDriverList->select(-1);
            m_xDriverLabel->set_sensitive(bGloballyEnabled);
            m_xDriver->set_sensitive(bGloballyEnabled);
            m_xDriverPoolingEnabled->set_sensitive(bGloballyEnabled && !m_xDriverPoolingEnabledImg->get_visible());
        }
        else
            OSL_ENSURE(bLocalDriverChanged, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");

        m_xTimeoutLabel->set_sensitive(bGloballyEnabled && m_xDriverPoolingEnabled->get_active() && !m_xTimeoutImg->get_visible());
        m_xTimeout->set_sensitive(bGloballyEnabled && m_xDriverPoolingEnabled->get_active() && !m_xTimeoutImg->get_visible());

        if (bLocalDriverChanged)
        {
            // update the list
            const int nDriverPos = m_xDriverList->get_selected_index();
            if (nDriverPos == -1)
                return;
            m_aSettings[nDriverPos].bEnabled = m_xDriverPoolingEnabled->get_active();
            updateCurrentRow();
        }
    }

} // namespace offapp

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */