Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cui / source / options / dbregister.cxx
blob24df2e4930c6e6bd8d9d8d9974eb6b1c4d90fe1e
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 <dbregister.hxx>
21 #include "dbregistersettings.hxx"
22 #include <svl/filenotation.hxx>
23 #include <helpids.h>
24 #include <tools/debug.hxx>
25 #include <strings.hrc>
26 #include <bitmaps.hlst>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weld.hxx>
29 #include <svl/itemset.hxx>
30 #include "doclinkdialog.hxx"
31 #include <dialmgr.hxx>
32 #include "dbregisterednamesconfig.hxx"
33 #include <svx/databaseregistrationui.hxx>
35 #define COL_TYPE 0
37 namespace svx
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::ui::dialogs;
43 using namespace ::com::sun::star::uno;
44 using namespace ::svt;
46 // class RegistrationItemSetHolder -------------------------------------------------
48 RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet& _rMasterSet )
49 :m_aRegistrationItems( _rMasterSet )
51 DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
54 RegistrationItemSetHolder::~RegistrationItemSetHolder()
58 // class DatabaseRegistrationDialog ------------------------------------------------
60 DatabaseRegistrationDialog::DatabaseRegistrationDialog(weld::Window* pParent, const SfxItemSet& rInAttrs)
61 : RegistrationItemSetHolder(rInAttrs)
62 , SfxSingleTabDialogController(pParent, &getRegistrationItems())
64 SetTabPage(DbRegistrationOptionsPage::Create(get_content_area(), this, &getRegistrationItems()));
65 m_xDialog->set_title(CuiResId(RID_SVXSTR_REGISTERED_DATABASES));
68 short DatabaseRegistrationDialog::run()
70 short result = SfxSingleTabDialogController::run();
71 if (result == RET_OK)
73 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
74 if ( GetOutputItemSet() )
75 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
77 return result;
80 // class DbRegistrationOptionsPage --------------------------------------------------
82 DbRegistrationOptionsPage::DbRegistrationOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
83 : SfxTabPage(pPage, pController, "cui/ui/dbregisterpage.ui", "DbRegisterPage", &rSet)
84 , m_nOldCount(0)
85 , m_bModified(false)
86 , m_xNew(m_xBuilder->weld_button("new"))
87 , m_xEdit(m_xBuilder->weld_button("edit"))
88 , m_xDelete(m_xBuilder->weld_button("delete"))
89 , m_xPathBox(m_xBuilder->weld_tree_view("pathctrl"))
90 , m_xIter(m_xPathBox->make_iterator())
92 Size aControlSize(m_xPathBox->get_approximate_digit_width() * 60,
93 m_xPathBox->get_height_rows(12));
94 m_xPathBox->set_size_request(aControlSize.Width(), aControlSize.Height());
96 std::vector<int> aWidths;
97 aWidths.push_back(m_xPathBox->get_approximate_digit_width() * 20);
98 m_xPathBox->set_column_fixed_widths(aWidths);
100 m_xNew->connect_clicked( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
101 m_xEdit->connect_clicked( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
102 m_xDelete->connect_clicked( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
104 m_xPathBox->connect_column_clicked(LINK(this, DbRegistrationOptionsPage, HeaderSelect_Impl));
106 m_xPathBox->make_sorted();
107 m_xPathBox->connect_row_activated( LINK( this, DbRegistrationOptionsPage, PathBoxDoubleClickHdl ) );
108 m_xPathBox->connect_changed( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
110 m_xPathBox->set_help_id(HID_DBPATH_CTL_PATH);
113 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
115 for (int i = 0, nCount = m_xPathBox->n_children(); i < nCount; ++i )
116 delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
119 std::unique_ptr<SfxTabPage> DbRegistrationOptionsPage::Create( weld::Container* pPage, weld::DialogController* pController,
120 const SfxItemSet* rAttrSet )
122 return std::make_unique<DbRegistrationOptionsPage>(pPage, pController, *rAttrSet);
125 bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet* rCoreSet )
127 // the settings for the single drivers
128 bool bModified = false;
129 DatabaseRegistrations aRegistrations;
130 int nCount = m_xPathBox->n_children();
131 for (int i = 0; i < nCount; ++i)
133 DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
134 if ( pRegistration && !pRegistration->sLocation.isEmpty() )
136 OUString sName(m_xPathBox->get_text(i, 0));
137 OFileNotation aTransformer( pRegistration->sLocation );
138 aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
141 if ( m_nOldCount != aRegistrations.size() || m_bModified )
143 rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ));
144 bModified = true;
147 return bModified;
150 void DbRegistrationOptionsPage::Reset( const SfxItemSet* rSet )
152 // the settings for the single drivers
153 const DatabaseMapItem* pRegistrations = rSet->GetItem<DatabaseMapItem>(SID_SB_DB_REGISTER);
154 if ( !pRegistrations )
155 return;
157 m_xPathBox->clear();
159 const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
160 m_nOldCount = rRegistrations.size();
161 for (auto const& elem : rRegistrations)
163 OFileNotation aTransformer( elem.second.sLocation );
164 insertNewEntry( elem.first, aTransformer.get( OFileNotation::N_SYSTEM ), elem.second.bReadOnly );
167 OUString aUserData = GetUserData();
168 if ( !aUserData.isEmpty() )
170 sal_Int32 nIdx {0};
171 // restore column width
172 std::vector<int> aWidths;
173 aWidths.push_back(aUserData.getToken(0, ';', nIdx).toInt32());
174 m_xPathBox->set_column_fixed_widths(aWidths);
175 // restore sort direction
176 bool bUp = aUserData.getToken(0, ';', nIdx).toInt32() != 0;
177 m_xPathBox->set_sort_order(bUp);
178 m_xPathBox->set_sort_indicator(bUp ? TRISTATE_TRUE : TRISTATE_FALSE, COL_TYPE);
182 void DbRegistrationOptionsPage::FillUserData()
184 OUString aUserData = OUString::number( m_xPathBox->get_column_width(COL_TYPE) ) + ";";
185 bool bUp = m_xPathBox->get_sort_order();
186 aUserData += (bUp ? OUStringLiteral("1") : OUStringLiteral("0"));
187 SetUserData( aUserData );
190 IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl, weld::Button&, void)
192 int nEntry = m_xPathBox->get_selected_index();
193 if (nEntry != -1)
195 std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(GetFrameWeld(),
196 VclMessageType::Question, VclButtonsType::YesNo, CuiResId(RID_SVXSTR_QUERY_DELETE_CONFIRM)));
197 if (xQuery->run() == RET_YES)
198 m_xPathBox->remove(nEntry);
202 IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl, weld::Button&, void)
204 openLinkDialog(OUString(),OUString());
207 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathBoxDoubleClickHdl, weld::TreeView&, bool)
209 EditHdl(*m_xEdit);
210 return true;
213 IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl, weld::Button&, void)
215 int nEntry = m_xPathBox->get_selected_index();
216 if (nEntry == -1)
217 return;
219 DatabaseRegistration* pOldRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
220 if (!pOldRegistration || pOldRegistration->bReadOnly)
221 return;
223 OUString sOldName = m_xPathBox->get_text(nEntry, 0);
224 openLinkDialog(sOldName, pOldRegistration->sLocation, nEntry);
227 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, int, nCol, void )
229 if (nCol != COL_TYPE)
230 return;
232 bool bSortMode = m_xPathBox->get_sort_order();
234 //set new arrow positions in headerbar
235 bSortMode = !bSortMode;
236 m_xPathBox->set_sort_order(bSortMode);
238 //sort lists
239 m_xPathBox->set_sort_indicator(bSortMode ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
242 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl, weld::TreeView&, void)
244 DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_selected_id().toInt64());
246 bool bReadOnly = true;
247 if (pRegistration)
249 bReadOnly = pRegistration->bReadOnly;
252 m_xEdit->set_sensitive( !bReadOnly );
253 m_xDelete->set_sensitive( !bReadOnly );
256 void DbRegistrationOptionsPage::insertNewEntry(const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly)
258 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(new DatabaseRegistration(_sLocation, _bReadOnly))));
259 m_xPathBox->insert(nullptr, -1, &_sName, &sId, nullptr, nullptr, nullptr, false, m_xIter.get());
261 if (_bReadOnly)
262 m_xPathBox->set_image(*m_xIter, RID_SVXBMP_LOCK);
264 m_xPathBox->set_text(*m_xIter, _sLocation, 1);
267 void DbRegistrationOptionsPage::openLinkDialog(const OUString& sOldName, const OUString& sOldLocation, int nEntry)
269 ODocumentLinkDialog aDlg(GetFrameWeld(), nEntry == -1);
271 aDlg.setLink(sOldName, sOldLocation);
272 aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
274 if (aDlg.run() == RET_OK)
276 OUString sNewName,sNewLocation;
277 aDlg.getLink(sNewName,sNewLocation);
278 if ( nEntry == -1 || sNewName != sOldName || sNewLocation != sOldLocation )
280 if (nEntry != -1)
282 delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
283 m_xPathBox->remove(nEntry);
285 insertNewEntry( sNewName, sNewLocation, false );
286 m_bModified = true;
291 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, const OUString&, _rName, bool )
293 int nCount = m_xPathBox->n_children();
294 for (int i = 0; i < nCount; ++i)
296 if (m_xPathBox->get_text(i, 0) == _rName)
297 return false;
299 return true;
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */