1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 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/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 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 . 18 */ 19 20 #include "doclinkdialog.hxx" 21 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> 33 34 namespace svx 35 { 36 using namespace ::com::sun::star; 37 using namespace ::com::sun::star::uno; 38 using namespace ::com::sun::star::ucb; 39 using namespace ::svt; 40 ODocumentLinkDialog(weld::Window * pParent,bool _bCreateNew)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"))) 48 { 49 if (!_bCreateNew) 50 m_xDialog->set_title(m_xAltTitle->get_label()); 51 52 m_xURL->SetSmartProtocol(INetProtocol::File); 53 m_xURL->DisableHistory(); 54 m_xURL->SetFilter("*.odb"); 55 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) ); 60 61 validate(); 62 } 63 ~ODocumentLinkDialog()64 ODocumentLinkDialog::~ODocumentLinkDialog() 65 { 66 } 67 setLink(const OUString & rName,const OUString & rURL)68 void ODocumentLinkDialog::setLink(const OUString& rName, const OUString& rURL) 69 { 70 m_xName->set_text(rName); 71 m_xURL->set_entry_text(rURL); 72 validate(); 73 } 74 getLink(OUString & rName,OUString & rURL) const75 void ODocumentLinkDialog::getLink(OUString& rName, OUString& rURL) const 76 { 77 rName = m_xName->get_text(); 78 rURL = m_xURL->get_active_text(); 79 } 80 validate()81 void ODocumentLinkDialog::validate( ) 82 { 83 m_xOK->set_sensitive((!m_xName->get_text().isEmpty()) && (!m_xURL->get_active_text().isEmpty())); 84 } 85 IMPL_LINK_NOARG(ODocumentLinkDialog,OnOk,weld::Button &,void)86 IMPL_LINK_NOARG(ODocumentLinkDialog, OnOk, weld::Button&, void) 87 { 88 // get the current URL 89 OUString sURL = m_xURL->get_active_text(); 90 OFileNotation aTransformer(sURL); 91 sURL = aTransformer.get(OFileNotation::N_URL); 92 93 // check for the existence of the selected file 94 bool bFileExists = false; 95 try 96 { 97 ::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext()); 98 if (aFile.isDocument()) 99 bFileExists = true; 100 } 101 catch(Exception&) 102 { 103 } 104 105 if (!bFileExists) 106 { 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 ) 116 { 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; 123 } 124 125 OUString sCurrentText = m_xName->get_text(); 126 if ( m_aNameValidator.IsSet() ) 127 { 128 if ( !m_aNameValidator.Call( sCurrentText ) ) 129 { 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(); 135 136 m_xName->select_region(0, -1); 137 m_xName->grab_focus(); 138 return; 139 } 140 } 141 142 m_xDialog->response(RET_OK); 143 } 144 IMPL_LINK_NOARG(ODocumentLinkDialog,OnBrowseFile,weld::Button &,void)145 IMPL_LINK_NOARG(ODocumentLinkDialog, OnBrowseFile, weld::Button&, void) 146 { 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 ) 151 { 152 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension()); 153 aFileDlg.SetCurrentFilter(pFilter->GetUIName()); 154 } 155 156 OUString sPath = m_xURL->get_active_text(); 157 if (!sPath.isEmpty()) 158 { 159 OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM ); 160 aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) ); 161 } 162 163 if (ERRCODE_NONE != aFileDlg.Execute()) 164 return; 165 166 if (m_xName->get_text().isEmpty()) 167 { // default the name to the base of the chosen URL 168 INetURLObject aParser; 169 170 aParser.SetSmartProtocol(INetProtocol::File); 171 aParser.SetSmartURL(aFileDlg.GetPath()); 172 173 m_xName->set_text(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset)); 174 175 m_xName->select_region(0, -1); 176 m_xName->grab_focus(); 177 } 178 else 179 m_xURL->grab_focus(); 180 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)); 184 185 validate(); 186 } 187 IMPL_LINK_NOARG(ODocumentLinkDialog,OnEntryModified,weld::Entry &,void)188 IMPL_LINK_NOARG(ODocumentLinkDialog, OnEntryModified, weld::Entry&, void) 189 { 190 validate(); 191 } 192 IMPL_LINK_NOARG(ODocumentLinkDialog,OnComboBoxModified,weld::ComboBox &,void)193 IMPL_LINK_NOARG(ODocumentLinkDialog, OnComboBoxModified, weld::ComboBox&, void) 194 { 195 validate(); 196 } 197 } 198 199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 200