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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* -*- 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 <osl/diagnose.h>
#include "connpooloptions.hxx"
#include <svtools/editbrowsebox.hxx>
#include <vcl/field.hxx>
#include <vcl/builderfactory.hxx>
#include "connpoolsettings.hxx"
#include <svl/eitem.hxx>
#include <strings.hrc>
#include <dialmgr.hxx>

using ::svt::EditBrowseBox;

namespace offapp
{
    /// Widget for the Connection Pool options page
    class DriverListControl : public EditBrowseBox
    {
        using Window::Update;
    protected:
        DriverPoolingSettings                   m_aSavedSettings;
        DriverPoolingSettings                   m_aSettings;
        DriverPoolingSettings::const_iterator   m_aSeekRow;

        OUString                                m_sYes;
        OUString                                m_sNo;

        Link<const DriverPooling*,void>               m_aRowChangeHandler;

    public:
        explicit DriverListControl(vcl::Window* _pParent);

        virtual void Init() override;
                void Update(const DriverPoolingSettings& _rSettings);
        virtual OUString GetCellText( long nRow, sal_uInt16 nColId ) const override;

        // the handler will be called with a DriverPoolingSettings::const_iterator as parameter,
        // or NULL if no valid current row exists
        void SetRowChangeHandler(const Link<const DriverPooling*,void>& _rHdl) { m_aRowChangeHandler = _rHdl; }

        DriverPooling* getCurrentRow();
        void                                    updateCurrentRow();

        const DriverPoolingSettings& getSettings() const { return m_aSettings; }

        void        saveValue()             { m_aSavedSettings = m_aSettings; }
        bool    isModified() const;

    protected:
        virtual void InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol ) override;
        virtual ::svt::CellController* GetController( long nRow, sal_uInt16 nCol ) override;

        virtual void PaintCell( OutputDevice& rDev, const ::tools::Rectangle& rRect, sal_uInt16 nColId ) const override;

        virtual bool SeekRow( long nRow ) override;
        virtual bool SaveModified() override;

        virtual bool IsTabAllowed(bool _bForward) const override;

        virtual void StateChanged( StateChangedType nStateChange ) override;

        virtual void CursorMoved() override;

    protected:
        virtual sal_uInt32 GetTotalCellWidth(long nRow, sal_uInt16 nColId) override;


