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 <sal/config.h>
22 #include <boost/noncopyable.hpp>
23 #include <vcl/svapp.hxx>
24 #include <vcl/settings.hxx>
25 #include <osl/module.hxx>
26 #include <osl/file.hxx>
27 #include <unotools/transliterationwrapper.hxx>
29 #include "callform.hxx"
31 #include "adiasync.hxx"
35 typedef void (CALLTYPE
* ExFuncPtr1
)(void*);
36 typedef void (CALLTYPE
* ExFuncPtr2
)(void*, void*);
37 typedef void (CALLTYPE
* ExFuncPtr3
)(void*, void*, void*);
38 typedef void (CALLTYPE
* ExFuncPtr4
)(void*, void*, void*, void*);
39 typedef void (CALLTYPE
* ExFuncPtr5
)(void*, void*, void*, void*, void*);
40 typedef void (CALLTYPE
* ExFuncPtr6
)(void*, void*, void*, void*, void*, void*);
41 typedef void (CALLTYPE
* ExFuncPtr7
)(void*, void*, void*, void*, void*, void*, void*);
42 typedef void (CALLTYPE
* ExFuncPtr8
)(void*, void*, void*, void*, void*, void*, void*, void*);
43 typedef void (CALLTYPE
* ExFuncPtr9
)(void*, void*, void*, void*, void*, void*, void*, void*, void*);
44 typedef void (CALLTYPE
* ExFuncPtr10
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
45 typedef void (CALLTYPE
* ExFuncPtr11
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
46 typedef void (CALLTYPE
* ExFuncPtr12
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
47 typedef void (CALLTYPE
* ExFuncPtr13
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
48 typedef void (CALLTYPE
* ExFuncPtr14
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
49 typedef void (CALLTYPE
* ExFuncPtr15
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
50 typedef void (CALLTYPE
* ExFuncPtr16
)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
52 typedef void (CALLTYPE
* GetFuncCountPtr
)(sal_uInt16
& nCount
);
53 typedef void (CALLTYPE
* GetFuncDataPtr
)
54 (sal_uInt16
& nNo
, sal_Char
* pFuncName
, sal_uInt16
& nParamCount
, ParamType
* peType
, sal_Char
* pInternalName
);
56 typedef void (CALLTYPE
* SetLanguagePtr
)( sal_uInt16
& nLanguage
);
57 typedef void (CALLTYPE
* GetParamDesc
)
58 (sal_uInt16
& nNo
, sal_uInt16
& nParam
, sal_Char
* pName
, sal_Char
* pDesc
);
60 typedef void (CALLTYPE
* IsAsync
) ( sal_uInt16
& nNo
,
62 typedef void (CALLTYPE
* Advice
) ( sal_uInt16
& nNo
,
63 AdvData
& pfCallback
);
64 typedef void (CALLTYPE
* Unadvice
)( double& nHandle
);
66 typedef void (CALLTYPE
* FARPROC
) ( void );
70 #ifndef DISABLE_DYNLOADING
71 #define GETFUNCTIONCOUNT "GetFunctionCount"
72 #define GETFUNCTIONDATA "GetFunctionData"
73 #define SETLANGUAGE "SetLanguage"
74 #define GETPARAMDESC "GetParameterDescription"
75 #define ISASYNC "IsAsync"
76 #define ADVICE "Advice"
77 #define UNADVICE "Unadvice"
80 class ModuleData
: private boost::noncopyable
82 friend class ModuleCollection
;
84 osl::Module
* pInstance
;
86 ModuleData(const OUString
& rStr
, osl::Module
* pInst
) : aName(rStr
), pInstance(pInst
) {}
87 ~ModuleData() { delete pInstance
; }
89 const OUString
& GetName() const { return aName
; }
90 osl::Module
* GetInstance() const { return pInstance
; }
93 FuncData::FuncData(const ModuleData
*pModule
,
94 const OUString
& rIName
,
95 const OUString
& rFName
,
98 const ParamType
* peType
,
100 pModuleData (pModule
),
101 aInternalName (rIName
),
104 nParamCount (nCount
),
107 for (sal_uInt16 i
= 0; i
< MAXFUNCPARAM
; i
++)
108 eParamType
[i
] = peType
[i
];
111 FuncData::FuncData(const FuncData
& rData
) :
112 pModuleData (rData
.pModuleData
),
113 aInternalName (rData
.aInternalName
),
114 aFuncName (rData
.aFuncName
),
115 nNumber (rData
.nNumber
),
116 nParamCount (rData
.nParamCount
),
117 eAsyncType (rData
.eAsyncType
)
119 for (sal_uInt16 i
= 0; i
< MAXFUNCPARAM
; i
++)
120 eParamType
[i
] = rData
.eParamType
[i
];
125 class ModuleCollection
127 typedef boost::ptr_map
<OUString
, ModuleData
> MapType
;
130 ModuleCollection() {}
132 const ModuleData
* findByName(const OUString
& rName
) const;
133 void insert(ModuleData
* pNew
);
137 const ModuleData
* ModuleCollection::findByName(const OUString
& rName
) const
139 MapType::const_iterator it
= maData
.find(rName
);
140 return it
== maData
.end() ? NULL
: it
->second
;
143 void ModuleCollection::insert(ModuleData
* pNew
)
148 OUString aName
= pNew
->GetName();
149 maData
.insert(aName
, pNew
);
152 void ModuleCollection::clear()
157 ModuleCollection aModuleCollection
;
161 bool InitExternalFunc(const OUString
& rModuleName
)
163 #ifdef DISABLE_DYNLOADING
167 // Module already loaded?
168 const ModuleData
* pTemp
= aModuleCollection
.findByName(rModuleName
);
176 osl::Module
* pLib
= new osl::Module( aNP
);
179 FARPROC fpGetCount
= (FARPROC
)pLib
->getFunctionSymbol(GETFUNCTIONCOUNT
);
180 FARPROC fpGetData
= (FARPROC
)pLib
->getFunctionSymbol(GETFUNCTIONDATA
);
181 if ((fpGetCount
!= NULL
) && (fpGetData
!= NULL
))
183 FARPROC fpIsAsync
= (FARPROC
)pLib
->getFunctionSymbol(ISASYNC
);
184 FARPROC fpAdvice
= (FARPROC
)pLib
->getFunctionSymbol(ADVICE
);
185 FARPROC fpSetLanguage
= (FARPROC
)pLib
->getFunctionSymbol(SETLANGUAGE
);
188 LanguageType eLanguage
= Application::GetSettings().GetUILanguageTag().getLanguageType();
189 sal_uInt16 nLanguage
= (sal_uInt16
) eLanguage
;
190 (*reinterpret_cast<SetLanguagePtr
>(fpSetLanguage
))( nLanguage
);
193 // Module in die Collection aufnehmen
194 ModuleData
* pModuleData
= new ModuleData(rModuleName
, pLib
);
195 aModuleCollection
.insert(pModuleData
);
197 // Schnittstelle initialisieren
198 AdvData pfCallBack
= &ScAddInAsyncCallBack
;
199 FuncCollection
* pFuncCol
= ScGlobal::GetFuncCollection();
201 (*reinterpret_cast<GetFuncCountPtr
>(fpGetCount
))(nCount
);
202 for (sal_uInt16 i
=0; i
< nCount
; i
++)
204 sal_Char cFuncName
[256];
205 sal_Char cInternalName
[256];
206 sal_uInt16 nParamCount
;
207 ParamType eParamType
[MAXFUNCPARAM
];
208 ParamType eAsyncType
= ParamType::NONE
;
209 // initialize all, in case the AddIn behaves bad
211 cInternalName
[0] = 0;
213 for ( sal_uInt16 j
=0; j
<MAXFUNCPARAM
; j
++ )
215 eParamType
[j
] = ParamType::NONE
;
217 (*reinterpret_cast<GetFuncDataPtr
>(fpGetData
))(i
, cFuncName
, nParamCount
,
218 eParamType
, cInternalName
);
221 (*reinterpret_cast<IsAsync
>(fpIsAsync
))(i
, &eAsyncType
);
222 if ( fpAdvice
&& eAsyncType
!= ParamType::NONE
)
223 (*reinterpret_cast<Advice
>(fpAdvice
))( i
, pfCallBack
);
225 OUString
aInternalName( cInternalName
, strlen(cInternalName
), osl_getThreadTextEncoding() );
226 OUString
aFuncName( cFuncName
, strlen(cFuncName
), osl_getThreadTextEncoding() );
227 FuncData
* pFuncData
= new FuncData( pModuleData
,
234 pFuncCol
->insert(pFuncData
);
247 void ExitExternalFunc()
249 aModuleCollection
.clear();
252 bool FuncData::Call(void** ppParam
) const
254 #ifdef DISABLE_DYNLOADING
259 osl::Module
* pLib
= pModuleData
->GetInstance();
260 FARPROC fProc
= (FARPROC
)pLib
->getFunctionSymbol(aFuncName
);
266 (*reinterpret_cast<ExFuncPtr1
>(fProc
))(ppParam
[0]);
270 (*reinterpret_cast<ExFuncPtr2
>(fProc
))(ppParam
[0], ppParam
[1]);
274 (*reinterpret_cast<ExFuncPtr3
>(fProc
))(ppParam
[0], ppParam
[1], ppParam
[2]);
278 (*reinterpret_cast<ExFuncPtr4
>(fProc
))(ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3]);
282 (*reinterpret_cast<ExFuncPtr5
>(fProc
))(ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4]);
286 (*reinterpret_cast<ExFuncPtr6
>(fProc
))(ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5]);
290 (*reinterpret_cast<ExFuncPtr7
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
295 (*reinterpret_cast<ExFuncPtr8
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
296 ppParam
[6], ppParam
[7]);
300 (*reinterpret_cast<ExFuncPtr9
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
301 ppParam
[6], ppParam
[7], ppParam
[8]);
305 (*reinterpret_cast<ExFuncPtr10
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
306 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9]);
310 (*reinterpret_cast<ExFuncPtr11
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
311 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9], ppParam
[10]);
315 (*reinterpret_cast<ExFuncPtr12
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
316 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9], ppParam
[10], ppParam
[11]);
320 (*reinterpret_cast<ExFuncPtr13
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
321 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9], ppParam
[10], ppParam
[11],
326 (*reinterpret_cast<ExFuncPtr14
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
327 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9], ppParam
[10], ppParam
[11],
328 ppParam
[12], ppParam
[13]);
332 (*reinterpret_cast<ExFuncPtr15
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
333 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9], ppParam
[10], ppParam
[11],
334 ppParam
[12], ppParam
[13], ppParam
[14]);
338 (*reinterpret_cast<ExFuncPtr16
>(fProc
))( ppParam
[0], ppParam
[1], ppParam
[2], ppParam
[3], ppParam
[4], ppParam
[5],
339 ppParam
[6], ppParam
[7], ppParam
[8], ppParam
[9], ppParam
[10], ppParam
[11],
340 ppParam
[12], ppParam
[13], ppParam
[14], ppParam
[15]);
350 bool FuncData::Unadvice( double nHandle
)
352 #ifdef DISABLE_DYNLOADING
357 osl::Module
* pLib
= pModuleData
->GetInstance();
358 FARPROC fProc
= (FARPROC
)pLib
->getFunctionSymbol(UNADVICE
);
361 reinterpret_cast< ::Unadvice
>(fProc
)(nHandle
);
368 const OUString
& FuncData::GetModuleName() const
370 return pModuleData
->GetName();
373 bool FuncData::getParamDesc( OUString
& aName
, OUString
& aDesc
, sal_uInt16 nParam
) const
375 #ifdef DISABLE_DYNLOADING
382 if ( nParam
<= nParamCount
)
384 osl::Module
* pLib
= pModuleData
->GetInstance();
385 FARPROC fProc
= (FARPROC
) pLib
->getFunctionSymbol(GETPARAMDESC
);
388 sal_Char pcName
[256];
389 sal_Char pcDesc
[256];
390 *pcName
= *pcDesc
= 0;
391 sal_uInt16 nFuncNo
= nNumber
; // nicht per Reference versauen lassen..
392 reinterpret_cast< ::GetParamDesc
>(fProc
)( nFuncNo
, nParam
, pcName
, pcDesc
);
393 aName
= OUString( pcName
, 256, osl_getThreadTextEncoding() );
394 aDesc
= OUString( pcDesc
, 256, osl_getThreadTextEncoding() );
407 FuncCollection::FuncCollection() {}
408 FuncCollection::FuncCollection(const FuncCollection
& r
) : maData(r
.maData
) {}
410 const FuncData
* FuncCollection::findByName(const OUString
& rName
) const
412 MapType::const_iterator it
= maData
.find(rName
);
413 return it
== maData
.end() ? NULL
: it
->second
;
416 FuncData
* FuncCollection::findByName(const OUString
& rName
)
418 MapType::iterator it
= maData
.find(rName
);
419 return it
== maData
.end() ? NULL
: it
->second
;
422 void FuncCollection::insert(FuncData
* pNew
)
424 OUString aName
= pNew
->GetInternalName();
425 maData
.insert(aName
, pNew
);
428 FuncCollection::const_iterator
FuncCollection::begin() const
430 return maData
.begin();
433 FuncCollection::const_iterator
FuncCollection::end() const
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */