Commit 7dcc906c authored by Mohammed Sadiq's avatar Mohammed Sadiq
Browse files

manager: Add chat-deleted signal and use it

items-changed signal is emitted after the item is removed from the list.
If the removed item is selected_item, it may have already freed.  So the
CHATTY_IS_CHAT (selected_item) is invoking a UB.  Let's fix it.
parent 644ee26e
......@@ -100,6 +100,7 @@ enum {
enum {
AUTHORIZE_BUDDY,
CHAT_DELETED,
NOTIFY_ADDED,
CONNECTION_ERROR,
N_SIGNALS
......@@ -622,26 +623,6 @@ chatty_conv_stack_add_conv (ChattyConversation *chatty_conv)
g_strfreev (name_split);
}
static void
chatty_conv_remove_conv (ChattyConversation *chatty_conv)
{
ChattyWindow *window;
GtkWidget *convs_notebook;
guint index;
window = chatty_application_get_main_window (CHATTY_APPLICATION_DEFAULT ());
convs_notebook = chatty_window_get_convs_notebook (window);
index = gtk_notebook_page_num (GTK_NOTEBOOK(convs_notebook),
chatty_conv->chat_view);
gtk_notebook_remove_page (GTK_NOTEBOOK(convs_notebook), index);
g_debug ("chatty_conv_remove_conv conv");
}
static ChattyConversation *
chatty_conv_find_conv (PurpleConversation * conv)
{
......@@ -767,8 +748,6 @@ chatty_conv_destroy (PurpleConversation *conv)
chatty_conv = CHATTY_CONVERSATION (conv);
chatty_conv_remove_conv (chatty_conv);
g_debug ("chatty_conv_destroy conv");
g_free (chatty_conv);
......@@ -1969,6 +1948,23 @@ chatty_manager_class_init (ChattyManagerClass *klass)
4, CHATTY_TYPE_PP_ACCOUNT, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING);
/**
* ChattyManager::chat-deleted:
* @self: a #ChattyManager
* @chat: A #ChattyChat
*
* Emitted when a chat is deleted. ‘chat-deleted’ is
* emitted just before the chat is actually deleted
* and thus @chat will still point to a valid memory.
*/
signals [CHAT_DELETED] =
g_signal_new ("chat-deleted",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
1, CHATTY_TYPE_CHAT);
/**
* ChattyManager::connection-error:
* @self: a #ChattyManager
......@@ -2424,6 +2420,7 @@ chatty_manager_delete_conversation (ChattyManager *self,
}
if (chat) {
g_signal_emit (self, signals[CHAT_DELETED], 0, chat);
chatty_utils_remove_list_item (G_LIST_STORE (model), chat);
}
}
......
......@@ -140,6 +140,25 @@ window_chat_list_row_new (ChattyItem *item,
return row;
}
static GtkWidget *
window_get_view_for_chat (ChattyWindow *self,
ChattyChat *chat)
{
g_autoptr(GList) children = NULL;
g_assert (CHATTY_IS_WINDOW (self));
g_assert (CHATTY_IS_CHAT (chat));
children = gtk_container_get_children (GTK_CONTAINER (self->convs_notebook));
for (GList *child = children; child; child = child->next)
if (CHATTY_IS_CHAT_VIEW (child->data) &&
chatty_chat_view_get_chat (child->data) == chat)
return child->data;
return NULL;
}
static void
window_set_item (ChattyWindow *self,
ChattyItem *item)
......@@ -196,13 +215,6 @@ window_chat_changed_cb (ChattyWindow *self)
gtk_widget_set_sensitive (self->header_sub_menu_button, has_child);
if (!CHATTY_IS_CHAT (self->selected_item)) {
self->selected_item = NULL;
chatty_avatar_set_item (CHATTY_AVATAR (self->sub_header_icon), NULL);
gtk_label_set_label (GTK_LABEL (self->sub_header_label), "");
}
/*
* When the items are re-arranged, the selection will be lost.
* Re-select it. In GTK4, A #GtkListView with #GtkSingleSelection
......@@ -880,6 +892,23 @@ window_active_protocols_changed_cb (ChattyWindow *self)
window_chat_changed_cb (self);
}
static void
window_chat_deleted_cb (ChattyWindow *self,
ChattyChat *chat)
{
GtkWidget *view;
g_assert (CHATTY_IS_WINDOW (self));
g_assert (CHATTY_IS_CHAT (chat));
if (self->selected_item == (gpointer)chat)
window_set_item (self, NULL);
view = window_get_view_for_chat (self, chat);
if (view)
gtk_widget_destroy (view);
}
static void
chatty_window_unmap (GtkWidget *widget)
......@@ -1049,6 +1078,9 @@ chatty_window_init (ChattyWindow *self)
g_signal_connect_object (self->manager, "notify::active-protocols",
G_CALLBACK (window_active_protocols_changed_cb), self,
G_CONNECT_SWAPPED);
g_signal_connect_object (self->manager, "chat-deleted",
G_CALLBACK (window_chat_deleted_cb), self,
G_CONNECT_SWAPPED);
g_signal_connect_after (G_OBJECT (self->convs_notebook),
"switch-page",
......
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