[folks] telepathy: Fix build with --disable-zeitgeist



commit 5867cda8e899a873e65796b9aa0492bfc32b95f0
Author: Philip Withnall <philip tecnocode co uk>
Date:   Fri Jul 5 15:26:51 2013 +0100

    telepathy: Fix build with --disable-zeitgeist
    
    Vala doesn’t pass #if conditionals through to generated C code, so when
    building folks from a tarball, the --[enable|disable]-zeitgeist option was
    having no effect, and Zeitgeist was always being pulled into the build.
    
    Fix this by separating the Zeitgeist code out into a small noinst shim
    library and always generating C code for two versions of it: one with
    Zeitgeist enabled, and one with it disabled. The correct one’s C code is
    then compiled and linked into libfolks-telepathy.la when building from the
    tarball.
    
    This is a horrible, ugly fix, but the only better fix is to disable
    distribution of generated C code. This was attempted, but thwarted by
    automake’s Vala support.
    
    Closes: https://bugzilla.gnome.org/show_bug.cgi?id=701099

 backends/telepathy/lib/Makefile.am             |  142 ++++++++++++++-
 backends/telepathy/lib/tp-zeitgeist-dummy.vala |   50 +++++
 backends/telepathy/lib/tp-zeitgeist.vala       |  231 ++++++++++++++++++++++++
 backends/telepathy/lib/tpf-persona-store.vala  |  166 ++---------------
 backends/telepathy/lib/tpf-persona.vala        |   18 +--
 configure.ac                                   |    1 +
 6 files changed, 437 insertions(+), 171 deletions(-)
---
diff --git a/backends/telepathy/lib/Makefile.am b/backends/telepathy/lib/Makefile.am
index f4e93b4..4590902 100644
--- a/backends/telepathy/lib/Makefile.am
+++ b/backends/telepathy/lib/Makefile.am
@@ -16,11 +16,26 @@ VAPIGENFLAGS += \
        --vapidir=$(abs_top_srcdir)/folks \
        $(NULL)
 
-noinst_LTLIBRARIES = libtp-lowlevel.la
+noinst_LTLIBRARIES = \
+       libtp-lowlevel.la \
+       $(NULL)
 folks_telepathydir = $(libdir)
 folks_telepathy_LTLIBRARIES = libfolks-telepathy.la
 
+if ENABLE_ZEITGEIST
+noinst_LTLIBRARIES += libtp-zeitgeist.la
+else
+noinst_LTLIBRARIES += libtp-zeitgeist-dummy.la
+endif
+
+# Necessary because they're only conditionally listed in noinst_LTLIBRARIES.
+CLEANFILES = \
+       libtp-zeitgeist.la \
+       libtp-zeitgeist-dummy.la \
+       $(NULL)
+
 dist_noinst_DATA =
+EXTRA_DIST =
 
 ##################################################################
 # Telepathy Lowlevel library
@@ -71,6 +86,104 @@ endif
 dist_noinst_DATA += tp-lowlevel.vapi
 
 ##################################################################
