* Travis Reitter <travis.reitter@collabora.co.uk>
*/
-#include <config.h>
-
-#include <string.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 "config.h"
+#include "empathy-individual-manager.h"
-#include <extensions/extensions.h>
+#include <tp-account-widgets/tpaw-utils.h>
+#include <telepathy-glib/telepathy-glib-dbus.h>
-#include "empathy-individual-manager.h"
#include "empathy-utils.h"
#define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualManager)
+/* We just expose the $TOP_INDIVIDUALS_LEN more popular individuals as that's
+ * what the view actually care about. We just want to notify it when this list
+ * 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
FolksIndividualAggregator *aggregator;
GHashTable *individuals; /* Individual.id -> Individual */
gboolean contacts_loaded;
+
+ /* 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
+{
+ PROP_TOP_INDIVIDUALS = 1,
+ N_PROPS
+};
+
enum
{
FAVOURITES_CHANGED,
static EmpathyIndividualManager *manager_singleton = NULL;
+static void
+individual_manager_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ EmpathyIndividualManager *self = EMPATHY_INDIVIDUAL_MANAGER (object);
+ EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
+
+ switch (property_id)
+ {
+ case PROP_TOP_INDIVIDUALS:
+ g_value_set_pointer (value, priv->top_individuals);
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
static void
individual_group_changed_cb (FolksIndividual *individual,
gchar *group,
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)
+{
+ 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
+check_top_individuals (EmpathyIndividualManager *self)
+{
+ EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
+ GSequenceIter *iter;
+ GList *l, *new_list = NULL;
+ gboolean modified = FALSE;
+ guint i;
+
+ iter = g_sequence_get_begin_iter (priv->individuals_pop);
+ l = priv->top_individuals;
+
+ /* Check if the TOP_INDIVIDUALS_LEN first individuals in individuals_pop are
+ * still the same as the ones in top_individuals */
+ for (i = 0; i < TOP_INDIVIDUALS_LEN && !g_sequence_iter_is_end (iter); i++)
+ {
+ FolksIndividual *individual = g_sequence_get (iter);
+ guint pop;
+
+ /* Don't include individual having 0 as pop */
+ pop = compute_popularity (individual);
+ if (pop <= 0)
+ break;
+
+ if (!modified)
+ {
+ if (l == NULL)
+ {
+ /* Old list is shorter than the new one */
+ modified = TRUE;
+ }
+ else
+ {
+ modified = (individual != l->data);
+
+ l = g_list_next (l);
+ }
+ }
+
+ new_list = g_list_prepend (new_list, individual);
+
+ iter = g_sequence_iter_next (iter);
+ }
+
+ g_list_free (priv->top_individuals);
+ priv->top_individuals = g_list_reverse (new_list);
+
+ if (modified)
+ {
+ DEBUG ("Top individuals changed:");
+
+ for (l = priv->top_individuals; l != NULL; l = g_list_next (l))
+ {
+ FolksIndividual *individual = l->data;
+
+ DEBUG (" %s (%u)",
+ folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
+ compute_popularity (individual));
+ }
+
+ g_object_notify (G_OBJECT (self), "top-individuals");
+ }
+}
+
+static gint
+compare_individual_by_pop (gconstpointer a,
+ gconstpointer b,
+ gpointer user_data)
+{
+ guint pop_a, pop_b;
+
+ pop_a = compute_popularity (FOLKS_INDIVIDUAL (a));
+ pop_b = compute_popularity (FOLKS_INDIVIDUAL (b));
+
+ return pop_b - pop_a;
+}
+
+static void
+individual_notify_im_interaction_count (FolksIndividual *individual,
+ GParamSpec *pspec,
+ EmpathyIndividualManager *self)
+{
+ 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);
+
+ /* 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
add_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
{
g_strdup (folks_individual_get_id (individual)),
g_object_ref (individual));
+ g_sequence_insert_sorted (priv->individuals_pop, g_object_ref (individual),
+ compare_individual_by_pop, NULL);
+ check_top_individuals (self);
+
g_signal_connect (individual, "group-changed",
G_CALLBACK (individual_group_changed_cb), self);
g_signal_connect (individual, "notify::is-favourite",
G_CALLBACK (individual_notify_is_favourite_cb), self);
+ g_signal_connect (individual, "notify::im-interaction-count",
+ G_CALLBACK (individual_notify_im_interaction_count), self);
}
static void
remove_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
{
EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
+ GSequenceIter *iter;
+
+ iter = g_sequence_lookup (priv->individuals_pop, 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,
individual_group_changed_cb, self);
g_signal_handlers_disconnect_by_func (individual,
individual_notify_is_favourite_cb, self);
+ g_signal_handlers_disconnect_by_func (individual,
+ individual_notify_im_interaction_count, self);
g_hash_table_remove (priv->individuals, folks_individual_get_id (individual));
}
/* Make sure we handle each added individual only once. */
if (ind == NULL || g_list_find (added_set, ind) != NULL)
- continue;
+ goto while_next;
added_set = g_list_prepend (added_set, ind);
g_signal_connect (ind, "notify::personas",
added_filtered = g_list_prepend (added_filtered, ind);
}
+while_next:
g_clear_object (&ind);
}
g_clear_object (&iter);
g_hash_table_unref (priv->individuals);
- g_signal_handlers_disconnect_by_func (priv->aggregator,
- aggregator_individuals_changed_cb, object);
tp_clear_object (&priv->aggregator);
G_OBJECT_CLASS (empathy_individual_manager_parent_class)->dispose (object);
}
+static void
+individual_manager_finalize (GObject *object)
+{
+ EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
+
+ g_sequence_free (priv->individuals_pop);
+
+ G_OBJECT_CLASS (empathy_individual_manager_parent_class)->finalize (object);
+}
+
static GObject *
individual_manager_constructor (GType type,
guint n_props,
empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GParamSpec *spec;
+ object_class->get_property = individual_manager_get_property;
object_class->dispose = individual_manager_dispose;
+ object_class->finalize = individual_manager_finalize;
object_class->constructor = individual_manager_constructor;
+ spec = g_param_spec_pointer ("top-individuals", "top individuals",
+ "Top Individuals",
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+ g_object_class_install_property (object_class, PROP_TOP_INDIVIDUALS, spec);
+
signals[GROUPS_CHANGED] =
g_signal_new ("groups-changed",
G_TYPE_FROM_CLASS (klass),
priv->individuals = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_object_unref);
- priv->aggregator = folks_individual_aggregator_new ();
- g_signal_connect (priv->aggregator, "individuals-changed-detailed",
- G_CALLBACK (aggregator_individuals_changed_cb), self);
- g_signal_connect (priv->aggregator, "notify::is-quiescent",
- G_CALLBACK (aggregator_is_quiescent_notify_cb), self);
+ priv->individuals_pop = g_sequence_new (g_object_unref);
+
+ priv->aggregator = folks_individual_aggregator_dup ();
+ tp_g_signal_connect_object (priv->aggregator, "individuals-changed-detailed",
+ G_CALLBACK (aggregator_individuals_changed_cb), self, 0);
+ tp_g_signal_connect_object (priv->aggregator, "notify::is-quiescent",
+ G_CALLBACK (aggregator_is_quiescent_notify_cb), self, 0);
folks_individual_aggregator_prepare (priv->aggregator, NULL, NULL);
}
tp_contact = tpf_persona_get_contact (persona);
if (tp_contact == NULL)
- continue;
+ goto while_next;
conn = tp_contact_get_connection (tp_contact);
if (!tp_proxy_has_interface_by_id (conn,
TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
- continue;
+ goto while_next;
if (blocked)
tp_contact_block_async (tp_contact, abusive, NULL, NULL);
else
tp_contact_unblock_async (tp_contact, NULL, NULL);
}
+
+while_next:
g_clear_object (&persona);
}
g_clear_object (&iter);
(gpointer) group);
}
-static void
-link_personas_cb (FolksIndividualAggregator *aggregator,
- GAsyncResult *async_result,
- gpointer user_data)
+gboolean
+empathy_individual_manager_get_contacts_loaded (EmpathyIndividualManager *self)
{
- GError *error = NULL;
-
- folks_individual_aggregator_link_personas_finish (aggregator, async_result,
- &error);
+ EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
- if (error != NULL)
- {
- g_warning ("Failed to link personas: %s", error->message);
- g_clear_error (&error);
- }
+ return priv->contacts_loaded;
}
-void
-empathy_individual_manager_link_personas (EmpathyIndividualManager *self,
- GeeSet *personas)
+GList *
+empathy_individual_manager_get_top_individuals (EmpathyIndividualManager *self)
{
- EmpathyIndividualManagerPriv *priv;
-
- g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
- g_return_if_fail (personas != NULL);
-
- priv = GET_PRIV (self);
-
- DEBUG ("Linking %u personas",
- gee_collection_get_size (GEE_COLLECTION (personas)));
+ EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
- folks_individual_aggregator_link_personas (priv->aggregator, personas,
- (GAsyncReadyCallback) link_personas_cb, NULL);
+ return priv->top_individuals;
}
static void
-unlink_individual_cb (FolksIndividualAggregator *aggregator,
- GAsyncResult *async_result,
+unprepare_cb (GObject *source,
+ GAsyncResult *result,
gpointer user_data)
{
GError *error = NULL;
+ GSimpleAsyncResult *my_result = user_data;
- folks_individual_aggregator_unlink_individual_finish (aggregator,
- async_result, &error);
+ folks_individual_aggregator_unprepare_finish (
+ FOLKS_INDIVIDUAL_AGGREGATOR (source), result, &error);
if (error != NULL)
{
- g_warning ("Failed to unlink individual: %s", error->message);
- g_clear_error (&error);
+ DEBUG ("Failed to unprepare the aggregator: %s", error->message);
+ g_simple_async_result_take_error (my_result, error);
}
-}
-
-void
-empathy_individual_manager_unlink_individual (EmpathyIndividualManager *self,
- FolksIndividual *individual)
-{
- EmpathyIndividualManagerPriv *priv;
-
- g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
- g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
-
- priv = GET_PRIV (self);
-
- DEBUG ("Unlinking individual '%s'", folks_individual_get_id (individual));
- folks_individual_aggregator_unlink_individual (priv->aggregator, individual,
- (GAsyncReadyCallback) unlink_individual_cb, NULL);
+ g_simple_async_result_complete (my_result);
+ g_object_unref (my_result);
}
-gboolean
-empathy_individual_manager_get_contacts_loaded (EmpathyIndividualManager *self)
+void
+empathy_individual_manager_unprepare_async (
+ EmpathyIndividualManager *self,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
+ GSimpleAsyncResult *result;
- return priv->contacts_loaded;
-}
-
-static gboolean
-individual_has_contact (FolksIndividual *individual,
- TpContact *contact)
-{
- GeeSet *personas;
- GeeIterator *iter;
- gboolean found = FALSE;
-
- personas = folks_individual_get_personas (individual);
- iter = gee_iterable_iterator (GEE_ITERABLE (personas));
-
- while (!found && gee_iterator_next (iter))
- {
- TpfPersona *persona = gee_iterator_get (iter);
-
- if (TPF_IS_PERSONA (persona))
- {
- TpContact *c = tpf_persona_get_contact (persona);
-
- if (c == contact)
- found = TRUE;
- }
-
- g_clear_object (&persona);
- }
-
- g_clear_object (&iter);
+ result = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
+ empathy_individual_manager_unprepare_async);
- return found;
+ folks_individual_aggregator_unprepare (priv->aggregator, unprepare_cb,
+ result);
}
-/* Try finding a FolksIndividual containing @contact as one of his persona */
-FolksIndividual *
-empathy_individual_manager_lookup_by_contact (EmpathyIndividualManager *self,
- TpContact *contact)
+gboolean
+empathy_individual_manager_unprepare_finish (
+ EmpathyIndividualManager *self,
+ GAsyncResult *result,
+ GError **error)
{
- EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
- GHashTableIter iter;
- gpointer value;
-
- g_hash_table_iter_init (&iter, priv->individuals);
- while (g_hash_table_iter_next (&iter, NULL, &value))
- {
- FolksIndividual *individual = value;
-
- if (individual_has_contact (individual, contact))
- return individual;
- }
-
- return NULL;
+ tpaw_implement_finish_void (self,
+ empathy_individual_manager_unprepare_async)
}