]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-roster-view.c
Use a flat namespace for internal includes
[empathy.git] / libempathy-gtk / empathy-roster-view.c
index 557728527900b52cee7bd7ea7059be0edf71c4cd..336f629c7b95eefab22467486e162818cbb1f465 100644 (file)
@@ -4,19 +4,21 @@
 
 #include <glib/gi18n-lib.h>
 
-#include <libempathy/empathy-contact-groups.h>
+#include "empathy-contact-groups.h"
 
-#include <libempathy-gtk/empathy-roster-contact.h>
-#include <libempathy-gtk/empathy-roster-group.h>
-#include <libempathy-gtk/empathy-ui-utils.h>
-
-#include <libempathy/empathy-utils.h>
+#include "empathy-roster-contact.h"
+#include "empathy-roster-group.h"
+#include "empathy-ui-utils.h"
 
 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
 
 /* Flashing delay for icons (milliseconds). */
 #define FLASH_TIMEOUT 500
 
+/* Delay in milliseconds between the last stroke on the keyboard and the start
+ * of the live search. */
+#define SEARCH_TIMEOUT 500
+
 enum
 {
   PROP_MODEL = 1,
@@ -61,6 +63,8 @@ struct _EmpathyRosterViewPriv
   guint flash_id;
   gboolean display_flash_event;
 
+  guint search_id;
+
   gboolean show_offline;
   gboolean show_groups;
   gboolean empty;
@@ -345,6 +349,32 @@ add_to_group (EmpathyRosterView *self,
     }
 }
 
+static void
+individual_favourite_change_cb (FolksIndividual *individual,
+    GParamSpec *spec,
+    EmpathyRosterView *self)
+{
+  /* We may have to refilter the contact as only favorite contacts are always
+   * displayed regardless of their presence. */
+  GHashTable *contacts;
+  GtkWidget *contact;
+
+  contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
+  if (contacts == NULL)
+    return;
+
+  if (self->priv->show_groups)
+    contact = g_hash_table_lookup (contacts,
+        EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
+  else
+    contact = g_hash_table_lookup (contacts, NO_GROUP);
+
+  if (contact == NULL)
+    return;
+
+  egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
+}
+
 static void
 individual_added (EmpathyRosterView *self,
     FolksIndividual *individual)
@@ -367,7 +397,7 @@ individual_added (EmpathyRosterView *self,
     {
       GList *groups, *l;
 
-      groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
+      groups = empathy_roster_model_dup_groups_for_individual (self->priv->model,
           individual);
 
       if (g_list_length (groups) > 0)
@@ -383,8 +413,11 @@ individual_added (EmpathyRosterView *self,
           add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
         }
 
-      g_list_free (groups);
+      g_list_free_full (groups, g_free);
     }
+
+  tp_g_signal_connect_object (individual, "notify::is-favourite",
+      G_CALLBACK (individual_favourite_change_cb), self, 0);
 }
 
 static void
@@ -559,13 +592,14 @@ contact_in_top (EmpathyRosterView *self,
 
       individual = empathy_roster_contact_get_individual (contact);
 
-      groups = empathy_roster_model_get_groups_for_individual (
+      groups = empathy_roster_model_dup_groups_for_individual (
           self->priv->model, individual);
 
-      if (g_list_find (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP) != NULL)
+      if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
+            (GCompareFunc) g_strcmp0) != NULL)
         result = TRUE;
 
-      g_list_free (groups);
+      g_list_free_full (groups, g_free);
 
       return result;
     }
@@ -793,6 +827,17 @@ remove_from_displayed (EmpathyRosterView *self,
   check_if_empty (self);
 }
 
