/* * roster_list.c * * Copyright (C) 2012 - 2014 James Booth * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Profanity is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Profanity. If not, see . * * In addition, as a special exception, the copyright holders give permission to * link the code of portions of this program with the OpenSSL library under * certain conditions as described in each individual source file, and * distribute linked combinations including the two. * * You must obey the GNU General Public License in all respects for all of the * code used other than OpenSSL. If you modify file(s) with this exception, you * may extend this exception to your version of the file(s), but you are not * obligated to do so. If you do not wish to do so, delete this exception * statement from your version. If you delete this exception statement from all * source files in the program, then also delete it here. * */ #include #include #include #include "roster_list.h" #include "resource.h" #include "contact.h" #include "jid.h" #include "tools/autocomplete.h" // nicknames static Autocomplete name_ac; // barejids static Autocomplete barejid_ac; // fulljids static Autocomplete fulljid_ac; // groups static Autocomplete groups_ac; // contacts, indexed on barejid static GHashTable *contacts; // nickname to jid map static GHashTable *name_to_barejid; static gboolean _key_equals(void *key1, void *key2); static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2); static void _replace_name(const char * const current_name, const char * const new_name, const char * const barejid); static void _add_name_and_barejid(const char * const name, const char * const barejid); static gint _compare_contacts(PContact a, PContact b); void roster_clear(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); } gboolean roster_update_presence(const char * const barejid, Resource *resource, GDateTime *last_activity) { assert(barejid != NULL); assert(resource != NULL); PContact contact = g_hash_table_lookup(contacts, barejid); if (contact == NULL) { return FALSE; } if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) { p_contact_set_last_activity(contact, last_activity); } p_contact_set_presence(contact, resource); Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); autocomplete_add(fulljid_ac, jid->fulljid); jid_destroy(jid); return TRUE; } PContact roster_get_contact(const char * const barejid) { return g_hash_table_lookup(contacts, barejid); } gboolean roster_contact_offline(const char * const barejid, const char * const resourc

; } GSList * roster_get_groups(void) { return autocomplete_get_list(groups_ac); } char * roster_find_group(char *search_str) { return autocomplete_complete(groups_ac, search_str, TRUE); } char * roster_find_jid(char *search_str) { return autocomplete_complete(barejid_ac, search_str, TRUE); } static gboolean _key_equals(void *key1, void *key2) { gchar *str1 = (gchar *) key1; gchar *str2 = (gchar *) key2; return (g_strcmp0(str1, str2) == 0); } static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2) { if ((dt1 == NULL) && (dt2 == NULL)) { return TRUE; } else if ((dt1 == NULL) && (dt2 != NULL)) { return FALSE; } else if ((dt1 != NULL) && (dt2 == NULL)) { return FALSE; } else { return g_date_time_equal(dt1, dt2); } } static void _replace_name(const char * const current_name, const char * const new_name, const char * const barejid) { // current handle exists already if (current_name != NULL) { autocomplete_remove(name_ac, current_name); g_hash_table_remove(name_to_barejid, current_name); _add_name_and_barejid(new_name, barejid); // no current handle } else if (new_name != NULL) { autocomplete_remove(name_ac, barejid); g_hash_table_remove(name_to_barejid, barejid); _add_name_and_barejid(new_name, barejid); } } static void _add_name_and_barejid(const char * const name, const char * const barejid) { if (name != NULL) { autocomplete_add(name_ac, name); g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); } else { autocomplete_add(name_ac, barejid); g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); } } static gint _compare_contacts(PContact a, PContact b) { const char * utf8_str_a = NULL; const char * utf8_str_b = NULL; if (p_contact_name(a) != NULL) { utf8_str_a = p_contact_name(a); } else { utf8_str_a = p_contact_barejid(a); } if (p_contact_name(b) != NULL) { utf8_str_b = p_contact_name(b); } else { utf8_str_b = p_contact_barejid(b); } gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); gint result = g_strcmp0(key_a, key_b); g_free(key_a); g_free(key_b); return result; }