[empathy: 1/4] Add chat manager which tracks chats closing and can respawn them (bug #609832)



commit d95165b819c15207ad0a133ea38684043a3776fc
Author: Jonny Lamb <jonnylamb gnome org>
Date:   Sun Feb 28 19:08:43 2010 +0000

    Add chat manager which tracks chats closing and can respawn them (bug #609832)
    
    Signed-off-by: Jonny Lamb <jonnylamb gnome org>

 src/Makefile.am            |    1 +
 src/empathy-chat-manager.c |  137 ++++++++++++++++++++++++++++++++++++++++++++
 src/empathy-chat-manager.h |   69 ++++++++++++++++++++++
 src/empathy-chat-window.c  |   20 +++++++
 src/empathy-chat-window.ui |    8 +++
 src/empathy-main-window.c  |   20 +++++++
 src/empathy.c              |    6 ++
 7 files changed, 261 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 3f8d157..ae92c67 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -109,6 +109,7 @@ empathy_handwritten_source = \
 	empathy-preferences.c empathy-preferences.h			\
 	empathy-sidebar.c empathy-sidebar.h				\
 	empathy-status-icon.c empathy-status-icon.h			\
+	empathy-chat-manager.c empathy-chat-manager.h			\
 	empathy.c
 
 empathy_SOURCES =							\
diff --git a/src/empathy-chat-manager.c b/src/empathy-chat-manager.c
new file mode 100644
index 0000000..051d07b
--- /dev/null
+++ b/src/empathy-chat-manager.c
@@ -0,0 +1,137 @@
+/*
+ * empathy-chat-manager.c - Source for EmpathyChatManager
+ * Copyright (C) 2010 Collabora Ltd.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <libempathy/empathy-dispatcher.h>
+
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include <libempathy/empathy-debug.h>
+
+#include "empathy-chat-manager.h"
+
+G_DEFINE_TYPE(EmpathyChatManager, empathy_chat_manager, G_TYPE_OBJECT)
+
+/* private structure */
+typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
+
+struct _EmpathyChatManagerPriv
+{
+  GQueue *queue;
+};
+
+#define GET_PRIV(o) \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
+    EmpathyChatManagerPriv))
+
+static EmpathyChatManager *chat_manager_singleton = NULL;
+
+static void
+empathy_chat_manager_init (EmpathyChatManager *self)
+{
+  EmpathyChatManagerPriv *priv = GET_PRIV (self);
+
+  priv->queue = g_queue_new ();
+}
+
+static void
+empathy_chat_manager_finalize (GObject *object)
+{
+  EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
+  EmpathyChatManagerPriv *priv = GET_PRIV (self);
+
+  if (priv->queue != NULL)
+    {
+      g_queue_foreach (priv->queue, (GFunc) g_object_unref, NULL);
+      g_queue_free (priv->queue);
+      priv->queue = NULL;
+    }
+
+  G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
+}
+
+static GObject *
+empathy_chat_manager_constructor (GType type,
+    guint n_construct_params,
+    GObjectConstructParam *construct_params)
+{
+  GObject *retval;
+
+  if (!chat_manager_singleton)
+    {
+      retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
+        (type, n_construct_params, construct_params);
+
+      chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
+      g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
+    }
+  else
+    {
+      retval = g_object_ref (chat_manager_singleton);
+    }
+
+  return retval;
+}
+
+static void
+empathy_chat_manager_class_init (
+  EmpathyChatManagerClass *empathy_chat_manager_class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
+
+  object_class->finalize = empathy_chat_manager_finalize;
+  object_class->constructor = empathy_chat_manager_constructor;
+
+  g_type_class_add_private (empathy_chat_manager_class,
+    sizeof (EmpathyChatManagerPriv));
+}
+
+EmpathyChatManager *
+empathy_chat_manager_dup_singleton (void)
+{
+  return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
+}
+
+void
+empathy_chat_manager_closed_chat (EmpathyChatManager *self,
+    EmpathyContact *contact)
+{
+  EmpathyChatManagerPriv *priv = GET_PRIV (self);
+
+  DEBUG ("Adding contact to queue: %s", empathy_contact_get_id (contact));
+
+  g_queue_push_tail (priv->queue, g_object_ref (contact));
+}
+
+void
+empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
+{
+  EmpathyChatManagerPriv *priv = GET_PRIV (self);
+  EmpathyContact *contact;
+
+  contact = g_queue_pop_tail (priv->queue);
+
+  if (contact == NULL)
+    return;
+
+  DEBUG ("Removing contact from queue and starting a chat with them: %s",
+      empathy_contact_get_id (contact));
+
+  empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
+
+  g_object_unref (contact);
+}
diff --git a/src/empathy-chat-manager.h b/src/empathy-chat-manager.h
new file mode 100644
index 0000000..af4d977
--- /dev/null
+++ b/src/empathy-chat-manager.h
@@ -0,0 +1,69 @@
+/*
+ * empathy-chat-manager.h - Header for EmpathyChatManager
+ * Copyright (C) 2010 Collabora Ltd.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __EMPATHY_CHAT_MANAGER_H__
+#define __EMPATHY_CHAT_MANAGER_H__
+
+#include <glib-object.h>
+
+#include <libempathy/empathy-contact.h>
+
+G_BEGIN_DECLS
+
+typedef struct _EmpathyChatManager EmpathyChatManager;
+typedef struct _EmpathyChatManagerClass EmpathyChatManagerClass;
+
+struct _EmpathyChatManagerClass
+{
+  GObjectClass parent_class;
+};
+
+struct _EmpathyChatManager
+{
+  GObject parent;
+};
+
+GType empathy_chat_manager_get_type (void);
+
+/* TYPE MACROS */
+#define EMPATHY_TYPE_CHAT_MANAGER \
+  (empathy_chat_manager_get_type ())
+#define EMPATHY_CHAT_MANAGER(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj), EMPATHY_TYPE_CHAT_MANAGER, \
+    EmpathyChatManager))
+#define EMPATHY_CHAT_MANAGER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass), EMPATHY_TYPE_CHAT_MANAGER, \
+    EmpathyChatManagerClass))
+#define EMPATHY_IS_CHAT_MANAGER(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj), EMPATHY_TYPE_CHAT_MANAGER))
+#define EMPATHY_IS_CHAT_MANAGER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass), EMPATHY_TYPE_CHAT_MANAGER))
+#define EMPATHY_CHAT_MANAGER_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), EMPATHY_TYPE_CHAT_MANAGER, \
+    EmpathyChatManagerClass))
+
+EmpathyChatManager *empathy_chat_manager_dup_singleton (void);
+
+void empathy_chat_manager_closed_chat (EmpathyChatManager *self,
+    EmpathyContact *contact);
+void empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self);
+
+G_END_DECLS
+
+#endif /* #ifndef __EMPATHY_CHAT_MANAGER_H__*/
diff --git a/src/empathy-chat-window.c b/src/empathy-chat-window.c
index 2e2f5c0..50543f2 100644
--- a/src/empathy-chat-window.c
+++ b/src/empathy-chat-window.c
@@ -54,6 +54,7 @@
 #include <libempathy-gtk/empathy-ui-utils.h>
 #include <libempathy-gtk/empathy-notify-manager.h>
 
