From: <arm...@de...> - 2006-08-22 19:05:39
|
Author: abauer Date: Tue Aug 22 21:05:33 2006 New Revision: 1218 URL: http://www.opensync.org/browser/?sc=3D1&rev=3D1218 Log: Ported the data and the contact format ported the xml contact format Modified: branches/dev-branch/formats/SConscript branches/dev-branch/formats/contact.c branches/dev-branch/formats/data.c branches/dev-branch/formats/vformats-xml/ (props changed) branches/dev-branch/formats/vformats-xml/opensync-xml.h branches/dev-branch/formats/vformats-xml/xml-support.c branches/dev-branch/formats/vformats-xml/xml-vcard.c Modified: branches/dev-branch/formats/SConscript URL: http://www.opensync.org/browser/branches/dev-branch/formats/SConscript= ?rev=3D1218&op=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- branches/dev-branch/formats/SConscript (original) +++ branches/dev-branch/formats/SConscript Tue Aug 22 21:05:33 2006 @@ -2,10 +2,24 @@ =20 env.Append( CPPPATH =3D '.' ) =20 +# The basic plugins + file =3D env.SharedLibrary('file', ['file.c'], LIBS =3D ['opensync'], LIBP= ATH =3D '#opensync') data =3D env.SharedLibrary('data', ['data.c'], LIBS =3D ['opensync'], LIBP= ATH =3D '#opensync') +contact =3D env.SharedLibrary('contact', ['contact.c'], LIBS =3D ['opensyn= c'], LIBPATH =3D '#opensync') + +# The xml support library + +xml =3D env.SharedLibrary('opensync-xml', ['vformats-xml/xml-support.c'], = LIBS =3D ['opensync'], LIBPATH =3D '#opensync') + +# The xml formats + +xmlvcard =3D env.SharedLibrary('xml-vcard', ['vformats-xml/xml-vcard.c', '= vformats-xml/vformat.c'], LIBS =3D ['opensync', 'opensync-xml'], LIBPATH = =3D ['#opensync', '.']) =20 env.Install(install_format, file) env.Install(install_format, data) +env.Install(install_format, contact) +env.Install(install_format, xmlvcard) +env.Install(install_lib, xml) env.Install(install_inc + '/opensync-1.0/opensync/', 'file.h') env.Alias('install', install_prefix) Modified: branches/dev-branch/formats/contact.c URL: http://www.opensync.org/browser/branches/dev-branch/formats/contact.c?= rev=3D1218&op=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- branches/dev-branch/formats/contact.c (original) +++ branches/dev-branch/formats/contact.c Tue Aug 22 21:05:33 2006 @@ -19,83 +19,149 @@ */ =20 #include <opensync/opensync.h> +#include <opensync/opensync_internals.h> +#include <opensync/opensync-support.h> +#include <opensync/opensync-serializer.h> +#include <opensync/opensync-format.h> #include <glib.h> -#include <string.h> -#include <stdio.h> =20 -static OSyncConvCmpResult compare_vcard(OSyncChange *leftchange, OSyncChan= ge *rightchange) +static OSyncConvCmpResult compare_vcard(const char *leftdata, unsigned int= leftsize, const char *rightdata, unsigned int rightsize) { - int leftinpsize =3D osync_change_get_datasize(leftchange); - char *leftinput =3D osync_change_get_data(leftchange); - int rightinpsize =3D osync_change_get_datasize(rightchange); - char *rightinput =3D osync_change_get_data(rightchange); + /* Consider empty block equal NULL pointers */ + if (!leftsize) leftdata =3D NULL; + if (!rightsize) rightdata =3D NULL; =09 - if (leftinpsize =3D=3D rightinpsize) { - if (!memcmp(leftinput, rightinput, leftinpsize)) - return CONV_DATA_SAME; + if (!leftdata && !rightdata) + return OSYNC_CONV_DATA_SAME; + =09 + if (leftdata && rightdata && (leftsize =3D=3D rightsize)) { + if (!memcmp(leftdata, rightdata, leftsize)) + return OSYNC_CONV_DATA_SAME; + else + return OSYNC_CONV_DATA_MISMATCH; } =09 - //Get the name of the contact and compare - //If the same, return SIMILAR -=09 - return CONV_DATA_MISMATCH; + return OSYNC_CONV_DATA_MISMATCH; } =20 -static osync_bool detect_plain_as_vcard21(OSyncFormatEnv *env, const char = *data, int size) +static osync_bool detect_plain_as_vcard21(const char *data, int size) { - osync_debug("VCARD21", 3, "start: %s", __func__); -=09 if (!data) return FALSE; =09 return g_pattern_match_simple("*BEGIN:VCARD*VERSION:2.1*", data); } =20 -static osync_bool detect_plain_as_vcard30(OSyncFormatEnv *env, const char = *data, int size) +static osync_bool detect_plain_as_vcard30(const char *data, int size) { - osync_debug("VCARD30", 3, "start: %s", __func__); -=09 if (!data) return FALSE; =20 return g_pattern_match_simple("*BEGIN:VCARD*VERSION:3.0*", data); } =20 -static void create_vcard21(OSyncChange *change) +static void create_vcard21(char **data, unsigned int *size) { - char *vcard =3D g_strdup_printf("BEGIN:VCARD\r\nVERSION:2.1\r\nN:%s;%s;;;= \r\nEND:VCARD\r\n", osync_rand_str(10), osync_rand_str(10)); - osync_change_set_data(change, vcard, strlen(vcard) + 1, TRUE); - if (!osync_change_get_uid(change)) - osync_change_set_uid(change, osync_rand_str(6)); + *data =3D g_strdup_printf("BEGIN:VCARD\r\nVERSION:2.1\r\nN:%s;%s;;;\r\nEN= D:VCARD\r\n", osync_rand_str(10), osync_rand_str(10)); + *size =3D strlen(*data); } =20 -static void create_vcard30(OSyncChange *change) +static void create_vcard30(char **data, unsigned int *size) { - char *vcard =3D g_strdup_printf("BEGIN:VCARD\r\nVERSION:3.0\r\nN:%s;%s;;;= \r\nEND:VCARD\r\n", osync_rand_str(10), osync_rand_str(10)); - osync_change_set_data(change, vcard, strlen(vcard) + 1, TRUE); - if (!osync_change_get_uid(change)) - osync_change_set_uid(change, osync_rand_str(6)); + *data =3D g_strdup_printf("BEGIN:VCARD\r\nVERSION:3.0\r\nN:%s;%s;;;\r\nEN= D:VCARD\r\n", osync_rand_str(10), osync_rand_str(10)); + *size =3D strlen(*data); } =20 -static OSyncFilterAction vcard_categories_filter(OSyncChange *change, char= *config) +static osync_bool vcard_categories_filter(OSyncData *data, const char *con= fig) { //Check what categories are supported here. - return OSYNC_FILTER_IGNORE; + return FALSE; } =20 -void get_info(OSyncEnv *env) +static void destroy_vcard(char *input, size_t inpsize) { - osync_env_register_objtype(env, "contact"); + g_free(input); +} + +osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *format =3D osync_objformat_new("vcard21", "contact", erro= r); + if (!format) + return FALSE; =09 - osync_env_register_objformat(env, "contact", "vcard21"); - osync_env_format_set_compare_func(env, "vcard21", compare_vcard); - osync_env_format_set_create_func(env, "vcard21", create_vcard21); - osync_env_register_detector(env, "plain", "vcard21", detect_plain_as_vcar= d21); - osync_env_register_filter_function(env, "vcard_categories_filter", "conta= ct", "vcard21", vcard_categories_filter); + osync_objformat_set_compare_func(format, compare_vcard); + osync_objformat_set_create_func(format, create_vcard21); + osync_objformat_set_destroy_func(format, destroy_vcard); +=09 + osync_format_env_register_objformat(env, format); + osync_objformat_unref(format); +=09 +=09 + format =3D osync_objformat_new("vcard30", "contact", error); + if (!format) + return FALSE; +=09 + osync_objformat_set_compare_func(format, compare_vcard); + osync_objformat_set_create_func(format, create_vcard30); + osync_objformat_set_destroy_func(format, destroy_vcard); +=09 + osync_format_env_register_objformat(env, format); + osync_objformat_unref(format); +=09 +=09 + OSyncCustomFilter *filter =3D osync_custom_filter_new("contact", "vcard21= ", "vcard_categories_filter", vcard_categories_filter, error); + if (!filter) + return FALSE; +=09 + osync_format_env_register_filter(env, filter); + osync_custom_filter_unref(filter); +=09 + filter =3D osync_custom_filter_new("contact", "vcard30", "vcard_categorie= s_filter", vcard_categories_filter, error); + if (!filter) + return FALSE; +=09 + osync_format_env_register_filter(env, filter); + osync_custom_filter_unref(filter); +=09 + return TRUE; +} =20 - osync_env_register_objformat(env, "contact", "vcard30"); - osync_env_format_set_compare_func(env, "vcard30", compare_vcard); - osync_env_format_set_create_func(env, "vcard30", create_vcard30); - osync_env_register_detector(env, "plain", "vcard30", detect_plain_as_vcar= d30); - osync_env_register_filter_function(env, "vcard_categories_filter", "conta= ct", "vcard30", vcard_categories_filter); +osync_bool get_conversion_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *plain =3D osync_format_env_find_objformat(env, "plain"); + if (!plain) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find plain format= "); + return FALSE; + } +=09 + OSyncObjFormat *vcard21 =3D osync_format_env_find_objformat(env, "vcard21= "); + if (!vcard21) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find vcard21 form= at"); + return FALSE; + } +=09 + OSyncObjFormat *vcard30 =3D osync_format_env_find_objformat(env, "vcard30= "); + if (!vcard30) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find vcard30 form= at"); + return FALSE; + } +=09 + OSyncFormatConverter *conv =3D osync_converter_new_detector(plain, vcard2= 1, detect_plain_as_vcard21, error); + if (!conv) + return FALSE; + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); +=09 + conv =3D osync_converter_new_detector(plain, vcard30, detect_plain_as_vca= rd30, error); + if (!conv) + return FALSE; + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); +=09 + return TRUE; } + +int get_version(void) +{ + return 1; +} Modified: branches/dev-branch/formats/data.c URL: http://www.opensync.org/browser/branches/dev-branch/formats/data.c?rev= =3D1218&op=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- branches/dev-branch/formats/data.c (original) +++ branches/dev-branch/formats/data.c Tue Aug 22 21:05:33 2006 @@ -24,7 +24,6 @@ #include <opensync/opensync-serializer.h> #include <opensync/opensync-format.h> #include <glib.h> -#include "file.h" =20 static OSyncConvCmpResult compare_plain(const char *leftdata, unsigned int= leftsize, const char *rightdata, unsigned int rightsize) { Modified: branches/dev-branch/formats/vformats-xml/opensync-xml.h URL: http://www.opensync.org/browser/branches/dev-branch/formats/vformats-x= ml/opensync-xml.h?rev=3D1218&op=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- branches/dev-branch/formats/vformats-xml/opensync-xml.h (original) +++ branches/dev-branch/formats/vformats-xml/opensync-xml.h Tue Aug 22 21:0= 5:33 2006 @@ -58,10 +58,10 @@ xmlXPathObject *osxml_get_unknown_nodes(xmlDoc *doc); OSyncConvCmpResult osxml_compare(xmlDoc *leftinpdoc, xmlDoc *rightinpdoc, = OSyncXMLScore *scores, int default_score, int treshold); char *osxml_write_to_string(xmlDoc *doc); -osync_bool osxml_copy(const char *input, int inpsize, char **output, int *= outpsize); +osync_bool osxml_copy(const char *input, unsigned int inpsize, char **outp= ut, unsigned int *outpsize, OSyncError **error); =20 -osync_bool osxml_marshall(const char *input, int inpsize, char **output, i= nt *outpsize, OSyncError **error); -osync_bool osxml_demarshall(const char *input, int inpsize, char **output,= int *outpsize, OSyncError **error); +osync_bool osxml_marshal(const char *input, unsigned int inpsize, OSyncMes= sage *message, OSyncError **error); +osync_bool osxml_demarshal(OSyncMessage *message, char **output, unsigned = int *outpsize, OSyncError **error); =20 #ifdef __cplusplus } Modified: branches/dev-branch/formats/vformats-xml/xml-support.c URL: http://www.opensync.org/browser/branches/dev-branch/formats/vformats-x= ml/xml-support.c?rev=3D1218&op=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- branches/dev-branch/formats/vformats-xml/xml-support.c (original) +++ branches/dev-branch/formats/vformats-xml/xml-support.c Tue Aug 22 21:05= :33 2006 @@ -19,6 +19,7 @@ */ =20 #include "opensync-xml.h" +#include <opensync/opensync-serializer.h> #include <glib.h> =20 xmlNode *osxml_node_add_root(xmlDoc *doc, const char *name) @@ -381,7 +382,7 @@ return (char *)temp; } =20 -osync_bool osxml_copy(const char *input, int inpsize, char **output, int *= outpsize) +osync_bool osxml_copy(const char *input, unsigned int inpsize, char **outp= ut, unsigned int *outpsize, OSyncError **error) { xmlDoc *doc =3D (xmlDoc *)(input); xmlDoc *newdoc =3D xmlCopyDoc(doc, TRUE); @@ -390,20 +391,24 @@ return TRUE; } =20 -osync_bool osxml_marshall(const char *input, int inpsize, char **output, i= nt *outpsize, OSyncError **error) +osync_bool osxml_marshal(const char *input, unsigned int inpsize, OSyncMes= sage *message, OSyncError **error) { xmlDoc *doc =3D (xmlDoc*)input; xmlChar *result; int size; xmlDocDumpMemory(doc, &result, &size); - *output =3D (char*)result; - *outpsize =3D size; + osync_message_write_buffer(message, result, size); +=09 return TRUE; } =20 -osync_bool osxml_demarshall(const char *input, int inpsize, char **output,= int *outpsize, OSyncError **error) -{ - xmlDoc *doc =3D xmlParseMemory(input, inpsize); +osync_bool osxml_demarshal(OSyncMessage *message, char **output, unsigned = int *outpsize, OSyncError **error) +{ + void *input =3D NULL; + int size =3D 0; + osync_message_read_buffer(message, &input, &size); +=09 + xmlDoc *doc =3D xmlParseMemory((char *)input, size); if (!doc) { osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid XML data received"); goto error; Modified: branches/dev-branch/formats/vformats-xml/xml-vcard.c URL: http://www.opensync.org/browser/branches/dev-branch/formats/vformats-x= ml/xml-vcard.c?rev=3D1218&op=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- branches/dev-branch/formats/vformats-xml/xml-vcard.c (original) +++ branches/dev-branch/formats/vformats-xml/xml-vcard.c Tue Aug 22 21:05:3= 3 2006 @@ -20,6 +20,7 @@ =20 #include <opensync/opensync.h> #include <opensync/opensync-format.h> +#include <opensync/opensync-data.h> =20 #include "opensync-xml.h" #include "vformat.h" @@ -342,521 +343,10 @@ osync_trace(TRACE_EXIT, "%s", __func__); } =20 -static osync_bool conv_vcard_to_xml(void *conv_data, char *input, int inps= ize, char **output, int *outpsize, osync_bool *free_input, OSyncError **err= or) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p, %i, %p, %p, %p, %p)", __func__, conv= _data, input, inpsize, output, outpsize, free_input, error); -=09 - GHashTable *hooks =3D (GHashTable *)conv_data; -=09 - osync_trace(TRACE_INTERNAL, "Input Vcard is:\n%s", input); -=09 - //Parse the vcard - VFormat *vcard =3D vformat_new_from_string(input); -=09 - osync_trace(TRACE_INTERNAL, "Creating xml doc"); -=09 - //Create a new xml document - xmlDoc *doc =3D xmlNewDoc((xmlChar*)"1.0"); - xmlNode *root =3D osxml_node_add_root(doc, "contact"); -=09 - osync_trace(TRACE_INTERNAL, "parsing attributes"); -=09 - //For every attribute we have call the handling hook - GList *attributes =3D vformat_get_attributes(vcard); - GList *a =3D NULL; - for (a =3D attributes; a; a =3D a->next) { - VFormatAttribute *attr =3D a->data; - vcard_handle_attribute(hooks, root, attr); - } -=09 - osync_trace(TRACE_INTERNAL, "Output XML is:\n%s", osxml_write_to_string(d= oc)); -=09 - *free_input =3D TRUE; - *output =3D (char *)doc; - *outpsize =3D sizeof(doc); - osync_trace(TRACE_EXIT, "%s: TRUE", __func__); - return TRUE; -} - -static osync_bool needs_encoding(const unsigned char *tmp, const char *enc= oding) -{ - int i =3D 0; - if (!strcmp(encoding, "QUOTED-PRINTABLE")) { - while (tmp[i] !=3D 0) { - if (tmp[i] > 127 || tmp[i] =3D=3D 10 || tmp[i] =3D=3D 13) - return TRUE; - i++; - } - } else { - return !g_utf8_validate((gchar*)tmp, -1, NULL); - } - return FALSE; -} - -static osync_bool needs_charset(const unsigned char *tmp) -{ - int i =3D 0; - while (tmp[i] !=3D 0) { - if (tmp[i] > 127) - return TRUE; - i++; - } - return FALSE; -} - -static void add_value(VFormatAttribute *attr, xmlNode *parent, const char = *name, const char *encoding) -{ - char *tmp =3D osxml_find_node(parent, name); - - if (!tmp) { - /* If there is no node with the given name, add an empty value to the li= st. - * This is necessary because some fields (N and ADR, for example) need - * a specific order of the values - */ - tmp =3D ""; - } -=09 - if (needs_charset((unsigned char*)tmp)) - if (!vformat_attribute_has_param (attr, "CHARSET")) - vformat_attribute_add_param_with_value(attr, "CHARSET", "UTF-8"); -=09 - if (needs_encoding((unsigned char*)tmp, encoding)) { - if (!vformat_attribute_has_param (attr, "ENCODING")) - vformat_attribute_add_param_with_value(attr, "ENCODING", encoding); - vformat_attribute_add_value_decoded(attr, tmp, strlen(tmp) + 1); - } else - vformat_attribute_add_value(attr, tmp); - g_free(tmp); -} - -static void handle_xml_type_parameter(VFormatAttribute *attr, xmlNode *cur= rent) -{ - osync_trace(TRACE_INTERNAL, "Handling type xml parameter"); - char *content =3D (char*)xmlNodeGetContent(current); - VFormatParam *param =3D vformat_attribute_param_new("TYPE"); - vformat_attribute_param_add_value(param, content); - vformat_attribute_add_param (attr, param); - g_free(content); -} - -static void handle_xml_category_parameter(VFormatAttribute *attr, xmlNode = *current) -{ - osync_trace(TRACE_INTERNAL, "Handling category xml parameter"); - char *content =3D (char*)xmlNodeGetContent(current); - vformat_attribute_add_value(attr, content); - g_free(content); -} - -static void handle_xml_unit_parameter(VFormatAttribute *attr, xmlNode *cur= rent) -{ - osync_trace(TRACE_INTERNAL, "Handling unit xml parameter"); - char *content =3D (char*)xmlNodeGetContent(current); - vformat_attribute_add_value(attr, content); - g_free(content); -} - -static void xml_handle_unknown_parameter(VFormatAttribute *attr, xmlNode *= current) -{ - osync_trace(TRACE_INTERNAL, "Handling unknown xml parameter %s", current-= >name); - char *content =3D (char*)xmlNodeGetContent(current); - vformat_attribute_add_param_with_value(attr, (char*)current->name, conten= t); - g_free(content); -} - -static void xml_vcard_handle_parameter(OSyncHookTables *hooks, VFormatAttr= ibute *attr, xmlNode *current) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p, %p:%s)", __func__, hooks, attr, curr= ent, current ? (char *)current->name : "None"); -=09 - //Find the handler for this parameter - void (* xml_param_handler)(VFormatAttribute *attr, xmlNode *); - char *content =3D (char*)xmlNodeGetContent(current); - char *paramname =3D g_strdup_printf("%s=3D%s", current->name, content); - g_free(content); - xml_param_handler =3D g_hash_table_lookup(hooks->parameters, paramname); - g_free(paramname); - if (!xml_param_handler) - xml_param_handler =3D g_hash_table_lookup(hooks->parameters, current->na= me); -=09 - if (xml_param_handler =3D=3D HANDLE_IGNORE) { - osync_trace(TRACE_EXIT, "%s: Ignored", __func__); - return; - } -=09 - if (xml_param_handler) - xml_param_handler(attr, current); -=09 - osync_trace(TRACE_EXIT, "%s", __func__); -} - -static VFormatAttribute *xml_handle_unknown_attribute(VFormat *vcard, xmlN= ode *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling unknown xml attribute %s", root->na= me); - char *name =3D osxml_find_node(root, "NodeName"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, name); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_fullname_attribute(VFormat *vcard, xml= Node *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling fullname xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "FN"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_name_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling fullname xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "N"); - add_value(attr, root, "LastName", encoding); - add_value(attr, root, "FirstName", encoding); - add_value(attr, root, "Additional", encoding); - add_value(attr, root, "Prefix", encoding); - add_value(attr, root, "Suffix", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static void xml_vcard_handle_attribute(OSyncHookTables *hooks, VFormat *vc= ard, xmlNode *root, const char *encoding) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p, %p:%s)", __func__, hooks, vcard, roo= t, root ? (char *)root->name : "None"); - VFormatAttribute *attr =3D NULL; -=09 - //We need to find the handler for this attribute - VFormatAttribute *(* xml_attr_handler)(VFormat *vcard, xmlNode *root, con= st char *) =3D g_hash_table_lookup(hooks->attributes, root->name); - osync_trace(TRACE_INTERNAL, "xml hook is: %p", xml_attr_handler); - if (xml_attr_handler =3D=3D HANDLE_IGNORE) { - osync_trace(TRACE_EXIT, "%s: Ignored", __func__); - return; - } - if (xml_attr_handler) - attr =3D xml_attr_handler(vcard, root, encoding); - else { - osync_trace(TRACE_EXIT, "%s: Ignored2", __func__); - return; - } -=09 - //Handle all parameters of this attribute - xmlNode *child =3D root->xmlChildrenNode; - while (child) { - xml_vcard_handle_parameter(hooks, attr, child); - child =3D child->next; - } - osync_trace(TRACE_EXIT, "%s", __func__); -} - -static VFormatAttribute *handle_xml_photo_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling photo xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "PHOTO"); - add_value(attr, root, "Content", encoding); - vformat_attribute_add_param_with_value(attr, "ENCODING", "b"); - vformat_attribute_add_param_with_value(attr, "TYPE", osxml_find_node(root= , "Type")); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_birthday_attribute(VFormat *vcard, xml= Node *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling birthday xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "BDAY"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_address_attribute(VFormat *vcard, xmlN= ode *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling address xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "ADR"); - add_value(attr, root, "PostalBox", encoding); - add_value(attr, root, "ExtendedAddress", encoding); - add_value(attr, root, "Street", encoding); - add_value(attr, root, "City", encoding); - add_value(attr, root, "Region", encoding); - add_value(attr, root, "PostalCode", encoding); - add_value(attr, root, "Country", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_label_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling label xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "LABEL"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_telephone_attribute(VFormat *vcard, xm= lNode *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling telephone xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "TEL"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_email_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling email xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "EMAIL"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_mailer_attribute(VFormat *vcard, xmlNo= de *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling mailer xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "MAILER"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_timezone_attribute(VFormat *vcard, xml= Node *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling timezone xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "TZ"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_location_attribute(VFormat *vcard, xml= Node *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling location xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "GEO"); - add_value(attr, root, "Latitude", encoding); - add_value(attr, root, "Longitude", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_title_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling title xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "TITLE"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_role_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling role xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "ROLE"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_logo_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling logo xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "LOGO"); - add_value(attr, root, "Content", encoding); - vformat_attribute_add_param_with_value(attr, "ENCODING", "b"); - vformat_attribute_add_param_with_value(attr, "TYPE", osxml_find_node(root= , "Type")); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_organization_attribute(VFormat *vcard,= xmlNode *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling organization xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "ORG"); - add_value(attr, root, "Name", encoding); - add_value(attr, root, "Department", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_note_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling note xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "NOTE"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_revision_attribute(VFormat *vcard, xml= Node *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling revision xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "REV"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_sound_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling sound xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "SOUND"); - add_value(attr, root, "Content", encoding); - vformat_attribute_add_param_with_value(attr, "ENCODING", "b"); - vformat_attribute_add_param_with_value(attr, "TYPE", osxml_find_node(root= , "Type")); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_url_attribute(VFormat *vcard, xmlNode = *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling url xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "URL"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_uid_attribute(VFormat *vcard, xmlNode = *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling uid xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "UID"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_key_attribute(VFormat *vcard, xmlNode = *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling key xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "KEY"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_nickname_attribute(VFormat *vcard, xml= Node *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling nickname xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "NICKNAME"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_class_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling class xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "CLASS"); - add_value(attr, root, "Content", encoding); - vformat_add_attribute(vcard, attr); - return attr; -} - -static VFormatAttribute *handle_xml_categories_attribute(VFormat *vcard, x= mlNode *root, const char *encoding) -{ - osync_trace(TRACE_INTERNAL, "Handling categories xml attribute"); - VFormatAttribute *attr =3D vformat_attribute_new(NULL, "CATEGORIES"); - vformat_add_attribute(vcard, attr); - return attr; -} - -static osync_bool conv_xml_to_vcard(void *user_data, char *input, int inps= ize, char **output, int *outpsize, osync_bool *free_input, OSyncError **err= or, int target) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p, %i, %p, %p, %p, %p)", __func__, user= _data, input, inpsize, output, outpsize, free_input, error); -=09 - osync_trace(TRACE_INTERNAL, "Input XML is:\n%s", osxml_write_to_string((x= mlDoc *)input)); -=09 - //Get the root node of the input document - xmlNode *root =3D xmlDocGetRootElement((xmlDoc *)input); - if (!root) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get xml root elem= ent"); - osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(erro= r)); - return FALSE; - } -=09 - if (xmlStrcmp(root->name, (const xmlChar *)"contact")) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "Wrong xml root element"); - osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(erro= r)); - return FALSE; - } -=09 - //Make the new vcard - VFormat *vcard =3D vformat_new(); -=09 - osync_trace(TRACE_INTERNAL, "parsing cml attributes"); - const char *std_encoding =3D NULL; - if (target =3D=3D VFORMAT_CARD_21) - std_encoding =3D "QUOTED-PRINTABLE"; - else - std_encoding =3D "B"; -=09 - if (root) - root =3D root->children; - while (root) { - xml_vcard_handle_attribute((OSyncHookTables *)user_data, vcard, root, st= d_encoding); - root =3D root->next; - } -=09 - *free_input =3D TRUE; - *output =3D vformat_to_string(vcard, target); - osync_trace(TRACE_INTERNAL, "vcard output is: \n%s", *output); - *outpsize =3D strlen(*output) + 1; - osync_trace(TRACE_EXIT, "%s", __func__); -=09 - return TRUE; -} - -static osync_bool conv_xml_to_vcard30(void *user_data, char *input, int in= psize, char **output, int *outpsize, osync_bool *free_input, OSyncError **e= rror) -{ - return conv_xml_to_vcard(user_data, input, inpsize, output, outpsize, fre= e_input, error, VFORMAT_CARD_30); -} - -static osync_bool conv_xml_to_vcard21(void *user_data, char *input, int in= psize, char **output, int *outpsize, osync_bool *free_input, OSyncError **e= rror) -{ - return conv_xml_to_vcard(user_data, input, inpsize, output, outpsize, fre= e_input, error, VFORMAT_CARD_21); -} - -static OSyncConvCmpResult compare_contact(OSyncChange *leftchange, OSyncCh= ange *rightchange) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, leftchange, rightchange); -=09 - OSyncXMLScore score[] =3D - { - //{30, "/contact/FullName"}, - {100, "/contact/Name"}, - //{20, "/contact/Telephone"}, - //{20, "/contact/Address"}, - //{1, "/contact/UnknownNode"}, - {0, "/contact/*/Slot"}, - {0, "/contact/*/Type"}, - {0, "/contact/WantsHtml"}, - {0, "/contact/Class"}, - {0, "/contact/FileAs"}, - {0, "/contact/Uid"}, - {0, "/contact/Revision"}, - {0, NULL} - }; -=09 - OSyncConvCmpResult ret =3D osxml_compare((xmlDoc*)osync_change_get_data(l= eftchange), (xmlDoc*)osync_change_get_data(rightchange), score, 0, 99); -=09 - osync_trace(TRACE_EXIT, "%s: %i", __func__, ret); - return ret; -} - -static char *print_contact(OSyncChange *change) -{ - xmlDoc *doc =3D (xmlDoc *)osync_change_get_data(change); -=09 - return osxml_write_to_string(doc); -} - -static void destroy_xml(char *data, size_t size, osync_bool free_part) -{ - xmlFreeDoc((xmlDoc *)data); -} - -static void *init_vcard_to_xml(void) -{ - osync_trace(TRACE_ENTRY, "%s", __func__); +static osync_bool conv_vcard_to_xml(char *input, unsigned int inpsize, cha= r **output, unsigned int *outpsize, osync_bool *free_input, const char *con= fig, OSyncError **error) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p, %p, %p, %s, %p)", __func__, inpu= t, inpsize, output, outpsize, free_input, config, error); +=09 GHashTable *table =3D g_hash_table_new(g_str_hash, g_str_equal); =09 g_hash_table_insert(table, "FN", handle_fullname_attribute); @@ -893,18 +383,446 @@ =09 g_hash_table_insert(table, "TYPE", handle_type_parameter); =09 - osync_trace(TRACE_EXIT, "%s: %p", __func__, table); - return (void *)table; -} - -static void fin_vcard_to_xml(void *data) -{ - g_hash_table_destroy((GHashTable *)data); -} - -static void *init_xml_to_vcard(void) -{ - osync_trace(TRACE_ENTRY, "%s", __func__); + osync_trace(TRACE_INTERNAL, "Input Vcard is:\n%s", input); +=09 + //Parse the vcard + VFormat *vcard =3D vformat_new_from_string(input); +=09 + osync_trace(TRACE_INTERNAL, "Creating xml doc"); +=09 + //Create a new xml document + xmlDoc *doc =3D xmlNewDoc((xmlChar*)"1.0"); + xmlNode *root =3D osxml_node_add_root(doc, "contact"); +=09 + osync_trace(TRACE_INTERNAL, "parsing attributes"); +=09 + //For every attribute we have call the handling hook + GList *attributes =3D vformat_get_attributes(vcard); + GList *a =3D NULL; + for (a =3D attributes; a; a =3D a->next) { + VFormatAttribute *attr =3D a->data; + vcard_handle_attribute(table, root, attr); + } +=09 + osync_trace(TRACE_INTERNAL, "Output XML is:\n%s", osxml_write_to_string(d= oc)); +=09 + *free_input =3D TRUE; + *output =3D (char *)doc; + *outpsize =3D sizeof(doc); + osync_trace(TRACE_EXIT, "%s: TRUE", __func__); + return TRUE; +} + +static osync_bool needs_encoding(const unsigned char *tmp, const char *enc= oding) +{ + int i =3D 0; + if (!strcmp(encoding, "QUOTED-PRINTABLE")) { + while (tmp[i] !=3D 0) { + if (tmp[i] > 127 || tmp[i] =3D=3D 10 || tmp[i] =3D=3D 13) + return TRUE; + i++; + } + } else { + return !g_utf8_validate((gchar*)tmp, -1, NULL); + } + return FALSE; +} + +static osync_bool needs_charset(const unsigned char *tmp) +{ + int i =3D 0; + while (tmp[i] !=3D 0) { + if (tmp[i] > 127) + return TRUE; + i++; + } + return FALSE; +} + +static void add_value(VFormatAttribute *attr, xmlNode *parent, const char = *name, const char *encoding) +{ + char *tmp =3D osxml_find_node(parent, name); + + if (!tmp) { + /* If there is no node with the given name, add an empty value to the li= st. + * This is necessary because some fields (N and ADR, for example) need + * a specific order of the values + */ + tmp =3D ""; + } +=09 + if (needs_charset((unsigned char*)tmp)) + if (!vformat_attribute_has_param (attr, "CHARSET")) + vformat_attribute_add_param_with_value(attr, "CHARSET", "UTF-8"); +=09 + if (needs_encoding((unsigned char*)tmp, encoding)) { + if (!vformat_attribute_has_param (attr, "ENCODING")) + vformat_attribute_add_param_with_value(attr, "ENCODING", encoding); + vformat_attribute_add_value_decoded(attr, tmp, strlen(tmp) + 1); + } else + vformat_attribute_add_value(attr, tmp); + g_free(tmp); +} + +static void handle_xml_type_parameter(VFormatAttribute *attr, xmlNode *cur= rent) +{ + osync_trace(TRACE_INTERNAL, "Handling type xml parameter"); + char *content =3D (char*)xmlNodeGetContent(current); + VFormatParam *param =3D vformat_attribute_param_new("TYPE"); + vformat_attribute_param_add_value(param, content); + vformat_attribute_add_param (attr, param); + g_free(content); +} + +static void handle_xml_category_parameter(VFormatAttribute *attr, xmlNode = *current) +{ + osync_trace(TRACE_INTERNAL, "Handling category xml parameter"); + char *content =3D (char*)xmlNodeGetContent(current); + vformat_attribute_add_value(attr, content); + g_free(content); +} + +static void handle_xml_unit_parameter(VFormatAttribute *attr, xmlNode *cur= rent) +{ + osync_trace(TRACE_INTERNAL, "Handling unit xml parameter"); + char *content =3D (char*)xmlNodeGetContent(current); + vformat_attribute_add_value(attr, content); + g_free(content); +} + +static void xml_handle_unknown_parameter(VFormatAttribute *attr, xmlNode *= current) +{ + osync_trace(TRACE_INTERNAL, "Handling unknown xml parameter %s", current-= >name); + char *content =3D (char*)xmlNodeGetContent(current); + vformat_attribute_add_param_with_value(attr, (char*)current->name, conten= t); + g_free(content); +} + +static void xml_vcard_handle_parameter(OSyncHookTables *hooks, VFormatAttr= ibute *attr, xmlNode *current) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p:%s)", __func__, hooks, attr, curr= ent, current ? (char *)current->name : "None"); +=09 + //Find the handler for this parameter + void (* xml_param_handler)(VFormatAttribute *attr, xmlNode *); + char *content =3D (char*)xmlNodeGetContent(current); + char *paramname =3D g_strdup_printf("%s=3D%s", current->name, content); + g_free(content); + xml_param_handler =3D g_hash_table_lookup(hooks->parameters, paramname); + g_free(paramname); + if (!xml_param_handler) + xml_param_handler =3D g_hash_table_lookup(hooks->parameters, current->na= me); +=09 + if (xml_param_handler =3D=3D HANDLE_IGNORE) { + osync_trace(TRACE_EXIT, "%s: Ignored", __func__); + return; + } +=09 + if (xml_param_handler) + xml_param_handler(attr, current); +=09 + osync_trace(TRACE_EXIT, "%s", __func__); +} + +static VFormatAttribute *xml_handle_unknown_attribute(VFormat *vcard, xmlN= ode *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling unknown xml attribute %s", root->na= me); + char *name =3D osxml_find_node(root, "NodeName"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, name); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_fullname_attribute(VFormat *vcard, xml= Node *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling fullname xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "FN"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_name_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling fullname xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "N"); + add_value(attr, root, "LastName", encoding); + add_value(attr, root, "FirstName", encoding); + add_value(attr, root, "Additional", encoding); + add_value(attr, root, "Prefix", encoding); + add_value(attr, root, "Suffix", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static void xml_vcard_handle_attribute(OSyncHookTables *hooks, VFormat *vc= ard, xmlNode *root, const char *encoding) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p:%s)", __func__, hooks, vcard, roo= t, root ? (char *)root->name : "None"); + VFormatAttribute *attr =3D NULL; +=09 + //We need to find the handler for this attribute + VFormatAttribute *(* xml_attr_handler)(VFormat *vcard, xmlNode *root, con= st char *) =3D g_hash_table_lookup(hooks->attributes, root->name); + osync_trace(TRACE_INTERNAL, "xml hook is: %p", xml_attr_handler); + if (xml_attr_handler =3D=3D HANDLE_IGNORE) { + osync_trace(TRACE_EXIT, "%s: Ignored", __func__); + return; + } + if (xml_attr_handler) + attr =3D xml_attr_handler(vcard, root, encoding); + else { + osync_trace(TRACE_EXIT, "%s: Ignored2", __func__); + return; + } +=09 + //Handle all parameters of this attribute + xmlNode *child =3D root->xmlChildrenNode; + while (child) { + xml_vcard_handle_parameter(hooks, attr, child); + child =3D child->next; + } + osync_trace(TRACE_EXIT, "%s", __func__); +} + +static VFormatAttribute *handle_xml_photo_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling photo xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "PHOTO"); + add_value(attr, root, "Content", encoding); + vformat_attribute_add_param_with_value(attr, "ENCODING", "b"); + vformat_attribute_add_param_with_value(attr, "TYPE", osxml_find_node(root= , "Type")); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_birthday_attribute(VFormat *vcard, xml= Node *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling birthday xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "BDAY"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_address_attribute(VFormat *vcard, xmlN= ode *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling address xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "ADR"); + add_value(attr, root, "PostalBox", encoding); + add_value(attr, root, "ExtendedAddress", encoding); + add_value(attr, root, "Street", encoding); + add_value(attr, root, "City", encoding); + add_value(attr, root, "Region", encoding); + add_value(attr, root, "PostalCode", encoding); + add_value(attr, root, "Country", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_label_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling label xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "LABEL"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_telephone_attribute(VFormat *vcard, xm= lNode *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling telephone xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "TEL"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_email_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling email xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "EMAIL"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_mailer_attribute(VFormat *vcard, xmlNo= de *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling mailer xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "MAILER"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_timezone_attribute(VFormat *vcard, xml= Node *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling timezone xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "TZ"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_location_attribute(VFormat *vcard, xml= Node *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling location xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "GEO"); + add_value(attr, root, "Latitude", encoding); + add_value(attr, root, "Longitude", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_title_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling title xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "TITLE"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_role_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling role xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "ROLE"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_logo_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling logo xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "LOGO"); + add_value(attr, root, "Content", encoding); + vformat_attribute_add_param_with_value(attr, "ENCODING", "b"); + vformat_attribute_add_param_with_value(attr, "TYPE", osxml_find_node(root= , "Type")); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_organization_attribute(VFormat *vcard,= xmlNode *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling organization xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "ORG"); + add_value(attr, root, "Name", encoding); + add_value(attr, root, "Department", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_note_attribute(VFormat *vcard, xmlNode= *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling note xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "NOTE"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_revision_attribute(VFormat *vcard, xml= Node *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling revision xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "REV"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_sound_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling sound xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "SOUND"); + add_value(attr, root, "Content", encoding); + vformat_attribute_add_param_with_value(attr, "ENCODING", "b"); + vformat_attribute_add_param_with_value(attr, "TYPE", osxml_find_node(root= , "Type")); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_url_attribute(VFormat *vcard, xmlNode = *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling url xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "URL"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_uid_attribute(VFormat *vcard, xmlNode = *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling uid xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "UID"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_key_attribute(VFormat *vcard, xmlNode = *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling key xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "KEY"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_nickname_attribute(VFormat *vcard, xml= Node *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling nickname xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "NICKNAME"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_class_attribute(VFormat *vcard, xmlNod= e *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling class xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "CLASS"); + add_value(attr, root, "Content", encoding); + vformat_add_attribute(vcard, attr); + return attr; +} + +static VFormatAttribute *handle_xml_categories_attribute(VFormat *vcard, x= mlNode *root, const char *encoding) +{ + osync_trace(TRACE_INTERNAL, "Handling categories xml attribute"); + VFormatAttribute *attr =3D vformat_attribute_new(NULL, "CATEGORIES"); + vformat_add_attribute(vcard, attr); + return attr; +} + +static osync_bool conv_xml_to_vcard(char *input, unsigned int inpsize, cha= r **output, unsigned int *outpsize, osync_bool *free_input, const char *con= fig, OSyncError **error, int target) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p, %p, %p, %s, %p, %i)", __func__, = input, inpsize, output, outpsize, free_input, config, error, target); +=09 + osync_trace(TRACE_INTERNAL, "Input XML is:\n%s", osxml_write_to_string((x= mlDoc *)input)); +=09 + //Get the root node of the input document + xmlNode *root =3D xmlDocGetRootElement((xmlDoc *)input); + if (!root) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get xml root elem= ent"); + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(erro= r)); + return FALSE; + } +=09 + if (xmlStrcmp(root->name, (const xmlChar *)"contact")) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Wrong xml root element"); + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(erro= r)); + return FALSE; + } +=09 + //Make the new vcard + VFormat *vcard =3D vformat_new(); +=09 + osync_trace(TRACE_INTERNAL, "parsing cml attributes"); + const char *std_encoding =3D NULL; + if (target =3D=3D VFORMAT_CARD_21) + std_encoding =3D "QUOTED-PRINTABLE"; + else + std_encoding =3D "B"; =09 OSyncHookTables *hooks =3D g_malloc0(sizeof(OSyncHookTables)); =09 @@ -943,23 +861,76 @@ =09 g_hash_table_insert(hooks->parameters, "UnknownParameter", xml_handle_unk= nown_parameter); =09 - osync_trace(TRACE_EXIT, "%s: %p", __func__, hooks); - return (void *)hooks; -} - -static void fin_xml_to_vcard(void *data) -{ - OSyncHookTables *hooks =3D (OSyncHookTables *)hooks; - g_hash_table_destroy(hooks->attributes); - g_hash_table_destroy(hooks->parameters); - g_free(hooks); -} - -static time_t get_revision(OSyncChange *change, OSyncError **error) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, change, error); -=09 - xmlDoc *doc =3D (xmlDoc *)osync_change_get_data(change); + if (root) + root =3D root->children; + while (root) { + xml_vcard_handle_attribute(hooks, vcard, root, std_encoding); + root =3D root->next; + } +=09 + *free_input =3D TRUE; + *output =3D vformat_to_string(vcard, target); + osync_trace(TRACE_INTERNAL, "vcard output is: \n%s", *output); + *outpsize =3D strlen(*output) + 1; + osync_trace(TRACE_EXIT, "%s", __func__); +=09 + return TRUE; +} + +static osync_bool conv_xml_to_vcard30(char *input, unsigned int inpsize, c= har **output, unsigned int *outpsize, osync_bool *free_input, const char *c= onfig, OSyncError **error) +{ + return conv_xml_to_vcard(input, inpsize, output, outpsize, free_input, co= nfig, error, VFORMAT_CARD_30); +} + +static osync_bool conv_xml_to_vcard21(char *input, unsigned int inpsize, c= har **output, unsigned int *outpsize, osync_bool *free_input, const char *c= onfig, OSyncError **error) +{ + return conv_xml_to_vcard(input, inpsize, output, outpsize, free_input, co= nfig, error, VFORMAT_CARD_21); +} + +static OSyncConvCmpResult compare_contact(const char *leftdata, unsigned i= nt leftsize, const char *rightdata, unsigned int rightsize) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p, %i)", __func__, leftdata, leftsi= ze, rightdata, rightsize); +=09 + OSyncXMLScore score[] =3D + { + //{30, "/contact/FullName"}, + {100, "/contact/Name"}, + //{20, "/contact/Telephone"}, + //{20, "/contact/Address"}, + //{1, "/contact/UnknownNode"}, + {0, "/contact/*/Slot"}, + {0, "/contact/*/Type"}, + {0, "/contact/WantsHtml"}, + {0, "/contact/Class"}, + {0, "/contact/FileAs"}, + {0, "/contact/Uid"}, + {0, "/contact/Revision"}, + {0, NULL} + }; +=09 + OSyncConvCmpResult ret =3D osxml_compare((xmlDoc*)leftdata, (xmlDoc*)righ= tdata, score, 0, 99); +=09 + osync_trace(TRACE_EXIT, "%s: %i", __func__, ret); + return ret; +} + +static char *print_contact(const char *data, unsigned int size) +{ + xmlDoc *doc =3D (xmlDoc *)data; +=09 + return osxml_write_to_string(doc); +} + +static void destroy_xml(char *input, size_t inpsize) +{ + xmlFreeDoc((xmlDoc *)input); +} + +static time_t get_revision(const char *input, unsigned int inpsize, OSyncE= rror **error) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, input, inpsize, erro= r); +=09 + xmlDoc *doc =3D (xmlDoc *)input; =09 xmlXPathObject *xobj =3D osxml_get_nodeset(doc, "/contact/Revision"); =09 @@ -982,38 +953,73 @@ return time; } =20 -void get_format_info(OSyncFormatEnv *env) -{ - OSyncError *error =3D NULL; -=09 - OSyncObjFormat *format =3D osync_objformat_new("xml-contact", "contact", = &error); - if (!format) { - osync_trace(TRACE_ERROR, "Unable to register format xml-contact: %s", os= ync_error_print(&error)); - osync_error_unref(&error); - return; - } +osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *format =3D osync_objformat_new("xml-contact", "contact", = error); + if (!format) + return FALSE; =09 osync_objformat_set_compare_func(format, compare_contact); osync_objformat_set_destroy_func(format, destroy_xml); osync_objformat_set_print_func(format, print_contact); osync_objformat_set_copy_func(format, osxml_copy); osync_objformat_set_revision_func(format, get_revision); - osync_objformat_set_marshall_func(format, osxml_marshall); - osync_objformat_set_demarshall_func(format, osxml_demarshall); + osync_objformat_set_marshal_func(format, osxml_marshal); + osync_objformat_set_demarshal_func(format, osxml_demarshal); +=09 osync_format_env_register_objformat(env, format); -} - -void get_conversion_info(OSyncFormatEnv *env) -{ -=09 -=09 - osync_env_register_converter(env, CONVERTER_CONV, "vcard21", "xml-contact= ", conv_vcard_to_xml); - osync_env_converter_set_init(env, "vcard21", "xml-contact", init_vcard_to= _xml, fin_vcard_to_xml); - osync_env_register_converter(env, CONVERTER_CONV, "xml-contact", "vcard21= ", conv_xml_to_vcard21); - osync_env_converter_set_init(env, "xml-contact", "vcard21", init_xml_to_v= card, fin_xml_to_vcard); -=09 - osync_env_register_converter(env, CONVERTER_CONV, "vcard30", "xml-contact= ", conv_vcard_to_xml); - osync_env_converter_set_init(env, "vcard30", "xml-contact", init_vcard_to= _xml, fin_vcard_to_xml); - osync_env_register_converter(env, CONVERTER_CONV, "xml-contact", "vcard30= ", conv_xml_to_vcard30); - osync_env_converter_set_init(env, "xml-contact", "vcard30", init_xml_to_v= card, fin_xml_to_vcard); -} + osync_objformat_unref(format); + return TRUE; +} + +osync_bool get_conversion_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *xml =3D osync_format_env_find_objformat(env, "xml-contact= "); + if (!xml) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find xml contact = format"); + return FALSE; + } +=09 + OSyncObjFormat *vcard21 =3D osync_format_env_find_objformat(env, "vcard21= "); + if (!vcard21) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find vcard21 form= at"); + return FALSE; + } +=09 + OSyncObjFormat *vcard30 =3D osync_format_env_find_objformat(env, "vcard30= "); + if (!vcard30) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find vcard30 form= at"); + return FALSE; + } +=09 + OSyncFormatConverter *conv =3D osync_converter_new(OSYNC_CONVERTER_CONV, = vcard21, xml, conv_vcard_to_xml, error); + if (!conv) + return FALSE; + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); +=09 + conv =3D osync_converter_new(OSYNC_CONVERTER_CONV, vcard30, xml, conv_vca= rd_to_xml, error); + if (!conv) + return FALSE; + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); +=09 + conv =3D osync_converter_new(OSYNC_CONVERTER_CONV, xml, vcard21, conv_xml= _to_vcard21, error); + if (!conv) + return FALSE; + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); +=09 + conv =3D osync_converter_new(OSYNC_CONVERTER_CONV, xml, vcard30, conv_xml= _to_vcard30, error); + if (!conv) + return FALSE; + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); +=09 + return TRUE; +} + +int get_version(void) +{ + return 1; +} |