Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / docshell / servobj.cxx
bloba82c7191a663a6df6f3da16f35e244ae11503edd
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 .
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"
30 using namespace formula;
32 // -----------------------------------------------------------------------
34 static sal_Bool lcl_FillRangeFromName( ScRange& rRange, ScDocShell* pDocSh, const OUString& rName )
36 if (pDocSh)
38 ScDocument* pDoc = pDocSh->GetDocument();
39 ScRangeName* pNames = pDoc->GetRangeName();
40 if (pNames)
42 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(rName));
43 if (pData)
45 if ( pData->IsValidReference( rRange ) )
46 return sal_True;
50 return false;
53 ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
54 ScServerObject* pObjP)
55 : pObj(pObjP)
59 ScServerObjectSvtListenerForwarder::~ScServerObjectSvtListenerForwarder()
61 //! do NOT access pObj
64 void ScServerObjectSvtListenerForwarder::Notify( SvtBroadcaster& /* rBC */, const SfxHint& rHint)
66 pObj->Notify( aBroadcaster, rHint);
69 ScServerObject::ScServerObject( ScDocShell* pShell, const OUString& rItem ) :
70 aForwarder( this ),
71 pDocSh( pShell ),
72 bRefreshListener( false )
74 // parse item string
76 if ( lcl_FillRangeFromName( aRange, pDocSh, rItem ) )
78 aItemStr = rItem; // must be parsed again on ref update
80 else
82 // parse ref
83 ScDocument* pDoc = pDocSh->GetDocument();
84 SCTAB nTab = pDocSh->GetCurTab();
85 aRange.aStart.SetTab( nTab );
87 // For DDE link, we always must parse references using OOO A1 convention.
89 if ( aRange.Parse( rItem, pDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
91 // area reference
93 else if ( aRange.aStart.Parse( rItem, pDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
95 // cell reference
96 aRange.aEnd = aRange.aStart;
98 else
100 OSL_FAIL("ScServerObject: invalid item");
104 pDocSh->GetDocument()->GetLinkManager()->InsertServer( this );
105 pDocSh->GetDocument()->StartListeningArea( aRange, &aForwarder );
107 StartListening(*pDocSh); // um mitzubekommen, wenn die DocShell geloescht wird
108 StartListening(*SFX_APP()); // for SC_HINT_AREAS_CHANGED
111 ScServerObject::~ScServerObject()
113 Clear();
116 void ScServerObject::Clear()
118 if (pDocSh)
120 ScDocShell* pTemp = pDocSh;
121 pDocSh = NULL;
123 pTemp->GetDocument()->EndListeningArea( aRange, &aForwarder );
124 pTemp->GetDocument()->GetLinkManager()->RemoveServer( this );
125 EndListening(*pTemp);
126 EndListening(*SFX_APP());
130 void ScServerObject::EndListeningAll()
132 aForwarder.EndListeningAll();
133 SfxListener::EndListeningAll();
136 sal_Bool ScServerObject::GetData(
137 ::com::sun::star::uno::Any & rData /*out param*/,
138 const OUString & rMimeType, sal_Bool /* bSynchron */ )
140 if (!pDocSh)
141 return false;
143 // named ranges may have changed -> update aRange
144 if ( !aItemStr.isEmpty() )
146 ScRange aNew;
147 if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
149 aRange = aNew;
150 bRefreshListener = sal_True;
154 if ( bRefreshListener )
156 // refresh the listeners now (this is called from a timer)
158 EndListeningAll();
159 pDocSh->GetDocument()->StartListeningArea( aRange, &aForwarder );
160 StartListening(*pDocSh);
161 StartListening(*SFX_APP());
162 bRefreshListener = false;
165 OUString aDdeTextFmt = pDocSh->GetDdeTextFmt();
166 ScDocument* pDoc = pDocSh->GetDocument();
168 if( FORMAT_STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ))
170 ScImportExport aObj( pDoc, aRange );
171 if( aDdeTextFmt[0] == 'F' )
172 aObj.SetFormulas( sal_True );
173 if( aDdeTextFmt.equalsAscii( "SYLK" ) ||
174 aDdeTextFmt.equalsAscii( "FSYLK" ) )
176 OString aByteData;
177 if( aObj.ExportByteString( aByteData, osl_getThreadTextEncoding(), SOT_FORMATSTR_ID_SYLK ) )
179 rData <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
180 (const sal_Int8*)aByteData.getStr(),
181 aByteData.getLength() + 1 );
182 return 1;
184 return 0;
186 if( aDdeTextFmt.equalsAscii( "CSV" ) ||
187 aDdeTextFmt.equalsAscii( "FCSV" ) )
188 aObj.SetSeparator( ',' );
189 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
190 return aObj.ExportData( rMimeType, rData ) ? 1 : 0;
193 ScImportExport aObj( pDoc, aRange );
194 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
195 if( aObj.IsRef() )
196 return aObj.ExportData( rMimeType, rData ) ? 1 : 0;
197 return 0;
200 void ScServerObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
202 sal_Bool bDataChanged = false;
204 // DocShell can't be tested via type info, because SFX_HINT_DYING comes from the dtor
205 if ( &rBC == pDocSh )
207 // from DocShell, only SFX_HINT_DYING is interesting
208 if ( rHint.ISA(SfxSimpleHint) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
210 pDocSh = NULL;
211 EndListening(*SFX_APP());
212 // don't access DocShell anymore for EndListening etc.
215 else if (rBC.ISA(SfxApplication))
217 if ( !aItemStr.isEmpty() && rHint.ISA(SfxSimpleHint) &&
218 ((const SfxSimpleHint&)rHint).GetId() == SC_HINT_AREAS_CHANGED )
220 // check if named range was modified
221 ScRange aNew;
222 if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
223 bDataChanged = sal_True;
226 else
228 // must be from Area broadcasters
230 const ScHint* pScHint = PTR_CAST( ScHint, &rHint );
231 if (pScHint && (pScHint->GetId() & SC_HINT_DATACHANGED))
232 bDataChanged = sal_True;
233 else if (rHint.ISA(ScAreaChangedHint)) // position of broadcaster changed
235 ScRange aNewRange = ((const ScAreaChangedHint&)rHint).GetRange();
236 if ( aRange != aNewRange )
238 bRefreshListener = sal_True;
239 bDataChanged = sal_True;
242 else if (rHint.ISA(SfxSimpleHint))
244 sal_uLong nId = ((const SfxSimpleHint&)rHint).GetId();
245 if (nId == SFX_HINT_DYING)
247 // If the range is being deleted, listening must be restarted
248 // after the deletion is complete (done in GetData)
249 bRefreshListener = sal_True;
250 bDataChanged = sal_True;
255 if ( bDataChanged && HasDataLinks() )
256 SvLinkSource::NotifyDataChanged();
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */