1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: linkarea.cxx,v $
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"
54 #include "rangeutl.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
) ),
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
) );
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
);
109 #define FILTERNAME_HTML "HTML (StarCalc)"
110 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
112 IMPL_LINK( ScLinkedAreaDlg
, BrowseHdl
, PushButton
*, EMPTYARG
)
115 pDocInserter
= new sfx2::DocumentInserter(
116 0, String::CreateFromAscii( ScDocShell::Factory().GetShortName() ) );
117 pDocInserter
->StartExecuteModal( LINK( this, ScLinkedAreaDlg
, DialogClosedHdl
) );
121 IMPL_LINK( ScLinkedAreaDlg
, FileHdl
, ComboBox
*, EMPTYARG
)
123 String aEntered
= aCbUrl
.GetURL();
126 SfxMedium
* pMed
= pSourceShell
->GetMedium();
127 if ( pMed
->GetName() == aEntered
)
129 // already loaded - nothing to do
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
))
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();
152 void ScLinkedAreaDlg::LoadDocument( const String
& rFile
, const String
& rFilter
, const String
& rOptions
)
156 // unload old document
157 pSourceShell
->DoClose();
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();
175 ULONG nErr
= pSourceShell
->GetErrorCode();
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
,
189 LoadDocument( rFile
, rFilter
, rOptions
);
192 SfxMedium
* pMed
= pSourceShell
->GetMedium();
193 aCbUrl
.SetText( pMed
->GetName() );
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
);
210 aNfDelay
.SetValue( nRefresh
);
215 IMPL_LINK( ScLinkedAreaDlg
, RangeHdl
, MultiListBox
*, EMPTYARG
)
221 IMPL_LINK( ScLinkedAreaDlg
, ReloadHdl
, CheckBox
*, EMPTYARG
)
227 IMPL_LINK( ScLinkedAreaDlg
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
229 if ( _pFileDlg
->GetError() != ERRCODE_NONE
)
232 SfxMedium
* pMed
= pDocInserter
->CreateMedium();
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
);
247 pMed
->SetFilter( pNewFilter
);
250 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
251 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
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();
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() );
273 pSourceShell
->DoClose();
277 aCbUrl
.SetText( EMPTY_STRING
);
281 UpdateSourceRanges();
286 #undef FILTERNAME_HTML
287 #undef FILTERNAME_QUERY
289 void ScLinkedAreaDlg::UpdateSourceRanges()
291 aLbRanges
.SetUpdateMode( FALSE
);
296 ScAreaNameIterator
aIter( pSourceShell
->GetDocument() );
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()
323 SfxMedium
* pMed
= pSourceShell
->GetMedium();
324 return pMed
->GetName();
329 String
ScLinkedAreaDlg::GetFilter()
333 SfxMedium
* pMed
= pSourceShell
->GetMedium();
334 return pMed
->GetFilter()->GetFilterName();
339 String
ScLinkedAreaDlg::GetOptions()
343 SfxMedium
* pMed
= pSourceShell
->GetMedium();
344 return ScDocumentLoader::GetOptions( *pMed
);
349 String
ScLinkedAreaDlg::GetSource()
352 USHORT nCount
= aLbRanges
.GetSelectEntryCount();
353 for (USHORT i
=0; i
<nCount
; i
++)
356 aSource
.Append( (sal_Unicode
) ';' );
357 aSource
.Append( aLbRanges
.GetSelectEntry( i
) );
362 ULONG
ScLinkedAreaDlg::GetRefresh()
364 if ( aBtnReload
.IsChecked() )
365 return sal::static_int_cast
<ULONG
>( aNfDelay
.GetValue() );
367 return 0; // disabled