]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-menu.c
empathy-contact-menu.c: store the chatroom instead of the channel in contact_room_sub...
[empathy.git] / libempathy-gtk / empathy-contact-menu.c
index 4509dd171b2b6ce4c158ff0660d5081cb0be7e42..609b5ba16fcd97617d479e0e9dc5b0d9111921e6 100644 (file)
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
-#include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-log-manager.h>
+#include <libempathy/empathy-dispatcher.h>
+#include <libempathy/empathy-utils.h>
+#include <libempathy/empathy-chatroom-manager.h>
 
 #include "empathy-contact-menu.h"
 #include "empathy-images.h"
 #include "empathy-log-window.h"
 #include "empathy-contact-dialogs.h"
 
-#define DEBUG_DOMAIN "ContactMenu"
+GtkWidget *
+empathy_contact_menu_new (EmpathyContact             *contact,
+                         EmpathyContactFeatureFlags  features)
+{
+       GtkWidget    *menu;
+       GtkMenuShell *shell;
+       GtkWidget    *item;
+
+       g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+       if (features == EMPATHY_CONTACT_FEATURE_NONE) {
+               return NULL;
+       }
+
+       menu = gtk_menu_new ();
+       shell = GTK_MENU_SHELL (menu);
+
+       /* Chat */
+       if (features & EMPATHY_CONTACT_FEATURE_CHAT) {
+               item = empathy_contact_chat_menu_item_new (contact);
+               gtk_menu_shell_append (shell, item);
+               gtk_widget_show (item);
+       }
+
+       /* Call */
+       if (features & EMPATHY_CONTACT_FEATURE_CALL) {
+               item = empathy_contact_call_menu_item_new (contact);
+               gtk_menu_shell_append (shell, item);
+               gtk_widget_show (item);
+       }
+
+       /* Log */
+       if (features & EMPATHY_CONTACT_FEATURE_LOG) {
+               item = empathy_contact_log_menu_item_new (contact);
+               gtk_menu_shell_append (shell, item);
+               gtk_widget_show (item);
+       }
+
+  /* Invite */
+  item = empathy_contact_invite_menu_item_new (contact);
+       gtk_menu_shell_append (shell, item);
+       gtk_widget_show (item);
+
+       /* Separator */
+       if (features & (EMPATHY_CONTACT_FEATURE_EDIT |
+                       EMPATHY_CONTACT_FEATURE_INFO)) {
+               item = gtk_separator_menu_item_new ();
+               gtk_menu_shell_append (shell, item);
+               gtk_widget_show (item);
+       }
+
+       /* Edit */
+       if (features & EMPATHY_CONTACT_FEATURE_EDIT) {
+               item = empathy_contact_edit_menu_item_new (contact);
+               gtk_menu_shell_append (shell, item);
+               gtk_widget_show (item);
+       }
+
+       /* Info */
+       if (features & EMPATHY_CONTACT_FEATURE_INFO) {
+               item = empathy_contact_info_menu_item_new (contact);
+               gtk_menu_shell_append (shell, item);
+               gtk_widget_show (item);
+       }
+
+       return menu;
+}
 
 GtkWidget *
 empathy_contact_chat_menu_item_new (EmpathyContact *contact)
@@ -51,7 +119,7 @@ empathy_contact_chat_menu_item_new (EmpathyContact *contact)
        gtk_widget_show (image);
 
        g_signal_connect_swapped (item, "activate",
-                                 G_CALLBACK (empathy_chat_with_contact),
+                                 G_CALLBACK (empathy_dispatcher_chat_with_contact),
                                  contact);
        
        return item;
@@ -73,7 +141,7 @@ empathy_contact_call_menu_item_new (EmpathyContact *contact)
        gtk_widget_show (image);
 
        g_signal_connect_swapped (item, "activate",
-                                 G_CALLBACK (empathy_call_with_contact),
+                                 G_CALLBACK (empathy_dispatcher_call_with_contact),
                                  contact);
        
        return item;
@@ -172,3 +240,142 @@ empathy_contact_edit_menu_item_new (EmpathyContact *contact)
        return item;
 }
 
