xref: /aoo42x/main/sc/source/ui/miscdlgs/linkarea.cxx (revision cdf0e10c)
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_sc.hxx"
30 
31 #undef SC_DLLIMPLEMENTATION
32 
33 
34 
35 //------------------------------------------------------------------
36 
37 #include <sfx2/app.hxx>
38 #include <sfx2/docfile.hxx>
39 #include <sfx2/docfilt.hxx>
40 #include <sfx2/docinsert.hxx>
41 #include <sfx2/fcontnr.hxx>
42 #include <sfx2/filedlghelper.hxx>
43 #include <svtools/ehdl.hxx>
44 #include <svtools/sfxecode.hxx>
45 #include <vcl/waitobj.hxx>
46 
47 #include "linkarea.hxx"
48 #include "linkarea.hrc"
49 #include "scresid.hxx"
50 #include "sc.hrc"
51 #include "rangeutl.hxx"
52 #include "docsh.hxx"
53 #include "tablink.hxx"
54 
55 //==================================================================
56 
57 ScLinkedAreaDlg::ScLinkedAreaDlg( Window* pParent ) :
58 	ModalDialog ( pParent, ScResId( RID_SCDLG_LINKAREA ) ),
59 	//
60 	aFlLocation	( this, ScResId( FL_LOCATION ) ),
61 	aCbUrl		( this, ScResId( CB_URL ) ),
62 	aBtnBrowse	( this, ScResId( BTN_BROWSE ) ),
63 	aTxtHint	( this, ScResId( FT_HINT ) ),
64 	aFtRanges	( this, ScResId( FT_RANGES ) ),
65 	aLbRanges	( this, ScResId( LB_RANGES ) ),
66 	aBtnReload	( this, ScResId( BTN_RELOAD ) ),
67 	aNfDelay	( this, ScResId( NF_DELAY ) ),
68 	aFtSeconds	( this, ScResId( FT_SECONDS ) ),
69 	aBtnOk		( this, ScResId( BTN_OK ) ),
70 	aBtnCancel	( this, ScResId( BTN_CANCEL ) ),
71 	aBtnHelp	( this, ScResId( BTN_HELP ) ),
72 	//
73     pSourceShell( NULL ),
74     pDocInserter( NULL )
75 
76 {
77 	FreeResource();
78 
79 	aCbUrl.SetHelpId( HID_SCDLG_LINKAREAURL );	// SvtURLBox ctor always sets SID_OPENURL
80 	aCbUrl.SetSelectHdl( LINK( this, ScLinkedAreaDlg, FileHdl ) );
81 	aBtnBrowse.SetClickHdl( LINK( this, ScLinkedAreaDlg, BrowseHdl ) );
82 	aLbRanges.SetSelectHdl( LINK( this, ScLinkedAreaDlg, RangeHdl ) );
83 	aBtnReload.SetClickHdl( LINK( this, ScLinkedAreaDlg, ReloadHdl ) );
84 	UpdateEnable();
85 
86 	aNfDelay.SetAccessibleName(aBtnReload.GetText());
87 	aNfDelay.SetAccessibleRelationLabeledBy(&aBtnReload);
88 }
89 
90 ScLinkedAreaDlg::~ScLinkedAreaDlg()
91 {
92 	// pSourceShell is deleted by aSourceRef
93 }
94 
95 short ScLinkedAreaDlg::Execute()
96 {
97 	// set parent for file dialog or filter options
98 
99 	Window*	pOldDefParent = Application::GetDefDialogParent();
100 	Application::SetDefDialogParent( this );
101 
102 	short nRet = ModalDialog::Execute();
103 
104 	Application::SetDefDialogParent( pOldDefParent );
105 
106 	return nRet;
107 }
108 
109 #define FILTERNAME_HTML  "HTML (StarCalc)"
110 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
111 
112 IMPL_LINK( ScLinkedAreaDlg, BrowseHdl, PushButton*, EMPTYARG )
113 {
114     if ( !pDocInserter )
115         pDocInserter = new sfx2::DocumentInserter(
116             0, String::CreateFromAscii( ScDocShell::Factory().GetShortName() ) );
117     pDocInserter->StartExecuteModal( LINK( this, ScLinkedAreaDlg, DialogClosedHdl ) );
118     return 0;
119 }
120 
121 IMPL_LINK( ScLinkedAreaDlg, FileHdl, ComboBox*, EMPTYARG )
122 {
123 	String aEntered = aCbUrl.GetURL();
124 	if (pSourceShell)
125 	{
126 		SfxMedium* pMed = pSourceShell->GetMedium();
127 		if ( pMed->GetName() == aEntered )
128 		{
129 			//	already loaded - nothing to do
130 			return 0;
131 		}
132 	}
133 
134 	String aFilter;
135 	String aOptions;
136 	//	get filter name by looking at the file content (bWithContent = sal_True)
137     // Break operation if any error occured inside.
138     if (!ScDocumentLoader::GetFilterName( aEntered, aFilter, aOptions, sal_True, sal_True ))
139         return 0;
140 
141     // #i53241# replace HTML filter with DataQuery filter
142     if( aFilter.EqualsAscii( FILTERNAME_HTML ) )
143         aFilter.AssignAscii( FILTERNAME_QUERY );
144 
145 	LoadDocument( aEntered, aFilter, aOptions );
146 
147 	UpdateSourceRanges();
148 	UpdateEnable();
149 	return 0;
150 }
151 
152 void ScLinkedAreaDlg::LoadDocument( const String& rFile, const String& rFilter, const String& rOptions )
153 {
154 	if ( pSourceShell )
155 	{
156 		//	unload old document
157 		pSourceShell->DoClose();
158 		pSourceShell = NULL;
159 		aSourceRef.Clear();
160 	}
161 
162 	if ( rFile.Len() )
163 	{
164 		WaitObject aWait( this );
165 
166 		String aNewFilter = rFilter;
167 		String aNewOptions = rOptions;
168 
169 		SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, rFile );
170 
171 		ScDocumentLoader aLoader( rFile, aNewFilter, aNewOptions, 0, sal_True );	// with interaction
172 		pSourceShell = aLoader.GetDocShell();
173 		if ( pSourceShell )
174 		{
175 			sal_uLong nErr = pSourceShell->GetErrorCode();
176 			if (nErr)
177 				ErrorHandler::HandleError( nErr );		// including warnings
178 
179 			aSourceRef = pSourceShell;
180 			aLoader.ReleaseDocRef();	// don't call DoClose in DocLoader dtor
181 		}
182 	}
183 }
184 
185 void ScLinkedAreaDlg::InitFromOldLink( const String& rFile, const String& rFilter,
186 										const String& rOptions, const String& rSource,
187 										sal_uLong nRefresh )
188 {
189 	LoadDocument( rFile, rFilter, rOptions );
190 	if (pSourceShell)
191 	{
192 		SfxMedium* pMed = pSourceShell->GetMedium();
193 		aCbUrl.SetText( pMed->GetName() );
194 	}
195 	else
196 		aCbUrl.SetText( EMPTY_STRING );
197 
198 	UpdateSourceRanges();
199 
200 	xub_StrLen nRangeCount = rSource.GetTokenCount();
201 	for ( xub_StrLen i=0; i<nRangeCount; i++ )
202 	{
203 		String aRange = rSource.GetToken(i);
204 		aLbRanges.SelectEntry( aRange );
205 	}
206 
207 	sal_Bool bDoRefresh = ( nRefresh != 0 );
208 	aBtnReload.Check( bDoRefresh );
209 	if (bDoRefresh)
210 		aNfDelay.SetValue( nRefresh );
211 
212 	UpdateEnable();
213 }
214 
215 IMPL_LINK( ScLinkedAreaDlg, RangeHdl, MultiListBox*, EMPTYARG )
216 {
217 	UpdateEnable();
218 	return 0;
219 }
220 
221 IMPL_LINK( ScLinkedAreaDlg, ReloadHdl, CheckBox*, EMPTYARG )
222 {
223     UpdateEnable();
224     return 0;
225 }
226 
227 IMPL_LINK( ScLinkedAreaDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
228 {
229     if ( _pFileDlg->GetError() != ERRCODE_NONE )
230         return 0;
231 
232     SfxMedium* pMed = pDocInserter->CreateMedium();
233     if ( pMed )
234     {
235         WaitObject aWait( this );
236 
237         // #92296# replace HTML filter with DataQuery filter
238         const String aHTMLFilterName( RTL_CONSTASCII_USTRINGPARAM( FILTERNAME_HTML ) );
239         const String aWebQFilterName( RTL_CONSTASCII_USTRINGPARAM( FILTERNAME_QUERY ) );
240 
241         const SfxFilter* pFilter = pMed->GetFilter();
242         if( pFilter && (pFilter->GetFilterName() == aHTMLFilterName) )
243         {
244             const SfxFilter* pNewFilter =
245                 ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aWebQFilterName );
246             if( pNewFilter )
247                 pMed->SetFilter( pNewFilter );
248         }
249 
250         //  ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
251         SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
252 
253         if (pSourceShell)
254             pSourceShell->DoClose();        // deleted when assigning aSourceRef
255 
256         pMed->UseInteractionHandler( sal_True );    // to enable the filter options dialog
257 
258         pSourceShell = new ScDocShell;
259         aSourceRef = pSourceShell;
260         pSourceShell->DoLoad( pMed );
261 
262         sal_uLong nErr = pSourceShell->GetErrorCode();
263         if (nErr)
264             ErrorHandler::HandleError( nErr );              // including warnings
265 
266         if ( !pSourceShell->GetError() )                    // only errors
267         {
268             //aCbUrl.SetText( pSourceShell->GetTitle( SFX_TITLE_FULLNAME ) );
269             aCbUrl.SetText( pMed->GetName() );
270         }
271         else
272         {
273             pSourceShell->DoClose();
274             pSourceShell = NULL;
275             aSourceRef.Clear();
276 
277             aCbUrl.SetText( EMPTY_STRING );
278         }
279     }
280 
281     UpdateSourceRanges();
282     UpdateEnable();
283     return 0;
284 }
285 
286 #undef FILTERNAME_HTML
287 #undef FILTERNAME_QUERY
288 
289 void ScLinkedAreaDlg::UpdateSourceRanges()
290 {
291 	aLbRanges.SetUpdateMode( sal_False );
292 
293 	aLbRanges.Clear();
294 	if ( pSourceShell )
295 	{
296 		ScAreaNameIterator aIter( pSourceShell->GetDocument() );
297 		ScRange aDummy;
298 		String aName;
299 		while ( aIter.Next( aName, aDummy ) )
300 			aLbRanges.InsertEntry( aName );
301 	}
302 
303 	aLbRanges.SetUpdateMode( sal_True );
304 
305 	if ( aLbRanges.GetEntryCount() == 1 )
306 		aLbRanges.SelectEntryPos(0);
307 }
308 
309 void ScLinkedAreaDlg::UpdateEnable()
310 {
311 	sal_Bool bEnable = ( pSourceShell && aLbRanges.GetSelectEntryCount() );
312 	aBtnOk.Enable( bEnable );
313 
314 	sal_Bool bReload = aBtnReload.IsChecked();
315 	aNfDelay.Enable( bReload );
316 	aFtSeconds.Enable( bReload );
317 }
318 
319 String ScLinkedAreaDlg::GetURL()
320 {
321 	if (pSourceShell)
322 	{
323 		SfxMedium* pMed = pSourceShell->GetMedium();
324 		return pMed->GetName();
325 	}
326 	return EMPTY_STRING;
327 }
328 
329 String ScLinkedAreaDlg::GetFilter()
330 {
331 	if (pSourceShell)
332 	{
333 		SfxMedium* pMed = pSourceShell->GetMedium();
334 		return pMed->GetFilter()->GetFilterName();
335 	}
336 	return EMPTY_STRING;
337 }
338 
339 String ScLinkedAreaDlg::GetOptions()
340 {
341 	if (pSourceShell)
342 	{
343 		SfxMedium* pMed = pSourceShell->GetMedium();
344 		return ScDocumentLoader::GetOptions( *pMed );
345 	}
346 	return EMPTY_STRING;
347 }
348 
349 String ScLinkedAreaDlg::GetSource()
350 {
351 	String aSource;
352 	sal_uInt16 nCount = aLbRanges.GetSelectEntryCount();
353 	for (sal_uInt16 i=0; i<nCount; i++)
354 	{
355 		if (i > 0)
356 			aSource.Append( (sal_Unicode) ';' );
357 		aSource.Append( aLbRanges.GetSelectEntry( i ) );
358 	}
359 	return aSource;
360 }
361 
362 sal_uLong ScLinkedAreaDlg::GetRefresh()
363 {
364 	if ( aBtnReload.IsChecked() )
365         return sal::static_int_cast<sal_uLong>( aNfDelay.GetValue() );
366 	else
367 		return 0;	// disabled
368 }
369 
370