* mu-contacts: plug a leak + some optimization

This commit is contained in:
Dirk-Jan C. Binnema 2011-03-06 12:16:04 +02:00
parent 1a504b50a4
commit 3b445d9fac
1 changed files with 14 additions and 8 deletions

View File

@ -34,7 +34,7 @@ struct _ContactInfo {
typedef struct _ContactInfo ContactInfo; typedef struct _ContactInfo ContactInfo;
static void contact_info_destroy (ContactInfo *cinfo); static void contact_info_destroy (ContactInfo *cinfo);
static ContactInfo *contact_info_new (const char *name, time_t tstamp); static ContactInfo *contact_info_new (char *name, time_t tstamp);
struct _MuContacts { struct _MuContacts {
GKeyFile *_ccache; GKeyFile *_ccache;
@ -69,7 +69,8 @@ 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 ( cinfo = contact_info_new (/* note, contact_info_new 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),
g_key_file_get_uint64 (self->_ccache, groups[i], g_key_file_get_uint64 (self->_ccache, groups[i],
@ -125,8 +126,9 @@ 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))) {
g_hash_table_insert (self->_hash, g_strdup(email), ContactInfo *ci; /* note ci will take care of freeing the first param */
contact_info_new (name, tstamp)); ci = contact_info_new (name ? g_strdup(name) : NULL, tstamp);
g_hash_table_insert (self->_hash, g_strdup(email), ci);
return self->_dirty = TRUE; return self->_dirty = TRUE;
} }
@ -247,11 +249,13 @@ mu_contacts_destroy (MuContacts *self)
if (self->_ccache && self->_dirty) { if (self->_ccache && self->_dirty) {
serialize_cache (self); serialize_cache (self);
g_key_file_free (self->_ccache);
MU_WRITE_LOG("serialized contacts cache %s", MU_WRITE_LOG("serialized contacts cache %s",
self->_ccachefile); self->_ccachefile);
} }
if (self->_ccache)
g_key_file_free (self->_ccache);
g_free (self->_ccachefile); g_free (self->_ccachefile);
if (self->_hash) if (self->_hash)
@ -261,13 +265,15 @@ mu_contacts_destroy (MuContacts *self)
} }
/* note, we will *own* the name we get */
static ContactInfo * static ContactInfo *
contact_info_new (const char *name, time_t tstamp) contact_info_new (char *name, time_t tstamp)
{ {
ContactInfo *cinfo; ContactInfo *cinfo;
cinfo = g_slice_new (ContactInfo); cinfo = g_slice_new (ContactInfo);
cinfo->_name = name ? g_strdup (name) : NULL; /* removing leading, trailing whitespace from names */
cinfo->_name = name ? g_strstrip(name) : NULL;
cinfo->_tstamp = tstamp; cinfo->_tstamp = tstamp;
return cinfo; return cinfo;