1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cui.hxx"
26 
27 #include "doclinkdialog.hxx"
28 #include "doclinkdialog.hrc"
29 #include <cuires.hrc>
30 #include <tools/debug.hxx>
31 #include <svl/filenotation.hxx>
32 #include <vcl/msgbox.hxx>
33 #include <ucbhelper/content.hxx>
34 #include <dialmgr.hxx>
35 #include <tools/urlobj.hxx>
36 #include <sfx2/filedlghelper.hxx>
37 #include <sfx2/docfilt.hxx>
38 //......................................................................
39 namespace svx
40 {
41 //......................................................................
42 
43 	using namespace ::com::sun::star::uno;
44 	using namespace ::com::sun::star::ucb;
45 	using namespace ::svt;
46 
47 	//==================================================================
48 	//= ODocumentLinkDialog
49 	//==================================================================
50 	//------------------------------------------------------------------
ODocumentLinkDialog(Window * _pParent,sal_Bool _bCreateNew)51 	ODocumentLinkDialog::ODocumentLinkDialog( Window* _pParent, sal_Bool _bCreateNew )
52 		:ModalDialog( _pParent, CUI_RES(DLG_DOCUMENTLINK) )
53         ,m_aURLLabel        (this, CUI_RES(FT_URL))
54         ,m_aURL             (this, CUI_RES(CMB_URL))
55         ,m_aBrowseFile      (this, CUI_RES(PB_BROWSEFILE))
56         ,m_aNameLabel       (this, CUI_RES(FT_NAME))
57 		,m_aName			(this, CUI_RES(ET_NAME))
58         ,m_aBottomLine      (this, CUI_RES(FL_BOTTOM))
59 		,m_aOK				(this, CUI_RES(BTN_OK))
60         ,m_aCancel          (this, CUI_RES(BTN_CANCEL))
61 		,m_aHelp			(this, CUI_RES(BTN_HELP))
62 		,m_bCreatingNew(_bCreateNew)
63 	{
64 		String sText = String( CUI_RES( m_bCreatingNew ? STR_NEW_LINK : STR_EDIT_LINK ) );
65 		SetText(sText);
66 
67 		FreeResource();
68 
69 		String sTemp = String::CreateFromAscii("*.odb");
70 		m_aURL.SetFilter(sTemp);
71 
72 		m_aName.SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
73 		m_aURL.SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
74 		m_aBrowseFile.SetClickHdl( LINK(this, ODocumentLinkDialog, OnBrowseFile) );
75 		m_aOK.SetClickHdl( LINK(this, ODocumentLinkDialog, OnOk) );
76 
77 		m_aURL.SetDropDownLineCount(10);
78 
79 		validate();
80 
81 		//	m_aURL.SetHelpId( HID_DOCLINKEDIT_URL );
82 		m_aURL.SetDropDownLineCount( 5 );
83 	}
84 
85 	//------------------------------------------------------------------
set(const String & _rName,const String & _rURL)86 	void ODocumentLinkDialog::set( const String& _rName, const String& _rURL )
87 	{
88 		m_aName.SetText(_rName);
89 		m_aURL.SetText(_rURL);
90 		validate();
91 	}
92 
93 	//------------------------------------------------------------------
get(String & _rName,String & _rURL) const94 	void ODocumentLinkDialog::get( String& _rName, String& _rURL ) const
95 	{
96 		_rName = m_aName.GetText();
97 		_rURL = m_aURL.GetText();
98 	}
99 
100 	//------------------------------------------------------------------
validate()101 	void ODocumentLinkDialog::validate( )
102 	{
103 
104 		m_aOK.Enable( (0 != m_aName.GetText().Len()) && ( 0 != m_aURL.GetText().Len() ) );
105 	}
106 
107 	//------------------------------------------------------------------
IMPL_LINK(ODocumentLinkDialog,OnOk,void *,EMPTYARG)108     IMPL_LINK( ODocumentLinkDialog, OnOk, void*, EMPTYARG )
109 	{
110 		// get the current URL
111 		::rtl::OUString sURL = m_aURL.GetText();
112 		OFileNotation aTransformer(sURL);
113 		sURL = aTransformer.get(OFileNotation::N_URL);
114 
115 		// check for the existence of the selected file
116 		sal_Bool bFileExists = sal_False;
117 		try
118 		{
119 			::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >());
120 			if (aFile.isDocument())
121 				bFileExists = sal_True;
122 		}
123 		catch(Exception&)
124 		{
125 		}
126 
127 		if (!bFileExists)
128 		{
129 			String sMsg = String(CUI_RES(STR_LINKEDDOC_DOESNOTEXIST));
130 			sMsg.SearchAndReplaceAscii("$file$", m_aURL.GetText());
131 			ErrorBox aError(this, WB_OK , sMsg);
132 			aError.Execute();
133 			return 0L;
134 		} // if (!bFileExists)
135         INetURLObject aURL( sURL );
136         if ( aURL.GetProtocol() != INET_PROT_FILE )
137         {
138             String sMsg = String(CUI_RES(STR_LINKEDDOC_NO_SYSTEM_FILE));
139 			sMsg.SearchAndReplaceAscii("$file$", m_aURL.GetText());
140 			ErrorBox aError(this, WB_OK , sMsg);
141 			aError.Execute();
142 			return 0L;
143         }
144 
145 		String sCurrentText = m_aName.GetText();
146 		if ( m_aNameValidator.IsSet() )
147 		{
148 			if ( !m_aNameValidator.Call( &sCurrentText ) )
149 			{
150 				String sMsg = String(CUI_RES(STR_NAME_CONFLICT));
151 				sMsg.SearchAndReplaceAscii("$file$", sCurrentText);
152 				InfoBox aError(this, sMsg);
153 				aError.Execute();
154 
155 				m_aName.SetSelection(Selection(0,sCurrentText.Len()));
156 				m_aName.GrabFocus();
157 				return 0L;
158 			}
159 		}
160 
161 		EndDialog(RET_OK);
162 		return 0L;
163 	}
164 
165 	//------------------------------------------------------------------
IMPL_LINK(ODocumentLinkDialog,OnBrowseFile,void *,EMPTYARG)166     IMPL_LINK( ODocumentLinkDialog, OnBrowseFile, void*, EMPTYARG )
167 	{
168 		::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK | WB_STDMODAL | WB_OPEN);
169 		static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
170 		const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
171 		if ( pFilter )
172 		{
173 			aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
174 			aFileDlg.SetCurrentFilter(pFilter->GetUIName());
175 		}
176 
177 		String sPath = m_aURL.GetText();
178 		if (sPath.Len())
179 		{
180 			OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM );
181 			aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) );
182 		}
183 
184 		if (0 != aFileDlg.Execute())
185 			return 0L;
186 
187 		if (0 == m_aName.GetText().Len())
188 		{	// default the name to the base of the chosen URL
189 			INetURLObject aParser;
190 
191 			aParser.SetSmartProtocol(INET_PROT_FILE);
192 			aParser.SetSmartURL(aFileDlg.GetPath());
193 
194 			m_aName.SetText(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET));
195 
196 			m_aName.SetSelection(Selection(0,m_aName.GetText().Len()));
197 			m_aName.GrabFocus();
198 		}
199 		else
200 			m_aURL.GrabFocus();
201 
202 		// get the path in system notation
203 		OFileNotation aTransformer(aFileDlg.GetPath(), OFileNotation::N_URL);
204 		m_aURL.SetText(aTransformer.get(OFileNotation::N_SYSTEM));
205 
206 		validate();
207 		return 0L;
208 	}
209 
210 	//------------------------------------------------------------------
IMPL_LINK(ODocumentLinkDialog,OnTextModified,Control *,EMPTYARG)211     IMPL_LINK( ODocumentLinkDialog, OnTextModified, Control*, EMPTYARG )
212 	{
213 		validate( );
214 		return 0L;
215 	}
216 
217 //......................................................................
218 }	// namespace svx
219 //......................................................................
220 
221