+static gboolean
+contact_is_favorite (EmpathyRosterContact *contact)
+{
+  FolksIndividual *individual;
+
+  individual = empathy_roster_contact_get_individual (contact);
+
+  return folks_favourite_details_get_is_favourite (
+      FOLKS_FAVOURITE_DETAILS (individual));
+}
+
 /**
  * check if @contact should be displayed according to @self's current status
  * and without consideration for the state of @contact's groups.
@@ -815,7 +860,9 @@ contact_should_be_displayed (EmpathyRosterView *self,
   if (self->priv->show_offline)
       return TRUE;
 
-  if (contact_in_top (self, contact))
+  if (contact_in_top (self, contact) &&
+      contact_is_favorite (contact))
+    /* Favorite top contacts are always displayed */
     return TRUE;
 
   return empathy_roster_contact_is_online (contact);
@@ -864,6 +911,7 @@ filter_group (EmpathyRosterView *self,
     EmpathyRosterGroup *group)
 {
   GList *widgets, *l;
+  gboolean result = FALSE;
 
   /* Display the group if it contains at least one displayed contact */
   widgets = empathy_roster_group_get_widgets (group);
@@ -872,10 +920,15 @@ filter_group (EmpathyRosterView *self,
       EmpathyRosterContact *contact = l->data;
 
       if (contact_should_be_displayed (self, contact))
-        return TRUE;
+        {
+          result = TRUE;
+          break;
+        }
     }
 
-  return FALSE;
+  g_list_free (widgets);
+
+  return result;
 }
 
 static gboolean
@@ -1002,6 +1055,17 @@ empathy_roster_view_constructed (GObject *object)
   egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
 }
 
+static void
+clear_view (EmpathyRosterView *self)
+{
+  g_hash_table_remove_all (self->priv->roster_contacts);
+  g_hash_table_remove_all (self->priv->roster_groups);
+  g_hash_table_remove_all (self->priv->displayed_contacts);
+
+  gtk_container_foreach (GTK_CONTAINER (self),
+      (GtkCallback) gtk_widget_destroy, NULL);
+}
+
 static void
 empathy_roster_view_dispose (GObject *object)
 {
@@ -1009,11 +1073,21 @@ empathy_roster_view_dispose (GObject *object)
   void (*chain_up) (GObject *) =
       ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
 
+  /* Start by clearing the view so our internal hash tables are cleared from
+   * objects being destroyed. */
+  clear_view (self);
+
   stop_flashing (self);
 
   empathy_roster_view_set_live_search (self, NULL);
   g_clear_object (&self->priv->model);
 
+  if (self->priv->search_id != 0)
+    {
+      g_source_remove (self->priv->search_id);
+      self->priv->search_id = 0;
+    }
+
   if (chain_up != NULL)
     chain_up (object);
 }
@@ -1334,17 +1408,6 @@ empathy_roster_view_show_offline (EmpathyRosterView *self,
   g_object_notify (G_OBJECT (self), "show-offline");
 }
 
-static void
-clear_view (EmpathyRosterView *self)
-{
-  g_hash_table_remove_all (self->priv->roster_contacts);
-  g_hash_table_remove_all (self->priv->roster_groups);
-  g_hash_table_remove_all (self->priv->displayed_contacts);
-
-  gtk_container_foreach (GTK_CONTAINER (self),
-      (GtkCallback) gtk_widget_destroy, NULL);
-}
-
 void
 empathy_roster_view_show_groups (EmpathyRosterView *self,
     gboolean show)
@@ -1384,14 +1447,27 @@ select_first_contact (EmpathyRosterView *self)
   g_list_free (children);
 }
 
+static gboolean
+search_timeout_cb (EmpathyRosterView *self)
+{
+  egg_list_box_refilter (EGG_LIST_BOX (self));
+
+  select_first_contact (self);
+
+  self->priv->search_id = 0;
+  return G_SOURCE_REMOVE;
+}
+
 static void
 search_text_notify_cb (EmpathyLiveSearch *search,
     GParamSpec *pspec,
     EmpathyRosterView *self)
 {
-  egg_list_box_refilter (EGG_LIST_BOX (self));
+  if (self->priv->search_id != 0)
+    g_source_remove (self->priv->search_id);
 
-  select_first_contact (self);
+  self->priv->search_id = g_timeout_add (SEARCH_TIMEOUT,
+      (GSourceFunc) search_timeout_cb, self);
 }
 
 static void