diff options
author | Christopher James Lahey <clahey@helixcode.com> | 2000-04-05 04:58:46 +0800 |
---|---|---|
committer | Chris Lahey <clahey@src.gnome.org> | 2000-04-05 04:58:46 +0800 |
commit | d8db781adee587ed1fc5b948a68d1d0a7c2952c2 (patch) | |
tree | e2003adb7f8e6dbf4ec917adf95c7f594113ce39 /addressbook/demo/e-test-model.c | |
parent | 2d75a701029288c953a84ba0a696675f5a7eb77f (diff) | |
download | gsoc2013-evolution-d8db781adee587ed1fc5b948a68d1d0a7c2952c2.tar.gz gsoc2013-evolution-d8db781adee587ed1fc5b948a68d1d0a7c2952c2.tar.zst gsoc2013-evolution-d8db781adee587ed1fc5b948a68d1d0a7c2952c2.zip |
Changed this to backend to an ebook.
2000-04-04 Christopher James Lahey <clahey@helixcode.com>
* addressbook/demo/e-test-model.c,
addressbook/demo/e-test-model.h, addressbook/demo/Makefile.am:
Changed this to backend to an ebook.
* addressbook/backend/ebook/e-card-iterator.c,
addressbook/backend/ebook/e-card-iterator.h,
addressbook/backend/ebook/e-card-list-iterator.c,
addressbook/backend/ebook/e-card-list.c,
addressbook/backend/ebook/e-card-list.h,
addressbook/backend/ebook/e-card.c,
addressbook/backend/ebook/e-card.h: Fixed const correctness and
changed a couple of functions to be external.
* addressbook/Makefile.am: Fixed subdir ordering.
svn path=/trunk/; revision=2287
Diffstat (limited to 'addressbook/demo/e-test-model.c')
-rw-r--r-- | addressbook/demo/e-test-model.c | 312 |
1 files changed, 200 insertions, 112 deletions
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); } |