From c64eccfe004f5c30932fe571bb506626bb0c186d Mon Sep 17 00:00:00 2001 From: Iain Holmes Date: Tue, 8 May 2001 22:53:41 +0000 Subject: Importer changes svn path=/trunk/; revision=9722 --- ...Evolution_Addressbook_GnomeCard_Importer.oaf.in | 29 --- ...OME_Evolution_Addressbook_VCard_Importer.oaf.in | 29 +++ addressbook/backend/ebook/Makefile.am | 10 +- .../backend/ebook/evolution-gnomecard-importer.c | 214 ------------------ .../backend/ebook/evolution-vcard-importer.c | 243 +++++++++++++++++++++ 5 files changed, 277 insertions(+), 248 deletions(-) delete mode 100644 addressbook/backend/ebook/GNOME_Evolution_Addressbook_GnomeCard_Importer.oaf.in create mode 100644 addressbook/backend/ebook/GNOME_Evolution_Addressbook_VCard_Importer.oaf.in delete mode 100644 addressbook/backend/ebook/evolution-gnomecard-importer.c create mode 100644 addressbook/backend/ebook/evolution-vcard-importer.c (limited to 'addressbook/backend') diff --git a/addressbook/backend/ebook/GNOME_Evolution_Addressbook_GnomeCard_Importer.oaf.in b/addressbook/backend/ebook/GNOME_Evolution_Addressbook_GnomeCard_Importer.oaf.in deleted file mode 100644 index 69dfa90871..0000000000 --- a/addressbook/backend/ebook/GNOME_Evolution_Addressbook_GnomeCard_Importer.oaf.in +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/addressbook/backend/ebook/GNOME_Evolution_Addressbook_VCard_Importer.oaf.in b/addressbook/backend/ebook/GNOME_Evolution_Addressbook_VCard_Importer.oaf.in new file mode 100644 index 0000000000..9cd4699633 --- /dev/null +++ b/addressbook/backend/ebook/GNOME_Evolution_Addressbook_VCard_Importer.oaf.in @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/addressbook/backend/ebook/Makefile.am b/addressbook/backend/ebook/Makefile.am index da7307c1a5..624d1b072b 100644 --- a/addressbook/backend/ebook/Makefile.am +++ b/addressbook/backend/ebook/Makefile.am @@ -1,6 +1,6 @@ noinst_PROGRAMS = test-card test-client test-client-list -bin_PROGRAMS = evolution-gnomecard-importer \ +bin_PROGRAMS = evolution-vcard-importer \ load-pine-addressbook load-gnomecard-addressbook CORBA_SOURCE = \ @@ -106,9 +106,9 @@ test_card_LDADD = \ $(top_builddir)/libversit/libversit.la \ $(top_builddir)/e-util/libeutil.la -evolution_gnomecard_importer_SOURCES = \ - evolution-gnomecard-importer.c -evolution_gnomecard_importer_LDADD = \ +evolution_vcard_importer_SOURCES = \ + evolution-vcard-importer.c +evolution_vcard_importer_LDADD = \ libebook.la \ $(BONOBO_GNOME_LIBS) \ $(EXTRA_GNOME_LIBS) \ @@ -148,7 +148,7 @@ dist-hook: cd $(distdir); rm -f $(BUILT_SOURCES) oafdir = $(datadir)/oaf -oaf_in_files = GNOME_Evolution_Addressbook_GnomeCard_Importer.oaf.in +oaf_in_files = GNOME_Evolution_Addressbook_VCard_Importer.oaf.in oaf_DATA = $(oaf_in_files:.oaf.in=.oaf) @XML_I18N_MERGE_OAF_RULE@ diff --git a/addressbook/backend/ebook/evolution-gnomecard-importer.c b/addressbook/backend/ebook/evolution-gnomecard-importer.c deleted file mode 100644 index bb5e45cdd6..0000000000 --- a/addressbook/backend/ebook/evolution-gnomecard-importer.c +++ /dev/null @@ -1,214 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include - -#define COMPONENT_FACTORY_IID "OAFIID:GNOME_Evolution_Addressbook_GnomeCard_ImporterFactory" - -static BonoboGenericFactory *factory = NULL; - -typedef struct { - char *filename; - GList *cardlist; - GList *iterator; - EBook *book; - gboolean ready; -} GnomeCardImporter; - -static void -add_card_cb (EBook *book, EBookStatus status, const gchar *id, gpointer closure) -{ - ECard *card = E_CARD(closure); - char *vcard = e_card_get_vcard(card); - g_print ("Saved card: %s\n", vcard); - g_free(vcard); - gtk_object_unref(GTK_OBJECT(card)); -} - -static void -book_open_cb (EBook *book, EBookStatus status, gpointer closure) -{ - GnomeCardImporter *gci = (GnomeCardImporter *) closure; - - gci->cardlist = e_card_load_cards_from_file(gci->filename); - gci->ready = TRUE; -} - -static void -ebook_create (GnomeCardImporter *gci) -{ - gchar *path, *uri; - - gci->book = e_book_new (); - - if (!gci->book) { - printf ("%s: %s(): Couldn't create EBook, bailing.\n", - __FILE__, - __FUNCTION__); - return; - } - - path = g_concat_dir_and_file (g_get_home_dir (), - "evolution/local/Contacts/addressbook.db"); - uri = g_strdup_printf ("file://%s", path); - g_free (path); - - if (! e_book_load_uri (gci->book, uri, book_open_cb, gci)) { - printf ("error calling load_uri!\n"); - } - g_free(uri); -} - -/* EvolutionImporter methods */ -static void -process_item_fn (EvolutionImporter *importer, - CORBA_Object listener, - void *closure, - CORBA_Environment *ev) -{ - GnomeCardImporter *gci = (GnomeCardImporter *) closure; - ECard *card; - - if (gci->iterator == NULL) - gci->iterator = gci->cardlist; - - if (gci->ready == FALSE) { - GNOME_Evolution_ImporterListener_notifyResult (listener, - GNOME_Evolution_ImporterListener_NOT_READY, - gci->iterator ? TRUE : FALSE, - ev); - return; - } - - if (gci->iterator == NULL) { - GNOME_Evolution_ImporterListener_notifyResult (listener, - GNOME_Evolution_ImporterListener_UNSUPPORTED_OPERATION, - FALSE, ev); - return; - } - - card = gci->iterator->data; - e_book_add_card (gci->book, card, add_card_cb, card); - - gci->iterator = gci->iterator->next; - - GNOME_Evolution_ImporterListener_notifyResult (listener, - GNOME_Evolution_ImporterListener_OK, - gci->iterator ? TRUE : FALSE, - ev); - if (ev->_major != CORBA_NO_EXCEPTION) { - g_warning ("Error notifying listeners."); - } - - return; -} - -static char *supported_extensions[3] = { - ".vcf", ".gcrd", NULL -}; - -static gboolean -support_format_fn (EvolutionImporter *importer, - const char *filename, - void *closure) -{ - char *ext; - int i; - - ext = strrchr (filename, '.'); - for (i = 0; supported_extensions[i] != NULL; i++) { - if (strcmp (supported_extensions[i], ext) == 0) - return TRUE; - } - - return FALSE; -} - -static void -importer_destroy_cb (GtkObject *object, - GnomeCardImporter *gci) -{ - gtk_main_quit (); -} - -static gboolean -load_file_fn (EvolutionImporter *importer, - const char *filename, - const char *folderpath, - void *closure) -{ - GnomeCardImporter *gci; - - gci = (GnomeCardImporter *) closure; - gci->filename = g_strdup (filename); - gci->cardlist = NULL; - gci->iterator = NULL; - gci->ready = FALSE; - ebook_create (gci); - - return TRUE; -} - -static BonoboObject * -factory_fn (BonoboGenericFactory *_factory, - void *closure) -{ - EvolutionImporter *importer; - GnomeCardImporter *gci; - - gci = g_new (GnomeCardImporter, 1); - importer = evolution_importer_new (support_format_fn, load_file_fn, - process_item_fn, NULL, gci); - - gtk_signal_connect (GTK_OBJECT (importer), "destroy", - GTK_SIGNAL_FUNC (importer_destroy_cb), gci); - - return BONOBO_OBJECT (importer); -} - -static void -importer_init (void) -{ - if (factory != NULL) - return; - - factory = bonobo_generic_factory_new (COMPONENT_FACTORY_IID, - factory_fn, NULL); - - if (factory == NULL) { - g_error ("Unable to create factory"); - } - - bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory)); -} - -int -main (int argc, - char **argv) -{ - CORBA_ORB orb; - - gnome_init_with_popt_table ("Evolution-GnomeCard-Importer", - "0.0", argc, argv, oaf_popt_options, 0, - NULL); - orb = oaf_init (argc, argv); - if (bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL) == FALSE) { - g_error ("Could not initialize Bonobo."); - } - - importer_init (); - bonobo_main (); - - return 0; -} - - diff --git a/addressbook/backend/ebook/evolution-vcard-importer.c b/addressbook/backend/ebook/evolution-vcard-importer.c new file mode 100644 index 0000000000..2864627a39 --- /dev/null +++ b/addressbook/backend/ebook/evolution-vcard-importer.c @@ -0,0 +1,243 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#define COMPONENT_FACTORY_IID "OAFIID:GNOME_Evolution_Addressbook_VCard_ImporterFactory" + +static BonoboGenericFactory *factory = NULL; + +typedef struct { + char *filename; + GList *cardlist; + GList *iterator; + EBook *book; + gboolean ready; +} VCardImporter; + +static void +add_card_cb (EBook *book, EBookStatus status, const gchar *id, gpointer closure) +{ + ECard *card = E_CARD(closure); + char *vcard = e_card_get_vcard(card); + + g_free(vcard); + gtk_object_unref(GTK_OBJECT(card)); +} + +static void +book_open_cb (EBook *book, EBookStatus status, gpointer closure) +{ + VCardImporter *gci = (VCardImporter *) closure; + + gci->cardlist = e_card_load_cards_from_file(gci->filename); + gci->ready = TRUE; +} + +static void +ebook_create (VCardImporter *gci) +{ + gchar *path, *uri; + + gci->book = e_book_new (); + + if (!gci->book) { + printf ("%s: %s(): Couldn't create EBook, bailing.\n", + __FILE__, + __FUNCTION__); + return; + } + + path = g_concat_dir_and_file (g_get_home_dir (), + "evolution/local/Contacts/addressbook.db"); + uri = g_strdup_printf ("file://%s", path); + g_free (path); + + if (! e_book_load_uri (gci->book, uri, book_open_cb, gci)) { + printf ("error calling load_uri!\n"); + } + g_free(uri); +} + +/* EvolutionImporter methods */ +static void +process_item_fn (EvolutionImporter *importer, + CORBA_Object listener, + void *closure, + CORBA_Environment *ev) +{ + VCardImporter *gci = (VCardImporter *) closure; + ECard *card; + + if (gci->iterator == NULL) + gci->iterator = gci->cardlist; + + if (gci->ready == FALSE) { + GNOME_Evolution_ImporterListener_notifyResult (listener, + GNOME_Evolution_ImporterListener_NOT_READY, + gci->iterator ? TRUE : FALSE, + ev); + return; + } + + if (gci->iterator == NULL) { + GNOME_Evolution_ImporterListener_notifyResult (listener, + GNOME_Evolution_ImporterListener_UNSUPPORTED_OPERATION, + FALSE, ev); + return; + } + + card = gci->iterator->data; + e_book_add_card (gci->book, card, add_card_cb, card); + + gci->iterator = gci->iterator->next; + + GNOME_Evolution_ImporterListener_notifyResult (listener, + GNOME_Evolution_ImporterListener_OK, + gci->iterator ? TRUE : FALSE, + ev); + if (ev->_major != CORBA_NO_EXCEPTION) { + g_warning ("Error notifying listeners."); + } + + return; +} + +static char *supported_extensions[3] = { + ".vcf", ".gcrd", NULL +}; + +/* Actually check the contents of this file */ +static gboolean +check_file_is_vcard (const char *filename) +{ + FILE *handle; + char line[4096]; + gboolean result; + + handle = fopen (filename, "r"); + if (handle == NULL) { + return FALSE; + } + + fgets (line, 4096, handle); + if (line == NULL) { + fclose (handle); + return FALSE; + } + + if (strcmp (line, "BEGIN:VCARD") == 0) { + result = TRUE; + } else { + result = FALSE; + } + + fclose (handle); + return result; +} + +static gboolean +support_format_fn (EvolutionImporter *importer, + const char *filename, + void *closure) +{ + char *ext; + int i; + + ext = strrchr (filename, '.'); + for (i = 0; supported_extensions[i] != NULL; i++) { + if (strcmp (supported_extensions[i], ext) == 0) + return check_file_is_vcard (filename); + } + + return FALSE; +} + +static void +importer_destroy_cb (GtkObject *object, + VCardImporter *gci) +{ + gtk_main_quit (); +} + +static gboolean +load_file_fn (EvolutionImporter *importer, + const char *filename, + const char *folderpath, + void *closure) +{ + VCardImporter *gci; + + gci = (VCardImporter *) closure; + gci->filename = g_strdup (filename); + gci->cardlist = NULL; + gci->iterator = NULL; + gci->ready = FALSE; + ebook_create (gci); + + return TRUE; +} + +static BonoboObject * +factory_fn (BonoboGenericFactory *_factory, + void *closure) +{ + EvolutionImporter *importer; + VCardImporter *gci; + + gci = g_new (VCardImporter, 1); + importer = evolution_importer_new (support_format_fn, load_file_fn, + process_item_fn, NULL, gci); + + gtk_signal_connect (GTK_OBJECT (importer), "destroy", + GTK_SIGNAL_FUNC (importer_destroy_cb), gci); + + return BONOBO_OBJECT (importer); +} + +static void +importer_init (void) +{ + if (factory != NULL) + return; + + factory = bonobo_generic_factory_new (COMPONENT_FACTORY_IID, + factory_fn, NULL); + + if (factory == NULL) { + g_error ("Unable to create factory"); + } + + bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory)); +} + +int +main (int argc, + char **argv) +{ + CORBA_ORB orb; + + gnome_init_with_popt_table ("Evolution-VCard-Importer", + PACKAGE, argc, argv, oaf_popt_options, 0, + NULL); + orb = oaf_init (argc, argv); + if (bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL) == FALSE) { + g_error ("Could not initialize Bonobo."); + } + + importer_init (); + bonobo_main (); + + return 0; +} + + -- cgit v1.2.3