[gnome-documents] all: refactor GdPdfLoader to load GDataEntry objects



commit a31cbcf494a056271bc64bedaaf9dc0a76af99fb
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Aug 29 01:51:48 2011 -0400

    all: refactor GdPdfLoader to load GDataEntry objects
    
    And construct the GDataEntry client-side from the application. This will
    allow using the entry to update metadata, but it also removes redundant
    parsing of the GOA accounts between the loader and the JS code.

 src/Makefile-lib.am     |    1 +
 src/documents.js        |   60 ++++++++++
 src/lib/gd-pdf-loader.c |  276 +++++++++--------------------------------------
 src/lib/gd-pdf-loader.h |   42 ++------
 src/mainWindow.js       |   21 +---
 src/sources.js          |   59 +++++++---
 src/trackerUtils.js     |   37 -------
 7 files changed, 171 insertions(+), 325 deletions(-)
---
diff --git a/src/Makefile-lib.am b/src/Makefile-lib.am
index 23e1f2e..93533d6 100644
--- a/src/Makefile-lib.am
+++ b/src/Makefile-lib.am
@@ -44,6 +44,7 @@ Gd_1_0_gir_LIBS = libgdprivate-1.0.la
 Gd_1_0_gir_CFLAGS = $(AM_CPPFLAGS) $(gdprivate_cflags)
 Gd_1_0_gir_SCANNERFLAGS = --warn-all --symbol-prefix=gd --identifier-prefix=Gd
 Gd_1_0_gir_INCLUDES = \
+   GData-0.0 \
    Goa-1.0 \
    Gtk-3.0 \
    EvinceDocument-3.0 \
diff --git a/src/documents.js b/src/documents.js
index d0c68a3..a268309 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -22,6 +22,8 @@
 const GdkPixbuf = imports.gi.GdkPixbuf;
 const Gio = imports.gi.Gio;
 const Gd = imports.gi.Gd;
+const GData = imports.gi.GData;
+const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const _ = imports.gettext.gettext;
 
@@ -249,6 +251,22 @@ LocalDocument.prototype = {
 
             this.checkEmblemsAndUpdateInfo();
         }
+    },
+
+    loadPreview: function(cancellable, callback) {
+        Gd.pdf_loader_load_uri_async(this.uri, cancellable, Lang.bind(this,
+            function(source, res) {
+                let document = null;
+
+                try {
+                    document = Gd.pdf_loader_load_uri_finish(res);
+                } catch (e) {
+                    log('Unable to load the uri ' + this.uri + ' for preview: ' + e.toString());
+                }
+
+                callback(document);
+            }));
+
     }
 };
 
@@ -265,6 +283,48 @@ GoogleDocument.prototype = {
         // overridden
         this.identifier = cursor.get_string(Query.QueryColumns.IDENTIFIER)[0];
         this.defaultAppName = _("Google Docs");
+    },
+
+    loadPreview: function(cancellable, callback) {
+        let source = Global.sourceManager.getSourceByUrn(this.resourceUrn);
+
+        let authorizer = new Gd.GDataGoaAuthorizer({ goa_object: source.object });
+        let service = new GData.DocumentsService({ authorizer: authorizer });
+
+        // HACK: GJS doesn't support introspecting GTypes, so we need to use
+        // GObject.type_from_name(); but for that to work, we need at least one
+        // instance of the GType in question to have ever been created. Ensure that
+        let temp = new GData.DocumentsText();
+        service.query_single_entry_async
+            (service.get_primary_authorization_domain(),
+             this.identifier, null,
+             GObject.type_from_name('GDataDocumentsText'),
+             cancellable, Lang.bind(this,
+                 function(object, res) {
+                     let entry = null;
+                     try {
+                         entry = object.query_single_entry_finish(res);
+                     } catch (e) {
+                         log('Unable to query the GData entry: ' + e.toString());
+
+                         callback(null);
+                         return;
+                     }
+
+                     Gd.pdf_loader_load_entry_async
+                         (entry, service, cancellable, Lang.bind(this,
+                             function(source, res) {
+                                 let document = null;
+
+                                 try {
+                                     document = Gd.pdf_loader_load_entry_finish(res);
+                                 } catch (e) {
+                                     log('Unable to load the GData entry: ' + e.toString());
+                                 }
+
+                                 callback(document);
+                             }));
+                 }));
     }
 };
 
