Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / miscdlgs / linkarea.cxx
blob143799d52ce3c409ecf0f71e81c75f6e1402ee5b
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 .
20 #undef SC_DLLIMPLEMENTATION
22 #include <comphelper/string.hxx>
23 #include <sfx2/app.hxx>
24 #include <sfx2/docfile.hxx>
25 #include <sfx2/docfilt.hxx>
26 #include <sfx2/docinsert.hxx>
27 #include <sfx2/fcontnr.hxx>
28 #include <sfx2/filedlghelper.hxx>
29 #include <svtools/ehdl.hxx>
30 #include <svtools/sfxecode.hxx>
31 #include <vcl/waitobj.hxx>
33 #include "linkarea.hxx"
34 #include "scresid.hxx"
35 #include "sc.hrc"
36 #include "rangeutl.hxx"
37 #include "docsh.hxx"
38 #include "tablink.hxx"
40 ScLinkedAreaDlg::ScLinkedAreaDlg(vcl::Window* pParent)
41 : ModalDialog(pParent, "ExternalDataDialog", "modules/scalc/ui/externaldata.ui")
42 , pSourceShell(nullptr)
43 , pDocInserter(nullptr)
46 get(m_pCbUrl, "url");
47 get(m_pLbRanges, "ranges");
48 m_pLbRanges->EnableMultiSelection(true);
49 m_pLbRanges->SetDropDownLineCount(8);
50 get(m_pBtnBrowse, "browse");
51 get(m_pBtnReload, "reload");
52 get(m_pNfDelay, "delay");
53 get(m_pFtSeconds, "secondsft");
54 get(m_pBtnOk, "ok");
56 m_pCbUrl->SetSelectHdl( LINK( this, ScLinkedAreaDlg, FileHdl ) );
57 m_pBtnBrowse->SetClickHdl( LINK( this, ScLinkedAreaDlg, BrowseHdl ) );
58 m_pLbRanges->SetSelectHdl( LINK( this, ScLinkedAreaDlg, RangeHdl ) );
59 m_pBtnReload->SetClickHdl( LINK( this, ScLinkedAreaDlg, ReloadHdl ) );
60 UpdateEnable();
63 ScLinkedAreaDlg::~ScLinkedAreaDlg()
65 disposeOnce();
68 void ScLinkedAreaDlg::dispose()
70 // pSourceShell is deleted by aSourceRef
71 m_pCbUrl.clear();
72 m_pBtnBrowse.clear();
73 m_pLbRanges.clear();
74 m_pBtnReload.clear();
75 m_pNfDelay.clear();
76 m_pFtSeconds.clear();
77 m_pBtnOk.clear();
78 ModalDialog::dispose();
81 short ScLinkedAreaDlg::Execute()
83 return ModalDialog::Execute();
86 #define FILTERNAME_HTML "HTML (StarCalc)"
87 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
89 IMPL_LINK_NOARG_TYPED(ScLinkedAreaDlg, BrowseHdl, Button*, void)
91 if ( !pDocInserter )
92 pDocInserter = new sfx2::DocumentInserter(
93 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
94 pDocInserter->StartExecuteModal( LINK( this, ScLinkedAreaDlg, DialogClosedHdl ) );
97 IMPL_LINK_NOARG_TYPED(ScLinkedAreaDlg, FileHdl, ComboBox&, void)
99 OUString aEntered = m_pCbUrl->GetURL();
100 if (pSourceShell)
102 SfxMedium* pMed = pSourceShell->GetMedium();
103 if ( aEntered.equals(pMed->GetName()) )
105 // already loaded - nothing to do
106 return;
110 OUString aFilter;
111 OUString aOptions;
112 // get filter name by looking at the file content (bWithContent = true)
113 // Break operation if any error occurred inside.
114 if (!ScDocumentLoader::GetFilterName( aEntered, aFilter, aOptions, true, true ))
115 return;
117 // #i53241# replace HTML filter with DataQuery filter
118 if (aFilter == FILTERNAME_HTML)
119 aFilter = FILTERNAME_QUERY;
121 LoadDocument( aEntered, aFilter, aOptions );
123 UpdateSourceRanges();
124 UpdateEnable();
127 void ScLinkedAreaDlg::LoadDocument( const OUString& rFile, const OUString& rFilter, const OUString& rOptions )
129 if ( pSourceShell )
131 // unload old document
132 pSourceShell->DoClose();
133 pSourceShell = nullptr;
134 aSourceRef.Clear();
137 if ( !rFile.isEmpty() )
139 WaitObject aWait( this );
141 OUString aNewFilter = rFilter;
142 OUString aNewOptions = rOptions;
144 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, rFile );
146 ScDocumentLoader aLoader( rFile, aNewFilter, aNewOptions, 0, true ); // with interaction
147 pSourceShell = aLoader.GetDocShell();
148 if ( pSourceShell )
150 sal_uLong nErr = pSourceShell->GetErrorCode();
151 if (nErr)
152 ErrorHandler::HandleError( nErr ); // including warnings
154 aSourceRef = pSourceShell;
155 aLoader.ReleaseDocRef(); // don't call DoClose in DocLoader dtor
160 void ScLinkedAreaDlg::InitFromOldLink( const OUString& rFile, const OUString& rFilter,
161 const OUString& rOptions, const OUString& rSource,
162 sal_uLong nRefresh )
164 LoadDocument( rFile, rFilter, rOptions );
165 if (pSourceShell)
167 SfxMedium* pMed = pSourceShell->GetMedium();
168 m_pCbUrl->SetText( pMed->GetName() );
170 else
171 m_pCbUrl->SetText( EMPTY_OUSTRING );
173 UpdateSourceRanges();
175 sal_Int32 nRangeCount = comphelper::string::getTokenCount(rSource, ';');
176 for ( sal_Int32 i=0; i<nRangeCount; i++ )
178 OUString aRange = rSource.getToken(i,';');
179 m_pLbRanges->SelectEntry( aRange );
182 bool bDoRefresh = (nRefresh != 0);
183 m_pBtnReload->Check( bDoRefresh );
184 if (bDoRefresh)
185 m_pNfDelay->SetValue( nRefresh );
187 UpdateEnable();
190 IMPL_LINK_NOARG_TYPED(ScLinkedAreaDlg, RangeHdl, ListBox&, void)
192 UpdateEnable();
195 IMPL_LINK_NOARG_TYPED(ScLinkedAreaDlg, ReloadHdl, Button*, void)
197 UpdateEnable();
200 IMPL_LINK_TYPED( ScLinkedAreaDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
202 if ( _pFileDlg->GetError() != ERRCODE_NONE )
203 return;
205 SfxMedium* pMed = pDocInserter->CreateMedium();
206 if ( pMed )
208 WaitObject aWait( this );
210 // replace HTML filter with DataQuery filter
211 const OUString aHTMLFilterName( FILTERNAME_HTML );
212 const OUString aWebQFilterName( FILTERNAME_QUERY );
214 std::shared_ptr<const SfxFilter> pFilter = pMed->GetFilter();
215 if (pFilter && aHTMLFilterName.equals(pFilter->GetFilterName()))
217 std::shared_ptr<const SfxFilter> pNewFilter =
218 ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aWebQFilterName );
219 if( pNewFilter )
220 pMed->SetFilter( pNewFilter );
223 // ERRCTX_SFX_OPENDOC -> "Error loading document"
224 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
226 if (pSourceShell)
227 pSourceShell->DoClose(); // deleted when assigning aSourceRef
229 pMed->UseInteractionHandler( true ); // to enable the filter options dialog
231 pSourceShell = new ScDocShell;
232 aSourceRef = pSourceShell;
233 pSourceShell->DoLoad( pMed );
235 sal_uLong nErr = pSourceShell->GetErrorCode();
236 if (nErr)
237 ErrorHandler::HandleError( nErr ); // including warnings
239 if ( !pSourceShell->GetError() ) // only errors
241 m_pCbUrl->SetText( pMed->GetName() );
243 else
245 pSourceShell->DoClose();
246 pSourceShell = nullptr;
247 aSourceRef.Clear();
249 m_pCbUrl->SetText( EMPTY_OUSTRING );
253 UpdateSourceRanges();
254 UpdateEnable();
257 #undef FILTERNAME_HTML
258 #undef FILTERNAME_QUERY
260 void ScLinkedAreaDlg::UpdateSourceRanges()
262 m_pLbRanges->SetUpdateMode(false);
264 m_pLbRanges->Clear();
265 if ( pSourceShell )
267 ScAreaNameIterator aIter( &pSourceShell->GetDocument() );
268 ScRange aDummy;
269 OUString aName;
270 while ( aIter.Next( aName, aDummy ) )
271 m_pLbRanges->InsertEntry( aName );
274 m_pLbRanges->SetUpdateMode(true);
276 if ( m_pLbRanges->GetEntryCount() == 1 )
277 m_pLbRanges->SelectEntryPos(0);
280 void ScLinkedAreaDlg::UpdateEnable()
282 bool bEnable = ( pSourceShell && m_pLbRanges->GetSelectEntryCount() );
283 m_pBtnOk->Enable( bEnable );
285 bool bReload = m_pBtnReload->IsChecked();
286 m_pNfDelay->Enable( bReload );
287 m_pFtSeconds->Enable( bReload );
290 OUString ScLinkedAreaDlg::GetURL()
292 if (pSourceShell)
294 SfxMedium* pMed = pSourceShell->GetMedium();
295 return pMed->GetName();
297 return EMPTY_OUSTRING;
300 OUString ScLinkedAreaDlg::GetFilter()
302 if (pSourceShell)
304 SfxMedium* pMed = pSourceShell->GetMedium();
305 return pMed->GetFilter()->GetFilterName();
307 return OUString();
310 OUString ScLinkedAreaDlg::GetOptions()
312 if (pSourceShell)
314 SfxMedium* pMed = pSourceShell->GetMedium();
315 return ScDocumentLoader::GetOptions( *pMed );
317 return OUString();
320 OUString ScLinkedAreaDlg::GetSource()
322 OUStringBuffer aBuf;
323 const sal_Int32 nCount = m_pLbRanges->GetSelectEntryCount();
324 for (sal_Int32 i=0; i<nCount; ++i)
326 if (i > 0)
327 aBuf.append(';');
328 aBuf.append(m_pLbRanges->GetSelectEntry(i));
330 return aBuf.makeStringAndClear();
333 sal_uLong ScLinkedAreaDlg::GetRefresh()
335 if ( m_pBtnReload->IsChecked() )
336 return sal::static_int_cast<sal_uLong>( m_pNfDelay->GetValue() );
337 else
338 return 0; // disabled
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */