* mu-contacts: add mu_contacts_clear, whitespace fixes

This commit is contained in:
Dirk-Jan C. Binnema
2011-08-30 22:00:00 +03:00
parent 2c92018484
commit 1c4016b1ba
2 changed files with 100 additions and 63 deletions

View File

@ -43,7 +43,7 @@ static ContactInfo *contact_info_new (char *email, char *name,
struct _MuContacts { struct _MuContacts {
GKeyFile *_ccache; GKeyFile *_ccache;
gchar *_ccachefile; gchar *_path;
GHashTable *_hash; GHashTable *_hash;
gboolean _dirty; gboolean _dirty;
@ -185,34 +185,48 @@ set_comment (GKeyFile *kfile)
MuContacts* MuContacts*
mu_contacts_new (const gchar *ccachefile) mu_contacts_new (const gchar *path)
{ {
MuContacts *self; MuContacts *self;
g_return_val_if_fail (ccachefile, NULL); g_return_val_if_fail (path, NULL);
self = g_new0 (MuContacts, 1); self = g_new0 (MuContacts, 1);
self->_ccachefile = g_strdup (ccachefile); self->_path = g_strdup (path);
self->_ccache = load_key_file (ccachefile);
if (!self->_ccache || !set_comment (self->_ccache)) {
mu_contacts_destroy (self);
return NULL;
}
self->_hash = g_hash_table_new_full self->_hash = g_hash_table_new_full
(g_str_hash, g_str_equal, g_free, (g_str_hash, g_str_equal, g_free,
(GDestroyNotify)contact_info_destroy); (GDestroyNotify)contact_info_destroy);
self->_ccache = load_key_file (path);
if (!self->_ccache || !set_comment (self->_ccache)) {
mu_contacts_destroy (self);
return NULL;
}
deserialize_cache (self); deserialize_cache (self);
self->_dirty = FALSE;
MU_WRITE_LOG("deserialized contacts from cache %s", MU_WRITE_LOG("deserialized contacts from cache %s",
ccachefile); path);
self->_dirty = FALSE;
return self; return self;
} }
void
mu_contacts_clear (MuContacts *self)
{
g_return_if_fail (self);
if (self->_ccache)
g_key_file_free (self->_ccache);
g_hash_table_remove_all (self->_hash);
self->_ccache = g_key_file_new ();
self->_dirty = FALSE;
}
gboolean gboolean
mu_contacts_add (MuContacts *self, const char *email, const char* name, mu_contacts_add (MuContacts *self, const char *email, const char* name,
time_t tstamp) time_t tstamp)
@ -344,10 +358,10 @@ serialize_cache (MuContacts *self)
if (len) { if (len) {
GError *err; GError *err;
err = NULL; err = NULL;
rv = g_file_set_contents (self->_ccachefile, data, len, &err); rv = g_file_set_contents (self->_path, data, len, &err);
if (!rv) { if (!rv) {
g_warning ("failed to serialize cache to %s: %s", g_warning ("failed to serialize cache to %s: %s",
self->_ccachefile, err->message); self->_path, err->message);
g_error_free (err); g_error_free (err);
} }
g_free (data); g_free (data);
@ -365,13 +379,13 @@ mu_contacts_destroy (MuContacts *self)
if (self->_ccache && self->_dirty) { if (self->_ccache && self->_dirty) {
serialize_cache (self); serialize_cache (self);
MU_WRITE_LOG("serialized contacts cache %s", MU_WRITE_LOG("serialized contacts cache %s",
self->_ccachefile); self->_path);
} }
if (self->_ccache) if (self->_ccache)
g_key_file_free (self->_ccache); g_key_file_free (self->_ccache);
g_free (self->_ccachefile); g_free (self->_path);
if (self->_hash) if (self->_hash)
g_hash_table_destroy (self->_hash); g_hash_table_destroy (self->_hash);
@ -380,6 +394,7 @@ mu_contacts_destroy (MuContacts *self)
} }
static void static void
clear_str (char* str) clear_str (char* str)
{ {

View File

@ -53,7 +53,7 @@ MuContacts* mu_contacts_new (const gchar *ccachefile)
* *
* @return TRUE if succeeded, FALSE otherwise * @return TRUE if succeeded, FALSE otherwise
*/ */
gboolean mu_contacts_add (MuContacts *contacts, const char *email, gboolean mu_contacts_add (MuContacts *self, const char *email,
const char* name, time_t tstamp); const char* name, time_t tstamp);
/** /**
@ -61,7 +61,29 @@ gboolean mu_contacts_add (MuContacts *contacts, const char *email,
* *
* @param contacts a contacts object * @param contacts a contacts object
*/ */
void mu_contacts_destroy (MuContacts *contacts); void mu_contacts_destroy (MuContacts *self);
/**
* clear all contacts from the cache
*
* @param self a MuContacts instance
*/
void mu_contacts_clear (MuContacts *self);
/**
* get the path for the contacts cache file
*
* @param contacts a contacts object
*
* @return the path as a constant string (don't free), or NULL in case
* of error
*/
const gchar* mu_contacts_get_path (MuContacts *self);
/** /**
* call called for mu_contacts_foreach; returns the e-mail address, * call called for mu_contacts_foreach; returns the e-mail address,
@ -85,7 +107,7 @@ typedef void (*MuContactsForeachFunc) (const char *email, const char *name,
* @return TRUE if the function succeeded, or FALSE if the provide * @return TRUE if the function succeeded, or FALSE if the provide
* regular expression was invalid (and not NULL) * regular expression was invalid (and not NULL)
*/ */
gboolean mu_contacts_foreach (MuContacts *contacts, MuContactsForeachFunc func, gboolean mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func,
gpointer user_data, const char* pattern, size_t *num); gpointer user_data, const char* pattern, size_t *num);
G_END_DECLS G_END_DECLS