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

Removed contact_list, moved roster logic to xmpp/roster module

This commit is contained in:
James Booth 2013-05-06 23:04:46 +01:00
parent 05f2d29396
commit c1ee75da40
13 changed files with 261 additions and 298 deletions

View File

@ -1,7 +1,8 @@
bin_PROGRAMS = profanity
profanity_SOURCES = src/contact.c src/contact.h src/log.c src/common.c \
src/contact_list.c src/log.h src/profanity.c src/common.h \
src/contact_list.h src/main.c src/profanity.h src/chat_session.c \
profanity_SOURCES = \
src/contact.c src/contact.h src/log.c src/common.c \
src/log.h src/profanity.c src/common.h \
src/main.c src/profanity.h src/chat_session.c \
src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \
src/resource.c src/resource.h \
src/xmpp/xmpp.h src/xmpp/capabilities.c src/xmpp/connection.c \
@ -24,11 +25,32 @@ profanity_SOURCES = src/contact.c src/contact.h src/log.c src/common.c \
TESTS = tests/testsuite
check_PROGRAMS = tests/testsuite
tests_testsuite_SOURCES = tests/test_roster.c src/contact_list.c src/contact.c \
tests/test_common.c tests/test_history.c src/tools/history.c src/common.c \
tests/test_autocomplete.c src/tools/autocomplete.c tests/testsuite.c \
tests/test_parser.c src/command/parser.c tests/test_jid.c src/jid.c \
src/resource.c src/resource.h
tests_testsuite_SOURCES = \
src/contact.c src/contact.h src/log.c src/common.c \
src/log.h src/profanity.c src/common.h \
src/profanity.h src/chat_session.c \
src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \
src/resource.c src/resource.h \
src/xmpp/xmpp.h src/xmpp/capabilities.c src/xmpp/connection.c \
src/xmpp/iq.c src/xmpp/message.c src/xmpp/presence.c src/xmpp/stanza.c \
src/xmpp/stanza.h src/xmpp/message.h src/xmpp/iq.h src/xmpp/presence.h \
src/xmpp/capabilities.h src/xmpp/connection.h \
src/xmpp/roster.c src/xmpp/roster.h \
src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
src/command/command.h src/command/command.c src/command/history.c \
src/command/history.h src/command/parser.c \
src/command/parser.h \
src/tools/autocomplete.c src/tools/autocomplete.h \
src/tools/history.c src/tools/history.h \
src/tools/tinyurl.c src/tools/tinyurl.h \
src/config/accounts.c src/config/accounts.h \
src/config/preferences.c src/config/preferences.h \
src/config/theme.c src/config/theme.h \
tests/test_roster.c tests/test_common.c tests/test_history.c \
tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \
tests/test_jid.c
tests_testsuite_LDADD = -lheadunit -lstdc++
man_MANS = docs/profanity.1

View File

@ -36,7 +36,6 @@
#include "config/preferences.h"
#include "config/theme.h"
#include "contact.h"
#include "contact_list.h"
#include "jid.h"
#include "log.h"
#include "muc.h"

View File

