From 270ac0355857d8a47453fc29f6131f281ba168be Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 31 Oct 2012 23:41:30 +0000 Subject: [PATCH] Fixed invalid state transition from to --- src/chat_session.c | 79 +++++++++++++++++++++++++++++----------------- src/chat_session.h | 8 +++-- src/command.c | 2 +- src/input_win.c | 8 ++--- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/src/chat_session.c b/src/chat_session.c index a0649b76..fe8660b5 100644 --- a/src/chat_session.c +++ b/src/chat_session.c @@ -29,11 +29,11 @@ #include "log.h" #define PAUSED_TIMOUT 10.0 -#define INACTIVE_TIMOUT 120.0 -#define GONE_TIMOUT 600.0 +#define INACTIVE_TIMOUT 20.0 +#define GONE_TIMOUT 30.0 +//#define INACTIVE_TIMOUT 120.0 +//#define GONE_TIMOUT 600.0 -static ChatSession _chat_session_new(const char * const recipient, - gboolean recipient_supports); static void _chat_session_free(ChatSession session); typedef enum { @@ -68,6 +68,18 @@ chat_sessions_clear(void) g_hash_table_remove_all(sessions); } +void +chat_session_start(const char * const recipient, gboolean recipient_supports) +{ + ChatSession new_session = malloc(sizeof(struct chat_session_t)); + new_session->recipient = strdup(recipient); + new_session->recipient_supports = recipient_supports; + new_session->state = CHAT_STATE_STARTED; + new_session->active_timer = g_timer_new(); + new_session->sent = FALSE; + g_hash_table_insert(sessions, strdup(recipient), new_session); +} + gboolean chat_session_exists(const char * const recipient) { @@ -80,13 +92,6 @@ chat_session_exists(const char * const recipient) } } -void -chat_session_start(const char * const recipient, gboolean recipient_supports) -{ - ChatSession session = _chat_session_new(recipient, recipient_supports); - g_hash_table_insert(sessions, strdup(recipient), session); -} - void chat_session_set_composing(const char * const recipient) { @@ -119,13 +124,17 @@ chat_session_no_activity(const char * const recipient) session->sent = FALSE; } session->state = CHAT_STATE_GONE; + } else if (elapsed > INACTIVE_TIMOUT) { if (session->state != CHAT_STATE_INACTIVE) { session->sent = FALSE; } session->state = CHAT_STATE_INACTIVE; + } else if (elapsed > PAUSED_TIMOUT) { - if (session->state != CHAT_STATE_PAUSED) { + + if ((session->state != CHAT_STATE_PAUSED) && + (session->state != CHAT_STATE_ACTIVE)) { session->sent = FALSE; } session->state = CHAT_STATE_PAUSED; @@ -166,7 +175,7 @@ chat_session_end(const char * const recipient) } gboolean -chat_session_inactive(const char * const recipient) +chat_session_is_inactive(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); @@ -178,6 +187,19 @@ chat_session_inactive(const char * const recipient) } } +gboolean +chat_session_is_active(const char * const recipient) +{ + ChatSession session = g_hash_table_lookup(sessions, recipient); + + if (session == NULL) { + log_error("No chat session found for %s.", recipient); + return FALSE; + } else { + return (session->state == CHAT_STATE_ACTIVE); + } +} + void chat_session_set_active(const char * const recipient) { @@ -187,12 +209,12 @@ chat_session_set_active(const char * const recipient) log_error("No chat session found for %s.", recipient); } else { session->state = CHAT_STATE_ACTIVE; - session->sent = FALSE; + session->sent = TRUE; } } gboolean -chat_session_paused(const char * const recipient) +chat_session_is_paused(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); @@ -205,7 +227,7 @@ chat_session_paused(const char * const recipient) } gboolean -chat_session_gone(const char * const recipient) +chat_session_is_gone(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); @@ -217,6 +239,18 @@ chat_session_gone(const char * const recipient) } } +void +chat_session_set_gone(const char * const recipient) +{ + ChatSession session = g_hash_table_lookup(sessions, recipient); + + if (session == NULL) { + log_error("No chat session found for %s.", recipient); + } else { + session->state = CHAT_STATE_GONE; + } +} + gboolean chat_session_get_recipient_supports(const char * const recipient) { @@ -243,19 +277,6 @@ chat_session_set_recipient_supports(const char * const recipient, } } -static ChatSession -_chat_session_new(const char * const recipient, gboolean recipient_supports) -{ - ChatSession new_session = malloc(sizeof(struct chat_session_t)); - new_session->recipient = strdup(recipient); - new_session->recipient_supports = recipient_supports; - new_session->state = CHAT_STATE_STARTED; - new_session->active_timer = g_timer_new(); - new_session->sent = FALSE; - - return new_session; -} - static void _chat_session_free(ChatSession session) { diff --git a/src/chat_session.h b/src/chat_session.h index 34c1681b..43fead5e 100644 --- a/src/chat_session.h +++ b/src/chat_session.h @@ -39,10 +39,12 @@ void chat_session_set_recipient_supports(const char * const recipient, void chat_session_set_composing(const char * const recipient); void chat_session_no_activity(const char * const recipient); -gboolean chat_session_inactive(const char * const recipient); +gboolean chat_session_is_inactive(const char * const recipient); +gboolean chat_session_is_active(const char * const recipient); void chat_session_set_active(const char * const recipient); -gboolean chat_session_paused(const char * const recipient); -gboolean chat_session_gone(const char * const recipient); +gboolean chat_session_is_paused(const char * const recipient); +gboolean chat_session_is_gone(const char * const recipient); +void chat_session_set_gone(const char * const recipient); void chat_session_set_sent(const char * const recipient); gboolean chat_session_get_sent(const char * const recipient); diff --git a/src/command.c b/src/command.c index 03670f7e..956bd0e9 100644 --- a/src/command.c +++ b/src/command.c @@ -1015,7 +1015,7 @@ _cmd_close(const char * const inp, struct cmd_help_t help) // send chat state before closing if (chat_session_get_recipient_supports(recipient)) { - chat_session_gone(recipient); + chat_session_set_gone(recipient); jabber_send_gone(recipient); chat_session_end(recipient); } diff --git a/src/input_win.c b/src/input_win.c index 7defee6a..73720a9d 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -146,13 +146,13 @@ inp_get_char(int *ch, char *input, int *size) char *recipient = win_get_recipient(); chat_session_no_activity(recipient); - if (chat_session_gone(recipient) && + if (chat_session_is_gone(recipient) && !chat_session_get_sent(recipient)) { jabber_send_gone(recipient); - } else if (chat_session_inactive(recipient) && + } else if (chat_session_is_inactive(recipient) && !chat_session_get_sent(recipient)) { jabber_send_inactive(recipient); - } else if (chat_session_paused(recipient) && + } else if (chat_session_is_paused(recipient) && !chat_session_get_sent(recipient)) { jabber_send_paused(recipient); } @@ -165,7 +165,7 @@ inp_get_char(int *ch, char *input, int *size) char *recipient = win_get_recipient(); chat_session_set_composing(recipient); if (!chat_session_get_sent(recipient) || - chat_session_paused(recipient)) { + chat_session_is_paused(recipient)) { jabber_send_composing(recipient); } }