Initial empathy_contact_selector_set_visible implementation.
authorJonny Lamb <jonny.lamb@collabora.co.uk>
Fri, 10 Apr 2009 16:56:03 +0000 (16:56 +0000)
committerXavier Claessens <xclaesse@src.gnome.org>
Fri, 10 Apr 2009 16:56:03 +0000 (16:56 +0000)
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
svn path=/trunk/; revision=2805

libempathy-gtk/empathy-contact-selector.c
libempathy-gtk/empathy-contact-selector.h

index 8607fa0af319f29042777177f10e69718301c9f3..796bc64fb2cd23cdb25aece76fdc7003502137f0 100644 (file)
@@ -45,6 +45,7 @@ typedef struct
 {
   EmpathyContactList *contact_list;
   EmpathyContactListStore *store;
+  GtkTreeModel *model;
   gboolean dispose_run;
 } EmpathyContactSelectorPriv;
 
@@ -52,17 +53,17 @@ static void contact_selector_manage_blank_contact (
     EmpathyContactSelector *selector);
 
 static guint
-contact_selector_get_number_online_contacts (GtkTreeStore *store)
+contact_selector_get_number_online_contacts (GtkTreeModel *model)
 {
   GtkTreeIter tmp_iter;
   gboolean is_online;
   guint number_online_contacts = 0;
   gboolean ok;
 
-  for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
-      ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
+  for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
+      ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
     {
-      gtk_tree_model_get (GTK_TREE_MODEL (store),
+      gtk_tree_model_get (model,
           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
           &is_online, -1);
       if (is_online)
@@ -73,7 +74,7 @@ contact_selector_get_number_online_contacts (GtkTreeStore *store)
 }
 
 static gboolean
-contact_selector_get_iter_for_blank_contact (GtkTreeStore *store,
+contact_selector_get_iter_for_blank_contact (GtkTreeModel *model,
                                              GtkTreeIter *blank_iter)
 {
   GtkTreeIter tmp_iter;
@@ -81,10 +82,10 @@ contact_selector_get_iter_for_blank_contact (GtkTreeStore *store,
   gboolean is_present = FALSE;
   gboolean ok;
 
-  for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
-      ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
+  for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
+      ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
     {
-      gtk_tree_model_get (GTK_TREE_MODEL (store),
+      gtk_tree_model_get (model,
           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
           &tmp_contact, -1);
       if (tmp_contact == NULL)
@@ -106,7 +107,7 @@ contact_selector_add_blank_contact (EmpathyContactSelector *selector)
   GtkTreeIter blank_iter;
 
   gtk_tree_store_insert_with_values (
-      GTK_TREE_STORE (priv->store),&blank_iter, NULL, 0,
+      GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0,
       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
       EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
       EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
@@ -123,8 +124,7 @@ contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
   GtkTreeIter blank_iter;
 
-  if (contact_selector_get_iter_for_blank_contact (
-        GTK_TREE_STORE (priv->store), &blank_iter))
+  if (contact_selector_get_iter_for_blank_contact (priv->model, &blank_iter))
     gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
 }
 
@@ -134,8 +134,7 @@ contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
   guint number_online_contacts;
 
-  number_online_contacts = contact_selector_get_number_online_contacts (
-      GTK_TREE_STORE (priv->store));
+  number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
 
   if (number_online_contacts != 0)
     gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
@@ -202,8 +201,9 @@ contact_selector_constructor (GType type,
       G_CALLBACK (contact_selector_manage_blank_contact),
       contact_selector);
 
-  gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
-      GTK_TREE_MODEL (priv->store));
+  priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
+
+  gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
   gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
 
   renderer = gtk_cell_renderer_pixbuf_new ();
@@ -289,6 +289,12 @@ contact_selector_dispose (GObject *object)
       priv->contact_list = NULL;
     }
 
+  if (priv->model)
+    {
+      g_object_unref (priv->model);
+      priv->model = NULL;
+    }
+
   if (priv->store)
     {
       g_object_unref (priv->store);
@@ -337,8 +343,60 @@ empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
     return NULL;
 
-  gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
+  gtk_tree_model_get (priv->model, &iter,
       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
 
   return contact;
 }
+
+typedef struct
+{
+  EmpathyContactSelectorFilterFunc func;
+  gpointer user_data;
+} FilterData;
+
+static void
+filter_data_free (gpointer data)
+{
+  g_slice_free (FilterData, data);
+}
+
+static gboolean
+contact_selector_filter_visible_func (GtkTreeModel *model,
+                                     GtkTreeIter *iter,
+                                     gpointer user_data)
+{
+  EmpathyContact *contact;
+  gboolean visible = FALSE;
+  FilterData *data = (FilterData *) user_data;
+
+  gtk_tree_model_get (model, iter, 0, &contact, -1);
+
+  if (contact != NULL)
+    {
+      visible = data->func(contact, data->user_data);
+
+      g_object_unref (contact);
+    }
+
+  return visible;
+
+}
+
+void
+empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
+                                      EmpathyContactSelectorFilterFunc func,
+                                      gpointer user_data)
+{
+  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
+  FilterData *data;
+
+  data = g_slice_new0 (FilterData);
+  data->func = func;
+  data->user_data = user_data;
+
+  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
+      contact_selector_filter_visible_func, data, filter_data_free);
+
+  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));
+}
index 19ca8cef8609c2b9dd1d20d525a1f010d2cbbdd3..f7af92f2cfc4e010ab26a84e47339fd280d27a62 100644 (file)
@@ -63,6 +63,11 @@ GtkWidget * empathy_contact_selector_new (EmpathyContactList *contact_list);
 
 EmpathyContact * empathy_contact_selector_dup_selected (EmpathyContactSelector *selector);
 
+typedef gboolean (*EmpathyContactSelectorFilterFunc) (EmpathyContact *contact, gpointer user_data);
+
+void empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
+   EmpathyContactSelectorFilterFunc func, gpointer user_data);
+
 G_END_DECLS
 
 #endif /* __EMPATHY_CONTACT_SELECTOR_H__ */