00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "opensync.h"
00022 #include "opensync_internals.h"
00023
00024 #include "opensync-data.h"
00025
00026 #include "opensync-context.h"
00027 #include "opensync_context_private.h"
00028
00029 OSyncContext *osync_context_new(OSyncError **error)
00030 {
00031 OSyncContext *ctx = osync_try_malloc0(sizeof(OSyncContext), error);
00032 if (!ctx)
00033 return NULL;
00034
00035 ctx->ref_count = 1;
00036
00037 return ctx;
00038 }
00039
00040 OSyncContext *osync_context_ref(OSyncContext *context)
00041 {
00042 osync_assert(context);
00043
00044 g_atomic_int_inc(&(context->ref_count));
00045
00046 return context;
00047 }
00048
00049 void osync_context_unref(OSyncContext *context)
00050 {
00051 osync_assert(context);
00052
00053 if (g_atomic_int_dec_and_test(&(context->ref_count))) {
00054 osync_free(context);
00055 }
00056 }
00057
00058 void osync_context_set_callback(OSyncContext *context, OSyncContextCallbackFn callback, void *userdata)
00059 {
00060 osync_assert(context);
00061 context->callback_function = callback;
00062 context->callback_data = userdata;
00063 }
00064
00065 void osync_context_set_changes_callback(OSyncContext *context, OSyncContextChangeFn changes)
00066 {
00067 osync_assert(context);
00068 context->changes_function = changes;
00069 }
00070
00071 void osync_context_set_warning_callback(OSyncContext *context, OSyncContextCallbackFn warning)
00072 {
00073 osync_assert(context);
00074 context->warning_function = warning;
00075 }
00076
00077 void osync_context_report_osyncerror(OSyncContext *context, OSyncError *error)
00078 {
00079 osync_trace(TRACE_ENTRY, "%s(%p, %p:(%s))", __func__, context, error, osync_error_print(&error));
00080 osync_assert(context);
00081
00082 if (context->callback_function)
00083 (context->callback_function)(context->callback_data, error);
00084
00085 osync_trace(TRACE_EXIT, "%s", __func__);
00086 }
00087
00088 void osync_context_report_osyncwarning(OSyncContext *context, OSyncError *error)
00089 {
00090 osync_trace(TRACE_ENTRY, "%s(%p, %p:(%s))", __func__, context, error, osync_error_print(&error));
00091 osync_assert(context);
00092
00093 if (context->warning_function)
00094 (context->warning_function)(context->callback_data, error);
00095
00096 osync_trace(TRACE_EXIT, "%s", __func__);
00097 }
00098
00099 void osync_context_report_error(OSyncContext *context, OSyncErrorType type, const char *format, ...)
00100 {
00101 OSyncError *error = NULL;
00102 va_list args;
00103 osync_trace(TRACE_ENTRY, "%s(%p, %i, %s)", __func__, context, type, format);
00104 osync_assert(context);
00105
00106 va_start(args, format);
00107 osync_error_set_vargs(&error, type, format, args);
00108 osync_trace(TRACE_INTERNAL, "ERROR is: %s", osync_error_print(&error));
00109 va_end (args);
00110
00111 if (context->callback_function)
00112 (context->callback_function)(context->callback_data, error);
00113
00114 osync_error_unref(&error);
00115
00116 osync_trace(TRACE_EXIT, "%s", __func__);
00117 }
00118
00119 void osync_context_report_success(OSyncContext *context)
00120 {
00121 osync_trace(TRACE_ENTRY, "%s(%p)", __func__, context);
00122 osync_assert(context);
00123
00124 if (context->callback_function)
00125 (context->callback_function)(context->callback_data, NULL);
00126
00127 osync_trace(TRACE_EXIT, "%s", __func__);
00128 }
00129
00130 void osync_context_report_change(OSyncContext *context, OSyncChange *change)
00131 {
00132 OSyncData *data;
00133 osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, context, change);
00134 osync_assert(context);
00135 osync_assert(change);
00136
00137 osync_assert_msg(osync_change_get_uid(change), "You forgot to set a uid on the change you reported!");
00138 osync_assert_msg(osync_change_get_data(change) || osync_change_get_changetype(change) == OSYNC_CHANGE_TYPE_DELETED, "You need to report some data unless you report CHANGE_DELETED");
00139
00140 data = osync_change_get_data(change);
00141
00142 osync_assert_msg(((data && osync_data_get_objformat(data) != NULL) || osync_change_get_changetype(change) == OSYNC_CHANGE_TYPE_DELETED), "The reported change did not have a format set");
00143 osync_assert_msg(((data && osync_data_get_objtype(data) != NULL) || osync_change_get_changetype(change) == OSYNC_CHANGE_TYPE_DELETED), "The reported change did not have a objtype set");
00144
00145 osync_trace(TRACE_INTERNAL, "Reporting change with uid %s, changetype %i, data %p", osync_change_get_uid(change), osync_change_get_changetype(change), osync_change_get_data(change));
00146
00147 osync_assert_msg(context->changes_function, "The engine must set a callback to receive changes");
00148
00149 context->changes_function(change, context->callback_data);
00150
00151 osync_trace(TRACE_EXIT, "%s", __func__);
00152 }