Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / miscdlgs / linkarea.cxx
blob879afd4d1aae0563d34988e5e09867d3c54f054e
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 "scresid.hxx"
38 #include "sc.hrc"
39 #include "rangeutl.hxx"
40 #include "docsh.hxx"
41 #include "tablink.hxx"
43 //==================================================================
45 ScLinkedAreaDlg::ScLinkedAreaDlg(Window* pParent)
46 : ModalDialog(pParent, "ExternalDataDialog", "modules/scalc/ui/externaldata.ui")
47 , pSourceShell(NULL)
48 , pDocInserter(NULL)
51 get(m_pCbUrl, "url");
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");
59 get(m_pBtnOk, "ok");
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 ) );
65 UpdateEnable();
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 );
84 return nRet;
87 #define FILTERNAME_HTML "HTML (StarCalc)"
88 #define FILTERNAME_QUERY "calc_HTML_WebQuery"
90 IMPL_LINK_NOARG(ScLinkedAreaDlg, BrowseHdl)
92 if ( !pDocInserter )
93 pDocInserter = new sfx2::DocumentInserter(
94 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
95 pDocInserter->StartExecuteModal( LINK( this, ScLinkedAreaDlg, DialogClosedHdl ) );
96 return 0;
99 IMPL_LINK_NOARG(ScLinkedAreaDlg, FileHdl)
101 OUString aEntered = m_pCbUrl->GetURL();
102 if (pSourceShell)
104 SfxMedium* pMed = pSourceShell->GetMedium();
105 if ( aEntered.equals(pMed->GetName()) )
107 // already loaded - nothing to do
108 return 0;
112 OUString aFilter;
113 OUString aOptions;
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 ))
117 return 0;
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();
126 UpdateEnable();
127 return 0;
130 void ScLinkedAreaDlg::LoadDocument( const OUString& rFile, const OUString& rFilter, const OUString& rOptions )
132 if ( pSourceShell )
134 // unload old document
135 pSourceShell->DoClose();
136 pSourceShell = NULL;
137 aSourceRef.Clear();
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();
151 if ( pSourceShell )
153 sal_uLong nErr = pSourceShell->GetErrorCode();
154 if (nErr)
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,
165 sal_uLong nRefresh )
167 LoadDocument( rFile, rFilter, rOptions );
168 if (pSourceShell)
170 SfxMedium* pMed = pSourceShell->GetMedium();
171 m_pCbUrl->SetText( pMed->GetName() );
173 else
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 );
187 if (bDoRefresh)
188 m_pNfDelay->SetValue( nRefresh );
190 UpdateEnable();
193 IMPL_LINK_NOARG(ScLinkedAreaDlg, RangeHdl)
195 UpdateEnable();
196 return 0;
199 IMPL_LINK_NOARG(ScLinkedAreaDlg, ReloadHdl)
201 UpdateEnable();
202 return 0;
205 IMPL_LINK( ScLinkedAreaDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
207 if ( _pFileDlg->GetError() != ERRCODE_NONE )
208 return 0;
210 SfxMedium* pMed = pDocInserter->CreateMedium();
211 if ( pMed )
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 );
224 if( pNewFilter )
225 pMed->SetFilter( pNewFilter );
228 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
229 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
231 if (pSourceShell)
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();
241 if (nErr)
242 ErrorHandler::HandleError( nErr ); // including warnings
244 if ( !pSourceShell->GetError() ) // only errors
246 m_pCbUrl->SetText( pMed->GetName() );
248 else
250 pSourceShell->DoClose();
251 pSourceShell = NULL;
252 aSourceRef.Clear();
254 m_pCbUrl->SetText( EMPTY_OUSTRING );
258 UpdateSourceRanges();
259 UpdateEnable();
260 return 0;
263 #undef FILTERNAME_HTML
264 #undef FILTERNAME_QUERY
266 void ScLinkedAreaDlg::UpdateSourceRanges()
268 m_pLbRanges->SetUpdateMode(false);
270 m_pLbRanges->Clear();
271 if ( pSourceShell )
273 ScAreaNameIterator aIter( pSourceShell->GetDocument() );
274 ScRange aDummy;
275 OUString aName;
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()
298 if (pSourceShell)
300 SfxMedium* pMed = pSourceShell->GetMedium();
301 return pMed->GetName();
303 return EMPTY_OUSTRING;
306 OUString ScLinkedAreaDlg::GetFilter()
308 if (pSourceShell)
310 SfxMedium* pMed = pSourceShell->GetMedium();
311 return pMed->GetFilter()->GetFilterName();
313 return OUString();
316 OUString ScLinkedAreaDlg::GetOptions()
318 if (pSourceShell)
320 SfxMedium* pMed = pSourceShell->GetMedium();
321 return ScDocumentLoader::GetOptions( *pMed );
323 return OUString();
326 OUString ScLinkedAreaDlg::GetSource()
328 OUStringBuffer aBuf;
329 sal_uInt16 nCount = m_pLbRanges->GetSelectEntryCount();
330 for (sal_uInt16 i=0; i<nCount; i++)
332 if (i > 0)
333 aBuf.append(';');
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() );
343 else
344 return 0; // disabled
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */