1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/* evolution-python: Python bindings to libecal and libebook
* Copyright (c) 2007 John Stowers <john.stowers@gmail.com>
*
* 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 Library 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., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/
#include "evo-addressbook.h"
#include "evo-contact.h"
static EContactField search_fields[] = { E_CONTACT_FULL_NAME, E_CONTACT_EMAIL, E_CONTACT_NICKNAME, 0 };
static int n_search_fields = G_N_ELEMENTS (search_fields) - 1;
/**
* Split a string of tokens separated by whitespace into an array of tokens.
*
* Code taken from deskbar
*/
static GArray *
split_query_string (const gchar *str)
{
GArray *parts = g_array_sized_new (FALSE, FALSE, sizeof (char *), 2);
PangoLogAttr *attrs;
guint str_len = strlen (str), word_start = 0, i;
attrs = g_new0 (PangoLogAttr, str_len + 1);
/* TODO: do we need to specify a particular language or is NULL ok? */
pango_get_log_attrs (str, -1, -1, NULL, attrs, str_len + 1);
for (i = 0; i < str_len + 1; i++) {
char *start_word, *end_word, *word;
if (attrs[i].is_word_end) {
start_word = g_utf8_offset_to_pointer (str, word_start);
end_word = g_utf8_offset_to_pointer (str, i);
word = g_strndup (start_word, end_word - start_word);
g_array_append_val (parts, word);
}
if (attrs[i].is_word_start) {
word_start = i;
}
}
g_free (attrs);
return parts;
}
/**
* Create a query which looks for the specified string in a contact's full name, email addresses and
* nick name.
*
* Taken from deskbar
*/
static EBookQuery *
create_query (const char* s)
{
EBookQuery *query;
GArray *parts = split_query_string (s);
EBookQuery ***field_queries;
EBookQuery **q;
guint j;
int i;
q = g_new0 (EBookQuery *, n_search_fields);
field_queries = g_new0 (EBookQuery **, n_search_fields);
for (i = 0; i < n_search_fields; i++) {
field_queries[i] = g_new0 (EBookQuery *, parts->len);
for (j = 0; j < parts->len; j++) {
field_queries[i][j] = e_book_query_field_test (search_fields[i], E_BOOK_QUERY_CONTAINS, g_array_index (parts, gchar *, j));
}
q[i] = e_book_query_and (parts->len, field_queries[i], TRUE);
}
g_array_free (parts, TRUE);
query = e_book_query_or (n_search_fields, q, TRUE);
for (i = 0; i < n_search_fields; i++) {
g_free (field_queries[i]);
}
g_free (field_queries);
g_free (q);
return query;
}
EBook *
evo_addressbook_open(const char *uri)
{
GError *gerror = NULL;
ESourceList *sources = NULL;
ESource *source = NULL;
EBook *addressbook = NULL;
g_return_val_if_fail(uri != NULL, FALSE);
if (strcmp(uri, "default")) {
if (!e_book_get_addressbooks(&sources, NULL)) {
g_warning("Error getting addressbooks: %s", gerror ? gerror->message : "None");
g_clear_error(&gerror);
return NULL;
}
if (!(source = evo_environment_find_source(sources, uri))) {
g_warning("Error finding source \"%s\"", uri);
return NULL;
}
if (!(addressbook = e_book_new(source, &gerror))) {
g_warning("Failed to alloc new addressbook: %s", gerror ? gerror->message : "None");
g_clear_error(&gerror);
return NULL;
}
} else {
if (!(addressbook = e_book_new_default_addressbook(&gerror))) {
g_warning("Failed to alloc new default addressbook: %s", gerror ? gerror->message : "None");
g_clear_error(&gerror);
return NULL;
}
}
if (!e_book_open(addressbook, TRUE, &gerror)) {
g_warning("Failed to alloc new addressbook: %s", gerror ? gerror->message : "None");
g_clear_error(&gerror);
g_object_unref(addressbook);
return NULL;
}
return addressbook;
}
gboolean
evo_addressbook_get_changed_contacts(EBook *addressbook, GList **added, GList **modified, GList **deleted, char *change_id)
{
EContact *contact = NULL;
GList *changes = NULL;
EBookChange *ebc = NULL;
GList *l = NULL;
char *uid = NULL;
g_return_val_if_fail(change_id != NULL, FALSE);
if (!e_book_get_changes(addressbook, change_id, &changes, NULL)) {
g_warning("Unable to open changed contacts");
return FALSE;
}
for (l = changes; l; l = l->next)
{
ebc = (EBookChange *)l->data;
contact = (EContact *)ebc->contact;
uid = e_contact_get(contact, E_CONTACT_UID);
switch (ebc->change_type)
{
case E_BOOK_CHANGE_CARD_ADDED:
*added = g_list_prepend(*added, contact);
g_debug("Added");
break;
case E_BOOK_CHANGE_CARD_MODIFIED:
*modified = g_list_prepend(*modified, contact);
g_debug("Modified");
break;
case E_BOOK_CHANGE_CARD_DELETED:
*deleted = g_list_prepend(*deleted, contact);
g_debug("Deleted");
break;
}
g_free(uid);
}
g_debug("Changes: %i added, %i modified, %i deleted\n",
g_list_length(*added),
g_list_length(*modified),
g_list_length(*deleted)
);
return TRUE;
}
/*
* Note: you may get a message "WARNING **: FIXME: wait for completion unimplemented"
* if you call search_sync but are not running the gobject main loop.
* This appears to be harmless: http://bugzilla.gnome.org/show_bug.cgi?id=314544
*/
GList *
evo_addressbook_free_text_search(EBook *book, const char *query)
{
GList *contacts = NULL;
EBookQuery *book_query = create_query (query);
e_book_get_contacts (book, book_query, &contacts, NULL);
e_book_query_unref (book_query);
return contacts;
}
gboolean
evo_addressbook_contact_exists(EBook *book, EContact *contact)
{
GError *err = NULL;
GList *contacts = NULL;
int found = FALSE;
EBookQuery *query;
char *uid = NULL;
g_return_val_if_fail(contact != NULL, FALSE);
/* query by uid */
uid = (char *)e_contact_get(contact, E_CONTACT_UID);
if (!uid)
return FALSE;
query = e_book_query_field_test(E_CONTACT_UID, E_BOOK_QUERY_IS, uid);
/* ignore errors, but cancellation errors we don't try to go further either */
if (!e_book_get_contacts(book, query, &contacts, &err)) {
g_warning("Can't get contacts: %s", err->message);
g_clear_error(&err);
}
if (contacts != NULL) {
found = TRUE;
g_list_foreach(contacts, (GFunc)g_object_unref, NULL);
g_list_free(contacts);
}
e_book_query_unref(query);
return found;
}
gboolean
evo_addressbook_remove_contact(EBook *book, EContact *contact)
{
int found = FALSE;
const char *uid = NULL;
GError *err = NULL;
found = evo_addressbook_contact_exists(book, contact);
if (found) {
uid = e_contact_get_const(contact, E_CONTACT_UID);
if (e_book_remove_contact(book, uid, &err)) {
return TRUE;
} else {
g_warning("Error removing contact: %s", err->message);
g_clear_error(&err);
}
}
return FALSE;
}
EContact *
evo_addressbook_get_contact(EBook *book, const char *uid)
{
EContact *contact = NULL;
GError *error = NULL;
if (!e_book_get_contact (book, uid, &contact, &error)) {
g_warning("error %d getting card: %s\n", error->code, error->message);
g_clear_error (&error);
return NULL;
}
return contact;
}
char *
evo_addressbook_add_contact(EBook *book, EContact *contact)
{
GError *error = NULL;
if (!e_book_add_contact(book, contact, &error)) {
g_warning("error adding contact: %s\n", error->message);
g_clear_error (&error);
return NULL;
}
return evo_contact_get_uid(contact);
}
char *
evo_addressbook_get_uid(EBook *book)
{
ESource *source = NULL;
const char *uid = NULL;
source = e_book_get_source(book);
if (source)
uid = e_source_peek_uid(source);
if (uid)
return g_strdup(uid);
return NULL;
}
|