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 .
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 "scresid.hxx"
39 #include "rangeutl.hxx"
41 #include "tablink.hxx"
43 //==================================================================
45 ScLinkedAreaDlg::ScLinkedAreaDlg(Window
* pParent
)
46 : ModalDialog(pParent
, "ExternalDataDialog", "modules/scalc/ui/externaldata.ui")
52 get(m_pLbRanges
, "ranges");
53 m_pLbRanges
->EnableMultiSelection(true);
54 m_pLbRanges
->SetDropDownLineCount(8);
55 get(m_pBtnBrowse
, "browse");
56 get(m_pBtnReload
, "reload");
57 get(m_pNfDelay
, "delay");
58 get(m_pFtSeconds
, "secondsft");
61 m_pCbUrl
->SetSelectHdl( LINK( this, ScLinkedAreaDlg
, FileHdl
) );
62 m_pBtnBrowse
->SetClickHdl( LINK( this, ScLinkedAreaDlg
, BrowseHdl
) );
63 m_pLbRanges
->SetSelectHdl( LINK( this, ScLinkedAreaDlg
, RangeHdl
) );
64 m_pBtnReload
->SetClickHdl( LINK( this, ScLinkedAreaDlg
, ReloadHdl
) );
68 ScLinkedAreaDlg::~ScLinkedAreaDlg()
70 // pSourceShell is deleted by aSourceRef
73 short ScLinkedAreaDlg::Execute()
75 // set parent for file dialog or filter options
77 Window
* pOldDefParent
= Application::GetDefDialogParent();
78 Application::SetDefDialogParent( this );
80 short nRet
= ModalDialog::Execute();
82 Application::SetDefDialogParent( pOldDefParent
);
87 #define FILTERNAME_HTML "HTML (StarCalc)"
88 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
90 IMPL_LINK_NOARG(ScLinkedAreaDlg
, BrowseHdl
)
93 pDocInserter
= new sfx2::DocumentInserter(
94 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
95 pDocInserter
->StartExecuteModal( LINK( this, ScLinkedAreaDlg
, DialogClosedHdl
) );
99 IMPL_LINK_NOARG(ScLinkedAreaDlg
, FileHdl
)
101 OUString aEntered
= m_pCbUrl
->GetURL();
104 SfxMedium
* pMed
= pSourceShell
->GetMedium();
105 if ( aEntered
.equals(pMed
->GetName()) )
107 // already loaded - nothing to do
114 // get filter name by looking at the file content (bWithContent = true)
115 // Break operation if any error occurred inside.
116 if (!ScDocumentLoader::GetFilterName( aEntered
, aFilter
, aOptions
, true, true ))
119 // #i53241# replace HTML filter with DataQuery filter
120 if (aFilter
.equalsAscii(FILTERNAME_HTML
))
121 aFilter
= OUString(FILTERNAME_QUERY
);
123 LoadDocument( aEntered
, aFilter
, aOptions
);
125 UpdateSourceRanges();
130 void ScLinkedAreaDlg::LoadDocument( const OUString
& rFile
, const OUString
& rFilter
, const OUString
& rOptions
)
134 // unload old document
135 pSourceShell
->DoClose();
140 if ( !rFile
.isEmpty() )
142 WaitObject
aWait( this );
144 OUString aNewFilter
= rFilter
;
145 OUString aNewOptions
= rOptions
;
147 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, rFile
);
149 ScDocumentLoader
aLoader( rFile
, aNewFilter
, aNewOptions
, 0, true ); // with interaction
150 pSourceShell
= aLoader
.GetDocShell();
153 sal_uLong nErr
= pSourceShell
->GetErrorCode();
155 ErrorHandler::HandleError( nErr
); // including warnings
157 aSourceRef
= pSourceShell
;
158 aLoader
.ReleaseDocRef(); // don't call DoClose in DocLoader dtor
163 void ScLinkedAreaDlg::InitFromOldLink( const OUString
& rFile
, const OUString
& rFilter
,
164 const OUString
& rOptions
, const OUString
& rSource
,
167 LoadDocument( rFile
, rFilter
, rOptions
);
170 SfxMedium
* pMed
= pSourceShell
->GetMedium();
171 m_pCbUrl
->SetText( pMed
->GetName() );
174 m_pCbUrl
->SetText( EMPTY_OUSTRING
);
176 UpdateSourceRanges();
178 sal_Int32 nRangeCount
= comphelper::string::getTokenCount(rSource
, ';');
179 for ( sal_Int32 i
=0; i
<nRangeCount
; i
++ )
181 OUString aRange
= rSource
.getToken(i
,';');
182 m_pLbRanges
->SelectEntry( aRange
);
185 bool bDoRefresh
= (nRefresh
!= 0);
186 m_pBtnReload
->Check( bDoRefresh
);
188 m_pNfDelay
->SetValue( nRefresh
);
193 IMPL_LINK_NOARG(ScLinkedAreaDlg
, RangeHdl
)
199 IMPL_LINK_NOARG(ScLinkedAreaDlg
, ReloadHdl
)
205 IMPL_LINK( ScLinkedAreaDlg
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
207 if ( _pFileDlg
->GetError() != ERRCODE_NONE
)
210 SfxMedium
* pMed
= pDocInserter
->CreateMedium();
213 WaitObject
aWait( this );
215 // replace HTML filter with DataQuery filter
216 const OUString
aHTMLFilterName( FILTERNAME_HTML
);
217 const OUString
aWebQFilterName( FILTERNAME_QUERY
);
219 const SfxFilter
* pFilter
= pMed
->GetFilter();
220 if (pFilter
&& aHTMLFilterName
.equals(pFilter
->GetFilterName()))
222 const SfxFilter
* pNewFilter
=
223 ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aWebQFilterName
);
225 pMed
->SetFilter( pNewFilter
);
228 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
229 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
232 pSourceShell
->DoClose(); // deleted when assigning aSourceRef
234 pMed
->UseInteractionHandler( sal_True
); // to enable the filter options dialog
236 pSourceShell
= new ScDocShell
;
237 aSourceRef
= pSourceShell
;
238 pSourceShell
->DoLoad( pMed
);
240 sal_uLong nErr
= pSourceShell
->GetErrorCode();
242 ErrorHandler::HandleError( nErr
); // including warnings
244 if ( !pSourceShell
->GetError() ) // only errors
246 m_pCbUrl
->SetText( pMed
->GetName() );
250 pSourceShell
->DoClose();
254 m_pCbUrl
->SetText( EMPTY_OUSTRING
);
258 UpdateSourceRanges();
263 #undef FILTERNAME_HTML
264 #undef FILTERNAME_QUERY
266 void ScLinkedAreaDlg::UpdateSourceRanges()
268 m_pLbRanges
->SetUpdateMode(false);
270 m_pLbRanges
->Clear();
273 ScAreaNameIterator
aIter( pSourceShell
->GetDocument() );
276 while ( aIter
.Next( aName
, aDummy
) )
277 m_pLbRanges
->InsertEntry( aName
);
280 m_pLbRanges
->SetUpdateMode(true);
282 if ( m_pLbRanges
->GetEntryCount() == 1 )
283 m_pLbRanges
->SelectEntryPos(0);
286 void ScLinkedAreaDlg::UpdateEnable()
288 bool bEnable
= ( pSourceShell
&& m_pLbRanges
->GetSelectEntryCount() );
289 m_pBtnOk
->Enable( bEnable
);
291 bool bReload
= m_pBtnReload
->IsChecked();
292 m_pNfDelay
->Enable( bReload
);
293 m_pFtSeconds
->Enable( bReload
);
296 OUString
ScLinkedAreaDlg::GetURL()
300 SfxMedium
* pMed
= pSourceShell
->GetMedium();
301 return pMed
->GetName();
303 return EMPTY_OUSTRING
;
306 OUString
ScLinkedAreaDlg::GetFilter()
310 SfxMedium
* pMed
= pSourceShell
->GetMedium();
311 return pMed
->GetFilter()->GetFilterName();
316 OUString
ScLinkedAreaDlg::GetOptions()
320 SfxMedium
* pMed
= pSourceShell
->GetMedium();
321 return ScDocumentLoader::GetOptions( *pMed
);
326 OUString
ScLinkedAreaDlg::GetSource()
329 sal_uInt16 nCount
= m_pLbRanges
->GetSelectEntryCount();
330 for (sal_uInt16 i
=0; i
<nCount
; i
++)
334 aBuf
.append(m_pLbRanges
->GetSelectEntry(i
));
336 return aBuf
.makeStringAndClear();
339 sal_uLong
ScLinkedAreaDlg::GetRefresh()
341 if ( m_pBtnReload
->IsChecked() )
342 return sal::static_int_cast
<sal_uLong
>( m_pNfDelay
->GetValue() );
344 return 0; // disabled
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */