diff options
Diffstat (limited to 'addressbook/backend/ebook/e-card.c')
-rw-r--r-- | addressbook/backend/ebook/e-card.c | 144 |
1 files changed, 139 insertions, 5 deletions
diff --git a/addressbook/backend/ebook/e-card.c b/addressbook/backend/ebook/e-card.c index 044f27be3b..53ba425a48 100644 --- a/addressbook/backend/ebook/e-card.c +++ b/addressbook/backend/ebook/e-card.c @@ -28,6 +28,7 @@ #include <libversit/vcc.h> #include "e-util/ename/e-name-western.h" #include "e-util/ename/e-address-western.h" +#include "e-book.h" #define is_a_prop_of(obj,prop) (isAPropertyOf ((obj),(prop))) #define str_val(obj) (the_str = (vObjectValueType (obj))? fakeCString (vObjectUStringZValue (obj)) : calloc (1, 1)) @@ -129,6 +130,9 @@ static void set_phone_flags (VObject *vobj, ECardPhoneFlags flags); static ECardAddressFlags get_address_flags (VObject *vobj); static void set_address_flags (VObject *vobj, ECardAddressFlags flags); +static gchar *e_card_uri_extract_book_uri (const gchar *uri); +static gchar *e_card_uri_extract_card_id (const gchar *uri); + typedef void (* ParsePropertyFunc) (ECard *card, VObject *object); struct { @@ -234,6 +238,12 @@ e_card_duplicate(ECard *card) char *vcard = e_card_get_vcard(card); ECard *new_card = e_card_new(vcard); g_free (vcard); + + if (card->book) { + new_card->book = card->book; + gtk_object_ref (GTK_OBJECT (new_card->book)); + } + return new_card; } @@ -306,6 +316,8 @@ e_card_touch(ECard *card) const char * e_card_get_id (ECard *card) { + g_return_val_if_fail (card && E_IS_CARD (card), NULL); + return card->id; } @@ -320,11 +332,34 @@ e_card_get_id (ECard *card) void e_card_set_id (ECard *card, const char *id) { + g_return_if_fail (card && E_IS_CARD (card)); + if ( card->id ) g_free(card->id); card->id = g_strdup(id); } +EBook * +e_card_get_book (ECard *card) +{ + g_return_val_if_fail (card && E_IS_CARD (card), NULL); + + return card->book; +} + +const gchar * +e_card_get_uri (ECard *card) +{ + g_return_val_if_fail (card && E_IS_CARD (card), NULL); + + if (card->uri == NULL && card->id && *card->id && card->book) { + const char *book_uri = e_book_get_uri (card->book); + if (book_uri) + card->uri = g_strdup_printf ("%s/%s", book_uri, card->id); + } + return card->uri; +} + static gchar * e_card_date_to_string (ECardDate *dt) { @@ -341,6 +376,7 @@ static VObject * e_card_get_vobject (ECard *card) { VObject *vobj; + const char *tmp; vobj = newVObject (VCCardProp); @@ -571,8 +607,14 @@ e_card_get_vobject (ECard *card) } } - if (card->id) - addPropValue (vobj, VCUniqueStringProp, card->id); + tmp = e_card_get_uri (card); + if (tmp == NULL) + tmp = card->id; + if (tmp) { + g_message ("unique string = [%s]", tmp); + addPropValue (vobj, VCUniqueStringProp, tmp); + } + #if 0 @@ -1083,9 +1125,24 @@ parse_arbitrary(ECard *card, VObject *vobj) static void parse_id(ECard *card, VObject *vobj) { - if ( card->id ) - g_free(card->id); - assign_string(vobj, &(card->id)); + if ( vObjectValueType (vobj) ) { + gchar *str = fakeCString (vObjectUStringZValue (vobj)); + gchar *id; + if ( card->id ) + g_free(card->id); + if ( card->uri ) + g_free(card->uri); + + id = e_card_uri_extract_card_id (str); + if (id) { + card->id = id; + card->uri = g_strdup (str); + } else { + card->id = g_strdup (str); + card->uri = NULL; + } + free (str); + } } static void @@ -1770,6 +1827,9 @@ e_card_destroy (GtkObject *object) { ECard *card = E_CARD(object); g_free(card->id); + if (card->book) + gtk_object_unref (GTK_OBJECT (card->book)); + g_free(card->uri); g_free(card->file_as); g_free(card->fname); if ( card->name ) @@ -4047,3 +4107,77 @@ e_card_evolution_list_show_addresses (ECard *card) g_return_val_if_fail (card && E_IS_CARD (card), FALSE); return card->list_show_addresses; } + +static gchar * +e_card_uri_extract_book_uri (const gchar *uri) +{ + gchar *lastslash; + + if (uri == NULL) + return NULL; + + lastslash = strrchr (uri, '/'); + if (lastslash == NULL) + return NULL; + + return g_strndup (uri, lastslash - uri); +} + +static gchar * +e_card_uri_extract_card_id (const gchar *uri) +{ + gchar *lastslash; + + if (uri == NULL) + return NULL; + + lastslash = strrchr (uri, '/'); + return lastslash ? g_strdup (lastslash+1) : NULL; +} + +typedef struct _CardLoadData CardLoadData; +struct _CardLoadData { + gchar *card_id; + ECardCallback cb; + gpointer closure; +}; + +static void +card_load_cb (EBook *book, EBookStatus status, gpointer closure) +{ + CardLoadData *data = (CardLoadData *) closure; + ECard *card = NULL; + + if (status == E_BOOK_STATUS_SUCCESS) + card = e_book_get_card (book, data->card_id); + + if (data->cb != NULL) + data->cb (card, data->closure); + + g_free (data->card_id); + g_free (data); +} + +void +e_card_load_uri (const gchar *uri, ECardCallback cb, gpointer closure) +{ + CardLoadData *data; + gchar *book_uri; + gchar *card_id; + EBook *book; + + g_return_if_fail (uri != NULL); + + book_uri = e_card_uri_extract_book_uri (uri); + card_id = e_card_uri_extract_card_id (uri); + + data = g_new (CardLoadData, 1); + data->card_id = g_strdup (card_id); + data->cb = cb; + data->closure = closure; + + book = e_book_new (); + e_book_load_uri (book, book_uri, card_load_cb, data); + + g_free (book_uri); +} |