* mu-contacts: cosmetics

This commit is contained in:
Dirk-Jan C. Binnema 2011-05-18 19:19:08 +03:00
parent bb18f312d6
commit 75e7daac65
2 changed files with 26 additions and 18 deletions

View File

@ -45,7 +45,7 @@ struct _MuContacts {
static gboolean static gboolean
unserialize_cache (MuContacts *self) deserialize_cache (MuContacts *self)
{ {
GError *err; GError *err;
gchar **groups; gchar **groups;
@ -69,12 +69,16 @@ unserialize_cache (MuContacts *self)
groups = g_key_file_get_groups (self->_ccache, &len); groups = g_key_file_get_groups (self->_ccache, &len);
for (i = 0; i != len; ++i) { for (i = 0; i != len; ++i) {
ContactInfo *cinfo; ContactInfo *cinfo;
cinfo = contact_info_new (/* note, contact_info_new will *own* the string param, cinfo = contact_info_new (/* note, contact_info_new
* and take care of freeing it */ * will *own* the string
* param, and take care of
* freeing it */
g_key_file_get_string (self->_ccache, groups[i], g_key_file_get_string (self->_ccache, groups[i],
MU_CONTACTS_NAME_KEY, NULL), MU_CONTACTS_NAME_KEY, NULL),
(time_t)g_key_file_get_integer (self->_ccache, groups[i], (time_t)g_key_file_get_integer (self->_ccache, groups[i],
MU_CONTACTS_TIMESTAMP_KEY, NULL)); MU_CONTACTS_TIMESTAMP_KEY,
NULL));
/* note, we're using the groups[i], so don't free with g_strfreev */ /* note, we're using the groups[i], so don't free with g_strfreev */
g_hash_table_insert (self->_hash, groups[i], cinfo); g_hash_table_insert (self->_hash, groups[i], cinfo);
} }
@ -103,7 +107,7 @@ mu_contacts_new (const gchar *ccachefile)
(g_str_hash, g_str_equal, (g_str_hash, g_str_equal,
g_free, (GDestroyNotify)contact_info_destroy); g_free, (GDestroyNotify)contact_info_destroy);
unserialize_cache (contacts); deserialize_cache (contacts);
MU_WRITE_LOG("unserialized contacts cache %s", ccachefile); MU_WRITE_LOG("unserialized contacts cache %s", ccachefile);
contacts->_dirty = FALSE; contacts->_dirty = FALSE;
@ -126,7 +130,8 @@ mu_contacts_add (MuContacts *self, const char* name, const char *email,
cinfo = (ContactInfo*) g_hash_table_lookup (self->_hash, email); cinfo = (ContactInfo*) g_hash_table_lookup (self->_hash, email);
if (!cinfo || if (!cinfo ||
(cinfo->_tstamp < tstamp && !mu_str_is_empty(name))) { (cinfo->_tstamp < tstamp && !mu_str_is_empty(name))) {
ContactInfo *ci; /* note ci will take care of freeing the first param */ ContactInfo *ci; /* note ci will take care of freeing
* the first param */
ci = contact_info_new (name ? g_strdup(name) : NULL, tstamp); ci = contact_info_new (name ? g_strdup(name) : NULL, tstamp);
g_hash_table_insert (self->_hash, g_strdup(email), ci); g_hash_table_insert (self->_hash, g_strdup(email), ci);
return self->_dirty = TRUE; return self->_dirty = TRUE;
@ -144,20 +149,20 @@ struct _EachContactData {
typedef struct _EachContactData EachContactData; typedef struct _EachContactData EachContactData;
static void /* email will never be NULL, but ci->_name may be */ static void /* email will never be NULL, but ci->_name may be */
each_contact (const char* email, ContactInfo *ci, EachContactData *ecdata) each_contact (const char *email, ContactInfo *ci, EachContactData *ecdata)
{ {
/* ignore this contact if we have a regexp, and it matches /* ignore this contact if we have a regexp, and it matches
* neither email nor name (if we have a name) */ * neither email nor name (if we have a name) */
while (ecdata->_rx) { /* note, only once */ while (ecdata->_rx) { /* note, only once */
if (g_regex_match (ecdata->_rx, email, 0, NULL)) if (g_regex_match (ecdata->_rx, email, 0, NULL))
break; /* email matches? continue! */ break; /* email matches? continue! */
if (!ci->_name) if (!ci->_name)
return; /* email did not match, no name? ignore this one */ return; /* email did not match, no name? ignore this one */
if (g_regex_match (ecdata->_rx,ci->_name, 0, NULL)) if (g_regex_match (ecdata->_rx, ci->_name, 0, NULL))
break; /* name matches? continue! */ break; /* name matches? continue! */
return; /* nothing matched, ignore this one */ return; /* nothing matched, ignore this one */
} }
ecdata->_func (email, ci->_name, ci->_tstamp, ecdata->_user_data); ecdata->_func (email, ci->_name, ci->_tstamp, ecdata->_user_data);
++ecdata->_num; ++ecdata->_num;
} }
@ -190,7 +195,7 @@ mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func,
ecdata._user_data = user_data; ecdata._user_data = user_data;
ecdata._num = 0; ecdata._num = 0;
g_hash_table_foreach (self->_hash, (GHFunc) each_contact, &ecdata); g_hash_table_foreach (self->_hash, (GHFunc)each_contact, &ecdata);
if (ecdata._rx) if (ecdata._rx)
g_regex_unref (ecdata._rx); g_regex_unref (ecdata._rx);
@ -202,12 +207,15 @@ mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func,
} }
static void static void
each_keyval (const char *email, ContactInfo *cinfo, MuContacts *self) each_keyval (const char *email, ContactInfo *cinfo, MuContacts *self)
{ {
/* TODO: we use 'email' as key here; however, this fails for
* technically valid e-mail addresses containing '='
* ('smith=cool@domain.com') or '[]' (jones@[192.168.255.2]),
* because GKeyFile does not like '=' or '[]' in its group
* names. Maybe use a hash as the key, and 'email' as a
* value */
if (cinfo->_name) if (cinfo->_name)
g_key_file_set_string (self->_ccache, email, "name", g_key_file_set_string (self->_ccache, email, "name",
cinfo->_name); cinfo->_name);

View File

@ -28,7 +28,6 @@ G_BEGIN_DECLS
struct _MuContacts; struct _MuContacts;
typedef struct _MuContacts MuContacts; typedef struct _MuContacts MuContacts;
/** /**
* create a new MuContacts object; use mu_contacts_destroy when you no longer need it * create a new MuContacts object; use mu_contacts_destroy when you no longer need it
* *
@ -52,7 +51,8 @@ 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* name, const char *email, gboolean mu_contacts_add (MuContacts *contacts, const char* name,
const char *email,
time_t tstamp); time_t tstamp);
/** /**
@ -67,8 +67,8 @@ void mu_contacts_destroy (MuContacts *contacts);
* name (which may be NULL) and the timestamp for the address * name (which may be NULL) and the timestamp for the address
* *
*/ */
typedef void (*MuContactsForeachFunc) (const char *email, const char *name, time_t tstamp, typedef void (*MuContactsForeachFunc) (const char *email, const char *name,
gpointer user_data); time_t tstamp, gpointer user_data);
/** /**
* call a function for either each contact, or each contact satisfying * call a function for either each contact, or each contact satisfying