1
0
Fork 0

Added roster struct, create and destroy roster on connect/disconnect

This commit is contained in:
James Booth 2016-01-05 00:06:50 +00:00
parent 8e9bf08389
commit 055a5f71ce
18 changed files with 409 additions and 326 deletions

View File

@ -2599,7 +2599,20 @@ cmd_autocomplete(ProfWin *window, const char *const input)
void
cmd_reset_autocomplete(ProfWin *window)
{
roster_reset_search_attempts();
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
roster_reset_search_attempts();
if (window->type == WIN_CHAT) {
ProfChatWin *chatwin = (ProfChatWin*)window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
p_contact_resource_ac_reset(contact);
}
}
}
muc_invites_reset_ac();
accounts_reset_all_search();
accounts_reset_enabled_search();
@ -2692,15 +2705,6 @@ cmd_reset_autocomplete(ProfWin *window)
script_show_ac = NULL;
}
if (window->type == WIN_CHAT) {
ProfChatWin *chatwin = (ProfChatWin*)window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
p_contact_resource_ac_reset(contact);
}
}
if (window->type == WIN_MUC) {
ProfMucWin *mucwin = (ProfMucWin*)window;
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
@ -2836,6 +2840,8 @@ _cmd_complete_parameters(ProfWin *window, const char *const input)
int i;
char *result = NULL;
jabber_conn_status_t conn_status = jabber_get_connection_status();
// autocomplete boolean settings
gchar *boolean_choices[] = { "/beep", "/intype", "/states", "/outtype",
"/flash", "/splash", "/chlog", "/grlog", "/history", "/vercheck",
@ -2869,7 +2875,7 @@ _cmd_complete_parameters(ProfWin *window, const char *const input)
}
// otherwise autocomplete using roster
} else {
} else if (conn_status == JABBER_CONNECTED) {
gchar *contact_choices[] = { "/msg", "/info", "/status" };
// Remove quote character before and after names when doing autocomplete
char *unquoted = strip_arg_quotes(input);
@ -2891,9 +2897,11 @@ _cmd_complete_parameters(ProfWin *window, const char *const input)
}
}
result = autocomplete_param_with_func(input, "/invite", roster_contact_autocomplete);
if (result) {
return result;
if (conn_status == JABBER_CONNECTED) {
result = autocomplete_param_with_func(input, "/invite", roster_contact_autocomplete);
if (result) {
return result;
}
}
gchar *invite_choices[] = { "/decline", "/join" };
@ -3014,15 +3022,18 @@ _who_autocomplete(ProfWin *window, const char *const input)
return result;
}
} else {
int i = 0;
gchar *group_commands[] = { "/who any", "/who online", "/who offline",
"/who chat", "/who away", "/who xa", "/who dnd", "/who available",
"/who unavailable" };
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
int i = 0;
gchar *group_commands[] = { "/who any", "/who online", "/who offline",
"/who chat", "/who away", "/who xa", "/who dnd", "/who available",
"/who unavailable" };
for (i = 0; i < ARRAY_SIZE(group_commands); i++) {
result = autocomplete_param_with_func(input, group_commands[i], roster_group_autocomplete);
if (result) {
return result;
for (i = 0; i < ARRAY_SIZE(group_commands); i++) {
result = autocomplete_param_with_func(input, group_commands[i], roster_group_autocomplete);
if (result) {
return result;
}
}
}
@ -3055,18 +3066,23 @@ _roster_autocomplete(ProfWin *window, const char *const input)
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/roster nick", roster_barejid_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/roster clearnick", roster_barejid_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/roster remove", roster_barejid_autocomplete);
if (result) {
return result;
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
result = autocomplete_param_with_func(input, "/roster nick", roster_barejid_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/roster clearnick", roster_barejid_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/roster remove", roster_barejid_autocomplete);
if (result) {
return result;
}
}
result = autocomplete_param_with_ac(input, "/roster remove_all", roster_remove_all_ac, TRUE);
if (result) {
return result;
@ -3119,27 +3135,32 @@ static char*
_group_autocomplete(ProfWin *window, const char *const input)
{
char *result = NULL;
result = autocomplete_param_with_func(input, "/group show", roster_group_autocomplete);
if (result) {
return result;
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
result = autocomplete_param_with_func(input, "/group show", roster_group_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_no_with_func(input, "/group add", 4, roster_contact_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_no_with_func(input, "/group remove", 4, roster_contact_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/group add", roster_group_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/group remove", roster_group_autocomplete);
if (result) {
return result;
}
}
result = autocomplete_param_no_with_func(input, "/group add", 4, roster_contact_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_no_with_func(input, "/group remove", 4, roster_contact_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/group add", roster_group_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_func(input, "/group remove", roster_group_autocomplete);
if (result) {
return result;
}
result = autocomplete_param_with_ac(input, "/group", group_ac, TRUE);
if (result) {
return result;
@ -3361,9 +3382,13 @@ _otr_autocomplete(ProfWin *window, const char *const input)
{
char *found = NULL;
found = autocomplete_param_with_func(input, "/otr start", roster_contact_autocomplete);
if (found) {
return found;
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
found = autocomplete_param_with_func(input, "/otr start", roster_contact_autocomplete);
if (found) {
return found;
}
}
found = autocomplete_param_with_ac(input, "/otr log", otr_log_ac, TRUE);
@ -3372,24 +3397,25 @@ _otr_autocomplete(ProfWin *window, const char *const input)
}
// /otr policy always user@server.com
gboolean result;
gchar **args = parse_args(input, 3, 3, &result);
if (result && (strcmp(args[0], "policy") == 0)) {
GString *beginning = g_string_new("/otr ");
g_string_append(beginning, args[0]);
g_string_append(beginning, " ");
g_string_append(beginning, args[1]);
if (conn_status == JABBER_CONNECTED) {
gboolean result;
gchar **args = parse_args(input, 3, 3, &result);
if (result && (strcmp(args[0], "policy") == 0)) {
GString *beginning = g_string_new("/otr ");
g_string_append(beginning, args[0]);
g_string_append(beginning, " ");
g_string_append(beginning, args[1]);
found = autocomplete_param_with_func(input, beginning->str, roster_contact_autocomplete);
g_string_free(beginning, TRUE);
if (found) {
g_strfreev(args);
return found;
found = autocomplete_param_with_func(input, beginning->str, roster_contact_autocomplete);
g_string_free(beginning, TRUE);
if (found) {
g_strfreev(args);
return found;
}
}
g_strfreev(args);
}
g_strfreev(args);
found = autocomplete_param_with_ac(input, "/otr policy", otr_policy_ac, TRUE);
if (found) {
return found;
@ -3408,9 +3434,13 @@ _pgp_autocomplete(ProfWin *window, const char *const input)
{
char *found = NULL;
found = autocomplete_param_with_func(input, "/pgp start", roster_contact_autocomplete);
if (found) {
return found;
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
found = autocomplete_param_with_func(input, "/pgp start", roster_contact_autocomplete);
if (found) {
return found;
}
}
found = autocomplete_param_with_ac(input, "/pgp log", pgp_log_ac, TRUE);
@ -3438,9 +3468,11 @@ _pgp_autocomplete(ProfWin *window, const char *const input)
g_strfreev(args);
#endif
found = autocomplete_param_with_func(input, "/pgp setkey", roster_barejid_autocomplete);
if (found) {
return found;
if (conn_status == JABBER_CONNECTED) {
found = autocomplete_param_with_func(input, "/pgp setkey", roster_barejid_autocomplete);
if (found) {
return found;
}
}
found = autocomplete_param_with_ac(input, "/pgp", pgp_ac, TRUE);
@ -3529,7 +3561,8 @@ _resource_autocomplete(ProfWin *window, const char *const input)
{
char *found = NULL;
if (window->type == WIN_CHAT) {
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED && window->type == WIN_CHAT) {
ProfChatWin *chatwin = (ProfChatWin*)window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
PContact contact = roster_get_contact(chatwin->barejid);

View File

@ -2309,6 +2309,13 @@ cmd_resource(ProfWin *window, const char *const command, gchar **args)
cons_show("Resource can only be changed in chat windows.");
return TRUE;
}
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected.");
return TRUE;
}
ProfChatWin *chatwin = (ProfChatWin*)window;
if (g_strcmp0(cmd, "set") == 0) {

View File

@ -74,7 +74,7 @@ cl_ev_disconnect(void)
cons_show("%s logged out successfully.", jid);
jabber_disconnect();
roster_clear();
roster_destroy();
muc_invites_clear();
chat_sessions_clear();
tlscerts_clear_current();

View File

@ -64,6 +64,8 @@ sv_ev_login_account_success(char *account_name, int secured)
{
ProfAccount *account = accounts_get_account(account_name);
roster_create();
#ifdef HAVE_LIBOTR
otr_on_connect(account);
#endif
@ -150,7 +152,7 @@ sv_ev_lost_connection(void)
{
cons_show_error("Lost connection.");
roster_clear();
roster_destroy();
muc_invites_clear();
chat_sessions_clear();
ui_disconnected();
@ -168,8 +170,7 @@ sv_ev_failed_login(void)
}
void
sv_ev_room_invite(jabber_invite_t invite_type,
const char *const invitor, const char *const room,
sv_ev_room_invite(jabber_invite_t invite_type, const char *const invitor, const char *const room,
const char *const reason, const char *const password)
{
if (!muc_active(room) && !muc_invites_contain(room)) {

View File

@ -84,9 +84,13 @@ cb_policy(void *opdata, ConnContext *context)
}
static int
cb_is_logged_in(void *opdata, const char *accountname,
const char *protocol, const char *recipient)
cb_is_logged_in(void *opdata, const char *accountname, const char *protocol, const char *recipient)
{
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
return PRESENCE_OFFLINE;
}
PContact contact = roster_get_contact(recipient);
// not in roster

View File

@ -337,7 +337,6 @@ _init(char *log_level)
jabber_init();
cmd_init();
log_info("Initialising contact list");
roster_init();
muc_init();
tlscerts_init();
scripts_init();
@ -361,10 +360,13 @@ _shutdown(void)
ui_clear_win_title();
}
}
ui_close_all_wins();
jabber_disconnect();
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
cl_ev_disconnect();
}
jabber_shutdown();
roster_free();
muc_close();
caps_close();
ui_close();

View File

@ -34,6 +34,8 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <glib.h>
#include <assert.h>
@ -44,24 +46,28 @@
#include "tools/autocomplete.h"
#include "config/preferences.h"
// nicknames
static Autocomplete name_ac;
typedef struct prof_roster_t {
// contacts, indexed on barejid
GHashTable *contacts;
// barejids
static Autocomplete barejid_ac;
// nicknames
Autocomplete name_ac;
// fulljids
static Autocomplete fulljid_ac;
// barejids
Autocomplete barejid_ac;
// groups
static Autocomplete groups_ac;
GHashTable *group_count;
// fulljids
Autocomplete fulljid_ac;
// contacts, indexed on barejid
static GHashTable *contacts;
// nickname to barejid map
GHashTable *name_to_barejid;
// nickname to jid map
static GHashTable *name_to_barejid;
// groups
Autocomplete groups_ac;
GHashTable *group_count;
} ProfRoster;
static ProfRoster *roster = NULL;
static gboolean _key_equals(void *key1, void *key2);
static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2);
@ -71,23 +77,42 @@ static gint _compare_name(PContact a, PContact b);
static gint _compare_presence(PContact a, PContact b);
void
roster_clear(void)
roster_create(void)
{
autocomplete_clear(name_ac);
autocomplete_clear(barejid_ac);
autocomplete_clear(fulljid_ac);
autocomplete_clear(groups_ac);
g_hash_table_destroy(contacts);
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free);
g_hash_table_destroy(name_to_barejid);
name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_destroy(group_count);
group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
assert(roster == NULL);
roster = malloc(sizeof(ProfRoster));
roster->contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free);
roster->name_ac = autocomplete_new();
roster->barejid_ac = autocomplete_new();
roster->fulljid_ac = autocomplete_new();
roster->name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
roster->groups_ac = autocomplete_new();
roster->group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
}
void
roster_destroy(void)
{
assert(roster != NULL);
g_hash_table_destroy(roster->contacts);
autocomplete_free(roster->name_ac);
autocomplete_free(roster->barejid_ac);
autocomplete_free(roster->fulljid_ac);
g_hash_table_destroy(roster->name_to_barejid);
autocomplete_free(roster->groups_ac);
g_hash_table_destroy(roster->group_count);
free(roster);
roster = NULL;
}
gboolean
roster_update_presence(const char *const barejid, Resource *resource, GDateTime *last_activity)
{
assert(roster != NULL);
assert(barejid != NULL);
assert(resource != NULL);
@ -100,7 +125,7 @@ roster_update_presence(const char *const barejid, Resource *resource, GDateTime
}
p_contact_set_presence(contact, resource);
Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name);
autocomplete_add(fulljid_ac, jid->fulljid);
autocomplete_add(roster->fulljid_ac, jid->fulljid);
jid_destroy(jid);
return TRUE;
@ -109,8 +134,10 @@ roster_update_presence(const char *const barejid, Resource *resource, GDateTime
PContact
roster_get_contact(const char *const barejid)
{
assert(roster != NULL);
gchar *barejidlower = g_utf8_strdown(barejid, -1);
PContact contact = g_hash_table_lookup(contacts, barejidlower);
PContact contact = g_hash_table_lookup(roster->contacts, barejidlower);
g_free(barejidlower);
return contact;
@ -119,6 +146,8 @@ roster_get_contact(const char *const barejid)
char*
roster_get_msg_display_name(const char *const barejid, const char *const resource)
{
assert(roster != NULL);
GString *result = g_string_new("");
PContact contact = roster_get_contact(barejid);
@ -146,6 +175,8 @@ roster_get_msg_display_name(const char *const barejid, const char *const resourc
gboolean
roster_contact_offline(const char *const barejid, const char *const resource, const char *const status)
{
assert(roster != NULL);
PContact contact = roster_get_contact(barejid);
if (contact == NULL) {
@ -157,7 +188,7 @@ roster_contact_offline(const char *const barejid, const char *const resource, co
gboolean result = p_contact_remove_resource(contact, resource);
if (result == TRUE) {
Jid *jid = jid_create_from_bare_and_resource(barejid, resource);
autocomplete_remove(fulljid_ac, jid->fulljid);
autocomplete_remove(roster->fulljid_ac, jid->fulljid);
jid_destroy(jid);
}
@ -168,36 +199,19 @@ roster_contact_offline(const char *const barejid, const char *const resource, co
void
roster_reset_search_attempts(void)
{
autocomplete_reset(name_ac);
autocomplete_reset(barejid_ac);
autocomplete_reset(fulljid_ac);
autocomplete_reset(groups_ac);
}
assert(roster != NULL);
void
roster_init(void)
{
name_ac = autocomplete_new();
barejid_ac = autocomplete_new();
fulljid_ac = autocomplete_new();
groups_ac = autocomplete_new();
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free);
name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
}
void
roster_free(void)
{
autocomplete_free(name_ac);
autocomplete_free(barejid_ac);
autocomplete_free(fulljid_ac);
autocomplete_free(groups_ac);
autocomplete_reset(roster->name_ac);
autocomplete_reset(roster->barejid_ac);
autocomplete_reset(roster->fulljid_ac);
autocomplete_reset(roster->groups_ac);
}
void
roster_change_name(PContact contact, const char *const new_name)
{
assert(roster != NULL);
assert(contact != NULL);
const char *current_name = NULL;
@ -214,9 +228,11 @@ roster_change_name(PContact contact, const char *const new_name)
void
roster_remove(const char *const name, const char *const barejid)
{
autocomplete_remove(barejid_ac, barejid);
autocomplete_remove(name_ac, name);
g_hash_table_remove(name_to_barejid, name);
assert(roster != NULL);
autocomplete_remove(roster->barejid_ac, barejid);
autocomplete_remove(roster->name_ac, name);
g_hash_table_remove(roster->name_to_barejid, name);
// remove each fulljid
PContact contact = roster_get_contact(barejid);
@ -226,7 +242,7 @@ roster_remove(const char *const name, const char *const barejid)
GString *fulljid = g_string_new(strdup(barejid));
g_string_append(fulljid, "/");
g_string_append(fulljid, resources->data);
autocomplete_remove(fulljid_ac, fulljid->str);
autocomplete_remove(roster->fulljid_ac, fulljid->str);
g_string_free(fulljid, TRUE);
resources = g_list_next(resources);
}
@ -236,14 +252,14 @@ roster_remove(const char *const name, const char *const barejid)
GSList *curr = groups;
while (curr) {
gchar *group = curr->data;
if (g_hash_table_contains(group_count, group)) {
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, group));
if (g_hash_table_contains(roster->group_count, group)) {
int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, group));
count--;
if (count < 1) {
g_hash_table_remove(group_count, group);
autocomplete_remove(groups_ac, group);
g_hash_table_remove(roster->group_count, group);
autocomplete_remove(roster->groups_ac, group);
} else {
g_hash_table_insert(group_count, strdup(group), GINT_TO_POINTER(count));
g_hash_table_insert(roster->group_count, strdup(group), GINT_TO_POINTER(count));
}
}
curr = g_slist_next(curr);
@ -251,13 +267,15 @@ roster_remove(const char *const name, const char *const barejid)
}
// remove the contact
g_hash_table_remove(contacts, barejid);
g_hash_table_remove(roster->contacts, barejid);
}
void
roster_update(const char *const barejid, const char *const name, GSList *groups, const char *const subscription,
gboolean pending_out)
{
assert(roster != NULL);
PContact contact = roster_get_contact(barejid);
assert(contact != NULL);
@ -281,14 +299,14 @@ roster_update(const char *const barejid, const char *const name, GSList *groups,
if (!p_contact_in_group(contact, new_group)) {
// group doesn't yet exist
if (!g_hash_table_contains(group_count, new_group)) {
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(1));
autocomplete_add(groups_ac, curr_new_group->data);
if (!g_hash_table_contains(roster->group_count, new_group)) {
g_hash_table_insert(roster->group_count, strdup(new_group), GINT_TO_POINTER(1));
autocomplete_add(roster->groups_ac, curr_new_group->data);
// increment count
} else {
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, new_group));
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, new_group));
g_hash_table_insert(roster->group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
}
}
curr_new_group = g_slist_next(curr_new_group);
@ -300,14 +318,14 @@ roster_update(const char *const barejid, const char *const name, GSList *groups,
char *old_group = curr_old_group->data;
// removed from group
if (!g_slist_find_custom(groups, old_group, (GCompareFunc)g_strcmp0)) {
if (g_hash_table_contains(group_count, old_group)) {
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, old_group));
if (g_hash_table_contains(roster->group_count, old_group)) {
int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, old_group));
count--;
if (count < 1) {
g_hash_table_remove(group_count, old_group);
autocomplete_remove(groups_ac, old_group);
g_hash_table_remove(roster->group_count, old_group);
autocomplete_remove(roster->groups_ac, old_group);
} else {
g_hash_table_insert(group_count, strdup(old_group), GINT_TO_POINTER(count));
g_hash_table_insert(roster->group_count, strdup(old_group), GINT_TO_POINTER(count));
}
}
}
@ -322,31 +340,32 @@ gboolean
roster_add(const char *const barejid, const char *const name, GSList *groups, const char *const subscription,
gboolean pending_out)
{
assert(roster != NULL);
PContact contact = roster_get_contact(barejid);
if (contact) {
return FALSE;
}
contact = p_contact_new(barejid, name, groups, subscription, NULL,
pending_out);
contact = p_contact_new(barejid, name, groups, subscription, NULL, pending_out);
// add groups
GSList *curr_new_group = groups;
while (curr_new_group) {
char *new_group = curr_new_group->data;
if (g_hash_table_contains(group_count, new_group)) {
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, new_group));
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
if (g_hash_table_contains(roster->group_count, new_group)) {
int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, new_group));
g_hash_table_insert(roster->group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
} else {
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(1));
autocomplete_add(groups_ac, new_group);
g_hash_table_insert(roster->group_count, strdup(new_group), GINT_TO_POINTER(1));
autocomplete_add(roster->groups_ac, new_group);
}
curr_new_group = g_slist_next(curr_new_group);
}
g_hash_table_insert(contacts, strdup(barejid), contact);
autocomplete_add(barejid_ac, barejid);
g_hash_table_insert(roster->contacts, strdup(barejid), contact);
autocomplete_add(roster->barejid_ac, barejid);
_add_name_and_barejid(name, barejid);
return TRUE;
@ -355,8 +374,10 @@ roster_add(const char *const barejid, const char *const name, GSList *groups, co
char*
roster_barejid_from_name(const char *const name)
{
assert(roster != NULL);
if (name) {
return g_hash_table_lookup(name_to_barejid, name);
return g_hash_table_lookup(roster->name_to_barejid, name);
} else {
return NULL;
}
@ -365,12 +386,14 @@ roster_barejid_from_name(const char *const name)
GSList*
roster_get_contacts_by_presence(const char *const presence)
{
assert(roster != NULL);
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
g_hash_table_iter_init(&iter, roster->contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = (PContact)value;
if (g_strcmp0(p_contact_presence(contact), presence) == 0) {
@ -385,6 +408,8 @@ roster_get_contacts_by_presence(const char *const presence)
GSList*
roster_get_contacts(roster_ord_t order, gboolean include_offline)
{
assert(roster != NULL);
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
@ -397,7 +422,7 @@ roster_get_contacts(roster_ord_t order, gboolean include_offline)
cmp_func = (GCompareFunc) _compare_name;
}
g_hash_table_iter_init(&iter, contacts);
g_hash_table_iter_init(&iter, roster->contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = value;
const char *presence = p_contact_presence(contact);
@ -415,12 +440,14 @@ roster_get_contacts(roster_ord_t order, gboolean include_offline)
GSList*
roster_get_contacts_online(void)
{
assert(roster != NULL);
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
g_hash_table_iter_init(&iter, roster->contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
if(strcmp(p_contact_presence(value), "offline"))
result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_name);
@ -433,11 +460,13 @@ roster_get_contacts_online(void)
gboolean
roster_has_pending_subscriptions(void)
{
assert(roster != NULL);
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
g_hash_table_iter_init(&iter, roster->contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = (PContact) value;
if (p_contact_pending_out(contact)) {
@ -451,18 +480,24 @@ roster_has_pending_subscriptions(void)
char*
roster_contact_autocomplete(const char *const search_str)
{
return autocomplete_complete(name_ac, search_str, TRUE);
assert(roster != NULL);
return autocomplete_complete(roster->name_ac, search_str, TRUE);
}
char*
roster_fulljid_autocomplete(const char *const search_str)
{
return autocomplete_complete(fulljid_ac, search_str, TRUE);
assert(roster != NULL);
return autocomplete_complete(roster->fulljid_ac, search_str, TRUE);
}
GSList*
roster_get_nogroup(roster_ord_t order, gboolean include_offline)
{
assert(roster != NULL);
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
@ -475,7 +510,7 @@ roster_get_nogroup(roster_ord_t order, gboolean include_offline)
cmp_func = (GCompareFunc) _compare_name;
}
g_hash_table_iter_init(&iter, contacts);
g_hash_table_iter_init(&iter, roster->contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = value;
const char *presence = p_contact_presence(contact);
@ -496,6 +531,8 @@ roster_get_nogroup(roster_ord_t order, gboolean include_offline)
GSList*
roster_get_group(const char *const group, roster_ord_t order, gboolean include_offline)
{
assert(roster != NULL);
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
@ -508,7 +545,7 @@ roster_get_group(const char *const group, roster_ord_t order, gboolean include_o
cmp_func = (GCompareFunc) _compare_name;
}
g_hash_table_iter_init(&iter, contacts);
g_hash_table_iter_init(&iter, roster->contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = value;
const char *presence = p_contact_presence(contact);
@ -533,19 +570,25 @@ roster_get_group(const char *const group, roster_ord_t order, gboolean include_o
GSList*
roster_get_groups(void)
{
return autocomplete_create_list(groups_ac);
assert(roster != NULL);
return autocomplete_create_list(roster->groups_ac);
}
char*
roster_group_autocomplete(const char *const search_str)
{
return autocomplete_complete(groups_ac, search_str, TRUE);
assert(roster != NULL);
return autocomplete_complete(roster->groups_ac, search_str, TRUE);
}
char*
roster_barejid_autocomplete(const char *const search_str)
{
return autocomplete_complete(barejid_ac, search_str, TRUE);
assert(roster != NULL);
return autocomplete_complete(roster->barejid_ac, search_str, TRUE);
}
static gboolean
@ -574,15 +617,17 @@ _datetimes_equal(GDateTime *dt1, GDateTime *dt2)
static void
_replace_name(const char *const current_name, const char *const new_name, const char *const barejid)
{
assert(roster != NULL);
// current handle exists already
if (current_name) {
autocomplete_remove(name_ac, current_name);
g_hash_table_remove(name_to_barejid, current_name);
autocomplete_remove(roster->name_ac, current_name);
g_hash_table_remove(roster->name_to_barejid, current_name);
_add_name_and_barejid(new_name, barejid);
// no current handle
} else if (new_name) {
autocomplete_remove(name_ac, barejid);
g_hash_table_remove(name_to_barejid, barejid);
autocomplete_remove(roster->name_ac, barejid);
g_hash_table_remove(roster->name_to_barejid, barejid);
_add_name_and_barejid(new_name, barejid);
}
}
@ -590,12 +635,14 @@ _replace_name(const char *const current_name, const char *const new_name, const
static void
_add_name_and_barejid(const char *const name, const char *const barejid)
{
assert(roster != NULL);
if (name) {
autocomplete_add(name_ac, name);
g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid));
autocomplete_add(roster->name_ac, name);
g_hash_table_insert(roster->name_to_barejid, strdup(name), strdup(barejid));
} else {
autocomplete_add(name_ac, barejid);
g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid));
autocomplete_add(roster->name_ac, barejid);
g_hash_table_insert(roster->name_to_barejid, strdup(barejid), strdup(barejid));
}
}

View File

@ -50,8 +50,8 @@ gboolean roster_update_presence(const char *const barejid, Resource *resource, G
PContact roster_get_contact(const char *const barejid);
gboolean roster_contact_offline(const char *const barejid, const char *const resource, const char *const status);
void roster_reset_search_attempts(void);
void roster_init(void);
void roster_free(void);
void roster_create(void);
void roster_destroy(void);
void roster_change_name(PContact contact, const char *const new_name);
void roster_remove(const char *const name, const char *const barejid);
void roster_update(const char *const barejid, const char *const name, GSList *groups, const char *const subscription,

View File

@ -362,13 +362,18 @@ chatwin_get_string(ProfChatWin *chatwin)
GString *res = g_string_new("Chat ");
PContact contact = roster_get_contact(chatwin->barejid);
if (contact == NULL) {
g_string_append(res, chatwin->barejid);
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
PContact contact = roster_get_contact(chatwin->barejid);
if (contact == NULL) {
g_string_append(res, chatwin->barejid);
} else {
const char *display_name = p_contact_name_or_jid(contact);
g_string_append(res, display_name);
g_string_append_printf(res, " - %s", p_contact_presence(contact));
}
} else {
const char *display_name = p_contact_name_or_jid(contact);
g_string_append(res, display_name);
g_string_append_printf(res, " - %s", p_contact_presence(contact));
g_string_append(res, chatwin->barejid);
}
if (chatwin->unread > 0) {

View File

@ -746,8 +746,7 @@ cons_show_status(const char *const barejid)
}
void
cons_show_room_invite(const char *const invitor, const char * const room,
const char *const reason)
cons_show_room_invite(const char *const invitor, const char * const room, const char *const reason)
{
char *display_from = NULL;
PContact contact = roster_get_contact(invitor);

View File

@ -466,6 +466,11 @@ rosterwin_roster(void)
return;
}
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
return;
}
ProfLayoutSplit *layout = (ProfLayoutSplit*)console->layout;
assert(layout->memcheck == LAYOUT_SPLIT_MEMCHECK);

View File

@ -403,15 +403,18 @@ _show_contact_presence(ProfChatWin *chatwin)
theme_item_t presence_colour = THEME_TITLE_OFFLINE;
const char *presence = "offline";
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
if (resource) {
Resource *resourcep = p_contact_get_resource(contact, resource);
if (resourcep) {
presence = string_from_resource_presence(resourcep->presence);
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
if (resource) {
Resource *resourcep = p_contact_get_resource(contact, resource);
if (resourcep) {
presence = string_from_resource_presence(resourcep->presence);
}
} else {
presence = p_contact_presence(contact);
}
} else {
presence = p_contact_presence(contact);
}
}

View File

@ -248,10 +248,15 @@ win_get_title(ProfWin *window)
if (window->type == WIN_CHAT) {
ProfChatWin *chatwin = (ProfChatWin*) window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
const char *name = p_contact_name_or_jid(contact);
return strdup(name);
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status == JABBER_CONNECTED) {
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
const char *name = p_contact_name_or_jid(contact);
return strdup(name);
} else {
return strdup(chatwin->barejid);
}
} else {
return strdup(chatwin->barejid);
}

View File

@ -17,7 +17,7 @@
void clears_chat_sessions(void **state)
{
chat_sessions_init();
roster_init();
roster_create();
chat_session_recipient_active("bob@server.org", "laptop", FALSE);
chat_session_recipient_active("mike@server.org", "work", FALSE);

View File

@ -53,7 +53,7 @@ void cmd_roster_shows_roster_when_no_args(void **state)
will_return(jabber_get_connection_status, JABBER_CONNECTED);
roster_init();
roster_create();
roster_add("bob@server.org", "bob", NULL, "both", FALSE);
GSList *roster = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
@ -62,7 +62,7 @@ void cmd_roster_shows_roster_when_no_args(void **state)
gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
assert_true(result);
roster_free();
roster_destroy();
}
void cmd_roster_add_shows_message_when_no_jid(void **state)
@ -145,7 +145,7 @@ void cmd_roster_nick_shows_message_when_no_contact_exists(void **state)
{
gchar *args[] = { "nick", "bob@server.org", "bobster", NULL };
roster_init();
roster_create();
will_return(jabber_get_connection_status, JABBER_CONNECTED);
@ -154,7 +154,7 @@ void cmd_roster_nick_shows_message_when_no_contact_exists(void **state)
gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
assert_true(result);
roster_free();
roster_destroy();
}
void cmd_roster_nick_sends_name_change_request(void **state)
@ -163,9 +163,9 @@ void cmd_roster_nick_sends_name_change_request(void **state)
char *nick = "bobster";
gchar *args[] = { "nick", jid, nick, NULL };
roster_init();
roster_create();
GSList *groups = NULL;
groups = g_slist_append(groups, "group1");
groups = g_slist_append(groups, strdup("group1"));
roster_add(jid, "bob", groups, "both", FALSE);
will_return(jabber_get_connection_status, JABBER_CONNECTED);
@ -181,7 +181,7 @@ void cmd_roster_nick_sends_name_change_request(void **state)
PContact contact = roster_get_contact(jid);
assert_string_equal(p_contact_name(contact), nick);
roster_free();
roster_destroy();
}
void cmd_roster_clearnick_shows_message_when_no_jid(void **state)
@ -200,7 +200,7 @@ void cmd_roster_clearnick_shows_message_when_no_contact_exists(void **state)
{
gchar *args[] = { "clearnick", "bob@server.org", NULL };
roster_init();
roster_create();
will_return(jabber_get_connection_status, JABBER_CONNECTED);
@ -209,7 +209,7 @@ void cmd_roster_clearnick_shows_message_when_no_contact_exists(void **state)
gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
assert_true(result);
roster_free();
roster_destroy();
}
void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state)
@ -217,9 +217,9 @@ void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state
char *jid = "bob@server.org";
gchar *args[] = { "clearnick", jid, NULL };
roster_init();
roster_create();
GSList *groups = NULL;
groups = g_slist_append(groups, "group1");
groups = g_slist_append(groups, strdup("group1"));
roster_add(jid, "bob", groups, "both", FALSE);
will_return(jabber_get_connection_status, JABBER_CONNECTED);
@ -236,5 +236,5 @@ void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state
PContact contact = roster_get_contact(jid);
assert_null(p_contact_name(contact));
roster_free();
roster_destroy();
}

View File

@ -11,50 +11,46 @@
void empty_list_when_none_added(void **state)
{
roster_init();
roster_create();
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
assert_null(list);
roster_clear();
roster_free();
roster_destroy();
}
void contains_one_element(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
assert_int_equal(1, g_slist_length(list));
roster_clear();
roster_free();
roster_destroy();
}
void first_element_correct(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
PContact james = list->data;
assert_string_equal("James", p_contact_barejid(james));
roster_clear();
roster_free();
roster_destroy();
}
void contains_two_elements(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
roster_add("Dave", NULL, NULL, NULL, FALSE);
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
assert_int_equal(2, g_slist_length(list));
roster_clear();
roster_free();
roster_destroy();
}
void first_and_second_elements_correct(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
roster_add("Dave", NULL, NULL, NULL, FALSE);
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
@ -64,26 +60,24 @@ void first_and_second_elements_correct(void **state)
assert_string_equal("Dave", p_contact_barejid(first));
assert_string_equal("James", p_contact_barejid(second));
roster_clear();
roster_free();
roster_destroy();
}
void contains_three_elements(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
roster_add("Bob", NULL, NULL, NULL, FALSE);
roster_add("Dave", NULL, NULL, NULL, FALSE);
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
assert_int_equal(3, g_slist_length(list));
roster_clear();
roster_free();
roster_destroy();
}
void first_three_elements_correct(void **state)
{
roster_init();
roster_create();
roster_add("Bob", NULL, NULL, NULL, FALSE);
roster_add("Dave", NULL, NULL, NULL, FALSE);
roster_add("James", NULL, NULL, NULL, FALSE);
@ -95,13 +89,12 @@ void first_three_elements_correct(void **state)
assert_string_equal("James", p_contact_barejid(james));
assert_string_equal("Dave", p_contact_barejid(dave));
assert_string_equal("Bob", p_contact_barejid(bob));
roster_clear();
roster_free();
roster_destroy();
}
void add_twice_at_beginning_adds_once(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
roster_add("James", NULL, NULL, NULL, FALSE);
roster_add("Dave", NULL, NULL, NULL, FALSE);
@ -115,13 +108,12 @@ void add_twice_at_beginning_adds_once(void **state)
assert_string_equal("Bob", p_contact_barejid(first));
assert_string_equal("Dave", p_contact_barejid(second));
assert_string_equal("James", p_contact_barejid(third));
roster_clear();
roster_free();
roster_destroy();
}
void add_twice_in_middle_adds_once(void **state)
{
roster_init();
roster_create();
roster_add("James", NULL, NULL, NULL, FALSE);
roster_add("Dave", NULL, NULL, NULL, FALSE);
roster_add("James", NULL, NULL, NULL, FALSE);
@ -135,13 +127,12 @@ void add_twice_in_middle_adds_once(void **state)
assert_string_equal("Bob", p_contact_barejid(first));
assert_string_equal("Dave",