+# Zeitgeist shim libraries
+#
+# Two libraries with identical interfaces, one (libtp-zeitgeist.la) which links
+# to Zeitgeist, and one (libtp-zeitgeist-dummy.la) which does not (and uses
+# no-ops for all methods). C code for both is always generated, but the former
+# is only compiled if Zeitgeist is available. One is always linked into
+# libfolks-telepathy.la, depending on whether Zeitgeist support is enabled.
+#
+# These fun and games are necessary because Vala won't pass #if conditionals
+# through to generated C code (with good reason).
+#
+# See https://bugzilla.gnome.org/show_bug.cgi?id=701099.
+##################################################################
+
+libtp_zeitgeist_la_SOURCES = tp-zeitgeist.vala
+libtp_zeitgeist_dummy_la_SOURCES = tp-zeitgeist-dummy.vala
+
+libtp_zeitgeist_la_CFLAGS = \
+       $(AM_CFLAGS) \
+       $(CODE_COVERAGE_CFLAGS) \
+       $(ERROR_CFLAGS) \
+       $(GLIB_CFLAGS) \
+       $(GEE_CFLAGS) \
+       $(TP_GLIB_CFLAGS) \
+       $(ZEITGEIST_CFLAGS) \
+       $(NULL)
+libtp_zeitgeist_dummy_la_CFLAGS = \
+       $(AM_CFLAGS) \
+       $(CODE_COVERAGE_CFLAGS) \
+       $(ERROR_CFLAGS) \
+       $(GLIB_CFLAGS) \
+       $(GEE_CFLAGS) \
+       $(NULL)
+
+libtp_zeitgeist_la_CPPFLAGS = \
+       $(AM_CPPFLAGS) \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/folks \
+       -include $(CONFIG_HEADER) \
+       -DABS_TOP_SRCDIR=\"${abs_top_srcdir}\" \
+       -DABS_TOP_BUILDDIR=\"${abs_top_builddir}\" \
+       -DPKGLIBEXECDIR=\"${pkglibexecdir}\" \
+       -DPACKAGE_DATADIR=\"${pkgdatadir}\" \
+       $(NULL)
+libtp_zeitgeist_dummy_la_CPPFLAGS = $(libtp_zeitgeist_la_CPPFLAGS)
+
+libtp_zeitgeist_la_LIBADD = \
+       $(AM_LIBADD) \
+       $(top_builddir)/folks/libfolks.la \
+       $(GLIB_LIBS) \
+       $(GEE_LIBS) \
+       $(TP_GLIB_LIBS) \
+       $(ZEITGEIST_LIBS) \
+       $(NULL)
+libtp_zeitgeist_dummy_la_LIBADD = \
+       $(AM_LIBADD) \
+       $(top_builddir)/folks/libfolks.la \
+       $(GLIB_LIBS) \
+       $(GEE_LIBS) \
+       $(NULL)
+
+libtp_zeitgeist_la_VALAFLAGS = \
+       $(ERROR_VALAFLAGS) \
+       $(AM_VALAFLAGS) \
+       --vapidir=$(abs_top_srcdir)/folks \
+       --vapidir=$(abs_top_builddir)/folks \
+       --pkg gobject-2.0 \
+       --pkg gio-2.0 \
+       --pkg gee-0.8 \
+       --pkg zeitgeist-2.0 \
+       --pkg telepathy-glib \
+       --pkg folks \
+       --pkg build-conf \
+       --library tp-zeitgeist \
+       --vapi tp-zeitgeist.vapi \
+       -H tp-zeitgeist.h \
+       -g \
+       $(NULL)
+libtp_zeitgeist_dummy_la_VALAFLAGS = \
+       $(ERROR_VALAFLAGS) \
+       $(AM_VALAFLAGS) \
+       --vapidir=$(abs_top_srcdir)/folks \
+       --vapidir=$(abs_top_builddir)/folks \
+       --pkg gobject-2.0 \
+       --pkg folks \
+       --pkg build-conf \
+       --library tp-zeitgeist-dummy \
+       --vapi tp-zeitgeist-dummy.vapi \
+       -H tp-zeitgeist-dummy.h \
+       -g \
+       $(NULL)
+
+dist_noinst_DATA += \
+       tp-zeitgeist.vapi \
+       tp-zeitgeist-dummy.vapi \
+       $(NULL)
+
+##################################################################
 # Support library
 ##################################################################
 
@@ -78,7 +191,7 @@ pkgconfig_in = folks-telepathy.pc.in
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = $(pkgconfig_in:.in=)
 
-$(srcdir)/libfolks_telepathy_la_vala.stamp: tp-lowlevel.vapi
+$(srcdir)/libfolks_telepathy_la_vala.stamp: tp-lowlevel.vapi tp-zeitgeist.vapi tp-zeitgeist-dummy.vapi
 
 libfolks_telepathy_la_SOURCES = \
        tpf-namespace.vala \
@@ -92,7 +205,6 @@ libfolks_telepathy_la_SOURCES = \
 libfolks_telepathy_la_VALAFLAGS = \
        $(AM_VALAFLAGS) \
        $(ERROR_VALAFLAGS) \
-       $(ZEITGEIST_VALAFLAGS) \
        --library=folks-telepathy \
        --vapidir=$(abs_srcdir) \
        --vapidir=$(abs_builddir) \
@@ -106,13 +218,18 @@ libfolks_telepathy_la_VALAFLAGS = \
        --pkg gio-2.0 \
        --pkg gee-0.8 \
        --pkg telepathy-glib \
-       $(ZEITGEIST_PKG) \
        --includedir folks \
        --library folks-telepathy \
        --vapi folks-telepathy.vapi \
        -H folks-telepathy.h \
        $(NULL)
 
+if ENABLE_ZEITGEIST
+libfolks_telepathy_la_VALAFLAGS += --pkg tp-zeitgeist
+else
+libfolks_telepathy_la_VALAFLAGS += --pkg tp-zeitgeist-dummy
+endif
+
 libfolks_telepathy_la_CFLAGS = \
        $(AM_CFLAGS) \
        $(CODE_COVERAGE_CFLAGS) \
@@ -120,7 +237,6 @@ libfolks_telepathy_la_CFLAGS = \
        $(GLIB_CFLAGS) \
        $(GEE_CFLAGS) \
        $(TP_GLIB_CFLAGS) \
-       $(ZEITGEIST_CFLAGS) \
        $(NULL)
 
 libfolks_telepathy_la_LIBADD = \
@@ -129,10 +245,15 @@ libfolks_telepathy_la_LIBADD = \
        $(GLIB_LIBS) \
        $(GEE_LIBS) \
        $(TP_GLIB_LIBS) \
-       $(ZEITGEIST_LIBS) \
        $(top_builddir)/folks/libfolks.la \
        $(NULL)
 
+if ENABLE_ZEITGEIST
+libfolks_telepathy_la_LIBADD += libtp-zeitgeist.la
+else
+libfolks_telepathy_la_LIBADD += libtp-zeitgeist-dummy.la
+endif
+
 # The quoting here is unnecessary but harmless, and has the useful side-effect
 # that vim quickfix mode (:make) doesn't interpret the libtool --mode=link
 # command as an error message in a bizarrely named file
@@ -162,9 +283,8 @@ if NOT_RELEASE
 DISTCLEANFILES = $(dist_noinst_DATA)
 endif
 
-CLEANFILES =
 MAINTAINERCLEANFILES =
-EXTRA_DIST = \
+EXTRA_DIST += \
        $(libtp_lowlevel_la_SOURCES) \
        $(pkgconfig_in) \
        $(MAINTAINERCLEANFILES) \
@@ -241,6 +361,12 @@ FolksTelepathy_ API_VERSION_MAJOR@_ API_VERSION_MINOR@_gir_NAMESPACE = FolksTele
 FolksTelepathy_ API_VERSION_MAJOR@_ API_VERSION_MINOR@_gir_SCANNERFLAGS = --identifier-prefix=Tpf
 INTROSPECTION_GIRS += $(TPF_GIR)
 
+if ENABLE_ZEITGEIST
+FolksTelepathy_ API_VERSION_MAJOR@_ API_VERSION_MINOR@_gir_LIBS += libtp-zeitgeist.la
+else
+FolksTelepathy_ API_VERSION_MAJOR@_ API_VERSION_MINOR@_gir_LIBS += libtp-zeitgeist-dummy.la
+endif
+
 libfolks_telepathy_la_VALAFLAGS += \
         --gir=$(TPF_GIR) \
         --girdir=$(abs_builddir) \
diff --git a/backends/telepathy/lib/tp-zeitgeist-dummy.vala b/backends/telepathy/lib/tp-zeitgeist-dummy.vala
new file mode 100644
index 0000000..7cfa1a6
--- /dev/null
+++ b/backends/telepathy/lib/tp-zeitgeist-dummy.vala
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2013 Philip Withnall
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Authors:
+ *     Philip Withnall <philip tecnocode co uk>
+ */
+
+using Folks;
+using GLib;
+
+/**
+ * Dummy interface for the Zeitgeist code for libfolks-telepathy.la. This must
+ * implement exactly the same interface as tp-zeitgeist.vala, but without
+ * linking to Zeitgeist.
+ *
+ * See the note in Makefile.am, and
+ * https://bugzilla.gnome.org/show_bug.cgi?id=701099.
+ */
+public class FolksTpZeitgeist.Controller : Object
+{
+  [CCode (has_target = false)]
+  public delegate void IncreasePersonaCounter (Persona p,
+      DateTime converted_datetime);
+
+  public Controller (PersonaStore store, string protocol,
+      IncreasePersonaCounter im_interaction_cb,
+      IncreasePersonaCounter last_call_interaction_cb)
+    {
+      /* Dummy. */
+    }
+
+  public async void populate_counters ()
+    {
+      /* Dummy. */
+    }
+}
diff --git a/backends/telepathy/lib/tp-zeitgeist.vala b/backends/telepathy/lib/tp-zeitgeist.vala
new file mode 100644
index 0000000..2339dfa
--- /dev/null
+++ b/backends/telepathy/lib/tp-zeitgeist.vala
@@ -0,0 +1,231 @@
+/*
+ * Copyright (C) 2013 Philip Withnall
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Authors:
+ *     Philip Withnall <philip tecnocode co uk>
+ */
+
+using Folks;
+using GLib;
+using Gee;
+using TelepathyGLib;
+using Zeitgeist;
+
+/**
+ * Zeitgeist code for libfolks-telepathy.la. This is separated out from
+ * tpf-persona-store.vala so that it can be conditionally compiled out.
+ *
+ * See the note in Makefile.am, and
+ * https://bugzilla.gnome.org/show_bug.cgi?id=701099.
+ */
+public class FolksTpZeitgeist.Controller : Object
+{
+  private Zeitgeist.Log? _log = null;
+  private Zeitgeist.Monitor? _monitor = null;
+  private string _protocol;
+
+  /* This object is owned by the PersonaStore, so we don't want a cyclic
+   * reference. */
+  private unowned PersonaStore _store;
+
+  [CCode (has_target = false)]
+  public delegate void IncreasePersonaCounter (Persona p,
+      DateTime converted_datetime);
+
+  private IncreasePersonaCounter _im_interaction_cb;
+  private IncreasePersonaCounter _last_call_interaction_cb;
+
+  public Controller (PersonaStore store, string protocol,
+      IncreasePersonaCounter im_interaction_cb,
+      IncreasePersonaCounter last_call_interaction_cb)
+    {
+      this._store = store;
+      this._protocol = protocol;
+      this._im_interaction_cb = im_interaction_cb;
+      this._last_call_interaction_cb = last_call_interaction_cb;
+    }
+
+  ~Controller ()
+    {
+      if (this._monitor != null)
+        {
+          this._log.remove_monitor (this._monitor);
+          this._monitor = null;
+        }
+    }
+
+  public async void populate_counters ()
+    {
+      if (this._log == null)
+        {
+          this._log = new Zeitgeist.Log ();
+        }
+
+      /* Get all events for this account from Zeitgeist and increase the
+       * the counters of the personas */
+      try
+        {
+          TimeVal tm = TimeVal ();
+          int64 end_timestamp = tm.tv_sec;
+          /* We want events from the last 30 days only, A day has 86400 seconds.
+           * start_timestamp = end_timestamp - 30 days in seconds*/
+          int64 start_timestamp = end_timestamp - (86400 * 30);
+          GLib.GenericArray<Zeitgeist.Event> events =
+              this._get_zeitgeist_event_templates ();
+          var results = yield this._log.find_events (
+              new TimeRange (start_timestamp * 1000, end_timestamp * 1000),
+              events, StorageState.ANY, 0, ResultType.MOST_RECENT_EVENTS,
+              null);
+
+          foreach (var e in results)
+            {
+              var interaction_type = e.get_subject (0).interpretation;
+              for (var i = 1; i < e.num_subjects (); i++)
+                {
+                  var id =
+                      this._get_iid_from_event_metadata (e.get_subject (i).uri);
+                  if (id == null || interaction_type == null)
+                      continue;
+
+                  var persona = this._store.personas.get (id);
+                  if (persona == null)
+                      continue;
+
+                  persona.freeze_notify ();
+                  this._increase_persona_counter (persona, interaction_type, e);
+                }
+            }
+
+          /* Go back through and thaw notifications. */
+          foreach (var e in results)
+            {
+              var interaction_type = e.get_subject (0).interpretation;
+              for (var i = 1; i < e.num_subjects (); i++)
+                {
+                  var id =
+                      this._get_iid_from_event_metadata (e.get_subject (i).uri);
+                  if (id == null || interaction_type == null)
+                      continue;
+
+                  var persona = this._store.personas.get (id);
+                  if (persona == null)
+                      continue;
+
+                  persona.thaw_notify ();
+                }
+            }
+        }
+      catch
+        {
+          debug ("Failed to fetch events from Zeitgeist");
+        }
+
+      /* Prepare a monitor and install for this account to populate persona
+       * counters upon interaction changes.*/
+      if (this._monitor == null)
+        {
+          GLib.GenericArray<Zeitgeist.Event> monitor_events =
+              this._get_zeitgeist_event_templates ();
+          this._monitor = new Zeitgeist.Monitor (
+              new Zeitgeist.TimeRange.from_now (), monitor_events);
+          this._monitor.events_inserted.connect (this._handle_new_interaction);
+          try
+            {
+              this._log.install_monitor (this._monitor);
+            }
+          catch
+            {
+              warning ("Failed to install monitor for Zeitgeist");
+              this._monitor = null;
+            }
+        }
+    }
+
+  private string? _get_iid_from_event_metadata (string? uri)
+    {
+      /* Format a proper id represting a persona in the store.
+       * Zeitgeist uses x-telepathy-identifier as a prefix for telepathy, which
+       * is stored as the uri of a subject of an event. */
+      if (uri == null)
+        {
+          return null;
+        }
+      var new_uri = uri.replace ("x-telepathy-identifier:", "");
+      return this._protocol + ":" + new_uri;
+    }
+
+  private void _increase_persona_counter (Persona persona,
+      string interaction_type, Event event)
+    {
+      /* Increase the appropriate interaction counter, to signify that an
+       * interaction was successfully counted. */
+      var timestamp = (uint) (event.timestamp / 1000);
+      var converted_datetime = new DateTime.from_unix_utc (timestamp);
+      var interpretation = event.interpretation;
+
+      /* Only count send/receive for IM interactions */
+      if (interaction_type == Zeitgeist.NMO.IMMESSAGE &&
+          (interpretation == Zeitgeist.ZG.SEND_EVENT ||
+           interpretation == Zeitgeist.ZG.RECEIVE_EVENT))
+        {
+          this._im_interaction_cb (persona, converted_datetime);
+        }
+      /* Only count successful call for call interactions */
+      else if (interaction_type == Zeitgeist.NFO.AUDIO &&
+               interpretation == Zeitgeist.ZG.LEAVE_EVENT)
+        {
+          this._last_call_interaction_cb (persona, converted_datetime);
+        }
+    }
+
+  private void _handle_new_interaction (TimeRange timerange, ResultSet events)
+    {
+      foreach (var e in events)
+        {
+          for (var i = 1; i < e.num_subjects (); i++)
+            {
+              var id =
+                  this._get_iid_from_event_metadata (e.get_subject (i).uri);
+              var interaction_type = e.get_subject (0).interpretation;
+              if (id == null || interaction_type == null)
+                  continue;
+
+              var persona = this._store.personas.get (id);
+              if (persona == null)
+                  continue;
+
+              this._increase_persona_counter (persona, interaction_type, e);
+            }
+        }
+    }
+
+  private GLib.GenericArray<Zeitgeist.Event> _get_zeitgeist_event_templates ()
+    {
+      /* To fetch events from Zeitgeist about the interaction with contacts we
+       * create templates reflecting how the telepathy-logger stores events in
+       * Zeitgeist */
+      var origin =
+          this._store.id.replace (TelepathyGLib.ACCOUNT_OBJECT_PATH_BASE,
+                                  "x-telepathy-account-path:");
+      Event ev1 = new Event.full ("", "",
+          "dbus://org.freedesktop.Telepathy.Logger.service");
+      ev1.origin = origin;
+      var templates = new GLib.GenericArray<Zeitgeist.Event> ();
+      templates.add (ev1);
+      return templates;
+    }
+}
diff --git a/backends/telepathy/lib/tpf-persona-store.vala b/backends/telepathy/lib/tpf-persona-store.vala
index f08ad89..1e04efc 100644
--- a/backends/telepathy/lib/tpf-persona-store.vala
+++ b/backends/telepathy/lib/tpf-persona-store.vala
@@ -25,9 +25,6 @@ using GLib;
 using Gee;
 using TelepathyGLib;
 using Folks;
