[empathy] Split the group editing widgets out of EmpathyContactWidget



commit 8da7760544ee3c592f9a884a13b13103b3b5a674
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Tue Aug 10 19:43:54 2010 +0100

    Split the group editing widgets out of EmpathyContactWidget
    
    They can be shared between EmpathyContactWidget and a new
    EmpathyIndividualWidget, once it's written.

 libempathy-gtk/Makefile.am               |    2 +
 libempathy-gtk/empathy-contact-widget.c  |  307 +--------------
 libempathy-gtk/empathy-contact-widget.ui |  104 +-----
 libempathy-gtk/empathy-groups-widget.c   |  600 ++++++++++++++++++++++++++++++
 libempathy-gtk/empathy-groups-widget.h   |   66 ++++
 po/POTFILES.in                           |    1 +
 6 files changed, 688 insertions(+), 392 deletions(-)
---
diff --git a/libempathy-gtk/Makefile.am b/libempathy-gtk/Makefile.am
index 5cb086e..878f9fb 100644
--- a/libempathy-gtk/Makefile.am
+++ b/libempathy-gtk/Makefile.am
@@ -53,6 +53,7 @@ libempathy_gtk_handwritten_source =            	\
 	empathy-contact-selector-dialog.c \
 	empathy-contact-widget.c		\
 	empathy-geometry.c			\
+	empathy-groups-widget.c			\
 	empathy-individual-dialogs.c		\
 	empathy-individual-information-dialog.c	\
 	empathy-individual-linker.c		\
@@ -109,6 +110,7 @@ libempathy_gtk_headers =			\
 	empathy-contact-selector-dialog.h \
 	empathy-contact-widget.h		\
 	empathy-geometry.h			\
+	empathy-groups-widget.h			\
 	empathy-images.h			\
 	empathy-individual-dialogs.h		\
 	empathy-individual-information-dialog.h	\
diff --git a/libempathy-gtk/empathy-contact-widget.c b/libempathy-gtk/empathy-contact-widget.c
index de958c3..da4e002 100644
--- a/libempathy-gtk/empathy-contact-widget.c
+++ b/libempathy-gtk/empathy-contact-widget.c
@@ -47,6 +47,7 @@
 #include "empathy-account-chooser.h"
 #include "empathy-avatar-chooser.h"
 #include "empathy-avatar-image.h"
+#include "empathy-groups-widget.h"
 #include "empathy-ui-utils.h"
 #include "empathy-string-parser.h"
 #include "empathy-kludge-label.h"
@@ -114,10 +115,7 @@ typedef struct
 #endif
 
   /* Groups */
-  GtkWidget *vbox_groups;
-  GtkWidget *entry_group;
-  GtkWidget *button_group;
-  GtkWidget *treeview_groups;
+  GtkWidget *groups_widget;
 
   /* Details */
   GtkWidget *vbox_details;
@@ -550,287 +548,27 @@ contact_widget_client_setup (EmpathyContactWidget *information)
 }
 
 static void
-contact_widget_cell_toggled (GtkCellRendererToggle *cell,
-                             gchar *path_string,
-                             EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkTreeModel *model;
-  GtkListStore *store;
-  GtkTreePath *path;
-  GtkTreeIter iter;
-  gboolean was_enabled;
-  gchar *group;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  model = gtk_tree_view_get_model (view);
-  store = GTK_LIST_STORE (model);
-
-  path = gtk_tree_path_new_from_string (path_string);
-
-  gtk_tree_model_get_iter (model, &iter, path);
-  gtk_tree_model_get (model, &iter,
-      COL_ENABLED, &was_enabled,
-      COL_NAME, &group,
-      -1);
-
-  gtk_list_store_set (store, &iter, COL_ENABLED, !was_enabled, -1);
-  gtk_tree_path_free (path);
-
-  if (group != NULL)
-    {
-      empathy_contact_change_group (information->contact, group, !was_enabled);
-      g_free (group);
-    }
-}
-
-static void
-contact_widget_model_populate_columns (EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkTreeModel *model;
-  GtkTreeViewColumn *column;
-  GtkCellRenderer  *renderer;
-  guint col_offset;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  model = gtk_tree_view_get_model (view);
-
-  renderer = gtk_cell_renderer_toggle_new ();
-  g_signal_connect (renderer, "toggled",
-      G_CALLBACK (contact_widget_cell_toggled), information);
-
-  column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
-      "active", COL_ENABLED, NULL);
-
-  gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
-  gtk_tree_view_column_set_fixed_width (column, 50);
-  gtk_tree_view_append_column (view, column);
-
-  renderer = gtk_cell_renderer_text_new ();
-  col_offset = gtk_tree_view_insert_column_with_attributes (view,
-      -1, _("Group"),
-      renderer,
-      "text", COL_NAME,
-      /* "editable", COL_EDITABLE, */
-      NULL);
-
-  g_object_set_data (G_OBJECT (renderer),
-      "column", GINT_TO_POINTER (COL_NAME));
-
-  column = gtk_tree_view_get_column (view, col_offset - 1);
-  gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
-  gtk_tree_view_column_set_resizable (column,FALSE);
-  gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
-}
-
-static void
-contact_widget_model_setup (EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkListStore *store;
-  GtkTreeSelection *selection;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-
-  store = gtk_list_store_new (COL_COUNT,
-      G_TYPE_STRING,   /* name */
-      G_TYPE_BOOLEAN,  /* enabled */
-      G_TYPE_BOOLEAN); /* editable */
-
-  gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
-
-  selection = gtk_tree_view_get_selection (view);
-  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
-
-  contact_widget_model_populate_columns (information);
-
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
-      COL_NAME, GTK_SORT_ASCENDING);
-
-  g_object_unref (store);
-}
-
-static void
-contact_widget_groups_populate_data (EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkListStore *store;
-  GtkTreeIter iter;
-  GList *my_groups, *l;
-  GList *all_groups;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
-  gtk_list_store_clear (store);
-
-  all_groups = empathy_contact_list_get_all_groups (
-      EMPATHY_CONTACT_LIST (information->manager));
-  my_groups = empathy_contact_list_get_groups (
-      EMPATHY_CONTACT_LIST (information->manager),
-      information->contact);
-
-  for (l = all_groups; l; l = l->next)
-    {
-      const gchar *group_str;
-      gboolean enabled;
-
-      group_str = l->data;
-
-      enabled = g_list_find_custom (my_groups,
-          group_str, (GCompareFunc) strcmp) != NULL;
-
-      gtk_list_store_append (store, &iter);
-      gtk_list_store_set (store, &iter,
-          COL_NAME, group_str,
-          COL_EDITABLE, TRUE,
-          COL_ENABLED, enabled,
-          -1);
-    }
-
-  g_list_foreach (all_groups, (GFunc) g_free, NULL);
-  g_list_foreach (my_groups, (GFunc) g_free, NULL);
-  g_list_free (all_groups);
-  g_list_free (my_groups);
-}
-
-static gboolean
-contact_widget_model_find_name_foreach (GtkTreeModel *model,
-                                        GtkTreePath *path,
-                                        GtkTreeIter *iter,
-                                        FindName *data)
-{
-  gchar *name;
-
-  gtk_tree_model_get (model, iter,
-      COL_NAME, &name,
-      -1);
-
-  if (!name)
-      return FALSE;
-
-  if (data->name && strcmp (data->name, name) == 0)
-    {
-      data->found = TRUE;
-      data->found_iter = *iter;
-
-      g_free (name);
-
-      return TRUE;
-    }
-
-  g_free (name);
-
-  return FALSE;
-}
-
-static gboolean
-contact_widget_model_find_name (EmpathyContactWidget *information,
-                                const gchar *name,
-                                GtkTreeIter *iter)
-{
-  GtkTreeView *view;
-  GtkTreeModel *model;
-  FindName data;
-
-  if (EMP_STR_EMPTY (name))
-      return FALSE;
-
-  data.information = information;
-  data.name = name;
-  data.found = FALSE;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  model = gtk_tree_view_get_model (view);
-
-  gtk_tree_model_foreach (model,
-      (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
-      &data);
-
-  if (data.found == TRUE)
-    {
-      *iter = data.found_iter;
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-static void
-contact_widget_entry_group_changed_cb (GtkEditable *editable,
-                                       EmpathyContactWidget *information)
-{
-  GtkTreeIter iter;
-  const gchar *group;
-
-  group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
-
-  if (contact_widget_model_find_name (information, group, &iter))
-      gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
-  else
-      gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
-          !EMP_STR_EMPTY (group));
-}
-
-static void
-contact_widget_entry_group_activate_cb (GtkEntry *entry,
-                                        EmpathyContactWidget  *information)
-{
-  gtk_widget_activate (GTK_WIDGET (information->button_group));
-}
-
-static void
-contact_widget_button_group_clicked_cb (GtkButton *button,
-                                        EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkListStore *store;
-  GtkTreeIter iter;
-  const gchar *group;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
-
-  group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
-
-  gtk_list_store_append (store, &iter);
-  gtk_list_store_set (store, &iter,
-      COL_NAME, group,
-      COL_ENABLED, TRUE,
-      -1);
-
-  empathy_contact_change_group (information->contact, group, TRUE);
-}
-
-static void
-contact_widget_groups_notify_cb (EmpathyContactWidget *information)
-{
-  /* FIXME: not implemented */
-}
-
-static void
-contact_widget_groups_setup (EmpathyContactWidget *information)
-{
-  if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
-    {
-      contact_widget_model_setup (information);
-    }
-}
-
-static void
 contact_widget_groups_update (EmpathyContactWidget *information)
 {
   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
-      information->contact)
+      information->contact != NULL)
     {
-      g_signal_connect_swapped (information->contact, "notify::groups",
-          G_CALLBACK (contact_widget_groups_notify_cb), information);
-      contact_widget_groups_populate_data (information);
+      FolksPersona *persona =
+          empathy_contact_get_persona (information->contact);
+
+      if (FOLKS_IS_GROUPS (persona))
+        {
+          empathy_groups_widget_set_groupable (
+              EMPATHY_GROUPS_WIDGET (information->groups_widget),
+              FOLKS_GROUPS (persona));
+          gtk_widget_show (information->groups_widget);
 
-      gtk_widget_show (information->vbox_groups);
+          return;
+        }
     }
-  else
-      gtk_widget_hide (information->vbox_groups);
+
+  /* In case of failure */
+  gtk_widget_hide (information->groups_widget);
 }
 
 /* Converts the Location's GHashTable's key to a user readable string */
@@ -1421,8 +1159,6 @@ contact_widget_remove_contact (EmpathyContactWidget *information)
           contact_widget_presence_notify_cb, information);
       g_signal_handlers_disconnect_by_func (information->contact,
           contact_widget_avatar_notify_cb, information);
-      g_signal_handlers_disconnect_by_func (information->contact,
-          contact_widget_groups_notify_cb, information);
 
       tp_contact = empathy_contact_get_tp_contact (information->contact);
       if (tp_contact != NULL)
@@ -1871,10 +1607,7 @@ empathy_contact_widget_new (EmpathyContact *contact,
 #ifdef HAVE_LIBCHAMPLAIN
        "viewport_map", &information->viewport_map,
 #endif
-       "vbox_groups", &information->vbox_groups,
-       "entry_group", &information->entry_group,
-       "button_group", &information->button_group,
-       "treeview_groups", &information->treeview_groups,
+       "groups_widget", &information->groups_widget,
        "vbox_details", &information->vbox_details,
        "table_details", &information->table_details,
        "hbox_details_requested", &information->hbox_details_requested,
@@ -1886,9 +1619,6 @@ empathy_contact_widget_new (EmpathyContact *contact,
 
   empathy_builder_connect (gui, information,
       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
-      "entry_group", "changed", contact_widget_entry_group_changed_cb,
-      "entry_group", "activate", contact_widget_entry_group_activate_cb,
-      "button_group", "clicked", contact_widget_button_group_clicked_cb,
       NULL);
   information->table_location = NULL;
 
@@ -1900,7 +1630,6 @@ empathy_contact_widget_new (EmpathyContact *contact,
 
   /* Create widgets */
   contact_widget_contact_setup (information);
-  contact_widget_groups_setup (information);
   contact_widget_details_setup (information);
   contact_widget_client_setup (information);
 
diff --git a/libempathy-gtk/empathy-contact-widget.ui b/libempathy-gtk/empathy-contact-widget.ui
index 0792dcf..acfad6a 100644
--- a/libempathy-gtk/empathy-contact-widget.ui
+++ b/libempathy-gtk/empathy-contact-widget.ui
@@ -172,109 +172,7 @@
       </packing>
     </child>
     <child>
-      <object class="GtkVBox" id="vbox_groups">
-        <property name="spacing">6</property>
-        <child>
-          <object class="GtkLabel" id="label672">
-            <property name="visible">True</property>
-            <property name="xalign">0</property>
-            <property name="label" translatable="yes">Groups</property>
-            <attributes>
-              <attribute name="weight" value="bold"/>
-            </attributes>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="fill">False</property>
-            <property name="position">0</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GtkAlignment" id="alignment33">
-            <property name="visible">True</property>
-            <property name="left_padding">12</property>
-            <child>
-              <object class="GtkVBox" id="vbox224">
-                <property name="visible">True</property>
-                <property name="spacing">6</property>
-                <child>
-                  <object class="GtkLabel" id="label679">
-                    <property name="visible">True</property>
-                    <property name="xalign">0</property>
-                    <property name="label" translatable="yes">Select the groups you want this contact to appear in.  Note that you can select more than one group or no groups.</property>
-                    <property name="use_markup">True</property>
-                    <property name="wrap">True</property>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">False</property>
-                    <property name="position">0</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkHBox" id="hbox188">
-                    <property name="visible">True</property>
-                    <property name="spacing">12</property>
-                    <child>
-                      <object class="GtkEntry" id="entry_group">
-                        <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                      </object>
-                      <packing>
-                        <property name="position">0</property>
-                      </packing>
-                    </child>
-                    <child>
-                      <object class="GtkButton" id="button_group">
-                        <property name="label" translatable="yes">_Add Group</property>
-                        <property name="visible">True</property>
-                        <property name="sensitive">False</property>
-                        <property name="can_focus">True</property>
-                        <property name="receives_default">True</property>
-                        <property name="use_underline">True</property>
-                      </object>
-                      <packing>
-                        <property name="expand">False</property>
-                        <property name="fill">False</property>
-                        <property name="position">1</property>
-                      </packing>
-                    </child>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">False</property>
-                    <property name="position">1</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkScrolledWindow" id="scrolledwindow17">
-                    <property name="height_request">100</property>
-                    <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="hscrollbar_policy">never</property>
-                    <property name="vscrollbar_policy">automatic</property>
-                    <property name="shadow_type">in</property>
-                    <child>
-                      <object class="GtkTreeView" id="treeview_groups">
-                        <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                        <property name="headers_visible">False</property>
-                        <property name="enable_search">False</property>
-                      </object>
-                    </child>
-                  </object>
-                  <packing>
-                    <property name="position">2</property>
-                  </packing>
-                </child>
-              </object>
-            </child>
-          </object>
-          <packing>
-            <property name="position">1</property>
-          </packing>
-        </child>
-      </object>
+      <object class="EmpathyGroupsWidget" id="groups_widget"/>
       <packing>
         <property name="position">2</property>
       </packing>
diff --git a/libempathy-gtk/empathy-groups-widget.c b/libempathy-gtk/empathy-groups-widget.c
new file mode 100644
index 0000000..98c2a6a
--- /dev/null
+++ b/libempathy-gtk/empathy-groups-widget.c
@@ -0,0 +1,600 @@
+/*
+ * Copyright (C) 2007-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
+ *
+ * Authors: Xavier Claessens <xclaesse gmail com>
+ *          Philip Withnall <philip withnall collabora co uk>
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <gtk/gtk.h>
+#include <glib/gi18n-lib.h>
+
+#include <telepathy-glib/util.h>
+
+#include <folks/folks.h>
+
+#include <libempathy/empathy-utils.h>
+#include <libempathy/empathy-contact-manager.h>
+
+#include "empathy-groups-widget.h"
+#include "empathy-ui-utils.h"
+
+/**
+ * SECTION:empathy-groups-widget
+ * @title:EmpathyGroupsWidget
+ * @short_description: A widget used to edit the groups of a #FolksGroups
+ * @include: libempathy-gtk/empathy-groups-widget.h
+ *
+ * #EmpathyGroupsWidget is a widget which lists the groups of a #FolksGroups
+ * (i.e. a #FolksPersona or a #FolksIndividual) and allows them to be added and
+ * removed.
+ */
+
+/**
+ * EmpathyGroupsWidget:
+ * @parent: parent object
+ *
+ * Widget which displays and allows editing of the groups of a #FolksGroups
+ * (i.e. a #FolksPersona or #FolksIndividual).
+ */
+
+/* Delay before updating the widget when the id entry changed (seconds) */
+#define ID_CHANGED_TIMEOUT 1
+
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyGroupsWidget)
+
+typedef struct
+{
+  /* The object we're actually changing the groups of */
+  FolksGroups *groupable; /* owned */
+  GtkListStore *group_store; /* owned */
+
+  GtkWidget *add_group_entry; /* child widget */
+  GtkWidget *add_group_button; /* child widget */
+} EmpathyGroupsWidgetPriv;
+
+enum {
+  PROP_GROUPABLE = 1,
+};
+
+enum {
+  COL_NAME,
+  COL_ENABLED,
+  COL_EDITABLE
+};
+#define NUM_COLUMNS COL_EDITABLE + 1
+
+G_DEFINE_TYPE (EmpathyGroupsWidget, empathy_groups_widget, GTK_TYPE_BOX);
+
+typedef struct
+{
+  EmpathyGroupsWidget *widget;
+  const gchar *name;
+  gboolean found;
+  GtkTreeIter found_iter;
+} FindNameData;
+
+static gboolean
+model_find_name_foreach (GtkTreeModel *model,
+    GtkTreePath *path,
+    GtkTreeIter *iter,
+    FindNameData *data)
+{
+  gchar *name;
+
+  gtk_tree_model_get (model, iter,
+      COL_NAME, &name,
+      -1);
+
+  if (name != NULL && strcmp (data->name, name) == 0)
+    {
+      data->found = TRUE;
+      data->found_iter = *iter;
+
+      g_free (name);
+
+      return TRUE;
+    }
+
+  g_free (name);
+
+  return FALSE;
+}
+
+static gboolean
+model_find_name (EmpathyGroupsWidget *self,
+    const gchar *name,
+    GtkTreeIter *iter)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
+  FindNameData data;
+
+  if (EMP_STR_EMPTY (name))
+    return FALSE;
+
+  data.widget = self;
+  data.name = name;
+  data.found = FALSE;
+
+  gtk_tree_model_foreach (GTK_TREE_MODEL (priv->group_store),
+      (GtkTreeModelForeachFunc) model_find_name_foreach, &data);
+
+  if (data.found == TRUE)
+    {
+      *iter = data.found_iter;
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static void
+populate_data (EmpathyGroupsWidget *self)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
+  EmpathyContactManager *manager;
+  GtkTreeIter iter;
+  GHashTable *my_groups;
+  GList *all_groups, *l;
+
+  /* Remove the old groups */
+  gtk_list_store_clear (priv->group_store);
+
+  /* FIXME: We have to get the whole group list from EmpathyContactManager, as
+   * libfolks hasn't grown API to get the whole group list yet. (bgo#627398) */
+  manager = empathy_contact_manager_dup_singleton ();
+  all_groups = empathy_contact_list_get_all_groups (
+      EMPATHY_CONTACT_LIST (manager));
+  g_object_unref (manager);
+
+  /* Get the list of groups that this #FolksGroups is currently in */
+  my_groups = folks_groups_get_groups (priv->groupable);
+
+  for (l = all_groups; l != NULL; l = l->next)
+    {
+      const gchar *group_str = l->data;
+      gboolean enabled;
+
+      enabled = GPOINTER_TO_UINT (g_hash_table_lookup (my_groups, group_str));
+
+      gtk_list_store_append (priv->group_store, &iter);
+      gtk_list_store_set (priv->group_store, &iter,
+          COL_NAME, group_str,
+          COL_EDITABLE, TRUE,
+          COL_ENABLED, enabled,
+          -1);
+
+      g_free (l->data);
+    }
+
+  g_list_free (all_groups);
+}
+
+static void
+add_group_entry_changed_cb (GtkEditable *editable,
+    EmpathyGroupsWidget *self)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
+  GtkTreeIter iter;
+  const gchar *group;
+
+  group = gtk_entry_get_text (GTK_ENTRY (priv->add_group_entry));
+
+  if (model_find_name (self, group, &iter))
+    {
+      gtk_widget_set_sensitive (GTK_WIDGET (priv->add_group_button), FALSE);
+    }
+  else
+    {
+      gtk_widget_set_sensitive (GTK_WIDGET (priv->add_group_button),
+          !EMP_STR_EMPTY (group));
+    }
+}
+
+static void
+add_group_entry_activate_cb (GtkEntry *entry,
+    EmpathyGroupsWidget  *self)
+{
+  gtk_widget_activate (GTK_WIDGET (GET_PRIV (self)->add_group_button));
+}
+
+static void
+change_group_cb (FolksGroups *groupable,
+    GAsyncResult *async_result,
+    EmpathyGroupsWidget *self)
+{
+  GError *error = NULL;
+
+  folks_groups_change_group_finish (groupable, async_result, &error);
+
+  if (error != NULL)
+    {
+      g_warning ("Failed to change group: %s", error->message);
+      g_clear_error (&error);
+    }
+}
+
+static void
+add_group_button_clicked_cb (GtkButton *button,
+   EmpathyGroupsWidget *self)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
+  GtkTreeIter iter;
+  const gchar *group;
+
+  group = gtk_entry_get_text (GTK_ENTRY (priv->add_group_entry));
+
+  gtk_list_store_append (priv->group_store, &iter);
+  gtk_list_store_set (priv->group_store, &iter,
+      COL_NAME, group,
+      COL_ENABLED, TRUE,
+      -1);
+
+  folks_groups_change_group (priv->groupable, group, TRUE,
+      (GAsyncReadyCallback) change_group_cb, self);
+}
+
+static void
+cell_toggled_cb (GtkCellRendererToggle *cell,
+    const gchar *path_string,
+    EmpathyGroupsWidget *self)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
+  GtkTreePath *path;
+  GtkTreeIter iter;
+  gboolean was_enabled;
+  gchar *group;
+
+  path = gtk_tree_path_new_from_string (path_string);
+
+  gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->group_store), &iter,
+      path);
+  gtk_tree_model_get (GTK_TREE_MODEL (priv->group_store), &iter,
+      COL_ENABLED, &was_enabled,
+      COL_NAME, &group,
+      -1);
+
+  gtk_list_store_set (priv->group_store, &iter,
+      COL_ENABLED, !was_enabled,
+      -1);
+
+  gtk_tree_path_free (path);
+
+  if (group != NULL)
+    {
+      folks_groups_change_group (priv->groupable, group, !was_enabled,
+          (GAsyncReadyCallback) change_group_cb, self);
+      g_free (group);
+    }
+}
+
+
+static void
+groupable_group_changed_cb (FolksGroups *groups,
+    const gchar *group,
+    gboolean is_member,
+    EmpathyGroupsWidget *self)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
+  GtkTreeIter iter;
+
+  if (model_find_name (self, group, &iter) == TRUE)
+    {
+      gtk_list_store_set (priv->group_store, &iter,
+          COL_ENABLED, is_member,
+          -1);
+    }
+}
+
+static void
+set_up (EmpathyGroupsWidget *self)
+{
+  EmpathyGroupsWidgetPriv *priv;
+  GtkWidget *label, *alignment;
+  GtkBox *vbox, *hbox;
+  GtkTreeView *tree_view;
+  GtkTreeSelection *selection;
+  GtkTreeViewColumn *column;
+  GtkCellRenderer *renderer;
+  guint col_offset;
+  GtkScrolledWindow *scrolled_window;
+  gchar *markup;
+
+  priv = GET_PRIV (self);
+
+  /* Set up ourself */
+  gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
+      GTK_ORIENTATION_VERTICAL);
+  gtk_box_set_spacing (GTK_BOX (self), 6);
+
+  /* Create our child widgets */
+  label = gtk_label_new (NULL);
+  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+
+  markup = g_strdup_printf ("<b>%s</b>", _("Groups"));
+  gtk_label_set_markup (GTK_LABEL (label), markup);
+  g_free (markup);
+
+  gtk_box_pack_start (GTK_BOX (self), label, FALSE, FALSE, 0);
+  gtk_widget_show (label);
+
+  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
+  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
+
+  vbox = GTK_BOX (gtk_vbox_new (FALSE, 6));
+
+  label = gtk_label_new (_("Select the groups you want this contact to appear "
+      "in.  Note that you can select more than one group or no groups."));
+  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+
+  gtk_box_pack_start (vbox, label, FALSE, FALSE, 0);
+  gtk_widget_show (label);
+
+  hbox = GTK_BOX (gtk_hbox_new (FALSE, 12));
+
+  priv->add_group_entry = gtk_entry_new ();
+  g_signal_connect (priv->add_group_entry, "changed",
+      (GCallback) add_group_entry_changed_cb, self);
+  g_signal_connect (priv->add_group_entry, "activate",
+      (GCallback) add_group_entry_activate_cb, self);
+
+  gtk_box_pack_start (hbox, priv->add_group_entry, TRUE, TRUE, 0);
+  gtk_widget_show (priv->add_group_entry);
+
+  priv->add_group_button = gtk_button_new_with_mnemonic (_("_Add Group"));
+  gtk_widget_set_sensitive (priv->add_group_button, FALSE);
+  gtk_widget_set_receives_default (priv->add_group_button, TRUE);
+  g_signal_connect (priv->add_group_button, "clicked",
+      (GCallback) add_group_button_clicked_cb, self);
+
+  gtk_box_pack_start (hbox, priv->add_group_button, FALSE, FALSE, 0);
+  gtk_widget_show (priv->add_group_button);
+
+  gtk_box_pack_start (vbox, GTK_WIDGET (hbox), FALSE, FALSE, 0);
+  gtk_widget_show (GTK_WIDGET (hbox));
+
+  scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
+  gtk_scrolled_window_set_policy (scrolled_window, GTK_POLICY_NEVER,
+      GTK_POLICY_AUTOMATIC);
+  gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
+  gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
+
+  priv->group_store = gtk_list_store_new (NUM_COLUMNS,
+      G_TYPE_STRING,   /* name */
+      G_TYPE_BOOLEAN,  /* enabled */
+      G_TYPE_BOOLEAN); /* editable */
+
+  tree_view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (
+      GTK_TREE_MODEL (priv->group_store)));
+  gtk_tree_view_set_headers_visible (tree_view, FALSE);
+  gtk_tree_view_set_enable_search (tree_view, FALSE);
+
+  selection = gtk_tree_view_get_selection (tree_view);
+  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
+
+  renderer = gtk_cell_renderer_toggle_new ();
+  g_signal_connect (renderer, "toggled", (GCallback) cell_toggled_cb, self);
+
+  column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
+      "active", COL_ENABLED,
+      NULL);
+
+  gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
+  gtk_tree_view_column_set_fixed_width (column, 50);
+  gtk_tree_view_append_column (tree_view, column);
+
+  renderer = gtk_cell_renderer_text_new ();
+  col_offset = gtk_tree_view_insert_column_with_attributes (tree_view,
+      -1, _("Group"),
+      renderer,
+      "text", COL_NAME,
+      /* "editable", COL_EDITABLE, */
+      NULL);
+
+  column = gtk_tree_view_get_column (tree_view, col_offset - 1);
+  gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
+  gtk_tree_view_column_set_resizable (column, FALSE);
+  gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
+
+  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->group_store),
+      COL_NAME, GTK_SORT_ASCENDING);
+
+  gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (tree_view));
+  gtk_widget_show (GTK_WIDGET (tree_view));
+
+  gtk_box_pack_start (vbox, GTK_WIDGET (scrolled_window), TRUE, TRUE, 0);
+  gtk_widget_show (GTK_WIDGET (scrolled_window));
+
+  gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (vbox));
+  gtk_widget_show (GTK_WIDGET (vbox));
+
+  gtk_box_pack_start (GTK_BOX (self), alignment, TRUE, TRUE, 0);
+  gtk_widget_show (alignment);
+}
+
+static void
+empathy_groups_widget_init (EmpathyGroupsWidget *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_GROUPS_WIDGET, EmpathyGroupsWidgetPriv);
+
+  set_up (self);
+}
+
+static void
+get_property (GObject *object,
+    guint param_id,
+    GValue *value,
+    GParamSpec *pspec)
+{
+  EmpathyGroupsWidgetPriv *priv;
+
+  priv = GET_PRIV (object);
+
+  switch (param_id)
+    {
+      case PROP_GROUPABLE:
+        g_value_set_object (value, priv->groupable);
+        break;
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+        break;
+    }
+}
+
+static void
+set_property (GObject *object,
+    guint param_id,
+    const GValue *value,
+    GParamSpec *pspec)
+{
+  EmpathyGroupsWidgetPriv *priv;
+
+  priv = GET_PRIV (object);
+
+  switch (param_id)
+    {
+      case PROP_GROUPABLE:
+        empathy_groups_widget_set_groupable (EMPATHY_GROUPS_WIDGET (object),
+            g_value_get_object (value));
+        break;
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+        break;
+    }
+}
+
+static void
+dispose (GObject *object)
+{
+  EmpathyGroupsWidgetPriv *priv = GET_PRIV (object);
+
+  tp_clear_object (&priv->groupable);
+  tp_clear_object (&priv->group_store);
+
+  G_OBJECT_CLASS (empathy_groups_widget_parent_class)->dispose (object);
+}
+
+static void
+empathy_groups_widget_class_init (EmpathyGroupsWidgetClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = get_property;
+  object_class->set_property = set_property;
+  object_class->dispose = dispose;
+
+  /**
+   * EmpathyGroupsWidget:groupable:
+   *
+   * The #FolksGroups whose group membership is to be edited by the
+   * #EmpathyGroupsWidget.
+   */
+  g_object_class_install_property (object_class, PROP_GROUPABLE,
+      g_param_spec_object ("groupable",
+          "Groupable",
+          "The #FolksGroups whose groups are being edited.",
+          FOLKS_TYPE_GROUPS,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_type_class_add_private (object_class, sizeof (EmpathyGroupsWidgetPriv));
+}
+
+/**
+ * empathy_groups_widget_new:
+ * @groupable: a #FolksGroups, or %NULL
+ *
+ * Creates a new #EmpathyGroupsWidget to edit the groups of the given
+ * @groupable.
+ *
+ * Return value: a new #EmpathyGroupsWidget
+ */
+GtkWidget *
+empathy_groups_widget_new (FolksGroups *groupable)
+{
+  g_return_val_if_fail (groupable == NULL || FOLKS_IS_GROUPS (groupable),
+      NULL);
+
+  return GTK_WIDGET (g_object_new (EMPATHY_TYPE_GROUPS_WIDGET,
+      "groupable", groupable,
+      NULL));
+}
+
+/**
+ * empathy_groups_widget_get_groupable:
+ * @self: an #EmpathyGroupsWidget
+ *
+ * Get the #FolksGroups whose group membership is being edited by the
+ * #EmpathyGroupsWidget.
+ *
+ * Returns: the #FolksGroups associated with @widget, or %NULL
+ */
+FolksGroups *
+empathy_groups_widget_get_groupable (EmpathyGroupsWidget *self)
+{
+  g_return_val_if_fail (EMPATHY_IS_GROUPS_WIDGET (self), NULL);
+
+  return GET_PRIV (self)->groupable;
+}
+
+/**
+ * empathy_groups_widget_set_groupable:
+ * @self: an #EmpathyGroupsWidget
+ * @groupable: the #FolksGroups whose membership is to be edited, or %NULL
+ *
+ * Change the #FolksGroups whose group membership is to be edited by the
+ * #EmpathyGroupsWidget.
+ */
+void
+empathy_groups_widget_set_groupable (EmpathyGroupsWidget *self,
+    FolksGroups *groupable)
+{
+  EmpathyGroupsWidgetPriv *priv;
+
+  g_return_if_fail (EMPATHY_IS_GROUPS_WIDGET (self));
+  g_return_if_fail (groupable == NULL || FOLKS_IS_GROUPS (groupable));
+
+  priv = GET_PRIV (self);
+
+  if (groupable == priv->groupable)
+    return;
+
+  if (priv->groupable != NULL)
+    {
+      g_signal_handlers_disconnect_by_func (priv->groupable,
+          groupable_group_changed_cb, self);
+    }
+
+  tp_clear_object (&priv->groupable);
+
+  if (groupable != NULL)
+    {
+      priv->groupable = g_object_ref (groupable);
+
+      g_signal_connect (priv->groupable, "group-changed",
+          (GCallback) groupable_group_changed_cb, self);
+
+      populate_data (self);
+    }
+
+  g_object_notify (G_OBJECT (self), "groupable");
+}
diff --git a/libempathy-gtk/empathy-groups-widget.h b/libempathy-gtk/empathy-groups-widget.h
new file mode 100644
index 0000000..120d45e
--- /dev/null
+++ b/libempathy-gtk/empathy-groups-widget.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2007-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
+ *
+ * Authors: Xavier Claessens <xclaesse gmail com>
+ *          Philip Withnall <philip withnall collabora co uk>
+ */
+
+#ifndef __EMPATHY_GROUPS_WIDGET_H__
+#define __EMPATHY_GROUPS_WIDGET_H__
+
+#include <gtk/gtk.h>
+
+#include <folks/folks.h>
+
+G_BEGIN_DECLS
+
+#define EMPATHY_TYPE_GROUPS_WIDGET (empathy_groups_widget_get_type ())
+#define EMPATHY_GROUPS_WIDGET(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), \
+    EMPATHY_TYPE_GROUPS_WIDGET, EmpathyGroupsWidget))
+#define EMPATHY_GROUPS_WIDGET_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), \
+    EMPATHY_TYPE_GROUPS_WIDGET, EmpathyGroupsWidgetClass))
+#define EMPATHY_IS_GROUPS_WIDGET(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), \
+    EMPATHY_TYPE_GROUPS_WIDGET))
+#define EMPATHY_IS_GROUPS_WIDGET_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), \
+    EMPATHY_TYPE_GROUPS_WIDGET))
+#define EMPATHY_GROUPS_WIDGET_GET_CLASS(o) ( \
+    G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_GROUPS_WIDGET, \
+        EmpathyGroupsWidgetClass))
+
+typedef struct {
+	GtkBox parent;
+
+	/*<private>*/
+	gpointer priv;
+} EmpathyGroupsWidget;
+
+typedef struct {
+	GtkBoxClass parent_class;
+} EmpathyGroupsWidgetClass;
+
+GType empathy_groups_widget_get_type (void) G_GNUC_CONST;
+
+GtkWidget * empathy_groups_widget_new (FolksGroups *groupable);
+
+FolksGroups * empathy_groups_widget_get_groupable (
+    EmpathyGroupsWidget *self);
+void empathy_groups_widget_set_groupable (EmpathyGroupsWidget *self,
+    FolksGroups *groupable);
+
+G_END_DECLS
+
+#endif /*  __EMPATHY_GROUPS_WIDGET_H__ */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 209c8e5..d5a1e18 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -37,6 +37,7 @@ libempathy-gtk/empathy-contact-menu.c
 libempathy-gtk/empathy-contact-selector.c
 libempathy-gtk/empathy-contact-widget.c
 [type: gettext/glade]libempathy-gtk/empathy-contact-widget.ui
+libempathy-gtk/empathy-groups-widget.c
 libempathy-gtk/empathy-individual-dialogs.c
 libempathy-gtk/empathy-individual-linker.c
 libempathy-gtk/empathy-individual-menu.c



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