LibreOffice Module cui (master)  1
multipat.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 <sal/config.h>
21 
22 #include <osl/file.hxx>
23 #include <tools/urlobj.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
26 
27 #include <multipat.hxx>
28 #include <dialmgr.hxx>
29 
30 #include <strings.hrc>
32 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
33 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
34 
35 #include <unotools/pathoptions.hxx>
36 
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::ui::dialogs;
39 using namespace ::com::sun::star::uno;
40 
42 {
43  auto nCount = m_xRadioLB->n_children();
44  bool bIsSelected = m_xRadioLB->get_selected_index() != -1;
45  bool bEnable = nCount > 1;
46  m_xDelBtn->set_sensitive(bEnable && bIsSelected);
47 }
48 
50 {
51  auto nCount = m_xPathLB->n_children();
52  bool bIsSelected = m_xPathLB->get_selected_index() != -1;
53  bool bEnable = nCount > 1;
54  m_xDelBtn->set_sensitive(bEnable && bIsSelected);
55 }
56 
58 {
59  m_xRadioLB->select(nRow);
60  bool bChecked = m_xRadioLB->get_toggle(nRow) == TRISTATE_TRUE;
61  if (bChecked)
62  {
63  // we have radio button behavior -> so uncheck the other entries
64  int nCount = m_xRadioLB->n_children();
65  for (int i = 0; i < nCount; ++i)
66  {
67  if (i != nRow)
68  m_xRadioLB->set_toggle(i, TRISTATE_FALSE);
69  }
70  }
71 }
72 
73 IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const weld::TreeView::iter_col&, rRowCol, void)
74 {
75  HandleEntryChecked(m_xRadioLB->get_iter_index_in_parent(rRowCol.first));
76 }
77 
78 void SvxMultiPathDialog::AppendEntry(const OUString& rText, const OUString& rId)
79 {
80  m_xRadioLB->append();
81  const int nRow = m_xRadioLB->n_children() - 1;
82  m_xRadioLB->set_toggle(nRow, TRISTATE_FALSE);
83  m_xRadioLB->set_text(nRow, rText, 0);
84  m_xRadioLB->set_id(nRow, rId);
85 }
86 
88 {
90  Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
91 
92  if ( xFolderPicker->execute() != ExecutableDialogResults::OK )
93  return;
94 
95  INetURLObject aPath( xFolderPicker->getDirectory() );
96  aPath.removeFinalSlash();
97  OUString aURL = aPath.GetMainURL( INetURLObject::DecodeMechanism::NONE );
98  OUString sInsPath;
99  osl::FileBase::getSystemPathFromFileURL(aURL, sInsPath);
100 
101  if (m_xRadioLB->find_text(sInsPath) != -1)
102  {
103  OUString sMsg( CuiResId( RID_MULTIPATH_DBL_ERR ) );
104  sMsg = sMsg.replaceFirst( "%1", sInsPath );
105  std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_xDialog.get(),
106  VclMessageType::Info, VclButtonsType::Ok, sMsg));
107  xInfoBox->run();
108  }
109  else
110  {
111  AppendEntry(sInsPath, aURL);
112  }
113 
114  SelectHdl_Impl(*m_xRadioLB);
115 }
116 
118 {
120  Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
121 
122  if ( xFolderPicker->execute() != ExecutableDialogResults::OK )
123  return;
124 
125  INetURLObject aPath( xFolderPicker->getDirectory() );
126  aPath.removeFinalSlash();
127  OUString aURL = aPath.GetMainURL( INetURLObject::DecodeMechanism::NONE );
128  OUString sInsPath;
129  osl::FileBase::getSystemPathFromFileURL(aURL, sInsPath);
130 
131  if (m_xPathLB->find_text(sInsPath) != -1)
132  {
133  OUString sMsg( CuiResId( RID_MULTIPATH_DBL_ERR ) );
134  sMsg = sMsg.replaceFirst( "%1", sInsPath );
135  std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_xDialog.get(),
136  VclMessageType::Info, VclButtonsType::Ok, sMsg));
137  xInfoBox->run();
138  }
139  else
140  {
141  m_xPathLB->append(aURL, sInsPath);
142  }
143 
144  SelectHdl_Impl(*m_xPathLB);
145 }
146 
148 {
149  int nPos = m_xRadioLB->get_selected_index();
150  bool bChecked = m_xRadioLB->get_toggle(nPos) == TRISTATE_TRUE;
151  m_xRadioLB->remove(nPos);
152  int nCnt = m_xRadioLB->n_children();
153  if (nCnt)
154  {
155  --nCnt;
156 
157  if ( nPos > nCnt )
158  nPos = nCnt;
159  if (bChecked)
160  {
161  m_xRadioLB->set_toggle(nPos, TRISTATE_TRUE);
162  HandleEntryChecked(nPos);
163  }
164  m_xRadioLB->select(nPos);
165  }
166 
167  SelectHdl_Impl(*m_xRadioLB);
168 }
169 
171 {
172  int nPos = m_xPathLB->get_selected_index();
173  m_xPathLB->remove(nPos);
174  int nCnt = m_xPathLB->n_children();
175 
176  if (nCnt)
177  {
178  --nCnt;
179 
180  if ( nPos > nCnt )
181  nPos = nCnt;
182  m_xPathLB->select(nPos);
183  }
184 
185  SelectHdl_Impl(*m_xPathLB);
186 }
187 
189  : GenericDialogController(pParent, "cui/ui/multipathdialog.ui", "MultiPathDialog")
190  , m_xRadioLB(m_xBuilder->weld_tree_view("paths"))
191  , m_xAddBtn(m_xBuilder->weld_button("add"))
192  , m_xDelBtn(m_xBuilder->weld_button("delete"))
193 {
194  m_xRadioLB->set_size_request(m_xRadioLB->get_approximate_digit_width() * 60,
195  m_xRadioLB->get_text_height() * 10);
196  m_xRadioLB->enable_toggle_buttons(weld::ColumnToggleType::Radio);
197  m_xRadioLB->connect_toggled(LINK(this, SvxMultiPathDialog, CheckHdl_Impl));
198  m_xRadioLB->connect_changed(LINK(this, SvxMultiPathDialog, SelectHdl_Impl));
199 
200  m_xAddBtn->connect_clicked(LINK(this, SvxMultiPathDialog, AddHdl_Impl));
201  m_xDelBtn->connect_clicked(LINK(this, SvxMultiPathDialog, DelHdl_Impl));
202 
203  SelectHdl_Impl(*m_xRadioLB);
204 }
205 
207  : GenericDialogController(pParent, "cui/ui/selectpathdialog.ui", "SelectPathDialog")
208  , m_xPathLB(m_xBuilder->weld_tree_view("paths"))
209  , m_xAddBtn(m_xBuilder->weld_button("add"))
210  , m_xDelBtn(m_xBuilder->weld_button("delete"))
211 {
212  m_xPathLB->set_size_request(m_xPathLB->get_approximate_digit_width() * 60,
213  m_xPathLB->get_text_height() * 10);
214 
215  m_xPathLB->connect_changed(LINK(this, SvxPathSelectDialog, SelectHdl_Impl));
216  m_xAddBtn->connect_clicked(LINK(this, SvxPathSelectDialog, AddHdl_Impl));
217  m_xDelBtn->connect_clicked(LINK(this, SvxPathSelectDialog, DelHdl_Impl));
218 
219  SelectHdl_Impl(*m_xPathLB);
220 }
221 
223 {
224 }
225 
227 {
228  OUStringBuffer sNewPath;
230 
231  OUString sWritable;
232  for (int i = 0, nCount = m_xRadioLB->n_children(); i < nCount; ++i)
233  {
234  if (m_xRadioLB->get_toggle(i) == TRISTATE_TRUE)
235  sWritable = m_xRadioLB->get_id(i);
236  else
237  {
238  if (!sNewPath.isEmpty())
239  sNewPath.append(cDelim);
240  sNewPath.append(m_xRadioLB->get_id(i));
241  }
242  }
243  if (!sNewPath.isEmpty())
244  sNewPath.append(cDelim);
245  sNewPath.append(sWritable);
246 
247  return sNewPath.makeStringAndClear();
248 }
249 
251 {
252  OUStringBuffer sNewPath;
253 
254  for (int i = 0; i < m_xPathLB->n_children(); ++i)
255  {
256  if ( !sNewPath.isEmpty() )
257  sNewPath.append(SVT_SEARCHPATH_DELIMITER);
258  sNewPath.append( m_xPathLB->get_id(i));
259  }
260 
261  return sNewPath.makeStringAndClear();
262 }
263 
264 void SvxMultiPathDialog::SetPath( const OUString& rPath )
265 {
266  if ( !rPath.isEmpty() )
267  {
269  int nCount = 0;
270  sal_Int32 nIndex = 0;
271  do
272  {
273  const OUString sPath = rPath.getToken( 0, cDelim, nIndex );
274  OUString sSystemPath;
275  bool bIsSystemPath =
276  osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
277 
278  const OUString sEntry((bIsSystemPath ? sSystemPath : sPath));
279  AppendEntry(sEntry, sPath);
280  ++nCount;
281  }
282  while (nIndex >= 0);
283 
284  if (nCount)
285  {
286  m_xRadioLB->set_toggle(nCount - 1, TRISTATE_TRUE);
287  HandleEntryChecked(nCount - 1);
288  }
289  }
290 
291  SelectHdl_Impl(*m_xRadioLB);
292 }
293 
294 void SvxPathSelectDialog::SetPath(const OUString& rPath)
295 {
296  if ( !rPath.isEmpty() )
297  {
298  sal_Int32 nIndex = 0;
299  do
300  {
301  const OUString sPath = rPath.getToken( 0, SVT_SEARCHPATH_DELIMITER, nIndex );
302  OUString sSystemPath;
303  bool bIsSystemPath =
304  osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
305 
306  m_xPathLB->append(sPath, bIsSystemPath ? sSystemPath : sPath);
307  }
308  while (nIndex >= 0);
309  }
310 
311  SelectHdl_Impl(*m_xPathLB);
312 }
313 
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
sal_Int32 nIndex
std::unique_ptr< weld::Button > m_xDelBtn
Definition: multipat.hxx:38
TRISTATE_TRUE
sal_uInt16 sal_Unicode
std::pair< const TreeIter &, int > iter_col
int nCount
std::unique_ptr< weld::Button > m_xAddBtn
Definition: multipat.hxx:37
const sal_Unicode cDelim
OUString GetPath() const
Definition: multipat.cxx:250
void HandleEntryChecked(int nRow)
Definition: multipat.cxx:57
int i
SvxPathSelectDialog(weld::Window *pParent)
Definition: multipat.cxx:206
IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const weld::TreeView::iter_col &, rRowCol, void)
Definition: multipat.cxx:73
void SetPath(const OUString &rPath)
Definition: multipat.cxx:264
TRISTATE_FALSE
void AppendEntry(const OUString &rText, const OUString &rId)
Definition: multipat.cxx:78
bool removeFinalSlash()
std::unique_ptr< weld::Button > m_xAddBtn
Definition: multipat.hxx:61
virtual ~SvxMultiPathDialog() override
Definition: multipat.cxx:222
std::unique_ptr< weld::TreeView > m_xRadioLB
Definition: multipat.hxx:36
SvxMultiPathDialog(weld::Window *pParent)
Definition: multipat.cxx:188
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
#define SVT_SEARCHPATH_DELIMITER
std::unique_ptr< weld::Button > m_xDelBtn
Definition: multipat.hxx:62
void SetPath(const OUString &rPath)
Definition: multipat.cxx:294
IMPL_LINK_NOARG(SvxMultiPathDialog, SelectHdl_Impl, weld::TreeView &, void)
Definition: multipat.cxx:41
Reference< XExecutableDialog > m_xDialog
Reference< XComponentContext > getProcessComponentContext()
OUString GetPath() const
Definition: multipat.cxx:226
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
sal_uInt16 nPos
std::unique_ptr< weld::TreeView > m_xPathLB
Definition: multipat.hxx:60