[empathy] move 'Remove' item code to individual-menu



commit 6275c48f6ca11c9b31240fdfbfe2e508b6628f7e
Author: Guillaume Desmottes <guillaume desmottes collabora co uk>
Date:   Fri Jun 8 10:48:50 2012 +0200

    move 'Remove' item code to individual-menu
    
    There is no reason to not have it implemented like all the other menu items.
    
    Furtermore, having the logic in individual-menu.c will make it easier to use
    from the new roster view.

 libempathy-gtk/empathy-individual-menu.c |  205 ++++++++++++++++++++++++++++++
 libempathy-gtk/empathy-individual-menu.h |    1 +
 libempathy-gtk/empathy-individual-view.c |  205 +-----------------------------
 src/empathy-roster-window.c              |    3 +-
 4 files changed, 210 insertions(+), 204 deletions(-)
---
diff --git a/libempathy-gtk/empathy-individual-menu.c b/libempathy-gtk/empathy-individual-menu.c
index 7f65735..dffc657 100644
--- a/libempathy-gtk/empathy-individual-menu.c
+++ b/libempathy-gtk/empathy-individual-menu.c
@@ -97,6 +97,8 @@ static GtkWidget * empathy_individual_add_menu_item_new (
     FolksIndividual *individual);
 static GtkWidget * empathy_individiual_block_menu_item_new (
     FolksIndividual *individual);
+static GtkWidget * empathy_individiual_remove_menu_item_new (
+    FolksIndividual *individual);
 
 static void
 individual_menu_add_personas (GtkMenuShell *menu,
@@ -678,6 +680,196 @@ empathy_individiual_block_menu_item_new (FolksIndividual *individual)
   return item;
 }
 
+enum
+{
+  REMOVE_DIALOG_RESPONSE_CANCEL = 0,
+  REMOVE_DIALOG_RESPONSE_DELETE,
+  REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK,
+};
+
+static int
+remove_dialog_show (const gchar *message,
+    const gchar *secondary_text,
+    gboolean block_button,
+    GdkPixbuf *avatar)
+{
+  GtkWidget *dialog;
+  gboolean res;
+
+  dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
+      GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", message);
+
+  if (avatar != NULL)
+    {
+      GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
+      gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
+      gtk_widget_show (image);
+    }
+
+  if (block_button)
+    {
+      GtkWidget *button;
+
+      /* gtk_dialog_add_button() doesn't allow us to pass a string with a
+       * mnemonic so we have to create the button manually. */
+      button = gtk_button_new_with_mnemonic (
+          _("Delete and _Block"));
+
+      gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
+          REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK);
+
+      gtk_widget_show (button);
+    }
+
+  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+      GTK_STOCK_CANCEL, REMOVE_DIALOG_RESPONSE_CANCEL,
+      GTK_STOCK_DELETE, REMOVE_DIALOG_RESPONSE_DELETE, NULL);
+  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+      "%s", secondary_text);
+
+  gtk_widget_show (dialog);
+
+  res = gtk_dialog_run (GTK_DIALOG (dialog));
+  gtk_widget_destroy (dialog);
+
+  return res;
+}
+
+static void
+remove_got_avatar (GObject *source_object,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
+  GdkPixbuf *avatar;
+  EmpathyIndividualManager *manager;
+  gchar *text;
+  GeeSet *personas;
+  guint persona_count = 0;
+  gboolean can_block;
+  GError *error = NULL;
+  gint res;
+
+  avatar = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
+      result, &error);
+
+  if (error != NULL)
+    {
+      DEBUG ("Could not get avatar: %s", error->message);
+      g_error_free (error);
+    }
+
+  /* We couldn't retrieve the avatar, but that isn't a fatal error,
+   * so we still display the remove dialog. */
+
+  personas = folks_individual_get_personas (individual);
+
+  persona_count = gee_collection_get_size (GEE_COLLECTION (personas));
+
+  /* If we have more than one TpfPersona, display a different message
+   * ensuring the user knows that *all* of the meta-contacts' personas will
+   * be removed. */
+
+  if (persona_count < 2)
+    {
+      /* Not a meta-contact */
+      text =
+          g_strdup_printf (
+              _("Do you really want to remove the contact '%s'?"),
+              folks_alias_details_get_alias (
+                  FOLKS_ALIAS_DETAILS (individual)));
+    }
+  else
+    {
+      /* Meta-contact */
+      text =
+          g_strdup_printf (
+              _("Do you really want to remove the linked contact '%s'? "
+                "Note that this will remove all the contacts which make up "
+                "this linked contact."),
+              folks_alias_details_get_alias (
+                  FOLKS_ALIAS_DETAILS (individual)));
+    }
+
+
+  manager = empathy_individual_manager_dup_singleton ();
+  can_block = empathy_individual_manager_supports_blocking (manager,
+      individual);
+  res = remove_dialog_show (_("Removing contact"), text, can_block, avatar);
+
+  if (res == REMOVE_DIALOG_RESPONSE_DELETE ||
+      res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
+    {
+      gboolean abusive;
+
+      if (res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
+        {
+          if (!empathy_block_individual_dialog_show (NULL, individual,
+                avatar, &abusive))
+            goto finally;
+
+          empathy_individual_manager_set_blocked (manager, individual,
+              TRUE, abusive);
+        }
+
+      empathy_individual_manager_remove (manager, individual, "");
+    }
+
+ finally:
+  g_free (text);
+  g_object_unref (manager);
+}
+
+static void
+remove_activate_cb (GtkMenuItem *menuitem,
+    FolksIndividual *individual)
+{
+  empathy_pixbuf_avatar_from_individual_scaled_async (individual,
+      48, 48, NULL, remove_got_avatar, NULL);
+}
+
+static GtkWidget *
+empathy_individiual_remove_menu_item_new (FolksIndividual *individual)
+{
+  GeeSet *personas;
+  GeeIterator *iter;
+  gboolean can_remove = FALSE;
+  GtkWidget *item, *image;
+
+  /* If any of the Individual's personas can be removed, add an option to
+   * remove. This will act as a best-effort option. If any Personas cannot be
+   * removed from the server, then this option will just be inactive upon
+   * subsequent menu openings */
+  personas = folks_individual_get_personas (individual);
+  iter = gee_iterable_iterator (GEE_ITERABLE (personas));
+  while (!can_remove && gee_iterator_next (iter))
+    {
+      FolksPersona *persona = gee_iterator_get (iter);
+      FolksPersonaStore *store = folks_persona_get_store (persona);
+      FolksMaybeBool maybe_can_remove =
+          folks_persona_store_get_can_remove_personas (store);
+
+      if (maybe_can_remove == FOLKS_MAYBE_BOOL_TRUE)
+        can_remove = TRUE;
+
+      g_clear_object (&persona);
+    }
+  g_clear_object (&iter);
+
+  if (!can_remove)
+    return NULL;
+
+  item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
+  image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
+      GTK_ICON_SIZE_MENU);
+  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+
+  g_signal_connect (item, "activate",
+      G_CALLBACK (remove_activate_cb), individual);
+
+  return item;
+}
+
 static void
 constructed (GObject *object)
 {
@@ -816,6 +1008,19 @@ constructed (GObject *object)
     gtk_menu_shell_append (shell, item);
     gtk_widget_show (item);
   }
+
+  /* Separator & Remove */
+  if (features & EMPATHY_INDIVIDUAL_FEATURE_REMOVE &&
+      (item = empathy_individiual_remove_menu_item_new (individual)) != NULL) {
+    GtkWidget *sep;
+
+    sep = gtk_separator_menu_item_new ();
+    gtk_menu_shell_append (shell, sep);
+    gtk_widget_show (sep);
+
+    gtk_menu_shell_append (shell, item);
+    gtk_widget_show (item);
+  }
 }
 
 static void
diff --git a/libempathy-gtk/empathy-individual-menu.h b/libempathy-gtk/empathy-individual-menu.h
index a2187da..332433f 100644
--- a/libempathy-gtk/empathy-individual-menu.h
+++ b/libempathy-gtk/empathy-individual-menu.h
@@ -41,6 +41,7 @@ typedef enum {
 	EMPATHY_INDIVIDUAL_FEATURE_CALL_PHONE = 1 << 7,
 	EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT = 1 << 8,
 	EMPATHY_INDIVIDUAL_FEATURE_BLOCK = 1 << 9,
+	EMPATHY_INDIVIDUAL_FEATURE_REMOVE = 1 << 10,
 } EmpathyIndividualFeatureFlags;
 
 #define EMPATHY_TYPE_INDIVIDUAL_MENU (empathy_individual_menu_get_type ())
diff --git a/libempathy-gtk/empathy-individual-view.c b/libempathy-gtk/empathy-individual-view.c
index c048124..c143ef2 100644
--- a/libempathy-gtk/empathy-individual-view.c
+++ b/libempathy-gtk/empathy-individual-view.c
@@ -2331,15 +2331,12 @@ enum
 {
   REMOVE_DIALOG_RESPONSE_CANCEL = 0,
   REMOVE_DIALOG_RESPONSE_DELETE,
-  REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK,
 };
 
 static int
 individual_view_remove_dialog_show (GtkWindow *parent,
     const gchar *message,
-    const gchar *secondary_text,
-    gboolean block_button,
-    GdkPixbuf *avatar)
+    const gchar *secondary_text)
 {
   GtkWidget *dialog;
   gboolean res;
@@ -2347,28 +2344,6 @@ individual_view_remove_dialog_show (GtkWindow *parent,
   dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", message);
 
-  if (avatar != NULL)
-    {
-      GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
-      gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
-      gtk_widget_show (image);
-    }
-
-  if (block_button)
-    {
-      GtkWidget *button;
-
-      /* gtk_dialog_add_button() doesn't allow us to pass a string with a
-       * mnemonic so we have to create the button manually. */
-      button = gtk_button_new_with_mnemonic (
-          _("Delete and _Block"));
-
-      gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
-          REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK);
-
-      gtk_widget_show (button);
-    }
-
   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
       GTK_STOCK_CANCEL, REMOVE_DIALOG_RESPONSE_CANCEL,
       GTK_STOCK_DELETE, REMOVE_DIALOG_RESPONSE_DELETE, NULL);
@@ -2400,7 +2375,7 @@ individual_view_group_remove_activate_cb (GtkMenuItem *menuitem,
           group);
       parent = empathy_get_toplevel_window (GTK_WIDGET (view));
       if (individual_view_remove_dialog_show (parent, _("Removing group"),
-              text, FALSE, NULL) == REMOVE_DIALOG_RESPONSE_DELETE)
+              text) == REMOVE_DIALOG_RESPONSE_DELETE)
         {
           EmpathyIndividualManager *manager =
               empathy_individual_manager_dup_singleton ();
@@ -2491,142 +2466,12 @@ empathy_individual_view_get_group_menu (EmpathyIndividualView *view)
   return menu;
 }
 
