Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / docshell / servobj.cxx
blob71d1368c3872435162dd50c7f0a6605b8967a501
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 static bool lcl_FillRangeFromName( ScRange& rRange, ScDocShell* pDocSh, const OUString& rName )
34 if (pDocSh)
36 ScDocument* pDoc = pDocSh->GetDocument();
37 ScRangeName* pNames = pDoc->GetRangeName();
38 if (pNames)
40 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(rName));
41 if (pData)
43 if ( pData->IsValidReference( rRange ) )
44 return true;
48 return false;
51 ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
52 ScServerObject* pObjP)
53 : pObj(pObjP)
57 ScServerObjectSvtListenerForwarder::~ScServerObjectSvtListenerForwarder()
59 //! do NOT access pObj
62 void ScServerObjectSvtListenerForwarder::Notify( const SfxHint& rHint )
64 pObj->Notify( aBroadcaster, rHint);
67 ScServerObject::ScServerObject( ScDocShell* pShell, const OUString& rItem ) :
68 aForwarder( this ),
69 pDocSh( pShell ),
70 bRefreshListener( false )
72 // parse item string
74 if ( lcl_FillRangeFromName( aRange, pDocSh, rItem ) )
76 aItemStr = rItem; // must be parsed again on ref update
78 else
80 // parse ref
81 ScDocument* pDoc = pDocSh->GetDocument();
82 SCTAB nTab = pDocSh->GetCurTab();
83 aRange.aStart.SetTab( nTab );
85 // For DDE link, we always must parse references using OOO A1 convention.
87 if ( aRange.Parse( rItem, pDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
89 // area reference
91 else if ( aRange.aStart.Parse( rItem, pDoc, FormulaGrammar::CONV_OOO ) & SCA_VALID )
93 // cell reference
94 aRange.aEnd = aRange.aStart;
96 else
98 OSL_FAIL("ScServerObject: invalid item");
102 pDocSh->GetDocument()->GetLinkManager()->InsertServer( this );
103 pDocSh->GetDocument()->StartListeningArea( aRange, &aForwarder );
105 StartListening(*pDocSh); // um mitzubekommen, wenn die DocShell geloescht wird
106 StartListening(*SFX_APP()); // for SC_HINT_AREAS_CHANGED
109 ScServerObject::~ScServerObject()
111 Clear();
114 void ScServerObject::Clear()
116 if (pDocSh)
118 ScDocShell* pTemp = pDocSh;
119 pDocSh = NULL;
121 pTemp->GetDocument()->EndListeningArea( aRange, &aForwarder );
122 pTemp->GetDocument()->GetLinkManager()->RemoveServer( this );
123 EndListening(*pTemp);
124 EndListening(*SFX_APP());
128 void ScServerObject::EndListeningAll()
130 aForwarder.EndListeningAll();
131 SfxListener::EndListeningAll();
134 bool ScServerObject::GetData(
135 ::com::sun::star::uno::Any & rData /*out param*/,
136 const OUString & rMimeType, bool /* bSynchron */ )
138 if (!pDocSh)
139 return false;
141 // named ranges may have changed -> update aRange
142 if ( !aItemStr.isEmpty() )
144 ScRange aNew;
145 if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
147 aRange = aNew;
148 bRefreshListener = true;
152 if ( bRefreshListener )
154 // refresh the listeners now (this is called from a timer)
156 EndListeningAll();
157 pDocSh->GetDocument()->StartListeningArea( aRange, &aForwarder );
158 StartListening(*pDocSh);
159 StartListening(*SFX_APP());
160 bRefreshListener = false;
163 OUString aDdeTextFmt = pDocSh->GetDdeTextFmt();
164 ScDocument* pDoc = pDocSh->GetDocument();
166 if( FORMAT_STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ))
168 ScImportExport aObj( pDoc, aRange );
169 if( aDdeTextFmt[0] == 'F' )
170 aObj.SetFormulas( true );
171 if( aDdeTextFmt.equalsAscii( "SYLK" ) ||
172 aDdeTextFmt.equalsAscii( "FSYLK" ) )
174 OString aByteData;
175 if( aObj.ExportByteString( aByteData, osl_getThreadTextEncoding(), SOT_FORMATSTR_ID_SYLK ) )
177 rData <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
178 (const sal_Int8*)aByteData.getStr(),
179 aByteData.getLength() + 1 );
180 return true;
182 return false;
184 if( aDdeTextFmt.equalsAscii( "CSV" ) ||
185 aDdeTextFmt.equalsAscii( "FCSV" ) )
186 aObj.SetSeparator( ',' );
187 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
188 return aObj.ExportData( rMimeType, rData );
191 ScImportExport aObj( pDoc, aRange );
192 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
193 if( aObj.IsRef() )
194 return aObj.ExportData( rMimeType, rData );
195 return false;
198 void ScServerObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
200 bool bDataChanged = false;
202 // DocShell can't be tested via type info, because SFX_HINT_DYING comes from the dtor
203 if ( &rBC == pDocSh )
205 // from DocShell, only SFX_HINT_DYING is interesting
206 if ( rHint.ISA(SfxSimpleHint) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
208 pDocSh = NULL;
209 EndListening(*SFX_APP());
210 // don't access DocShell anymore for EndListening etc.
213 else if (rBC.ISA(SfxApplication))
215 if ( !aItemStr.isEmpty() && rHint.ISA(SfxSimpleHint) &&
216 ((const SfxSimpleHint&)rHint).GetId() == SC_HINT_AREAS_CHANGED )
218 // check if named range was modified
219 ScRange aNew;
220 if ( lcl_FillRangeFromName( aNew, pDocSh, aItemStr ) && aNew != aRange )
221 bDataChanged = true;
224 else
226 // must be from Area broadcasters
228 const ScHint* pScHint = PTR_CAST( ScHint, &rHint );
229 if (pScHint && (pScHint->GetId() & SC_HINT_DATACHANGED))
230 bDataChanged = true;
231 else if (rHint.ISA(ScAreaChangedHint)) // position of broadcaster changed
233 ScRange aNewRange = ((const ScAreaChangedHint&)rHint).GetRange();
234 if ( aRange != aNewRange )
236 bRefreshListener = true;
237 bDataChanged = true;
240 else if (rHint.ISA(SfxSimpleHint))
242 sal_uLong nId = ((const SfxSimpleHint&)rHint).GetId();
243 if (nId == SFX_HINT_DYING)
245 // If the range is being deleted, listening must be restarted
246 // after the deletion is complete (done in GetData)
247 bRefreshListener = true;
248 bDataChanged = true;
253 if ( bDataChanged && HasDataLinks() )
254 SvLinkSource::NotifyDataChanged();
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */