/* diversity-ebook-backend.c - * * Copyright 2008 OpenMoko, Inc. * Authored by Chia-I Wu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include /* for strcmp */ #include #include #include #include #include #include "diversity-jana.h" static struct { DiversityWorld *world; EBook *ebook; EBookView *eview; DiversityJana *jana; } ebook; static void bard_update(DiversityBard *bard, EContact *c) { const gchar *name, *phone; name = e_contact_get_const(c, E_CONTACT_FULL_NAME); if (!name) name = e_contact_get_const(c, E_CONTACT_NICKNAME); phone = e_contact_get_const(c, E_CONTACT_PHONE_MOBILE); if (!phone) phone = e_contact_get_const(c, E_CONTACT_PHONE_PRIMARY); diversity_bard_set_fullname(bard, name); diversity_bard_set_phone(bard, phone); g_print("%s updated: %s (%s)\n", diversity_bard_get_uid(bard), name, phone); } static DiversityBard *bard_new_from_contact(EContact *c) { DiversityBard *bard; const gchar *uid; bard = diversity_bard_new(); uid = e_contact_get_const(c, E_CONTACT_UID); DIVERSITY_OBJECT(bard)->id = g_strdup(uid); diversity_bard_set_uid(bard, uid); bard_update(bard, c); return bard; } static void add_one(EContact *c) { DiversityBard *self, *bard; const gchar *uid; uid = e_contact_get_const(c, E_CONTACT_UID); g_print("add uid %s\n", uid); bard = (DiversityBard *) diversity_world_lookup_object(ebook.world, DIVERSITY_OBJECT_TYPE_BARD, uid); if (bard) return; bard = bard_new_from_contact(c); diversity_world_add_object(ebook.world, DIVERSITY_OBJECT(bard)); self = diversity_world_get_self(ebook.world); diversity_object_connect(DIVERSITY_OBJECT(self), DIVERSITY_OBJECT(bard)); } static void update_one(EContact *c) { DiversityBard *bard; const gchar *uid; uid = e_contact_get_const(c, E_CONTACT_UID); g_print("update uid %s\n", uid); bard = (DiversityBard *) diversity_world_lookup_object(ebook.world, DIVERSITY_OBJECT_TYPE_BARD, uid); if (!bard) { g_warning("update non-existing uid %s", uid); add_one(c); return; } bard_update(bard, c); } static void remove_one(const gchar *uid) { DiversityBard *self, *bard; bard = (DiversityBard *) diversity_world_lookup_object(ebook.world, DIVERSITY_OBJECT_TYPE_BARD, uid); if (!bard) return; self = diversity_world_get_self(ebook.world); diversity_object_disconnect(DIVERSITY_OBJECT(self), DIVERSITY_OBJECT(bard)); diversity_world_remove_object(ebook.world, DIVERSITY_OBJECT(bard)); } static void on_contacts_added(EBookView *view, GList *contacts, gpointer data) { g_print("contacts added\n"); while (contacts) { add_one(E_CONTACT(contacts->data)); contacts = contacts->next; } } static void on_contacts_changed(EBookView *view, GList *contacts, gpointer data) { g_print("contacts changed\n"); while (contacts) { update_one(E_CONTACT(contacts->data)); contacts = contacts->next; } } static void on_contacts_removed(EBookView *view, GList *uids, gpointer data) { g_print("contacts removed\n"); while (uids) { remove_one(uids->data); uids = uids->next; } } static void on_tag_added(DiversityJana *jana, const gchar *uid, gdouble lon, gdouble lat, const gchar *desc) { DiversityObject *tag; tag = diversity_world_lookup_object(ebook.world, DIVERSITY_OBJECT_TYPE_TAG, uid); if (!tag) { tag = (DiversityObject *) diversity_tag_new(); tag->id = g_strdup(uid); diversity_object_set_accuracy(tag, DIVERSITY_OBJECT_ACCURACY_EXACT); diversity_object_geometry_set(tag, lon, lat, 0.0, 0.0); diversity_tag_set(DIVERSITY_TAG(tag), DIVERSITY_TAG_DESCRIPTION, desc); diversity_world_add_object(ebook.world, tag); } else { const gchar *old_desc; gdouble old_lon, old_lat; old_desc = diversity_tag_get(DIVERSITY_TAG(tag), DIVERSITY_TAG_DESCRIPTION); if (old_desc && desc) { if (strcmp(desc, old_desc) != 0) diversity_tag_set(DIVERSITY_TAG(tag), DIVERSITY_TAG_DESCRIPTION, desc); } else if (old_desc != desc) { diversity_tag_set(DIVERSITY_TAG(tag), DIVERSITY_TAG_DESCRIPTION, desc); } diversity_object_geometry_get(tag, &old_lon, &old_lat, NULL, NULL); if (old_lon != lon || old_lat != lat) diversity_object_geometry_set(tag, lon, lat, 0.0, 0.0); } } int ebook_init(DiversityWorld *world, GError **error) { EBookQuery *query; ebook.world = world; g_object_ref(world); ebook.ebook = e_book_new_system_addressbook(error); if (!ebook.ebook) return FALSE; if (!e_book_open(ebook.ebook, FALSE, error)) return FALSE; query = e_book_query_any_field_contains(""); if (e_book_get_book_view(ebook.ebook, query, NULL, -1, &ebook.eview, NULL)) { g_signal_connect(G_OBJECT(ebook.eview), "contacts-added", G_CALLBACK(on_contacts_added), NULL); g_signal_connect(G_OBJECT(ebook.eview), "contacts-changed", G_CALLBACK(on_contacts_changed), NULL); g_signal_connect(G_OBJECT(ebook.eview), "contacts-removed", G_CALLBACK(on_contacts_removed), NULL); e_book_view_start(ebook.eview); } e_book_query_unref(query); ebook.jana = g_object_new(DIVERSITY_TYPE_JANA, NULL); g_signal_connect(ebook.jana, "tag-added", G_CALLBACK(on_tag_added), NULL); diversity_jana_open_and_start(ebook.jana); return TRUE; } void ebook_fini(void) { if (ebook.world) { g_object_unref(G_OBJECT(ebook.world)); ebook.world = NULL; } if (ebook.jana) { g_object_unref(G_OBJECT(ebook.jana)); ebook.jana = NULL; } if (ebook.eview) { g_object_unref(G_OBJECT(ebook.eview)); ebook.eview = NULL; } if (ebook.ebook) { g_object_unref(G_OBJECT(ebook.ebook)); ebook.ebook = NULL; } }