Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sot/formats.hxx>
21 : #include <sfx2/app.hxx>
22 : #include <sfx2/linkmgr.hxx>
23 : #include "servobj.hxx"
24 : #include "docsh.hxx"
25 : #include "impex.hxx"
26 : #include "brdcst.hxx"
27 : #include "rangenam.hxx"
28 : #include "sc.hrc"
29 :
30 : using namespace formula;
31 :
32 10 : static bool lcl_FillRangeFromName( ScRange& rRange, ScDocShell* pDocSh, const OUString& rName )
33 : {
34 10 : if (pDocSh)
35 : {
36 10 : ScDocument& rDoc = pDocSh->GetDocument();
37 10 : ScRangeName* pNames = rDoc.GetRangeName();
38 10 : if (pNames)
39 : {
40 10 : const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(rName));
41 10 : if (pData)
42 : {
43 0 : if ( pData->IsValidReference( rRange ) )
44 0 : return true;
45 : }
46 : }
47 : }
48 10 : return false;
49 : }
50 :
51 10 : ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
52 : ScServerObject* pObjP)
53 10 : : pObj(pObjP)
54 : {
55 10 : }
56 :
57 10 : ScServerObjectSvtListenerForwarder::~ScServerObjectSvtListenerForwarder()
58 : {
59 : //! do NOT access pObj
60 10 : }
61 :
62 0 : void ScServerObjectSvtListenerForwarder::Notify( const SfxHint& rHint )
63 : {
64 0 : pObj->Notify( aBroadcaster, rHint);
65 0 : }
66 :
67 10 : ScServerObject::ScServerObject( ScDocShell* pShell, const OUString& rItem ) :
68 : aForwarder( this ),
69 : pDocSh( pShell ),
70 10 : bRefreshListener( false )
71 : {
72 : // parse item string
73 :
74 10 : if ( lcl_FillRangeFromName( aRange, pDocSh, rItem ) )
75 : {
76 0 : aItemStr = rItem; // must be parsed again on ref update
77 : }
78 : else
79 : {
80 : // parse ref
81 10 : ScDocument& rDoc = pDocSh->GetDocument();
82 10 : SCTAB nTab = ScDocShell::GetCurTab();
83 10 : aRange.aStart.SetTab( nTab );
84 :
85 : // For DDE link, we always must parse references using OOO A1 convention.
86 :
87 10 : if ( aRange.Parse( rItem, &rDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
88 : {
89 : // area reference
90 : }
91 10 : else if ( aRange.aStart.Parse( rItem, &rDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
92 : {
93 : // cell reference
94 10 : aRange.aEnd = aRange.aStart;
95 : }
96 : else
97 : {
98 : OSL_FAIL("ScServerObject: invalid item");
99 : }
100 : }
101 :
102 10 : pDocSh->GetDocument().GetLinkManager()->InsertServer( this );
103 10 : pDocSh->GetDocument().StartListeningArea( aRange, &aForwarder );
104 :
105 10 : StartListening(*pDocSh); // um mitzubekommen, wenn die DocShell geloescht wird
106 10 : StartListening(*SfxGetpApp()); // for SC_HINT_AREAS_CHANGED
107 10 : }
108 :
109 30 : ScServerObject::~ScServerObject()
110 : {
111 10 : Clear();
112 20 : }
113 :
114 10 : void ScServerObject::Clear()
115 : {
116 10 : if (pDocSh)
117 : {
118 10 : ScDocShell* pTemp = pDocSh;
119 10 : pDocSh = NULL;
120 :
121 10 : pTemp->GetDocument().EndListeningArea( aRange, &aForwarder );
122 10 : pTemp->GetDocument().GetLinkManager()->RemoveServer( this );
123 10 : EndListening(*pTemp);
124 10 : EndListening(*SfxGetpApp());
125 : }
126 10 : }
127 :
128 0 : void ScServerObject::EndListeningAll()
129 : {
130 0 : aForwarder.EndListeningAll();
131 0 : SfxListener::EndListeningAll();
132 0 : }
133 :
134 10 : bool ScServerObject::GetData(
135 : ::com::sun::star::uno::Any & rData /*out param*/,
136 : const OUString & rMimeType, bool /* bSynchron */ )
137 : {
138 10 : if (!pDocSh)
139 0 : return false;
140 :
141 : // named ranges may have changed -> update aRange
142 10 : if ( !aItemStr.isEmpty() )
143 : {
144 0 : ScRange aNew;
145 0 : if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
146 : {
147 0 : aRange = aNew;
148 0 : bRefreshListener = true;
149 : }
150 : }
151 :
152 10 : if ( bRefreshListener )
153 : {
154 : // refresh the listeners now (this is called from a timer)
155 :
156 0 : EndListeningAll();
157 0 : pDocSh->GetDocument().StartListeningArea( aRange, &aForwarder );
158 0 : StartListening(*pDocSh);
159 0 : StartListening(*SfxGetpApp());
160 0 : bRefreshListener = false;
161 : }
162 :
163 10 : OUString aDdeTextFmt = pDocSh->GetDdeTextFmt();
164 10 : ScDocument& rDoc = pDocSh->GetDocument();
165 :
166 10 : if( FORMAT_STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ))
167 : {
168 10 : ScImportExport aObj( &rDoc, aRange );
169 10 : if( aDdeTextFmt[0] == 'F' )
170 0 : aObj.SetFormulas( true );
171 20 : if( aDdeTextFmt.equalsAscii( "SYLK" ) ||
172 10 : aDdeTextFmt.equalsAscii( "FSYLK" ) )
173 : {
174 0 : OString aByteData;
175 0 : if( aObj.ExportByteString( aByteData, osl_getThreadTextEncoding(), SOT_FORMATSTR_ID_SYLK ) )
176 : {
177 0 : rData <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
178 0 : (const sal_Int8*)aByteData.getStr(),
179 0 : aByteData.getLength() + 1 );
180 0 : return true;
181 : }
182 0 : return false;
183 : }
184 20 : if( aDdeTextFmt.equalsAscii( "CSV" ) ||
185 10 : aDdeTextFmt.equalsAscii( "FCSV" ) )
186 0 : aObj.SetSeparator( ',' );
187 10 : aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
188 10 : return aObj.ExportData( rMimeType, rData );
189 : }
190 :
191 0 : ScImportExport aObj( &rDoc, aRange );
192 0 : aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
193 0 : if( aObj.IsRef() )
194 0 : return aObj.ExportData( rMimeType, rData );
195 10 : return false;
196 : }
197 :
198 46 : void ScServerObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
199 : {
200 46 : bool bDataChanged = false;
201 :
202 : // DocShell can't be tested via type info, because SFX_HINT_DYING comes from the dtor
203 46 : if ( &rBC == pDocSh )
204 : {
205 : // from DocShell, only SFX_HINT_DYING is interesting
206 0 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
207 0 : if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
208 : {
209 0 : pDocSh = NULL;
210 0 : EndListening(*SfxGetpApp());
211 : // don't access DocShell anymore for EndListening etc.
212 : }
213 : }
214 46 : else if (rBC.ISA(SfxApplication))
215 : {
216 46 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
217 46 : if ( !aItemStr.isEmpty() && pSimpleHint &&
218 0 : pSimpleHint->GetId() == SC_HINT_AREAS_CHANGED )
219 : {
220 : // check if named range was modified
221 0 : ScRange aNew;
222 0 : if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
223 0 : bDataChanged = true;
224 : }
225 : }
226 : else
227 : {
228 : // must be from Area broadcasters
229 :
230 0 : const ScHint* pScHint = dynamic_cast<const ScHint*>( &rHint );
231 0 : if (pScHint && (pScHint->GetId() & SC_HINT_DATACHANGED))
232 0 : bDataChanged = true;
233 0 : else if ( dynamic_cast<const ScAreaChangedHint*>(&rHint) ) // position of broadcaster changed
234 : {
235 0 : ScRange aNewRange = static_cast<const ScAreaChangedHint&>(rHint).GetRange();
236 0 : if ( aRange != aNewRange )
237 : {
238 0 : bRefreshListener = true;
239 0 : bDataChanged = true;
240 0 : }
241 : }
242 0 : else if ( dynamic_cast<const SfxSimpleHint*>(&rHint) )
243 : {
244 0 : sal_uLong nId = static_cast<const SfxSimpleHint&>(rHint).GetId();
245 0 : if (nId == SFX_HINT_DYING)
246 : {
247 : // If the range is being deleted, listening must be restarted
248 : // after the deletion is complete (done in GetData)
249 0 : bRefreshListener = true;
250 0 : bDataChanged = true;
251 : }
252 : }
253 : }
254 :
255 46 : if ( bDataChanged && HasDataLinks() )
256 0 : SvLinkSource::NotifyDataChanged();
257 274 : }
258 :
259 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|