bump product version to 6.4.0.3
[LibreOffice.git] / cui / source / dialogs / multipat.cxx
blobc81542b44840035be0fc12b40fcb8285e9c193ac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <osl/file.hxx>
23 #include <tools/urlobj.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
27 #include <multipat.hxx>
28 #include <dialmgr.hxx>
30 #include <strings.hrc>
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
33 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
35 #include <unotools/pathoptions.hxx>
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::ui::dialogs;
39 using namespace ::com::sun::star::uno;
41 IMPL_LINK_NOARG(SvxMultiPathDialog, SelectHdl_Impl, weld::TreeView&, void)
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);
49 IMPL_LINK_NOARG(SvxPathSelectDialog, SelectHdl_Impl, weld::TreeView&, void)
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);
57 void SvxMultiPathDialog::HandleEntryChecked(int nRow)
59 m_xRadioLB->select(nRow);
60 bool bChecked = m_xRadioLB->get_toggle(nRow, 0) == TRISTATE_TRUE;
61 if (bChecked)
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)
67 if (i != nRow)
68 m_xRadioLB->set_toggle(i, TRISTATE_FALSE, 0);
73 IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const row_col&, rRowCol, void)
75 HandleEntryChecked(rRowCol.first);
78 void SvxMultiPathDialog::AppendEntry(const OUString& rText, const OUString& rId)
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);
87 IMPL_LINK_NOARG(SvxMultiPathDialog, AddHdl_Impl, weld::Button&, void)
89 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
90 Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
92 if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
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);
100 if (m_xRadioLB->find_text(sInsPath) != -1)
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();
108 else
110 AppendEntry(sInsPath, aURL);
113 SelectHdl_Impl(*m_xRadioLB);
117 IMPL_LINK_NOARG(SvxPathSelectDialog, AddHdl_Impl, weld::Button&, void)
119 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
120 Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
122 if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
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);
130 if (m_xPathLB->find_text(sInsPath) != -1)
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();
138 else
140 m_xPathLB->append(aURL, sInsPath);
143 SelectHdl_Impl(*m_xPathLB);
147 IMPL_LINK_NOARG(SvxMultiPathDialog, DelHdl_Impl, weld::Button&, void)
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)
155 --nCnt;
157 if ( nPos > nCnt )
158 nPos = nCnt;
159 if (bChecked)
161 m_xRadioLB->set_toggle(nPos, TRISTATE_TRUE, 0);
162 HandleEntryChecked(nPos);
164 m_xRadioLB->select(nPos);
167 SelectHdl_Impl(*m_xRadioLB);
170 IMPL_LINK_NOARG(SvxPathSelectDialog, DelHdl_Impl, weld::Button&, void)
172 int nPos = m_xPathLB->get_selected_index();
173 m_xPathLB->remove(nPos);
174 int nCnt = m_xPathLB->n_children();
176 if (nCnt)
178 --nCnt;
180 if ( nPos > nCnt )
181 nPos = nCnt;
182 m_xPathLB->select(nPos);
185 SelectHdl_Impl(*m_xPathLB);
188 SvxMultiPathDialog::SvxMultiPathDialog(weld::Window* pParent)
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"))
194 m_xRadioLB->set_size_request(m_xRadioLB->get_approximate_digit_width() * 60,
195 m_xRadioLB->get_text_height() * 10);
197 std::vector<int> aWidths;
198 aWidths.push_back(m_xRadioLB->get_checkbox_column_width());
199 m_xRadioLB->set_column_fixed_widths(aWidths);
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));
209 SelectHdl_Impl(*m_xRadioLB);
212 SvxPathSelectDialog::SvxPathSelectDialog(weld::Window* pParent)
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"))
218 m_xPathLB->set_size_request(m_xPathLB->get_approximate_digit_width() * 60,
219 m_xPathLB->get_text_height() * 10);
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));
225 SelectHdl_Impl(*m_xPathLB);
228 SvxMultiPathDialog::~SvxMultiPathDialog()
232 OUString SvxMultiPathDialog::GetPath() const
234 OUStringBuffer sNewPath;
235 sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
237 OUString sWritable;
238 for (int i = 0, nCount = m_xRadioLB->n_children(); i < nCount; ++i)
240 if (m_xRadioLB->get_toggle(i, 0) == TRISTATE_TRUE)
241 sWritable = m_xRadioLB->get_id(i);
242 else
244 if (!sNewPath.isEmpty())
245 sNewPath.append(cDelim);
246 sNewPath.append(m_xRadioLB->get_id(i));
249 if (!sNewPath.isEmpty())
250 sNewPath.append(cDelim);
251 sNewPath.append(sWritable);
253 return sNewPath.makeStringAndClear();
256 OUString SvxPathSelectDialog::GetPath() const
258 OUStringBuffer sNewPath;
260 for (int i = 0; i < m_xPathLB->n_children(); ++i)
262 if ( !sNewPath.isEmpty() )
263 sNewPath.append(SVT_SEARCHPATH_DELIMITER);
264 sNewPath.append( m_xPathLB->get_id(i));
267 return sNewPath.makeStringAndClear();
270 void SvxMultiPathDialog::SetPath( const OUString& rPath )
272 if ( !rPath.isEmpty() )
274 const sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
275 int nCount = 0;
276 sal_Int32 nIndex = 0;
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;
284 const OUString sEntry((bIsSystemPath ? sSystemPath : sPath));
285 AppendEntry(sEntry, sPath);
286 ++nCount;
288 while (nIndex >= 0);
290 if (nCount)
292 m_xRadioLB->set_toggle(nCount - 1, TRISTATE_TRUE, 0);
293 HandleEntryChecked(nCount - 1);
297 SelectHdl_Impl(*m_xRadioLB);
300 void SvxPathSelectDialog::SetPath(const OUString& rPath)
302 if ( !rPath.isEmpty() )
304 sal_Int32 nIndex = 0;
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;
312 m_xPathLB->append(sPath, bIsSystemPath ? sSystemPath : sPath);
314 while (nIndex >= 0);
317 SelectHdl_Impl(*m_xPathLB);
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */