LibreOffice Module cui (master)  1
dbregister.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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>
34 
35 #define COL_TYPE 0
36 
37 namespace svx
38 {
39 
40 
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;
45 
46 // class RegistrationItemSetHolder -------------------------------------------------
47 
49  :m_aRegistrationItems( _rMasterSet )
50 {
52 }
53 
55 {
56 }
57 
58 // class DatabaseRegistrationDialog ------------------------------------------------
59 
61  : RegistrationItemSetHolder(rInAttrs)
62  , SfxSingleTabDialogController(pParent, &getRegistrationItems())
63 {
65  m_xDialog->set_title(CuiResId(RID_SVXSTR_REGISTERED_DATABASES));
66 }
67 
69 {
70  short result = SfxSingleTabDialogController::run();
71  if (result == RET_OK)
72  {
73  DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
74  if ( GetOutputItemSet() )
76  }
77  return result;
78 }
79 
80 // class DbRegistrationOptionsPage --------------------------------------------------
81 
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())
91 {
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());
95 
96  std::vector<int> aWidths;
97  aWidths.push_back(m_xPathBox->get_approximate_digit_width() * 20);
98  m_xPathBox->set_column_fixed_widths(aWidths);
99 
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 ) );
103 
104  m_xPathBox->connect_column_clicked(LINK(this, DbRegistrationOptionsPage, HeaderSelect_Impl));
105 
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 ) );
109 
110  m_xPathBox->set_help_id(HID_DBPATH_CTL_PATH);
111 }
112 
114 {
115  for (int i = 0, nCount = m_xPathBox->n_children(); i < nCount; ++i )
116  delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
117 }
118 
119 std::unique_ptr<SfxTabPage> DbRegistrationOptionsPage::Create( weld::Container* pPage, weld::DialogController* pController,
120  const SfxItemSet* rAttrSet )
121 {
122  return std::make_unique<DbRegistrationOptionsPage>(pPage, pController, *rAttrSet);
123 }
124 
126 {
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)
132  {
133  DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
134  if ( pRegistration && !pRegistration->sLocation.isEmpty() )
135  {
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 );
139  }
140  }
141  if ( m_nOldCount != aRegistrations.size() || m_bModified )
142  {
143  rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ));
144  bModified = true;
145  }
146 
147  return bModified;
148 }
149 
151 {
152  // the settings for the single drivers
153  const DatabaseMapItem* pRegistrations = rSet->GetItem<DatabaseMapItem>(SID_SB_DB_REGISTER);
154  if ( !pRegistrations )
155  return;
156 
157  m_xPathBox->clear();
158 
159  const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
160  m_nOldCount = rRegistrations.size();
161  for (auto const& elem : rRegistrations)
162  {
163  OFileNotation aTransformer( elem.second.sLocation );
164  insertNewEntry( elem.first, aTransformer.get( OFileNotation::N_SYSTEM ), elem.second.bReadOnly );
165  }
166 
167  OUString aUserData = GetUserData();
168  if ( !aUserData.isEmpty() )
169  {
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);
179  }
180 }
181 
183 {
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 );
188 }
189 
191 {
192  int nEntry = m_xPathBox->get_selected_index();
193  if (nEntry != -1)
194  {
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);
199  }
200 }
201 
203 {
204  openLinkDialog(OUString(),OUString());
205 }
206 
208 {
209  EditHdl(*m_xEdit);
210  return true;
211 }
212 
214 {
215  int nEntry = m_xPathBox->get_selected_index();
216  if (nEntry == -1)
217  return;
218 
219  DatabaseRegistration* pOldRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
220  if (!pOldRegistration || pOldRegistration->bReadOnly)
221  return;
222 
223  OUString sOldName = m_xPathBox->get_text(nEntry, 0);
224  openLinkDialog(sOldName, pOldRegistration->sLocation, nEntry);
225 }
226 
227 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, int, nCol, void )
228 {
229  if (nCol != COL_TYPE)
230  return;
231 
232  bool bSortMode = m_xPathBox->get_sort_order();
233 
234  //set new arrow positions in headerbar
235  bSortMode = !bSortMode;
236  m_xPathBox->set_sort_order(bSortMode);
237 
238  //sort lists
239  m_xPathBox->set_sort_indicator(bSortMode ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
240 }
241 
243 {
244  DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_selected_id().toInt64());
245 
246  bool bReadOnly = true;
247  if (pRegistration)
248  {
249  bReadOnly = pRegistration->bReadOnly;
250  }
251 
252  m_xEdit->set_sensitive( !bReadOnly );
253  m_xDelete->set_sensitive( !bReadOnly );
254 }
255 
256 void DbRegistrationOptionsPage::insertNewEntry(const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly)
257 {
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());
260 
261  if (_bReadOnly)
262  m_xPathBox->set_image(*m_xIter, RID_SVXBMP_LOCK);
263 
264  m_xPathBox->set_text(*m_xIter, _sLocation, 1);
265 }
266 
267 void DbRegistrationOptionsPage::openLinkDialog(const OUString& sOldName, const OUString& sOldLocation, int nEntry)
268 {
269  ODocumentLinkDialog aDlg(GetFrameWeld(), nEntry == -1);
270 
271  aDlg.setLink(sOldName, sOldLocation);
272  aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
273 
274  if (aDlg.run() == RET_OK)
275  {
276  OUString sNewName,sNewLocation;
277  aDlg.getLink(sNewName,sNewLocation);
278  if ( nEntry == -1 || sNewName != sOldName || sNewLocation != sOldLocation )
279  {
280  if (nEntry != -1)
281  {
282  delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
283  m_xPathBox->remove(nEntry);
284  }
285  insertNewEntry( sNewName, sNewLocation, false );
286  m_bModified = true;
287  }
288  }
289 }
290 
291 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, const OUString&, _rName, bool )
292 {
293  int nCount = m_xPathBox->n_children();
294  for (int i = 0; i < nCount; ++i)
295  {
296  if (m_xPathBox->get_text(i, 0) == _rName)
297  return false;
298  }
299  return true;
300 }
301 
302 }
303 
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual short run() override
Definition: dbregister.cxx:68
void SetUserData(const OUString &rString)
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rSet)
Definition: dbregister.cxx:119
void openLinkDialog(const OUString &sOldName, const OUString &sOldLocation, int nEntry=-1)
opens the LinkDialog to create a register pair
Definition: dbregister.cxx:267
IMPL_LINK_NOARG(ClassificationDialog, OnAsyncExpandHdl, void *, void)
std::map< OUString, DatabaseRegistration > DatabaseRegistrations
std::unique_ptr< weld::Button > m_xNew
Definition: dbregister.hxx:36
weld::Container * get_content_area()
std::unique_ptr< weld::TreeView > m_xPathBox
Definition: dbregister.hxx:39
bool bReadOnly
const SfxItemSet * GetOutputItemSet() const
TRISTATE_TRUE
virtual ~DbRegistrationOptionsPage() override
Definition: dbregister.cxx:113
RET_YES
int nCount
void insertNewEntry(const OUString &_sName, const OUString &_sLocation, const bool bReadOnly)
inserts a new entry in the tablistbox
Definition: dbregister.cxx:256
const OUString & GetUserData() const
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: dbregister.cxx:125
const char * sName
static void GetOptions(SfxItemSet &_rFillItems)
helper for DatabaseRegistrationDialog
Definition: dbregister.hxx:86
void setLink(const OUString &_rName, const OUString &_rURL)
#define DBG_ASSERT(sCon, aError)
RegistrationItemSetHolder(const SfxItemSet &_rMasterSet)
Definition: dbregister.cxx:48
int i
TRISTATE_FALSE
void setNameValidator(const Link< const OUString &, bool > &_rValidator)
IMPL_LINK(ClassificationDialog, SelectClassificationHdl, weld::ComboBox &, rBox, void)
void SetTabPage(std::unique_ptr< SfxTabPage > xTabPage)
std::unique_ptr< weld::Button > m_xDelete
Definition: dbregister.hxx:38
static void SetOptions(const SfxItemSet &_rSourceItems)
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
#define SID_SB_DB_REGISTER
#define HID_DBPATH_CTL_PATH
Definition: helpids.h:24
std::unique_ptr< weld::Button > m_xEdit
Definition: dbregister.hxx:37
const DatabaseRegistrations & getRegistrations() const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
DatabaseRegistrationDialog(weld::Window *pParent, const SfxItemSet &rAttr)
Definition: dbregister.cxx:60
std::unique_ptr< weld::TreeIter > m_xIter
Definition: dbregister.hxx:40
weld::Window * GetFrameWeld() const
virtual void Reset(const SfxItemSet *rSet) override
Definition: dbregister.cxx:150
DbRegistrationOptionsPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: dbregister.cxx:82
#define COL_TYPE
Definition: dbregister.cxx:35
RET_OK
Reference< XExecutableDialog > m_xDialog
virtual short run()
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage)
dialog for editing document links associated with data sources
void getLink(OUString &_rName, OUString &_rURL) const
OUString sId
Any result
virtual void FillUserData() override
Definition: dbregister.cxx:182
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
const SfxItemSet & getRegistrationItems() const
Definition: dbregister.hxx:96
weld::Window * GetFrameWeld(const SfxFrame *pFrame)