Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cui / source / options / dbregister.cxx
blob92cbbd0012e97b1acb5829740b706588dbb3f3f8
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::lang;
49 using namespace ::com::sun::star::ui::dialogs;
50 using namespace ::com::sun::star::uno;
51 using namespace ::svt;
53 // class RegistrationItemSetHolder -------------------------------------------------
55 RegistrationItemSetHolder::RegistrationItemSetHolder( SfxItemSet _aMasterSet )
56 :m_aRegistrationItems(std::move( _aMasterSet ))
58 DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
61 RegistrationItemSetHolder::~RegistrationItemSetHolder()
65 // class DatabaseRegistrationDialog ------------------------------------------------
67 DatabaseRegistrationDialog::DatabaseRegistrationDialog(weld::Window* pParent, const SfxItemSet& rInAttrs)
68 : RegistrationItemSetHolder(rInAttrs)
69 , SfxSingleTabDialogController(pParent, &getRegistrationItems())
71 SetTabPage(DbRegistrationOptionsPage::Create(get_content_area(), this, &getRegistrationItems()));
72 m_xDialog->set_title(CuiResId(RID_CUISTR_REGISTERED_DATABASES));
75 short DatabaseRegistrationDialog::run()
77 short result = SfxSingleTabDialogController::run();
78 if (result == RET_OK)
80 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
81 if ( GetOutputItemSet() )
82 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
84 return result;
87 // class DbRegistrationOptionsPage --------------------------------------------------
89 DbRegistrationOptionsPage::DbRegistrationOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
90 : SfxTabPage(pPage, pController, "cui/ui/dbregisterpage.ui", "DbRegisterPage", &rSet)
91 , m_nOldCount(0)
92 , m_bModified(false)
93 , m_xNew(m_xBuilder->weld_button("new"))
94 , m_xEdit(m_xBuilder->weld_button("edit"))
95 , m_xDelete(m_xBuilder->weld_button("delete"))
96 , m_xPathBox(m_xBuilder->weld_tree_view("pathctrl"))
97 , m_xIter(m_xPathBox->make_iterator())
99 Size aControlSize(m_xPathBox->get_approximate_digit_width() * 60,
100 m_xPathBox->get_height_rows(12));
101 m_xPathBox->set_size_request(aControlSize.Width(), aControlSize.Height());
103 std::vector<int> aWidths
105 o3tl::narrowing<int>(m_xPathBox->get_approximate_digit_width() * 20)
107 m_xPathBox->set_column_fixed_widths(aWidths);
109 m_xNew->connect_clicked( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
110 m_xEdit->connect_clicked( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
111 m_xDelete->connect_clicked( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
113 m_xPathBox->connect_column_clicked(LINK(this, DbRegistrationOptionsPage, HeaderSelect_Impl));
115 m_xPathBox->make_sorted();
116 m_xPathBox->connect_row_activated( LINK( this, DbRegistrationOptionsPage, PathBoxDoubleClickHdl ) );
117 m_xPathBox->connect_changed( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
119 m_xPathBox->set_help_id(HID_DBPATH_CTL_PATH);
122 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
124 for (int i = 0, nCount = m_xPathBox->n_children(); i < nCount; ++i )
125 delete weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(i));
128 std::unique_ptr<SfxTabPage> DbRegistrationOptionsPage::Create( weld::Container* pPage, weld::DialogController* pController,
129 const SfxItemSet* rAttrSet )
131 return std::make_unique<DbRegistrationOptionsPage>(pPage, pController, *rAttrSet);
134 bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet* rCoreSet )
136 // the settings for the single drivers
137 bool bModified = false;
138 DatabaseRegistrations aRegistrations;
139 int nCount = m_xPathBox->n_children();
140 for (int i = 0; i < nCount; ++i)
142 DatabaseRegistration* pRegistration = weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(i));
143 if ( pRegistration && !pRegistration->sLocation.isEmpty() )
145 OUString sName(m_xPathBox->get_text(i, 0));
146 OFileNotation aTransformer( pRegistration->sLocation );
147 aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
150 if ( m_nOldCount != aRegistrations.size() || m_bModified )
152 rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, std::move(aRegistrations) ));
153 bModified = true;
156 return bModified;
159 void DbRegistrationOptionsPage::Reset( const SfxItemSet* rSet )
161 // the settings for the single drivers
162 const DatabaseMapItem* pRegistrations = rSet->GetItem<DatabaseMapItem>(SID_SB_DB_REGISTER);
163 if ( !pRegistrations )
164 return;
166 m_xPathBox->clear();
168 const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
169 m_nOldCount = rRegistrations.size();
170 for (auto const& elem : rRegistrations)
172 OFileNotation aTransformer( elem.second.sLocation );
173 insertNewEntry( elem.first, aTransformer.get( OFileNotation::N_SYSTEM ), elem.second.bReadOnly );
176 OUString aUserData = GetUserData();
177 if ( aUserData.isEmpty() )
178 return;
180 sal_Int32 nIdx {0};
181 // restore column width
182 std::vector<int> aWidths
184 o3tl::toInt32(o3tl::getToken(aUserData, 0, ';', nIdx))
186 m_xPathBox->set_column_fixed_widths(aWidths);
187 // restore sort direction
188 bool bUp = o3tl::toInt32(o3tl::getToken(aUserData, 0, ';', nIdx)) != 0;
189 m_xPathBox->set_sort_order(bUp);
190 m_xPathBox->set_sort_indicator(bUp ? TRISTATE_TRUE : TRISTATE_FALSE, COL_TYPE);
193 void DbRegistrationOptionsPage::FillUserData()
195 OUString aUserData = OUString::number( m_xPathBox->get_column_width(COL_TYPE) ) + ";";
196 bool bUp = m_xPathBox->get_sort_order();
197 aUserData += (bUp ? std::u16string_view(u"1") : std::u16string_view(u"0"));
198 SetUserData( aUserData );
201 IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl, weld::Button&, void)
203 int nEntry = m_xPathBox->get_selected_index();
204 if (nEntry != -1)
206 std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(GetFrameWeld(),
207 VclMessageType::Question, VclButtonsType::YesNo, CuiResId(RID_CUISTR_QUERY_DELETE_CONFIRM)));
208 if (xQuery->run() == RET_YES)
209 m_xPathBox->remove(nEntry);
213 IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl, weld::Button&, void)
215 openLinkDialog(OUString(),OUString());
218 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathBoxDoubleClickHdl, weld::TreeView&, bool)
220 EditHdl(*m_xEdit);
221 return true;
224 IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl, weld::Button&, void)
226 int nEntry = m_xPathBox->get_selected_index();
227 if (nEntry == -1)
228 return;
230 DatabaseRegistration* pOldRegistration = weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(nEntry));
231 if (!pOldRegistration || pOldRegistration->bReadOnly)
232 return;
234 OUString sOldName = m_xPathBox->get_text(nEntry, 0);
235 openLinkDialog(sOldName, pOldRegistration->sLocation, nEntry);
238 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, int, nCol, void )
240 if (nCol != COL_TYPE)
241 return;
243 bool bSortMode = m_xPathBox->get_sort_order();
245 //set new arrow positions in headerbar
246 bSortMode = !bSortMode;
247 m_xPathBox->set_sort_order(bSortMode);
249 //sort lists
250 m_xPathBox->set_sort_indicator(bSortMode ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
253 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl, weld::TreeView&, void)
255 DatabaseRegistration* pRegistration = weld::fromId<DatabaseRegistration*>(m_xPathBox->get_selected_id());
257 bool bReadOnly = true;
258 if (pRegistration)
260 bReadOnly = pRegistration->bReadOnly;
263 m_xEdit->set_sensitive( !bReadOnly );
264 m_xDelete->set_sensitive( !bReadOnly );
267 void DbRegistrationOptionsPage::insertNewEntry(const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly)
269 OUString sId(weld::toId(new DatabaseRegistration(_sLocation, _bReadOnly)));
270 m_xPathBox->insert(nullptr, -1, &_sName, &sId, nullptr, nullptr, false, m_xIter.get());
272 if (_bReadOnly)
273 m_xPathBox->set_image(*m_xIter, RID_SVXBMP_LOCK);
275 m_xPathBox->set_text(*m_xIter, _sLocation, 1);
278 void DbRegistrationOptionsPage::openLinkDialog(const OUString& sOldName, const OUString& sOldLocation, int nEntry)
280 ODocumentLinkDialog aDlg(GetFrameWeld(), nEntry == -1);
282 aDlg.setLink(sOldName, sOldLocation);
284 // tdf#149195: control the name (ie check duplicate) only if new entry case
285 if (nEntry == -1)
286 aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
288 if (aDlg.run() != RET_OK)
289 return;
291 OUString sNewName,sNewLocation;
292 aDlg.getLink(sNewName,sNewLocation);
293 if ( nEntry == -1 || sNewName != sOldName || sNewLocation != sOldLocation )
295 if (nEntry != -1)
297 delete weld::fromId<DatabaseRegistration*>(m_xPathBox->get_id(nEntry));
298 m_xPathBox->remove(nEntry);
300 insertNewEntry( sNewName, sNewLocation, false );
301 m_bModified = true;
305 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, const OUString&, _rName, bool )
307 int nCount = m_xPathBox->n_children();
308 for (int i = 0; i < nCount; ++i)
310 if (m_xPathBox->get_text(i, 0) == _rName)
311 return false;
313 return true;
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */