diff options
Diffstat (limited to 'addressbook/demo')
-rw-r--r-- | addressbook/demo/Makefile.am | 12 | ||||
-rw-r--r-- | addressbook/demo/e-test-model.c | 312 | ||||
-rw-r--r-- | addressbook/demo/e-test-model.h | 20 |
3 files changed, 219 insertions, 125 deletions
diff --git a/addressbook/demo/Makefile.am b/addressbook/demo/Makefile.am index 4a7f39a503..84869488cc 100644 --- a/addressbook/demo/Makefile.am +++ b/addressbook/demo/Makefile.am @@ -6,6 +6,7 @@ INCLUDES = \ -I$(top_srcdir)/widgets/e-table \ -I$(top_srcdir) \ -I$(top_srcdir)/widgets/e-minicard \ + -I$(top_srcdir)/addressbook/backend/ebook \ $(BONOBO_HTML_GNOME_CFLAGS) \ -DEVOLUTION_VERSION=\""$(VERSION)"\" \ -DEVOLUTION_GLADEDIR=\""$(gladedir)"\" \ @@ -23,10 +24,13 @@ test_addressbook_SOURCES = \ test_addressbook_LDADD = \ $(EXTRA_GNOME_LIBS) \ + $(BONOBO_HTML_GNOME_LIBS) \ $(top_builddir)/widgets/e-minicard/libeminicard.a \ $(top_builddir)/widgets/e-table/libetable.a \ $(top_builddir)/widgets/e-text/libetext.a \ - $(top_builddir)/e-util/libeutil.la + $(top_builddir)/e-util/libeutil.la \ + $(top_builddir)/addressbook/backend/ebook/libebook.la \ + $(top_builddir)/libversit/libversit.la test_addressbook_LDFLAGS = `gnome-config --libs gdk_pixbuf` @@ -43,12 +47,14 @@ evolution_addressbook_SOURCES = \ e-test-model.h evolution_addressbook_LDADD = \ - $(EXTRA_GNOME_LIBS) \ + $(EXTRA_GNOME_LIBS) \ $(BONOBO_HTML_GNOME_LIBS) \ $(top_builddir)/widgets/e-minicard/libeminicard.a \ $(top_builddir)/widgets/e-table/libetable.a \ $(top_builddir)/widgets/e-text/libetext.a \ - $(top_builddir)/e-util/libeutil.la + $(top_builddir)/e-util/libeutil.la \ + $(top_builddir)/addressbook/backend/ebook/libebook.la \ + $(top_builddir)/libversit/libversit.la evolution_addressbook_LDFLAGS = `gnome-config --libs gdk_pixbuf` diff --git a/addressbook/demo/e-test-model.c b/addressbook/demo/e-test-model.c index c218283e48..941e1c006f 100644 --- a/addressbook/demo/e-test-model.c +++ b/addressbook/demo/e-test-model.c @@ -17,7 +17,7 @@ #define PARENT_TYPE e_table_model_get_type() /* * ETestModel callbacks - * These are the callbacks that define the behavior of our custom model. +n * These are the callbacks that define the behavior of our custom model. */ static void @@ -25,15 +25,15 @@ test_destroy(GtkObject *object) { ETestModel *model = E_TEST_MODEL(object); int i; + if (model->book) + gtk_object_unref(GTK_OBJECT(model->book)); + if (model->book_view) + gtk_object_unref(GTK_OBJECT(model->book_view)); for ( i = 0; i < model->data_count; i++ ) { - g_free(model->data[i]->email); - g_free(model->data[i]->full_name); - g_free(model->data[i]->street); - g_free(model->data[i]->phone); - g_free(model->data[i]); + gtk_object_unref(GTK_OBJECT(model->data[i])); } g_free(model->data); - g_free(model->filename); + g_free(model->uri); } /* This function returns the number of columns in our ETableModel. */ @@ -56,17 +56,54 @@ static void * test_value_at (ETableModel *etc, int col, int row) { ETestModel *test = E_TEST_MODEL(etc); + ECardList *list; + ECardIterator *iterator; + gchar *string; if ( col >= LAST_COL || row >= test->data_count ) return NULL; switch (col) { case EMAIL: - return test->data[row]->email; + gtk_object_get(GTK_OBJECT(test->data[row]), + "email", &list, + NULL); + iterator = e_card_list_get_iterator(list); + if (e_card_iterator_get(iterator)) + return (void *) e_card_iterator_get(iterator); + else + return ""; + gtk_object_unref(GTK_OBJECT(iterator)); + break; case FULL_NAME: - return test->data[row]->full_name; + gtk_object_get(GTK_OBJECT(test->data[row]), + "full_name", &string, + NULL); + if (string) + return string; + else + return ""; + break; case STREET: - return test->data[row]->street; + gtk_object_get(GTK_OBJECT(test->data[row]), + "street", &list, + NULL); + iterator = e_card_list_get_iterator(list); + if (e_card_iterator_get(iterator)) + return ((ECardDeliveryAddress *)e_card_iterator_get(iterator))->street; + else + return ""; + gtk_object_unref(GTK_OBJECT(iterator)); + break; case PHONE: - return test->data[row]->phone; + gtk_object_get(GTK_OBJECT(test->data[row]), + "phone", &list, + NULL); + iterator = e_card_list_get_iterator(list); + if (e_card_iterator_get(iterator)) + return ((ECardPhone *)e_card_iterator_get(iterator))->number; + else + return ""; + gtk_object_unref(GTK_OBJECT(iterator)); + break; default: return NULL; } @@ -77,28 +114,80 @@ static void test_set_value_at (ETableModel *etc, int col, int row, const void *val) { ETestModel *test = E_TEST_MODEL(etc); + ECardList *list; + ECardIterator *iterator; if ( col >= LAST_COL || row >= test->data_count ) return; switch (col) { case EMAIL: - g_free (test->data[row]->email); - test->data[row]->email = g_strdup (val); + gtk_object_get(GTK_OBJECT(test->data[row]), + "email", &list, + NULL); + iterator = e_card_list_get_iterator(list); + if (e_card_iterator_is_valid(iterator)) { + e_card_iterator_set(iterator, val); + } else { + e_card_list_append(list, val); + } + gtk_object_unref(GTK_OBJECT(iterator)); break; case FULL_NAME: - g_free (test->data[row]->full_name); - test->data[row]->full_name = g_strdup (val); + gtk_object_set(GTK_OBJECT(test->data[row]), + "full_name", val, + NULL); break; case STREET: - g_free (test->data[row]->street); - test->data[row]->street = g_strdup (val); + gtk_object_get(GTK_OBJECT(test->data[row]), + "address", &list, + NULL); + iterator = e_card_list_get_iterator(list); + if (e_card_iterator_is_valid(iterator)) { + const ECardDeliveryAddress *address = e_card_iterator_get(iterator); + ECardDeliveryAddress *address_copy = e_card_delivery_address_copy(address); + g_free(address_copy->street); + address_copy->street = g_strdup(val); + e_card_iterator_set(iterator, address_copy); + e_card_delivery_address_free(address_copy); + } else { + ECardDeliveryAddress *address = g_new(ECardDeliveryAddress, 1); + address->po = NULL; + address->ext = NULL; + address->street = g_strdup(val); + address->city = NULL; + address->region = NULL; + address->code = NULL; + address->country = NULL; + address->flags = 0; + e_card_list_append(list, address); + e_card_delivery_address_free(address); + } + gtk_object_unref(GTK_OBJECT(iterator)); break; case PHONE: - g_free (test->data[row]->phone); - test->data[row]->phone = g_strdup (val); + gtk_object_get(GTK_OBJECT(test->data[row]), + "phone", &list, + NULL); + iterator = e_card_list_get_iterator(list); + if (e_card_iterator_is_valid(iterator)) { + const ECardPhone *phone = e_card_iterator_get(iterator); + ECardPhone *phone_copy = e_card_phone_copy(phone); + g_free(phone_copy->number); + phone_copy->number = g_strdup(val); + e_card_iterator_set(iterator, phone_copy); + e_card_phone_free(phone_copy); + } else { + ECardPhone *phone = g_new(ECardPhone, 1); + phone->number = g_strdup(val); + phone->flags = 0; + e_card_list_append(list, phone); + e_card_phone_free(phone); + } + gtk_object_unref(GTK_OBJECT(iterator)); break; default: return; } + e_book_commit_card(test->book, test->data[row], NULL, NULL); if ( !etc->frozen ) e_table_model_cell_changed(etc, col, row); } @@ -155,7 +244,8 @@ e_test_model_init (GtkObject *object) ETestModel *model = E_TEST_MODEL(object); model->data = NULL; model->data_count = 0; - model->idle = 0; + model->book = NULL; + model->book_view = NULL; } GtkType @@ -181,117 +271,115 @@ e_test_model_get_type (void) return type; } -static gboolean -save(gpointer data) +void +e_test_model_add_column (ETestModel *model, Address *newadd) { - int i; - xmlDoc *document = xmlNewDoc("1.0"); - xmlNode *root; - ETestModel *model = data; +#if 0 + model->data = g_realloc(model->data, (++model->data_count) * sizeof(Address *)); + model->data[model->data_count - 1] = newadd; + e_test_model_queue_save(model); + if ( model && !E_TABLE_MODEL(model)->frozen ) + e_table_model_changed(E_TABLE_MODEL(model)); +#endif +} - root = xmlNewDocNode(document, NULL, "address-book", NULL); - xmlDocSetRootElement(document, root); - for ( i = 0; i < model->data_count; i++ ) { - xmlNode *xml_address = xmlNewChild(root, NULL, "address", NULL); - if ( model->data[i]->email && *model->data[i]->email ) - xmlSetProp(xml_address, "email", model->data[i]->email); - if ( model->data[i]->email && *model->data[i]->street ) - xmlSetProp(xml_address, "street", model->data[i]->street); - if ( model->data[i]->email && *model->data[i]->full_name ) - xmlSetProp(xml_address, "full-name", model->data[i]->full_name); - if ( model->data[i]->email && *model->data[i]->phone ) - xmlSetProp(xml_address, "phone", model->data[i]->phone); +static void +e_test_model_card_added(EBookView *book_view, + const GList *cards, + ETestModel *model) +{ + model->data = g_realloc(model->data, (model->data_count + g_list_length((GList *)cards)) * sizeof(ECard *)); + for ( ; cards; cards = cards->next) { + gtk_object_ref(GTK_OBJECT(cards->data)); + model->data[model->data_count++] = E_CARD (cards->data); } - xmlSaveFile (model->filename, document); - model->idle = 0; - gtk_object_unref(GTK_OBJECT(model)); - /* e_table_save_specification(E_TABLE(e_table), "spec"); */ - return FALSE; + e_table_model_changed(E_TABLE_MODEL(model)); } -void -e_test_model_queue_save(ETestModel *model) +static void +e_test_model_card_removed(EBookView *book_view, + const char *id, + ETestModel *model) { - if ( !model->idle ) { - gtk_object_ref(GTK_OBJECT(model)); - model->idle = g_idle_add(save, model); + int i; + for ( i = 0; i < model->data_count; i++) { + if ( !strcmp(e_card_get_id(model->data[i]), id) ) { + gtk_object_unref(GTK_OBJECT(model->data[i])); + memmove(model->data + i, model->data + i + 1, (model->data_count - i - 1) * sizeof (ECard *)); + } } + e_table_model_changed(E_TABLE_MODEL(model)); } -void -e_test_model_add_column (ETestModel *model, Address *newadd) +static void +e_test_model_card_changed(EBookView *book_view, + const GList *cards, + ETestModel *model) { - model->data = g_realloc(model->data, (++model->data_count) * sizeof(Address *)); - model->data[model->data_count - 1] = newadd; - e_test_model_queue_save(model); - if ( model && !E_TABLE_MODEL(model)->frozen ) - e_table_model_changed(E_TABLE_MODEL(model)); + for ( ; cards; cards = cards->next) { + int i; + for ( i = 0; i < model->data_count; i++) { + if ( !strcmp(e_card_get_id(model->data[i]), e_card_get_id(E_CARD(cards->data))) ) { + gtk_object_unref(GTK_OBJECT(model->data[i])); + model->data[i] = E_CARD(cards->data); + gtk_object_ref(GTK_OBJECT(model->data[i])); + e_table_model_row_changed(E_TABLE_MODEL(model), i); + break; + } + } + } } +static void +e_test_model_book_respond_get_view(EBook *book, + EBookStatus status, + EBookView *book_view, + ETestModel *model) +{ + if (status == E_BOOK_STATUS_SUCCESS) { + model->book_view = book_view; + gtk_object_ref(GTK_OBJECT(book_view)); + gtk_signal_connect(GTK_OBJECT(book_view), + "card_changed", + GTK_SIGNAL_FUNC(e_test_model_card_changed), + model); + gtk_signal_connect(GTK_OBJECT(book_view), + "card_removed", + GTK_SIGNAL_FUNC(e_test_model_card_removed), + model); + gtk_signal_connect(GTK_OBJECT(book_view), + "card_added", + GTK_SIGNAL_FUNC(e_test_model_card_added), + model); + } +} + +static void +e_test_model_uri_loaded(EBook *book, + EBookStatus status, + ETestModel *model) +{ + if (status == E_BOOK_STATUS_SUCCESS) { + e_book_get_book_view (book, + "", + (EBookBookViewCallback) e_test_model_book_respond_get_view, + model); + } +} ETableModel * -e_test_model_new (gchar *filename) +e_test_model_new (gchar *uri) { ETestModel *et; - xmlDoc *document; - xmlNode *xml_addressbook; - xmlNode *xml_address; et = gtk_type_new (e_test_model_get_type ()); - - /* First we fill in the simple data. */ - if ( g_file_exists(filename) ) { - e_table_model_freeze(E_TABLE_MODEL(et)); - document = xmlParseFile(filename); - xml_addressbook = xmlDocGetRootElement(document); - for (xml_address = xml_addressbook->childs; xml_address; xml_address = xml_address->next) { - char *datum; - Address *newadd; - - newadd = g_new(Address, 1); - - datum = xmlGetProp(xml_address, "email"); - if ( datum ) { - newadd->email = g_strdup(datum); - xmlFree(datum); - } else - newadd->email = g_strdup(""); - - datum = xmlGetProp(xml_address, "street"); - if ( datum ) { - newadd->street = g_strdup(datum); - xmlFree(datum); - } else - newadd->street = g_strdup(""); - - datum = xmlGetProp(xml_address, "full-name"); - if ( datum ) { - newadd->full_name = g_strdup(datum); - xmlFree(datum); - } else - newadd->full_name = g_strdup(""); - - datum = xmlGetProp(xml_address, "phone"); - if ( datum ) { - newadd->phone = g_strdup(datum); - xmlFree(datum); - } else - newadd->phone = g_strdup(""); - e_test_model_add_column (et, newadd); - } - xmlFreeDoc(document); - e_table_model_thaw(E_TABLE_MODEL(et)); - } - - et->filename = g_strdup(filename); - - gtk_signal_connect(GTK_OBJECT(et), "model_changed", - GTK_SIGNAL_FUNC(e_test_model_queue_save), NULL); - gtk_signal_connect(GTK_OBJECT(et), "model_row_changed", - GTK_SIGNAL_FUNC(e_test_model_queue_save), NULL); - gtk_signal_connect(GTK_OBJECT(et), "model_cell_changed", - GTK_SIGNAL_FUNC(e_test_model_queue_save), NULL); + et->uri = g_strdup(uri); + et->book = e_book_new(); + e_book_load_uri(et->book, + et->uri, + (EBookCallback) e_test_model_uri_loaded, + et); return E_TABLE_MODEL(et); } diff --git a/addressbook/demo/e-test-model.h b/addressbook/demo/e-test-model.h index dd3f8e0fa9..871ea11de8 100644 --- a/addressbook/demo/e-test-model.h +++ b/addressbook/demo/e-test-model.h @@ -3,6 +3,9 @@ #define _E_TEST_MODEL_H_ #include "e-table-model.h" +#include <e-book.h> +#include <e-book-view.h> +#include <e-card.h> #define E_TEST_MODEL_TYPE (e_test_model_get_type ()) #define E_TEST_MODEL(o) (GTK_CHECK_CAST ((o), E_TEST_MODEL_TYPE, ETestModel)) @@ -19,7 +22,6 @@ typedef struct _Address Address; typedef enum _Rows Rows; - struct _Address { gchar *email; gchar *full_name; @@ -35,16 +37,17 @@ enum _Rows { LAST_COL }; - - typedef struct { ETableModel parent; - Address **data; + EBook *book; + + EBookView *book_view; + + ECard **data; int data_count; - char *filename; - int idle; + char *uri; } ETestModel; @@ -54,11 +57,8 @@ typedef struct { GtkType e_test_model_get_type (void); -ETableModel *e_test_model_new (char *filename); +ETableModel *e_test_model_new (char *uri); -void e_test_model_queue_save(ETestModel *model); void e_test_model_add_column (ETestModel *model, Address *newadd); - #endif /* _E_TEST_MODEL_H_ */ - |