1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 
31 #include "doclinkdialog.hxx"
32 #include "doclinkdialog.hrc"
33 #include <cuires.hrc>
34 #include <tools/debug.hxx>
35 #include <svl/filenotation.hxx>
36 #include <vcl/msgbox.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <dialmgr.hxx>
39 #include <tools/urlobj.hxx>
40 #include <sfx2/filedlghelper.hxx>
41 #include <sfx2/docfilt.hxx>
42 //......................................................................
43 namespace svx
44 {
45 //......................................................................
46 
47 	using namespace ::com::sun::star::uno;
48 	using namespace ::com::sun::star::ucb;
49 	using namespace ::svt;
50 
51 	//==================================================================
52 	//= ODocumentLinkDialog
53 	//==================================================================
54 	//------------------------------------------------------------------
55 	ODocumentLinkDialog::ODocumentLinkDialog( Window* _pParent, sal_Bool _bCreateNew )
56 		:ModalDialog( _pParent, CUI_RES(DLG_DOCUMENTLINK) )
57         ,m_aURLLabel        (this, CUI_RES(FT_URL))
58         ,m_aURL             (this, CUI_RES(CMB_URL))
59         ,m_aBrowseFile      (this, CUI_RES(PB_BROWSEFILE))
60         ,m_aNameLabel       (this, CUI_RES(FT_NAME))
61 		,m_aName			(this, CUI_RES(ET_NAME))
62         ,m_aBottomLine      (this, CUI_RES(FL_BOTTOM))
63 		,m_aOK				(this, CUI_RES(BTN_OK))
64         ,m_aCancel          (this, CUI_RES(BTN_CANCEL))
65 		,m_aHelp			(this, CUI_RES(BTN_HELP))
66 		,m_bCreatingNew(_bCreateNew)
67 	{
68 		String sText = String( CUI_RES( m_bCreatingNew ? STR_NEW_LINK : STR_EDIT_LINK ) );
69 		SetText(sText);
70 
71 		FreeResource();
72 
73 		String sTemp = String::CreateFromAscii("*.odb");
74 		m_aURL.SetFilter(sTemp);
75 
76 		m_aName.SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
77 		m_aURL.SetModifyHdl( LINK(this, ODocumentLinkDialog, OnTextModified) );
78 		m_aBrowseFile.SetClickHdl( LINK(this, ODocumentLinkDialog, OnBrowseFile) );
79 		m_aOK.SetClickHdl( LINK(this, ODocumentLinkDialog, OnOk) );
80 
81 		m_aURL.SetDropDownLineCount(10);
82 
83 		validate();
84 
85 		//	m_aURL.SetHelpId( HID_DOCLINKEDIT_URL );
86 		m_aURL.SetDropDownLineCount( 5 );
87 	}
88 
89 	//------------------------------------------------------------------
90 	void ODocumentLinkDialog::set( const String& _rName, const String& _rURL )
91 	{
92 		m_aName.SetText(_rName);
93 		m_aURL.SetText(_rURL);
94 		validate();
95 	}
96 
97 	//------------------------------------------------------------------
98 	void ODocumentLinkDialog::get( String& _rName, String& _rURL ) const
99 	{
100 		_rName = m_aName.GetText();
101 		_rURL = m_aURL.GetText();
102 	}
103 
104 	//------------------------------------------------------------------
105 	void ODocumentLinkDialog::validate( )
106 	{
107 
108 		m_aOK.Enable( (0 != m_aName.GetText().Len()) && ( 0 != m_aURL.GetText().Len() ) );
109 	}
110 
111 	//------------------------------------------------------------------
112     IMPL_LINK( ODocumentLinkDialog, OnOk, void*, EMPTYARG )
113 	{
114 		// get the current URL
115 		::rtl::OUString sURL = m_aURL.GetText();
116 		OFileNotation aTransformer(sURL);
117 		sURL = aTransformer.get(OFileNotation::N_URL);
118 
119 		// check for the existence of the selected file
120 		sal_Bool bFileExists = sal_False;
121 		try
122 		{
123 			::ucbhelper::Content aFile(sURL, Reference< XCommandEnvironment >());
124 			if (aFile.isDocument())
125 				bFileExists = sal_True;
126 		}
127 		catch(Exception&)
128 		{
129 		}
130 
131 		if (!bFileExists)
132 		{
133 			String sMsg = String(CUI_RES(STR_LINKEDDOC_DOESNOTEXIST));
134 			sMsg.SearchAndReplaceAscii("$file$", m_aURL.GetText());
135 			ErrorBox aError(this, WB_OK , sMsg);
136 			aError.Execute();
137 			return 0L;
138 		} // if (!bFileExists)
139         INetURLObject aURL( sURL );
140         if ( aURL.GetProtocol() != INET_PROT_FILE )
141         {
142             String sMsg = String(CUI_RES(STR_LINKEDDOC_NO_SYSTEM_FILE));
143 			sMsg.SearchAndReplaceAscii("$file$", m_aURL.GetText());
144 			ErrorBox aError(this, WB_OK , sMsg);
145 			aError.Execute();
146 			return 0L;
147         }
148 
149 		String sCurrentText = m_aName.GetText();
150 		if ( m_aNameValidator.IsSet() )
151 		{
152 			if ( !m_aNameValidator.Call( &sCurrentText ) )
153 			{
154 				String sMsg = String(CUI_RES(STR_NAME_CONFLICT));
155 				sMsg.SearchAndReplaceAscii("$file$", sCurrentText);
156 				InfoBox aError(this, sMsg);
157 				aError.Execute();
158 
159 				m_aName.SetSelection(Selection(0,sCurrentText.Len()));
160 				m_aName.GrabFocus();
161 				return 0L;
162 			}
163 		}
164 
165 		EndDialog(RET_OK);
166 		return 0L;
167 	}
168 
169 	//------------------------------------------------------------------
170     IMPL_LINK( ODocumentLinkDialog, OnBrowseFile, void*, EMPTYARG )
171 	{
172 		::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK | WB_STDMODAL | WB_OPEN);
173 		static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
174 		const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
175 		if ( pFilter )
176 		{
177 			aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
178 			aFileDlg.SetCurrentFilter(pFilter->GetUIName());
179 		}
180 
181 		String sPath = m_aURL.GetText();
182 		if (sPath.Len())
183 		{
184 			OFileNotation aTransformer( sPath, OFileNotation::N_SYSTEM );
185 			aFileDlg.SetDisplayDirectory( aTransformer.get( OFileNotation::N_URL ) );
186 		}
187 
188 		if (0 != aFileDlg.Execute())
189 			return 0L;
190 
191 		if (0 == m_aName.GetText().Len())
192 		{	// default the name to the base of the chosen URL
193 			INetURLObject aParser;
194 
195 			aParser.SetSmartProtocol(INET_PROT_FILE);
196 			aParser.SetSmartURL(aFileDlg.GetPath());
197 
198 			m_aName.SetText(aParser.getBase(INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET));
199 
200 			m_aName.SetSelection(Selection(0,m_aName.GetText().Len()));
201 			m_aName.GrabFocus();
202 		}
203 		else
204 			m_aURL.GrabFocus();
205 
206 		// get the path in system notation
207 		OFileNotation aTransformer(aFileDlg.GetPath(), OFileNotation::N_URL);
208 		m_aURL.SetText(aTransformer.get(OFileNotation::N_SYSTEM));
209 
210 		validate();
211 		return 0L;
212 	}
213 
214 	//------------------------------------------------------------------
215     IMPL_LINK( ODocumentLinkDialog, OnTextModified, Control*, EMPTYARG )
216 	{
217 		validate( );
218 		return 0L;
219 	}
220 
221 //......................................................................
222 }	// namespace svx
223 //......................................................................
224 
225