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 5 : static bool lcl_FillRangeFromName( ScRange& rRange, ScDocShell* pDocSh, const OUString& rName )
33 : {
34 5 : if (pDocSh)
35 : {
36 5 : ScDocument& rDoc = pDocSh->GetDocument();
37 5 : ScRangeName* pNames = rDoc.GetRangeName();
38 5 : if (pNames)
39 : {
40 5 : const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(rName));
41 5 : if (pData)
42 : {
43 0 : if ( pData->IsValidReference( rRange ) )
44 0 : return true;
45 : }
46 : }
47 : }
48 5 : return false;
49 : }
50 :
51 5 : ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
52 : ScServerObject* pObjP)
53 5 : : pObj(pObjP)
54 : {
55 5 : }
56 :
57 5 : ScServerObjectSvtListenerForwarder::~ScServerObjectSvtListenerForwarder()
58 : {
59 : //! do NOT access pObj
60 5 : }
61 :
62 0 : void ScServerObjectSvtListenerForwarder::Notify( const SfxHint& rHint )
63 : {
64 0 : pObj->Notify( aBroadcaster, rHint);
65 0 : }
66 :
67 5 : ScServerObject::ScServerObject( ScDocShell* pShell, const OUString& rItem ) :
68 : aForwarder( this ),
69 : pDocSh( pShell ),
70 5 : bRefreshListener( false )
71 : {
72 : // parse item string
73 :
74 5 : 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 5 : ScDocument& rDoc = pDocSh->GetDocument();
82 5 : SCTAB nTab = ScDocShell::GetCurTab();
83 5 : aRange.aStart.SetTab( nTab );
84 :
85 : // For DDE link, we always must parse references using OOO A1 convention.
86 :
87 5 : if ( aRange.Parse( rItem, &rDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
88 : {
89 : // area reference
90 : }
91 5 : else if ( aRange.aStart.Parse( rItem, &rDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
92 : {
93 : // cell reference
94 5 : aRange.aEnd = aRange.aStart;
95 : }
96 : else
97 : {
98 : OSL_FAIL("ScServerObject: invalid item");
99 : }
100 : }
101 :
102 5 : pDocSh->GetDocument().GetLinkManager()->InsertServer( this );
103 5 : pDocSh->GetDocument().StartListeningArea( aRange, false, &aForwarder );
104 :
105 5 : StartListening(*pDocSh); // um mitzubekommen, wenn die DocShell geloescht wird
106 5 : StartListening(*SfxGetpApp()); // for SC_HINT_AREAS_CHANGED
107 5 : }
108 :
109 15 : ScServerObject::~ScServerObject()
110 : {
111 5 : Clear();
112 10 : }
113 :
114 5 : void ScServerObject::Clear()
115 : {
116 5 : if (pDocSh)
117 : {
118 5 : ScDocShell* pTemp = pDocSh;
119 5 : pDocSh = NULL;
120 :
121 5 : pTemp->GetDocument().EndListeningArea(aRange, false, &aForwarder);
122 5 : pTemp->GetDocument().GetLinkManager()->RemoveServer( this );
123 5 : EndListening(*pTemp);
124 5 : EndListening(*SfxGetpApp());
125 : }
126 5 : }
127 :
128 0 : void ScServerObject::EndListeningAll()
129 : {
130 0 : aForwarder.EndListeningAll();
131 0 : SfxListener::EndListeningAll();
132 0 : }
133 :
134 5 : bool ScServerObject::GetData(
135 : ::com::sun::star::uno::Any & rData /*out param*/,
136 : const OUString & rMimeType, bool /* bSynchron */ )
137 : {
138 5 : if (!pDocSh)
139 0 : return false;
140 :
141 : // named ranges may have changed -> update aRange
142 5 : 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 5 : if ( bRefreshListener )
153 : {
154 : // refresh the listeners now (this is called from a timer)
155 :
156 0 : EndListeningAll();
157 0 : pDocSh->GetDocument().StartListeningArea( aRange, false, &aForwarder );
158 0 : StartListening(*pDocSh);
159 0 : StartListening(*SfxGetpApp());
160 0 : bRefreshListener = false;
161 : }
162 :
163 5 : OUString aDdeTextFmt = pDocSh->GetDdeTextFmt();
164 5 : ScDocument& rDoc = pDocSh->GetDocument();
165 :
166 5 : if( SotClipboardFormatId::STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ))
167 : {
168 5 : ScImportExport aObj( &rDoc, aRange );
169 5 : if( aDdeTextFmt[0] == 'F' )
170 0 : aObj.SetFormulas( true );
171 5 : if( aDdeTextFmt == "SYLK" || aDdeTextFmt == "FSYLK" )
172 : {
173 0 : OString aByteData;
174 0 : if( aObj.ExportByteString( aByteData, osl_getThreadTextEncoding(), SotClipboardFormatId::SYLK ) )
175 : {
176 0 : rData <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
177 0 : reinterpret_cast<const sal_Int8*>(aByteData.getStr()),
178 0 : aByteData.getLength() + 1 );
179 0 : return true;
180 : }
181 0 : return false;
182 : }
183 5 : if( aDdeTextFmt == "CSV" || aDdeTextFmt == "FCSV" )
184 0 : aObj.SetSeparator( ',' );
185 5 : aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
186 5 : return aObj.ExportData( rMimeType, rData );
187 : }
188 :
189 0 : ScImportExport aObj( &rDoc, aRange );
190 0 : aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
191 0 : if( aObj.IsRef() )
192 0 : return aObj.ExportData( rMimeType, rData );
193 5 : return false;
194 : }
195 :
196 23 : void ScServerObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
197 : {
198 23 : bool bDataChanged = false;
199 :
200 : // DocShell can't be tested via type info, because SFX_HINT_DYING comes from the dtor
201 23 : if ( &rBC == pDocSh )
202 : {
203 : // from DocShell, only SFX_HINT_DYING is interesting
204 0 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
205 0 : if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
206 : {
207 0 : pDocSh = NULL;
208 0 : EndListening(*SfxGetpApp());
209 : // don't access DocShell anymore for EndListening etc.
210 : }
211 : }
212 23 : else if (rBC.ISA(SfxApplication))
213 : {
214 23 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
215 23 : if ( !aItemStr.isEmpty() && pSimpleHint &&
216 0 : pSimpleHint->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 = dynamic_cast<const ScHint*>( &rHint );
229 0 : if (pScHint && (pScHint->GetId() & SC_HINT_DATACHANGED))
230 0 : bDataChanged = true;
231 0 : else if ( dynamic_cast<const ScAreaChangedHint*>(&rHint) ) // position of broadcaster changed
232 : {
233 0 : ScRange aNewRange = static_cast<const ScAreaChangedHint&>(rHint).GetRange();
234 0 : if ( aRange != aNewRange )
235 : {
236 0 : bRefreshListener = true;
237 0 : bDataChanged = true;
238 0 : }
239 : }
240 0 : else if ( dynamic_cast<const SfxSimpleHint*>(&rHint) )
241 : {
242 0 : sal_uLong nId = static_cast<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 23 : if ( bDataChanged && HasDataLinks() )
254 0 : SvLinkSource::NotifyDataChanged();
255 179 : }
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|