cid#1640468 Dereference after null check
[LibreOffice.git] / cui / source / dialogs / multipat.cxx
blob03ba230df8f14968814c0833bb09fb0c44c2f333
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::ui::dialogs;
40 using namespace ::com::sun::star::uno;
42 IMPL_LINK_NOARG(SvxMultiPathDialog, SelectHdl_Impl, weld::TreeView&, void)
44 auto nCount = m_xRadioLB->n_children();
45 bool bIsSelected = m_xRadioLB->get_selected_index() != -1;
46 bool bEnable = nCount > 1;
47 m_xDelBtn->set_sensitive(bEnable && bIsSelected);
50 IMPL_LINK_NOARG(SvxPathSelectDialog, SelectHdl_Impl, weld::TreeView&, void)
52 auto nCount = m_xPathLB->n_children();
53 bool bIsSelected = m_xPathLB->get_selected_index() != -1;
54 bool bEnable = nCount > 1;
55 m_xDelBtn->set_sensitive(bEnable && bIsSelected);
58 void SvxMultiPathDialog::HandleEntryChecked(int nRow)
60 m_xRadioLB->select(nRow);
61 bool bChecked = m_xRadioLB->get_toggle(nRow) == TRISTATE_TRUE;
62 if (bChecked)
64 // we have radio button behavior -> so uncheck the other entries
65 int nCount = m_xRadioLB->n_children();
66 for (int i = 0; i < nCount; ++i)
68 if (i != nRow)
69 m_xRadioLB->set_toggle(i, TRISTATE_FALSE);
74 IMPL_LINK(SvxMultiPathDialog, CheckHdl_Impl, const weld::TreeView::iter_col&, rRowCol, void)
76 HandleEntryChecked(m_xRadioLB->get_iter_index_in_parent(rRowCol.first));
79 void SvxMultiPathDialog::AppendEntry(const OUString& rText, const OUString& rId)
81 m_xRadioLB->append();
82 const int nRow = m_xRadioLB->n_children() - 1;
83 m_xRadioLB->set_toggle(nRow, TRISTATE_FALSE);
84 m_xRadioLB->set_text(nRow, rText, 0);
85 m_xRadioLB->set_id(nRow, rId);
88 IMPL_LINK_NOARG(SvxMultiPathDialog, AddHdl_Impl, weld::Button&, void)
90 const Reference < XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
91 Reference < XFolderPicker2 > xFolderPicker = sfx2::createFolderPicker(xContext, m_xDialog.get());
93 if ( xFolderPicker->execute() != ExecutableDialogResults::OK )
94 return;
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);
118 IMPL_LINK_NOARG(SvxPathSelectDialog, AddHdl_Impl, weld::Button&, void)
120 const Reference < XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
121 Reference < XFolderPicker2 > xFolderPicker = sfx2::createFolderPicker(xContext, m_xDialog.get());
123 if ( xFolderPicker->execute() != ExecutableDialogResults::OK )
124 return;
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);
148 IMPL_LINK_NOARG(SvxMultiPathDialog, DelHdl_Impl, weld::Button&, void)
150 int nPos = m_xRadioLB->get_selected_index();
151 bool bChecked = m_xRadioLB->get_toggle(nPos) == TRISTATE_TRUE;
152 m_xRadioLB->remove(nPos);
153 int nCnt = m_xRadioLB->n_children();
154 if (nCnt)
156 --nCnt;
158 if ( nPos > nCnt )
159 nPos = nCnt;
160 if (bChecked)
162 m_xRadioLB->set_toggle(nPos, TRISTATE_TRUE);
163 HandleEntryChecked(nPos);
165 m_xRadioLB->select(nPos);
168 SelectHdl_Impl(*m_xRadioLB);
171 IMPL_LINK_NOARG(SvxPathSelectDialog, DelHdl_Impl, weld::Button&, void)
173 int nPos = m_xPathLB->get_selected_index();
174 m_xPathLB->remove(nPos);
175 int nCnt = m_xPathLB->n_children();
177 if (nCnt)
179 --nCnt;
181 if ( nPos > nCnt )
182 nPos = nCnt;
183 m_xPathLB->select(nPos);
186 SelectHdl_Impl(*m_xPathLB);
189 SvxMultiPathDialog::SvxMultiPathDialog(weld::Window* pParent)
190 : GenericDialogController(pParent, u"cui/ui/multipathdialog.ui"_ustr, u"MultiPathDialog"_ustr)
191 , m_xRadioLB(m_xBuilder->weld_tree_view(u"paths"_ustr))
192 , m_xAddBtn(m_xBuilder->weld_button(u"add"_ustr))
193 , m_xDelBtn(m_xBuilder->weld_button(u"delete"_ustr))
195 m_xRadioLB->set_size_request(m_xRadioLB->get_approximate_digit_width() * 60,
196 m_xRadioLB->get_text_height() * 10);
197 m_xRadioLB->enable_toggle_buttons(weld::ColumnToggleType::Radio);
198 m_xRadioLB->connect_toggled(LINK(this, SvxMultiPathDialog, CheckHdl_Impl));
199 m_xRadioLB->connect_selection_changed(LINK(this, SvxMultiPathDialog, SelectHdl_Impl));
201 m_xAddBtn->connect_clicked(LINK(this, SvxMultiPathDialog, AddHdl_Impl));
202 m_xDelBtn->connect_clicked(LINK(this, SvxMultiPathDialog, DelHdl_Impl));
204 SelectHdl_Impl(*m_xRadioLB);
207 SvxPathSelectDialog::SvxPathSelectDialog(weld::Window* pParent)
208 : GenericDialogController(pParent, u"cui/ui/selectpathdialog.ui"_ustr, u"SelectPathDialog"_ustr)
209 , m_xPathLB(m_xBuilder->weld_tree_view(u"paths"_ustr))
210 , m_xAddBtn(m_xBuilder->weld_button(u"add"_ustr))
211 , m_xDelBtn(m_xBuilder->weld_button(u"delete"_ustr))
213 m_xPathLB->set_size_request(m_xPathLB->get_approximate_digit_width() * 60,
214 m_xPathLB->get_text_height() * 10);
216 m_xPathLB->connect_selection_changed(LINK(this, SvxPathSelectDialog, SelectHdl_Impl));
217 m_xAddBtn->connect_clicked(LINK(this, SvxPathSelectDialog, AddHdl_Impl));
218 m_xDelBtn->connect_clicked(LINK(this, SvxPathSelectDialog, DelHdl_Impl));
220 SelectHdl_Impl(*m_xPathLB);
223 SvxMultiPathDialog::~SvxMultiPathDialog()
227 OUString SvxMultiPathDialog::GetPath() const
229 OUStringBuffer sNewPath;
230 sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
232 OUString sWritable;
233 for (int i = 0, nCount = m_xRadioLB->n_children(); i < nCount; ++i)
235 if (m_xRadioLB->get_toggle(i) == TRISTATE_TRUE)
236 sWritable = m_xRadioLB->get_id(i);
237 else
239 if (!sNewPath.isEmpty())
240 sNewPath.append(cDelim);
241 sNewPath.append(m_xRadioLB->get_id(i));
244 if (!sNewPath.isEmpty())
245 sNewPath.append(cDelim);
246 sNewPath.append(sWritable);
248 return sNewPath.makeStringAndClear();
251 OUString SvxPathSelectDialog::GetPath() const
253 OUStringBuffer sNewPath;
255 for (int i = 0; i < m_xPathLB->n_children(); ++i)
257 if ( !sNewPath.isEmpty() )
258 sNewPath.append(SVT_SEARCHPATH_DELIMITER);
259 sNewPath.append( m_xPathLB->get_id(i));
262 return sNewPath.makeStringAndClear();
265 void SvxMultiPathDialog::SetPath( std::u16string_view rPath )
267 if ( !rPath.empty() )
269 const sal_Unicode cDelim = SVT_SEARCHPATH_DELIMITER;
270 int nCount = 0;
271 sal_Int32 nIndex = 0;
274 const OUString sPath( o3tl::getToken(rPath, 0, cDelim, nIndex ) );
275 OUString sSystemPath;
276 bool bIsSystemPath =
277 osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
279 const OUString sEntry((bIsSystemPath ? sSystemPath : sPath));
280 AppendEntry(sEntry, sPath);
281 ++nCount;
283 while (nIndex >= 0);
285 if (nCount)
287 m_xRadioLB->set_toggle(nCount - 1, TRISTATE_TRUE);
288 HandleEntryChecked(nCount - 1);
292 SelectHdl_Impl(*m_xRadioLB);
295 void SvxPathSelectDialog::SetPath(std::u16string_view rPath)
297 if ( !rPath.empty() )
299 sal_Int32 nIndex = 0;
302 const OUString sPath( o3tl::getToken(rPath, 0, SVT_SEARCHPATH_DELIMITER, nIndex ) );
303 OUString sSystemPath;
304 bool bIsSystemPath =
305 osl::FileBase::getSystemPathFromFileURL(sPath, sSystemPath) == osl::FileBase::E_None;
307 m_xPathLB->append(sPath, bIsSystemPath ? sSystemPath : sPath);
309 while (nIndex >= 0);
312 SelectHdl_Impl(*m_xPathLB);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */