Use correct object
[LibreOffice.git] / cui / source / options / dbregister.cxx
blob45c8398d23a6691aeb24948b39b03205c630e1d8
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 <sal/config.h>
22 #include <string_view>
24 #include <dbregister.hxx>
25 #include "dbregistersettings.hxx"
26 #include <o3tl/safeint.hxx>
27 #include <svl/filenotation.hxx>
28 #include <helpids.h>
29 #include <tools/debug.hxx>
30 #include <strings.hrc>
31 #include <bitmaps.hlst>
32 #include <utility>
33 #include <vcl/svapp.hxx>
34 #include <vcl/weld.hxx>
35 #include <svl/itemset.hxx>
36 #include "doclinkdialog.hxx"
37 #include <dialmgr.hxx>
38 #include "dbregisterednamesconfig.hxx"
39 #include <svx/databaseregistrationui.hxx>
40 #include <o3tl/string_view.hxx>
42 #define COL_TYPE 0
44 namespace svx
48 using namespace ::com::sun::star::ui::dialogs;
49 using namespace ::svt;
51 // class RegistrationItemSetHolder -------------------------------------------------
53 RegistrationItemSetHolder::RegistrationItemSetHolder( SfxItemSet _aMasterSet )
54 :m_aRegistrationItems(std::move( _aMasterSet ))
56 DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
59 RegistrationItemSetHolder::~RegistrationItemSetHolder()
63 // class DatabaseRegistrationDialog ------------------------------------------------
65 DatabaseRegistrationDialog::DatabaseRegistrationDialog(weld::Window* pParent, const SfxItemSet& rInAttrs)
66 : RegistrationItemSetHolder(rInAttrs)
67 , SfxSingleTabDialogController(pParent, &getRegistrationItems())
69 SetTabPage(DbRegistrationOptionsPage::Create(get_content_area(), this, &getRegistrationItems()));
70 m_xDialog->set_title(CuiResId(RID_CUISTR_REGISTERED_DATABASES));
73 short DatabaseRegistrationDialog::run()
75 short result = SfxSingleTabDialogController::run();
76 if (result == RET_OK)
78 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
79 if ( GetOutputItemSet() )
80 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
82 return result;
85 // class DbRegistrationOptionsPage --------------------------------------------------
87 DbRegistrationOptionsPage::DbRegistrationOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
88 : SfxTabPage(pPage, pController, u"cui/ui/dbregisterpage.ui"_ustr, u"DbRegisterPage"_ustr, &rSet)
89 , m_nOldCount(0)
90 , m_bModified(false)
91 , m_xNew(m_xBuilder->weld_button(u"new"_ustr))
92 , m_xEdit(m_xBuilder->weld_button(u"edit"_ustr))
93 , m_xDelete(m_xBuilder->weld_button(u"delete"_ustr))
94 , m_xPathBox(m_xBuilder->weld_tree_view(u"pathctrl"_ustr))
95 , m_xIter(m_xPathBox->make_iterator())
97 Size aControlSize(m_xPathBox->get_approximate_digit_width() * 60,
98 m_xPathBox->get_height_rows(12));
99 m_xPathBox->set_size_request(aControlSize.Width(), aControlSize.Height());
101 std::vector<int> aWidths
103 o3tl::narrowing<int>(m_xPathBox->get_approximate_digit_width() * 20)
105 m_xPathBox->set_column_fixed_widths(aWidths);
107 m_xNew->connect_clicked( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
108 m_xEdit->connect_clicked( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
109 m_xDelete->connect_clicked( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
111 m_xPathBox->connect_column_clicked(LINK(this, DbRegistrationOptionsPage, HeaderSelect_Impl));
113 m_xPathBox->make_sorted();
114 m_xPathBox->connect_row_activated( LINK( this, DbRegistrationOptionsPage, PathBoxDoubleClickHdl ) );
115 m_xPathBox->connect_selection_changed(LINK(this, DbRegistrationOptionsPage, PathSelect_Impl));
117 m_xPathBox->set_help_id(HID_DBPATH_CTL_PATH);
120 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
122 for (int i = 0, nCount = m_xPathBox->n_children(); i < nCount; ++i )
123 delete weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(i));
126 std::unique_ptr<SfxTabPage> DbRegistrationOptionsPage::Create( weld::Container* pPage, weld::DialogController* pController,
127 const SfxItemSet* rAttrSet )
129 return std::make_unique<DbRegistrationOptionsPage>(pPage, pController, *rAttrSet);
132 OUString DbRegistrationOptionsPage::GetAllStrings()
134 OUString sAllStrings;
136 if (const auto pString = m_xBuilder->weld_label(u"label1"_ustr))
137 sAllStrings += pString->get_label() + " ";
139 return sAllStrings.replaceAll("_", "");
142 bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet* rCoreSet )
144 // the settings for the single drivers
145 bool bModified = false;
146 DatabaseRegistrations aRegistrations;
147 int nCount = m_xPathBox->n_children();
148 for (int i = 0; i < nCount; ++i)
150 DatabaseRegistration* pRegistration = weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(i));
151 if ( pRegistration && !pRegistration->sLocation.isEmpty() )
153 OUString sName(m_xPathBox->get_text(i, 0));
154 OFileNotation aTransformer( pRegistration->sLocation );
155 aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
158 if ( m_nOldCount != aRegistrations.size() || m_bModified )
160 rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, std::move(aRegistrations) ));
161 bModified = true;
164 return bModified;
167 void DbRegistrationOptionsPage::Reset( const SfxItemSet* rSet )
169 // the settings for the single drivers
170 const DatabaseMapItem* pRegistrations = rSet->GetItem<DatabaseMapItem>(SID_SB_DB_REGISTER);
171 if ( !pRegistrations )
172 return;
174 m_xPathBox->clear();
176 const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
177 m_nOldCount = rRegistrations.size();
178 for (auto const& elem : rRegistrations)
180 OFileNotation aTransformer( elem.second.sLocation );
181 insertNewEntry( elem.first, aTransformer.get( OFileNotation::N_SYSTEM ), elem.second.bReadOnly );
184 OUString aUserData = GetUserData();
185 if ( aUserData.isEmpty() )
186 return;
188 sal_Int32 nIdx {0};
189 // restore column width
190 std::vector<int> aWidths
192 o3tl::toInt32(o3tl::getToken(aUserData, 0, ';', nIdx))
194 m_xPathBox->set_column_fixed_widths(aWidths);
195 // restore sort direction
196 bool bUp = o3tl::toInt32(o3tl::getToken(aUserData, 0, ';', nIdx)) != 0;
197 m_xPathBox->set_sort_order(bUp);
198 m_xPathBox->set_sort_indicator(bUp ? TRISTATE_TRUE : TRISTATE_FALSE, COL_TYPE);
201 void DbRegistrationOptionsPage::FillUserData()
203 OUString aUserData = OUString::number( m_xPathBox->get_column_width(COL_TYPE) ) + ";";
204 bool bUp = m_xPathBox->get_sort_order();
205 aUserData += (bUp ? std::u16string_view(u"1") : std::u16string_view(u"0"));
206 SetUserData( aUserData );
209 IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl, weld::Button&, void)
211 int nEntry = m_xPathBox->get_selected_index();
212 if (nEntry != -1)
214 std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(GetFrameWeld(),
215 VclMessageType::Question, VclButtonsType::YesNo, CuiResId(RID_CUISTR_QUERY_DELETE_CONFIRM)));
216 if (xQuery->run() == RET_YES)
217 m_xPathBox->remove(nEntry);
221 IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl, weld::Button&, void)
223 openLinkDialog(OUString(),OUString());
226 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathBoxDoubleClickHdl, weld::TreeView&, bool)
228 EditHdl(*m_xEdit);
229 return true;
232 IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl, weld::Button&, void)
234 int nEntry = m_xPathBox->get_selected_index();
235 if (nEntry == -1)
236 return;
238 DatabaseRegistration* pOldRegistration = weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(nEntry));
239 if (!pOldRegistration || pOldRegistration->bReadOnly)
240 return;
242 OUString sOldName = m_xPathBox->get_text(nEntry, 0);
243 openLinkDialog(sOldName, pOldRegistration->sLocation, nEntry);
246 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, int, nCol, void )
248 if (nCol != COL_TYPE)
249 return;
251 bool bSortMode = m_xPathBox->get_sort_order();
253 //set new arrow positions in headerbar
254 bSortMode = !bSortMode;
255 m_xPathBox->set_sort_order(bSortMode);
257 //sort lists
258 m_xPathBox->set_sort_indicator(bSortMode ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
261 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl, weld::TreeView&, void)
263 DatabaseRegistration* pRegistration = weld::fromId<DatabaseRegistration*>(m_xPathBox->get_selected_id());
265 bool bReadOnly = true;
266 if (pRegistration)
268 bReadOnly = pRegistration->bReadOnly;
271 m_xEdit->set_sensitive( !bReadOnly );
272 m_xDelete->set_sensitive( !bReadOnly );
275 void DbRegistrationOptionsPage::insertNewEntry(const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly)
277 OUString sId(weld::toId(new DatabaseRegistration(_sLocation, _bReadOnly)));
278 m_xPathBox->insert(nullptr, -1, &_sName, &sId, nullptr, nullptr, false, m_xIter.get());
280 if (_bReadOnly)
281 m_xPathBox->set_image(*m_xIter, RID_SVXBMP_LOCK);
283 m_xPathBox->set_text(*m_xIter, _sLocation, 1);
286 void DbRegistrationOptionsPage::openLinkDialog(const OUString& sOldName, const OUString& sOldLocation, int nEntry)
288 ODocumentLinkDialog aDlg(GetFrameWeld(), nEntry == -1);
290 aDlg.setLink(sOldName, sOldLocation);
292 // tdf#149195: control the name (ie check duplicate) only if new entry case
293 if (nEntry == -1)
294 aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
296 if (aDlg.run() != RET_OK)
297 return;
299 OUString sNewName,sNewLocation;
300 aDlg.getLink(sNewName,sNewLocation);
301 if ( nEntry == -1 || sNewName != sOldName || sNewLocation != sOldLocation )
303 if (nEntry != -1)
305 delete weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(nEntry));
306 m_xPathBox->remove(nEntry);
308 insertNewEntry( sNewName, sNewLocation, false );
309 m_bModified = true;
313 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, const OUString&, _rName, bool )
315 int nCount = m_xPathBox->n_children();
316 for (int i = 0; i < nCount; ++i)
318 if (m_xPathBox->get_text(i, 0) == _rName)
319 return false;
321 return true;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */