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