1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
27 #include "rangenam.hxx"
30 using namespace formula
;
32 static bool lcl_FillRangeFromName( ScRange
& rRange
, ScDocShell
* pDocSh
, const OUString
& rName
)
36 ScDocument
* pDoc
= pDocSh
->GetDocument();
37 ScRangeName
* pNames
= pDoc
->GetRangeName();
40 const ScRangeData
* pData
= pNames
->findByUpperName(ScGlobal::pCharClass
->uppercase(rName
));
43 if ( pData
->IsValidReference( rRange
) )
51 ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
52 ScServerObject
* 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
) :
70 bRefreshListener( false )
74 if ( lcl_FillRangeFromName( aRange
, pDocSh
, rItem
) )
76 aItemStr
= rItem
; // must be parsed again on ref update
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
)
91 else if ( aRange
.aStart
.Parse( rItem
, pDoc
, FormulaGrammar::CONV_OOO
) & SCA_VALID
)
94 aRange
.aEnd
= aRange
.aStart
;
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()
114 void ScServerObject::Clear()
118 ScDocShell
* pTemp
= pDocSh
;
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 */ )
141 // named ranges may have changed -> update aRange
142 if ( !aItemStr
.isEmpty() )
145 if ( lcl_FillRangeFromName( aNew
, pDocSh
, aItemStr
) && aNew
!= aRange
)
148 bRefreshListener
= true;
152 if ( bRefreshListener
)
154 // refresh the listeners now (this is called from a timer)
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" ) )
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 );
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 ) );
194 return aObj
.ExportData( rMimeType
, rData
);
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
)
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
220 if ( lcl_FillRangeFromName( aNew
, pDocSh
, aItemStr
) && aNew
!= aRange
)
226 // must be from Area broadcasters
228 const ScHint
* pScHint
= PTR_CAST( ScHint
, &rHint
);
229 if (pScHint
&& (pScHint
->GetId() & SC_HINT_DATACHANGED
))
231 else if (rHint
.ISA(ScAreaChangedHint
)) // position of broadcaster changed
233 ScRange aNewRange
= ((const ScAreaChangedHint
&)rHint
).GetRange();
234 if ( aRange
!= aNewRange
)
236 bRefreshListener
= 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;
253 if ( bDataChanged
&& HasDataLinks() )
254 SvLinkSource::NotifyDataChanged();
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */