xref: /trunk/main/cui/source/dialogs/hldoctp.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 "cuihyperdlg.hxx"
32 #include <unotools/localfilehelper.hxx>
33 #include <sfx2/filedlghelper.hxx>
34 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
35 
36 #include "hldoctp.hxx"
37 #include "hyperdlg.hrc"
38 #include "hlmarkwn_def.hxx" //ADD CHINA001
39 
40 sal_Char __READONLY_DATA sHash[]                = "#";
41 sal_Char __READONLY_DATA sFileScheme[]          = INET_FILE_SCHEME;
42 sal_Char __READONLY_DATA sNewsSRVScheme[] = "news://";
43     // TODO news:// is nonsense
44 sal_Char __READONLY_DATA sHTTPScheme[]    = INET_HTTP_SCHEME;
45 
46 /*************************************************************************
47 |*
48 |* Contructor / Destructor
49 |*
50 |************************************************************************/
51 
52 SvxHyperlinkDocTp::SvxHyperlinkDocTp ( Window *pParent, const SfxItemSet& rItemSet)
53     : SvxHyperlinkTabPageBase ( pParent, CUI_RES( RID_SVXPAGE_HYPERLINK_DOCUMENT ), rItemSet ),
54     maGrpDocument   ( this, CUI_RES (GRP_DOCUMENT) ),
55     maFtPath        ( this, CUI_RES (FT_PATH_DOC) ),
56     maCbbPath       ( this, INET_PROT_FILE ),
57     maBtFileopen    ( this, CUI_RES (BTN_FILEOPEN) ),
58     maGrpTarget     ( this, CUI_RES (GRP_TARGET) ),
59     maFtTarget      ( this, CUI_RES (FT_TARGET_DOC) ),
60     maEdTarget      ( this, CUI_RES (ED_TARGET_DOC) ),
61     maFtURL         ( this, CUI_RES (FT_URL) ),
62     maFtFullURL     ( this, CUI_RES (FT_FULL_URL) ),
63     maBtBrowse      ( this, CUI_RES (BTN_BROWSE) ),
64     mbMarkWndOpen   ( sal_False )
65 {
66     // Set HC bitmaps and disable display of bitmap names.
67     maBtBrowse.SetModeImage( Image( CUI_RES( IMG_BROWSE_HC ) ), BMP_COLOR_HIGHCONTRAST );
68     maBtBrowse.EnableTextDisplay (sal_False);
69     maBtFileopen.SetModeImage( Image( CUI_RES( IMG_FILEOPEN_HC ) ), BMP_COLOR_HIGHCONTRAST );
70     maBtFileopen.EnableTextDisplay (sal_False);
71 
72     InitStdControls();
73     FreeResource();
74 
75     // Init URL-Box (pos&size, Open-Handler)
76     maCbbPath.SetPosSizePixel ( LogicToPixel( Point( COL_2, 15 ), MAP_APPFONT ),
77                                 LogicToPixel( Size ( 176 - COL_DIFF, 60), MAP_APPFONT ) );
78     maCbbPath.Show();
79     String aFileScheme( INET_FILE_SCHEME, RTL_TEXTENCODING_ASCII_US );
80     maCbbPath.SetBaseURL(aFileScheme);
81     maCbbPath.SetHelpId( HID_HYPERDLG_DOC_PATH );
82 
83     SetExchangeSupport ();
84 
85     // overload handlers
86     maBtFileopen.SetClickHdl ( LINK ( this, SvxHyperlinkDocTp, ClickFileopenHdl_Impl ) );
87     maBtBrowse.SetClickHdl   ( LINK ( this, SvxHyperlinkDocTp, ClickTargetHdl_Impl ) );
88     maCbbPath.SetModifyHdl   ( LINK ( this, SvxHyperlinkDocTp, ModifiedPathHdl_Impl ) );
89     maEdTarget.SetModifyHdl  ( LINK ( this, SvxHyperlinkDocTp, ModifiedTargetHdl_Impl ) );
90 
91     maCbbPath.SetLoseFocusHdl( LINK ( this, SvxHyperlinkDocTp, LostFocusPathHdl_Impl ) );
92 
93     maBtBrowse.SetAccessibleRelationMemberOf( &maGrpTarget );
94     maBtBrowse.SetAccessibleRelationLabeledBy( &maFtTarget );
95     maBtFileopen.SetAccessibleRelationMemberOf( &maGrpDocument );
96     maBtFileopen.SetAccessibleRelationLabeledBy( &maFtPath );
97     maTimer.SetTimeoutHdl ( LINK ( this, SvxHyperlinkDocTp, TimeoutHdl_Impl ) );
98 }
99 
100 SvxHyperlinkDocTp::~SvxHyperlinkDocTp ()
101 {
102 }
103 
104 /*************************************************************************
105 |*
106 |* Fill all dialog-controls except controls in groupbox "more..."
107 |*
108 |************************************************************************/
109 
110 void SvxHyperlinkDocTp::FillDlgFields ( String& aStrURL )
111 {
112     INetURLObject aURL ( aStrURL );
113 
114     String aStrMark;
115     xub_StrLen nPos = aStrURL.SearchAscii( sHash );
116     // path
117     maCbbPath.SetText ( aStrURL.Copy( 0, ( nPos == STRING_NOTFOUND ? aStrURL.Len() : nPos ) ) );
118 
119     // set target in document at editfield
120     if ( nPos != STRING_NOTFOUND && nPos<aStrURL.Len()-1 )
121         aStrMark = aStrURL.Copy( nPos+1, aStrURL.Len() );
122      maEdTarget.SetText ( aStrMark );
123 
124     ModifiedPathHdl_Impl ( NULL );
125 }
126 
127 /*************************************************************************
128 |*
129 |* retrieve current url-string
130 |*
131 |************************************************************************/
132 
133 String SvxHyperlinkDocTp::GetCurrentURL ()
134 {
135     // get data from dialog-controls
136     String aStrURL;
137     String aStrPath ( maCbbPath.GetText() );
138     const String aBaseURL ( maCbbPath.GetBaseURL() );
139     String aStrMark( maEdTarget.GetText() );
140 
141     if ( aStrPath != aEmptyStr )
142     {
143         INetURLObject aURL( aStrPath );
144         if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )    // maybe the path is already a valid
145             aStrURL = aStrPath;                             // hyperlink, then we can use this path directly
146         else
147             utl::LocalFileHelper::ConvertSystemPathToURL( aStrPath, aBaseURL, aStrURL );
148 
149         //#105788# always create a URL even if it is not valid
150         if( aStrURL == aEmptyStr )
151             aStrURL = aStrPath;
152     }
153 
154     if( aStrMark != aEmptyStr )
155     {
156         aStrURL.AppendAscii( sHash );
157         aStrURL += aStrMark;
158     }
159 
160     return aStrURL;
161 }
162 
163 /*************************************************************************
164 |*
165 |* retrieve and prepare data from dialog-fields
166 |*
167 |************************************************************************/
168 
169 void SvxHyperlinkDocTp::GetCurentItemData ( String& aStrURL, String& aStrName,
170                                             String& aStrIntName, String& aStrFrame,
171                                             SvxLinkInsertMode& eMode )
172 {
173     // get data from standard-fields
174     aStrURL = GetCurrentURL();
175 
176     if( aStrURL.EqualsIgnoreCaseAscii( sFileScheme ) )
177          aStrURL=aEmptyStr;
178 
179     GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
180 }
181 
182 /*************************************************************************
183 |*
184 |* static method to create Tabpage
185 |*
186 |************************************************************************/
187 
188 IconChoicePage* SvxHyperlinkDocTp::Create( Window* pWindow, const SfxItemSet& rItemSet )
189 {
190     return( new SvxHyperlinkDocTp( pWindow, rItemSet ) );
191 }
192 
193 /*************************************************************************
194 |*
195 |* Set initial focus
196 |*
197 |************************************************************************/
198 
199 void SvxHyperlinkDocTp::SetInitFocus()
200 {
201     maCbbPath.GrabFocus();
202 }
203 
204 /*************************************************************************
205 |*
206 |* Click on imagebutton : fileopen
207 |*
208 |************************************************************************/
209 
210 IMPL_LINK ( SvxHyperlinkDocTp, ClickFileopenHdl_Impl, void *, EMPTYARG )
211 {
212     // Open Fileopen-Dialog
213     ::sfx2::FileDialogHelper aDlg(
214         com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0,
215         GetParent() );
216     String aOldURL( GetCurrentURL() );
217     if( aOldURL.EqualsIgnoreCaseAscii( sFileScheme, 0, sizeof( sFileScheme ) - 1 ) )
218     {
219         aDlg.SetDisplayDirectory( aOldURL );
220     }
221 
222     DisableClose( sal_True );
223     ErrCode nError = aDlg.Execute();
224     DisableClose( sal_False );
225 
226     if ( ERRCODE_NONE == nError )
227     {
228         String aURL( aDlg.GetPath() );
229         String aPath;
230 
231         utl::LocalFileHelper::ConvertURLToSystemPath( aURL, aPath );
232 
233         maCbbPath.SetBaseURL( aURL );
234         maCbbPath.SetText( aPath );
235 
236         if ( aOldURL != GetCurrentURL() )
237             ModifiedPathHdl_Impl (NULL);
238     }
239 
240     return( 0L );
241 }
242 
243 /*************************************************************************
244 |*
245 |* Click on imagebutton : target
246 |*
247 |************************************************************************/
248 
249 IMPL_LINK ( SvxHyperlinkDocTp, ClickTargetHdl_Impl, void *, EMPTYARG )
250 {
251     if ( GetPathType ( maStrURL ) == Type_ExistsFile  ||
252          maStrURL == aEmptyStr                        ||
253          maStrURL.EqualsIgnoreCaseAscii( sFileScheme ) ||
254          maStrURL.SearchAscii( sHash ) == 0 )
255     {
256         mpMarkWnd->SetError( LERR_NOERROR );
257 
258         EnterWait();
259 
260         if ( maStrURL.EqualsIgnoreCaseAscii( sFileScheme ) )
261             mpMarkWnd->RefreshTree ( aEmptyStr );
262         else
263             mpMarkWnd->RefreshTree ( maStrURL );
264 
265         LeaveWait();
266     }
267     else
268         mpMarkWnd->SetError( LERR_DOCNOTOPEN );
269 
270     ShowMarkWnd ();
271 
272     return( 0L );
273 }
274 
275 /*************************************************************************
276 |*
277 |* Contens of combobox "Path" modified
278 |*
279 |************************************************************************/
280 
281 IMPL_LINK ( SvxHyperlinkDocTp, ModifiedPathHdl_Impl, void *, EMPTYARG )
282 {
283     maStrURL = GetCurrentURL();
284 
285     maTimer.SetTimeout( 2500 );
286     maTimer.Start();
287 
288     maFtFullURL.SetText( maStrURL );
289 
290     return( 0L );
291 }
292 
293 /*************************************************************************
294 |*
295 |* If path-field was modify, to browse the new doc after timeout
296 |*
297 |************************************************************************/
298 
299 IMPL_LINK ( SvxHyperlinkDocTp, TimeoutHdl_Impl, Timer *, EMPTYARG )
300 {
301     if ( IsMarkWndVisible() && ( GetPathType( maStrURL )==Type_ExistsFile ||
302                                   maStrURL == aEmptyStr                   ||
303                                   maStrURL.EqualsIgnoreCaseAscii( sFileScheme ) ) )
304     {
305         EnterWait();
306 
307         if ( maStrURL.EqualsIgnoreCaseAscii( sFileScheme ) )
308             mpMarkWnd->RefreshTree ( aEmptyStr );
309         else
310             mpMarkWnd->RefreshTree ( maStrURL );
311 
312         LeaveWait();
313     }
314 
315     return( 0L );
316 }
317 
318 /*************************************************************************
319 |*
320 |* Contens of editfield "Target" modified
321 |*
322 |************************************************************************/
323 
324 IMPL_LINK ( SvxHyperlinkDocTp, ModifiedTargetHdl_Impl, void *, EMPTYARG )
325 {
326     maStrURL = GetCurrentURL();
327 
328     if ( IsMarkWndVisible() )
329         mpMarkWnd->SelectEntry ( maEdTarget.GetText() );
330 
331     maFtFullURL.SetText( maStrURL );
332 
333     return( 0L );
334 }
335 
336 /*************************************************************************
337 |*
338 |* editfield "Target" lost focus
339 |*
340 |************************************************************************/
341 
342 IMPL_LINK ( SvxHyperlinkDocTp, LostFocusPathHdl_Impl, void *, EMPTYARG )
343 {
344     maStrURL = GetCurrentURL();
345 
346     maFtFullURL.SetText( maStrURL );
347 
348     return (0L);
349 }
350 
351 /*************************************************************************
352 |*
353 |* Get String from Bookmark-Wnd
354 |*
355 |************************************************************************/
356 
357 void SvxHyperlinkDocTp::SetMarkStr ( String& aStrMark )
358 {
359     maEdTarget.SetText ( aStrMark );
360 
361     ModifiedTargetHdl_Impl ( NULL );
362 }
363 
364 /*************************************************************************
365 |*
366 |* retrieve kind of pathstr
367 |*
368 |************************************************************************/
369 
370 SvxHyperlinkDocTp::EPathType SvxHyperlinkDocTp::GetPathType ( String& aStrPath )
371 {
372     INetURLObject aURL( aStrPath, INET_PROT_FILE );
373 
374     if( aURL.HasError() )
375         return Type_Invalid;
376     else
377         return Type_ExistsFile;
378 }
379