android: Update app-specific/MIME type icons
[LibreOffice.git] / cui / source / dialogs / multipat.cxx
blob085d21f995de42c7ec748cc3bffefbfc320edb2a
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 <sfx2/filedlghelper.hxx>
24 #include <tools/urlobj.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/weld.hxx>
28 #include <multipat.hxx>
29 #include <dialmgr.hxx>
31 #include <strings.hrc>
32 #include <comphelper/processfactory.hxx>
33 #include <com/sun/star/ui/dialogs/XFolderPicker2.hpp>
34 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
36 #include <unotools/pathoptions.hxx>
37 #include <o3tl/string_view.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) == 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_FALSE);
75 IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const weld::TreeView::iter_col&, rRowCol, void)
77 HandleEntryChecked(m_xRadioLB->get_iter_index_in_parent(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);
85 m_xRadioLB->set_text(nRow, rText, 0);
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 = sfx2::createFolderPicker(xContext, m_xDialog.get());
94 if ( xFolderPicker->execute() != ExecutableDialogResults::OK )
95 return;
97 INetURLObject aPath( xFolderPicker->getDirectory() );
98 aPath.removeFinalSlash();
99 OUString aURL = aPath.GetMainURL( INetURLObject::DecodeMechanism::NONE );
100 OUString sInsPath;
101 osl::FileBase::getSystemPathFromFileURL(aURL, sInsPath);
103 if (m_xRadioLB->find_text(sInsPath) != -1)
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();
111 else
113 AppendEntry(sInsPath, aURL);
116 SelectHdl_Impl(*m_xRadioLB);
119 IMPL_LINK_NOARG(SvxPathSelectDialog, AddHdl_Impl, weld::Button&, void)
121 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
122 Reference < XFolderPicker2 > xFolderPicker = sfx2::createFolderPicker(xContext, m_xDialog.get());
124 if ( xFolderPicker->execute() != ExecutableDialogResults::OK )
125 return;
127 INetURLObject aPath( xFolderPicker->getDirectory() );
128 aPath.removeFinalSlash();
129 OUString aURL = aPath.GetMainURL( INetURLObject::DecodeMechanism::NONE );
130 OUString sInsPath;
131 osl::FileBase::getSystemPathFromFileURL(aURL, sInsPath);
133 if (m_xPathLB->find_text(sInsPath) != -1)
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();
141 else
143 m_xPathLB->append(aURL, sInsPath);
146 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) == 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);
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);
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));
202 m_xAddBtn->connect_clicked(LINK(this, SvxMultiPathDialog, AddHdl_Impl));
203 m_xDelBtn->connect_clicked(LINK(this, SvxMultiPathDialog, DelHdl_Impl));
205 SelectHdl_Impl(*m_xRadioLB);
208 SvxPathSelectDialog::SvxPathSelectDialog(weld::Window* pParent)
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"))
214 m_xPathLB->set_size_request(m_xPathLB->get_approximate_digit_width() * 60,
215 m_xPathLB->get_text_height() * 10);
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));
221 SelectHdl_Impl(*m_xPathLB);
224 SvxMultiPathDialog::~SvxMultiPathDialog()
228 OUString SvxMultiPathDialog::GetPath() const
230 OUStringBuffer sNewPath;
231 sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
233 OUString sWritable;
234 for (int i = 0, nCount = m_xRadioLB->n_children(); i < nCount; ++i)
236 if (m_xRadioLB->get_toggle(i) == TRISTATE_TRUE)
237 sWritable = m_xRadioLB->get_id(i);
238 else
240 if (!sNewPath.isEmpty())
241 sNewPath.append(cDelim);
242 sNewPath.append(m_xRadioLB->get_id(i));
245 if (!sNewPath.isEmpty())
246 sNewPath.append(cDelim);
247 sNewPath.append(sWritable);
249 return sNewPath.makeStringAndClear();
252 OUString SvxPathSelectDialog::GetPath() const
254 OUStringBuffer sNewPath;
256 for (int i = 0; i < m_xPathLB->n_children(); ++i)
258 if ( !sNewPath.isEmpty() )
259 sNewPath.append(SVT_SEARCHPATH_DELIMITER);
260 sNewPath.append( m_xPathLB->get_id(i));
263 return sNewPath.makeStringAndClear();
266 void SvxMultiPathDialog::SetPath( std::u16string_view rPath )
268 if ( !rPath.empty() )
270 const sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
271 int nCount = 0;
272 sal_Int32 nIndex = 0;
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;
280 const OUString sEntry((bIsSystemPath ? sSystemPath : sPath));
281 AppendEntry(sEntry, sPath);
282 ++nCount;
284 while (nIndex >= 0);
286 if (nCount)
288 m_xRadioLB->set_toggle(nCount - 1, TRISTATE_TRUE);
289 HandleEntryChecked(nCount - 1);
293 SelectHdl_Impl(*m_xRadioLB);
296 void SvxPathSelectDialog::SetPath(std::u16string_view rPath)
298 if ( !rPath.empty() )
300 sal_Int32 nIndex = 0;
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;
308 m_xPathLB->append(sPath, bIsSystemPath ? sSystemPath : sPath);
310 while (nIndex >= 0);
313 SelectHdl_Impl(*m_xPathLB);
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */