1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
36 #include "rangeutl.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)
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");
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
) );
63 ScLinkedAreaDlg::~ScLinkedAreaDlg()
68 void ScLinkedAreaDlg::dispose()
70 // pSourceShell is deleted by aSourceRef
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)
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();
102 SfxMedium
* pMed
= pSourceShell
->GetMedium();
103 if ( aEntered
.equals(pMed
->GetName()) )
105 // already loaded - nothing to do
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 ))
117 // #i53241# replace HTML filter with DataQuery filter
118 if (aFilter
== FILTERNAME_HTML
)
119 aFilter
= FILTERNAME_QUERY
;
121 LoadDocument( aEntered
, aFilter
, aOptions
);
123 UpdateSourceRanges();
127 void ScLinkedAreaDlg::LoadDocument( const OUString
& rFile
, const OUString
& rFilter
, const OUString
& rOptions
)
131 // unload old document
132 pSourceShell
->DoClose();
133 pSourceShell
= nullptr;
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();
150 sal_uLong nErr
= pSourceShell
->GetErrorCode();
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
,
164 LoadDocument( rFile
, rFilter
, rOptions
);
167 SfxMedium
* pMed
= pSourceShell
->GetMedium();
168 m_pCbUrl
->SetText( pMed
->GetName() );
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
);
185 m_pNfDelay
->SetValue( nRefresh
);
190 IMPL_LINK_NOARG_TYPED(ScLinkedAreaDlg
, RangeHdl
, ListBox
&, void)
195 IMPL_LINK_NOARG_TYPED(ScLinkedAreaDlg
, ReloadHdl
, Button
*, void)
200 IMPL_LINK_TYPED( ScLinkedAreaDlg
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
, void )
202 if ( _pFileDlg
->GetError() != ERRCODE_NONE
)
205 SfxMedium
* pMed
= pDocInserter
->CreateMedium();
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
);
220 pMed
->SetFilter( pNewFilter
);
223 // ERRCTX_SFX_OPENDOC -> "Error loading document"
224 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
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();
237 ErrorHandler::HandleError( nErr
); // including warnings
239 if ( !pSourceShell
->GetError() ) // only errors
241 m_pCbUrl
->SetText( pMed
->GetName() );
245 pSourceShell
->DoClose();
246 pSourceShell
= nullptr;
249 m_pCbUrl
->SetText( EMPTY_OUSTRING
);
253 UpdateSourceRanges();
257 #undef FILTERNAME_HTML
258 #undef FILTERNAME_QUERY
260 void ScLinkedAreaDlg::UpdateSourceRanges()
262 m_pLbRanges
->SetUpdateMode(false);
264 m_pLbRanges
->Clear();
267 ScAreaNameIterator
aIter( &pSourceShell
->GetDocument() );
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()
294 SfxMedium
* pMed
= pSourceShell
->GetMedium();
295 return pMed
->GetName();
297 return EMPTY_OUSTRING
;
300 OUString
ScLinkedAreaDlg::GetFilter()
304 SfxMedium
* pMed
= pSourceShell
->GetMedium();
305 return pMed
->GetFilter()->GetFilterName();
310 OUString
ScLinkedAreaDlg::GetOptions()
314 SfxMedium
* pMed
= pSourceShell
->GetMedium();
315 return ScDocumentLoader::GetOptions( *pMed
);
320 OUString
ScLinkedAreaDlg::GetSource()
323 const sal_Int32 nCount
= m_pLbRanges
->GetSelectEntryCount();
324 for (sal_Int32 i
=0; i
<nCount
; ++i
)
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() );
338 return 0; // disabled
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */