]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-individual-linker.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-individual-linker.c
index 2729790465e549599c5d88ee7a40280dc27517f8..3aee55d7839ca8eb623c3c108abfd5b0382cfd05 100644 (file)
@@ -66,6 +66,9 @@ typedef struct {
   EmpathyIndividualView *individual_view; /* child widget */
   GtkWidget *preview_widget; /* child widget */
   EmpathyPersonaStore *persona_store; /* owned */
+  GtkTreeViewColumn *toggle_column; /* child widget */
+  GtkCellRenderer *toggle_renderer; /* child widget */
+  GtkWidget *search_widget; /* child widget */
 
   FolksIndividual *start_individual; /* owned, allow-none */
   FolksIndividual *new_individual; /* owned, allow-none */
@@ -78,6 +81,7 @@ typedef struct {
 
 enum {
   PROP_START_INDIVIDUAL = 1,
+  PROP_HAS_CHANGED,
 };
 
 G_DEFINE_TYPE (EmpathyIndividualLinker, empathy_individual_linker,
@@ -117,6 +121,21 @@ contact_toggle_cell_data_func (GtkTreeViewColumn *tree_column,
   tp_clear_object (&individual);
 }
 
+static void
+update_toggle_renderers (EmpathyIndividualLinker *self)
+{
+  EmpathyIndividualLinkerPriv *priv = GET_PRIV (self);
+
+  /* Re-setting the cell data func to the same function causes a refresh of the
+   * entire column, ensuring that each toggle button is correctly active or
+   * inactive. This is necessary because one Individual might appear multiple
+   * times in the list (in different groups), so toggling one instance of the
+   * Individual should toggle all of them. */
+  gtk_tree_view_column_set_cell_data_func (priv->toggle_column,
+      priv->toggle_renderer,
+      (GtkTreeCellDataFunc) contact_toggle_cell_data_func, self, NULL);
+}
+
 static void
 link_individual (EmpathyIndividualLinker *self,
     FolksIndividual *individual)
@@ -137,6 +156,13 @@ link_individual (EmpathyIndividualLinker *self,
       g_list_copy (folks_individual_get_personas (individual)));
   folks_individual_set_personas (priv->new_individual, new_persona_list);
   g_list_free (new_persona_list);
+
+  /* Update the toggle renderers, so that if this Individual is listed in
+   * another group in the EmpathyIndividualView, the toggle button for that
+   * group is updated. */
+  update_toggle_renderers (self);
+
+  g_object_notify (G_OBJECT (self), "has-changed");
 }
 
 static void
@@ -166,6 +192,13 @@ unlink_individual (EmpathyIndividualLinker *self,
   new_persona_list = g_list_reverse (new_persona_list);
   folks_individual_set_personas (priv->new_individual, new_persona_list);
   g_list_free (new_persona_list);
+
+  /* Update the toggle renderers, so that if this Individual is listed in
+   * another group in the EmpathyIndividualView, the toggle button for that
+   * group is updated. */
+  update_toggle_renderers (self);
+
+  g_object_notify (G_OBJECT (self), "has-changed");
 }
 
 static void
@@ -185,6 +218,9 @@ toggle_individual_row (EmpathyIndividualLinker *self,
       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
       -1);
 
+  if (individual == NULL)
+    return;
+
   individual_added = GPOINTER_TO_UINT (g_hash_table_lookup (
       priv->changed_individuals, individual));
 
@@ -216,15 +252,86 @@ row_toggled_cb (GtkCellRendererToggle *cell_renderer,
   gtk_tree_path_free (tree_path);
 }
 
+static gboolean
+individual_view_drag_motion_cb (GtkWidget *widget,
+    GdkDragContext *context,
+    gint x,
+    gint y,
+    guint time_)
+{
+  EmpathyIndividualView *view = EMPATHY_INDIVIDUAL_VIEW (widget);
+  GdkAtom target;
+
+  target = gtk_drag_dest_find_target (GTK_WIDGET (view), context, NULL);
+
+  if (target == gdk_atom_intern_static_string ("text/persona-id"))
+    {
+      GtkTreePath *path;
+
+      /* FIXME: It doesn't make sense for us to highlight a specific row or
+       * position to drop a Persona in, so just highlight the entire widget.
+       * Since I can't find a way to do this, just highlight the first possible
+       * position in the tree. */
+      gdk_drag_status (context, gdk_drag_context_get_suggested_action (context),
+          time_);
+
+      path = gtk_tree_path_new_first ();
+      gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (view), path,
+          GTK_TREE_VIEW_DROP_BEFORE);
+      gtk_tree_path_free (path);
+
+      return TRUE;
+    }
+
+  /* Unknown or unhandled drag target */
+  gdk_drag_status (context, GDK_ACTION_DEFAULT, time_);
+  gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (view), NULL, 0);
+
+  return FALSE;
+}
+
+static gboolean
+individual_view_drag_persona_received_cb (EmpathyIndividualView *view,
+    GdkDragAction action,
+    FolksPersona *persona,
+    FolksIndividual *individual,
+    EmpathyIndividualLinker *self)
+{
+  EmpathyIndividualLinkerPriv *priv = GET_PRIV (self);
+
+  /* A Persona has been dragged onto the EmpathyIndividualView (from the
+   * EmpathyPersonaView), so we try to remove the Individual which contains
+   * the Persona from the link. */
+  if (individual != priv->start_individual)
+    {
+      unlink_individual (self, individual);
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+persona_view_drag_individual_received_cb (EmpathyPersonaView *view,
+    GdkDragAction action,
+    FolksIndividual *individual,
+    EmpathyIndividualLinker *self)
+{
+  /* An Individual has been dragged onto the EmpathyPersonaView (from the
+   * EmpathyIndividualView), so we try to add the Individual to the link. */
+  link_individual (self, individual);
+
+  return TRUE;
+}
+
 static void
 set_up (EmpathyIndividualLinker *self)
 {
   EmpathyIndividualLinkerPriv *priv;
   EmpathyIndividualManager *individual_manager;
-  GtkCellRenderer *cell_renderer;
   GtkWidget *top_vbox;
   GtkPaned *paned;
-  GtkWidget *label, *scrolled_window, *search_bar;
+  GtkWidget *label, *scrolled_window;
   GtkBox *vbox;
   EmpathyPersonaView *persona_view;
   gchar *tmp;
@@ -258,19 +365,35 @@ set_up (EmpathyIndividualLinker *self)
   empathy_individual_store_set_show_protocols (priv->individual_store, FALSE);
 
   priv->individual_view = empathy_individual_view_new (priv->individual_store,
-      EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
+      EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_DRAG |
+      EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_DROP |
+      EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
+      EMPATHY_INDIVIDUAL_FEATURE_NONE);
   empathy_individual_view_set_show_offline (priv->individual_view, TRUE);
+  empathy_individual_view_set_show_untrusted (priv->individual_view, FALSE);
 
   g_signal_connect (priv->individual_view, "row-activated",
       (GCallback) row_activated_cb, self);
+  g_signal_connect (priv->individual_view, "drag-motion",
+      (GCallback) individual_view_drag_motion_cb, self);
+  g_signal_connect (priv->individual_view, "drag-persona-received",
+      (GCallback) individual_view_drag_persona_received_cb, self);
 
   /* Add a checkbox column to the selector */
-  cell_renderer = gtk_cell_renderer_toggle_new ();
-  g_signal_connect (cell_renderer, "toggled", (GCallback) row_toggled_cb, self);
-  gtk_tree_view_insert_column_with_data_func (
-      GTK_TREE_VIEW (priv->individual_view), 0, NULL, cell_renderer,
+  priv->toggle_renderer = gtk_cell_renderer_toggle_new ();
+  g_signal_connect (priv->toggle_renderer, "toggled",
+      (GCallback) row_toggled_cb, self);
+
+  priv->toggle_column = gtk_tree_view_column_new ();
+  gtk_tree_view_column_pack_start (priv->toggle_column, priv->toggle_renderer,
+      FALSE);
+  gtk_tree_view_column_set_cell_data_func (priv->toggle_column,
+      priv->toggle_renderer,
       (GtkTreeCellDataFunc) contact_toggle_cell_data_func, self, NULL);
 
+  gtk_tree_view_insert_column (GTK_TREE_VIEW (priv->individual_view),
+      priv->toggle_column, 0);
+
   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
@@ -284,11 +407,12 @@ set_up (EmpathyIndividualLinker *self)
   gtk_widget_show (scrolled_window);
 
   /* Live search */
-  search_bar = empathy_live_search_new (GTK_WIDGET (priv->individual_view));
+  priv->search_widget = empathy_live_search_new (
+      GTK_WIDGET (priv->individual_view));
   empathy_individual_view_set_live_search (priv->individual_view,
-      EMPATHY_LIVE_SEARCH (search_bar));
+      EMPATHY_LIVE_SEARCH (priv->search_widget));
 
-  gtk_box_pack_end (vbox, search_bar, FALSE, TRUE, 0);
+  gtk_box_pack_end (vbox, priv->search_widget, FALSE, TRUE, 0);
 
   gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (vbox));
   gtk_paned_pack1 (paned, alignment, TRUE, FALSE);
@@ -323,9 +447,12 @@ set_up (EmpathyIndividualLinker *self)
   priv->persona_store = empathy_persona_store_new (priv->new_individual);
   empathy_persona_store_set_show_protocols (priv->persona_store, TRUE);
   persona_view = empathy_persona_view_new (priv->persona_store,
-      EMPATHY_PERSONA_VIEW_FEATURE_NONE);
+      EMPATHY_PERSONA_VIEW_FEATURE_ALL);
   empathy_persona_view_set_show_offline (persona_view, TRUE);
 
+  g_signal_connect (persona_view, "drag-individual-received",
+      (GCallback) persona_view_drag_individual_received_cb, self);
+
   gtk_container_add (GTK_CONTAINER (scrolled_window),
       GTK_WIDGET (persona_view));
   gtk_widget_show (GTK_WIDGET (persona_view));
@@ -383,6 +510,10 @@ get_property (GObject *object,
       case PROP_START_INDIVIDUAL:
         g_value_set_object (value, priv->start_individual);
         break;
+      case PROP_HAS_CHANGED:
+        g_value_set_boolean (value, empathy_individual_linker_get_has_changed (
+            EMPATHY_INDIVIDUAL_LINKER (object)));
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
         break;
@@ -434,31 +565,6 @@ finalize (GObject *object)
   G_OBJECT_CLASS (empathy_individual_linker_parent_class)->finalize (object);
 }
 
-static void
-size_request (GtkWidget *widget,
-    GtkRequisition *requisition)
-{
-  GtkBin *bin = GTK_BIN (widget);
-  GtkWidget *child;
-
-  requisition->width =
-      gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
-  requisition->height =
-      gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
-
-  child = gtk_bin_get_child (bin);
-
-  if (child && gtk_widget_get_visible (child))
-    {
-      GtkRequisition child_requisition;
-
-      gtk_widget_size_request (child, &child_requisition);
-
-      requisition->width += child_requisition.width;
-      requisition->height += child_requisition.height;
-    }
-}
-
 static void
 size_allocate (GtkWidget *widget,
     GtkAllocation *allocation)
@@ -497,7 +603,6 @@ empathy_individual_linker_class_init (EmpathyIndividualLinkerClass *klass)
   object_class->dispose = dispose;
   object_class->finalize = finalize;
 
-  widget_class->size_request = size_request;
   widget_class->size_allocate = size_allocate;
 
   /**
@@ -515,6 +620,24 @@ empathy_individual_linker_class_init (EmpathyIndividualLinkerClass *klass)
           FOLKS_TYPE_INDIVIDUAL,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * EmpathyIndividualLinker:has-changed:
+   *
+   * Whether #FolksIndividual<!-- -->s have been added to or removed from
+   * the linked individual currently displayed in the widget.
+   *
+   * This will be %FALSE after the widget is initialised, and set to %TRUE when
+   * an individual is checked in the individual view on the left of the widget.
+   * If the individual is later unchecked, this will be reset to %FALSE, etc.
+   */
+  g_object_class_install_property (object_class, PROP_HAS_CHANGED,
+      g_param_spec_boolean ("has-changed",
+          "Changed?",
+          "Whether individuals have been added to or removed from the linked "
+          "individual currently displayed in the widget.",
+          FALSE,
+          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
   g_type_class_add_private (object_class, sizeof (EmpathyIndividualLinkerPriv));
 }
 
@@ -598,7 +721,10 @@ empathy_individual_linker_set_start_individual (EmpathyIndividualLinker *self,
   empathy_persona_store_set_individual (priv->persona_store,
       priv->new_individual);
 
+  g_object_freeze_notify (G_OBJECT (self));
   g_object_notify (G_OBJECT (self), "start-individual");
+  g_object_notify (G_OBJECT (self), "has-changed");
+  g_object_thaw_notify (G_OBJECT (self));
 }
 
 /**
@@ -630,3 +756,39 @@ empathy_individual_linker_get_linked_personas (EmpathyIndividualLinker *self)
   g_assert (personas != NULL);
   return personas;
 }
+
+/**
+ * empathy_individual_linker_get_has_changed:
+ * @self: an #EmpathyIndividualLinker
+ *
+ * Return whether #FolksIndividual<!-- -->s have been added to or removed from
+ * the linked individual currently displayed in the widget.
+ *
+ * This will be %FALSE after the widget is initialised, and set to %TRUE when
+ * an individual is checked in the individual view on the left of the widget.
+ * If the individual is later unchecked, this will be reset to %FALSE, etc.
+ *
+ * Return value: %TRUE if the linked individual has been changed, %FALSE
+ * otherwise
+ */
+gboolean
+empathy_individual_linker_get_has_changed (EmpathyIndividualLinker *self)
+{
+  EmpathyIndividualLinkerPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_LINKER (self), FALSE);
+
+  priv = GET_PRIV (self);
+
+  return (g_hash_table_size (priv->changed_individuals) > 0) ? TRUE : FALSE;
+}
+
+void
+empathy_individual_linker_set_search_text (EmpathyIndividualLinker *self,
+    const gchar *search_text)
+{
+  g_return_if_fail (EMPATHY_IS_INDIVIDUAL_LINKER (self));
+
+  empathy_live_search_set_text (
+      EMPATHY_LIVE_SEARCH (GET_PRIV (self)->search_widget), search_text);
+}