1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
21 #include "doclinkdialog.hrc"
23 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
24 #include <comphelper/processfactory.hxx>
26 #include <svl/filenotation.hxx>
27 #include <vcl/msgbox.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 //......................................................................
36 //......................................................................
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::ucb
;
41 using namespace ::svt
;
43 //==================================================================
44 //= ODocumentLinkDialog
45 //==================================================================
46 //------------------------------------------------------------------
47 ODocumentLinkDialog::ODocumentLinkDialog( Window
* _pParent
, sal_Bool _bCreateNew
)
48 :ModalDialog( _pParent
, CUI_RES(DLG_DOCUMENTLINK
) )
49 ,m_aURLLabel (this, CUI_RES(FT_URL
))
50 ,m_aURL (this, CUI_RES(CMB_URL
))
51 ,m_aBrowseFile (this, CUI_RES(PB_BROWSEFILE
))
52 ,m_aNameLabel (this, CUI_RES(FT_NAME
))
53 ,m_aName (this, CUI_RES(ET_NAME
))
54 ,m_aBottomLine (this, CUI_RES(FL_BOTTOM
))
55 ,m_aOK (this, CUI_RES(BTN_OK
))
56 ,m_aCancel (this, CUI_RES(BTN_CANCEL
))
57 ,m_aHelp (this, CUI_RES(BTN_HELP
))
58 ,m_bCreatingNew(_bCreateNew
)
60 String sText
= String( CUI_RES( m_bCreatingNew
? STR_NEW_LINK
: STR_EDIT_LINK
) );
65 OUString
sTemp("*.odb");
66 m_aURL
.SetFilter(sTemp
);
68 m_aName
.SetModifyHdl( LINK(this, ODocumentLinkDialog
, OnTextModified
) );
69 m_aURL
.SetModifyHdl( LINK(this, ODocumentLinkDialog
, OnTextModified
) );
70 m_aBrowseFile
.SetClickHdl( LINK(this, ODocumentLinkDialog
, OnBrowseFile
) );
71 m_aOK
.SetClickHdl( LINK(this, ODocumentLinkDialog
, OnOk
) );
73 m_aURL
.SetDropDownLineCount(10);
77 m_aURL
.SetDropDownLineCount( 5 );
80 //------------------------------------------------------------------
81 void ODocumentLinkDialog::set( const String
& _rName
, const String
& _rURL
)
83 m_aName
.SetText(_rName
);
84 m_aURL
.SetText(_rURL
);
88 //------------------------------------------------------------------
89 void ODocumentLinkDialog::get( String
& _rName
, String
& _rURL
) const
91 _rName
= m_aName
.GetText();
92 _rURL
= m_aURL
.GetText();
95 //------------------------------------------------------------------
96 void ODocumentLinkDialog::validate( )
99 m_aOK
.Enable( ( !m_aName
.GetText().isEmpty()) && ( !m_aURL
.GetText().isEmpty() ) );
102 //------------------------------------------------------------------
103 IMPL_LINK_NOARG(ODocumentLinkDialog
, OnOk
)
105 // get the current URL
106 OUString sURL
= m_aURL
.GetText();
107 OFileNotation
aTransformer(sURL
);
108 sURL
= aTransformer
.get(OFileNotation::N_URL
);
110 // check for the existence of the selected file
111 sal_Bool bFileExists
= sal_False
;
114 ::ucbhelper::Content
aFile(sURL
, Reference
< XCommandEnvironment
>(), comphelper::getProcessComponentContext());
115 if (aFile
.isDocument())
116 bFileExists
= sal_True
;
124 String sMsg
= String(CUI_RES(STR_LINKEDDOC_DOESNOTEXIST
));
125 sMsg
.SearchAndReplaceAscii("$file$", m_aURL
.GetText());
126 ErrorBox
aError(this, WB_OK
, sMsg
);
129 } // if (!bFileExists)
130 INetURLObject
aURL( sURL
);
131 if ( aURL
.GetProtocol() != INET_PROT_FILE
)
133 String sMsg
= String(CUI_RES(STR_LINKEDDOC_NO_SYSTEM_FILE
));
134 sMsg
.SearchAndReplaceAscii("$file$", m_aURL
.GetText());
135 ErrorBox
aError(this, WB_OK
, sMsg
);
140 String sCurrentText
= m_aName
.GetText();
141 if ( m_aNameValidator
.IsSet() )
143 if ( !m_aNameValidator
.Call( &sCurrentText
) )
145 String sMsg
= String(CUI_RES(STR_NAME_CONFLICT
));
146 sMsg
.SearchAndReplaceAscii("$file$", sCurrentText
);
147 InfoBox
aError(this, sMsg
);
150 m_aName
.SetSelection(Selection(0,sCurrentText
.Len()));
160 //------------------------------------------------------------------
161 IMPL_LINK_NOARG(ODocumentLinkDialog
, OnBrowseFile
)
163 ::sfx2::FileDialogHelper
aFileDlg(
164 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION
, 0);
165 const SfxFilter
* pFilter
= SfxFilter::GetFilterByName(OUString("StarOffice XML (Base)"));
168 aFileDlg
.AddFilter(pFilter
->GetUIName(),pFilter
->GetDefaultExtension());
169 aFileDlg
.SetCurrentFilter(pFilter
->GetUIName());
172 String sPath
= m_aURL
.GetText();
175 OFileNotation
aTransformer( sPath
, OFileNotation::N_SYSTEM
);
176 aFileDlg
.SetDisplayDirectory( aTransformer
.get( OFileNotation::N_URL
) );
179 if (0 != aFileDlg
.Execute())
182 if (m_aName
.GetText().isEmpty())
183 { // default the name to the base of the chosen URL
184 INetURLObject aParser
;
186 aParser
.SetSmartProtocol(INET_PROT_FILE
);
187 aParser
.SetSmartURL(aFileDlg
.GetPath());
189 m_aName
.SetText(aParser
.getBase(INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
));
191 m_aName
.SetSelection(Selection(0,m_aName
.GetText().getLength()));
197 // get the path in system notation
198 OFileNotation
aTransformer(aFileDlg
.GetPath(), OFileNotation::N_URL
);
199 m_aURL
.SetText(aTransformer
.get(OFileNotation::N_SYSTEM
));
205 //------------------------------------------------------------------
206 IMPL_LINK_NOARG(ODocumentLinkDialog
, OnTextModified
)
212 //......................................................................
214 //......................................................................
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */