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 "connpooloptions.hxx"
23 #include <svl/filenotation.hxx>
24 #include <helpids.h>
25 #include <tools/debug.hxx>
27 #include <strings.hrc>
28 #include <bitmaps.hlst>
29 #include <vcl/svapp.hxx>
30 #include <vcl/weld.hxx>
31 #include <svl/eitem.hxx>
32 #include <com/sun/star/uno/Exception.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
35 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
36 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
37 #include <svl/itemset.hxx>
38 #include "doclinkdialog.hxx"
40 #include <sfx2/docfilt.hxx>
41 #include <dialmgr.hxx>
43 #include <svx/dialogs.hrc>
45 
46 #define TAB_WIDTH1 80
47 
48 #define COL_TYPE 0
49 
50 namespace svx
51 {
52 
53 
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::ui::dialogs;
56 using namespace ::com::sun::star::uno;
57 using namespace ::svt;
58 
59 // class RegistrationItemSetHolder -------------------------------------------------
60 
62  :m_aRegistrationItems( _rMasterSet )
63 {
65 }
66 
68 {
69 }
70 
71 // class DatabaseRegistrationDialog ------------------------------------------------
72 
74  : RegistrationItemSetHolder(rInAttrs)
75  , SfxSingleTabDialogController(pParent, &getRegistrationItems())
76 {
77  TabPageParent aParent(get_content_area(), this);
79  m_xDialog->set_title(CuiResId(RID_SVXSTR_REGISTERED_DATABASES));
80 }
81 
83 {
84  short result = SfxSingleTabDialogController::run();
85  if (result == RET_OK)
86  {
87  DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
88  if ( GetOutputItemSet() )
90  }
91  return result;
92 }
93 
94 // class DbRegistrationOptionsPage --------------------------------------------------
95 
97  : SfxTabPage(pParent, "cui/ui/dbregisterpage.ui", "DbRegisterPage", &rSet)
98  , m_nOldCount(0)
99  , m_bModified(false)
100  , m_xNew(m_xBuilder->weld_button("new"))
101  , m_xEdit(m_xBuilder->weld_button("edit"))
102  , m_xDelete(m_xBuilder->weld_button("delete"))
103  , m_xPathBox(m_xBuilder->weld_tree_view("pathctrl"))
104  , m_xIter(m_xPathBox->make_iterator())
105 {
106  Size aControlSize(248, 147);
107  aControlSize = LogicToPixel(aControlSize, MapMode(MapUnit::MapAppFont));
108  m_xPathBox->set_size_request(aControlSize.Width(), aControlSize.Height());
109 
110  std::vector<int> aWidths;
111  aWidths.push_back(LogicToPixel(Size(TAB_WIDTH1, 0), MapMode(MapUnit::MapAppFont)).Width());
112  m_xPathBox->set_column_fixed_widths(aWidths);
113 
114  m_xNew->connect_clicked( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
115  m_xEdit->connect_clicked( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
116  m_xDelete->connect_clicked( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
117 
118  m_xPathBox->connect_column_clicked(LINK(this, DbRegistrationOptionsPage, HeaderSelect_Impl));
119 
120  m_xPathBox->make_sorted();
121  m_xPathBox->connect_row_activated( LINK( this, DbRegistrationOptionsPage, PathBoxDoubleClickHdl ) );
122  m_xPathBox->connect_changed( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
123 
124  m_xPathBox->set_help_id(HID_DBPATH_CTL_PATH);
125 }
126 
128 {
129  disposeOnce();
130 }
131 
133 {
134  for (int i = 0, nCount = m_xPathBox->n_children(); i < nCount; ++i )
135  delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
137 }
138 
140  const SfxItemSet* rAttrSet )
141 {
142  return VclPtr<DbRegistrationOptionsPage>::Create( pParent, *rAttrSet );
143 }
144 
146 {
147  // the settings for the single drivers
148  bool bModified = false;
149  DatabaseRegistrations aRegistrations;
150  int nCount = m_xPathBox->n_children();
151  for (int i = 0; i < nCount; ++i)
152  {
153  DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(i).toInt64());
154  if ( pRegistration && !pRegistration->sLocation.isEmpty() )
155  {
156  OUString sName(m_xPathBox->get_text(i, 0));
157  OFileNotation aTransformer( pRegistration->sLocation );
158  aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
159  }
160  }
161  if ( m_nOldCount != aRegistrations.size() || m_bModified )
162  {
163  rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ));
164  bModified = true;
165  }
166 
167  return bModified;
168 }
169 
171 {
172  // the settings for the single drivers
173  const DatabaseMapItem* pRegistrations = rSet->GetItem<DatabaseMapItem>(SID_SB_DB_REGISTER);
174  if ( !pRegistrations )
175  return;
176 
177  m_xPathBox->clear();
178 
179  const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
180  m_nOldCount = rRegistrations.size();
181  for (auto const& elem : rRegistrations)
182  {
183  OFileNotation aTransformer( elem.second.sLocation );
184  insertNewEntry( elem.first, aTransformer.get( OFileNotation::N_SYSTEM ), elem.second.bReadOnly );
185  }
186 
187  OUString aUserData = GetUserData();
188  if ( !aUserData.isEmpty() )
189  {
190  sal_Int32 nIdx {0};
191  // restore column width
192  std::vector<int> aWidths;
193  aWidths.push_back(aUserData.getToken(0, ';', nIdx).toInt32());
194  m_xPathBox->set_column_fixed_widths(aWidths);
195  // restore sort direction
196  bool bUp = aUserData.getToken(0, ';', nIdx).toInt32() != 0;
197  m_xPathBox->set_sort_order(bUp);
198  m_xPathBox->set_sort_indicator(bUp ? TRISTATE_TRUE : TRISTATE_FALSE, COL_TYPE);
199  }
200 }
201 
203 {
204  OUString aUserData = OUString::number( m_xPathBox->get_column_width(COL_TYPE) ) + ";";
205  bool bUp = m_xPathBox->get_sort_order();
206  aUserData += (bUp ? OUString("1") : OUString("0"));
207  SetUserData( aUserData );
208 }
209 
211 {
212  int nEntry = m_xPathBox->get_selected_index();
213  if (nEntry != -1)
214  {
215  std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(GetDialogFrameWeld(),
216  VclMessageType::Question, VclButtonsType::YesNo, CuiResId(RID_SVXSTR_QUERY_DELETE_CONFIRM)));
217  if (xQuery->run() == RET_YES)
218  m_xPathBox->remove(nEntry);
219  }
220 }
221 
223 {
224  openLinkDialog(OUString(),OUString());
225 }
226 
228 {
229  EditHdl(*m_xEdit);
230 }
231 
233 {
234  int nEntry = m_xPathBox->get_selected_index();
235  if (nEntry == -1)
236  return;
237 
238  DatabaseRegistration* pOldRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
239  if (!pOldRegistration || pOldRegistration->bReadOnly)
240  return;
241 
242  OUString sOldName = m_xPathBox->get_text(nEntry, 0);
243  openLinkDialog(sOldName, pOldRegistration->sLocation, nEntry);
244 }
245 
246 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, int, nCol, void )
247 {
248  if (nCol != COL_TYPE)
249  return;
250 
251  bool bSortMode = m_xPathBox->get_sort_order();
252 
253  //set new arrow positions in headerbar
254  bSortMode = !bSortMode;
255  m_xPathBox->set_sort_order(bSortMode);
256 
257  //sort lists
258  m_xPathBox->set_sort_indicator(bSortMode ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
259 }
260 
262 {
263  DatabaseRegistration* pRegistration = reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_selected_id().toInt64());
264 
265  bool bReadOnly = true;
266  if (pRegistration)
267  {
268  bReadOnly = pRegistration->bReadOnly;
269  }
270 
271  m_xEdit->set_sensitive( !bReadOnly );
272  m_xDelete->set_sensitive( !bReadOnly );
273 }
274 
275 void DbRegistrationOptionsPage::insertNewEntry(const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly)
276 {
277  OUString sId(OUString::number(reinterpret_cast<sal_Int64>(new DatabaseRegistration(_sLocation, _bReadOnly))));
278  m_xPathBox->insert(nullptr, -1, &_sName, &sId, nullptr, nullptr, nullptr, false, m_xIter.get());
279 
280  if (_bReadOnly)
281  m_xPathBox->set_image(*m_xIter, RID_SVXBMP_LOCK);
282 
283  m_xPathBox->set_text(*m_xIter, _sLocation, 1);
284 }
285 
286 void DbRegistrationOptionsPage::openLinkDialog(const OUString& sOldName, const OUString& sOldLocation, int nEntry)
287 {
288  ODocumentLinkDialog aDlg(GetDialogFrameWeld(), nEntry == -1);
289 
290  aDlg.setLink(sOldName, sOldLocation);
291  aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
292 
293  if (aDlg.run() == RET_OK)
294  {
295  OUString sNewName,sNewLocation;
296  aDlg.getLink(sNewName,sNewLocation);
297  if ( nEntry == -1 || sNewName != sOldName || sNewLocation != sOldLocation )
298  {
299  if (nEntry != -1)
300  {
301  delete reinterpret_cast<DatabaseRegistration*>(m_xPathBox->get_id(nEntry).toInt64());
302  m_xPathBox->remove(nEntry);
303  }
304  insertNewEntry( sNewName, sNewLocation, false );
305  m_bModified = true;
306  }
307  }
308 }
309 
310 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, const OUString&, _rName, bool )
311 {
312  int nCount = m_xPathBox->n_children();
313  for (int i = 0; i < nCount; ++i)
314  {
315  if (m_xPathBox->get_text(i, 0) == _rName)
316  return false;
317  }
318  return true;
319 }
320 
321 }
322 
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
virtual void dispose() override
Definition: dbregister.cxx:132
virtual short run() override
Definition: dbregister.cxx:82
void SetUserData(const OUString &rString)
long Height() const
void openLinkDialog(const OUString &sOldName, const OUString &sOldLocation, int nEntry=-1)
opens the LinkDialog to create a register pair
Definition: dbregister.cxx:286
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
const SfxItemSet * GetOutputItemSet() const
TRISTATE_TRUE
virtual ~DbRegistrationOptionsPage() override
Definition: dbregister.cxx:127
RET_YES
DbRegistrationOptionsPage(TabPageParent pParent, const SfxItemSet &rSet)
Definition: dbregister.cxx:96
void SetTabPage(SfxTabPage *pTabPage)
void insertNewEntry(const OUString &_sName, const OUString &_sLocation, const bool bReadOnly)
inserts a new entry in the tablistbox
Definition: dbregister.cxx:275
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: dbregister.cxx:145
virtual void dispose() override
static VclPtr< SfxTabPage > Create(TabPageParent pParent, const SfxItemSet *rSet)
Definition: dbregister.cxx:139
static void GetOptions(SfxItemSet &_rFillItems)
helper for DatabaseRegistrationDialog
Definition: dbregister.hxx:87
void setLink(const OUString &_rName, const OUString &_rURL)
weld::Window * GetDialogFrameWeld() const
#define DBG_ASSERT(sCon, aError)
RegistrationItemSetHolder(const SfxItemSet &_rMasterSet)
Definition: dbregister.cxx:61
OUString sName
MetadataImporterPluginType * result
int i
TRISTATE_FALSE
void setNameValidator(const Link< const OUString &, bool > &_rValidator)
IMPL_LINK(ClassificationDialog, SelectClassificationHdl, weld::ComboBox &, rBox, void)
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:73
std::unique_ptr< weld::TreeIter > m_xIter
Definition: dbregister.hxx:40
virtual void Reset(const SfxItemSet *rSet) override
Definition: dbregister.cxx:170
const OUString & GetUserData()
#define COL_TYPE
Definition: dbregister.cxx:48
RET_OK
static VclPtr< reference_type > Create(Arg &&...arg)
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
#define TAB_WIDTH1
Definition: dbregister.cxx:46
virtual void FillUserData() override
Definition: dbregister.cxx:202
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
const SfxItemSet & getRegistrationItems() const
Definition: dbregister.hxx:97
OUString sId