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

210 lines
6.2 KiB
C
Raw Normal View History

2015-01-11 15:20:17 -05:00
/*
* chat_state.c
*
2018-01-21 10:00:02 -05:00
* Copyright (C) 2012 - 2018 James Booth <boothj5@gmail.com>
2015-01-11 15:20:17 -05:00
*
* 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
2016-07-23 20:14:49 -04:00
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
2015-01-11 15:20:17 -05:00
*
* 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 <stdlib.h>
#include <assert.h>
2015-01-11 15:20:17 -05:00
#include <glib.h>
2016-07-24 10:43:51 -04:00
#include "config/preferences.h"
2016-07-24 10:14:46 -04:00
#include "ui/window_list.h"
#include "ui/win_types.h"
2015-01-11 15:20:17 -05:00
#include "xmpp/xmpp.h"
2016-07-24 10:43:51 -04:00
#include "xmpp/chat_state.h"
#include "xmpp/chat_session.h"
2015-01-11 15:20:17 -05:00
#define PAUSED_TIMEOUT 10.0
2015-01-11 18:25:50 -05:00
#define INACTIVE_TIMEOUT 30.0
2015-01-11 15:20:17 -05:00
2015-10-25 20:52:33 -04:00
static void _send_if_supported(const char *const barejid, void (*send_func)(const char *const));
2015-01-11 15:20:17 -05:00
ChatState*
chat_state_new(void)
{
ChatState *new_state = malloc(sizeof(struct prof_chat_state_t));
new_state->type = CHAT_STATE_GONE;
new_state->timer = g_timer_new();
return new_state;
}
void
chat_state_free(ChatState *state)
{
if (state && state->timer!= NULL) {
g_timer_destroy(state->timer);
}
free(state);
}
void
2015-10-25 20:52:33 -04:00
chat_state_handle_idle(const char *const barejid, ChatState *state)
2015-01-11 15:20:17 -05:00
{
2015-01-11 18:25:50 -05:00
gdouble elapsed = g_timer_elapsed(state->timer, NULL);
2015-01-11 15:20:17 -05:00
// TYPING -> PAUSED
2015-01-11 18:25:50 -05:00
if (state->type == CHAT_STATE_COMPOSING && elapsed > PAUSED_TIMEOUT) {
2015-01-11 15:20:17 -05:00
state->type = CHAT_STATE_PAUSED;
g_timer_start(state->timer);
if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) {
_send_if_supported(barejid, message_send_paused);
}
return;
}
// PAUSED|ACTIVE -> INACTIVE
2015-01-11 18:25:50 -05:00
if ((state->type == CHAT_STATE_PAUSED || state->type == CHAT_STATE_ACTIVE) && elapsed > INACTIVE_TIMEOUT) {
2015-01-11 15:20:17 -05:00
state->type = CHAT_STATE_INACTIVE;
g_timer_start(state->timer);
if (prefs_get_boolean(PREF_STATES)) {
_send_if_supported(barejid, message_send_inactive);
}
return;
}
// INACTIVE -> GONE
2015-01-11 18:25:50 -05:00
if (state->type == CHAT_STATE_INACTIVE) {
if (prefs_get_gone() != 0 && (elapsed > (prefs_get_gone() * 60.0))) {
ChatSession *session = chat_session_get(barejid);
if (session) {
// never move to GONE when resource override
if (!session->resource_override) {
if (prefs_get_boolean(PREF_STATES)) {
_send_if_supported(barejid, message_send_gone);
}
chat_session_remove(barejid);
state->type = CHAT_STATE_GONE;
g_timer_start(state->timer);
}
} else {
2015-01-11 15:20:17 -05:00
if (prefs_get_boolean(PREF_STATES)) {
2015-01-11 18:25:50 -05:00
message_send_gone(barejid);
2015-01-11 15:20:17 -05:00
}
state->type = CHAT_STATE_GONE;
g_timer_start(state->timer);
}
2015-01-11 18:25:50 -05:00
return;
2015-01-11 15:20:17 -05:00
}
}
}
void
2015-10-25 20:52:33 -04:00
chat_state_handle_typing(const char *const barejid, ChatState *state)
2015-01-11 15:20:17 -05:00
{
// ACTIVE|INACTIVE|PAUSED|GONE -> COMPOSING
if (state->type != CHAT_STATE_COMPOSING) {
state->type = CHAT_STATE_COMPOSING;
g_timer_start(state->timer);
if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) {
_send_if_supported(barejid, message_send_composing);
}
}
}
void
chat_state_active(ChatState *state)
{
state->type = CHAT_STATE_ACTIVE;
g_timer_start(state->timer);
}
void
2015-10-25 20:52:33 -04:00
chat_state_gone(const char *const barejid, ChatState *state)
2015-01-11 15:20:17 -05:00
{
if (state->type != CHAT_STATE_GONE) {
if (prefs_get_boolean(PREF_STATES)) {
_send_if_supported(barejid, message_send_gone);
}
state->type = CHAT_STATE_GONE;
g_timer_start(state->timer);
}
}
void
chat_state_idle(void)
{
jabber_conn_status_t status = connection_get_status();
if (status == JABBER_CONNECTED) {
GSList *recipients = wins_get_chat_recipients();
GSList *curr = recipients;
while (curr) {
char *barejid = curr->data;
ProfChatWin *chatwin = wins_get_chat(barejid);
chat_state_handle_idle(chatwin->barejid, chatwin->state);
curr = g_slist_next(curr);
}
if (recipients) {
g_slist_free(recipients);
}
}
}
void
chat_state_activity(void)
{
jabber_conn_status_t status = connection_get_status();
ProfWin *current = wins_get_current();
if ((status == JABBER_CONNECTED) && (current->type == WIN_CHAT)) {
ProfChatWin *chatwin = (ProfChatWin*)current;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
chat_state_handle_typing(chatwin->barejid, chatwin->state);
}
}
2015-01-11 15:20:17 -05:00
static void
2015-10-25 20:52:33 -04:00
_send_if_supported(const char *const barejid, void (*send_func)(const char *const))
2015-01-11 15:20:17 -05:00
{
gboolean send = TRUE;
GString *jid = g_string_new(barejid);
ChatSession *session = chat_session_get(barejid);
if (session) {
if (session->send_states) {
g_string_append(jid, "/");
g_string_append(jid, session->resource);
} else {
send = FALSE;
}
}
if (send) {
send_func(jid->str);
}
g_string_free(jid, TRUE);
2015-10-25 20:52:33 -04:00
}