-#if HAVE_ZEITGEIST
-using Zeitgeist;
-#endif
 
 extern const string G_LOG_DOMAIN;
 extern const string BACKEND_NAME;
@@ -101,10 +98,7 @@ public class Tpf.PersonaStore : Folks.PersonaStore
 
   private Account _account;
 
-#if HAVE_ZEITGEIST
-  private Zeitgeist.Log? _log = null;
-  private Zeitgeist.Monitor? _monitor = null;
-#endif
+  private FolksTpZeitgeist.Controller _zg_controller;
 
   /**
    * The Telepathy account this store is based upon.
@@ -297,13 +291,8 @@ public class Tpf.PersonaStore : Folks.PersonaStore
               this._account_manager_invalidated_cb);
           this._account_manager = null;
         }
-#if HAVE_ZEITGEIST
-      if (this._monitor != null)
-        {
-          this._log.remove_monitor (this._monitor);
-          this._monitor = null;
-        }
-#endif
+
+      this._zg_controller = null;
     }
 
   private string _format_maybe_bool (MaybeBool input)
@@ -1196,9 +1185,7 @@ public class Tpf.PersonaStore : Folks.PersonaStore
           new GLib.GenericArray<TelepathyGLib.Contact> ());
 
       this._got_initial_members = true;
-#if HAVE_ZEITGEIST
       this._populate_counters.begin ();
-#endif
       this._notify_if_is_quiescent ();
     }
 
@@ -1714,142 +1701,23 @@ public class Tpf.PersonaStore : Folks.PersonaStore
       return store;
     }
 
-#if HAVE_ZEITGEIST
-  private string? _get_iid_from_event_metadata (string? uri)
-    {
-      /* Format a proper id represting a persona in the store.
-       * Zeitgeist uses x-telepathy-identifier as a prefix for telepathy, which
-       * is stored as the uri of a subject of an event. */
-      if (uri == null)
-        {
-          return null;
-        }
-      var new_uri = uri.replace ("x-telepathy-identifier:", "");
-      return this.account.protocol + ":" + new_uri;
-    }
-
-  private void _increase_persona_counter (string? id, string? interaction_type, Event event)
-    {
-      /* Check if the persona id and interaction is valid. If so increase the
-       * appropriate interacton counter, to signify that an
-       * interaction was successfully counted. */
-      if (id != null && this._personas.has_key (id) && interaction_type != null)
-        {
-          var persona = this._personas.get (id);
-          var timestamp = (uint) (event.timestamp / 1000);
-          var converted_datetime = new DateTime.from_unix_utc (timestamp);
-          var interpretation = event.interpretation;
-
-          /* Only count send/receive for IM interactions */
-          if (interaction_type == Zeitgeist.NMO.IMMESSAGE &&
-              (interpretation == Zeitgeist.ZG.SEND_EVENT ||
-               interpretation == Zeitgeist.ZG.RECEIVE_EVENT))
-            {
-              persona._increase_im_interaction_counter (id, converted_datetime);
-            }
-          /* Only count successful call for call interactions */
-          else if (interaction_type == Zeitgeist.NFO.AUDIO &&
-                    interpretation == Zeitgeist.ZG.LEAVE_EVENT)
-            {
-              persona._increase_last_call_interaction_counter (id,
-                  converted_datetime);
-            }
-        }
-    }
-
-  private void _handle_new_interaction (TimeRange timerange, ResultSet events)
-    {
-      foreach (var e in events)
-        {
-          for (var i = 1; i < e.num_subjects (); i++)
-            {
-              var id = this._get_iid_from_event_metadata (e.get_subject (i).uri);
-              var interaction_type = e.get_subject (0).interpretation;
-              this._increase_persona_counter (id, interaction_type, e);
-            }
-        }
-    }
-
-  private GLib.GenericArray<Zeitgeist.Event> _get_zeitgeist_event_templates ()
-    {
-      /* To fetch events from Zeitgeist about the interaction with contacts we
-       * create templates reflecting how the telepathy-logger stores events in
-       * Zeitgeist */
-      var origin = this.id.replace (TelepathyGLib.ACCOUNT_OBJECT_PATH_BASE,
-                                    "x-telepathy-account-path:");
-      Event ev1 = new Event.full ("", "", "dbus://org.freedesktop.Telepathy.Logger.service");
-      ev1.origin = origin;
-      var templates = new GLib.GenericArray<Zeitgeist.Event> ();
-      templates.add (ev1);
-      return templates;
-    }
-
   /* This method is safe to call multiple times concurrently. */
   private async void _populate_counters ()
     {
-      if (this._log == null)
-        {
-          this._log = new Zeitgeist.Log ();
-        }
-
-      /* Get all events for this account from Zeitgeist and increase the
-       * the counters of the personas */
-      try
-        {
-          TimeVal tm = TimeVal ();
-          int64 end_timestamp = tm.tv_sec;
-          /* We want events from the last 30 days only, A day has 86400 seconds.
-           * start_timestamp = end_timestamp - 30 days in seconds*/
-          int64 start_timestamp = end_timestamp - (86400 * 30);
-          GLib.GenericArray<Zeitgeist.Event> events = this._get_zeitgeist_event_templates ();
-          var results = yield this._log.find_events (
-              new TimeRange (start_timestamp * 1000, end_timestamp * 1000),
-              events, StorageState.ANY, 0, ResultType.MOST_RECENT_EVENTS,
-              null);
-          foreach (var persona in this._personas.values)
-            {
-              persona.freeze_notify ();
-              persona._reset_interaction ();
-            }
-          foreach (var e in results)
-            {
-              var interaction_type = e.get_subject (0).interpretation;
-              for (var i = 1; i < e.num_subjects (); i++)
-                {
-                  var id = this._get_iid_from_event_metadata (e.get_subject (i).uri);
-                  this._increase_persona_counter (id, interaction_type, e);
-                }
-            }
-          foreach (var persona in this.personas.values)
-            {
-              persona.thaw_notify ();
-            }
-        }
-      catch
-        {
-          debug ("Failed to fetch events from Zeitgeist");
-        }
-
-      /* Prepare a monitor and install for this account to populate persona
-       * counters upon interaction changes.*/
-      if (this._monitor == null)
-        {
-          GLib.GenericArray<Zeitgeist.Event> monitor_events = this._get_zeitgeist_event_templates ();
-          this._monitor = new Zeitgeist.Monitor (new Zeitgeist.TimeRange.from_now (),
-              monitor_events);
-          this._monitor.events_inserted.connect (this._handle_new_interaction);
-          try
-            {
-              this._log.install_monitor (this._monitor);
-            }
-          catch
-            {
-              warning ("Failed to install monitor for Zeitgeist");
-              this._monitor = null;
-            }
-        }
-
+      this._zg_controller = new FolksTpZeitgeist.Controller (this,
+          this.account.protocol, (p, dt) =>
+        {
+          var persona = p as Tpf.Persona;
+          assert (persona != null);
+          persona._increase_im_interaction_counter (dt);
+        },
+          (p, dt) =>
+        {
+          var persona = p as Tpf.Persona;
+          assert (persona != null);
+          persona._increase_last_call_interaction_counter (dt);
+        });
+      yield this._zg_controller.populate_counters ();
       this._notify_if_is_quiescent ();
     }
