1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Merge branch 'presence'

This commit is contained in:
James Booth 2012-05-26 16:20:03 +01:00
commit 9f5cff096c
11 changed files with 2003 additions and 42 deletions

1
DESIGN
View File

@ -179,4 +179,5 @@ types:
PStrFunc: A function that will get a string out of the data structure PStrFunc: A function that will get a string out of the data structure
PCopyFunc: A function that will make a copy the data structure, allocating PCopyFunc: A function that will make a copy the data structure, allocating
memory for it. memory for it.
PEqualDeepFunc: A function to compare two structures by comparing all members.
GDestroyNotify: A function that will free memory for the data structure. GDestroyNotify: A function that will free memory for the data structure.

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 {
@ -95,3 +97,12 @@ const char * p_contact_status(const PContact contact)
{ {
return contact->status; return contact->status;
} }
int p_contacts_equal_deep(const PContact c1, const PContact c2)
{
int name_eq = (g_strcmp0(c1->name, c2->name) == 0);
int show_eq = (g_strcmp0(c1->show, c2->show) == 0);
int status_eq = (g_strcmp0(c1->status, c2->status) == 0);
return (name_eq && show_eq && status_eq);
}

View File

@ -32,5 +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_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);
} }
@ -49,15 +50,15 @@ void reset_search_attempts(void)
p_autocomplete_reset(ac); p_autocomplete_reset(ac);
} }
void contact_list_remove(const char * const name) gboolean contact_list_remove(const char * const name)
{ {
p_autocomplete_remove(ac, name); return p_autocomplete_remove(ac, name);
} }
void contact_list_add(const char * const name, const char * const show, gboolean contact_list_add(const char * const name, const char * const show,
const char * const status) const char * const status)
{ {
p_autocomplete_add(ac, p_contact_new(name, show, status)); return p_autocomplete_add(ac, p_contact_new(name, show, status));
} }
GSList * get_contact_list(void) GSList * get_contact_list(void)

View File

@ -30,9 +30,9 @@
void contact_list_init(void); void contact_list_init(void);
void contact_list_clear(void); void contact_list_clear(void);
void reset_search_attempts(void); void reset_search_attempts(void);
void contact_list_add(const char * const name, const char * const show, gboolean contact_list_add(const char * const name, const char * const show,
const char * const status); const char * const status);
void contact_list_remove(const char * const name); gboolean contact_list_remove(const char * const name);
GSList * get_contact_list(void); GSList * get_contact_list(void);
char * find_contact(char *search_str); char * find_contact(char *search_str);

