static gint replace_record (GnomePilotConduitSyncAbs *conduit, EAddrLocalRecord *local, GnomePilotRecord *remote, EAddrConduitContext *ctxt) { EContact *new_contact; EBookChange *ebc; char *old_id; int retval = 0; g_return_val_if_fail (remote != NULL, -1); LOG (g_message ("replace_record: replace %s with %s\n", print_local (local), print_remote (remote))); old_id = e_contact_get (local->contact, E_CONTACT_UID); ebc = g_hash_table_lookup (ctxt->changed_hash, old_id); new_contact = ecard_from_remote_record (ctxt, remote, local->contact); g_object_unref (local->contact); local->contact = new_contact; if (ebc && ebc->change_type == E_BOOK_CHANGE_CARD_DELETED) { if (!e_book_add_contact (ctxt->ebook, local->contact, NULL)) { WARN (G_STRLOC ": failed to add card\n"); return -1; } } else { if (!e_book_commit_contact (ctxt->ebook, local->contact, NULL)) { WARN (G_STRLOC ": failed to commit card\n"); return -1; } } /* Adding a record causes wombat to assign a new uid so we must tidy */ if (ebc && ebc->change_type == E_BOOK_CHANGE_CARD_DELETED) { const char *uid = e_contact_get_const (local->contact, E_CONTACT_UID); gboolean arch; arch = e_pilot_map_uid_is_archived (ctxt->map, uid); e_pilot_map_insert (ctxt->map, remote->ID, uid, arch); ebc = g_hash_table_lookup (ctxt->changed_hash, old_id); if (ebc) { g_hash_table_remove (ctxt->changed_hash, old_id); g_object_unref (ebc->contact); g_object_ref (local->contact); ebc->contact = local->contact; /* FIXME We should possibly be duplicating the uid */ g_hash_table_insert (ctxt->changed_hash, (gpointer) uid, ebc); } } return retval; }
static void contacts_edit_ok_cb (GtkWidget *button, ContactsData *data) { GError *error = NULL; if (data->contact && data->changed) { /* Clean contact */ contacts_clean_contact (data->contact); /* Commit changes */ e_book_commit_contact(data->book, data->contact, &error); if (error) { GtkWidget *dialog; GtkWidget *toplevel = gtk_widget_get_toplevel (button); dialog = gtk_message_dialog_new ( GTK_WINDOW (toplevel), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Couldn't update contact") ); gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG (dialog), _("Information couldn't be updated, error was: %s"), error->message ); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); g_warning ("Cannot commit contact: %s", error->message); g_error_free (error); } } data->changed = FALSE; contacts_edit_pane_hide (data); }
void bbdb_sync_buddy_list (void) { GList *blist, *l; EBook *book = NULL; /* Get the Gaim buddy list */ blist = bbdb_get_gaim_buddy_list (); if (blist == NULL) return; /* Open the addressbook */ book = bbdb_open_addressbook (GAIM_ADDRESSBOOK); if (book == NULL) { free_buddy_list (blist); return; } printf ("bbdb: Synchronizing buddy list to contacts...\n"); /* Walk the buddy list */ for (l = blist; l != NULL; l = l->next) { GaimBuddy *b = l->data; EBookQuery *query; GList *contacts; GError *error = NULL; EContact *c; if (b->alias == NULL || strlen (b->alias) == 0) b->alias = b->account_name; /* Look for an exact match full name == buddy alias */ query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, b->alias); e_book_get_contacts (book, query, &contacts, NULL); e_book_query_unref (query); if (contacts != NULL) { /* FIXME: If there's more than one contact with this name, just give up; we're not smart enough for this. */ if (contacts->next != NULL) continue; c = E_CONTACT (contacts->data); if (! bbdb_merge_buddy_to_contact (book, b, c)) continue; /* Write it out to the addressbook */ if (! e_book_commit_contact (book, c, &error)) { g_warning ("bbdb: Could not modify contact: %s\n", error->message); g_error_free (error); } continue; } /* Otherwise, create a new contact. */ c = e_contact_new (); e_contact_set (c, E_CONTACT_FULL_NAME, (gpointer) b->alias); if (! bbdb_merge_buddy_to_contact (book, b, c)) { g_object_unref (G_OBJECT (c)); continue; } if (! e_book_add_contact (book, c, &error)) { g_warning ("bbdb: Failed to add new contact: %s\n", error->message); g_error_free (error); return; } g_object_unref (G_OBJECT (c)); } /* Update the last-sync'd time */ { GConfClient *gconf; time_t last_sync; char *last_sync_str; gconf = gconf_client_get_default (); time (&last_sync); last_sync_str = g_strdup_printf ("%ld", (glong) last_sync); gconf_client_set_string (gconf, GCONF_KEY_GAIM_LAST_SYNC, last_sync_str, NULL); g_free (last_sync_str); g_object_unref (G_OBJECT (gconf)); } printf ("bbdb: Done syncing buddy list to contacts.\n"); }