Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / sc / source / ui / miscdlgs / linkarea.cxx
blob949691b3ab3d3378735f5939669b8d3ba8b3b40e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #undef SC_DLLIMPLEMENTATION
25 #include <comphelper/string.hxx>
26 #include <sfx2/app.hxx>
27 #include <sfx2/docfile.hxx>
28 #include <sfx2/docfilt.hxx>
29 #include <sfx2/docinsert.hxx>
30 #include <sfx2/fcontnr.hxx>
31 #include <sfx2/filedlghelper.hxx>
32 #include <svtools/ehdl.hxx>
33 #include <svtools/sfxecode.hxx>
34 #include <vcl/waitobj.hxx>
36 #include "linkarea.hxx"
37 #include "linkarea.hrc"
38 #include "scresid.hxx"
39 #include "sc.hrc"
40 #include "rangeutl.hxx"
41 #include "docsh.hxx"
42 #include "tablink.hxx"
44 //==================================================================
46 ScLinkedAreaDlg::ScLinkedAreaDlg( Window* pParent ) :
47 ModalDialog ( pParent, ScResId( RID_SCDLG_LINKAREA ) ),
49 aFlLocation ( this, ScResId( FL_LOCATION ) ),
50 aCbUrl ( this, ScResId( CB_URL ) ),
51 aBtnBrowse ( this, ScResId( BTN_BROWSE ) ),
52 aTxtHint ( this, ScResId( FT_HINT ) ),
53 aFtRanges ( this, ScResId( FT_RANGES ) ),
54 aLbRanges ( this, ScResId( LB_RANGES ) ),
55 aBtnReload ( this, ScResId( BTN_RELOAD ) ),
56 aNfDelay ( this, ScResId( NF_DELAY ) ),
57 aFtSeconds ( this, ScResId( FT_SECONDS ) ),
58 aBtnOk ( this, ScResId( BTN_OK ) ),
59 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
60 aBtnHelp ( this, ScResId( BTN_HELP ) ),
62 pSourceShell( NULL ),
63 pDocInserter( NULL )
66 FreeResource();
68 aCbUrl.SetHelpId( HID_SCDLG_LINKAREAURL ); // SvtURLBox ctor always sets SID_OPENURL
69 aCbUrl.SetSelectHdl( LINK( this, ScLinkedAreaDlg, FileHdl ) );
70 aBtnBrowse.SetClickHdl( LINK( this, ScLinkedAreaDlg, BrowseHdl ) );
71 aLbRanges.SetSelectHdl( LINK( this, ScLinkedAreaDlg, RangeHdl ) );
72 aBtnReload.SetClickHdl( LINK( this, ScLinkedAreaDlg, ReloadHdl ) );
73 UpdateEnable();
75 aNfDelay.SetAccessibleName(aBtnReload.GetText());
76 aNfDelay.SetAccessibleRelationLabeledBy(&aBtnReload);
79 ScLinkedAreaDlg::~ScLinkedAreaDlg()
81 // pSourceShell is deleted by aSourceRef
84 short ScLinkedAreaDlg::Execute()
86 // set parent for file dialog or filter options
88 Window* pOldDefParent = Application::GetDefDialogParent();
89 Application::SetDefDialogParent( this );
91 short nRet = ModalDialog::Execute();
93 Application::SetDefDialogParent( pOldDefParent );
95 return nRet;
98 #define FILTERNAME_HTML "HTML (StarCalc)"
99 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
101 IMPL_LINK_NOARG(ScLinkedAreaDlg, BrowseHdl)
103 if ( !pDocInserter )
104 pDocInserter = new sfx2::DocumentInserter(
105 rtl::OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
106 pDocInserter->StartExecuteModal( LINK( this, ScLinkedAreaDlg, DialogClosedHdl ) );
107 return 0;
110 IMPL_LINK_NOARG(ScLinkedAreaDlg, FileHdl)
112 rtl::OUString aEntered = aCbUrl.GetURL();
113 if (pSourceShell)
115 SfxMedium* pMed = pSourceShell->GetMedium();
116 if ( aEntered.equals(pMed->GetName()) )
118 // already loaded - nothing to do
119 return 0;
123 rtl::OUString aFilter;
124 rtl::OUString aOptions;
125 // get filter name by looking at the file content (bWithContent = true)
126 // Break operation if any error occurred inside.
127 if (!ScDocumentLoader::GetFilterName( aEntered, aFilter, aOptions, true, true ))
128 return 0;
130 // #i53241# replace HTML filter with DataQuery filter
131 if (aFilter.equalsAscii(FILTERNAME_HTML))
132 aFilter = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(FILTERNAME_QUERY));
134 LoadDocument( aEntered, aFilter, aOptions );
136 UpdateSourceRanges();
137 UpdateEnable();
138 return 0;
141 void ScLinkedAreaDlg::LoadDocument( const rtl::OUString& rFile, const String& rFilter, const String& rOptions )
143 if ( pSourceShell )
145 // unload old document
146 pSourceShell->DoClose();
147 pSourceShell = NULL;
148 aSourceRef.Clear();
151 if ( !rFile.isEmpty() )
153 WaitObject aWait( this );
155 rtl::OUString aNewFilter = rFilter;
156 rtl::OUString aNewOptions = rOptions;
158 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, rFile );
160 ScDocumentLoader aLoader( rFile, aNewFilter, aNewOptions, 0, true ); // with interaction
161 pSourceShell = aLoader.GetDocShell();
162 if ( pSourceShell )
164 sal_uLong nErr = pSourceShell->GetErrorCode();
165 if (nErr)
166 ErrorHandler::HandleError( nErr ); // including warnings
168 aSourceRef = pSourceShell;
169 aLoader.ReleaseDocRef(); // don't call DoClose in DocLoader dtor
174 void ScLinkedAreaDlg::InitFromOldLink( const String& rFile, const String& rFilter,
175 const String& rOptions, const String& rSource,
176 sal_uLong nRefresh )
178 LoadDocument( rFile, rFilter, rOptions );
179 if (pSourceShell)
181 SfxMedium* pMed = pSourceShell->GetMedium();
182 aCbUrl.SetText( pMed->GetName() );
184 else
185 aCbUrl.SetText( EMPTY_STRING );
187 UpdateSourceRanges();
189 xub_StrLen nRangeCount = comphelper::string::getTokenCount(rSource, ';');
190 for ( xub_StrLen i=0; i<nRangeCount; i++ )
192 String aRange = rSource.GetToken(i);
193 aLbRanges.SelectEntry( aRange );
196 bool bDoRefresh = (nRefresh != 0);
197 aBtnReload.Check( bDoRefresh );
198 if (bDoRefresh)
199 aNfDelay.SetValue( nRefresh );
201 UpdateEnable();
204 IMPL_LINK_NOARG(ScLinkedAreaDlg, RangeHdl)
206 UpdateEnable();
207 return 0;
210 IMPL_LINK_NOARG(ScLinkedAreaDlg, ReloadHdl)
212 UpdateEnable();
213 return 0;
216 IMPL_LINK( ScLinkedAreaDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
218 if ( _pFileDlg->GetError() != ERRCODE_NONE )
219 return 0;
221 SfxMedium* pMed = pDocInserter->CreateMedium();
222 if ( pMed )
224 WaitObject aWait( this );
226 // replace HTML filter with DataQuery filter
227 const rtl::OUString aHTMLFilterName( RTL_CONSTASCII_USTRINGPARAM( FILTERNAME_HTML ) );
228 const rtl::OUString aWebQFilterName( RTL_CONSTASCII_USTRINGPARAM( FILTERNAME_QUERY ) );
230 const SfxFilter* pFilter = pMed->GetFilter();
231 if (pFilter && aHTMLFilterName.equals(pFilter->GetFilterName()))
233 const SfxFilter* pNewFilter =
234 ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aWebQFilterName );
235 if( pNewFilter )
236 pMed->SetFilter( pNewFilter );
239 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
240 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
242 if (pSourceShell)
243 pSourceShell->DoClose(); // deleted when assigning aSourceRef
245 pMed->UseInteractionHandler( sal_True ); // to enable the filter options dialog
247 pSourceShell = new ScDocShell;
248 aSourceRef = pSourceShell;
249 pSourceShell->DoLoad( pMed );
251 sal_uLong nErr = pSourceShell->GetErrorCode();
252 if (nErr)
253 ErrorHandler::HandleError( nErr ); // including warnings
255 if ( !pSourceShell->GetError() ) // only errors
257 aCbUrl.SetText( pMed->GetName() );
259 else
261 pSourceShell->DoClose();
262 pSourceShell = NULL;
263 aSourceRef.Clear();
265 aCbUrl.SetText( EMPTY_STRING );
269 UpdateSourceRanges();
270 UpdateEnable();
271 return 0;
274 #undef FILTERNAME_HTML
275 #undef FILTERNAME_QUERY
277 void ScLinkedAreaDlg::UpdateSourceRanges()
279 aLbRanges.SetUpdateMode(false);
281 aLbRanges.Clear();
282 if ( pSourceShell )
284 ScAreaNameIterator aIter( pSourceShell->GetDocument() );
285 ScRange aDummy;
286 rtl::OUString aName;
287 while ( aIter.Next( aName, aDummy ) )
288 aLbRanges.InsertEntry( aName );
291 aLbRanges.SetUpdateMode(true);
293 if ( aLbRanges.GetEntryCount() == 1 )
294 aLbRanges.SelectEntryPos(0);
297 void ScLinkedAreaDlg::UpdateEnable()
299 bool bEnable = ( pSourceShell && aLbRanges.GetSelectEntryCount() );
300 aBtnOk.Enable( bEnable );
302 bool bReload = aBtnReload.IsChecked();
303 aNfDelay.Enable( bReload );
304 aFtSeconds.Enable( bReload );
307 rtl::OUString ScLinkedAreaDlg::GetURL()
309 if (pSourceShell)
311 SfxMedium* pMed = pSourceShell->GetMedium();
312 return pMed->GetName();
314 return EMPTY_STRING;
317 rtl::OUString ScLinkedAreaDlg::GetFilter()
319 if (pSourceShell)
321 SfxMedium* pMed = pSourceShell->GetMedium();
322 return pMed->GetFilter()->GetFilterName();
324 return rtl::OUString();
327 rtl::OUString ScLinkedAreaDlg::GetOptions()
329 if (pSourceShell)
331 SfxMedium* pMed = pSourceShell->GetMedium();
332 return ScDocumentLoader::GetOptions( *pMed );
334 return rtl::OUString();
337 rtl::OUString ScLinkedAreaDlg::GetSource()
339 rtl::OUStringBuffer aBuf;
340 sal_uInt16 nCount = aLbRanges.GetSelectEntryCount();
341 for (sal_uInt16 i=0; i<nCount; i++)
343 if (i > 0)
344 aBuf.append(sal_Unicode(';'));
345 aBuf.append(aLbRanges.GetSelectEntry(i));
347 return aBuf.makeStringAndClear();
350 sal_uLong ScLinkedAreaDlg::GetRefresh()
352 if ( aBtnReload.IsChecked() )
353 return sal::static_int_cast<sal_uLong>( aNfDelay.GetValue() );
354 else
355 return 0; // disabled
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */