OpenSync 0.22
opensync/opensync_convreg.c
00001 /*
00002  * libopensync - A synchronization framework
00003  * Copyright (C) 2004-2005  Armin Bauer <armin.bauer@opensync.org>
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00018  * 
00019  */
00020  
00021 #include "opensync.h"
00022 #include "opensync_internals.h"
00023 
00031 
00032 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00033 OSyncObjFormatTemplate *osync_env_find_format_template(OSyncEnv *env, const char *name)
00034 {
00035         GList *o;
00036         for (o = env->format_templates; o; o = o->next) {
00037                 OSyncObjFormatTemplate *tmpl = o->data;
00038                 if (!strcmp(tmpl->name, name))
00039                         return tmpl;
00040         }
00041         return NULL;
00042 }
00043 
00044 OSyncObjTypeTemplate *osync_env_find_objtype_template(OSyncEnv *env, const char *name)
00045 {
00046         GList *o;
00047         for (o = env->objtype_templates; o; o = o->next) {
00048                 OSyncObjTypeTemplate *tmpl = o->data;
00049                 if (!strcmp(tmpl->name, name))
00050                         return tmpl;
00051         }
00052         return NULL;
00053 }
00054 
00055 OSyncDataDetector *osync_env_find_detector(OSyncEnv *env, const char *sourcename, const char *targetname)
00056 {
00057         GList *o;
00058         for (o = env->data_detectors; o; o = o->next) {
00059                 OSyncDataDetector *tmpl = o->data;
00060                 if (!strcmp(tmpl->sourceformat, sourcename) && !strcmp(tmpl->targetformat, targetname))
00061                         return tmpl;
00062         }
00063         return NULL;
00064 }
00065 
00066 OSyncConverterTemplate *osync_env_find_converter_template(OSyncEnv *env, const char *sourcename, const char *targetname)
00067 {
00068         GList *o;
00069         for (o = env->converter_templates; o; o = o->next) {
00070                 OSyncConverterTemplate *tmpl = o->data;
00071                 if (!strcmp(tmpl->source_format, sourcename) && !strcmp(tmpl->target_format, targetname))
00072                         return tmpl;
00073         }
00074         return NULL;
00075 }
00076 #endif
00077 
00080 void osync_env_register_detector(OSyncEnv *env, const char *sourceformat, const char *format, OSyncFormatDetectDataFunc detect_func)
00081 {
00082         g_assert(detect_func);
00083         OSyncDataDetector *detector = g_malloc0(sizeof(OSyncDataDetector));
00084         detector->sourceformat = strdup(sourceformat);
00085         detector->targetformat = strdup(format);
00086         detector->detect_func = detect_func;
00087 
00088         //Register the "inverse" detector which of course will always work
00089         env->data_detectors = g_list_append(env->data_detectors, detector);
00090         detector = g_malloc0(sizeof(OSyncDataDetector));
00091         detector->sourceformat = strdup(format);
00092         detector->targetformat = strdup(sourceformat);
00093         detector->detect_func = NULL;
00094 
00095         env->data_detectors = g_list_append(env->data_detectors, detector);
00096 }
00097 
00098 void osync_env_register_filter_function(OSyncEnv *env, const char *name, const char *objtype, const char *format, OSyncFilterFunction hook)
00099 {
00100         OSyncCustomFilter *function = g_malloc0(sizeof(OSyncCustomFilter));
00101         function->name = g_strdup(name);
00102         function->objtype = g_strdup(objtype);
00103         function->format = g_strdup(format);
00104         function->hook = hook;
00105         
00106         env->filter_functions = g_list_append(env->filter_functions, function);
00107 }
00108 
00109 void osync_env_register_objformat(OSyncEnv *env, const char *typename, const char *name)
00110 {
00111         OSyncObjFormatTemplate *format = NULL;
00112         if (!(format = osync_env_find_format_template(env, name))) {
00113                 format = g_malloc0(sizeof(OSyncObjFormatTemplate));
00114                 format->name = strdup(name);
00115                 format->objtype = g_strdup(typename);
00116                 //We default to malloc style!
00117                 //format->copy_func = osync_format_malloced_copy;
00118                 //format->destroy_func = osync_format_malloced_destroy;
00119                 env->format_templates = g_list_append(env->format_templates, format);
00120         }
00121 }
00122 
00123 void osync_env_register_objtype(OSyncEnv *env, const char *name)
00124 {
00125         OSyncObjTypeTemplate *type = NULL;
00126         if (!(type = osync_env_find_objtype_template(env, name))) {
00127                 type = g_malloc0(sizeof(OSyncObjTypeTemplate));
00128                 type->name = g_strdup(name);
00129                 env->objtype_templates = g_list_append(env->objtype_templates, type);
00130         }
00131 }
00132 
00133 void osync_env_register_converter(OSyncEnv *env, ConverterType type, const char *sourcename, const char *targetname, OSyncFormatConvertFunc convert_func)
00134 {
00135         OSyncConverterTemplate *converter = g_malloc0(sizeof(OSyncConverterTemplate));
00136 
00137         converter->source_format = sourcename;
00138         converter->target_format = targetname;
00139         converter->convert_func = convert_func;
00140         converter->type = type;
00141         env->converter_templates = g_list_append(env->converter_templates, converter);
00142 }
00143 
00144 void osync_env_converter_set_init(OSyncEnv *env, const char *sourcename, const char *targetname, OSyncFormatConverterInitFunc init_func, OSyncFormatConverterFinalizeFunc fin_func)
00145 {
00146         OSyncConverterTemplate *converter = osync_env_find_converter_template(env, sourcename, targetname);
00147         osync_assert_msg(converter != NULL, "You need to register the converter first");
00148         
00149         converter->init_func = init_func;
00150         converter->fin_func = fin_func;
00151 }
00152 
00153 void osync_env_register_extension(OSyncEnv *env, const char *from_format, const char *to_format, const char *extension_name, OSyncFormatExtInitFunc init_func)
00154 {
00155         OSyncFormatExtensionTemplate *ext = g_malloc0(sizeof(OSyncFormatExtensionTemplate));
00156         ext->from_formatname = g_strdup(from_format);
00157         ext->to_formatname = g_strdup(to_format);
00158         ext->name = g_strdup(extension_name);
00159         ext->init_func = init_func;
00160         
00161         env->extension_templates = g_list_append(env->extension_templates, ext);
00162 }
00163 
00164 void osync_env_format_set_demarshall_func(OSyncEnv *env, const char *formatname, OSyncFormatDemarshallFunc demarshall_func)
00165 {
00166         osync_trace(TRACE_INTERNAL, "osync_env_format_set_demarshall_func(%p, %s, %p)", env, formatname, demarshall_func);
00167         g_assert(env);
00168         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00169         osync_assert_msg(format, "You need to register the formattype first");
00170         format->demarshall_func = demarshall_func;
00171 }
00172 
00173 void osync_env_format_set_marshall_func(OSyncEnv *env, const char *formatname, OSyncFormatMarshallFunc marshall_func)
00174 {
00175         osync_trace(TRACE_INTERNAL, "osync_env_format_set_marshall_func(%p, %s, %p)", env, formatname, marshall_func);
00176         g_assert(env);
00177         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00178         osync_assert_msg(format, "You need to register the formattype first");
00179         format->marshall_func = marshall_func;
00180 }
00181 
00182 void osync_env_format_set_compare_func(OSyncEnv *env, const char *formatname, OSyncFormatCompareFunc cmp_func)
00183 {
00184         osync_trace(TRACE_INTERNAL, "osync_env_format_set_compare_func(%p, %s, %p)", env, formatname, cmp_func);
00185         g_assert(env);
00186         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00187         osync_assert_msg(format, "You need to register the formattype first");
00188         format->cmp_func = cmp_func;
00189 }
00190 
00191 void osync_env_format_set_destroy_func(OSyncEnv *env, const char *formatname, OSyncFormatDestroyFunc destroy_func)
00192 {
00193         g_assert(env);
00194         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00195         osync_assert_msg(format, "You need to register the formattype first");
00196         format->destroy_func = destroy_func;
00197 }
00198 
00199 void osync_env_format_set_copy_func(OSyncEnv *env, const char *formatname, OSyncFormatCopyFunc copy_func)
00200 {
00201         g_assert(env);
00202         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00203         osync_assert_msg(format, "You need to register the formattype first");
00204         format->copy_func = copy_func;
00205 }
00206 
00225 /*void osync_env_format_set_detect_func(OSyncEnv *env, const char *formatname, OSyncFormatDetectFunc detect_func)
00226 {
00227         g_assert(env);
00228         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00229         osync_assert_msg(format, "You need to register the formattype first");
00230         format->detect_func = detect_func;
00231 }*/
00232 
00233 void osync_env_format_set_duplicate_func(OSyncEnv *env, const char *formatname, OSyncFormatDuplicateFunc dupe_func)
00234 {
00235         g_assert(env);
00236         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00237         osync_assert_msg(format, "You need to register the formattype first");
00238         format->duplicate_func = dupe_func;
00239 }
00240 
00241 void osync_env_format_set_create_func(OSyncEnv *env, const char *formatname, OSyncFormatCreateFunc create_func)
00242 {
00243         g_assert(env);
00244         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00245         osync_assert_msg(format, "You need to register the formattype first");
00246         format->create_func = create_func;
00247 }
00248 
00249 void osync_env_format_set_print_func(OSyncEnv *env, const char *formatname, OSyncFormatPrintFunc print_func)
00250 {
00251         g_assert(env);
00252         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00253         osync_assert_msg(format, "You need to register the formattype first");
00254         format->print_func = print_func;
00255 }
00256 
00257 void osync_env_format_set_revision_func(OSyncEnv *env, const char *formatname, OSyncFormatRevisionFunc revision_func)
00258 {
00259         g_assert(env);
00260         OSyncObjFormatTemplate *format = osync_env_find_format_template(env, formatname);
00261         osync_assert_msg(format, "You need to register the formattype first");
00262         format->revision_func = revision_func;
00263 }