Changed indentation from tabs to whitespace
[opensync.git] / wrapper / opensync-context.i
blob3baf10d520368faea18d28f6a7680465cce6dd9e
1 typedef struct {} Context;
2 %extend Context {
3 /* called by python-module plugin */
4 Context(PyObject *obj) {
5 Context *context = PyCObject_AsVoidPtr(obj);
6 osync_context_ref(context);
7 return context;
10 Context() {
11 Error *err = NULL;
12 Context *context = osync_context_new(&err);
13 if (raise_exception_on_error(err))
14 return NULL;
15 else
16 return context;
19 ~Context() {
20 osync_context_unref(self);
23 void report_error(ErrorType type, const char *msg) {
24 osync_context_report_error(self, type, "%s", msg);
27 void report_success() {
28 osync_context_report_success(self);
31 void report_osyncerror(Error *error) {
32 osync_context_report_osyncerror(self, error);
35 void report_osyncwarning(Error *error) {
36 osync_context_report_osyncwarning(self, error);
39 void report_change(Change *change) {
40 osync_context_report_change(self, change);
44 static void context_callback_wrapper(void *clientdata, OSyncError *err) {
45 PyGILState_STATE pystate = PyGILState_Ensure();
47 PyObject *handler = clientdata;
48 PyObject *errobj = SWIG_NewPointerObj(err, SWIGTYPE_p_Error, 0);
49 PyObject *result = PyObject_CallMethod(handler, "callback", "(O)", errobj);
50 Py_XDECREF(result);
52 /* there's no way to report an error to OpenSync, so we just print it */
53 if (PyErr_Occurred())
54 PyErr_Print();
56 PyGILState_Release(pystate);
59 static void context_changes_wrapper(OSyncChange *change, void *clientdata) {
60 PyGILState_STATE pystate = PyGILState_Ensure();
62 PyObject *handler = clientdata;
63 PyObject *chgobj = SWIG_NewPointerObj(change, SWIGTYPE_p_Change, 0);
64 PyObject *result = PyObject_CallMethod(handler, "changes", "(O)", chgobj);
65 Py_XDECREF(result);
67 /* there's no way to report an error to OpenSync, so we just print it */
68 if (PyErr_Occurred())
69 PyErr_Print();
71 PyGILState_Release(pystate);
74 static void context_warning_wrapper(void *clientdata, OSyncError *err) {
75 PyGILState_STATE pystate = PyGILState_Ensure();
77 PyObject *handler = clientdata;
78 PyObject *errobj = SWIG_NewPointerObj(err, SWIGTYPE_p_Error, 0);
79 PyObject *result = PyObject_CallMethod(handler, "warning", "(O)", errobj);
80 Py_XDECREF(result);
82 /* there's no way to report an error to OpenSync, so we just print it */
83 if (PyErr_Occurred())
84 PyErr_Print();
86 PyGILState_Release(pystate);
90 /* this should be passed an instance of a subclass of the ContextCallbacks class below
91 * FIXME: we'll leak a reference to the handler object if this callback is replaced */
92 void set_callback_object(PyObject *handler) {
93 Py_INCREF(handler);
94 PyEval_InitThreads();
95 osync_context_set_callback(self, context_callback_wrapper, handler);
96 osync_context_set_changes_callback(self, context_changes_wrapper);
97 osync_context_set_warning_callback(self, context_warning_wrapper);
101 %pythoncode %{
102 class ContextCallbacks:
103 """A purely-Python class that should be subclassed by code that wishes to handle context callbacks."""
104 def callback(self, err):
105 """If err is set, this call indicates that an error was reported. Otherwise success."""
106 pass
108 def changes(self, change):
109 """Called to report a change object."""
110 pass
112 def warning(self, err):
113 """Called to report a non-fatal error."""
114 pass