    private:
        OUString implGetCellText(const DriverPoolingSettings::const_iterator& _rPos, sal_uInt16 _nColId) const;
    };

    DriverListControl::DriverListControl(vcl::Window* _pParent)
        :EditBrowseBox(_pParent, EditBrowseBoxFlags::NO_HANDLE_COLUMN_CONTENT, WB_BORDER,
                       BrowserMode::AUTO_VSCROLL | BrowserMode::AUTO_HSCROLL | BrowserMode::HIDECURSOR | BrowserMode::AUTOSIZE_LASTCOL | BrowserMode::KEEPHIGHLIGHT)
        ,m_aSeekRow(m_aSettings.end())
        ,m_sYes(CuiResId(RID_SVXSTR_YES))
        ,m_sNo(CuiResId(RID_SVXSTR_NO))
    {
        SetStyle((GetStyle() & ~WB_HSCROLL) | WB_AUTOHSCROLL);
    }

    VCL_BUILDER_FACTORY(DriverListControl)

    bool DriverListControl::IsTabAllowed(bool /*_bForward*/) const
    {
        // no travelling within the fields via RETURN and TAB
        return false;
    }

    bool DriverListControl::isModified() 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;
    }


    void DriverListControl::Init()
    {
        EditBrowseBox::Init();

        Size aColWidth = LogicToPixel(Size(160, 0), MapMode(MapUnit::MapAppFont));
        InsertDataColumn(1, CuiResId(RID_SVXSTR_DRIVER_NAME), aColWidth.Width());
        aColWidth = LogicToPixel(Size(30, 0), MapMode(MapUnit::MapAppFont));
        InsertDataColumn(2, CuiResId(RID_SVXSTR_POOLED_FLAG), aColWidth.Width());
        aColWidth = LogicToPixel(Size(60, 0), MapMode(MapUnit::MapAppFont));
        InsertDataColumn(3, CuiResId(RID_SVXSTR_POOL_TIMEOUT), aColWidth.Width());
            // Attention: the resource of the string is local to the resource of the enclosing dialog!
    }


    void DriverListControl::CursorMoved()
    {
        EditBrowseBox::CursorMoved();

        // call the row change handler
        if ( m_aRowChangeHandler.IsSet() )
        {
            if ( GetCurRow() >= 0 )
            {   // == -1 may happen in case the browse box has just been cleared
                m_aRowChangeHandler.Call( getCurrentRow() );
            }
        }
    }

    DriverPooling* DriverListControl::getCurrentRow()
    {
        OSL_ENSURE( ( GetCurRow() < m_aSettings.size() ) && ( GetCurRow() >= 0 ),
            "DriverListControl::getCurrentRow: invalid current row!");

        if ( ( GetCurRow() >= 0 ) && ( GetCurRow() < m_aSettings.size() ) )
            return &(*(m_aSettings.begin() + GetCurRow()));

        return nullptr;
    }


    void DriverListControl::updateCurrentRow()
    {
        Window::Invalidate( GetRowRectPixel( GetCurRow() ), InvalidateFlags::Update );
    }


    void DriverListControl::Update(const DriverPoolingSettings& _rSettings)
    {
        m_aSettings = _rSettings;

        SetUpdateMode(false);
        RowRemoved(0, GetRowCount());
        RowInserted(0, m_aSettings.size());
        SetUpdateMode(true);

        ActivateCell(1, 0);
    }


    sal_uInt32 DriverListControl::GetTotalCellWidth(long nRow, sal_uInt16 nColId)
    {
        return GetDataWindow().GetTextWidth(GetCellText(nRow, nColId));
    }


    OUString DriverListControl::implGetCellText(const DriverPoolingSettings::const_iterator& _rPos, sal_uInt16 _nColId) const
    {
        OSL_ENSURE(_rPos < m_aSettings.end(), "DriverListControl::implGetCellText: invalid position!");

        OUString sReturn;
        switch (_nColId)
        {
            case 1:
                sReturn = _rPos->sName;
                break;
            case 2:
                sReturn = _rPos->bEnabled ? m_sYes : m_sNo;
                break;
            case 3:
                if (_rPos->bEnabled)
                    sReturn = OUString::number(_rPos->nTimeoutSeconds);
                break;
            default:
                OSL_FAIL("DriverListControl::implGetCellText: invalid column id!");
        }
        return sReturn;
    }


    void DriverListControl::StateChanged( StateChangedType nStateChange )
    {
        if (StateChangedType::Enable == nStateChange)
            Window::Invalidate(InvalidateFlags::Update);
        EditBrowseBox::StateChanged( nStateChange );
    }


    OUString DriverListControl::GetCellText( long nRow, sal_uInt16 nColId ) const
    {
        OUString sReturn;
        if (nRow > m_aSettings.size())
        {
            OSL_FAIL("DriverListControl::GetCellText: don't ask me for such rows!");
        }
        else
        {
            sReturn = implGetCellText(m_aSettings.begin() + nRow, nColId);
        }
        return sReturn;
    }


    void DriverListControl::InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol )
    {
        rController->GetWindow().SetText(GetCellText(nRow, nCol));
    }


    ::svt::CellController* DriverListControl::GetController( long /*nRow*/, sal_uInt16 /*nCol*/ )
    {
        return nullptr;
    }


    bool DriverListControl::SaveModified()
    {
        return true;
    }


    bool DriverListControl::SeekRow( long _nRow )
    {
        EditBrowseBox::SeekRow(_nRow);

        if (_nRow < m_aSettings.size())
            m_aSeekRow = m_aSettings.begin() + _nRow;
        else
            m_aSeekRow = m_aSettings.end();

        return m_aSeekRow != m_aSettings.end();
    }


    void DriverListControl::PaintCell( OutputDevice& rDev, const ::tools::Rectangle& rRect, sal_uInt16 nColId ) const
    {
        OSL_ENSURE(m_aSeekRow != m_aSettings.end(), "DriverListControl::PaintCell: invalid row!");

        if (m_aSeekRow != m_aSettings.end())
        {
            rDev.SetClipRegion(vcl::Region(rRect));

            DrawTextFlags nStyle = DrawTextFlags::Clip;
            if (!IsEnabled())
                nStyle |= DrawTextFlags::Disable;
            switch (nColId)
            {
                case 1: nStyle |= DrawTextFlags::Left; break;
                case 2:
                case 3: nStyle |= DrawTextFlags::Center; break;
            }

            rDev.DrawText(rRect, implGetCellText(m_aSeekRow, nColId), nStyle);

            rDev.SetClipRegion();
        }
    }

    ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(vcl::Window* _pParent, const SfxItemSet& _rAttrSet)
        : SfxTabPage(_pParent, "ConnPoolPage", "cui/ui/connpooloptions.ui", &_rAttrSet)
    {
        get(m_pEnablePooling, "connectionpooling");
        get(m_pDriversLabel, "driverslabel");
        get(m_pDriverList, "driverlist");
        get(m_pDriverLabel, "driverlabel");
        get(m_pDriver, "driver");
        get(m_pDriverPoolingEnabled, "enablepooling");
        get(m_pTimeoutLabel, "timeoutlabel");
        get(m_pTimeout, "timeout");

        Size aControlSize(248, 100);
        aControlSize = LogicToPixel(aControlSize, MapMode(MapUnit::MapAppFont));
        m_pDriverList->set_width_request(aControlSize.Width());
        m_pDriverList->set_height_request(aControlSize.Height());
        m_pDriverList->Init();
        m_pDriverList->Show();

        m_pEnablePooling->SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
        m_pDriverPoolingEnabled->SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );

        m_pDriverList->SetRowChangeHandler( LINK(this, ConnectionPoolOptionsPage, OnDriverRowChanged) );
    }

    ConnectionPoolOptionsPage::~ConnectionPoolOptionsPage()
    {
        disposeOnce();
    }

    void ConnectionPoolOptionsPage::dispose()
    {
        m_pEnablePooling.clear();
        m_pDriversLabel.clear();
        m_pDriverList.clear();
        m_pDriverLabel.clear();
        m_pDriver.clear();
        m_pDriverPoolingEnabled.clear();
        m_pTimeoutLabel.clear();
        m_pTimeout.clear();
        SfxTabPage::dispose();
    }

    VclPtr<SfxTabPage> ConnectionPoolOptionsPage::Create(TabPageParent _pParent, const SfxItemSet* _rAttrSet)
    {
        return VclPtr<ConnectionPoolOptionsPage>::Create(_pParent.pParent, *_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_pEnablePooling->Check(pEnabled == nullptr || pEnabled->GetValue());

        m_pEnablePooling->SaveValue();

        // the settings for the single drivers
        const DriverPoolingSettingsItem* pDriverSettings = _rSet.GetItem<DriverPoolingSettingsItem>(SID_SB_DRIVER_TIMEOUTS);
        if (pDriverSettings)
            m_pDriverList->Update(pDriverSettings->getSettings());
        else
        {
            OSL_FAIL("ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
            m_pDriverList->Update(DriverPoolingSettings());
        }
        m_pDriverList->saveValue();

        // reflect the new settings
        OnEnabledDisabled(m_pEnablePooling);
    }


    bool ConnectionPoolOptionsPage::EventNotify( NotifyEvent& _rNEvt )
    {
        if (MouseNotifyEvent::LOSEFOCUS == _rNEvt.GetType())
            if (m_pTimeout->IsWindowOrChild(_rNEvt.GetWindow()))
                commitTimeoutField();

        return SfxTabPage::EventNotify(_rNEvt);
    }


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

        bool bModified = false;
        // the enabled flag
        if (m_pEnablePooling->IsValueChangedFromSaved())
        {
            _rSet->Put(SfxBoolItem(SID_SB_POOLING_ENABLED, m_pEnablePooling->IsChecked()));
            bModified = true;
        }

        // the settings for the single drivers
        if (m_pDriverList->isModified())
        {
            _rSet->Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, m_pDriverList->getSettings()));
            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( ConnectionPoolOptionsPage, OnDriverRowChanged, const DriverPooling*, pDriverPos, void )<--- syntax error
    {
        bool bValidRow = (nullptr != pDriverPos);
        m_pDriverPoolingEnabled->Enable(bValidRow && m_pEnablePooling->IsChecked());
        m_pTimeoutLabel->Enable(bValidRow);
        m_pTimeout->Enable(bValidRow);

        if (!bValidRow)
        {   // positioned on an invalid row
            m_pDriver->SetText(OUString());
        }
        else
        {
            m_pDriver->SetText(pDriverPos->sName);
            m_pDriverPoolingEnabled->Check(pDriverPos->bEnabled);
            m_pTimeout->SetText(OUString::number(pDriverPos->nTimeoutSeconds));

            OnEnabledDisabled(m_pDriverPoolingEnabled);
        }
    }


    void ConnectionPoolOptionsPage::commitTimeoutField()
    {
        if (DriverPooling* pCurrentDriver = m_pDriverList->getCurrentRow())
        {
            pCurrentDriver->nTimeoutSeconds = static_cast<long>(m_pTimeout->GetValue());
            m_pDriverList->updateCurrentRow();
        }
    }


    IMPL_LINK( ConnectionPoolOptionsPage, OnEnabledDisabled, Button*, _pCheckBox, void )
    {
        bool bGloballyEnabled = m_pEnablePooling->IsChecked();
        bool bLocalDriverChanged = m_pDriverPoolingEnabled == _pCheckBox;

        if (m_pEnablePooling == _pCheckBox)
        {
            m_pDriversLabel->Enable(bGloballyEnabled);
            m_pDriverList->Enable(bGloballyEnabled);
            m_pDriverLabel->Enable(bGloballyEnabled);
            m_pDriver->Enable(bGloballyEnabled);
            m_pDriverPoolingEnabled->Enable(bGloballyEnabled);
        }
        else
            OSL_ENSURE(bLocalDriverChanged, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");

        m_pTimeoutLabel->Enable(bGloballyEnabled && m_pDriverPoolingEnabled->IsChecked());
        m_pTimeout->Enable(bGloballyEnabled && m_pDriverPoolingEnabled->IsChecked());

        if (bLocalDriverChanged)
        {
            // update the list
            m_pDriverList->getCurrentRow()->bEnabled = m_pDriverPoolingEnabled->IsChecked();
            m_pDriverList->updateCurrentRow();
        }
    }

} // namespace offapp

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