From e6749d669db500942d9ad44df34077de34a36580 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 31 Jan 2013 00:01:38 +0000 Subject: [PATCH] Option to use last presence at login, or use a specific presence --- src/accounts.c | 70 +++++++++++++++++++++++++++++++++++++++------ src/accounts.h | 7 ++++- src/common.c | 16 +++++++++++ src/common.h | 10 +++++++ src/ui_windows.c | 16 +++++++---- src/xmpp.h | 10 ------- src/xmpp_iq.c | 7 ++--- src/xmpp_presence.c | 17 ----------- 8 files changed, 106 insertions(+), 47 deletions(-) diff --git a/src/accounts.c b/src/accounts.c index 568391bf..5cfbe299 100644 --- a/src/accounts.c +++ b/src/accounts.c @@ -152,6 +152,7 @@ accounts_get_account(const char * const name) } else { ProfAccount *account = malloc(sizeof(ProfAccount)); account->name = strdup(name); + gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL); if (jid != NULL) { account->jid = strdup(jid); @@ -160,13 +161,16 @@ accounts_get_account(const char * const name) g_key_file_set_string(accounts, name, "jid", name); _save_accounts(); } + account->enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL); + gchar *server = g_key_file_get_string(accounts, name, "server", NULL); if (server != NULL) { account->server = strdup(server); } else { account->server = NULL; } + gchar *resource = g_key_file_get_string(accounts, name, "resource", NULL); if (resource != NULL) { account->resource = strdup(resource); @@ -174,6 +178,24 @@ accounts_get_account(const char * const name) account->resource = NULL; } + gchar *presence = g_key_file_get_string(accounts, name, "presence.last", NULL); + if (presence == NULL || (!presence_valid_string(presence))) { + account->last_presence = strdup("online"); + } else { + account->last_presence = strdup(presence); + } + + presence = g_key_file_get_string(accounts, name, "presence.login", NULL); + if (presence == NULL) { + account->login_presence = strdup("online"); + } else if (strcmp(presence, "last") == 0) { + account->login_presence = strdup("last"); + } else if (!presence_valid_string(presence)) { + account->login_presence = strdup("online"); + } else { + account->login_presence = strdup(presence); + } + return account; } } @@ -186,6 +208,8 @@ accounts_free_account(ProfAccount *account) FREE_SET_NULL(account->jid); FREE_SET_NULL(account->resource); FREE_SET_NULL(account->server); + FREE_SET_NULL(account->last_presence); + FREE_SET_NULL(account->login_presence); FREE_SET_NULL(account); } } @@ -319,19 +343,47 @@ accounts_set_login_presence(const char * const account_name, const char * const } } -void -account_get_login_presence(const char * const account_name, char *str) +jabber_presence_t +accounts_get_last_presence(const char * const account_name) +{ + gchar *setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL); + if (setting == NULL || (strcmp(setting, "online") == 0)) { + return PRESENCE_ONLINE; + } else if (strcmp(setting, "chat") == 0) { + return PRESENCE_CHAT; + } else if (strcmp(setting, "away") == 0) { + return PRESENCE_AWAY; + } else if (strcmp(setting, "xa") == 0) { + return PRESENCE_XA; + } else if (strcmp(setting, "dnd") == 0) { + return PRESENCE_DND; + } else { + log_warning("Error reading presence.last for account: '%s', value: '%s', defaulting to 'online'", + account_name, setting); + return PRESENCE_ONLINE; + } +} + +jabber_presence_t +account_get_login_presence(const char * const account_name) { - static char *online = "online"; gchar *setting = g_key_file_get_string(accounts, account_name, "presence.login", NULL); - if (setting == NULL) { - str = online; - } else if (!presence_valid_string(setting)) { + if (setting == NULL || (strcmp(setting, "online") == 0)) { + return PRESENCE_ONLINE; + } else if (strcmp(setting, "chat") == 0) { + return PRESENCE_CHAT; + } else if (strcmp(setting, "away") == 0) { + return PRESENCE_AWAY; + } else if (strcmp(setting, "xa") == 0) { + return PRESENCE_XA; + } else if (strcmp(setting, "dnd") == 0) { + return PRESENCE_DND; + } else if (strcmp(setting, "last") == 0) { + return accounts_get_last_presence(account_name); + } else { log_warning("Error reading presence.login for account: '%s', value: '%s', defaulting to 'online'", account_name, setting); - str = online; - } else { - str = setting; + return PRESENCE_ONLINE; } } diff --git a/src/accounts.h b/src/accounts.h index 695fa8f1..bf05ba93 100644 --- a/src/accounts.h +++ b/src/accounts.h @@ -23,11 +23,15 @@ #ifndef ACCOUNTS_H #define ACCOUNTS_H +#include "common.h" + typedef struct prof_account_t { gchar *name; gchar *jid; gchar *resource; gchar *server; + gchar *last_presence; + gchar *login_presence; gboolean enabled; } ProfAccount; @@ -52,6 +56,7 @@ void accounts_set_server(const char * const account_name, const char * const val void accounts_set_resource(const char * const account_name, const char * const value); void accounts_set_last_presence(const char * const account_name, const char * const value); void accounts_set_login_presence(const char * const account_name, const char * const value); -void account_get_login_presence(const char * const account_name, char *str); +jabber_presence_t account_get_login_presence(const char * const account_name); +jabber_presence_t accounts_get_last_presence(const char * const account_name); #endif diff --git a/src/common.c b/src/common.c index a58be8f2..61017536 100644 --- a/src/common.c +++ b/src/common.c @@ -246,6 +246,22 @@ release_get_latest() } } +gboolean +presence_valid_string(const char * const str) +{ + if (str == NULL) { + return FALSE; + } else if ((strcmp(str, "online") == 0) || + (strcmp(str, "chat") == 0) || + (strcmp(str, "away") == 0) || + (strcmp(str, "xa") == 0) || + (strcmp(str, "dnd") == 0)) { + return TRUE; + } else { + return FALSE; + } +} + static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data) { diff --git a/src/common.h b/src/common.h index 93d59895..31201b86 100644 --- a/src/common.h +++ b/src/common.h @@ -54,6 +54,15 @@ resource = NULL; \ } +typedef enum { + PRESENCE_OFFLINE, + PRESENCE_ONLINE, + PRESENCE_AWAY, + PRESENCE_DND, + PRESENCE_CHAT, + PRESENCE_XA +} jabber_presence_t; + gchar* p_utf8_substring(const gchar *str, glong start_pos, glong end_pos); void p_slist_free_full(GSList *items, GDestroyNotify free_func); void create_dir(char *name); @@ -64,5 +73,6 @@ char* encode_xml(const char * const xml); char * prof_getline(FILE *stream); int octet_compare(unsigned char *str1, unsigned char *str2); char* release_get_latest(void); +gboolean presence_valid_string(const char * const str); #endif diff --git a/src/ui_windows.c b/src/ui_windows.c index ba755ff2..26e7a197 100644 --- a/src/ui_windows.c +++ b/src/ui_windows.c @@ -1225,16 +1225,22 @@ cons_show_account(ProfAccount *account) { cons_show("%s account details:", account->name); if (account->enabled) { - cons_show("enabled : TRUE"); + cons_show("enabled : TRUE"); } else { - cons_show("enabled : FALSE"); + cons_show("enabled : FALSE"); } - cons_show("jid : %s", account->jid); + cons_show("jid : %s", account->jid); if (account->resource != NULL) { - cons_show("resource : %s", account->resource); + cons_show("resource : %s", account->resource); } if (account->server != NULL) { - cons_show("server : %s", account->server); + cons_show("server : %s", account->server); + } + if (account->last_presence != NULL) { + cons_show("Last presence : %s", account->last_presence); + } + if (account->login_presence != NULL) { + cons_show("Login presence : %s", account->login_presence); } cons_show(""); } diff --git a/src/xmpp.h b/src/xmpp.h index ffe0aae8..bc44ecbf 100644 --- a/src/xmpp.h +++ b/src/xmpp.h @@ -109,15 +109,6 @@ typedef enum { JABBER_DISCONNECTED } jabber_conn_status_t; -typedef enum { - PRESENCE_OFFLINE, - PRESENCE_ONLINE, - PRESENCE_AWAY, - PRESENCE_DND, - PRESENCE_CHAT, - PRESENCE_XA -} jabber_presence_t; - typedef enum { PRESENCE_SUBSCRIBE, PRESENCE_SUBSCRIBED, @@ -178,7 +169,6 @@ void iq_roster_request(void); // presence functions void presence_add_handlers(void); void presence_init(void); -gboolean presence_valid_string(const char * const str); void presence_subscription(const char * const jid, const jabber_subscr_t action); GList* presence_get_subscription_requests(void); void presence_free_sub_requests(void); diff --git a/src/xmpp_iq.c b/src/xmpp_iq.c index 1126f53c..60722080 100644 --- a/src/xmpp_iq.c +++ b/src/xmpp_iq.c @@ -149,11 +149,8 @@ _iq_handle_roster_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, item = xmpp_stanza_get_next(item); } - /* TODO: Save somehow last presence show and use it for initial - * presence rather than PRESENCE_ONLINE. It will be helpful - * when I set dnd status and reconnect for some reason */ - // send initial presence - presence_update(PRESENCE_ONLINE, NULL, 0); + jabber_presence_t connect_presence = account_get_login_presence(jabber_get_account_name()); + presence_update(connect_presence, NULL, 0); } return 1; diff --git a/src/xmpp_presence.c b/src/xmpp_presence.c index f2918812..125e1400 100644 --- a/src/xmpp_presence.c +++ b/src/xmpp_presence.c @@ -56,23 +56,6 @@ presence_add_handlers(void) HANDLE(NULL, NULL, _presence_handler); } -gboolean -presence_valid_string(const char * const str) -{ - if (str == NULL) { - return FALSE; - } else if ((strcmp(str, "online") == 0) || - (strcmp(str, "chat") == 0) || - (strcmp(str, "away") == 0) || - (strcmp(str, "xa") == 0) || - (strcmp(str, "dnd") == 0)) { - return TRUE; - } else { - return FALSE; - } -} - - void presence_subscription(const char * const jid, const jabber_subscr_t action) {