Commit 48c3703b authored by Mohammed Sadiq's avatar Mohammed Sadiq
Browse files

manager: Add open-chat signal and use it

This will help us dissociate libpurple handling UI and isolate
UI code from program logic.
parent 7dcc906c
......@@ -173,6 +173,21 @@ application_show_connection_error (ChattyApplication *self,
run_dialog_and_destroy (GTK_DIALOG (dialog));
}
static void
application_open_chat (ChattyApplication *self,
ChattyChat *chat)
{
g_assert (CHATTY_IS_APPLICATION (self));
g_assert (CHATTY_IS_CHAT (chat));
if (!self->main_window) {
self->main_window = chatty_window_new (GTK_APPLICATION (self));
g_object_add_weak_pointer (G_OBJECT (self->main_window), (gpointer *)&self->main_window);
}
chatty_window_open_chat (CHATTY_WINDOW (self->main_window), chat);
}
static gboolean
application_open_uri (ChattyApplication *self)
{
......@@ -318,6 +333,10 @@ chatty_application_activate (GApplication *application)
G_CALLBACK (gtk_widget_hide_on_delete),
NULL);
g_signal_connect_object (self->manager, "open-chat",
G_CALLBACK (application_open_chat), self,
G_CONNECT_SWAPPED);
if (self->show_window)
gtk_window_present (GTK_WINDOW (self->main_window));
......
......@@ -103,6 +103,7 @@ enum {
CHAT_DELETED,
NOTIFY_ADDED,
CONNECTION_ERROR,
OPEN_CHAT,
N_SIGNALS
};
......@@ -561,68 +562,6 @@ on_feedback_triggered (LfbEvent *event,
}
}
static void
chatty_conv_switch_conv (ChattyConversation *chatty_conv)
{
ChattyWindow *window;
GtkWidget *convs_notebook;
int page_num;
window = chatty_application_get_main_window (CHATTY_APPLICATION_DEFAULT ());
convs_notebook = chatty_window_get_convs_notebook (window);
page_num = gtk_notebook_page_num (GTK_NOTEBOOK(convs_notebook),
chatty_conv->chat_view);
gtk_notebook_set_current_page (GTK_NOTEBOOK(convs_notebook),
page_num);
g_debug ("chatty_conv_switch_conv active_conv: %s page_num %i",
purple_conversation_get_name (chatty_conv->conv), page_num);
}
static void
chatty_conv_stack_add_conv (ChattyConversation *chatty_conv)
{
ChattyWindow *window;
PurpleConversation *conv = chatty_conv->conv;
GtkWidget *convs_notebook;
const gchar *tab_txt;
gchar *text;
gchar **name_split;
window = chatty_application_get_main_window (CHATTY_APPLICATION_DEFAULT ());
convs_notebook = chatty_window_get_convs_notebook (window);
tab_txt = purple_conversation_get_title (conv);
gtk_notebook_append_page (GTK_NOTEBOOK(convs_notebook),
chatty_conv->chat_view, NULL);
name_split = g_strsplit (tab_txt, "@", -1);
text = g_strdup_printf ("%s %s",name_split[0], " >");
gtk_notebook_set_tab_label_text (GTK_NOTEBOOK(convs_notebook),
chatty_conv->chat_view, text);
gtk_widget_show (chatty_conv->chat_view);
gtk_notebook_set_current_page (GTK_NOTEBOOK(convs_notebook), 0);
if (purple_prefs_get_bool (CHATTY_PREFS_ROOT "/conversations/show_tabs")) {
gtk_notebook_set_show_tabs (GTK_NOTEBOOK(convs_notebook), TRUE);
} else {
gtk_notebook_set_show_tabs (GTK_NOTEBOOK(convs_notebook), FALSE);
}
g_free (text);
g_strfreev (name_split);
}
static ChattyConversation *
chatty_conv_find_conv (PurpleConversation * conv)
{
......@@ -720,14 +659,7 @@ chatty_conv_new (PurpleConversation *conv)
}
}
chatty_conv->chat_view = chatty_chat_view_new ();
chat = chatty_manager_add_conversation (chatty_manager_get_default (), chatty_conv->conv);
chatty_chat_view_set_chat (CHATTY_CHAT_VIEW (chatty_conv->chat_view), chat);
gtk_widget_show (chatty_conv->chat_view);
chatty_conv_stack_add_conv (chatty_conv);
conv_node = chatty_utils_get_conv_blist_node (conv);
if (conv_node != NULL &&
......@@ -737,7 +669,7 @@ chatty_conv_new (PurpleConversation *conv)
purple_conversation_set_logging (conv, purple_value_get_boolean (value));
}
chatty_manager_load_more_chat (chatty_manager_get_default (), chat, LAZY_LOAD_INITIAL_MSGS_LIMIT);
chatty_manager_load_more_chat (chatty_manager_get_default (), chat, 1);
}
......@@ -1015,13 +947,16 @@ chatty_conv_muc_list_update_user (PurpleConversation *conv,
static void
chatty_conv_present_conversation (PurpleConversation *conv)
{
ChattyConversation *chatty_conv;
ChattyManager *self;
ChattyChat *chat;
chatty_conv = CHATTY_CONVERSATION (conv);
self = chatty_manager_get_default ();
chat = chatty_manager_find_purple_conv (self, conv);
g_return_if_fail (chat);
g_debug ("chatty_conv_present_conversation conv: %s", purple_conversation_get_name (conv));
g_debug ("%s conversation: %s", G_STRFUNC, chatty_item_get_name (CHATTY_ITEM (chat)));
chatty_conv_switch_conv (chatty_conv);
g_signal_emit (self, signals[OPEN_CHAT], 0, chat);
}
......@@ -1998,6 +1933,22 @@ chatty_manager_class_init (ChattyManagerClass *klass)
0, NULL, NULL, NULL,
G_TYPE_NONE,
3, CHATTY_TYPE_PP_ACCOUNT, G_TYPE_STRING, G_TYPE_STRING);
/**
* ChattyManager::open-chat:
* @self: A #ChattyManager
* @chat: A #ChattyChat
*
* Emitted when user requests to open a chat. UI can hook
* to this signal to do whatever appropriate.
*/
signals [OPEN_CHAT] =
g_signal_new ("open-chat",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
1, CHATTY_TYPE_CHAT);
}
static void
......
......@@ -31,6 +31,7 @@
#include "dialogs/chatty-user-info-dialog.h"
#include "dialogs/chatty-muc-info-dialog.h"
#define LAZY_LOAD_INITIAL_MSGS_LIMIT 20
struct _ChattyWindow
{
......@@ -1085,6 +1086,11 @@ chatty_window_init (ChattyWindow *self)
g_signal_connect_after (G_OBJECT (self->convs_notebook),
"switch-page",
G_CALLBACK (window_notebook_after_switch_cb), self);
if (purple_prefs_get_bool (CHATTY_PREFS_ROOT "/conversations/show_tabs"))
gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self->convs_notebook), TRUE);
else
gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self->convs_notebook), FALSE);
}
......@@ -1158,14 +1164,6 @@ chatty_window_set_uri (ChattyWindow *self,
g_free (who);
}
GtkWidget *
chatty_window_get_convs_notebook (ChattyWindow *self)
{
g_return_val_if_fail (CHATTY_IS_WINDOW (self), NULL);
return self->convs_notebook;
}
ChattyChat *
chatty_window_get_active_chat (ChattyWindow *self)
{
......@@ -1185,3 +1183,41 @@ chatty_window_get_active_chat (ChattyWindow *self)
return chatty_chat_view_get_chat (CHATTY_CHAT_VIEW (child));
}
void
chatty_window_open_chat (ChattyWindow *self,
ChattyChat *chat)
{
GtkWidget *view;
int page_num;
g_return_if_fail (CHATTY_IS_WINDOW (self));
g_return_if_fail (CHATTY_IS_CHAT (chat));
view = window_get_view_for_chat (self, chat);
if (!view) {
const char *name;
g_auto(GStrv) split = NULL;
g_autofree char *label = NULL;
view = chatty_chat_view_new ();
name = chatty_item_get_name (CHATTY_ITEM (chat));
split = g_strsplit (name, "@", -1);
label = g_strdup_printf ("%s %s", split[0], " >");
chatty_chat_view_set_chat (CHATTY_CHAT_VIEW (view), chat);
gtk_widget_show (view);
gtk_container_add (GTK_CONTAINER (self->convs_notebook), view);
gtk_notebook_set_tab_label_text (GTK_NOTEBOOK(self->convs_notebook), view, label);
chatty_manager_load_more_chat (chatty_manager_get_default (), chat, LAZY_LOAD_INITIAL_MSGS_LIMIT);
}
page_num = gtk_notebook_page_num (GTK_NOTEBOOK (self->convs_notebook), view);
gtk_notebook_set_current_page (GTK_NOTEBOOK (self->convs_notebook), page_num);
hdy_leaflet_set_visible_child (HDY_LEAFLET (self->content_box), self->convs_notebook);
gtk_window_present (GTK_WINDOW (self));
}
......@@ -46,10 +46,10 @@ void chatty_window_set_uri (ChattyWindow *self,
void chatty_window_change_view (ChattyWindow *self, guint state);
GtkWidget *chatty_window_get_convs_notebook (ChattyWindow *self);
void chatty_window_chat_list_select_first (ChattyWindow *self);
ChattyChat *chatty_window_get_active_chat (ChattyWindow *self);
void chatty_window_open_chat (ChattyWindow *self,
ChattyChat *chat);
G_END_DECLS
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