/* mutt-eds-query.c * Sertaç Ö. Yıldız * Paul W. Frields * (updates for EDS >= 3.6.0) * * compilation (requires evolution-data-server development libraries): * gcc `pkg-config --cflags --libs libebook-1.2` -o mutt-eds-query mutt-eds-query.c */ #include #include #if EDS_CHECK_VERSION(3,6,0) #include #include #else #include #include #include #endif #define PROGRAM_NAME "mutt-eds-query" #define VERSION "0.2" static void print_mutt_line (const gchar *email, const gchar *name, const gchar *nick, const gchar *notes) { printf ("%s\t%s\t%-10s %s\n", email ? email : "", name ? name : "", nick ? nick : "", notes ? notes: ""); } static void process_results (gpointer data, gpointer user_data) { EContact *contact; const gchar *fullname, *nickname, *notes; GList *emails, *e; g_return_if_fail (E_IS_CONTACT(data)); contact = E_CONTACT (data); fullname = e_contact_get_const (contact, E_CONTACT_FULL_NAME); nickname = e_contact_get_const (contact, E_CONTACT_NICKNAME); notes = e_contact_get_const (contact, E_CONTACT_NOTE); emails = e_contact_get (contact, E_CONTACT_EMAIL); for (e = emails; e; e = e->next) print_mutt_line ((const char *) e->data, fullname, nickname, notes); g_list_foreach (emails, (GFunc) g_free, NULL); g_list_free (emails); } static void usage (const gchar *me) { fprintf (stderr, "%s (v" VERSION ") : simple e-d-s wrapper for mutt\n", me); fprintf (stderr, "Usage: %s \n\n", me); } int main (int argc, char *argv[]) { #if EDS_CHECK_VERSION(3,6,0) ESourceRegistry *registry; GMainContext *context; ESource *source, *addressbook; EBookClient *client; GSList *contacts; #else EBook *book; GList *contacts; #endif EBookQuery *query; gchar *s; g_type_init(); if ( argc > 1 && g_strstr_len (argv[1], 3, "-h")) { usage (PROGRAM_NAME); exit (1); } if (argc == 1) query = e_book_query_any_field_contains (""); else query = e_book_query_any_field_contains (argv[1]); if (query == NULL) { fprintf (stderr, "Couldn't create query.\n"); exit (2); } #if EDS_CHECK_VERSION(3,6,0) registry = e_source_registry_new_sync (NULL, NULL); if (registry == NULL) { fprintf (stderr, "Couldn't get ESourceRegistry.\n"); exit (2); } source = e_source_registry_ref_builtin_address_book (registry); if (source == NULL) { fprintf (stderr, "Couldn't get ESource.\n"); exit (2); } client = e_book_client_new (source, NULL); if (client == NULL) { fprintf (stderr, "Couldn't get EBookClient.\n"); exit (2); } s = e_book_query_to_string (query); if (!e_book_client_get_contacts_sync (client, s, &contacts, NULL, NULL)) { fprintf (stderr, "Couldn't get query results.\n"); exit (2); } #else book = e_book_new_system_addressbook (NULL); if (!e_book_open (book, TRUE, NULL)) { fprintf (stderr, "Couldn't load system addressbook.\n"); exit (2); } if (!e_book_get_contacts (book, query, &contacts, NULL)) { fprintf (stderr, "Couldn't get query results.\n"); exit (2); } #endif e_book_query_unref (query); #if EDS_CHECK_VERSION(3,6,0) g_object_unref (client); #else g_object_unref (book); #endif if (contacts == NULL) { printf ("No matches.\n"); return (-1); } else { printf ("Matches for '%s'...\n", argv[1]); #if EDS_CHECK_VERSION(3,6,0) g_slist_foreach (contacts, (GFunc) process_results, NULL); #else g_list_foreach (contacts, (GFunc) process_results, NULL); #endif } return 0; }