@ -1,232 +0,0 @@
/*
* contact_list.c
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#include <assert.h>
#include <string.h>
#include <glib.h>
#include "contact.h"
#include "jid.h"
#include "tools/autocomplete.h"
static Autocomplete ac;
static Autocomplete resource_ac;
static GHashTable *contacts;
static gboolean _key_equals(void *key1, void *key2);
static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2);
void
roster_init(void)
{
ac = autocomplete_new();
resource_ac = autocomplete_new();
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
(GDestroyNotify)p_contact_free);
}
void
roster_clear(void)
{
autocomplete_clear(ac);
autocomplete_clear(resource_ac);
g_hash_table_destroy(contacts);
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
(GDestroyNotify)p_contact_free);
}
void
roster_free()
{
autocomplete_free(ac);
autocomplete_free(resource_ac);
}
void
roster_reset_search_attempts(void)
{
autocomplete_reset(ac);
autocomplete_reset(resource_ac);
}
gboolean
roster_add(const char * const barejid, const char * const name,
const char * const subscription, const char * const offline_message,
gboolean pending_out)
{
gboolean added = FALSE;
PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) {
contact = p_contact_new(barejid, name, subscription, offline_message,
pending_out);
g_hash_table_insert(contacts, strdup(barejid), contact);
autocomplete_add(ac, strdup(barejid));
added = TRUE;
}
return added;
}
void
roster_remove(const char * const barejid)
{
g_hash_table_remove(contacts, barejid);
}
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(resource_ac, strdup(jid->fulljid));
jid_destroy(jid);
return TRUE;
}
gboolean
roster_contact_offline(const char * const barejid,
const char * const resource, const char * const status)
{
PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) {
return FALSE;
}
if (resource == NULL) {
return TRUE;
} else {
gboolean result = p_contact_remove_resource(contact, resource);
if (result == TRUE) {
Jid *jid = jid_create_from_bare_and_resource(barejid, resource);
autocomplete_remove(resource_ac, jid->fulljid);
jid_destroy(jid);
}
return result;
}
}
void
roster_update_subscription(const char * const barejid,
const char * const subscription, gboolean pending_out)
{
PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) {
contact = p_contact_new_subscription(barejid, subscription, pending_out);
g_hash_table_insert(contacts, strdup(barejid), contact);
} else {
p_contact_set_subscription(contact, subscription);
p_contact_set_pending_out(contact, pending_out);
}
}
gboolean
roster_has_pending_subscriptions(void)
{
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = (PContact) value;
if (p_contact_pending_out(contact)) {
return TRUE;
}
}
return FALSE;
}
GSList *
roster_get_contacts(void)
{
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
result = g_slist_append(result, value);
}
// resturn all contact structs
return result;
}
char *
roster_find_contact(char *search_str)
{
return autocomplete_complete(ac, search_str);
}
char *
roster_find_resource(char *search_str)
{
return autocomplete_complete(resource_ac, search_str);
}
PContact
roster_get_contact(const char const *barejid)
{
return g_hash_table_lookup(contacts, barejid);
}
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);
}
}

View File

@ -1,50 +0,0 @@
/*
* contact_list.h
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef CONTACT_LIST_H
#define CONTACT_LIST_H
#include <glib.h>
#include "contact.h"
void roster_init(void);
void roster_clear(void);
void roster_free(void);
void roster_reset_search_attempts(void);
void roster_remove(const char * const barejid);
gboolean roster_add(const char * const barejid, const char * const name,
const char * const subscription, const char * const offline_message,
gboolean pending_out);
gboolean roster_update_presence(const char * const barejid,
Resource *resource, GDateTime *last_activity);
void roster_update_subscription(const char * const barejid,
const char * const subscription, gboolean pending_out);
gboolean roster_has_pending_subscriptions(void);
GSList * roster_get_contacts(void);
char * roster_find_contact(char *search_str);
char * roster_find_resource(char *search_str);
PContact roster_get_contact(const char const *barejid);
gboolean roster_contact_offline(const char * const barejid,
const char * const resource, const char * const status);
#endif

View File

@ -38,7 +38,6 @@
#include "command/command.h"
#include "common.h"
#include "contact.h"
#include "contact_list.h"
#include "log.h"
#include "muc.h"
#include "resource.h"

View File

@ -32,11 +32,11 @@
#include "command/command.h"
#include "common.h"
#include "config/preferences.h"
#include "contact_list.h"
#include "config/theme.h"
#include "ui/notifier.h"
#include "ui/window.h"
#include "ui/ui.h"
#include "xmpp/xmpp.h"
#define CONS_WIN_TITLE "_cons"

View File

@ -40,13 +40,13 @@
#include "config/preferences.h"
#include "config/theme.h"
#include "contact.h"
#include "contact_list.h"
#include "jid.h"
#include "log.h"
#include "muc.h"
#include "ui/notifier.h"
#include "ui/ui.h"
#include "ui/window.h"
#include "xmpp/xmpp.h"
// the window currently being displayed
static int current_index = 0;

View File

@ -37,10 +37,10 @@
#include "common.h"
#include "config/preferences.h"
#include "config/theme.h"
#include "contact_list.h"
#include "log.h"
#include "profanity.h"
#include "ui/ui.h"
#include "xmpp/xmpp.h"
#define _inp_win_refresh() prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1)

View File

@ -28,13 +28,13 @@
#include <glib.h>
#include <strophe.h>
#include "contact_list.h"
#include "log.h"
#include "muc.h"
#include "profanity.h"
#include "xmpp/capabilities.h"
#include "xmpp/connection.h"
#include "xmpp/stanza.h"
#include "xmpp/xmpp.h"
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_IQ, type, ctx)

View File

@ -20,14 +20,16 @@
*
*/
#include <assert.h>
#include <string.h>
#include <glib.h>
#include <strophe.h>
#include "contact_list.h"
#include "log.h"
#include "tools/autocomplete.h"
#include "xmpp/connection.h"
#include "xmpp/roster.h"
#include "xmpp/stanza.h"
#include "xmpp/xmpp.h"
@ -38,6 +40,13 @@ static int _roster_handle_set(xmpp_conn_t * const conn,
static int _roster_handle_result(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
static Autocomplete ac;
static Autocomplete resource_ac;
static GHashTable *contacts;
static gboolean _key_equals(void *key1, void *key2);
static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2);
void
roster_add_handlers(void)
{
@ -124,3 +133,197 @@ _roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
return 1;
}
void
roster_init(void)
{
ac = autocomplete_new();
resource_ac = autocomplete_new();
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
(GDestroyNotify)p_contact_free);
}
void
roster_clear(void)
{
autocomplete_clear(ac);
autocomplete_clear(resource_ac);
g_hash_table_destroy(contacts);
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
(GDestroyNotify)p_contact_free);
}
void
roster_free()
{
autocomplete_free(ac);
autocomplete_free(resource_ac);
}
void
roster_reset_search_attempts(void)
{
autocomplete_reset(ac);
autocomplete_reset(resource_ac);
}
gboolean
roster_add(const char * const barejid, const char * const name,
const char * const subscription, const char * const offline_message,
gboolean pending_out)
{
gboolean added = FALSE;
PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) {
contact = p_contact_new(barejid, name, subscription, offline_message,
pending_out);
g_hash_table_insert(contacts, strdup(barejid), contact);
autocomplete_add(ac, strdup(barejid));
added = TRUE;
}
return added;
}
void
roster_remove(const char * const barejid)
{
g_hash_table_remove(contacts, barejid);
}
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(resource_ac, strdup(jid->fulljid));
jid_destroy(jid);
return TRUE;
}
gboolean
roster_contact_offline(const char * const barejid,
const char * const resource, const char * const status)
{
PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) {
return FALSE;
}
if (resource == NULL) {
return TRUE;
} else {
gboolean result = p_contact_remove_resource(contact, resource);
if (result == TRUE) {
Jid *jid = jid_create_from_bare_and_resource(barejid, resource);
autocomplete_remove(resource_ac, jid->fulljid);
jid_destroy(jid);
}
return result;
}
}
void
roster_update_subscription(const char * const barejid,
const char * const subscription, gboolean pending_out)
{
PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) {
contact = p_contact_new_subscription(barejid, subscription, pending_out);
g_hash_table_insert(contacts, strdup(barejid), contact);
} else {
p_contact_set_subscription(contact, subscription);
p_contact_set_pending_out(contact, pending_out);
}
}
gboolean
roster_has_pending_subscriptions(void)
{
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PContact contact = (PContact) value;
if (p_contact_pending_out(contact)) {
return TRUE;
}
}
return FALSE;
}
GSList *
roster_get_contacts(void)
{
GSList *result = NULL;
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, contacts);
while (g_hash_table_iter_next(&iter, &key, &value)) {
result = g_slist_append(result, value);
}
// resturn all contact structs
return result;
}
char *
roster_find_contact(char *search_str)
{
return autocomplete_complete(ac, search_str);
}
char *
roster_find_resource(char *search_str)
{
return autocomplete_complete(resource_ac, search_str);
}
PContact
roster_get_contact(const char const *barejid)
{
return g_hash_table_lookup(contacts, barejid);
}
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);
}
}

View File

@ -26,4 +26,8 @@
void roster_add_handlers(void);
void roster_request(void);
void roster_remove(const char * const barejid);
void roster_update_subscription(const char * const barejid,
const char * const subscription, gboolean pending_out);
#endif

View File

@ -26,6 +26,7 @@
#include <strophe.h>
#include "config/accounts.h"
#include "contact.h"
#include "jid.h"
#define JABBER_PRIORITY_MIN -128
@ -123,4 +124,21 @@ void iq_disco_items_request(gchar *jid);
Capabilities* caps_get(const char * const caps_str);
void caps_close(void);
void roster_clear(void);
gboolean roster_update_presence(const char * const barejid,
Resource *resource, GDateTime *last_activity);
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);
gboolean roster_has_pending_subscriptions(void);
GSList * roster_get_contacts(void);
char * roster_find_contact(char *search_str);
char * roster_find_resource(char *search_str);
gboolean roster_add(const char * const barejid, const char * const name,
const char * const subscription, const char * const offline_message,
gboolean pending_out);
#endif

View File

@ -6,7 +6,7 @@
#include <glib.h>
#include "contact.h"
#include "contact_list.h"
#include "xmpp/xmpp.h"
static void setup(void)
{