From 65a8471be96f39065f8914dbd14c2b2910fc916f Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Wed, 15 Sep 2010 15:31:47 -0400
Subject: [PATCH] [#4064] create/delete instant messaging tabs automatically

---
 sflphone-client-gnome/src/actions.c         | 24 +++++++++++++++++--
 sflphone-client-gnome/src/imwindow.c        | 26 +++++++++++++++++++++
 sflphone-client-gnome/src/imwindow.h        |  6 +++++
 sflphone-client-gnome/src/widget/imwidget.c |  7 +++++-
 sflphone-client-gnome/src/widget/imwidget.h |  1 +
 sflphone-common/src/sip/sipvoiplink.cpp     |  2 ++
 6 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/sflphone-client-gnome/src/actions.c b/sflphone-client-gnome/src/actions.c
index a7ea725504..d6c7303999 100644
--- a/sflphone-client-gnome/src/actions.c
+++ b/sflphone-client-gnome/src/actions.c
@@ -52,6 +52,7 @@
 #include <linux/if.h>
 
 #include <widget/imwidget.h>
+#include <imwindow.h>
 
 GHashTable * ip2ip_profile=NULL;
 
@@ -199,8 +200,11 @@ sflphone_hung_up (callable_obj_t * c)
     call_remove_all_errors (c);
     update_actions();
 
-    /* Update the IM interface */
-    im_widget_update_state (IM_WIDGET (c->_im_widget), FALSE);
+    // test wether the widget contain text, if not remove it
+    if ( (im_window_get_nb_tabs() > 1) && c->_im_widget && ! (IM_WIDGET (c->_im_widget)->containText))
+        im_window_remove_tab (c->_im_widget);
+    else
+        im_widget_update_state (IM_WIDGET (c->_im_widget), FALSE);
 
 #if GTK_CHECK_VERSION(2,10,0)
     status_tray_icon_blink (FALSE);
@@ -380,7 +384,13 @@ sflphone_hang_up()
                 call_remove_all_errors (selectedCall);
                 selectedCall->_state = CALL_STATE_DIALING;
                 set_timestamp (&selectedCall->_time_stop);
+
+                //if ( (im_window_get_nb_tabs() > 1) && selectedCall->_im_widget &&
+                //        ! (IM_WIDGET (selectedCall->_im_widget)->containText))
+                //    im_window_remove_tab (selectedCall->_im_widget);
+                //else
                 im_widget_update_state (IM_WIDGET (selectedCall->_im_widget), FALSE);
+
                 break;
             case CALL_STATE_FAILURE:
                 dbus_hang_up (selectedCall);
@@ -437,10 +447,20 @@ sflphone_pick_up()
         switch (selectedCall->_state) {
             case CALL_STATE_DIALING:
                 sflphone_place_call (selectedCall);
+
+                // if instant messaging window is visible, create new tab (deleted automatically if not used)
+                if (im_window_is_visible())
+                    im_widget_display ( (IMWidget **) (&selectedCall->_im_widget), NULL, selectedCall->_callID, NULL);
+
                 break;
             case CALL_STATE_INCOMING:
                 selectedCall->_history_state = INCOMING;
                 calltree_update_call (history, selectedCall, NULL);
+
+                // if instant messaging window is visible, create new tab (deleted automatically if not used)
+                if (im_window_is_visible())
+                    im_widget_display ( (IMWidget **) (&selectedCall->_im_widget), NULL, selectedCall->_callID, NULL);
+
                 dbus_accept (selectedCall);
                 DEBUG ("from sflphone_pick_up : ");
                 stop_notification();
diff --git a/sflphone-client-gnome/src/imwindow.c b/sflphone-client-gnome/src/imwindow.c
index 15c0206cbe..e9bb916c1f 100644
--- a/sflphone-client-gnome/src/imwindow.c
+++ b/sflphone-client-gnome/src/imwindow.c
@@ -138,6 +138,23 @@ im_window_get()
     return im_window;
 }
 
+
+gboolean
+im_window_is_active ()
+{
+
+    if (!im_window)
+        return FALSE;
+    else
+        return gtk_window_is_active (im_window);
+}
+
+gboolean
+im_window_is_visible ()
+{
+    return gtk_widget_get_visible (im_window);
+}
+
 void
 im_window_show ()
 {
@@ -157,6 +174,15 @@ im_window_add (GtkWidget *widget)
         ERROR ("Could not create the main instant messaging window");
 }
 
+gint
+im_window_get_nb_tabs()
+{
+    if (im_notebook)
+        return gtk_notebook_get_n_pages (im_notebook);
+    else
+        return 0;
+}
+
 static void
 close_tab_cb (GtkButton *button UNUSED, gpointer userdata)
 {
diff --git a/sflphone-client-gnome/src/imwindow.h b/sflphone-client-gnome/src/imwindow.h
index dbfab07cd9..232c8bf10a 100644
--- a/sflphone-client-gnome/src/imwindow.h
+++ b/sflphone-client-gnome/src/imwindow.h
@@ -60,6 +60,12 @@ void im_window_remove_tab (GtkWidget *widget);
 
 void im_window_show ();
 
+gboolean im_window_is_active (void);
+
+gboolean im_window_is_visible (void);
+
+gint im_window_get_nb_tab (void);
+
 /*! @function
 @abstract	Add a new tab in the notebook. Each tab is an IM Widget
 @param		The IM widget
diff --git a/sflphone-client-gnome/src/widget/imwidget.c b/sflphone-client-gnome/src/widget/imwidget.c
index 3d3dd9493d..367a200b73 100644
--- a/sflphone-client-gnome/src/widget/imwidget.c
+++ b/sflphone-client-gnome/src/widget/imwidget.c
@@ -107,6 +107,9 @@ im_widget_add_message (IMWidget *im, const gchar *from, const gchar *message, gi
         gchar *script = g_strdup_printf ("add_message('%s', '%s', '%s', '%s');", message_escaped, from, css_class, msgtime);
         webkit_web_view_execute_script (WEBKIT_WEB_VIEW (im->web_view), script);
 
+        /* Mark it as used */
+        im->containText = TRUE;
+
         /* Cleanup */
         g_free (script);
         g_free (message_escaped);
@@ -275,6 +278,8 @@ im_widget_init (IMWidget *im)
     im->js_global = JSContextGetGlobalObject (im->js_context);
     webkit_web_view_load_uri (WEBKIT_WEB_VIEW (im->web_view), "file://" DATA_DIR "/webkit/im/im.html");
 
+    im->containText = FALSE;
+
     g_signal_connect (G_OBJECT (im->web_frame), "notify", G_CALLBACK (on_frame_loading_done), im);
 }
 
@@ -437,7 +442,7 @@ call_state_image_widget (call_state_t state)
             image = gtk_image_new_from_stock (GTK_STOCK_IM, GTK_ICON_SIZE_LARGE_TOOLBAR);
             break;
         default:
-            image = gtk_image_new_from_stock (GTK_STOCK_FAIL, GTK_ICON_SIZE_LARGE_TOOLBAR);
+            image = gtk_image_new_from_stock (GTK_STOCK_IM, GTK_ICON_SIZE_LARGE_TOOLBAR);
             break;
 
     }
