Commit 7370008a authored by Mohammed Sadiq's avatar Mohammed Sadiq
Browse files

chat-view: Add draft message support

parent 556a1d54
......@@ -9,6 +9,8 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#define G_LOG_DOMAIN "chatty-chat-view"
#include <glib/gi18n.h>
#include <gspell/gspell.h>
......@@ -26,6 +28,7 @@
#include "chatty-message-row.h"
#include "chatty-attachments-view.h"
#include "chatty-chat-view.h"
#include "chatty-log.h"
struct _ChattyChatView
{
......@@ -51,12 +54,17 @@ struct _ChattyChatView
GtkWidget *scroll_down_button;
GtkTextBuffer *message_input_buffer;
GtkAdjustment *vadjustment;
ChattyHistory *history;
ChattyChat *chat;
ChattyMessage *draft_message;
guint refresh_typing_id;
guint save_timeout_id;
gboolean draft_is_loading;
gboolean first_scroll_to_bottom;
};
#define SAVE_TIMEOUT 300 /* milliseconds */
#define INDICATOR_WIDTH 60
#define INDICATOR_HEIGHT 40
#define INDICATOR_MARGIN 2
......@@ -495,6 +503,9 @@ chat_view_send_message_button_clicked_cb (ChattyChatView *self)
if (CHATTY_IS_PP_CHAT (self->chat) &&
chatty_pp_chat_run_command (CHATTY_PP_CHAT (self->chat), message)) {
g_clear_handle_id (&self->save_timeout_id, g_source_remove);
g_clear_object (&self->draft_message);
gtk_widget_hide (self->send_message_button);
gtk_text_buffer_delete (self->message_input_buffer, &start, &end);
......@@ -503,7 +514,7 @@ chat_view_send_message_button_clicked_cb (ChattyChatView *self)
account = chatty_chat_get_account (self->chat);
if (chatty_account_get_status (account) != CHATTY_CONNECTED)
return;
goto end;
gtk_widget_grab_focus (self->message_input);
......@@ -534,6 +545,9 @@ chat_view_send_message_button_clicked_cb (ChattyChatView *self)
chatty_attachments_view_reset (CHATTY_ATTACHMENTS_VIEW (self->attachment_view));
gtk_text_buffer_delete (self->message_input_buffer, &start, &end);
end:
g_clear_handle_id (&self->save_timeout_id, g_source_remove);
g_clear_object (&self->draft_message);
}
static gboolean
......@@ -555,6 +569,44 @@ chat_view_input_key_pressed_cb (ChattyChatView *self,
return FALSE;
}
static gboolean
chat_view_save_message_to_db (gpointer user_data)
{
ChattyChatView *self = user_data;
g_autofree char *escaped = NULL;
g_autofree char *text = NULL;
ChattyProtocol protocol;
g_assert (CHATTY_IS_CHAT_VIEW (self));
g_clear_handle_id (&self->save_timeout_id, g_source_remove);
g_return_val_if_fail (self->chat, G_SOURCE_REMOVE);
CHATTY_TRACE (chatty_chat_get_chat_name (self->chat), "Saving draft to");
protocol = chatty_item_get_protocols (CHATTY_ITEM (self->chat));
g_object_get (self->message_input_buffer, "text", &text, NULL);
if (protocol == CHATTY_PROTOCOL_MATRIX ||
protocol == CHATTY_PROTOCOL_XMPP ||
protocol == CHATTY_PROTOCOL_TELEGRAM)
escaped = purple_markup_escape_text (text, -1);
if (!self->draft_message) {
g_autofree char *uid = g_uuid_string_random ();
self->draft_message = chatty_message_new (NULL, NULL,
uid, time (NULL),
escaped ? CHATTY_MESSAGE_HTML_ESCAPED : CHATTY_MESSAGE_TEXT,
CHATTY_DIRECTION_OUT, CHATTY_STATUS_DRAFT);
}
chatty_message_set_text (self->draft_message, escaped ? escaped : text);
chatty_history_add_message (self->history, self->chat, self->draft_message);
gtk_text_buffer_set_modified (self->message_input_buffer, FALSE);
return G_SOURCE_REMOVE;
}
static void
chat_view_message_input_changed_cb (ChattyChatView *self)
......@@ -563,9 +615,17 @@ chat_view_message_input_changed_cb (ChattyChatView *self)
g_assert (CHATTY_IS_CHAT_VIEW (self));
g_clear_handle_id (&self->save_timeout_id, g_source_remove);
has_text = gtk_text_buffer_get_char_count (self->message_input_buffer) > 0;
gtk_widget_set_visible (self->send_message_button, has_text);
if (!self->draft_is_loading)
self->save_timeout_id = g_timeout_add (SAVE_TIMEOUT,
chat_view_save_message_to_db,
self);
if (self->draft_is_loading)
return;
if (chatty_settings_get_send_typing (chatty_settings_get_default ()))
chatty_update_typing_status (self);
......@@ -691,6 +751,21 @@ chat_view_file_requested_cb (ChattyChatView *self,
g_object_ref (self));
}
static void
chat_get_draft_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
g_autoptr(ChattyChatView) self = user_data;
g_autofree char *draft = NULL;
draft = chatty_history_get_draft_finish (self->history, result, NULL);
self->draft_is_loading = TRUE;
g_object_set (self->message_input_buffer, "text", draft ? draft : "", NULL);
self->draft_is_loading = FALSE;
}
static void
chatty_chat_view_map (GtkWidget *widget)
{
......@@ -706,6 +781,9 @@ chatty_chat_view_finalize (GObject *object)
{
ChattyChatView *self = (ChattyChatView *)object;
g_clear_handle_id (&self->save_timeout_id, g_source_remove);
g_clear_object (&self->draft_message);
g_clear_object (&self->history);
g_clear_object (&self->chat);
G_OBJECT_CLASS (chatty_chat_view_parent_class)->finalize (object);
......@@ -827,6 +905,8 @@ chatty_chat_view_set_chat (ChattyChatView *self,
gtk_widget_hide (self->scroll_down_button);
self->first_scroll_to_bottom = FALSE;
g_clear_handle_id (&self->save_timeout_id, g_source_remove);
g_clear_object (&self->draft_message);
}
gtk_widget_set_sensitive (self->message_input, !!chat);
......@@ -858,6 +938,9 @@ chatty_chat_view_set_chat (ChattyChatView *self,
if (g_list_model_get_n_items (messages) <= 3)
chatty_chat_load_past_messages (chat, -1);
chatty_history_get_draft_async (self->history, self->chat,
chat_get_draft_cb,
g_object_ref (self));
if (account)
g_signal_connect_object (account, "notify::status",
......@@ -897,3 +980,13 @@ chatty_chat_view_get_chat (ChattyChatView *self)
return self->chat;
}
void
chatty_chat_view_set_db (ChattyChatView *self,
ChattyHistory *history)
{
g_return_if_fail (CHATTY_IS_CHAT_VIEW (self));
g_return_if_fail (CHATTY_IS_HISTORY (history));
g_set_object (&self->history, history);
}
......@@ -13,6 +13,7 @@
#include <gtk/gtk.h>
#include "chatty-history.h"
#include "chatty-chat.h"
G_BEGIN_DECLS
......@@ -25,5 +26,7 @@ GtkWidget *chatty_chat_view_new (void);
void chatty_chat_view_set_chat (ChattyChatView *self,
ChattyChat *chat);
ChattyChat *chatty_chat_view_get_chat (ChattyChatView *self);
void chatty_chat_view_set_db (ChattyChatView *self,
ChattyHistory *history);
G_END_DECLS
......@@ -986,6 +986,8 @@ chatty_window_init (ChattyWindow *self)
hdy_search_bar_connect_entry (HDY_SEARCH_BAR (self->chats_search_bar),
GTK_ENTRY (self->chats_search_entry));
self->manager = g_object_ref (chatty_manager_get_default ());
chatty_chat_view_set_db (CHATTY_CHAT_VIEW (self->chat_view),
chatty_manager_get_history (self->manager));
g_signal_connect_object (self->manager, "notify::active-protocols",
G_CALLBACK (window_active_protocols_changed_cb), self,
G_CONNECT_SWAPPED);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment