cid#1636690 Dereference after null check
[LibreOffice.git] / cui / source / options / doclinkdialog.cxx
blob76d8126a9b502dfa25a42296442fa91b1874971e
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 "doclinkdialog.hxx"
22 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <officecfg/Office/DataAccess.hxx>
28 #include <strings.hrc>
29 #include <svl/filenotation.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/weld.hxx>
32 #include <ucbhelper/content.hxx>
33 #include <dialmgr.hxx>
34 #include <tools/urlobj.hxx>
35 #include <sfx2/filedlghelper.hxx>
36 #include <sfx2/docfilt.hxx>
38 namespace svx
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::ucb;
43 using namespace ::svt;
45 ODocumentLinkDialog::ODocumentLinkDialog(weld::Window* pParent, bool _bCreateNew)
46 : GenericDialogController(pParent, u"cui/ui/databaselinkdialog.ui"_ustr, u"DatabaseLinkDialog"_ustr)
47 , m_xBrowseFile(m_xBuilder->weld_button(u"browse"_ustr))
48 , m_xName(m_xBuilder->weld_entry(u"name"_ustr))
49 , m_xOK(m_xBuilder->weld_button(u"ok"_ustr))
50 , m_xAltTitle(m_xBuilder->weld_label(u"alttitle"_ustr))
51 , m_xURL(new SvtURLBox(m_xBuilder->weld_combo_box(u"url"_ustr)))
53 if (!_bCreateNew)
54 m_xDialog->set_title(m_xAltTitle->get_label());
56 m_xURL->SetSmartProtocol(INetProtocol::File);
57 m_xURL->DisableHistory();
58 m_xURL->SetFilter(u"*.odb");
60 const css::uno::Reference < css::uno::XComponentContext >& xContext(::comphelper::getProcessComponentContext());
61 m_xReadWriteAccess = css::configuration::ReadWriteAccess::create(xContext, u"*"_ustr);
63 m_xName->connect_changed( LINK(this, ODocumentLinkDialog, OnEntryModified) );
64 m_xURL->connect_changed( LINK(this, ODocumentLinkDialog, OnComboBoxModified) );
65 m_xBrowseFile->connect_clicked( LINK(this, ODocumentLinkDialog, OnBrowseFile) );
66 m_xOK->connect_clicked( LINK(this, ODocumentLinkDialog, OnOk) );
68 validate();
71 ODocumentLinkDialog::~ODocumentLinkDialog()
75 void ODocumentLinkDialog::setLink(const OUString& rName, const OUString& rURL)
77 m_xName->set_text(rName);
78 m_xURL->set_entry_text(rURL);
79 validate();
82 void ODocumentLinkDialog::getLink(OUString& rName, OUString& rURL) const
84 rName = m_xName->get_text();
85 rURL = m_xURL->get_active_text();
88 void ODocumentLinkDialog::validate( )
90 m_xOK->set_sensitive((!m_xName->get_text().isEmpty()) && (!m_xURL->get_active_text().isEmpty()));
92 if (m_xOK->get_sensitive())
94 Reference<container::XNameAccess> xItemList = officecfg::Office::DataAccess::RegisteredNames::get();
95 Sequence< OUString > lNodeNames = xItemList->getElementNames();
97 for (const OUString& sNodeName : lNodeNames)
99 Reference<css::beans::XPropertySet> xSet;
100 xItemList->getByName(sNodeName) >>= xSet;
102 OUString aDatabaseName;
103 if (xSet->getPropertySetInfo()->hasPropertyByName(u"Name"_ustr))
104 xSet->getPropertyValue(u"Name"_ustr) >>= aDatabaseName;
106 if (!aDatabaseName.isEmpty() && m_xName->get_text() == aDatabaseName)
108 const OUString aConfigPath = officecfg::Office::DataAccess::RegisteredNames::path() + "/" + sNodeName;
109 if (m_xReadWriteAccess->hasPropertyByHierarchicalName(aConfigPath + "/Name"))
111 css::beans::Property aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName(aConfigPath + "/Name");
112 bool bReadOnly = (aProperty.Attributes & css::beans::PropertyAttribute::READONLY) != 0;
114 m_xURL->set_sensitive(!bReadOnly);
115 m_xBrowseFile->set_sensitive(!bReadOnly);
118 if (m_xReadWriteAccess->hasPropertyByHierarchicalName(aConfigPath + "/Location"))
120 css::beans::Property aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName(aConfigPath + "/Location");
121 bool bReadOnly = (aProperty.Attributes & css::beans::PropertyAttribute::READONLY) != 0;
123 m_xName->set_sensitive(!bReadOnly);
125 break;
131 IMPL_LINK_NOARG(ODocumentLinkDialog, OnOk, weld::Button&, void)
133 // get the current URL
134 OUString sURL = m_xURL->get_active_text();
135 OFileNotation aTransformer(sURL);
136 sURL = aTransformer.get(OFileNotation::N_URL);
138 // check for the existence of the selected file
139 bool bFileExists = false;
142 ::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
143 if (aFile.isDocument())
144 bFileExists = true;
146 catch(Exception&)
150 if (!bFileExists)
152 OUString sMsg = CuiResId(STR_LINKEDDOC_DOESNOTEXIST);
153 sMsg = sMsg.replaceFirst("$file$", m_xURL->get_active_text());
154 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
155 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
156 xErrorBox->run();
157 return;
158 } // if (!bFileExists)
159 INetURLObject aURL( sURL );
160 if ( aURL.GetProtocol() != INetProtocol::File )
162 OUString sMsg = CuiResId(STR_LINKEDDOC_NO_SYSTEM_FILE);
163 sMsg = sMsg.replaceFirst("$file$", m_xURL->get_active_text());
164 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
165 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
166 xErrorBox->run();
167 return;
170 OUString sCurrentText = m_xName->get_text();
171 if ( m_aNameValidator.IsSet() )
173 if ( !m_aNameValidator.Call( sCurrentText ) )
175 OUString sMsg = CuiResId(STR_NAME_CONFLICT);
176 sMsg = sMsg.replaceFirst("$file$", sCurrentText);
177 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
178 VclMessageType::Info, VclButtonsType::Ok, sMsg));
179 xErrorBox->run();
181 m_xName->select_region(0, -1);
182 m_xName->grab_focus();
183 return;
187 m_xDialog->response(RET_OK);
190 IMPL_LINK_NOARG(ODocumentLinkDialog, OnBrowseFile, weld::Button&, void)
192 ::sfx2::FileDialogHelper aFileDlg(
193 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION, FileDialogFlags::NONE, m_xDialog.get());
194 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName(u"StarOffice XML (Base)"_ustr);
195 if ( pFilter )
197 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
198 aFileDlg.SetCurrentFilter(pFilter->GetUIName());
201 OUString sPath = m_xURL->get_active_text();
202 if (!sPath.isEmpty())
204 OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM );
205 aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) );
208 if (ERRCODE_NONE != aFileDlg.Execute())
209 return;
211 if (m_xName->get_text().isEmpty())
212 { // default the name to the base of the chosen URL
213 INetURLObject aParser;
215 aParser.SetSmartProtocol(INetProtocol::File);
216 aParser.SetSmartURL(aFileDlg.GetPath());
218 m_xName->set_text(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset));
220 m_xName->select_region(0, -1);
221 m_xName->grab_focus();
223 else
224 m_xURL->grab_focus();
226 // get the path in system notation
227 OFileNotation aTransformer(aFileDlg.GetPath(), OFileNotation::N_URL);
228 m_xURL->set_entry_text(aTransformer.get(OFileNotation::N_SYSTEM));
230 validate();
233 IMPL_LINK_NOARG(ODocumentLinkDialog, OnEntryModified, weld::Entry&, void)
235 validate();
238 IMPL_LINK_NOARG(ODocumentLinkDialog, OnComboBoxModified, weld::ComboBox&, void)
240 validate();
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */