OpenSync  0.22
opensync_convreg.c
1 /*
2  * libopensync - A synchronization framework
3  * Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "opensync.h"
22 #include "opensync_internals.h"
23 
31 
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 OSyncObjFormatTemplate *osync_env_find_format_template(OSyncEnv *env, const char *name)
34 {
35  GList *o;
36  for (o = env->format_templates; o; o = o->next) {
37  OSyncObjFormatTemplate *tmpl = o->data;
38  if (!strcmp(tmpl->name, name))
39  return tmpl;
40  }
41  return NULL;
42 }
43 
44 OSyncObjTypeTemplate *osync_env_find_objtype_template(OSyncEnv *env, const char *name)
45 {
46  GList *o;
47  for (o = env->objtype_templates; o; o = o->next) {
48  OSyncObjTypeTemplate *tmpl = o->data;
49  if (!strcmp(tmpl->name, name))
50  return tmpl;
51  }
52  return NULL;
53 }
54 
55 OSyncDataDetector *osync_env_find_detector(OSyncEnv *env, const char *sourcename, const char *targetname)
56 {
57  GList *o;
58  for (o = env->data_detectors; o; o = o->next) {
59  OSyncDataDetector *tmpl = o->data;
60  if (!strcmp(tmpl->sourceformat, sourcename) && !strcmp(tmpl->targetformat, targetname))
61  return tmpl;
62  }
63  return NULL;
64 }
65 
66 OSyncConverterTemplate *osync_env_find_converter_template(OSyncEnv *env, const char *sourcename, const char *targetname)
67 {
68  GList *o;
69  for (o = env->converter_templates; o; o = o->next) {
70  OSyncConverterTemplate *tmpl = o->data;
71  if (!strcmp(tmpl->source_format, sourcename) && !strcmp(tmpl->target_format, targetname))
72  return tmpl;
73  }
74  return NULL;
75 }
76 #endif
77 
80 void osync_env_register_detector(OSyncEnv *env, const char *sourceformat, const char *format, OSyncFormatDetectDataFunc detect_func)
81 {
82  g_assert(detect_func);
83  OSyncDataDetector *detector = g_malloc0(sizeof(OSyncDataDetector));
84  detector->sourceformat = strdup(sourceformat);
85  detector->targetformat = strdup(format);
86  detector->detect_func = detect_func;
87 
88  //Register the "inverse" detector which of course will always work
89  env->data_detectors = g_list_append(env->data_detectors, detector);
90  detector = g_malloc0(sizeof(OSyncDataDetector));
91  detector->sourceformat = strdup(format);
92  detector->targetformat = strdup(sourceformat);
93  detector->detect_func = NULL;
94 
95  env->data_detectors = g_list_append(env->data_detectors, detector);
96 }
97 
98 void osync_env_register_filter_function(OSyncEnv *env, const char *name, const char *objtype, const char *format, OSyncFilterFunction hook)
99 {
100  OSyncCustomFilter *function = g_malloc0(sizeof(OSyncCustomFilter));
101  function->name = g_strdup(name);
102  function->objtype = g_strdup(objtype);
103  function->format = g_strdup(format);
104  function->hook = hook;
105 
106  env->filter_functions = g_list_append(env->filter_functions, function);
107 }
108 
109 void osync_env_register_objformat(OSyncEnv *env, const char *typename, const char *name)
110 {
111  OSyncObjFormatTemplate *format = NULL;
112  if (!(format = osync_env_find_format_template(env, name))) {
113  format = g_malloc0(sizeof(OSyncObjFormatTemplate));
114  format->name = strdup(name);
115  format->objtype = g_strdup(typename);
116  //We default to malloc style!
117  //format->copy_func = osync_format_malloced_copy;
118  //format->destroy_func = osync_format_malloced_destroy;
119  env->format_templates = g_list_append(env->format_templates, format);
120  }
121 }
122 
123 void osync_env_register_objtype(OSyncEnv *env, const char *name)
124 {
125  OSyncObjTypeTemplate *type = NULL;
126  if (!(type = osync_env_find_objtype_template(env, name))) {
127  type = g_malloc0(sizeof(OSyncObjTypeTemplate));
128  type->name = g_strdup(name);
129  env->objtype_templates = g_list_append(env->objtype_templates, type);
130  }
131 }
132 
133 void osync_env_register_converter(OSyncEnv *env, ConverterType type, const char *sourcename, const char *targetname, OSyncFormatConvertFunc convert_func)
134 {
135  OSyncConverterTemplate *converter = g_malloc0(sizeof(OSyncConverterTemplate));
136 
137  converter->source_format = sourcename;
138  converter->target_format = targetname;
139  converter->convert_func = convert_func;
140  converter->type = type;
141  env->converter_templates = g_list_append(env->converter_templates, converter);
142 }
143 
144 void osync_env_converter_set_init(OSyncEnv *env, const char *sourcename, const char *targetname, OSyncFormatConverterInitFunc init_func, OSyncFormatConverterFinalizeFunc fin_func)
145 {
146  OSyncConverterTemplate *converter = osync_env_find_converter_template(env, sourcename, targetname);
147  osync_assert_msg(converter != NULL, "You need to register the converter first");
148 
149  converter->init_func = init_func;
150  converter->fin_func = fin_func;
151 }
152 
153 void osync_env_register_extension(OSyncEnv *env, const char *from_format, const char *to_format, const char *extension_name, OSyncFormatExtInitFunc init_func)
154 {
155  OSyncFormatExtensionTemplate *ext = g_malloc0(sizeof(OSyncFormatExtensionTemplate));
156  ext->from_formatname = g_strdup(from_format);
157  ext->to_formatname = g_strdup(to_format);
158  ext->name = g_strdup(extension_name);
159  ext->init_func = init_func;
160 
161  env->extension_templates = g_list_append(env->extension_templates, ext);
162 }
163 
164 void osync_env_format_set_demarshall_func(OSyncEnv *env, const char *formatname, OSyncFormatDemarshallFunc demarshall_func)
165 {
166  osync_trace(TRACE_INTERNAL, "osync_env_format_set_demarshall_func(%p, %s, %p)", env, formatname, demarshall_func);
167  g_assert(env);
168  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
169  osync_assert_msg(format, "You need to register the formattype first");
170  format->demarshall_func = demarshall_func;
171 }
172 
173 void osync_env_format_set_marshall_func(OSyncEnv *env, const char *formatname, OSyncFormatMarshallFunc marshall_func)
174 {
175  osync_trace(TRACE_INTERNAL, "osync_env_format_set_marshall_func(%p, %s, %p)", env, formatname, marshall_func);
176  g_assert(env);
177  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
178  osync_assert_msg(format, "You need to register the formattype first");
179  format->marshall_func = marshall_func;
180 }
181 
182 void osync_env_format_set_compare_func(OSyncEnv *env, const char *formatname, OSyncFormatCompareFunc cmp_func)
183 {
184  osync_trace(TRACE_INTERNAL, "osync_env_format_set_compare_func(%p, %s, %p)", env, formatname, cmp_func);
185  g_assert(env);
186  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
187  osync_assert_msg(format, "You need to register the formattype first");
188  format->cmp_func = cmp_func;
189 }
190 
191 void osync_env_format_set_destroy_func(OSyncEnv *env, const char *formatname, OSyncFormatDestroyFunc destroy_func)
192 {
193  g_assert(env);
194  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
195  osync_assert_msg(format, "You need to register the formattype first");
196  format->destroy_func = destroy_func;
197 }
198 
199 void osync_env_format_set_copy_func(OSyncEnv *env, const char *formatname, OSyncFormatCopyFunc copy_func)
200 {
201  g_assert(env);
202  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
203  osync_assert_msg(format, "You need to register the formattype first");
204  format->copy_func = copy_func;
205 }
206 
225 /*void osync_env_format_set_detect_func(OSyncEnv *env, const char *formatname, OSyncFormatDetectFunc detect_func)
226 {
227  g_assert(env);
228  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
229  osync_assert_msg(format, "You need to register the formattype first");
230  format->detect_func = detect_func;
231 }*/
232 
233 void osync_env_format_set_duplicate_func(OSyncEnv *env, const char *formatname, OSyncFormatDuplicateFunc dupe_func)
234 {
235  g_assert(env);
236  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
237  osync_assert_msg(format, "You need to register the formattype first");
238  format->duplicate_func = dupe_func;
239 }
240 
241 void osync_env_format_set_create_func(OSyncEnv *env, const char *formatname, OSyncFormatCreateFunc create_func)
242 {
243  g_assert(env);
244  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
245  osync_assert_msg(format, "You need to register the formattype first");
246  format->create_func = create_func;
247 }
248 
249 void osync_env_format_set_print_func(OSyncEnv *env, const char *formatname, OSyncFormatPrintFunc print_func)
250 {
251  g_assert(env);
252  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
253  osync_assert_msg(format, "You need to register the formattype first");
254  format->print_func = print_func;
255 }
256 
257 void osync_env_format_set_revision_func(OSyncEnv *env, const char *formatname, OSyncFormatRevisionFunc revision_func)
258 {
259  g_assert(env);
260  OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
261  osync_assert_msg(format, "You need to register the formattype first");
262  format->revision_func = revision_func;
263 }
Represents a custom filter that can be used to call hooks.
void osync_trace(OSyncTraceType type, const char *message,...)
Used for tracing the application.
Represent a detector for a given format.