From 9593e17c3c414a4a78ec73bfee8464a78fa28dc1 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 27 Sep 2015 23:53:06 +0100 Subject: [PATCH] Include last activity in initial presence --- src/config/accounts.c | 11 +++++++++++ src/config/accounts.h | 1 + src/event/client_events.c | 4 ++-- src/event/client_events.h | 4 ++-- src/xmpp/roster.c | 26 ++++++++++++++++++++++++-- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/config/accounts.c b/src/config/accounts.c index 9b838f59..0abfc0d4 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -786,11 +786,22 @@ accounts_set_last_activity(const char * const account_name) if (res) { char *timestr = g_time_val_to_iso8601(&nowtv); g_key_file_set_string(accounts, account_name, "last.activity", timestr); + free(timestr); _save_accounts(); } } } +char * +accounts_get_last_activity(const char * const account_name) +{ + if (accounts_account_exists(account_name)) { + return g_key_file_get_string(accounts, account_name, "last.activity", NULL); + } else { + return NULL; + } +} + void accounts_set_login_presence(const char * const account_name, const char * const value) { diff --git a/src/config/accounts.h b/src/config/accounts.h index 14bdbf97..b64cafa6 100644 --- a/src/config/accounts.h +++ b/src/config/accounts.h @@ -68,6 +68,7 @@ void accounts_set_otr_policy(const char * const account_name, const char * const void accounts_set_last_presence(const char * const account_name, const char * const value); void accounts_set_last_status(const char * const account_name, const char * const value); void accounts_set_last_activity(const char * const account_name); +char * accounts_get_last_activity(const char * const account_name); void accounts_set_login_presence(const char * const account_name, const char * const value); resource_presence_t accounts_get_login_presence(const char * const account_name); char * accounts_get_last_status(const char * const account_name); diff --git a/src/event/client_events.c b/src/event/client_events.c index 761452df..c3e5070d 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -65,7 +65,7 @@ cl_ev_connect_account(ProfAccount *account) } void -cl_ev_presence_send(const resource_presence_t presence_type, const char * const msg, const int idle) +cl_ev_presence_send(const resource_presence_t presence_type, const char * const msg, const int idle_secs) { char *signed_status = NULL; @@ -78,7 +78,7 @@ cl_ev_presence_send(const resource_presence_t presence_type, const char * const account_free(account); #endif - presence_send(presence_type, msg, idle, signed_status); + presence_send(presence_type, msg, idle_secs, signed_status); free(signed_status); } diff --git a/src/event/client_events.h b/src/event/client_events.h index 207299c5..ea1bdef7 100644 --- a/src/event/client_events.h +++ b/src/event/client_events.h @@ -38,10 +38,10 @@ jabber_conn_status_t cl_ev_connect_jid(const char * const jid, const char * const passwd, const char * const altdomain, const int port); jabber_conn_status_t cl_ev_connect_account(ProfAccount *account); -void cl_ev_presence_send(const resource_presence_t presence_type, const char * const msg, const int idle); +void cl_ev_presence_send(const resource_presence_t presence_type, const char * const msg, const int idle_secs); void cl_ev_send_msg(ProfChatWin *chatwin, const char * const msg); void cl_ev_send_muc_msg(ProfMucWin *mucwin, const char * const msg); void cl_ev_send_priv_msg(ProfPrivateWin *privwin, const char * const msg); -#endif \ No newline at end of file +#endif diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index 55695d75..020f01f6 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -342,8 +342,30 @@ _roster_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, v sv_ev_roster_received(); - resource_presence_t conn_presence = accounts_get_login_presence(jabber_get_account_name()); - cl_ev_presence_send(conn_presence, NULL, 0); + char *account = jabber_get_account_name(); + resource_presence_t conn_presence = accounts_get_login_presence(account); + + char *last_activity_str = accounts_get_last_activity(account); + if (last_activity_str) { + GDateTime *nowdt = g_date_time_new_now_utc(); + + GTimeVal lasttv; + gboolean res = g_time_val_from_iso8601(last_activity_str, &lasttv); + if (res) { + GDateTime *lastdt = g_date_time_new_from_timeval_utc(&lasttv); + GTimeSpan diff_micros = g_date_time_difference(nowdt, lastdt); + int diff_secs = (diff_micros / 1000) / 1000; + cl_ev_presence_send(conn_presence, NULL, diff_secs); + g_date_time_unref(lastdt); + } else { + cl_ev_presence_send(conn_presence, NULL, 0); + } + + free(last_activity_str); + g_date_time_unref(nowdt); + } else { + cl_ev_presence_send(conn_presence, NULL, 0); + } return 1; }