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 0 : static bool lcl_FillRangeFromName( ScRange& rRange, ScDocShell* pDocSh, const OUString& rName )
33 : {
34 0 : if (pDocSh)
35 : {
36 0 : ScDocument* pDoc = pDocSh->GetDocument();
37 0 : ScRangeName* pNames = pDoc->GetRangeName();
38 0 : if (pNames)
39 : {
40 0 : const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(rName));
41 0 : if (pData)
42 : {
43 0 : if ( pData->IsValidReference( rRange ) )
44 0 : return true;
45 : }
46 : }
47 : }
48 0 : return false;
49 : }
50 :
51 0 : ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
52 : ScServerObject* pObjP)
53 0 : : pObj(pObjP)
54 : {
55 0 : }
56 :
57 0 : ScServerObjectSvtListenerForwarder::~ScServerObjectSvtListenerForwarder()
58 : {
59 : //! do NOT access pObj
60 0 : }
61 :
62 0 : void ScServerObjectSvtListenerForwarder::Notify( const SfxHint& rHint )
63 : {
64 0 : pObj->Notify( aBroadcaster, rHint);
65 0 : }
66 :
67 0 : ScServerObject::ScServerObject( ScDocShell* pShell, const OUString& rItem ) :
68 : aForwarder( this ),
69 : pDocSh( pShell ),
70 0 : bRefreshListener( false )
71 : {
72 : // parse item string
73 :
74 0 : 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 0 : ScDocument* pDoc = pDocSh->GetDocument();
82 0 : SCTAB nTab = pDocSh->GetCurTab();
83 0 : aRange.aStart.SetTab( nTab );
84 :
85 : // For DDE link, we always must parse references using OOO A1 convention.
86 :
87 0 : if ( aRange.Parse( rItem, pDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
88 : {
89 : // area reference
90 : }
91 0 : else if ( aRange.aStart.Parse( rItem, pDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
92 : {
93 : // cell reference
94 0 : aRange.aEnd = aRange.aStart;
95 : }
96 : else
97 : {
98 : OSL_FAIL("ScServerObject: invalid item");
99 : }
100 : }
101 :
102 0 : pDocSh->GetDocument()->GetLinkManager()->InsertServer( this );
103 0 : pDocSh->GetDocument()->StartListeningArea( aRange, &aForwarder );
104 :
105 0 : StartListening(*pDocSh); // um mitzubekommen, wenn die DocShell geloescht wird
106 0 : StartListening(*SFX_APP()); // for SC_HINT_AREAS_CHANGED
107 0 : }
108 :
109 0 : ScServerObject::~ScServerObject()
110 : {
111 0 : Clear();
112 0 : }
113 :
114 0 : void ScServerObject::Clear()
115 : {
116 0 : if (pDocSh)
117 : {
118 0 : ScDocShell* pTemp = pDocSh;
119 0 : pDocSh = NULL;
120 :
121 0 : pTemp->GetDocument()->EndListeningArea( aRange, &aForwarder );
122 0 : pTemp->GetDocument()->GetLinkManager()->RemoveServer( this );
123 0 : EndListening(*pTemp);
124 0 : EndListening(*SFX_APP());
125 : }
126 0 : }
127 :
128 0 : void ScServerObject::EndListeningAll()
129 : {
130 0 : aForwarder.EndListeningAll();
131 0 : SfxListener::EndListeningAll();
132 0 : }
133 :
134 0 : bool ScServerObject::GetData(
135 : ::com::sun::star::uno::Any & rData /*out param*/,
136 : const OUString & rMimeType, bool /* bSynchron */ )
137 : {
138 0 : if (!pDocSh)
139 0 : return false;
140 :
141 : // named ranges may have changed -> update aRange
142 0 : 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 0 : 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(*SFX_APP());
160 0 : bRefreshListener = false;
161 : }
162 :
163 0 : OUString aDdeTextFmt = pDocSh->GetDdeTextFmt();
164 0 : ScDocument* pDoc = pDocSh->GetDocument();
165 :
166 0 : if( FORMAT_STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ))
167 : {
168 0 : ScImportExport aObj( pDoc, aRange );
169 0 : if( aDdeTextFmt[0] == 'F' )
170 0 : aObj.SetFormulas( true );
171 0 : if( aDdeTextFmt.equalsAscii( "SYLK" ) ||
172 0 : 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 0 : if( aDdeTextFmt.equalsAscii( "CSV" ) ||
185 0 : aDdeTextFmt.equalsAscii( "FCSV" ) )
186 0 : aObj.SetSeparator( ',' );
187 0 : aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
188 0 : return aObj.ExportData( rMimeType, rData );
189 : }
190 :
191 0 : ScImportExport aObj( pDoc, aRange );
192 0 : aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
193 0 : if( aObj.IsRef() )
194 0 : return aObj.ExportData( rMimeType, rData );
195 0 : return false;
196 : }
197 :
198 0 : void ScServerObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
199 : {
200 0 : bool bDataChanged = false;
201 :
202 : // DocShell can't be tested via type info, because SFX_HINT_DYING comes from the dtor
203 0 : if ( &rBC == pDocSh )
204 : {
205 : // from DocShell, only SFX_HINT_DYING is interesting
206 0 : if ( rHint.ISA(SfxSimpleHint) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
207 : {
208 0 : pDocSh = NULL;
209 0 : EndListening(*SFX_APP());
210 : // don't access DocShell anymore for EndListening etc.
211 : }
212 : }
213 0 : else if (rBC.ISA(SfxApplication))
214 : {
215 0 : if ( !aItemStr.isEmpty() && rHint.ISA(SfxSimpleHint) &&
216 0 : ((const SfxSimpleHint&)rHint).GetId() == SC_HINT_AREAS_CHANGED )
217 : {
218 : // check if named range was modified
219 0 : ScRange aNew;
220 0 : if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
221 0 : bDataChanged = true;
222 : }
223 : }
224 : else
225 : {
226 : // must be from Area broadcasters
227 :
228 0 : const ScHint* pScHint = PTR_CAST( ScHint, &rHint );
229 0 : if (pScHint && (pScHint->GetId() & SC_HINT_DATACHANGED))
230 0 : bDataChanged = true;
231 0 : else if (rHint.ISA(ScAreaChangedHint)) // position of broadcaster changed
232 : {
233 0 : ScRange aNewRange = ((const ScAreaChangedHint&)rHint).GetRange();
234 0 : if ( aRange != aNewRange )
235 : {
236 0 : bRefreshListener = true;
237 0 : bDataChanged = true;
238 : }
239 : }
240 0 : else if (rHint.ISA(SfxSimpleHint))
241 : {
242 0 : sal_uLong nId = ((const SfxSimpleHint&)rHint).GetId();
243 0 : if (nId == SFX_HINT_DYING)
244 : {
245 : // If the range is being deleted, listening must be restarted
246 : // after the deletion is complete (done in GetData)
247 0 : bRefreshListener = true;
248 0 : bDataChanged = true;
249 : }
250 : }
251 : }
252 :
253 0 : if ( bDataChanged && HasDataLinks() )
254 0 : SvLinkSource::NotifyDataChanged();
255 0 : }
256 :
257 :
258 :
259 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|