1729
examplelog.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -335,11 +335,15 @@ static int _jabber_presence_handler(xmpp_conn_t * const conn,
if (strcmp(short_jid, short_from) !=0) { if (strcmp(short_jid, short_from) !=0) {
if (type == NULL) {// online if (type == NULL) {// online
gboolean result = contact_list_add(short_from, show_str, status_str);
if (result) {
win_contact_online(short_from, show_str, status_str); win_contact_online(short_from, show_str, status_str);
contact_list_add(short_from, show_str, status_str); }
} else {// offline } else {// offline
gboolean result = contact_list_remove(short_from);
if (result) {
win_contact_offline(short_from, show_str, status_str); win_contact_offline(short_from, show_str, status_str);
contact_list_remove(short_from); }
} }
win_page_off(); win_page_off();

57
presence_example.txt Normal file
View File

@ -0,0 +1,57 @@
<presence to="james.booth@framework"
from="tony.walsh@framework/407908c3">
<show>away</show>
<status>I'm not here right now</status>
<c hash="sha-1"
xmlns="http://jabber.org/protocol/caps"
ver="I22W7CegORwdbnu0ZiQwGpxr0Go="
node="http://pidgin.im/"/>
<x xmlns="vcard-temp:x:update"><photo/></x>
</presence>
<presence to="james.booth@framework"
from="tony.walsh@framework/407908c3">
<show>away</show>
<status>I'm not here right now</status>
<query xmlns="jabber:iq:last"
seconds="302"/>
<c hash="sha-1"
xmlns="http://jabber.org/protocol/caps"
ver="I22W7CegORwdbnu0ZiQwGpxr0Go="
node="http://pidgin.im/"/>
<x xmlns="vcard-temp:x:update"><photo/></x>
</presence>
<presence to="james.booth@framework"
from="chris.hosegood@framework/e63f97b3">
<priority>1</priority>
<c hash="sha-1"
xmlns="http://jabber.org/protocol/caps"
ext="voice-v1 camera-v1 video-v1"
ver="AcN1/PEN8nq7AHD+9jpxMV4U6YM="
node="http://pidgin.im/"/>
<x xmlns="vcard-temp:x:update">
<photo>768d0f2eb2785fb470a1a629d46b4482ebea5c55</photo>
</x>
</presence>
<presence to="james.booth@framework"
type="unavailable"
from="chris.hosegood@framework/e63f97b3"/>
<presence to="james.booth@framework"
from="chris.hosegood@framework/c718dfe1">
<priority>1</priority>
<c hash="sha-1"
xmlns="http://jabber.org/protocol/caps"
ext="voice-v1 camera-v1 video-v1"
ver="AcN1/PEN8nq7AHD+9jpxMV4U6YM="
node="http://pidgin.im/"/>
<x xmlns="vcard-temp:x:update">
<photo>768d0f2eb2785fb470a1a629d46b4482ebea5c55</photo>
</x>
</presence>

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;
} }
@ -89,11 +96,11 @@ void p_autocomplete_reset(PAutocomplete ac)
} }
} }
void p_autocomplete_add(PAutocomplete ac, void *item) gboolean p_autocomplete_add(PAutocomplete ac, void *item)
{ {
if (ac->items == NULL) { if (ac->items == NULL) {
ac->items = g_slist_append(ac->items, item); ac->items = g_slist_append(ac->items, item);
return; return TRUE;
} else { } else {
GSList *curr = ac->items; GSList *curr = ac->items;
@ -103,13 +110,18 @@ void p_autocomplete_add(PAutocomplete ac, void *item)
if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) > 0) { if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) > 0) {
ac->items = g_slist_insert_before(ac->items, ac->items = g_slist_insert_before(ac->items,
curr, item); curr, item);
return; return TRUE;
// 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) {
// only update if data different
if (!ac->equal_deep_func(curr->data, item)) {
ac->free_func(curr->data); ac->free_func(curr->data);
curr->data = item; curr->data = item;
return; return TRUE;
} else {
return FALSE;
}
} }
curr = g_slist_next(curr); curr = g_slist_next(curr);
@ -118,11 +130,11 @@ void p_autocomplete_add(PAutocomplete ac, void *item)
// hit end, append // hit end, append
ac->items = g_slist_append(ac->items, item); ac->items = g_slist_append(ac->items, item);
return; return TRUE;
} }
} }
void p_autocomplete_remove(PAutocomplete ac, const char * const item) gboolean p_autocomplete_remove(PAutocomplete ac, const char * const item)
{ {
// reset last found if it points to the item to be removed // reset last found if it points to the item to be removed
if (ac->last_found != NULL) if (ac->last_found != NULL)
@ -130,7 +142,7 @@ void p_autocomplete_remove(PAutocomplete ac, const char * const item)
ac->last_found = NULL; ac->last_found = NULL;
if (!ac->items) { if (!ac->items) {
return; return FALSE;
} else { } else {
GSList *curr = ac->items; GSList *curr = ac->items;
@ -140,13 +152,13 @@ void p_autocomplete_remove(PAutocomplete ac, const char * const item)
ac->items = g_slist_remove(ac->items, curr->data); ac->items = g_slist_remove(ac->items, curr->data);
ac->free_func(current_item); ac->free_func(current_item);
return; return TRUE;
} }
curr = g_slist_next(curr); curr = g_slist_next(curr);
} }
return; return FALSE;
} }
} }
@ -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

@ -28,14 +28,16 @@
typedef struct p_autocomplete_t *PAutocomplete; typedef struct p_autocomplete_t *PAutocomplete;
typedef const char * (*PStrFunc)(const void *obj); typedef const char * (*PStrFunc)(const void *obj);
typedef void * (*PCopyFunc)(const void *obj); typedef void * (*PCopyFunc)(const void *obj);
typedef int (*PEqualFunc)(const void *o1, const void *o2);
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);
void p_autocomplete_add(PAutocomplete ac, void *item); gboolean p_autocomplete_add(PAutocomplete ac, void *item);
void p_autocomplete_remove(PAutocomplete ac, const char * const item); gboolean p_autocomplete_remove(PAutocomplete ac, const char * const item);
GSList * p_autocomplete_get_list(PAutocomplete ac); GSList * p_autocomplete_get_list(PAutocomplete ac);
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str); gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str);

View File

@ -15,7 +15,10 @@ 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 =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free); (GDestroyNotify)p_contact_free);
p_autocomplete_clear(ac); p_autocomplete_clear(ac);
} }
@ -46,7 +49,10 @@ 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 =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free); (GDestroyNotify)p_contact_free);
GSList *result = p_autocomplete_get_list(ac); GSList *result = p_autocomplete_get_list(ac);
@ -70,7 +76,10 @@ 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 =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free); (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,7 +107,10 @@ 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 =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free); (GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
@ -128,7 +140,10 @@ 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 =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free); (GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2); p_autocomplete_add(ac, contact2);
@ -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);
@ -236,6 +260,114 @@ static void add_two_same_updates_with_funcs(void)
p_autocomplete_clear(ac); p_autocomplete_clear(ac);
} }
static void add_one_returns_true(void)
{
char *item = strdup("Hello");
PAutocomplete ac = p_autocomplete_new();
int result = p_autocomplete_add(ac, item);
assert_true(result);
p_autocomplete_clear(ac);
}
static void add_one_returns_true_with_funcs(void)
{
PContact contact = p_contact_new("James", "Online", "I'm here");
PAutocomplete ac =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
int result = p_autocomplete_add(ac, contact);
assert_true(result);
p_autocomplete_clear(ac);
}
static void add_two_different_returns_true(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Hello there");
PAutocomplete ac = p_autocomplete_new();
int result1 = p_autocomplete_add(ac, item1);
int result2 = p_autocomplete_add(ac, item2);
assert_true(result1);
assert_true(result2);
p_autocomplete_clear(ac);
}
static void add_two_different_returns_true_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("JamesB", "Away", "Out to lunch");
PAutocomplete ac =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
int result1 = p_autocomplete_add(ac, contact1);
int result2 = p_autocomplete_add(ac, contact2);
assert_true(result1);
assert_true(result2);
p_autocomplete_clear(ac);
}
static void add_two_same_returns_false(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Hello");
PAutocomplete ac = p_autocomplete_new();
int result1 = p_autocomplete_add(ac, item1);
int result2 = p_autocomplete_add(ac, item2);
assert_true(result1);
assert_false(result2);
p_autocomplete_clear(ac);
}
static void add_two_same_returns_false_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("James", "Online", "I'm here");
PAutocomplete ac =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
int result1 = p_autocomplete_add(ac, contact1);
int result2 = p_autocomplete_add(ac, contact2);
assert_true(result1);
assert_false(result2);
p_autocomplete_clear(ac);
}
static void add_two_same_different_data_returns_true(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("James", "Away", "I'm not here right now");
PAutocomplete ac =
p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,
(GDestroyNotify)p_contact_free);
int result1 = p_autocomplete_add(ac, contact1);
int result2 = p_autocomplete_add(ac, contact2);
assert_true(result1);
assert_true(result2);
p_autocomplete_clear(ac);
}
void register_prof_autocomplete_tests(void) void register_prof_autocomplete_tests(void)
{ {
TEST_MODULE("prof_autocomplete tests"); TEST_MODULE("prof_autocomplete tests");
@ -257,4 +389,11 @@ void register_prof_autocomplete_tests(void)
TEST(add_two_same_adds_one_with_funcs); TEST(add_two_same_adds_one_with_funcs);
TEST(add_two_same_updates); TEST(add_two_same_updates);
TEST(add_two_same_updates_with_funcs); TEST(add_two_same_updates_with_funcs);
TEST(add_one_returns_true);
TEST(add_one_returns_true_with_funcs);
TEST(add_two_different_returns_true);
TEST(add_two_different_returns_true_with_funcs);
TEST(add_two_same_returns_false);
TEST(add_two_same_returns_false_with_funcs);
TEST(add_two_same_different_data_returns_true);
} }