diff --git a/src/lib/gd-pdf-loader.c b/src/lib/gd-pdf-loader.c
index 1998519..3481c85 100644
--- a/src/lib/gd-pdf-loader.c
+++ b/src/lib/gd-pdf-loader.c
@@ -30,16 +30,6 @@
 #include <evince-view.h>
 #include <glib/gstdio.h>
 
-/* TODO:
- * - investigate the GDataDocumentsType bug
- */
-
-G_DEFINE_TYPE (GdPdfLoader, gd_pdf_loader, G_TYPE_OBJECT);
-
-enum {
-  PROP_SOURCE_ID = 1,
-};
-
 typedef struct {
   GSimpleAsyncResult *result;
   GCancellable *cancellable;
@@ -59,23 +49,8 @@ typedef struct {
   gboolean unlink_cache;
 } PdfLoadJob;
 
-struct _GdPdfLoaderPrivate {
-  gchar *source_id;
-};
-
 /* --------------------------- utils -------------------------------- */
 
-#define GOA_DOCS_TRACKER_PREFIX "goa:documents:"
-
-static gchar *
-strip_tracker_prefix (const gchar *source_id)
-{
-  if (g_str_has_prefix (source_id, GOA_DOCS_TRACKER_PREFIX))
-    return g_strdup (source_id + strlen (GOA_DOCS_TRACKER_PREFIX));
-
-  return NULL;
-}
-
 static gchar **
 query_supported_document_types (void)
 {
@@ -157,6 +132,7 @@ pdf_load_job_free (PdfLoadJob *job)
 static PdfLoadJob *
 pdf_load_job_new (GSimpleAsyncResult *result,
                   const gchar *uri,
+                  GDataEntry *entry,
                   GCancellable *cancellable)
 {
   PdfLoadJob *retval;
@@ -164,10 +140,14 @@ pdf_load_job_new (GSimpleAsyncResult *result,
   retval = g_slice_new0 (PdfLoadJob);
   retval->result = g_object_ref (result);
   retval->cancellable = g_object_ref (cancellable);
-  retval->uri = g_strdup (uri);
   retval->unoconv_pid = -1;
   retval->unlink_cache = FALSE;
 
+  if (uri != NULL)
+    retval->uri = g_strdup (uri);
+  if (entry != NULL)
+    retval->gdata_entry = g_object_ref (entry);
+
   return retval;
 }
 
@@ -389,28 +369,16 @@ gdata_cache_query_info_ready_cb (GObject *source,
 }
 
 static void
-single_entry_ready_cb (GObject *source,
-                       GAsyncResult *res,
-                       gpointer user_data)
+pdf_load_job_from_google_documents (PdfLoadJob *job)
 {
-  GDataEntry *entry;
-  GError *error = NULL;
   gchar *tmp_name;
   gchar *tmp_path, *pdf_path;
   GFile *pdf_file;
-  PdfLoadJob *job = user_data;
-
-  job->gdata_entry = entry = 
-    gdata_service_query_single_entry_finish (GDATA_SERVICE (source), res, &error);
-
-  if (error != NULL) {
-    pdf_load_job_complete_error (job, error);
-    return;
-  }
 
-  job->original_file_mtime = gdata_entry_get_updated (entry);
+  job->original_file_mtime = gdata_entry_get_updated (job->gdata_entry);
 
-  tmp_name = g_strdup_printf ("gnome-documents-%d.pdf", g_str_hash (job->uri));
+  tmp_name = g_strdup_printf ("gnome-documents-%d.pdf", 
+                              g_str_hash (gdata_entry_get_id (job->gdata_entry)));
   tmp_path = g_build_filename (g_get_user_cache_dir (), "gnome-documents", NULL);
   job->pdf_path = pdf_path =
     g_build_filename (tmp_path, tmp_name, NULL);
@@ -432,102 +400,6 @@ single_entry_ready_cb (GObject *source,
 }
 
 static void
-pdf_load_job_from_google_documents_with_object (PdfLoadJob *job,
-                                                GoaObject *object)
-{
-  EGDataGoaAuthorizer *authorizer;
-
-  authorizer = e_gdata_goa_authorizer_new (object);
-  job->gdata_service = GDATA_SERVICE (gdata_documents_service_new (GDATA_AUTHORIZER (authorizer)));
-
-  /* FIXME: using GDATA_TYPE_DOCUMENTS_TEXT here is plain wrong,
-   * but I can't seem to use a more generic class, or GData segfaults.
-   * OTOH, using this type always works, even for presentations/spreadsheets.
-   *
-   * This needs to be fixed in libgdata, see
-   * https://bugzilla.gnome.org/show_bug.cgi?id=656971
-   */
-  gdata_service_query_single_entry_async (job->gdata_service,
-                                          gdata_documents_service_get_primary_authorization_domain (),
-                                          job->uri,
-                                          NULL, GDATA_TYPE_DOCUMENTS_TEXT,
-                                          job->cancellable, single_entry_ready_cb, job);
-
-  g_object_unref (authorizer);
-}
-
-static void
-client_ready_cb (GObject *source,
-                 GAsyncResult *res,
-                 gpointer user_data)
-{
-  GoaObject *object, *target = NULL;
-  GoaAccount *account;
-  GoaClient *client;
-  GError *error = NULL;
-  GList *accounts, *l;
-  gchar *stripped_id;
-  PdfLoadJob *job = user_data;
-  GdPdfLoader *self;
-
-  client = goa_client_new_finish (res, &error);
-
-  if (error != NULL) {
-    pdf_load_job_complete_error (job, error);
-    return;
-  }
-
-  self = GD_PDF_LOADER (g_async_result_get_source_object (G_ASYNC_RESULT (job->result)));
-  stripped_id = strip_tracker_prefix (self->priv->source_id);
-  g_object_unref (self);
-
-  if (stripped_id == NULL) {
-    pdf_load_job_complete_error 
-      (job,
-       g_error_new_literal (G_IO_ERROR, 0,
-                            "Wrong source ID; passed in a google URL, "
-                            "but the source ID is not coming from GOA"));
-    return;
-  }
-
-  accounts = goa_client_get_accounts (client);
-  for (l = accounts; l != NULL; l = l->next) {
-    object = l->data;
-    account = goa_object_peek_account (object);
-    
-    if (account == NULL)
-      continue;
-
-    if (goa_object_peek_documents (object) == NULL)
-      continue;
-
-    if (g_strcmp0 (goa_account_get_id (account), stripped_id) == 0) {
-      target = object;
-      break;
-    }
-  }
-
-  if (target != NULL) {
-    pdf_load_job_from_google_documents_with_object (job, target);
-  } else {
-    pdf_load_job_complete_error 
-      (job,
-       g_error_new_literal (G_IO_ERROR, 0,
-                            "Cannot find the specified GOA account"));
-  }
-
-  g_free (stripped_id);
-  g_list_free_full (accounts, g_object_unref);
-  g_object_unref (client);
-}
-
-static void
-pdf_load_job_from_google_documents (PdfLoadJob *job)
-{
-  goa_client_new (job->cancellable, client_ready_cb, job);
-}
-
-static void
 unoconv_child_watch_cb (GPid pid,
                         gint status,
                         gpointer user_data)
@@ -741,112 +613,68 @@ pdf_load_job_from_regular_file (PdfLoadJob *job)
 static void
 pdf_load_job_start (PdfLoadJob *job)
 {
-  if (g_str_has_prefix (job->uri, "https://docs.google.com";)) {
+  if (job->gdata_entry != NULL) {
     pdf_load_job_from_google_documents (job);
   } else {
     pdf_load_job_from_regular_file (job);
   }
 }
 
-static void
-gd_pdf_loader_dispose (GObject *object)
+void
+gd_pdf_loader_load_uri_async (const gchar *uri,
+                              GCancellable *cancellable,
+                              GAsyncReadyCallback callback,
+                              gpointer user_data)
 {
-  GdPdfLoader *self = GD_PDF_LOADER (object);
+  PdfLoadJob *job;
+  GSimpleAsyncResult *result;
 
-  g_free (self->priv->source_id);
+  result = g_simple_async_result_new (NULL, callback, user_data,
+                                      gd_pdf_loader_load_uri_async);
 
-  G_OBJECT_CLASS (gd_pdf_loader_parent_class)->dispose (object);
-}
+  job = pdf_load_job_new (result, uri, NULL, cancellable);
 
-static void
-gd_pdf_loader_get_property (GObject *object,
-                            guint       prop_id,
-                            GValue     *value,
-                            GParamSpec *pspec)
-{
-  GdPdfLoader *self = GD_PDF_LOADER (object);
-
-  switch (prop_id) {
-  case PROP_SOURCE_ID:
-    g_value_set_string (value, self->priv->source_id);
-    break;
-  default:
-    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    break;
-  }
-}
+  pdf_load_job_start (job);
 
-static void
-gd_pdf_loader_set_property (GObject *object,
-                            guint       prop_id,
-                            const GValue *value,
-                            GParamSpec *pspec)
-{
-  GdPdfLoader *self = GD_PDF_LOADER (object);
-
-  switch (prop_id) {
-  case PROP_SOURCE_ID:
-    self->priv->source_id = g_value_dup_string (value);
-    break;
-  default:
-    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    break;
-  }
+  g_object_unref (result);
 }
 
-static void
-gd_pdf_loader_class_init (GdPdfLoaderClass *klass)
+/**
+ * gd_pdf_loader_load_uri_finish:
+ * @res:
+ * @error: (allow-none) (out):
+ *
+ * Returns: (transfer full):
+ */
+EvDocument *
+gd_pdf_loader_load_uri_finish (GAsyncResult *res,
+                               GError **error)
 {
-  GObjectClass *oclass;
-
-  oclass = G_OBJECT_CLASS (klass);
-  oclass->dispose = gd_pdf_loader_dispose;
-  oclass->get_property = gd_pdf_loader_get_property;
-  oclass->set_property = gd_pdf_loader_set_property;
-
-  g_object_class_install_property
-    (oclass,
-     PROP_SOURCE_ID,
-     g_param_spec_string ("source-id",
-                          "Source ID",
-                          "The ID of the source we're loading from",
-                          NULL,
-                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
-
-  g_type_class_add_private (klass, sizeof (GdPdfLoaderPrivate));
-}
+  EvDocument *retval;
 
-static void
-gd_pdf_loader_init (GdPdfLoader *self)
-{
-  self->priv =
-    G_TYPE_INSTANCE_GET_PRIVATE (self,
-                                 GD_TYPE_PDF_LOADER,
-                                 GdPdfLoaderPrivate);
-}
+  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
+    return NULL;
 
-GdPdfLoader *
-gd_pdf_loader_new (const gchar *source_id)
-{
-  return g_object_new (GD_TYPE_PDF_LOADER,
-                       "source-id", source_id,
-                       NULL);
+  retval = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
+  return retval;
 }
 
+
 void
-gd_pdf_loader_load_uri_async (GdPdfLoader *self,
-                              const gchar *uri,
-                              GCancellable *cancellable,
-                              GAsyncReadyCallback callback,
-                              gpointer user_data)
+gd_pdf_loader_load_entry_async (GDataEntry *entry,
+                                GDataDocumentsService *service,
+                                GCancellable *cancellable,
+                                GAsyncReadyCallback callback,
+                                gpointer user_data)
 {
   PdfLoadJob *job;
   GSimpleAsyncResult *result;
 
-  result = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
-                                      gd_pdf_loader_load_uri_async);
+  result = g_simple_async_result_new (NULL, callback, user_data,
+                                      gd_pdf_loader_load_entry_async);
 
-  job = pdf_load_job_new (result, uri, cancellable);
+  job = pdf_load_job_new (result, NULL, entry, cancellable);
+  job->gdata_service = g_object_ref (service);
 
   pdf_load_job_start (job);
 
@@ -854,17 +682,15 @@ gd_pdf_loader_load_uri_async (GdPdfLoader *self,
 }
 
 /**
- * gd_pdf_loader_load_uri_finish:
- * @self:
+ * gd_pdf_loader_load_entry_finish:
  * @res:
  * @error: (allow-none) (out):
  *
  * Returns: (transfer full):
  */
 EvDocument *
-gd_pdf_loader_load_uri_finish (GdPdfLoader *self,
-                               GAsyncResult *res,
-                               GError **error)
+gd_pdf_loader_load_entry_finish (GAsyncResult *res,
+                                 GError **error)
 {
   EvDocument *retval;
 
diff --git a/src/lib/gd-pdf-loader.h b/src/lib/gd-pdf-loader.h
index 81a4d12..3f3d6c4 100644
--- a/src/lib/gd-pdf-loader.h
+++ b/src/lib/gd-pdf-loader.h
@@ -26,45 +26,25 @@
 #include <glib-object.h>
 #include <gio/gio.h>
 #include <evince-document.h>
+#include <gdata/gdata.h>
 
 G_BEGIN_DECLS
 
-#define GD_TYPE_PDF_LOADER            (gd_pdf_loader_get_type ())
-#define GD_PDF_LOADER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GD_TYPE_PDF_LOADER, GdPdfLoader))
-#define GD_IS_PDF_LOADER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GD_TYPE_PDF_LOADER))
-#define GD_PDF_LOADER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GD_TYPE_PDF_LOADER, GdPdfLoaderClass))
-#define GD_IS_PDF_LOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GD_TYPE_PDF_LOADER))
-#define GD_PDF_LOADER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GD_TYPE_PDF_LOADER, GdPdfLoaderClass))
-
-typedef struct _GdPdfLoader          GdPdfLoader;
-typedef struct _GdPdfLoaderPrivate   GdPdfLoaderPrivate;
-typedef struct _GdPdfLoaderClass     GdPdfLoaderClass;
-
-struct _GdPdfLoader
-{
-  GObject parent_instance;
-
-  GdPdfLoaderPrivate *priv;
-};
-
-struct _GdPdfLoaderClass
-{
-  GObjectClass parent_class;
-};
-
-GType    gd_pdf_loader_get_type     (void) G_GNUC_CONST;
-
-GdPdfLoader *gd_pdf_loader_new (const gchar *source_id);
-
-void gd_pdf_loader_load_uri_async (GdPdfLoader *self,
-                                   const gchar *uri,
+void gd_pdf_loader_load_uri_async (const gchar *uri,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer user_data);
-EvDocument *gd_pdf_loader_load_uri_finish (GdPdfLoader *self,
-                                           GAsyncResult *res,
+EvDocument *gd_pdf_loader_load_uri_finish (GAsyncResult *res,
                                            GError **error);
 
+void gd_pdf_loader_load_entry_async (GDataEntry *entry,
+                                     GDataDocumentsService *service,
+                                     GCancellable *cancellable,
+                                     GAsyncReadyCallback callback,
+                                     gpointer user_data);
+EvDocument *gd_pdf_loader_load_entry_finish (GAsyncResult *res,
+                                             GError **error);
+
 G_END_DECLS
 
 #endif /* __GD_PDF_LOADER_H__ */
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 6b7c0c6..e4efe8e 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -188,16 +188,12 @@ MainWindow.prototype = {
         }
 
         let doc = Global.documentManager.lookupDocument(urn);
+        this._loaderCancellable = new Gio.Cancellable();
 
-        TrackerUtils.sourceIdFromResourceUrn(doc.resourceUrn, Lang.bind(this,
-            function(sourceId) {
-                this._loaderCancellable = new Gio.Cancellable();
-                this._pdfLoader = new Gd.PdfLoader({ source_id: sourceId });
-                this._pdfLoader.load_uri_async(doc.identifier, this._loaderCancellable, Lang.bind(this, this._onDocumentLoaded));
+        this._loaderTimeout = Mainloop.timeout_add(_PDF_LOADER_TIMEOUT,
+            Lang.bind(this, this._onPdfLoaderTimeout));
 
-                this._loaderTimeout = Mainloop.timeout_add(_PDF_LOADER_TIMEOUT,
-                                                           Lang.bind(this, this._onPdfLoaderTimeout));
-            }));
+        doc.loadPreview(this._loaderCancellable, Lang.bind(this, this._onDocumentLoaded));
     },
 
     _onPdfLoaderTimeout: function() {
@@ -211,14 +207,9 @@ MainWindow.prototype = {
         return false;
     },
 
-    _onDocumentLoaded: function(loader, res) {
-        let document = null;
-        try {
-            document = loader.load_uri_finish(res);
-        } catch (e) {
-            log("Unable to load the PDF document: " + e.toString());
+    _onDocumentLoaded: function(document) {
+        if (!document)
             return;
-        }
 
         this._loaderCancellable = null;
         let model = EvView.DocumentModel.new_with_document(document);
diff --git a/src/sources.js b/src/sources.js
index 7cf868d..a7fb5a2 100644
--- a/src/sources.js
+++ b/src/sources.js
@@ -34,26 +34,38 @@ const _ = imports.gettext.gettext;
 const Global = imports.global;
 const TrackerUtils = imports.trackerUtils;
 
-function Source(id, name, initCallback) {
-    this._init(id, name, initCallback);
+function Source(params) {
+    this._init(params);
 };
 
 Source.prototype = {
-    _init: function(id, name, initCallback) {
-        this.id = id;
-        this.name = name;
+    _init: function(params) {
+        this.id = null;
+        this.name = null;
+        this.object = null;
 
-        this._initCallback = initCallback;
+        if (params.object) {
+            this.object = params.object;
+
+            let account = this.object.get_account();
+            this.id = account.get_id();
+            this.name = account.get_name();
+        } else {
+            this.id = params.id;
+            this.name = params.name;
+        }
 
-        if (id == 'all' || id == 'local') {
-            this.resourceUrn = id;
+        this._initCallback = params.initCallback;
+
+        if (this.id == 'all' || this.id == 'local') {
+            this.resourceUrn = null;
             Mainloop.idle_add(Lang.bind(this,
                 function() {
                     this._initCallback();
                     return false;
                 }));
         } else {
-            TrackerUtils.resourceUrnFromSourceId(id, Lang.bind(this,
+            TrackerUtils.resourceUrnFromSourceId(this.id, Lang.bind(this,
                 function(resourceUrn) {
                     this.resourceUrn = resourceUrn;
                     this._initCallback();
@@ -124,8 +136,12 @@ SourceManager.prototype = {
 
         // two outstanding ops for the local sources, and one for the GOA client
         this._outstandingOps = 3;
-        this.sources.push(new Source('all', _("All"), Lang.bind(this, this._initSourceCollector)));
-        this.sources.push(new Source('local', _("Local"), Lang.bind(this, this._initSourceCollector)));
+        this.sources.push(new Source({ id: 'all',
+                                       name: _("All"),
+                                       initCallback: Lang.bind(this, this._initSourceCollector) }));
+        this.sources.push(new Source({ id: 'local',
+                                       name: _("Local"),
+                                       initCallback: Lang.bind(this, this._initSourceCollector) }));
 
         Goa.Client.new(null, Lang.bind(this, this._onGoaClientCreated));
     },
@@ -143,18 +159,15 @@ SourceManager.prototype = {
 
         accounts.forEach(Lang.bind(this,
             function(object) {
-                let account = object.get_account();
-                if (!account)
+                if (!object.get_account())
                     return;
 
                 if (!object.get_documents())
                     return;
 
-                let id = account.get_id();
-                let name = account.get_provider_name();
-
                 this._outstandingOps++;
-                this.sources.push(new Source(id, name, Lang.bind(this, this._initSourceCollector)));
+                this.sources.push(new Source({ object: object,
+                                               initCallback: Lang.bind(this, this._initSourceCollector) }));
             }));
 
         let activeSourceId = Global.settings.get_string('active-source');
@@ -191,6 +204,18 @@ SourceManager.prototype = {
 
     getActiveSourceFilter: function(subject) {
         return this.activeSource.getFilter(subject);
+    },
+
+    getSourceByUrn: function(resourceUrn) {
+        let matched = this.sources.filter(Lang.bind(this,
+            function(source) {
+                return (source.resourceUrn == resourceUrn);
+            }));
+
+        if (!matched.length)
+            return null;
+
+        return matched[0];
     }
 };
 Signals.addSignalMethods(SourceManager.prototype);
diff --git a/src/trackerUtils.js b/src/trackerUtils.js
index dd1a77d..25219d8 100644
--- a/src/trackerUtils.js
+++ b/src/trackerUtils.js
@@ -23,43 +23,6 @@ const GLib = imports.gi.GLib;
 
 const Global = imports.global;
 
-function sourceIdFromResourceUrn(resourceUrn, callback) {
-    //FIXME: is this right?
-    if(resourceUrn[0] != '<')
-        resourceUrn = '<' + resourceUrn + '>';
-
-    let sparql = ('SELECT ?id WHERE { %s a nie:DataSource; nao:identifier ?id }').format(resourceUrn);
-
-    Global.connection.query_async
-        (sparql, null,
-         function(object, res) {
-             let cursor = null;
-             try {
-                 cursor = object.query_finish(res);
-             } catch (e) {
-                 log('Unable to resolve resource URN -> account ID: ' + e.toString());
-                 return;
-             }
-
-             cursor.next_async(null,
-                 function(object, res) {
-                     try {
-                         let valid = cursor.next_finish(res);
-
-                         if (!valid) {
-                             callback(null);
-                             return;
-                         }
-                     } catch (e) {
-                         log('Unable to resolve resource URN -> account ID: ' + e.toString());
-                     }
-
-                     let sourceId = cursor.get_string(0)[0];
-                     callback(sourceId);
-                 });
-         });
-}
-
 function resourceUrnFromSourceId(sourceId, callback) {
     let sparql = ('SELECT ?urn WHERE { ?urn a nie:DataSource; nao:identifier \"goa:documents:%s\" }').format(sourceId);
 



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