Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #undef SC_DLLIMPLEMENTATION
31 : :
32 : :
33 : :
34 : : #include <comphelper/string.hxx>
35 : : #include <sfx2/app.hxx>
36 : : #include <sfx2/docfile.hxx>
37 : : #include <sfx2/docfilt.hxx>
38 : : #include <sfx2/docinsert.hxx>
39 : : #include <sfx2/fcontnr.hxx>
40 : : #include <sfx2/filedlghelper.hxx>
41 : : #include <svtools/ehdl.hxx>
42 : : #include <svtools/sfxecode.hxx>
43 : : #include <vcl/waitobj.hxx>
44 : :
45 : : #include "linkarea.hxx"
46 : : #include "linkarea.hrc"
47 : : #include "scresid.hxx"
48 : : #include "sc.hrc"
49 : : #include "rangeutl.hxx"
50 : : #include "docsh.hxx"
51 : : #include "tablink.hxx"
52 : :
53 : : //==================================================================
54 : :
55 : 0 : ScLinkedAreaDlg::ScLinkedAreaDlg( Window* pParent ) :
56 : : ModalDialog ( pParent, ScResId( RID_SCDLG_LINKAREA ) ),
57 : : //
58 : : aFlLocation ( this, ScResId( FL_LOCATION ) ),
59 : : aCbUrl ( this, ScResId( CB_URL ) ),
60 : : aBtnBrowse ( this, ScResId( BTN_BROWSE ) ),
61 : : aTxtHint ( this, ScResId( FT_HINT ) ),
62 : : aFtRanges ( this, ScResId( FT_RANGES ) ),
63 : : aLbRanges ( this, ScResId( LB_RANGES ) ),
64 : : aBtnReload ( this, ScResId( BTN_RELOAD ) ),
65 : : aNfDelay ( this, ScResId( NF_DELAY ) ),
66 : : aFtSeconds ( this, ScResId( FT_SECONDS ) ),
67 : : aBtnOk ( this, ScResId( BTN_OK ) ),
68 : : aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
69 : : aBtnHelp ( this, ScResId( BTN_HELP ) ),
70 : : //
71 : : pSourceShell( NULL ),
72 : 0 : pDocInserter( NULL )
73 : :
74 : : {
75 : 0 : FreeResource();
76 : :
77 : 0 : aCbUrl.SetHelpId( HID_SCDLG_LINKAREAURL ); // SvtURLBox ctor always sets SID_OPENURL
78 : 0 : aCbUrl.SetSelectHdl( LINK( this, ScLinkedAreaDlg, FileHdl ) );
79 : 0 : aBtnBrowse.SetClickHdl( LINK( this, ScLinkedAreaDlg, BrowseHdl ) );
80 : 0 : aLbRanges.SetSelectHdl( LINK( this, ScLinkedAreaDlg, RangeHdl ) );
81 : 0 : aBtnReload.SetClickHdl( LINK( this, ScLinkedAreaDlg, ReloadHdl ) );
82 : 0 : UpdateEnable();
83 : :
84 : 0 : aNfDelay.SetAccessibleName(aBtnReload.GetText());
85 : 0 : aNfDelay.SetAccessibleRelationLabeledBy(&aBtnReload);
86 : 0 : }
87 : :
88 : 0 : ScLinkedAreaDlg::~ScLinkedAreaDlg()
89 : : {
90 : : // pSourceShell is deleted by aSourceRef
91 : 0 : }
92 : :
93 : 0 : short ScLinkedAreaDlg::Execute()
94 : : {
95 : : // set parent for file dialog or filter options
96 : :
97 : 0 : Window* pOldDefParent = Application::GetDefDialogParent();
98 : 0 : Application::SetDefDialogParent( this );
99 : :
100 : 0 : short nRet = ModalDialog::Execute();
101 : :
102 : 0 : Application::SetDefDialogParent( pOldDefParent );
103 : :
104 : 0 : return nRet;
105 : : }
106 : :
107 : : #define FILTERNAME_HTML "HTML (StarCalc)"
108 : : #define FILTERNAME_QUERY "calc_HTML_WebQuery"
109 : :
110 : 0 : IMPL_LINK_NOARG(ScLinkedAreaDlg, BrowseHdl)
111 : : {
112 : 0 : if ( !pDocInserter )
113 : : pDocInserter = new sfx2::DocumentInserter(
114 : 0 : rtl::OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
115 : 0 : pDocInserter->StartExecuteModal( LINK( this, ScLinkedAreaDlg, DialogClosedHdl ) );
116 : 0 : return 0;
117 : : }
118 : :
119 : 0 : IMPL_LINK_NOARG(ScLinkedAreaDlg, FileHdl)
120 : : {
121 : 0 : rtl::OUString aEntered = aCbUrl.GetURL();
122 : 0 : if (pSourceShell)
123 : : {
124 : 0 : SfxMedium* pMed = pSourceShell->GetMedium();
125 : 0 : if ( aEntered.equals(pMed->GetName()) )
126 : : {
127 : : // already loaded - nothing to do
128 : 0 : return 0;
129 : : }
130 : : }
131 : :
132 : 0 : rtl::OUString aFilter;
133 : 0 : rtl::OUString aOptions;
134 : : // get filter name by looking at the file content (bWithContent = TRUE)
135 : : // Break operation if any error occurred inside.
136 : 0 : if (!ScDocumentLoader::GetFilterName( aEntered, aFilter, aOptions, true, true ))
137 : 0 : return 0;
138 : :
139 : : // #i53241# replace HTML filter with DataQuery filter
140 : 0 : if (aFilter.equalsAscii(FILTERNAME_HTML))
141 : 0 : aFilter = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(FILTERNAME_QUERY));
142 : :
143 : 0 : LoadDocument( aEntered, aFilter, aOptions );
144 : :
145 : 0 : UpdateSourceRanges();
146 : 0 : UpdateEnable();
147 : 0 : return 0;
148 : : }
149 : :
150 : 0 : void ScLinkedAreaDlg::LoadDocument( const rtl::OUString& rFile, const String& rFilter, const String& rOptions )
151 : : {
152 : 0 : if ( pSourceShell )
153 : : {
154 : : // unload old document
155 : 0 : pSourceShell->DoClose();
156 : 0 : pSourceShell = NULL;
157 : 0 : aSourceRef.Clear();
158 : : }
159 : :
160 : 0 : if ( !rFile.isEmpty() )
161 : : {
162 : 0 : WaitObject aWait( this );
163 : :
164 : 0 : rtl::OUString aNewFilter = rFilter;
165 : 0 : rtl::OUString aNewOptions = rOptions;
166 : :
167 : 0 : SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, rFile );
168 : :
169 : 0 : ScDocumentLoader aLoader( rFile, aNewFilter, aNewOptions, 0, true ); // with interaction
170 : 0 : pSourceShell = aLoader.GetDocShell();
171 : 0 : if ( pSourceShell )
172 : : {
173 : 0 : sal_uLong nErr = pSourceShell->GetErrorCode();
174 : 0 : if (nErr)
175 : 0 : ErrorHandler::HandleError( nErr ); // including warnings
176 : :
177 : 0 : aSourceRef = pSourceShell;
178 : 0 : aLoader.ReleaseDocRef(); // don't call DoClose in DocLoader dtor
179 : 0 : }
180 : : }
181 : 0 : }
182 : :
183 : 0 : void ScLinkedAreaDlg::InitFromOldLink( const String& rFile, const String& rFilter,
184 : : const String& rOptions, const String& rSource,
185 : : sal_uLong nRefresh )
186 : : {
187 : 0 : LoadDocument( rFile, rFilter, rOptions );
188 : 0 : if (pSourceShell)
189 : : {
190 : 0 : SfxMedium* pMed = pSourceShell->GetMedium();
191 : 0 : aCbUrl.SetText( pMed->GetName() );
192 : : }
193 : : else
194 : 0 : aCbUrl.SetText( EMPTY_STRING );
195 : :
196 : 0 : UpdateSourceRanges();
197 : :
198 : 0 : xub_StrLen nRangeCount = comphelper::string::getTokenCount(rSource, ';');
199 : 0 : for ( xub_StrLen i=0; i<nRangeCount; i++ )
200 : : {
201 : 0 : String aRange = rSource.GetToken(i);
202 : 0 : aLbRanges.SelectEntry( aRange );
203 : 0 : }
204 : :
205 : 0 : bool bDoRefresh = (nRefresh != 0);
206 : 0 : aBtnReload.Check( bDoRefresh );
207 : 0 : if (bDoRefresh)
208 : 0 : aNfDelay.SetValue( nRefresh );
209 : :
210 : 0 : UpdateEnable();
211 : 0 : }
212 : :
213 : 0 : IMPL_LINK_NOARG(ScLinkedAreaDlg, RangeHdl)
214 : : {
215 : 0 : UpdateEnable();
216 : 0 : return 0;
217 : : }
218 : :
219 : 0 : IMPL_LINK_NOARG(ScLinkedAreaDlg, ReloadHdl)
220 : : {
221 : 0 : UpdateEnable();
222 : 0 : return 0;
223 : : }
224 : :
225 : 0 : IMPL_LINK( ScLinkedAreaDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
226 : : {
227 : 0 : if ( _pFileDlg->GetError() != ERRCODE_NONE )
228 : 0 : return 0;
229 : :
230 : 0 : SfxMedium* pMed = pDocInserter->CreateMedium();
231 : 0 : if ( pMed )
232 : : {
233 : 0 : WaitObject aWait( this );
234 : :
235 : : // replace HTML filter with DataQuery filter
236 : 0 : const rtl::OUString aHTMLFilterName( RTL_CONSTASCII_USTRINGPARAM( FILTERNAME_HTML ) );
237 : 0 : const rtl::OUString aWebQFilterName( RTL_CONSTASCII_USTRINGPARAM( FILTERNAME_QUERY ) );
238 : :
239 : 0 : const SfxFilter* pFilter = pMed->GetFilter();
240 : 0 : if (pFilter && aHTMLFilterName.equals(pFilter->GetFilterName()))
241 : : {
242 : : const SfxFilter* pNewFilter =
243 : 0 : ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aWebQFilterName );
244 : 0 : if( pNewFilter )
245 : 0 : pMed->SetFilter( pNewFilter );
246 : : }
247 : :
248 : : // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
249 : 0 : SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
250 : :
251 : 0 : if (pSourceShell)
252 : 0 : pSourceShell->DoClose(); // deleted when assigning aSourceRef
253 : :
254 : 0 : pMed->UseInteractionHandler( sal_True ); // to enable the filter options dialog
255 : :
256 : 0 : pSourceShell = new ScDocShell;
257 : 0 : aSourceRef = pSourceShell;
258 : 0 : pSourceShell->DoLoad( pMed );
259 : :
260 : 0 : sal_uLong nErr = pSourceShell->GetErrorCode();
261 : 0 : if (nErr)
262 : 0 : ErrorHandler::HandleError( nErr ); // including warnings
263 : :
264 : 0 : if ( !pSourceShell->GetError() ) // only errors
265 : : {
266 : 0 : aCbUrl.SetText( pMed->GetName() );
267 : : }
268 : : else
269 : : {
270 : 0 : pSourceShell->DoClose();
271 : 0 : pSourceShell = NULL;
272 : 0 : aSourceRef.Clear();
273 : :
274 : 0 : aCbUrl.SetText( EMPTY_STRING );
275 : 0 : }
276 : : }
277 : :
278 : 0 : UpdateSourceRanges();
279 : 0 : UpdateEnable();
280 : 0 : return 0;
281 : : }
282 : :
283 : : #undef FILTERNAME_HTML
284 : : #undef FILTERNAME_QUERY
285 : :
286 : 0 : void ScLinkedAreaDlg::UpdateSourceRanges()
287 : : {
288 : 0 : aLbRanges.SetUpdateMode(false);
289 : :
290 : 0 : aLbRanges.Clear();
291 : 0 : if ( pSourceShell )
292 : : {
293 : 0 : ScAreaNameIterator aIter( pSourceShell->GetDocument() );
294 : 0 : ScRange aDummy;
295 : 0 : rtl::OUString aName;
296 : 0 : while ( aIter.Next( aName, aDummy ) )
297 : 0 : aLbRanges.InsertEntry( aName );
298 : : }
299 : :
300 : 0 : aLbRanges.SetUpdateMode(true);
301 : :
302 : 0 : if ( aLbRanges.GetEntryCount() == 1 )
303 : 0 : aLbRanges.SelectEntryPos(0);
304 : 0 : }
305 : :
306 : 0 : void ScLinkedAreaDlg::UpdateEnable()
307 : : {
308 : 0 : bool bEnable = ( pSourceShell && aLbRanges.GetSelectEntryCount() );
309 : 0 : aBtnOk.Enable( bEnable );
310 : :
311 : 0 : bool bReload = aBtnReload.IsChecked();
312 : 0 : aNfDelay.Enable( bReload );
313 : 0 : aFtSeconds.Enable( bReload );
314 : 0 : }
315 : :
316 : 0 : rtl::OUString ScLinkedAreaDlg::GetURL()
317 : : {
318 : 0 : if (pSourceShell)
319 : : {
320 : 0 : SfxMedium* pMed = pSourceShell->GetMedium();
321 : 0 : return pMed->GetName();
322 : : }
323 : 0 : return EMPTY_STRING;
324 : : }
325 : :
326 : 0 : rtl::OUString ScLinkedAreaDlg::GetFilter()
327 : : {
328 : 0 : if (pSourceShell)
329 : : {
330 : 0 : SfxMedium* pMed = pSourceShell->GetMedium();
331 : 0 : return pMed->GetFilter()->GetFilterName();
332 : : }
333 : 0 : return rtl::OUString();
334 : : }
335 : :
336 : 0 : rtl::OUString ScLinkedAreaDlg::GetOptions()
337 : : {
338 : 0 : if (pSourceShell)
339 : : {
340 : 0 : SfxMedium* pMed = pSourceShell->GetMedium();
341 : 0 : return ScDocumentLoader::GetOptions( *pMed );
342 : : }
343 : 0 : return rtl::OUString();
344 : : }
345 : :
346 : 0 : rtl::OUString ScLinkedAreaDlg::GetSource()
347 : : {
348 : 0 : rtl::OUStringBuffer aBuf;
349 : 0 : sal_uInt16 nCount = aLbRanges.GetSelectEntryCount();
350 : 0 : for (sal_uInt16 i=0; i<nCount; i++)
351 : : {
352 : 0 : if (i > 0)
353 : 0 : aBuf.append(sal_Unicode(';'));
354 : 0 : aBuf.append(aLbRanges.GetSelectEntry(i));
355 : : }
356 : 0 : return aBuf.makeStringAndClear();
357 : : }
358 : :
359 : 0 : sal_uLong ScLinkedAreaDlg::GetRefresh()
360 : : {
361 : 0 : if ( aBtnReload.IsChecked() )
362 : 0 : return sal::static_int_cast<sal_uLong>( aNfDelay.GetValue() );
363 : : else
364 : 0 : return 0; // disabled
365 : : }
366 : :
367 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|