1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* A class to cache address book conents on local file system
4 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6 * Authors: Devashish Sharma <sdevashish@novell.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "e-book-backend-db-cache.h"
29 #include "e-book-backend.h"
30 #include "e-book-backend-sexp.h"
33 string_to_dbt (const gchar *str,
36 memset (dbt, 0, sizeof (DBT));
37 dbt->data = (gpointer) str;
38 dbt->size = strlen (str) + 1;
39 dbt->flags = DB_DBT_USERMEM;
43 get_filename_from_uri (const gchar *uri)
45 const gchar *user_cache_dir;
46 gchar *mangled_uri, *filename;
48 user_cache_dir = e_get_user_cache_dir ();
50 /* Mangle the URI to not contain invalid characters. */
51 mangled_uri = g_strdelimit (g_strdup (uri), ":/", '_');
53 filename = g_build_filename (
54 user_cache_dir, "addressbook",
55 mangled_uri, "cache.db", NULL);
63 * e_book_backend_db_cache_set_filename:
65 * @filename: filename to be set
67 * Set the filename for db cacahe file.
71 e_book_backend_db_cache_set_filename (DB *db,
72 const gchar *filename)
74 DBT uid_dbt, vcard_dbt;
77 string_to_dbt ("filename", &uid_dbt);
78 string_to_dbt (filename, &vcard_dbt);
80 db_error = db->put (db, NULL, &uid_dbt, &vcard_dbt, 0);
82 g_warning ("db->put failed with %d", db_error);
88 * e_book_backend_db_cache_get_filename:
91 * Get the filename for db cacahe file.
95 e_book_backend_db_cache_get_filename (DB *db)
97 DBT uid_dbt, vcard_dbt;
101 string_to_dbt ("filename", &uid_dbt);
102 memset (&vcard_dbt, 0 , sizeof (vcard_dbt));
103 vcard_dbt.flags = DB_DBT_MALLOC;
105 db_error = db->get (db, NULL, &uid_dbt, &vcard_dbt, 0);
107 g_warning ("db-<get failed with %d", db_error);
111 filename = g_strdup (vcard_dbt.data);
112 g_free (vcard_dbt.data);
118 * e_book_backend_db_cache_get_contact:
120 * @uid: a unique contact ID
122 * Get a cached contact. Note that the returned #EContact will be
123 * newly created, and must be unreffed by the caller when no longer
126 * Returns: A cached #EContact, or %NULL if @uid is not cached.
129 e_book_backend_db_cache_get_contact (DB *db,
132 DBT uid_dbt, vcard_dbt;
134 EContact *contact = NULL;
136 g_return_val_if_fail (uid != NULL, NULL);
138 string_to_dbt (uid, &uid_dbt);
139 memset (&vcard_dbt, 0 , sizeof (vcard_dbt));
140 vcard_dbt.flags = DB_DBT_MALLOC;
142 db_error = db->get (db, NULL, &uid_dbt, &vcard_dbt,0);
144 g_warning ("db->get failed with %d", db_error);
148 contact = e_contact_new_from_vcard_with_uid ((const gchar *) vcard_dbt.data, uid);
149 g_free (vcard_dbt.data);
154 * e_book_backend_db_cache_add_contact:
156 * @contact: an #EContact
158 * Adds @contact to @cache.
160 * Returns: %TRUE if the contact was cached successfully, %FALSE otherwise.
163 e_book_backend_db_cache_add_contact (DB *db,
166 DBT uid_dbt, vcard_dbt;
171 uid = e_contact_get_const (contact, E_CONTACT_UID);
174 printf("name:%s, email:%s\n",
175 (gchar *) e_contact_get (contact, E_CONTACT_GIVEN_NAME),
176 (gchar *) e_contact_get (contact, E_CONTACT_EMAIL_1));
179 string_to_dbt (uid, &uid_dbt);
181 vcard_str = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
182 string_to_dbt (vcard_str, &vcard_dbt);
184 /* db_error = db->del (db, NULL, &uid_dbt, 0); */
185 db_error = db->put (db, NULL, &uid_dbt, &vcard_dbt, 0);
190 g_warning ("db->put failed with %d", db_error);
198 * e_book_backend_db_cache_remove_contact:
200 * @uid: a unique contact ID
202 * Removes the contact identified by @uid from @cache.
204 * Returns: %TRUE if the contact was found and removed, %FALSE otherwise.
207 e_book_backend_db_cache_remove_contact (DB *db,
214 g_return_val_if_fail (uid != NULL, FALSE);
216 string_to_dbt (uid, &uid_dbt);
217 db_error = db->del (db, NULL, &uid_dbt, 0);
220 g_warning ("db->del failed with %d", db_error);
229 * e_book_backend_db_cache_check_contact:
231 * @uid: a unique contact ID
233 * Checks if the contact identified by @uid exists in @cache.
235 * Returns: %TRUE if the cache contains the contact, %FALSE otherwise.
238 e_book_backend_db_cache_check_contact (DB *db,
241 DBT uid_dbt, vcard_dbt;
244 g_return_val_if_fail (uid != NULL, FALSE);
246 string_to_dbt (uid, &uid_dbt);
247 memset (&vcard_dbt, 0 , sizeof (vcard_dbt));
248 vcard_dbt.flags = DB_DBT_MALLOC;
250 db_error = db->get (db, NULL, &uid_dbt, &vcard_dbt,0);
254 free (vcard_dbt.data);
260 * e_book_backend_db_cache_get_contacts:
262 * @query: an s-expression
264 * Returns a list of #EContact elements from @cache matching @query.
265 * When done with the list, the caller must unref the contacts and
268 * Returns: A #GList of pointers to #EContact.
271 e_book_backend_db_cache_get_contacts (DB *db,
275 DBT uid_dbt, vcard_dbt;
278 EBookBackendSExp *sexp = NULL;
282 sexp = e_book_backend_sexp_new (query);
287 db_error = db->cursor (db, NULL, &dbc, 0);
289 g_warning ("db->cursor failed with %d", db_error);
291 g_object_unref (sexp);
295 memset (&vcard_dbt, 0 , sizeof (vcard_dbt));
296 memset (&uid_dbt, 0, sizeof (uid_dbt));
297 db_error = dbc->c_get (dbc, &uid_dbt, &vcard_dbt, DB_FIRST);
299 while (db_error == 0) {
300 if (vcard_dbt.data && !strncmp (vcard_dbt.data, "BEGIN:VCARD", 11)) {
301 contact = e_contact_new_from_vcard (vcard_dbt.data);
303 if (!sexp || e_book_backend_sexp_match_contact (sexp, contact))
304 list = g_list_prepend (list, contact);
306 g_object_unref (contact);
308 db_error = dbc->c_get (dbc, &uid_dbt, &vcard_dbt, DB_NEXT);
311 db_error = dbc->c_close (dbc);
313 g_warning ("db->c_close failed with %d", db_error);
316 g_object_unref (sexp);
318 return g_list_reverse (list);
322 * e_book_backend_db_cache_search:
324 * @query: an s-expression
326 * Returns an array of pointers to unique contact ID strings for contacts
327 * in @cache matching @query. When done with the array, the caller must
328 * free the ID strings and the array.
330 * Returns: A #GPtrArray of pointers to contact ID strings.
333 e_book_backend_db_cache_search (DB *db,
336 GList *matching_contacts, *temp;
337 GPtrArray *ptr_array;
339 matching_contacts = e_book_backend_db_cache_get_contacts (db, query);
340 ptr_array = g_ptr_array_new ();
342 temp = matching_contacts;
343 for (; matching_contacts != NULL; matching_contacts = g_list_next (matching_contacts)) {
344 g_ptr_array_add (ptr_array, e_contact_get (matching_contacts->data, E_CONTACT_UID));
345 g_object_unref (matching_contacts->data);
353 * e_book_backend_db_cache_exists:
354 * @uri: URI for the cache
356 * Checks if an #EBookBackendCache exists at @uri.
358 * Returns: %TRUE if cache exists, %FALSE if not.
361 e_book_backend_db_cache_exists (const gchar *uri)
364 gboolean exists = FALSE;
365 file_name = get_filename_from_uri (uri);
367 if (file_name && g_file_test (file_name, G_FILE_TEST_EXISTS))
376 * e_book_backend_db_cache_set_populated:
379 * Flags @cache as being populated - that is, it is up-to-date on the
380 * contents of the book it's caching.
383 e_book_backend_db_cache_set_populated (DB *db)
385 DBT uid_dbt, vcard_dbt;
388 string_to_dbt ("populated", &uid_dbt);
389 string_to_dbt ("TRUE", &vcard_dbt);
390 db_error = db->put (db, NULL, &uid_dbt, &vcard_dbt, 0);
392 g_warning ("db->put failed with %d", db_error);
398 * e_book_backend_cache_is_populated:
401 * Checks if @cache is populated.
403 * Returns: %TRUE if @cache is populated, %FALSE otherwise.
406 e_book_backend_db_cache_is_populated (DB *db)
408 DBT uid_dbt, vcard_dbt;
411 string_to_dbt ("populated", &uid_dbt);
412 memset (&vcard_dbt, 0, sizeof (vcard_dbt));
413 vcard_dbt.flags = DB_DBT_MALLOC;
415 db_error = db->get (db, NULL, &uid_dbt, &vcard_dbt, 0);
420 free (vcard_dbt.data);
426 * e_book_backend_db_cache_set_time:
431 e_book_backend_db_cache_set_time (DB *db,
434 DBT uid_dbt, vcard_dbt;
437 string_to_dbt ("last_update_time", &uid_dbt);
438 string_to_dbt (t, &vcard_dbt);
440 db_error = db->put (db, NULL, &uid_dbt, &vcard_dbt, 0);
442 g_warning ("db->put failed with %d", db_error);
447 * e_book_backend_db_cache_get_time:
452 e_book_backend_db_cache_get_time (DB *db)
454 DBT uid_dbt, vcard_dbt;
458 string_to_dbt ("last_update_time", &uid_dbt);
459 memset (&vcard_dbt, 0, sizeof (vcard_dbt));
460 vcard_dbt.flags = DB_DBT_MALLOC;
462 db_error = db->get (db, NULL, &uid_dbt, &vcard_dbt, 0);
464 g_warning ("db->get failed with %d", db_error);
466 t = g_strdup (vcard_dbt.data);
467 free (vcard_dbt.data);