1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Only update a contact if data changed

This commit is contained in:
James Booth 2012-05-24 00:33:23 +01:00
parent 4b4359d09f
commit fef15b932c
6 changed files with 68 additions and 31 deletions

View File

@ -23,6 +23,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <glib.h>
#include "contact.h" #include "contact.h"
struct p_contact_t { struct p_contact_t {
@ -96,16 +98,11 @@ const char * p_contact_status(const PContact contact)
return contact->status; return contact->status;
} }
int p_contact_names_equal(const PContact c1, const PContact c2)
{
return strcmp(c1->name, c2->name);
}
int p_contacts_equal_deep(const PContact c1, const PContact c2) int p_contacts_equal_deep(const PContact c1, const PContact c2)
{ {
int name_eq = strcmp(c1->name, c2->name); int name_eq = (g_strcmp0(c1->name, c2->name) == 0);
int show_eq = strcmp(c1->show, c2->show); int show_eq = (g_strcmp0(c1->show, c2->show) == 0);
int status_eq = strcmp(c1->status, c2->status); int status_eq = (g_strcmp0(c1->status, c2->status) == 0);
return (name_eq && show_eq && status_eq); return (name_eq && show_eq && status_eq);
} }

View File

@ -32,7 +32,6 @@ void p_contact_free(PContact contact);
const char * p_contact_name(PContact contact); const char * p_contact_name(PContact contact);
const char * p_contact_show(PContact contact); const char * p_contact_show(PContact contact);
const char * p_contact_status(PContact contact); const char * p_contact_status(PContact contact);
int p_contact_names_equal(const PContact c1, const PContact c2);
int p_contacts_equal_deep(const PContact c1, const PContact c2); int p_contacts_equal_deep(const PContact c1, const PContact c2);
#endif #endif

View File

@ -36,6 +36,7 @@ void contact_list_init(void)
{ {
ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy, (PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free); (GDestroyNotify)p_contact_free);
} }

View File

@ -34,20 +34,22 @@ struct p_autocomplete_t {
gchar *search_str; gchar *search_str;
PStrFunc str_func; PStrFunc str_func;
PCopyFunc copy_func; PCopyFunc copy_func;
PEqualDeepFunc equal_deep_func;
GDestroyNotify free_func; GDestroyNotify free_func;
}; };
static gchar * _search_from(PAutocomplete ac, GSList *curr); static gchar * _search_from(PAutocomplete ac, GSList *curr);
static const char *_str_func_default(const char *orig); static const char *_str_func_default(const char *orig);
static const char *_copy_func_default(const char *orig); static const char *_copy_func_default(const char *orig);
static int _deep_equals_func_default(const char *o1, const char *o2);
PAutocomplete p_autocomplete_new(void) PAutocomplete p_autocomplete_new(void)
{ {
return p_obj_autocomplete_new(NULL, NULL, NULL); return p_obj_autocomplete_new(NULL, NULL, NULL, NULL);
} }
PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
GDestroyNotify free_func) PEqualDeepFunc equal_deep_func, GDestroyNotify free_func)
{ {
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t)); PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));
new->items = NULL; new->items = NULL;
@ -69,6 +71,11 @@ PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
else else
new->free_func = (GDestroyNotify)free; new->free_func = (GDestroyNotify)free;
if (equal_deep_func)
new->equal_deep_func = equal_deep_func;
else
new->equal_deep_func = (PEqualDeepFunc)_deep_equals_func_default;
return new; return new;
} }
@ -107,9 +114,14 @@ gboolean p_autocomplete_add(PAutocomplete ac, void *item)
// update // update
} else if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) == 0) { } else if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) == 0) {
ac->free_func(curr->data); // only update if data different
curr->data = item; if (!ac->equal_deep_func(curr->data, item)) {
return TRUE; ac->free_func(curr->data);
curr->data = item;
return TRUE;
} else {
return FALSE;
}
} }
curr = g_slist_next(curr); curr = g_slist_next(curr);
@ -233,3 +245,7 @@ static const char *_copy_func_default(const char *orig)
return strdup(orig); return strdup(orig);
} }
static int _deep_equals_func_default(const char *o1, const char *o2)
{
return (strcmp(o1, o2) == 0);
}

View File

@ -33,7 +33,7 @@ typedef int (*PEqualDeepFunc)(const void *o1, const void *o2);
PAutocomplete p_autocomplete_new(void); PAutocomplete p_autocomplete_new(void);
PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
GDestroyNotify free_func); PEqualDeepFunc equal_deep_func, GDestroyNotify free_func);
void p_autocomplete_clear(PAutocomplete ac); void p_autocomplete_clear(PAutocomplete ac);
void p_autocomplete_reset(PAutocomplete ac); void p_autocomplete_reset(PAutocomplete ac);
gboolean p_autocomplete_add(PAutocomplete ac, void *item); gboolean p_autocomplete_add(PAutocomplete ac, void *item);

View File

@ -15,8 +15,11 @@ static void clear_empty(void)
static void clear_empty_with_free_func(void) static void clear_empty_with_free_func(void)
{ {
PAutocomplete ac = p_obj_autocomplete_new(NULL, NULL, PAutocomplete ac =
(GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_clear(ac); p_autocomplete_clear(ac);
} }
@ -46,8 +49,11 @@ static void get_after_create_returns_null(void)
static void get_after_create_with_copy_func_returns_null(void) static void get_after_create_with_copy_func_returns_null(void)
{ {
PAutocomplete ac = p_obj_autocomplete_new(NULL, (PCopyFunc)p_contact_copy, PAutocomplete ac =
(GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
GSList *result = p_autocomplete_get_list(ac); GSList *result = p_autocomplete_get_list(ac);
assert_is_null(result); assert_is_null(result);
@ -70,8 +76,11 @@ static void add_one_and_complete(void)
static void add_one_and_complete_with_funcs(void) static void add_one_and_complete_with_funcs(void)
{ {
PContact contact = p_contact_new("James", "Online", "I'm here"); PContact contact = p_contact_new("James", "Online", "I'm here");
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, PAutocomplete ac =
(GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact); p_autocomplete_add(ac, contact);
char *result = p_autocomplete_complete(ac, "Jam"); char *result = p_autocomplete_complete(ac, "Jam");
@ -98,8 +107,11 @@ static void add_two_and_complete_returns_first_with_funcs(void)
{ {
PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, PAutocomplete ac =
(GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
char *result = p_autocomplete_complete(ac, "Jam"); char *result = p_autocomplete_complete(ac, "Jam");
@ -128,8 +140,11 @@ static void add_two_and_complete_returns_second_with_funcs(void)
{ {
PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, PAutocomplete ac =
(GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
char *result1 = p_autocomplete_complete(ac, "Jam"); char *result1 = p_autocomplete_complete(ac, "Jam");
@ -158,8 +173,11 @@ static void add_two_adds_two_with_funcs(void)
{ {
PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, PAutocomplete ac =
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
GSList *result = p_autocomplete_get_list(ac); GSList *result = p_autocomplete_get_list(ac);
@ -187,8 +205,11 @@ static void add_two_same_adds_one_with_funcs(void)
{ {
PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, PAutocomplete ac =
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
GSList *result = p_autocomplete_get_list(ac); GSList *result = p_autocomplete_get_list(ac);
@ -220,8 +241,11 @@ static void add_two_same_updates_with_funcs(void)
{ {
PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, PAutocomplete ac =
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
GSList *result = p_autocomplete_get_list(ac); GSList *result = p_autocomplete_get_list(ac);