+typedef struct 
+{
+  EmpathyContact *contact;
+  EmpathyChatroom *chatroom;
+} contact_room_sub_menu_item_activate_cb_ctx;
+
+static contact_room_sub_menu_item_activate_cb_ctx *
+contact_room_sub_menu_item_activate_cb_ctx_new (EmpathyContact *contact,
+                                                EmpathyChatroom *chatroom)
+{
+  contact_room_sub_menu_item_activate_cb_ctx *ctx;
+
+  ctx = g_slice_new (contact_room_sub_menu_item_activate_cb_ctx);
+
+  ctx->contact = g_object_ref (contact);
+  ctx->chatroom = g_object_ref (chatroom);
+
+  return ctx;
+}
+
+static void
+contact_room_sub_menu_item_activate_cb_ctx_free (
+    contact_room_sub_menu_item_activate_cb_ctx *ctx)
+{
+  /* FIXME: seems this is never called... */
+  g_object_unref (ctx->contact);
+  g_object_unref (ctx->chatroom);
+
+  g_slice_free (contact_room_sub_menu_item_activate_cb_ctx, ctx);
+}
+
+static void
+contact_room_sub_menu_item_activate_cb (
+    GtkWidget *item,
+    contact_room_sub_menu_item_activate_cb_ctx *ctx)
+{
+  GArray *handles;
+  TpHandle handle;
+  TpChannel *channel;
+
+  g_object_get (ctx->chatroom, "tp-channel", &channel, NULL);
+  if (channel == NULL)
+    /* channel was invalidated. Ignoring */
+    return;
+
+  /* send invitation */
+  handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
+  handle = empathy_contact_get_handle (ctx->contact);
+  g_array_append_val (handles, handle);
+
+  tp_cli_channel_interface_group_call_add_members (channel, -1, handles,
+      _("Inviting to this room"), NULL, NULL, NULL, NULL);
+
+  g_array_free (handles, TRUE);
+  g_object_unref (channel);
+}
+
+static GtkWidget *
+create_room_sub_menu_item (EmpathyContact *contact,
+                           EmpathyChatroom *chatroom)
+{
+       GtkWidget *item;
+  contact_room_sub_menu_item_activate_cb_ctx *ctx;
+
+       g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+  item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
+
+  ctx = contact_room_sub_menu_item_activate_cb_ctx_new (contact, chatroom);
+
+  g_signal_connect_data (item, "activate",
+      G_CALLBACK (contact_room_sub_menu_item_activate_cb), ctx,
+      (GClosureNotify) contact_room_sub_menu_item_activate_cb_ctx_free, 0);
+
+       return item;
+}
+
+GtkWidget *
+empathy_contact_invite_menu_item_new (EmpathyContact *contact)
+{
+       GtkWidget *item;
+       GtkWidget *image;
+  GtkWidget *room_item;
+  EmpathyChatroomManager *mgr;
+  GList *rooms, *l;
+  GtkWidget *submenu;
+  GtkMenuShell *submenu_shell;
+  gboolean have_rooms = FALSE;
+
+       g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+       item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to chatroom"));
+       image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
+                                             GTK_ICON_SIZE_MENU);
+       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+
+  mgr = empathy_chatroom_manager_new (NULL);
+  rooms = empathy_chatroom_manager_get_chatrooms (mgr,
+      empathy_contact_get_account (contact));
+
+  /* create rooms sub menu */
+  submenu = gtk_menu_new ();
+  submenu_shell = GTK_MENU_SHELL (submenu);
+
+  for (l = rooms; l != NULL; l = g_list_next (l))
+    {
+      EmpathyChatroom *chatroom = l->data;
+      TpChannel *channel;
+
+      g_object_get (chatroom, "tp-channel", &channel, NULL);
+      if (channel != NULL)
+        {
+          have_rooms = TRUE;
+
+          room_item = create_room_sub_menu_item (contact, chatroom);
+          gtk_menu_shell_append (submenu_shell, room_item);
+          gtk_widget_show (room_item);
+
+          g_object_unref (channel);
+        }
+    }
+
+  if (have_rooms)
+    {
+      gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
+    }
+  else
+    {
+      gtk_widget_set_sensitive (item, FALSE);
+      gtk_widget_destroy (submenu);
+    }
+
+       gtk_widget_show (image);
+
+  g_object_unref (mgr);
+  g_list_free (rooms);
+
+       return item;
+}