Commit 644ee26e authored by Mohammed Sadiq's avatar Mohammed Sadiq
Browse files

Add API to get active chat and use it

parent 8ee3bbe7
......@@ -381,3 +381,25 @@ chatty_application_get_main_window (ChattyApplication *self)
return NULL;
}
/**
* chatty_application_get_active_chat:
* @self: A #ChattyApplication
*
* Get the currently shown chat
*
* Returns: (transfer none): A #ChattyChat if a chat
* is shown. %NULL if no chat is shown. If window is
* hidden, %NULL is returned regardless of wether a
* chat is shown or not.
*/
ChattyChat *
chatty_application_get_active_chat (ChattyApplication *self)
{
g_return_val_if_fail (CHATTY_IS_APPLICATION (self), NULL);
if (self->main_window)
return chatty_window_get_active_chat (CHATTY_WINDOW (self->main_window));
return NULL;
}
......@@ -36,5 +36,6 @@ G_DECLARE_FINAL_TYPE (ChattyApplication, chatty_application, CHATTY, APPLICATION
ChattyApplication *chatty_application_new (void);
ChattyWindow *chatty_application_get_main_window (ChattyApplication *self);
ChattyChat *chatty_application_get_active_chat (ChattyApplication *self);
G_END_DECLS
......@@ -20,37 +20,6 @@
#include "chatty-chat-view.h"
/**
* chatty_conv_container_get_active_chatty_conv:
* @notebook: a GtkNotebook
*
* Returns the chatty conversation that is
* currently set active in the notebook
*
* Returns: ChattyConversation
*
*/
ChattyConversation *
chatty_conv_container_get_active_chatty_conv (GtkNotebook *notebook)
{
int index;
GtkWidget *tab_cont;
index = gtk_notebook_get_current_page (GTK_NOTEBOOK(notebook));
if (index == -1) {
index = 0;
}
tab_cont = gtk_notebook_get_nth_page (GTK_NOTEBOOK(notebook), index);
if (!tab_cont) {
return NULL;
}
return g_object_get_data (G_OBJECT(tab_cont), "ChattyConversation");
}
/**
* chatty_conv_im_with_buddy:
* @account: a PurpleAccount
......
......@@ -36,7 +36,6 @@ typedef enum
void chatty_conv_im_with_buddy (PurpleAccount *account, const char *username);
void chatty_conv_show_conversation (PurpleConversation *conv);
void chatty_conv_join_chat (PurpleChat *chat);
ChattyConversation * chatty_conv_container_get_active_chatty_conv (GtkNotebook *notebook);
void chatty_conv_add_history_since_component(GHashTable *components, const char *account, const char *room);
......
......@@ -742,9 +742,6 @@ 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);
g_object_set_data (G_OBJECT (chatty_conv->chat_view),
"ChattyConversation",
chatty_conv);
gtk_widget_show (chatty_conv->chat_view);
......@@ -861,17 +858,13 @@ chatty_conv_write_conversation (PurpleConversation *conv,
ChattyChat *chat;
g_autoptr(ChattyMessage) chat_message = NULL;
ChattyConversation *chatty_conv;
ChattyConversation *active_chatty_conv;
ChattyManager *self;
PurpleConversationType type;
PurpleConnection *gc;
PurpleAccount *account;
PurpleBuddy *buddy = NULL;
PurpleBlistNode *node;
gboolean conv_active;
GdkPixbuf *avatar = NULL;
ChattyWindow *window;
GtkWidget *convs_notebook;
const char *buddy_name;
gchar *titel;
g_autofree char *uuid = NULL;
......@@ -960,16 +953,12 @@ chatty_conv_write_conversation (PurpleConversation *conv,
chat_message = chatty_message_new (NULL, NULL, message, uuid, 0, CHATTY_DIRECTION_SYSTEM, 0);
chatty_chat_append_message (chat, chat_message);
} else if (pcm.flags & PURPLE_MESSAGE_RECV) {
ChattyChat *active_chat;
window = chatty_application_get_main_window (CHATTY_APPLICATION_DEFAULT ());
active_chat = chatty_application_get_active_chat (CHATTY_APPLICATION_DEFAULT ());
convs_notebook = chatty_window_get_convs_notebook (window);
active_chatty_conv = chatty_conv_container_get_active_chatty_conv (GTK_NOTEBOOK(convs_notebook));
conv_active = (chatty_conv == active_chatty_conv && gtk_widget_is_drawable (convs_notebook));
if (buddy && purple_blist_node_get_bool (node, "chatty-notifications") && !conv_active) {
if (buddy && purple_blist_node_get_bool (node, "chatty-notifications") &&
active_chat != chat) {
g_autoptr(GdkPixbuf) image = NULL;
ChattyPpBuddy *pp_buddy;
......
......@@ -1133,3 +1133,23 @@ chatty_window_get_convs_notebook (ChattyWindow *self)
return self->convs_notebook;
}
ChattyChat *
chatty_window_get_active_chat (ChattyWindow *self)
{
GtkWidget *child;
gint current_page;
g_return_val_if_fail (CHATTY_IS_WINDOW (self), NULL);
current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK (self->convs_notebook));
if (current_page == -1)
return NULL;
child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (self->convs_notebook), current_page);
if (!gtk_widget_is_drawable (child))
return NULL;
return chatty_chat_view_get_chat (CHATTY_CHAT_VIEW (child));
}
......@@ -9,6 +9,8 @@
#include <gtk/gtk.h>
#include "chatty-chat.h"
#include "chatty-settings.h"
G_BEGIN_DECLS
......@@ -47,6 +49,7 @@ 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);
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