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