+#include "empathy-chat-manager.h"
 #include "empathy-chat-window.h"
 #include "empathy-about-dialog.h"
 #include "empathy-invite-participant-dialog.h"
@@ -1059,6 +1060,19 @@ chat_window_tabs_previous_activate_cb (GtkAction         *action,
 }
 
 static void
+chat_window_tabs_undo_close_tab_activate_cb (GtkAction         *action,
+					     EmpathyChatWindow *window)
+{
+	EmpathyChatManager *chat_manager;
+
+	chat_manager = empathy_chat_manager_dup_singleton ();
+
+	empathy_chat_manager_undo_closed_chat (chat_manager);
+
+	g_object_unref (chat_manager);
+}
+
+static void
 chat_window_tabs_left_activate_cb (GtkAction         *action,
 				   EmpathyChatWindow *window)
 {
@@ -1850,6 +1864,7 @@ empathy_chat_window_init (EmpathyChatWindow *window)
 			      "menu_edit_find", "activate", chat_window_find_activate_cb,
 			      "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
 			      "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
+			      "menu_tabs_undo_close_tab", "activate", chat_window_tabs_undo_close_tab_activate_cb,
 			      "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
 			      "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
 			      "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
@@ -2099,6 +2114,7 @@ empathy_chat_window_remove_chat (EmpathyChatWindow *window,
 	EmpathyChatWindowPriv *priv;
 	gint                   position;
 	EmpathyContact        *remote_contact;
+	EmpathyChatManager    *chat_manager;
 
 	g_return_if_fail (window != NULL);
 	g_return_if_fail (EMPATHY_IS_CHAT (chat));
@@ -2114,6 +2130,10 @@ empathy_chat_window_remove_chat (EmpathyChatWindow *window,
 		g_signal_handlers_disconnect_by_func (remote_contact,
 						      chat_window_update_chat_tab,
 						      chat);
+
+		chat_manager = empathy_chat_manager_dup_singleton ();
+		empathy_chat_manager_closed_chat (chat_manager, remote_contact);
+		g_object_unref (chat_manager);
 	}
 
 	position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
diff --git a/src/empathy-chat-window.ui b/src/empathy-chat-window.ui
index 4ba3502..076cedc 100644
--- a/src/empathy-chat-window.ui
+++ b/src/empathy-chat-window.ui
@@ -112,6 +112,13 @@
           <accelerator key="Page_Down" modifiers="GDK_CONTROL_MASK"/>
         </child>
         <child>
+          <object class="GtkAction" id="menu_tabs_undo_close_tab">
+            <property name="name">menu_tabs_undo_close_tab</property>
+            <property name="label" translatable="yes">_Undo Close Tab</property>
+          </object>
+          <accelerator key="t" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
+        </child>
+        <child>
           <object class="GtkAction" id="menu_tabs_left">
             <property name="name">menu_tabs_left</property>
             <property name="label" translatable="yes">Move Tab _Left</property>
@@ -173,6 +180,7 @@
         <menu action="menu_tabs">
           <menuitem action="menu_tabs_prev"/>
           <menuitem action="menu_tabs_next"/>
+          <menuitem action="menu_tabs_undo_close_tab"/>
           <separator/>
           <menuitem action="menu_tabs_left"/>
           <menuitem action="menu_tabs_right"/>
diff --git a/src/empathy-main-window.c b/src/empathy-main-window.c
index 240f4b1..a1544fa 100644
--- a/src/empathy-main-window.c
+++ b/src/empathy-main-window.c
@@ -25,6 +25,7 @@
 
 #include <sys/stat.h>
 #include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
 #include <glib/gi18n.h>
 
 #include <telepathy-glib/account-manager.h>
@@ -53,6 +54,7 @@
 #include <libempathy-gtk/empathy-ui-utils.h>
 
 #include "empathy-accounts-dialog.h"
+#include "empathy-chat-manager.h"
 #include "empathy-main-window.h"
 #include "ephy-spinner.h"
 #include "empathy-preferences.h"
@@ -656,6 +658,23 @@ main_window_destroy_cb (GtkWidget         *widget,
 	g_free (window);
 }
 
+static gboolean
+main_window_key_press_event_cb  (GtkWidget *window,
+				 GdkEventKey *event,
+				 gpointer user_data)
+{
+	EmpathyChatManager *chat_manager;
+
+	if (event->keyval == GDK_T
+	    && event->state & GDK_SHIFT_MASK
+	    && event->state & GDK_CONTROL_MASK) {
+		chat_manager = empathy_chat_manager_dup_singleton ();
+		empathy_chat_manager_undo_closed_chat (chat_manager);
+		g_object_unref (chat_manager);
+	}
+	return FALSE;
+}
+
 static void
 main_window_chat_quit_cb (GtkAction         *action,
 			  EmpathyMainWindow *window)
@@ -1292,6 +1311,7 @@ empathy_main_window_show (void)
 
 	empathy_builder_connect (gui, window,
 			      "main_window", "destroy", main_window_destroy_cb,
+			      "main_window", "key-press-event", main_window_key_press_event_cb,
 			      "chat_quit", "activate", main_window_chat_quit_cb,
 			      "chat_new_message", "activate", main_window_chat_new_message_cb,
 			      "chat_new_call", "activate", main_window_chat_new_call_cb,
diff --git a/src/empathy.c b/src/empathy.c
index f847914..d18e165 100644
--- a/src/empathy.c
+++ b/src/empathy.c
@@ -72,6 +72,7 @@
 #include "empathy-import-mc4-accounts.h"
 #include "empathy-accounts-common.h"
 #include "empathy-accounts-dialog.h"
+#include "empathy-chat-manager.h"
 #include "empathy-status-icon.h"
 #include "empathy-call-window.h"
 #include "empathy-chat-window.h"
@@ -581,6 +582,7 @@ main (int argc, char *argv[])
   GtkWidget *window;
   EmpathyIdle *idle;
   EmpathyConnectivity *connectivity;
+  EmpathyChatManager *chat_manager;
   GError *error = NULL;
   UniqueApp *unique_app;
   gboolean chatroom_manager_ready;
@@ -677,6 +679,9 @@ main (int argc, char *argv[])
   window = empathy_main_window_show ();
   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
 
+  /* Chat manager */
+  chat_manager = empathy_chat_manager_dup_singleton ();
+
   g_signal_connect (unique_app, "message-received",
       G_CALLBACK (unique_app_message_cb), window);
 
@@ -726,6 +731,7 @@ main (int argc, char *argv[])
   g_object_unref (debug_sender);
 #endif
 
+  g_object_unref (chat_manager);
   g_object_unref (idle);
   g_object_unref (connectivity);
   g_object_unref (icon);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]