Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cui / source / options / doclinkdialog.cxx
blob1baf362e778bde6c4d695c4194f695b6f6257fc2
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 <comphelper/processfactory.hxx>
24 #include <strings.hrc>
25 #include <svl/filenotation.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/weld.hxx>
28 #include <ucbhelper/content.hxx>
29 #include <dialmgr.hxx>
30 #include <tools/urlobj.hxx>
31 #include <sfx2/filedlghelper.hxx>
32 #include <sfx2/docfilt.hxx>
34 namespace svx
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::ucb;
39 using namespace ::svt;
41 ODocumentLinkDialog::ODocumentLinkDialog(weld::Window* pParent, bool _bCreateNew)
42 : GenericDialogController(pParent, "cui/ui/databaselinkdialog.ui", "DatabaseLinkDialog")
43 , m_xBrowseFile(m_xBuilder->weld_button("browse"))
44 , m_xName(m_xBuilder->weld_entry("name"))
45 , m_xOK(m_xBuilder->weld_button("ok"))
46 , m_xAltTitle(m_xBuilder->weld_label("alttitle"))
47 , m_xURL(new URLBox(m_xBuilder->weld_combo_box("url")))
49 if (!_bCreateNew)
50 m_xDialog->set_title(m_xAltTitle->get_label());
52 m_xURL->SetSmartProtocol(INetProtocol::File);
53 m_xURL->DisableHistory();
54 m_xURL->SetFilter("*.odb");
56 m_xName->connect_changed( LINK(this, ODocumentLinkDialog, OnEntryModified) );
57 m_xURL->connect_changed( LINK(this, ODocumentLinkDialog, OnComboBoxModified) );
58 m_xBrowseFile->connect_clicked( LINK(this, ODocumentLinkDialog, OnBrowseFile) );
59 m_xOK->connect_clicked( LINK(this, ODocumentLinkDialog, OnOk) );
61 validate();
64 ODocumentLinkDialog::~ODocumentLinkDialog()
68 void ODocumentLinkDialog::setLink(const OUString& rName, const OUString& rURL)
70 m_xName->set_text(rName);
71 m_xURL->set_entry_text(rURL);
72 validate();
75 void ODocumentLinkDialog::getLink(OUString& rName, OUString& rURL) const
77 rName = m_xName->get_text();
78 rURL = m_xURL->get_active_text();
81 void ODocumentLinkDialog::validate( )
83 m_xOK->set_sensitive((!m_xName->get_text().isEmpty()) && (!m_xURL->get_active_text().isEmpty()));
86 IMPL_LINK_NOARG(ODocumentLinkDialog, OnOk, weld::Button&, void)
88 // get the current URL
89 OUString sURL = m_xURL->get_active_text();
90 OFileNotation aTransformer(sURL);
91 sURL = aTransformer.get(OFileNotation::N_URL);
93 // check for the existence of the selected file
94 bool bFileExists = false;
95 try
97 ::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
98 if (aFile.isDocument())
99 bFileExists = true;
101 catch(Exception&)
105 if (!bFileExists)
107 OUString sMsg = CuiResId(STR_LINKEDDOC_DOESNOTEXIST);
108 sMsg = sMsg.replaceFirst("$file$", m_xURL->get_active_text());
109 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
110 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
111 xErrorBox->run();
112 return;
113 } // if (!bFileExists)
114 INetURLObject aURL( sURL );
115 if ( aURL.GetProtocol() != INetProtocol::File )
117 OUString sMsg = CuiResId(STR_LINKEDDOC_NO_SYSTEM_FILE);
118 sMsg = sMsg.replaceFirst("$file$", m_xURL->get_active_text());
119 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
120 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
121 xErrorBox->run();
122 return;
125 OUString sCurrentText = m_xName->get_text();
126 if ( m_aNameValidator.IsSet() )
128 if ( !m_aNameValidator.Call( sCurrentText ) )
130 OUString sMsg = CuiResId(STR_NAME_CONFLICT);
131 sMsg = sMsg.replaceFirst("$file$", sCurrentText);
132 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
133 VclMessageType::Info, VclButtonsType::Ok, sMsg));
134 xErrorBox->run();
136 m_xName->select_region(0, -1);
137 m_xName->grab_focus();
138 return;
142 m_xDialog->response(RET_OK);
145 IMPL_LINK_NOARG(ODocumentLinkDialog, OnBrowseFile, weld::Button&, void)
147 ::sfx2::FileDialogHelper aFileDlg(
148 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION, FileDialogFlags::NONE, m_xDialog.get());
149 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName("StarOffice XML (Base)");
150 if ( pFilter )
152 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
153 aFileDlg.SetCurrentFilter(pFilter->GetUIName());
156 OUString sPath = m_xURL->get_active_text();
157 if (!sPath.isEmpty())
159 OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM );
160 aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) );
163 if (ERRCODE_NONE != aFileDlg.Execute())
164 return;
166 if (m_xName->get_text().isEmpty())
167 { // default the name to the base of the chosen URL
168 INetURLObject aParser;
170 aParser.SetSmartProtocol(INetProtocol::File);
171 aParser.SetSmartURL(aFileDlg.GetPath());
173 m_xName->set_text(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset));
175 m_xName->select_region(0, -1);
176 m_xName->grab_focus();
178 else
179 m_xURL->grab_focus();
181 // get the path in system notation
182 OFileNotation aTransformer(aFileDlg.GetPath(), OFileNotation::N_URL);
183 m_xURL->set_entry_text(aTransformer.get(OFileNotation::N_SYSTEM));
185 validate();
188 IMPL_LINK_NOARG(ODocumentLinkDialog, OnEntryModified, weld::Entry&, void)
190 validate();
193 IMPL_LINK_NOARG(ODocumentLinkDialog, OnComboBoxModified, weld::ComboBox&, void)
195 validate();
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */