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
/* -*- 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 <dbregister.hxx>
#include "dbregistersettings.hxx"
#include <svl/filenotation.hxx>
#include <helpids.h>
#include <tools/debug.hxx>
#include <strings.hrc>
#include <bitmaps.hlst>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <svl/itemset.hxx>
#include "doclinkdialog.hxx"
#include <dialmgr.hxx>
#include "dbregisterednamesconfig.hxx"
#include <svx/databaseregistrationui.hxx>

#define COL_TYPE       0

namespace svx
{


using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::ui::dialogs;
using namespace ::com::sun::star::uno;
using namespace ::svt;

// class RegistrationItemSetHolder  -------------------------------------------------

RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet& _rMasterSet )
    :m_aRegistrationItems( _rMasterSet )
{
    DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
}

RegistrationItemSetHolder::~RegistrationItemSetHolder()
{
}

// class DatabaseRegistrationDialog  ------------------------------------------------

DatabaseRegistrationDialog::DatabaseRegistrationDialog(weld::Window* pParent, const SfxItemSet& rInAttrs)
    : RegistrationItemSetHolder(rInAttrs)
    , SfxSingleTabDialogController(pParent, &getRegistrationItems())
{
    SetTabPage(DbRegistrationOptionsPage::Create(get_content_area(), this, &getRegistrationItems()));
    m_xDialog->set_title(CuiResId(RID_SVXSTR_REGISTERED_DATABASES));
}

short DatabaseRegistrationDialog::run()
{
    short result = SfxSingleTabDialogController::run();
    if (result == RET_OK)
    {
        DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
        if ( GetOutputItemSet() )
            DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
    }
    return result;
}

// class DbRegistrationOptionsPage --------------------------------------------------

DbRegistrationOptionsPage::DbRegistrationOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
    : SfxTabPage(pPage, pController, "cui/ui/dbregisterpage.ui", "DbRegisterPage", &rSet)
    , m_nOldCount(0)
    , m_bModified(false)
    , m_xNew(m_xBuilder->weld_button("new"))
    , m_xEdit(m_xBuilder->weld_button("edit"))
    , m_xDelete(m_xBuilder->weld_button("delete"))
    , m_xPathBox(m_xBuilder->weld_tree_view("pathctrl"))
    , m_xIter(m_xPathBox->make_iterator())
{
    Size aControlSize(m_xPathBox->get_approximate_digit_width() * 60,
                      m_xPathBox->get_height_rows(12));
    m_xPathBox->set_size_request(aControlSize.Width(), aControlSize.Height());

    std::vector<int> aWidths;
    aWidths.push_back(m_xPathBox->get_approximate_digit_width() * 20);
    m_xPathBox->set_column_fixed_widths(aWidths);

    m_xNew->connect_clicked( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
    m_xEdit->connect_clicked( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
    m_xDelete->connect_clicked( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );

    m_xPathBox->connect_column_clicked(LINK(this, DbRegistrationOptionsPage, HeaderSelect_Impl));

    m_xPathBox->make_sorted();
    m_xPathBox->connect_row_activated( LINK( this, DbRegistrationOptionsPage, PathBoxDoubleClickHdl ) );
    m_xPathBox->connect_changed( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );

    m_xPathBox->set_help_id(HID_DBPATH_CTL_PATH);
}

DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
{
    for (int i = 0, nCount = m_xPathBox->n_children(); i < nCount; ++i )
        delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
}

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

bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet* rCoreSet )
{
    // the settings for the single drivers
    bool bModified = false;
    DatabaseRegistrations aRegistrations;
    int nCount = m_xPathBox->n_children();
    for (int i = 0; i < nCount; ++i)
    {
        DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
        if ( pRegistration && !pRegistration->sLocation.isEmpty() )
        {
            OUString sName(m_xPathBox->get_text(i, 0));
            OFileNotation aTransformer( pRegistration->sLocation );
            aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
        }
    }
    if ( m_nOldCount != aRegistrations.size() || m_bModified )
    {
        rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ));
        bModified = true;
    }

    return bModified;
}

void DbRegistrationOptionsPage::Reset( const SfxItemSet* rSet )
{
    // the settings for the single drivers
    const DatabaseMapItem* pRegistrations = rSet->GetItem<DatabaseMapItem>(SID_SB_DB_REGISTER);
    if ( !pRegistrations )
        return;

    m_xPathBox->clear();

    const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
    m_nOldCount = rRegistrations.size();
    for (auto const& elem : rRegistrations)
    {
        OFileNotation aTransformer( elem.second.sLocation );
        insertNewEntry( elem.first, aTransformer.get( OFileNotation::N_SYSTEM ), elem.second.bReadOnly );
    }

    OUString aUserData = GetUserData();
    if ( !aUserData.isEmpty() )
    {
        sal_Int32 nIdx {0};
        // restore column width
        std::vector<int> aWidths;
        aWidths.push_back(aUserData.getToken(0, ';', nIdx).toInt32());
        m_xPathBox->set_column_fixed_widths(aWidths);
        // restore sort direction
        bool bUp = aUserData.getToken(0, ';', nIdx).toInt32() != 0;
        m_xPathBox->set_sort_order(bUp);
        m_xPathBox->set_sort_indicator(bUp ? TRISTATE_TRUE : TRISTATE_FALSE, COL_TYPE);
    }
}

void DbRegistrationOptionsPage::FillUserData()
{
    OUString aUserData = OUString::number( m_xPathBox->get_column_width(COL_TYPE) ) + ";";
    bool bUp = m_xPathBox->get_sort_order();
    aUserData += (bUp ? OUStringLiteral("1") : OUStringLiteral("0"));
    SetUserData( aUserData );
}

IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl, weld::Button&, void)<--- syntax error
{
    int nEntry = m_xPathBox->get_selected_index();
    if (nEntry != -1)
    {
        std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(GetFrameWeld(),
                                                    VclMessageType::Question, VclButtonsType::YesNo, CuiResId(RID_SVXSTR_QUERY_DELETE_CONFIRM)));
        if (xQuery->run() == RET_YES)
            m_xPathBox->remove(nEntry);
    }
}

IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl, weld::Button&, void)
{
    openLinkDialog(OUString(),OUString());
}

IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathBoxDoubleClickHdl, weld::TreeView&, bool)
{
    EditHdl(*m_xEdit);
    return true;
}

IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl, weld::Button&, void)
{
    int nEntry = m_xPathBox->get_selected_index();
    if (nEntry == -1)
        return;

    DatabaseRegistration* pOldRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
    if (!pOldRegistration || pOldRegistration->bReadOnly)
        return;

    OUString sOldName = m_xPathBox->get_text(nEntry, 0);
    openLinkDialog(sOldName, pOldRegistration->sLocation, nEntry);
}

IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, int, nCol, void )
{
    if (nCol != COL_TYPE)
        return;

    bool bSortMode = m_xPathBox->get_sort_order();

    //set new arrow positions in headerbar
    bSortMode = !bSortMode;
    m_xPathBox->set_sort_order(bSortMode);

    //sort lists
    m_xPathBox->set_sort_indicator(bSortMode ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
}

IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl, weld::TreeView&, void)
{
    DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_selected_id().toInt64());

    bool bReadOnly = true;
    if (pRegistration)
    {
        bReadOnly = pRegistration->bReadOnly;
    }

    m_xEdit->set_sensitive( !bReadOnly );
    m_xDelete->set_sensitive( !bReadOnly );
}

void DbRegistrationOptionsPage::insertNewEntry(const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly)
{
    OUString sId(OUString::number(reinterpret_cast<sal_Int64>(new DatabaseRegistration(_sLocation, _bReadOnly))));
    m_xPathBox->insert(nullptr, -1, &_sName, &sId, nullptr, nullptr, false, m_xIter.get());

    if (_bReadOnly)
        m_xPathBox->set_image(*m_xIter, RID_SVXBMP_LOCK);

    m_xPathBox->set_text(*m_xIter, _sLocation, 1);
}

void DbRegistrationOptionsPage::openLinkDialog(const OUString& sOldName, const OUString& sOldLocation, int nEntry)
{
    ODocumentLinkDialog aDlg(GetFrameWeld(), nEntry == -1);

    aDlg.setLink(sOldName, sOldLocation);
    aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );

    if (aDlg.run() != RET_OK)
        return;

    OUString sNewName,sNewLocation;
    aDlg.getLink(sNewName,sNewLocation);
    if ( nEntry == -1 || sNewName != sOldName || sNewLocation != sOldLocation )
    {
        if (nEntry != -1)
        {
            delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
            m_xPathBox->remove(nEntry);
        }
        insertNewEntry( sNewName, sNewLocation, false );
        m_bModified = true;
    }
}

IMPL_LINK( DbRegistrationOptionsPage, NameValidator, const OUString&, _rName, bool )
{
    int nCount = m_xPathBox->n_children();
    for (int i = 0; i < nCount; ++i)
    {
        if (m_xPathBox->get_text(i, 0) == _rName)
            return false;
    }
    return true;
}

}

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