diff --git a/sflphone-client-gnome/src/widget/imwidget.h b/sflphone-client-gnome/src/widget/imwidget.h
index bcb1b32a64..ea0909f213 100644
--- a/sflphone-client-gnome/src/widget/imwidget.h
+++ b/sflphone-client-gnome/src/widget/imwidget.h
@@ -67,6 +67,7 @@ struct _IMWidget {
     WebKitWebFrame *web_frame;      // Our web frame
     JSGlobalContextRef js_context;  // The frame's global JS context
     JSObjectRef js_global;          // The frame's global context JS object
+    gboolean containText;
 };
 
 struct _IMWidgetClass {
diff --git a/sflphone-common/src/sip/sipvoiplink.cpp b/sflphone-common/src/sip/sipvoiplink.cpp
index 2943cb67a2..6fe5945c9e 100644
--- a/sflphone-common/src/sip/sipvoiplink.cpp
+++ b/sflphone-common/src/sip/sipvoiplink.cpp
@@ -3118,6 +3118,7 @@ void call_on_state_changed (pjsip_inv_session *inv, pjsip_event *e)
         return;
     }
 
+    /*
     pjsip_hdr *allow_header = NULL;
     std::string *allowed_options = NULL;
 
@@ -3134,6 +3135,7 @@ void call_on_state_changed (pjsip_inv_session *inv, pjsip_event *e)
 
     if (allowed_options)
         delete allowed_options;
+    */
 
     // If this is an outgoing INVITE that was created because of
     // REFER/transfer, send NOTIFY to transferer.
-- 
GitLab