sync master with lastest vba changes
[ooovba.git] / sc / source / ui / miscdlgs / linkarea.cxx
blob98c0f76400cb953af753051d7bee67eef8a5420f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: linkarea.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #undef SC_DLLIMPLEMENTATION
38 //------------------------------------------------------------------
40 #include <sfx2/app.hxx>
41 #include <sfx2/docfile.hxx>
42 #include <sfx2/docfilt.hxx>
43 #include <sfx2/docinsert.hxx>
44 #include <sfx2/fcontnr.hxx>
45 #include <sfx2/filedlghelper.hxx>
46 #include <svtools/ehdl.hxx>
47 #include <svtools/sfxecode.hxx>
48 #include <vcl/waitobj.hxx>
50 #include "linkarea.hxx"
51 #include "linkarea.hrc"
52 #include "scresid.hxx"
53 #include "sc.hrc"
54 #include "rangeutl.hxx"
55 #include "docsh.hxx"
56 #include "tablink.hxx"
58 //==================================================================
60 ScLinkedAreaDlg::ScLinkedAreaDlg( Window* pParent ) :
61 ModalDialog ( pParent, ScResId( RID_SCDLG_LINKAREA ) ),
63 aFlLocation ( this, ScResId( FL_LOCATION ) ),
64 aCbUrl ( this, ScResId( CB_URL ) ),
65 aBtnBrowse ( this, ScResId( BTN_BROWSE ) ),
66 aTxtHint ( this, ScResId( FT_HINT ) ),
67 aFtRanges ( this, ScResId( FT_RANGES ) ),
68 aLbRanges ( this, ScResId( LB_RANGES ) ),
69 aBtnReload ( this, ScResId( BTN_RELOAD ) ),
70 aNfDelay ( this, ScResId( NF_DELAY ) ),
71 aFtSeconds ( this, ScResId( FT_SECONDS ) ),
72 aBtnOk ( this, ScResId( BTN_OK ) ),
73 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
74 aBtnHelp ( this, ScResId( BTN_HELP ) ),
76 pSourceShell( NULL ),
77 pDocInserter( NULL )
80 FreeResource();
82 aCbUrl.SetHelpId( HID_SCDLG_LINKAREAURL ); // SvtURLBox ctor always sets SID_OPENURL
83 aCbUrl.SetSelectHdl( LINK( this, ScLinkedAreaDlg, FileHdl ) );
84 aBtnBrowse.SetClickHdl( LINK( this, ScLinkedAreaDlg, BrowseHdl ) );
85 aLbRanges.SetSelectHdl( LINK( this, ScLinkedAreaDlg, RangeHdl ) );
86 aBtnReload.SetClickHdl( LINK( this, ScLinkedAreaDlg, ReloadHdl ) );
87 UpdateEnable();
90 ScLinkedAreaDlg::~ScLinkedAreaDlg()
92 // pSourceShell is deleted by aSourceRef
95 short ScLinkedAreaDlg::Execute()
97 // set parent for file dialog or filter options
99 Window* pOldDefParent = Application::GetDefDialogParent();
100 Application::SetDefDialogParent( this );
102 short nRet = ModalDialog::Execute();
104 Application::SetDefDialogParent( pOldDefParent );
106 return nRet;
109 #define FILTERNAME_HTML "HTML (StarCalc)"
110 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
112 IMPL_LINK( ScLinkedAreaDlg, BrowseHdl, PushButton*, EMPTYARG )
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;
121 IMPL_LINK( ScLinkedAreaDlg, FileHdl, ComboBox*, EMPTYARG )
123 String aEntered = aCbUrl.GetURL();
124 if (pSourceShell)
126 SfxMedium* pMed = pSourceShell->GetMedium();
127 if ( pMed->GetName() == aEntered )
129 // already loaded - nothing to do
130 return 0;
134 String aFilter;
135 String aOptions;
136 // get filter name by looking at the file content (bWithContent = TRUE)
137 // Break operation if any error occured inside.
138 if (!ScDocumentLoader::GetFilterName( aEntered, aFilter, aOptions, TRUE, TRUE ))
139 return 0;
141 // #i53241# replace HTML filter with DataQuery filter
142 if( aFilter.EqualsAscii( FILTERNAME_HTML ) )
143 aFilter.AssignAscii( FILTERNAME_QUERY );
145 LoadDocument( aEntered, aFilter, aOptions );
147 UpdateSourceRanges();
148 UpdateEnable();
149 return 0;
152 void ScLinkedAreaDlg::LoadDocument( const String& rFile, const String& rFilter, const String& rOptions )
154 if ( pSourceShell )
156 // unload old document
157 pSourceShell->DoClose();
158 pSourceShell = NULL;
159 aSourceRef.Clear();
162 if ( rFile.Len() )
164 WaitObject aWait( this );
166 String aNewFilter = rFilter;
167 String aNewOptions = rOptions;
169 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, rFile );
171 ScDocumentLoader aLoader( rFile, aNewFilter, aNewOptions, 0, TRUE ); // with interaction
172 pSourceShell = aLoader.GetDocShell();
173 if ( pSourceShell )
175 ULONG nErr = pSourceShell->GetErrorCode();
176 if (nErr)
177 ErrorHandler::HandleError( nErr ); // including warnings
179 aSourceRef = pSourceShell;
180 aLoader.ReleaseDocRef(); // don't call DoClose in DocLoader dtor
185 void ScLinkedAreaDlg::InitFromOldLink( const String& rFile, const String& rFilter,
186 const String& rOptions, const String& rSource,
187 ULONG nRefresh )
189 LoadDocument( rFile, rFilter, rOptions );
190 if (pSourceShell)
192 SfxMedium* pMed = pSourceShell->GetMedium();
193 aCbUrl.SetText( pMed->GetName() );
195 else
196 aCbUrl.SetText( EMPTY_STRING );
198 UpdateSourceRanges();
200 xub_StrLen nRangeCount = rSource.GetTokenCount();
201 for ( xub_StrLen i=0; i<nRangeCount; i++ )
203 String aRange = rSource.GetToken(i);
204 aLbRanges.SelectEntry( aRange );
207 BOOL bDoRefresh = ( nRefresh != 0 );
208 aBtnReload.Check( bDoRefresh );
209 if (bDoRefresh)
210 aNfDelay.SetValue( nRefresh );
212 UpdateEnable();
215 IMPL_LINK( ScLinkedAreaDlg, RangeHdl, MultiListBox*, EMPTYARG )
217 UpdateEnable();
218 return 0;
221 IMPL_LINK( ScLinkedAreaDlg, ReloadHdl, CheckBox*, EMPTYARG )
223 UpdateEnable();
224 return 0;
227 IMPL_LINK( ScLinkedAreaDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
229 if ( _pFileDlg->GetError() != ERRCODE_NONE )
230 return 0;
232 SfxMedium* pMed = pDocInserter->CreateMedium();
233 if ( pMed )
235 WaitObject aWait( this );
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 ) );
241 const SfxFilter* pFilter = pMed->GetFilter();
242 if( pFilter && (pFilter->GetFilterName() == aHTMLFilterName) )
244 const SfxFilter* pNewFilter =
245 ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aWebQFilterName );
246 if( pNewFilter )
247 pMed->SetFilter( pNewFilter );
250 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
251 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
253 if (pSourceShell)
254 pSourceShell->DoClose(); // deleted when assigning aSourceRef
256 pMed->UseInteractionHandler( TRUE ); // to enable the filter options dialog
258 pSourceShell = new ScDocShell;
259 aSourceRef = pSourceShell;
260 pSourceShell->DoLoad( pMed );
262 ULONG nErr = pSourceShell->GetErrorCode();
263 if (nErr)
264 ErrorHandler::HandleError( nErr ); // including warnings
266 if ( !pSourceShell->GetError() ) // only errors
268 //aCbUrl.SetText( pSourceShell->GetTitle( SFX_TITLE_FULLNAME ) );
269 aCbUrl.SetText( pMed->GetName() );
271 else
273 pSourceShell->DoClose();
274 pSourceShell = NULL;
275 aSourceRef.Clear();
277 aCbUrl.SetText( EMPTY_STRING );
281 UpdateSourceRanges();
282 UpdateEnable();
283 return 0;
286 #undef FILTERNAME_HTML
287 #undef FILTERNAME_QUERY
289 void ScLinkedAreaDlg::UpdateSourceRanges()
291 aLbRanges.SetUpdateMode( FALSE );
293 aLbRanges.Clear();
294 if ( pSourceShell )
296 ScAreaNameIterator aIter( pSourceShell->GetDocument() );
297 ScRange aDummy;
298 String aName;
299 while ( aIter.Next( aName, aDummy ) )
300 aLbRanges.InsertEntry( aName );
303 aLbRanges.SetUpdateMode( TRUE );
305 if ( aLbRanges.GetEntryCount() == 1 )
306 aLbRanges.SelectEntryPos(0);
309 void ScLinkedAreaDlg::UpdateEnable()
311 BOOL bEnable = ( pSourceShell && aLbRanges.GetSelectEntryCount() );
312 aBtnOk.Enable( bEnable );
314 BOOL bReload = aBtnReload.IsChecked();
315 aNfDelay.Enable( bReload );
316 aFtSeconds.Enable( bReload );
319 String ScLinkedAreaDlg::GetURL()
321 if (pSourceShell)
323 SfxMedium* pMed = pSourceShell->GetMedium();
324 return pMed->GetName();
326 return EMPTY_STRING;
329 String ScLinkedAreaDlg::GetFilter()
331 if (pSourceShell)
333 SfxMedium* pMed = pSourceShell->GetMedium();
334 return pMed->GetFilter()->GetFilterName();
336 return EMPTY_STRING;
339 String ScLinkedAreaDlg::GetOptions()
341 if (pSourceShell)
343 SfxMedium* pMed = pSourceShell->GetMedium();
344 return ScDocumentLoader::GetOptions( *pMed );
346 return EMPTY_STRING;
349 String ScLinkedAreaDlg::GetSource()
351 String aSource;
352 USHORT nCount = aLbRanges.GetSelectEntryCount();
353 for (USHORT i=0; i<nCount; i++)
355 if (i > 0)
356 aSource.Append( (sal_Unicode) ';' );
357 aSource.Append( aLbRanges.GetSelectEntry( i ) );
359 return aSource;
362 ULONG ScLinkedAreaDlg::GetRefresh()
364 if ( aBtnReload.IsChecked() )
365 return sal::static_int_cast<ULONG>( aNfDelay.GetValue() );
366 else
367 return 0; // disabled