bump product version to 6.3.0.0.beta1
[LibreOffice.git] / cui / source / dialogs / multipat.cxx
blob6789e83c2a295f5bee15c3294c8232cd70488e98
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>
26 #include <sfx2/filedlghelper.hxx>
28 #include <multipat.hxx>
29 #include <dialmgr.hxx>
31 #include <strings.hrc>
32 #include <comphelper/processfactory.hxx>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
35 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
37 #include <unotools/pathoptions.hxx>
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::ui::dialogs;
41 using namespace ::com::sun::star::uno;
43 IMPL_LINK_NOARG(SvxMultiPathDialog, SelectHdl_Impl, weld::TreeView&, void)
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);
51 IMPL_LINK_NOARG(SvxPathSelectDialog, SelectHdl_Impl, weld::TreeView&, void)
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);
59 void SvxMultiPathDialog::HandleEntryChecked(int nRow)
61 m_xRadioLB->select(nRow);
62 bool bChecked = m_xRadioLB->get_toggle(nRow, 0) == TRISTATE_TRUE;
63 if (bChecked)
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)
69 if (i != nRow)
70 m_xRadioLB->set_toggle(i, TRISTATE_TRUE, 0);
75 IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const row_col&, rRowCol, void)
77 HandleEntryChecked(rRowCol.first);
80 void SvxMultiPathDialog::AppendEntry(const OUString& rText, const OUString& rId)
82 m_xRadioLB->append();
83 const int nRow = m_xRadioLB->n_children() - 1;
84 m_xRadioLB->set_toggle(nRow, TRISTATE_FALSE, 0);
85 m_xRadioLB->set_text(nRow, rText, 1);
86 m_xRadioLB->set_id(nRow, rId);
89 IMPL_LINK_NOARG(SvxMultiPathDialog, AddHdl_Impl, weld::Button&, void)
91 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
92 Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
94 if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
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);
102 if (m_xRadioLB->find_text(sInsPath) != -1)
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();
110 else
112 AppendEntry(sInsPath, aURL);
115 SelectHdl_Impl(*m_xRadioLB);
119 IMPL_LINK_NOARG(SvxPathSelectDialog, AddHdl_Impl, weld::Button&, void)
121 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
122 Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
124 if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
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);
132 if (m_xPathLB->find_text(sInsPath) != -1)
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();
140 else
142 m_xPathLB->append(aURL, sInsPath);
145 SelectHdl_Impl(*m_xPathLB);
149 IMPL_LINK_NOARG(SvxMultiPathDialog, DelHdl_Impl, weld::Button&, void)
151 int nPos = m_xRadioLB->get_selected_index();
152 bool bChecked = m_xRadioLB->get_toggle(nPos, 0) == TRISTATE_TRUE;
153 m_xRadioLB->remove(nPos);
154 int nCnt = m_xRadioLB->n_children();
155 if (nCnt)
157 --nCnt;
159 if ( nPos > nCnt )
160 nPos = nCnt;
161 if (bChecked)
163 m_xRadioLB->set_toggle(nPos, TRISTATE_TRUE, 0);
164 HandleEntryChecked(nPos);
166 m_xRadioLB->select(nPos);
169 SelectHdl_Impl(*m_xRadioLB);
172 IMPL_LINK_NOARG(SvxPathSelectDialog, DelHdl_Impl, weld::Button&, void)
174 int nPos = m_xPathLB->get_selected_index();
175 m_xPathLB->remove(nPos);
176 int nCnt = m_xPathLB->n_children();
178 if (nCnt)
180 --nCnt;
182 if ( nPos > nCnt )
183 nPos = nCnt;
184 m_xPathLB->select(nPos);
187 SelectHdl_Impl(*m_xPathLB);
190 SvxMultiPathDialog::SvxMultiPathDialog(weld::Window* pParent)
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"))
196 m_xRadioLB->set_size_request(m_xRadioLB->get_approximate_digit_width() * 60,
197 m_xRadioLB->get_text_height() * 10);
199 std::vector<int> aWidths;
200 aWidths.push_back(m_xRadioLB->get_checkbox_column_width());
201 m_xRadioLB->set_column_fixed_widths(aWidths);
203 std::vector<int> aRadioColumns;
204 aRadioColumns.push_back(0);
205 m_xRadioLB->set_toggle_columns_as_radio(aRadioColumns);
206 m_xRadioLB->connect_toggled(LINK(this, SvxMultiPathDialog, CheckHdl_Impl));
207 m_xRadioLB->connect_changed(LINK(this, SvxMultiPathDialog, SelectHdl_Impl));
208 m_xAddBtn->connect_clicked(LINK(this, SvxMultiPathDialog, AddHdl_Impl));
209 m_xDelBtn->connect_clicked(LINK(this, SvxMultiPathDialog, DelHdl_Impl));
211 SelectHdl_Impl(*m_xRadioLB);
214 SvxPathSelectDialog::SvxPathSelectDialog(weld::Window* pParent)
215 : GenericDialogController(pParent, "cui/ui/selectpathdialog.ui", "SelectPathDialog")
216 , m_xPathLB(m_xBuilder->weld_tree_view("paths"))
217 , m_xAddBtn(m_xBuilder->weld_button("add"))
218 , m_xDelBtn(m_xBuilder->weld_button("delete"))
220 m_xPathLB->set_size_request(m_xPathLB->get_approximate_digit_width() * 60,
221 m_xPathLB->get_text_height() * 10);
223 m_xPathLB->connect_changed(LINK(this, SvxPathSelectDialog, SelectHdl_Impl));
224 m_xAddBtn->connect_clicked(LINK(this, SvxPathSelectDialog, AddHdl_Impl));
225 m_xDelBtn->connect_clicked(LINK(this, SvxPathSelectDialog, DelHdl_Impl));
227 SelectHdl_Impl(*m_xPathLB);
230 SvxMultiPathDialog::~SvxMultiPathDialog()
234 OUString SvxMultiPathDialog::GetPath() const
236 OUStringBuffer sNewPath;
237 sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
239 OUString sWritable;
240 for (int i = 0, nCount = m_xRadioLB->n_children(); i < nCount; ++i)
242 if (m_xRadioLB->get_toggle(i, 0) == TRISTATE_TRUE)
243 sWritable = m_xRadioLB->get_id(i);
244 else
246 if (!sNewPath.isEmpty())
247 sNewPath.append(cDelim);
248 sNewPath.append(m_xRadioLB->get_id(i));
251 if (!sNewPath.isEmpty())
252 sNewPath.append(cDelim);
253 sNewPath.append(sWritable);
255 return sNewPath.makeStringAndClear();
258 OUString SvxPathSelectDialog::GetPath() const
260 OUStringBuffer sNewPath;
262 for (int i = 0; i < m_xPathLB->n_children(); ++i)
264 if ( !sNewPath.isEmpty() )
265 sNewPath.append(SVT_SEARCHPATH_DELIMITER);
266 sNewPath.append( m_xPathLB->get_id(i));
269 return sNewPath.makeStringAndClear();
272 void SvxMultiPathDialog::SetPath( const OUString& rPath )
274 if ( !rPath.isEmpty() )
276 const sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
277 int nCount = 0;
278 sal_Int32 nIndex = 0;
281 const OUString sPath = rPath.getToken( 0, cDelim, nIndex );
282 OUString sSystemPath;
283 bool bIsSystemPath =
284 osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
286 const OUString sEntry((bIsSystemPath ? sSystemPath : sPath));
287 AppendEntry(sEntry, sPath);
288 ++nCount;
290 while (nIndex >= 0);
292 if (nCount)
294 m_xRadioLB->set_toggle(nCount - 1, TRISTATE_TRUE, 0);
295 HandleEntryChecked(nCount - 1);
299 SelectHdl_Impl(*m_xRadioLB);
302 void SvxPathSelectDialog::SetPath(const OUString& rPath)
304 if ( !rPath.isEmpty() )
306 sal_Int32 nIndex = 0;
309 const OUString sPath = rPath.getToken( 0, SVT_SEARCHPATH_DELIMITER, nIndex );
310 OUString sSystemPath;
311 bool bIsSystemPath =
312 osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
314 m_xPathLB->append(sPath, bIsSystemPath ? sSystemPath : sPath);
316 while (nIndex >= 0);
319 SelectHdl_Impl(*m_xPathLB);
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */