Bump version to 4.3-4
[LibreOffice.git] / cui / source / options / doclinkdialog.cxx
blob7c8c2e0eeb407d5d985c72e100d2912db63659fa
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/msgbox.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;
43 //= ODocumentLinkDialog
46 ODocumentLinkDialog::ODocumentLinkDialog( Window* _pParent, bool _bCreateNew )
47 : ModalDialog(_pParent, "DatabaseLinkDialog",
48 "cui/ui/databaselinkdialog.ui")
49 ,m_bCreatingNew(_bCreateNew)
51 get(m_pURL, "url");
52 get(m_pOK, "ok");
53 get(m_pName, "name");
54 get(m_pBrowseFile, "browse");
56 if (!m_bCreatingNew)
57 SetText(get<FixedText>("alttitle")->GetText());
59 m_pURL->SetFilter("*.odb");
61 m_pName->SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
62 m_pURL->SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
63 m_pBrowseFile->SetClickHdl( LINK(this, ODocumentLinkDialog, OnBrowseFile) );
64 m_pOK->SetClickHdl( LINK(this, ODocumentLinkDialog, OnOk) );
66 m_pURL->SetDropDownLineCount(10);
68 validate();
70 m_pURL->SetDropDownLineCount( 5 );
74 void ODocumentLinkDialog::setLink( const OUString& _rName, const OUString& _rURL )
76 m_pName->SetText(_rName);
77 m_pURL->SetText(_rURL);
78 validate();
82 void ODocumentLinkDialog::getLink( OUString& _rName, OUString& _rURL ) const
84 _rName = m_pName->GetText();
85 _rURL = m_pURL->GetText();
89 void ODocumentLinkDialog::validate( )
92 m_pOK->Enable( ( !m_pName->GetText().isEmpty()) && ( !m_pURL->GetText().isEmpty() ) );
96 IMPL_LINK_NOARG(ODocumentLinkDialog, OnOk)
98 // get the current URL
99 OUString sURL = m_pURL->GetText();
100 OFileNotation aTransformer(sURL);
101 sURL = aTransformer.get(OFileNotation::N_URL);
103 // check for the existence of the selected file
104 bool bFileExists = false;
107 ::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
108 if (aFile.isDocument())
109 bFileExists = true;
111 catch(Exception&)
115 if (!bFileExists)
117 OUString sMsg = CUI_RES(STR_LINKEDDOC_DOESNOTEXIST);
118 sMsg = sMsg.replaceFirst("$file$", m_pURL->GetText());
119 ErrorBox aError(this, WB_OK , sMsg);
120 aError.Execute();
121 return 0L;
122 } // if (!bFileExists)
123 INetURLObject aURL( sURL );
124 if ( aURL.GetProtocol() != INET_PROT_FILE )
126 OUString sMsg = CUI_RES(STR_LINKEDDOC_NO_SYSTEM_FILE);
127 sMsg = sMsg.replaceFirst("$file$", m_pURL->GetText());
128 ErrorBox aError(this, WB_OK , sMsg);
129 aError.Execute();
130 return 0L;
133 OUString sCurrentText = m_pName->GetText();
134 if ( m_aNameValidator.IsSet() )
136 if ( !m_aNameValidator.Call( &sCurrentText ) )
138 OUString sMsg = CUI_RES(STR_NAME_CONFLICT);
139 sMsg = sMsg.replaceFirst("$file$", sCurrentText);
140 InfoBox aError(this, sMsg);
141 aError.Execute();
143 m_pName->SetSelection(Selection(0,sCurrentText.getLength()));
144 m_pName->GrabFocus();
145 return 0L;
149 EndDialog(RET_OK);
150 return 0L;
154 IMPL_LINK_NOARG(ODocumentLinkDialog, OnBrowseFile)
156 ::sfx2::FileDialogHelper aFileDlg(
157 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION, 0);
158 const SfxFilter* pFilter = SfxFilter::GetFilterByName(OUString("StarOffice XML (Base)"));
159 if ( pFilter )
161 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
162 aFileDlg.SetCurrentFilter(pFilter->GetUIName());
165 OUString sPath = m_pURL->GetText();
166 if (!sPath.isEmpty())
168 OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM );
169 aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) );
172 if (0 != aFileDlg.Execute())
173 return 0L;
175 if (m_pName->GetText().isEmpty())
176 { // default the name to the base of the chosen URL
177 INetURLObject aParser;
179 aParser.SetSmartProtocol(INET_PROT_FILE);
180 aParser.SetSmartURL(aFileDlg.GetPath());
182 m_pName->SetText(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET));
184 m_pName->SetSelection(Selection(0,m_pName->GetText().getLength()));
185 m_pName->GrabFocus();
187 else
188 m_pURL->GrabFocus();
190 // get the path in system notation
191 OFileNotation aTransformer(aFileDlg.GetPath(), OFileNotation::N_URL);
192 m_pURL->SetText(aTransformer.get(OFileNotation::N_SYSTEM));
194 validate();
195 return 0L;
199 IMPL_LINK_NOARG(ODocumentLinkDialog, OnTextModified)
201 validate( );
202 return 0L;
206 } // namespace svx
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */