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, 0) == 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, 0);
69  }
70  }
71 }
72 
73 IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const row_col&, rRowCol, void)
74 {
75  HandleEntryChecked(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, 0);
83  m_xRadioLB->set_text(nRow, rText, 1);
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  {
94  INetURLObject aPath( xFolderPicker->getDirectory() );
95  aPath.removeFinalSlash();
96  OUString aURL = aPath.GetMainURL( INetURLObject::DecodeMechanism::NONE );
97  OUString sInsPath;
98  osl::FileBase::getSystemPathFromFileURL(aURL, sInsPath);
99 
100  if (m_xRadioLB->find_text(sInsPath) != -1)
101  {
102  OUString sMsg( CuiResId( RID_MULTIPATH_DBL_ERR ) );
103  sMsg = sMsg.replaceFirst( "%1", sInsPath );
104  std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_xDialog.get(),
105  VclMessageType::Info, VclButtonsType::Ok, sMsg));
106  xInfoBox->run();
107  }
108  else
109  {
110  AppendEntry(sInsPath, aURL);
111  }
112 
113  SelectHdl_Impl(*m_xRadioLB);
114  }
115 }
116 
118 {
120  Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
121 
122  if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
123  {
124  INetURLObject aPath( xFolderPicker->getDirectory() );
125  aPath.removeFinalSlash();
126  OUString aURL = aPath.GetMainURL( INetURLObject::DecodeMechanism::NONE );
127  OUString sInsPath;
128  osl::FileBase::getSystemPathFromFileURL(aURL, sInsPath);
129 
130  if (m_xPathLB->find_text(sInsPath) != -1)
131  {
132  OUString sMsg( CuiResId( RID_MULTIPATH_DBL_ERR ) );
133  sMsg = sMsg.replaceFirst( "%1", sInsPath );
134  std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_xDialog.get(),
135  VclMessageType::Info, VclButtonsType::Ok, sMsg));
136  xInfoBox->run();
137  }
138  else
139  {
140  m_xPathLB->append(aURL, sInsPath);
141  }
142 
143  SelectHdl_Impl(*m_xPathLB);
144  }
145 }
146 
148 {
149  int nPos = m_xRadioLB->get_selected_index();
150  bool bChecked = m_xRadioLB->get_toggle(nPos, 0) == 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, 0);
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 
197  std::vector<int> aWidths;
198  aWidths.push_back(m_xRadioLB->get_checkbox_column_width());
199  m_xRadioLB->set_column_fixed_widths(aWidths);
200 
201  std::vector<int> aRadioColumns;
202  aRadioColumns.push_back(0);
203  m_xRadioLB->set_toggle_columns_as_radio(aRadioColumns);
204  m_xRadioLB->connect_toggled(LINK(this, SvxMultiPathDialog, CheckHdl_Impl));
205  m_xRadioLB->connect_changed(LINK(this, SvxMultiPathDialog, SelectHdl_Impl));
206  m_xAddBtn->connect_clicked(LINK(this, SvxMultiPathDialog, AddHdl_Impl));
207  m_xDelBtn->connect_clicked(LINK(this, SvxMultiPathDialog, DelHdl_Impl));
208 
209  SelectHdl_Impl(*m_xRadioLB);
210 }
211 
213  : GenericDialogController(pParent, "cui/ui/selectpathdialog.ui", "SelectPathDialog")
214  , m_xPathLB(m_xBuilder->weld_tree_view("paths"))
215  , m_xAddBtn(m_xBuilder->weld_button("add"))
216  , m_xDelBtn(m_xBuilder->weld_button("delete"))
217 {
218  m_xPathLB->set_size_request(m_xPathLB->get_approximate_digit_width() * 60,
219  m_xPathLB->get_text_height() * 10);
220 
221  m_xPathLB->connect_changed(LINK(this, SvxPathSelectDialog, SelectHdl_Impl));
222  m_xAddBtn->connect_clicked(LINK(this, SvxPathSelectDialog, AddHdl_Impl));
223  m_xDelBtn->connect_clicked(LINK(this, SvxPathSelectDialog, DelHdl_Impl));
224 
225  SelectHdl_Impl(*m_xPathLB);
226 }
227 
229 {
230 }
231 
233 {
234  OUStringBuffer sNewPath;
236 
237  OUString sWritable;
238  for (int i = 0, nCount = m_xRadioLB->n_children(); i < nCount; ++i)
239  {
240  if (m_xRadioLB->get_toggle(i, 0) == TRISTATE_TRUE)
241  sWritable = m_xRadioLB->get_id(i);
242  else
243  {
244  if (!sNewPath.isEmpty())
245  sNewPath.append(cDelim);
246  sNewPath.append(m_xRadioLB->get_id(i));
247  }
248  }
249  if (!sNewPath.isEmpty())
250  sNewPath.append(cDelim);
251  sNewPath.append(sWritable);
252 
253  return sNewPath.makeStringAndClear();
254 }
255 
257 {
258  OUStringBuffer sNewPath;
259 
260  for (int i = 0; i < m_xPathLB->n_children(); ++i)
261  {
262  if ( !sNewPath.isEmpty() )
263  sNewPath.append(SVT_SEARCHPATH_DELIMITER);
264  sNewPath.append( m_xPathLB->get_id(i));
265  }
266 
267  return sNewPath.makeStringAndClear();
268 }
269 
270 void SvxMultiPathDialog::SetPath( const OUString& rPath )
271 {
272  if ( !rPath.isEmpty() )
273  {
275  int nCount = 0;
276  sal_Int32 nIndex = 0;
277  do
278  {
279  const OUString sPath = rPath.getToken( 0, cDelim, nIndex );
280  OUString sSystemPath;
281  bool bIsSystemPath =
282  osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
283 
284  const OUString sEntry((bIsSystemPath ? sSystemPath : sPath));
285  AppendEntry(sEntry, sPath);
286  ++nCount;
287  }
288  while (nIndex >= 0);
289 
290  if (nCount)
291  {
292  m_xRadioLB->set_toggle(nCount - 1, TRISTATE_TRUE, 0);
293  HandleEntryChecked(nCount - 1);
294  }
295  }
296 
297  SelectHdl_Impl(*m_xRadioLB);
298 }
299 
300 void SvxPathSelectDialog::SetPath(const OUString& rPath)
301 {
302  if ( !rPath.isEmpty() )
303  {
304  sal_Int32 nIndex = 0;
305  do
306  {
307  const OUString sPath = rPath.getToken( 0, SVT_SEARCHPATH_DELIMITER, nIndex );
308  OUString sSystemPath;
309  bool bIsSystemPath =
310  osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
311 
312  m_xPathLB->append(sPath, bIsSystemPath ? sSystemPath : sPath);
313  }
314  while (nIndex >= 0);
315  }
316 
317  SelectHdl_Impl(*m_xPathLB);
318 }
319 
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
sal_Int32 nIndex
std::unique_ptr< weld::Button > m_xDelBtn
Definition: multipat.hxx:39
TRISTATE_TRUE
sal_uInt16 sal_Unicode
int nCount
std::unique_ptr< weld::Button > m_xAddBtn
Definition: multipat.hxx:38
const sal_Unicode cDelim
OUString GetPath() const
Definition: multipat.cxx:256
void HandleEntryChecked(int nRow)
Definition: multipat.cxx:57
int i
SvxPathSelectDialog(weld::Window *pParent)
Definition: multipat.cxx:212
void SetPath(const OUString &rPath)
Definition: multipat.cxx:270
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:63
virtual ~SvxMultiPathDialog() override
Definition: multipat.cxx:228
std::unique_ptr< weld::TreeView > m_xRadioLB
Definition: multipat.hxx:37
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:64
void SetPath(const OUString &rPath)
Definition: multipat.cxx:300
IMPL_LINK_NOARG(SvxMultiPathDialog, SelectHdl_Impl, weld::TreeView &, void)
Definition: multipat.cxx:41
Reference< XExecutableDialog > m_xDialog
IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const row_col &, rRowCol, void)
Definition: multipat.cxx:73
Reference< XComponentContext > getProcessComponentContext()
OUString GetPath() const
Definition: multipat.cxx:232
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage)
sal_uInt16 nPos
std::unique_ptr< weld::TreeView > m_xPathLB
Definition: multipat.hxx:62