From: <svn...@op...> - 2009-09-05 12:35:02
|
Author: dgollub Date: Sat Sep 5 14:34:42 2009 New Revision: 5750 URL: http://www.opensync.org/changeset/5750 Log: PoC of caps-converter in vformat plugin for capabilities conversion from plain vcard fieldnames to xmlformat(-contact) refs #1146 Modified: format-plugins/vformat/src/vformat-xmlformat.c format-plugins/vformat/src/xmlformat-common.h format-plugins/vformat/src/xmlformat-vcard.c format-plugins/vformat/src/xmlformat-vcard.h Modified: format-plugins/vformat/src/vformat-xmlformat.c ============================================================================== --- format-plugins/vformat/src/vformat-xmlformat.c Sat Sep 5 14:21:58 2009 (r5749) +++ format-plugins/vformat/src/vformat-xmlformat.c Sat Sep 5 14:34:42 2009 (r5750) @@ -204,7 +204,16 @@ osync_format_env_register_converter(env, conv, &error); osync_converter_unref(conv); + /** Register Caps Converter */ + OSyncCapsConverter *caps_converter = osync_caps_converter_new("vcard30", "xmlformat", caps_conv_vcard_to_xmlformat, &error); + if (!caps_converter) + goto error; + return TRUE; + +error: + osync_trace(TRACE_ERROR, "%s", osync_error_print(&error)); + return FALSE; } #endif // BUILD_XMLFORMAT_VCARD Modified: format-plugins/vformat/src/xmlformat-common.h ============================================================================== --- format-plugins/vformat/src/xmlformat-common.h Sat Sep 5 14:21:58 2009 (r5749) +++ format-plugins/vformat/src/xmlformat-common.h Sat Sep 5 14:34:42 2009 (r5750) @@ -30,6 +30,7 @@ #include <opensync/opensync-xmlformat.h> #include <opensync/opensync-format.h> #include <opensync/opensync-time.h> +#include <opensync/opensync-capabilities.h> #include "vformat.h" Modified: format-plugins/vformat/src/xmlformat-vcard.c ============================================================================== --- format-plugins/vformat/src/xmlformat-vcard.c Sat Sep 5 14:21:58 2009 (r5749) +++ format-plugins/vformat/src/xmlformat-vcard.c Sat Sep 5 14:34:42 2009 (r5750) @@ -1938,3 +1938,97 @@ return conv_xmlformat_to_vcard(input, inpsize, output, outpsize, free_input, config, error, VFORMAT_CARD_21); } +/* + * Caps converter + * */ + +static GHashTable* get_vcard_hash() +{ + osync_trace(TRACE_ENTRY, "%s", __func__); + GHashTable *hash = g_hash_table_new(g_str_hash, g_str_equal); + + g_hash_table_insert(hash, "BEGIN", ""); + g_hash_table_insert(hash, "VERSION", ""); + g_hash_table_insert(hash, "END", ""); + + g_hash_table_insert(hash, "ADR", "Address"); + g_hash_table_insert(hash, "AGENT", "Agent"); + g_hash_table_insert(hash, "BDAY", "Birthday"); + g_hash_table_insert(hash, "CATEGORIES", "Categories"); + g_hash_table_insert(hash, "CLASS", "Class"); + g_hash_table_insert(hash, "EMAIL", "EMail"); + g_hash_table_insert(hash, "FN", "FormattedName"); + g_hash_table_insert(hash, "GEO", "Location"); + g_hash_table_insert(hash, "KEY", "Key"); + g_hash_table_insert(hash, "LABEL", "AddressLabel"); + g_hash_table_insert(hash, "LOGO", "Logo"); + g_hash_table_insert(hash, "MAILER", "Mailer"); + g_hash_table_insert(hash, "N", "Name"); + g_hash_table_insert(hash, "NICKNAME", "Nickname"); + g_hash_table_insert(hash, "NOTE", "Note"); + g_hash_table_insert(hash, "ORG", "Organization"); + g_hash_table_insert(hash, "PHOTO", "Photo"); + g_hash_table_insert(hash, "REV", "Revision"); + g_hash_table_insert(hash, "ROLE", "Role"); + g_hash_table_insert(hash, "SOUND", "Sound"); + g_hash_table_insert(hash, "TEL", "Telephone"); + g_hash_table_insert(hash, "TITLE", "Title"); + g_hash_table_insert(hash, "TZ", "Timezone"); + g_hash_table_insert(hash, "UID", "Uid"); + g_hash_table_insert(hash, "URL", "Url"); + + osync_trace(TRACE_EXIT, "%s", __func__); + return hash; +} + +osync_bool caps_conv_generic(OSyncCapabilities *oldcaps, OSyncCapabilities *newcaps, const char *objtype, GHashTable *hash, OSyncError **error) +{ + + OSyncCapabilitiesObjType *newcapsobjtype, *capsobjtype = osync_capabilities_get_objtype(oldcaps, objtype); + OSyncList *c, *oldcapslist = osync_capabilities_objtype_get_caps(capsobjtype); + + newcapsobjtype = osync_capabilities_objtype_new(newcaps, objtype, error); + if (!newcapsobjtype) + goto error; + + for (c = oldcapslist; c; c = c->next) { + OSyncCapability *oldcap = (OSyncCapability *) c->data; + const char *name = osync_capability_get_name(oldcap); + const char *new_name = g_hash_table_lookup(hash, name); + if (!new_name) { + osync_trace(TRACE_INTERNAL, "Couldn't find counter-part for capability \"%s\"", __NULLSTR(name)); + continue; + } + + OSyncCapability *newcap = osync_capability_new(newcapsobjtype, error); + if (!newcap) + goto error; + + osync_capability_set_name(newcap, new_name); + } + + + return TRUE; +error: + return FALSE; +} + +osync_bool caps_conv_vcard_to_xmlformat(OSyncCapabilities *oldcaps, OSyncCapabilities **newcaps, const char *config, void *userdata, OSyncError **error) +{ + + GHashTable *vcard_hash = get_vcard_hash(); + + + *newcaps = osync_capabilities_new("xmlformat" ,error); + if (!*newcaps) + goto error; + + if (!caps_conv_generic(oldcaps, *newcaps, "contact", vcard_hash, error)) + goto error; + + return TRUE; + +error: + return FALSE; +} + Modified: format-plugins/vformat/src/xmlformat-vcard.h ============================================================================== --- format-plugins/vformat/src/xmlformat-vcard.h Sat Sep 5 14:21:58 2009 (r5749) +++ format-plugins/vformat/src/xmlformat-vcard.h Sat Sep 5 14:34:42 2009 (r5750) @@ -41,6 +41,6 @@ osync_bool conv_xmlformat_to_vcard21(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error); - +osync_bool caps_conv_vcard_to_xmlformat(OSyncCapabilities *oldcaps, OSyncCapabilities **newcaps, const char *config, void *userdata, OSyncError **error); #endif /*XMLFORMAT_VCARD_H_*/ |