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