-static void
-got_avatar (GObject *source_object,
-    GAsyncResult *result,
-    gpointer user_data)
-{
-  FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
-  EmpathyIndividualView *view = user_data;
-  EmpathyIndividualViewPriv *priv = GET_PRIV (view);
-  GdkPixbuf *avatar;
-  EmpathyIndividualManager *manager;
-  gchar *text;
-  GtkWindow *parent;
-  GeeSet *personas;
-  guint persona_count = 0;
-  gboolean can_block;
-  GError *error = NULL;
-  gint res;
-
-  avatar = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
-      result, &error);
-
-  if (error != NULL)
-    {
-      DEBUG ("Could not get avatar: %s", error->message);
-      g_error_free (error);
-    }
-
-  /* We couldn't retrieve the avatar, but that isn't a fatal error,
-   * so we still display the remove dialog. */
-
-  personas = folks_individual_get_personas (individual);
-
-  if (priv->show_uninteresting)
-    {
-      persona_count = gee_collection_get_size (GEE_COLLECTION (personas));
-    }
-  else
-    {
-      GeeIterator *iter;
-
-      iter = gee_iterable_iterator (GEE_ITERABLE (personas));
-      while (persona_count < 2 && gee_iterator_next (iter))
-        {
-          FolksPersona *persona = gee_iterator_get (iter);
-
-          if (empathy_folks_persona_is_interesting (persona))
-            persona_count++;
-
-          g_clear_object (&persona);
-        }
-      g_clear_object (&iter);
-    }
-
-  /* If we have more than one TpfPersona, display a different message
-   * ensuring the user knows that *all* of the meta-contacts' personas will
-   * be removed. */
-
-  if (persona_count < 2)
-    {
-      /* Not a meta-contact */
-      text =
-          g_strdup_printf (
-              _("Do you really want to remove the contact '%s'?"),
-              folks_alias_details_get_alias (
-                  FOLKS_ALIAS_DETAILS (individual)));
-    }
-  else
-    {
-      /* Meta-contact */
-      text =
-          g_strdup_printf (
-              _("Do you really want to remove the linked contact '%s'? "
-                "Note that this will remove all the contacts which make up "
-                "this linked contact."),
-              folks_alias_details_get_alias (
-                  FOLKS_ALIAS_DETAILS (individual)));
-    }
-
-
-  manager = empathy_individual_manager_dup_singleton ();
-  can_block = empathy_individual_manager_supports_blocking (manager,
-      individual);
-  parent = empathy_get_toplevel_window (GTK_WIDGET (view));
-  res = individual_view_remove_dialog_show (parent, _("Removing contact"),
-          text, can_block, avatar);
-
-  if (res == REMOVE_DIALOG_RESPONSE_DELETE ||
-      res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
-    {
-      gboolean abusive;
-
-      if (res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
-        {
-          if (!empathy_block_individual_dialog_show (parent, individual,
-                avatar, &abusive))
-            goto finally;
-
-          empathy_individual_manager_set_blocked (manager, individual,
-              TRUE, abusive);
-        }
-
-      empathy_individual_manager_remove (manager, individual, "");
-    }
-
- finally:
-  g_free (text);
-  g_object_unref (manager);
-}
-
-static void
-individual_view_remove_activate_cb (GtkMenuItem *menuitem,
-    EmpathyIndividualView *view)
-{
-  FolksIndividual *individual;
-
-  individual = empathy_individual_view_dup_selected (view);
-
-  if (individual != NULL)
-    {
-      empathy_pixbuf_avatar_from_individual_scaled_async (individual,
-          48, 48, NULL, got_avatar, view);
-      g_object_unref (individual);
-    }
-}
-
 GtkWidget *
 empathy_individual_view_get_individual_menu (EmpathyIndividualView *view)
 {
   EmpathyIndividualViewPriv *priv = GET_PRIV (view);
   FolksIndividual *individual;
   GtkWidget *menu = NULL;
-  GtkWidget *item;
-  GtkWidget *image;
-  gboolean can_remove = FALSE;
-  GeeSet *personas;
-  GeeIterator *iter;
 
   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_VIEW (view), NULL);
 
@@ -2641,55 +2486,9 @@ empathy_individual_view_get_individual_menu (EmpathyIndividualView *view)
   if (!empathy_folks_individual_contains_contact (individual))
     goto out;
 
-  /* If any of the Individual's personas can be removed, add an option to
-   * remove. This will act as a best-effort option. If any Personas cannot be
-   * removed from the server, then this option will just be inactive upon
-   * subsequent menu openings */
-  personas = folks_individual_get_personas (individual);
-  iter = gee_iterable_iterator (GEE_ITERABLE (personas));
-  while (!can_remove && gee_iterator_next (iter))
-    {
-      FolksPersona *persona = gee_iterator_get (iter);
-      FolksPersonaStore *store = folks_persona_get_store (persona);
-      FolksMaybeBool maybe_can_remove =
-          folks_persona_store_get_can_remove_personas (store);
-
-      if (maybe_can_remove == FOLKS_MAYBE_BOOL_TRUE)
-        can_remove = TRUE;
-
-      g_clear_object (&persona);
-    }
-  g_clear_object (&iter);
-
   menu = empathy_individual_menu_new (individual, priv->individual_features,
       priv->store);
 
-  /* Remove contact */
-  if ((priv->view_features &
-      EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_REMOVE) &&
-      can_remove)
-    {
-      /* create the menu if required, or just add a separator */
-      if (menu == NULL)
-        menu = gtk_menu_new ();
-      else
-        {
-          item = gtk_separator_menu_item_new ();
-          gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-          gtk_widget_show (item);
-        }
-
-      /* Remove */
-      item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
-      image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
-          GTK_ICON_SIZE_MENU);
-      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
-      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-      gtk_widget_show (item);
-      g_signal_connect (item, "activate",
-          G_CALLBACK (individual_view_remove_activate_cb), view);
-    }
-
 out:
   g_object_unref (individual);
 
diff --git a/src/empathy-roster-window.c b/src/empathy-roster-window.c
index f614a80..f7b8377 100644
--- a/src/empathy-roster-window.c
+++ b/src/empathy-roster-window.c
@@ -2277,7 +2277,8 @@ empathy_roster_window_init (EmpathyRosterWindow *self)
       EMPATHY_INDIVIDUAL_FEATURE_INFO |
       EMPATHY_INDIVIDUAL_FEATURE_LOG |
       EMPATHY_INDIVIDUAL_FEATURE_SMS |
-      EMPATHY_INDIVIDUAL_FEATURE_CALL_PHONE);
+      EMPATHY_INDIVIDUAL_FEATURE_CALL_PHONE |
+      EMPATHY_INDIVIDUAL_FEATURE_REMOVE);
 
   gtk_widget_show (GTK_WIDGET (self->priv->individual_view));
   gtk_container_add (GTK_CONTAINER (sw),



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