]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-individual-manager.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy / empathy-individual-manager.c
index e80e132248fbc8458ff94701018bae3fd0546c10..71960b5d2887d409a2049a689561b35721870d67 100644 (file)
  *          Travis Reitter <travis.reitter@collabora.co.uk>
  */
 
-#include <config.h>
-
-#include <string.h>
-
-#include <telepathy-glib/telepathy-glib.h>
-#include <telepathy-glib/account-manager.h>
-#include <telepathy-glib/enums.h>
-#include <telepathy-glib/proxy-subclass.h>
-#include <telepathy-glib/util.h>
-
-#include <folks/folks.h>
-#include <folks/folks-telepathy.h>
-
-#include <extensions/extensions.h>
-
+#include "config.h"
 #include "empathy-individual-manager.h"
+
 #include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
  * changes, not when the position of every single individual is updated. */
 #define TOP_INDIVIDUALS_LEN 5
 
+/* The constant INDIVIDUALS_COUNT_COMPRESS_FACTOR represents the number of
+ * interactions needed to be considered as 1 interaction */
+#define INTERACTION_COUNT_COMPRESS_FACTOR 50
+
+/* The constant DAY_IN_SECONDS represents the seconds in a day */
+#define DAY_IN_SECONDS 86400
+
 /* This class only stores and refs Individuals who contain an EmpathyContact.
  *
  * This class merely forwards along signals from the aggregator and individuals
@@ -58,11 +52,12 @@ typedef struct
   GHashTable *individuals; /* Individual.id -> Individual */
   gboolean contacts_loaded;
 
-  /* FolksIndividual sorted by popularity (most popular first) */
+  /* reffed FolksIndividual sorted by popularity (most popular first) */
   GSequence *individuals_pop;
   /* The TOP_INDIVIDUALS_LEN first FolksIndividual (borrowed) from
    * individuals_pop */
   GList *top_individuals;
+  guint global_interaction_counter;
 } EmpathyIndividualManagerPriv;
 
 enum
@@ -127,13 +122,35 @@ individual_notify_is_favourite_cb (FolksIndividual *individual,
       is_favourite);
 }
 
+
+/* Contacts that have been interacted with within the last 30 days and have
+ * have an interaction count > INTERACTION_COUNT_COMPRESS_FACTOR have a
+ * popularity value of the count/INTERACTION_COUNT_COMPRESS_FACTOR */
 static guint
 compute_popularity (FolksIndividual *individual)
 {
-  /* TODO: we should have a better heuristic using the last time we interacted
-   * with the contact as well. */
-  return folks_interaction_details_get_im_interaction_count (
-      FOLKS_INTERACTION_DETAILS (individual));
+  FolksInteractionDetails *details = FOLKS_INTERACTION_DETAILS (individual);
+  GDateTime *last;
+  guint  current_timestamp, count;
+  float timediff;
+
+  last = folks_interaction_details_get_last_im_interaction_datetime (details);
+  if (last == NULL)
+    return 0;
+
+  /* Convert g_get_real_time () fro microseconds to seconds */
+  current_timestamp = g_get_real_time () / 1000000;
+  timediff = current_timestamp - g_date_time_to_unix (last);
+
+  if (timediff / DAY_IN_SECONDS > 30)
+    return 0;
+
+  count = folks_interaction_details_get_im_interaction_count (details);
+  count = count / INTERACTION_COUNT_COMPRESS_FACTOR;
+  if (count == 0)
+    return 0;
+
+  return count;
 }
 
 static void
@@ -220,8 +237,16 @@ individual_notify_im_interaction_count (FolksIndividual *individual,
 {
   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
 
+  /* We don't use g_sequence_sort_changed() because we'll first have to find
+   * the iter of @individual using g_sequence_lookup() but the lookup function
+   * won't work as it assumes that the sequence is sorted which is no longer
+   * the case at this point as @individual's popularity just changed. */
   g_sequence_sort (priv->individuals_pop, compare_individual_by_pop, NULL);
-  check_top_individuals (self);
+
+  /* Only check for top individuals after 10 interaction events happen */
+  if (priv->global_interaction_counter % 10 == 0)
+    check_top_individuals (self);
+  priv->global_interaction_counter++;
 }
 
 static void
@@ -255,8 +280,14 @@ remove_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
       compare_individual_by_pop, NULL);
   if (iter != NULL)
     {
+      /* priv->top_individuals borrows its reference from
+       * priv->individuals_pop so we take a reference on the individual while
+       * removing it to make sure it stays alive while calling
+       * check_top_individuals(). */
+      g_object_ref (individual);
       g_sequence_remove (iter);
       check_top_individuals (self);
+      g_object_unref (individual);
     }
 
   g_signal_handlers_disconnect_by_func (individual,
@@ -402,6 +433,7 @@ individual_manager_dispose (GObject *object)
   EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
 
   g_hash_table_unref (priv->individuals);
+
   tp_clear_object (&priv->aggregator);
 
   G_OBJECT_CLASS (empathy_individual_manager_parent_class)->dispose (object);
@@ -870,3 +902,50 @@ empathy_individual_manager_get_top_individuals (EmpathyIndividualManager *self)
 
   return priv->top_individuals;
 }
+
+static void
+unprepare_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  GError *error = NULL;
+  GSimpleAsyncResult *my_result = user_data;
+
+  folks_individual_aggregator_unprepare_finish (
+      FOLKS_INDIVIDUAL_AGGREGATOR (source), result, &error);
+
+  if (error != NULL)
+    {
+      DEBUG ("Failed to unprepare the aggregator: %s", error->message);
+      g_simple_async_result_take_error (my_result, error);
+    }
+
+  g_simple_async_result_complete (my_result);
+  g_object_unref (my_result);
+}
+
+void
+empathy_individual_manager_unprepare_async (
+    EmpathyIndividualManager *self,
+    GAsyncReadyCallback callback,
+    gpointer user_data)
+{
+  EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
+  GSimpleAsyncResult *result;
+
+  result = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
+      empathy_individual_manager_unprepare_async);
+
+  folks_individual_aggregator_unprepare (priv->aggregator, unprepare_cb,
+      result);
+}
+
+gboolean
+empathy_individual_manager_unprepare_finish (
+    EmpathyIndividualManager *self,
+    GAsyncResult *result,
+    GError **error)
+{
+  empathy_implement_finish_void (self,
+      empathy_individual_manager_unprepare_async)
+}