bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / options / doclinkdialog.cxx
blobf5f24b7413804aeea0c326cef384e8aa454b871c
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 <cuires.hrc>
25 #include <svl/filenotation.hxx>
26 #include <vcl/layout.hxx>
27 #include <ucbhelper/content.hxx>
28 #include <dialmgr.hxx>
29 #include <tools/urlobj.hxx>
30 #include <sfx2/filedlghelper.hxx>
31 #include <sfx2/docfilt.hxx>
33 namespace svx
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::ucb;
40 using namespace ::svt;
42 ODocumentLinkDialog::ODocumentLinkDialog( vcl::Window* _pParent, bool _bCreateNew )
43 : ModalDialog(_pParent, "DatabaseLinkDialog",
44 "cui/ui/databaselinkdialog.ui")
45 ,m_bCreatingNew(_bCreateNew)
47 get(m_pURL, "url");
48 get(m_pOK, "ok");
49 get(m_pName, "name");
50 get(m_pBrowseFile, "browse");
52 if (!m_bCreatingNew)
53 SetText(get<FixedText>("alttitle")->GetText());
55 m_pURL->SetFilter("*.odb");
57 m_pName->SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
58 m_pURL->SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
59 m_pBrowseFile->SetClickHdl( LINK(this, ODocumentLinkDialog, OnBrowseFile) );
60 m_pOK->SetClickHdl( LINK(this, ODocumentLinkDialog, OnOk) );
62 m_pURL->SetDropDownLineCount(10);
64 validate();
66 m_pURL->SetDropDownLineCount( 5 );
69 ODocumentLinkDialog::~ODocumentLinkDialog()
71 disposeOnce();
74 void ODocumentLinkDialog::dispose()
76 m_pURL.clear();
77 m_pBrowseFile.clear();
78 m_pName.clear();
79 m_pOK.clear();
80 ModalDialog::dispose();
84 void ODocumentLinkDialog::setLink( const OUString& _rName, const OUString& _rURL )
86 m_pName->SetText(_rName);
87 m_pURL->SetText(_rURL);
88 validate();
92 void ODocumentLinkDialog::getLink( OUString& _rName, OUString& _rURL ) const
94 _rName = m_pName->GetText();
95 _rURL = m_pURL->GetText();
99 void ODocumentLinkDialog::validate( )
102 m_pOK->Enable( ( !m_pName->GetText().isEmpty()) && ( !m_pURL->GetText().isEmpty() ) );
106 IMPL_LINK_NOARG(ODocumentLinkDialog, OnOk)
108 // get the current URL
109 OUString sURL = m_pURL->GetText();
110 OFileNotation aTransformer(sURL);
111 sURL = aTransformer.get(OFileNotation::N_URL);
113 // check for the existence of the selected file
114 bool bFileExists = false;
117 ::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
118 if (aFile.isDocument())
119 bFileExists = true;
121 catch(Exception&)
125 if (!bFileExists)
127 OUString sMsg = CUI_RES(STR_LINKEDDOC_DOESNOTEXIST);
128 sMsg = sMsg.replaceFirst("$file$", m_pURL->GetText());
129 ScopedVclPtrInstance< MessageDialog > aError(this, sMsg);
130 aError->Execute();
131 return 0L;
132 } // if (!bFileExists)
133 INetURLObject aURL( sURL );
134 if ( aURL.GetProtocol() != INetProtocol::File )
136 OUString sMsg = CUI_RES(STR_LINKEDDOC_NO_SYSTEM_FILE);
137 sMsg = sMsg.replaceFirst("$file$", m_pURL->GetText());
138 ScopedVclPtrInstance< MessageDialog > aError(this, sMsg);
139 aError->Execute();
140 return 0L;
143 OUString sCurrentText = m_pName->GetText();
144 if ( m_aNameValidator.IsSet() )
146 if ( !m_aNameValidator.Call( &sCurrentText ) )
148 OUString sMsg = CUI_RES(STR_NAME_CONFLICT);
149 sMsg = sMsg.replaceFirst("$file$", sCurrentText);
150 ScopedVclPtrInstance< MessageDialog > aError(this, sMsg, VCL_MESSAGE_INFO);
151 aError->Execute();
153 m_pName->SetSelection(Selection(0,sCurrentText.getLength()));
154 m_pName->GrabFocus();
155 return 0L;
159 EndDialog(RET_OK);
160 return 0L;
164 IMPL_LINK_NOARG(ODocumentLinkDialog, OnBrowseFile)
166 ::sfx2::FileDialogHelper aFileDlg(
167 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION, 0);
168 const SfxFilter* pFilter = SfxFilter::GetFilterByName(OUString("StarOffice XML (Base)"));
169 if ( pFilter )
171 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
172 aFileDlg.SetCurrentFilter(pFilter->GetUIName());
175 OUString sPath = m_pURL->GetText();
176 if (!sPath.isEmpty())
178 OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM );
179 aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) );
182 if (0 != aFileDlg.Execute())
183 return 0L;
185 if (m_pName->GetText().isEmpty())
186 { // default the name to the base of the chosen URL
187 INetURLObject aParser;
189 aParser.SetSmartProtocol(INetProtocol::File);
190 aParser.SetSmartURL(aFileDlg.GetPath());
192 m_pName->SetText(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET));
194 m_pName->SetSelection(Selection(0,m_pName->GetText().getLength()));
195 m_pName->GrabFocus();
197 else
198 m_pURL->GrabFocus();
200 // get the path in system notation
201 OFileNotation aTransformer(aFileDlg.GetPath(), OFileNotation::N_URL);
202 m_pURL->SetText(aTransformer.get(OFileNotation::N_SYSTEM));
204 validate();
205 return 0L;
209 IMPL_LINK_NOARG(ODocumentLinkDialog, OnTextModified)
211 validate( );
212 return 0L;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */