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