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 <sfx2/objsh.hxx>
21 :
22 : #include "adiasync.hxx"
23 : #include "brdcst.hxx"
24 : #include "global.hxx"
25 : #include "document.hxx"
26 : #include "sc.hrc"
27 : #include <osl/thread.h>
28 :
29 76 : ScAddInAsyncs theAddInAsyncTbl;
30 76 : static ScAddInAsync aSeekObj;
31 :
32 : extern "C" {
33 0 : void CALLTYPE ScAddInAsyncCallBack( double& nHandle, void* pData )
34 : {
35 0 : ScAddInAsync::CallBack( sal_uLong( nHandle ), pData );
36 0 : }
37 : }
38 :
39 76 : ScAddInAsync::ScAddInAsync() :
40 : SvtBroadcaster(),
41 : pDocs( NULL ),
42 : mpFuncData( NULL ),
43 : nHandle( 0 ),
44 : meType( ParamType::NONE ),
45 76 : bValid( false )
46 : { // nur fuer aSeekObj !
47 76 : }
48 :
49 0 : ScAddInAsync::ScAddInAsync(sal_uLong nHandleP, FuncData* pFuncData, ScDocument* pDoc) :
50 : SvtBroadcaster(),
51 : pStr( NULL ),
52 : mpFuncData(pFuncData),
53 : nHandle( nHandleP ),
54 0 : meType(pFuncData->GetAsyncType()),
55 0 : bValid( false )
56 : {
57 0 : pDocs = new ScAddInDocs();
58 0 : pDocs->insert( pDoc );
59 0 : theAddInAsyncTbl.insert( this );
60 0 : }
61 :
62 152 : ScAddInAsync::~ScAddInAsync()
63 : {
64 : // aSeekObj does not have that, handle 0 does not exist otherwise
65 76 : if ( nHandle )
66 : {
67 : // in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
68 0 : mpFuncData->Unadvice( (double)nHandle );
69 0 : if ( meType == ParamType::PTR_STRING && pStr ) // include type comparison because of union
70 0 : delete pStr;
71 0 : delete pDocs;
72 : }
73 76 : }
74 :
75 0 : ScAddInAsync* ScAddInAsync::Get( sal_uLong nHandleP )
76 : {
77 0 : ScAddInAsync* pRet = 0;
78 0 : aSeekObj.nHandle = nHandleP;
79 0 : ScAddInAsyncs::iterator it = theAddInAsyncTbl.find( &aSeekObj );
80 0 : if ( it != theAddInAsyncTbl.end() )
81 0 : pRet = *it;
82 0 : aSeekObj.nHandle = 0;
83 0 : return pRet;
84 : }
85 :
86 0 : void ScAddInAsync::CallBack( sal_uLong nHandleP, void* pData )
87 : {
88 : ScAddInAsync* p;
89 0 : if ( (p = Get( nHandleP )) == NULL )
90 0 : return;
91 :
92 0 : if ( !p->HasListeners() )
93 : {
94 : // not in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
95 0 : theAddInAsyncTbl.erase( p );
96 0 : delete p;
97 0 : return ;
98 : }
99 0 : switch ( p->meType )
100 : {
101 : case ParamType::PTR_DOUBLE :
102 0 : p->nVal = *(double*)pData;
103 0 : break;
104 : case ParamType::PTR_STRING :
105 : {
106 0 : sal_Char* pChar = (sal_Char*)pData;
107 0 : if ( p->pStr )
108 0 : *p->pStr = OUString( pChar, strlen(pChar),osl_getThreadTextEncoding() );
109 : else
110 0 : p->pStr = new OUString( pChar, strlen(pChar), osl_getThreadTextEncoding() );
111 0 : break;
112 : }
113 : default :
114 : OSL_FAIL( "unknown AsyncType" );
115 0 : return;
116 : }
117 0 : p->bValid = true;
118 0 : p->Broadcast( ScHint(SC_HINT_DATACHANGED, ScAddress()) );
119 :
120 0 : for ( ScAddInDocs::iterator it = p->pDocs->begin(); it != p->pDocs->end(); ++it )
121 : {
122 0 : ScDocument* pDoc = *it;
123 0 : pDoc->TrackFormulas();
124 0 : pDoc->GetDocumentShell()->Broadcast( SfxSimpleHint( FID_DATACHANGED ) );
125 : }
126 : }
127 :
128 4192 : void ScAddInAsync::RemoveDocument( ScDocument* pDocumentP )
129 : {
130 4192 : if ( !theAddInAsyncTbl.empty() )
131 : {
132 0 : for( ScAddInAsyncs::reverse_iterator iter1 = theAddInAsyncTbl.rbegin(); iter1 != theAddInAsyncTbl.rend(); ++iter1 )
133 : { // backwards because of pointer-movement in array
134 0 : ScAddInAsync* pAsync = *iter1;
135 0 : ScAddInDocs* p = pAsync->pDocs;
136 0 : ScAddInDocs::iterator iter2 = p->find( pDocumentP );
137 0 : if( iter2 != p->end() )
138 : {
139 0 : p->erase( iter2 );
140 0 : if ( p->empty() )
141 : { // this AddIn is not used anymore
142 0 : theAddInAsyncTbl.erase( --(iter1.base()) );
143 0 : delete pAsync;
144 : }
145 : }
146 : }
147 : }
148 4420 : }
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|