-#endif
 }
diff --git a/backends/telepathy/lib/tpf-persona.vala b/backends/telepathy/lib/tpf-persona.vala
index 249aa89..299a86c 100644
--- a/backends/telepathy/lib/tpf-persona.vala
+++ b/backends/telepathy/lib/tpf-persona.vala
@@ -1398,8 +1398,7 @@ public class Tpf.Persona : Folks.Persona,
       return store._ensure_persona_for_contact (contact);
     }
 
-  internal void _increase_im_interaction_counter (string id,
-      DateTime converted_datetime)
+  internal void _increase_im_interaction_counter (DateTime converted_datetime)
     {
       this._im_interaction_count++;
       this.notify_property ("im-interaction-count");
@@ -1411,12 +1410,11 @@ public class Tpf.Persona : Folks.Persona,
         }
       debug ("Persona %s IM interaction details changed:\n" +
           " - count: %u \n - timestamp: %lld",
-          id, this._im_interaction_count,
+          this.iid, this._im_interaction_count,
           this._last_im_interaction_datetime.format ("%H %M %S - %d %m %y"));
     }
 
-  internal void _increase_last_call_interaction_counter (string id,
-      DateTime converted_datetime)
+  internal void _increase_last_call_interaction_counter (DateTime converted_datetime)
     {
       this._call_interaction_count++;
       this.notify_property ("call-interaction-count");
@@ -1428,15 +1426,7 @@ public class Tpf.Persona : Folks.Persona,
         }
       debug ("Persona %s Call interaction details changed:\n" +
           " - count: %u \n - timestamp: %lld",
-          id, this._call_interaction_count,
+          this.iid, this._call_interaction_count,
           this._last_call_interaction_datetime.format ("%H %M %S - %d %m %y"));
     }
-
-  internal void _reset_interaction ()
-    {
-      this._call_interaction_count = 0;
-      this._im_interaction_count = 0;
-      this._last_call_interaction_datetime = null;
-      this._last_im_interaction_datetime = null;
-    }
 }
diff --git a/configure.ac b/configure.ac
index c77c047..d017822 100644
--- a/configure.ac
+++ b/configure.ac
@@ -240,6 +240,7 @@ AS_IF([test "x$have_zeitgeist" == "xyes"], [
    ZEITGEIST_PKG="--pkg zeitgeist-2.0"
    AC_SUBST([ZEITGEIST_PKG])
 ])
+AM_CONDITIONAL([ENABLE_ZEITGEIST], [test "x$have_zeitgeist" = "xyes"])
 
 # Ignore post 0.20 deprecations
 TP_GLIB_CFLAGS="$TP_GLIB_CFLAGS -DTP_VERSION_MIN_REQUIRED=TP_VERSION_0_20"



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