0
|
1 |
/* Copyright (c) 2008 Nokia Corporation
|
|
2 |
*
|
|
3 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
* you may not use this file except in compliance with the License.
|
|
5 |
* You may obtain a copy of the License at
|
|
6 |
*
|
|
7 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
*
|
|
9 |
* Unless required by applicable law or agreed to in writing, software
|
|
10 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
* See the License for the specific language governing permissions and
|
|
13 |
* limitations under the License.
|
|
14 |
*/
|
|
15 |
|
|
16 |
|
|
17 |
#include "scriptextmodule.h"
|
|
18 |
|
|
19 |
using namespace LIW;
|
|
20 |
|
|
21 |
PyObject* SPyValue_FromVariant(TLiwVariant &);
|
|
22 |
PyObject* SPyLiwErr_SetString(char *err_string, int err_no=0);
|
|
23 |
int GetErrorCode(CLiwGenericParamList &aOutParamList);
|
|
24 |
PyObject* GetErrorMessage(CLiwGenericParamList &aOutParamList,
|
|
25 |
bool aDefaultMsg=true);
|
|
26 |
|
|
27 |
CLiwCallback::CLiwCallback()
|
|
28 |
{
|
|
29 |
}
|
|
30 |
|
|
31 |
CLiwCallback* CLiwCallback::NewL()
|
|
32 |
{
|
|
33 |
CLiwCallback *callbackHandle = new(ELeave) CLiwCallback();
|
|
34 |
CleanupStack::PushL(callbackHandle);
|
|
35 |
callbackHandle->ConstructL();
|
|
36 |
CleanupStack::Pop(callbackHandle);
|
|
37 |
return callbackHandle;
|
|
38 |
}
|
|
39 |
|
|
40 |
void CLiwCallback::ConstructL()
|
|
41 |
{
|
|
42 |
iCallbackRegister = new(ELeave) RHashMap<TInt, PyObject*> ();
|
|
43 |
}
|
|
44 |
|
|
45 |
void CLiwCallback::RegisterCallbackL(TInt aTranId, PyObject *aCallback)
|
|
46 |
{
|
|
47 |
iCallbackRegister->InsertL(aTranId, aCallback);
|
|
48 |
}
|
|
49 |
|
|
50 |
PyObject* CLiwCallback::GetRegisteredCallbackL(TInt aTransId)
|
|
51 |
{
|
|
52 |
return iCallbackRegister->FindL(aTransId);
|
|
53 |
}
|
|
54 |
|
|
55 |
TInt CLiwCallback::UnregisterCallback(TInt aTransId)
|
|
56 |
{
|
|
57 |
return iCallbackRegister->Remove(aTransId);
|
|
58 |
}
|
|
59 |
|
|
60 |
void CLiwCallback::Reset()
|
|
61 |
{
|
|
62 |
PyObject *pyFun;
|
|
63 |
THashMapIter <TInt, PyObject*> mapIter(*iCallbackRegister);
|
|
64 |
|
|
65 |
while (pyFun=(PyObject*)mapIter.NextValue())
|
|
66 |
{
|
|
67 |
Py_DECREF(pyFun);
|
|
68 |
mapIter.RemoveCurrent();
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
CLiwCallback::~CLiwCallback()
|
|
73 |
{
|
|
74 |
delete iCallbackRegister;
|
|
75 |
}
|
|
76 |
|
|
77 |
TInt CLiwCallback::HandleNotifyL(TInt aTransId, TInt aEventId,
|
|
78 |
CLiwGenericParamList& aEventParamList,
|
|
79 |
const CLiwGenericParamList& /*aInParamList*/)
|
|
80 |
{
|
|
81 |
PyGILState_STATE gstate = PyGILState_Ensure();
|
|
82 |
|
|
83 |
PyObject *callbackArglist = NULL, *callRet = NULL;
|
|
84 |
PyObject *retvalWrapper = NULL;
|
|
85 |
TInt err = KErrNone;
|
|
86 |
|
|
87 |
PyObject *retDict = PyDict_New();
|
|
88 |
if (!retDict)
|
|
89 |
{
|
|
90 |
PyErr_NoMemory();
|
|
91 |
PyErr_Print();
|
|
92 |
PyGILState_Release(gstate);
|
|
93 |
return -1;
|
|
94 |
}
|
|
95 |
|
|
96 |
err = GetErrorCode(aEventParamList);
|
|
97 |
PyDict_SetItemString(retDict, ERROR_CODE, Py_BuildValue("i", err));
|
|
98 |
|
|
99 |
if (err != SErrNone)
|
|
100 |
{
|
|
101 |
PyObject *errMsg;
|
|
102 |
if (errMsg = GetErrorMessage(aEventParamList, false))
|
|
103 |
PyDict_SetItemString(retDict, ERROR_MSG, errMsg);
|
|
104 |
}
|
|
105 |
|
|
106 |
PyObject *callbackFun = NULL;
|
|
107 |
TInt leaveCode = KErrNone;
|
|
108 |
TRAP(leaveCode, callbackFun = GetRegisteredCallbackL(aTransId));
|
|
109 |
if (leaveCode != KErrNone)
|
|
110 |
{
|
|
111 |
Py_DECREF(retDict);
|
|
112 |
SPyLiwErr_SetString("error fetching the callback function", leaveCode);
|
|
113 |
PyErr_Print();
|
|
114 |
PyGILState_Release(gstate);
|
|
115 |
return leaveCode;
|
|
116 |
}
|
|
117 |
UnregisterCallback(aTransId);
|
|
118 |
|
|
119 |
TInt pos = 0 ;
|
|
120 |
const TLiwGenericParam* retValue = \
|
|
121 |
aEventParamList.FindFirst(pos, KReturnValue);
|
|
122 |
if (pos != KErrNotFound)
|
|
123 |
{
|
|
124 |
TLiwVariant retVal = retValue->Value();
|
|
125 |
retvalWrapper = SPyValue_FromVariant(retVal);
|
|
126 |
retVal.Reset();
|
|
127 |
PyDict_SetItemString(retDict, RETURN_VAL, retvalWrapper);
|
|
128 |
}
|
|
129 |
|
|
130 |
callbackArglist = Py_BuildValue("(iiO)", aTransId, aEventId, retDict);
|
|
131 |
if (!callbackArglist)
|
|
132 |
{
|
|
133 |
Py_DECREF(retDict);
|
|
134 |
Py_DECREF(callbackFun);
|
|
135 |
SPyLiwErr_SetString("error creating argument list for callback");
|
|
136 |
PyErr_Print();
|
|
137 |
PyGILState_Release(gstate);
|
|
138 |
return -1;
|
|
139 |
}
|
|
140 |
|
|
141 |
callRet = PyEval_CallObject(callbackFun, callbackArglist);
|
|
142 |
Py_DECREF(callbackArglist);
|
|
143 |
Py_DECREF(callbackFun);
|
|
144 |
|
|
145 |
if (!callRet)
|
|
146 |
{
|
|
147 |
if (PyErr_Occurred() == PyExc_OSError)
|
|
148 |
{
|
|
149 |
PyObject *type, *value, *traceback;
|
|
150 |
PyErr_Fetch(&type, &value, &traceback);
|
|
151 |
if (PyInt_Check(value))
|
|
152 |
err = PyInt_AS_LONG(value);
|
|
153 |
Py_XDECREF(type);
|
|
154 |
Py_XDECREF(value);
|
|
155 |
Py_XDECREF(traceback);
|
|
156 |
}
|
|
157 |
else
|
|
158 |
{
|
|
159 |
PyErr_Print();
|
|
160 |
err = -1;
|
|
161 |
}
|
|
162 |
}
|
|
163 |
Py_XDECREF(callRet);
|
|
164 |
PyGILState_Release(gstate);
|
|
165 |
|
|
166 |
return err;
|
|
167 |
}
|