[gnome-documents] all: implement reading/writing back favorite category to GDocs



commit 7e80e782eb6ac212694d909133ba65002c50d1ae
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Aug 29 02:37:30 2011 -0400

    all: implement reading/writing back favorite category to GDocs

 src/documents.js           |   86 ++++++++++++++++++++++++++++++++++----------
 src/miner/gd-gdata-miner.c |   67 ++++++++++++++++++++++++++++++++--
 src/view.js                |    2 +-
 3 files changed, 132 insertions(+), 23 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index a268309..b771d9f 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -33,6 +33,7 @@ const Signals = imports.signals;
 const ChangeMonitor = imports.changeMonitor;
 const Global = imports.global;
 const Query = imports.query;
+const TrackerUtils = imports.trackerUtils;
 const Utils = imports.utils;
 
 function DocCommon(cursor) {
@@ -146,6 +147,10 @@ DocCommon.prototype = {
 
     open: function(screen, timestamp) {
         Gtk.show_uri(screen, this.uri, timestamp);
+    },
+
+    setFavorite: function(favorite) {
+        TrackerUtils.setFavorite(this.urn, favorite, null);
     }
 };
 Signals.addSignalMethods(DocCommon.prototype);
@@ -161,9 +166,6 @@ LocalDocument.prototype = {
 
     _init: function(cursor) {
         DocCommon.prototype._init.call(this, cursor);
-
-        // overridden
-        this.identifier = cursor.get_string(Query.QueryColumns.URI)[0];
     },
 
     refreshIcon: function() {
@@ -270,6 +272,9 @@ LocalDocument.prototype = {
     }
 };
 
+const _GOOGLE_DOCS_SCHEME_LABELS = "http://schemas.google.com/g/2005/labels";;
+const _GOOGLE_DOCS_TERM_STARRED = "http://schemas.google.com/g/2005/labels#starred";;
+
 function GoogleDocument(cursor) {
     this._init(cursor);
 }
@@ -285,7 +290,7 @@ GoogleDocument.prototype = {
         this.defaultAppName = _("Google Docs");
     },
 
-    loadPreview: function(cancellable, callback) {
+    _createGDataEntry: function(cancellable, callback) {
         let source = Global.sourceManager.getSourceByUrn(this.resourceUrn);
 
         let authorizer = new Gd.GDataGoaAuthorizer({ goa_object: source.object });
@@ -306,25 +311,68 @@ GoogleDocument.prototype = {
                          entry = object.query_single_entry_finish(res);
                      } catch (e) {
                          log('Unable to query the GData entry: ' + e.toString());
-
-                         callback(null);
-                         return;
                      }
+                     callback(entry, service);
+                 }));
+    },
 
-                     Gd.pdf_loader_load_entry_async
-                         (entry, service, cancellable, Lang.bind(this,
-                             function(source, res) {
-                                 let document = null;
+    loadPreview: function(cancellable, callback) {
+        this._createGDataEntry(cancellable, Lang.bind(this,
+            function(entry, service) {
+                if (!entry)
+                    callback(null);
+
+                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);
+                        }));
+            }));
+    },
 
-                                 try {
-                                     document = Gd.pdf_loader_load_entry_finish(res);
-                                 } catch (e) {
-                                     log('Unable to load the GData entry: ' + e.toString());
-                                 }
+    setFavorite: function(favorite) {
+        DocCommon.prototype.setFavorite.call(this, favorite);
+        this._createGDataEntry(null, Lang.bind(this,
+            function(entry, service) {
+                if (!entry)
+                    return;
 
-                                 callback(document);
-                             }));
-                 }));
+                let starred = null;
+                let categories = entry.get_categories();
+                categories.forEach(
+                    function(category) {
+                        if (category.scheme == _GOOGLE_DOCS_SCHEME_LABELS &&
+                            category.term == _GOOGLE_DOCS_TERM_STARRED)
+                            starred = category;
+                    });
+
+                if (!starred) {
+                    starred = new GData.Category({ scheme: _GOOGLE_DOCS_SCHEME_LABELS,
+                                                   term: _GOOGLE_DOCS_TERM_STARRED });
+                    entry.add_category(starred);
+                }
+
+                starred.set_label(favorite ? 'starred' : '');
+
+                service.update_entry_async
+                    (service.get_primary_authorization_domain(),
+                     entry, null, Lang.bind(this,
+                         function(service, res) {
+                             try {
+                                 service.update_entry_finish(res);
+                             } catch (e) {
+                                 log('Unable to update the entry ' + e.toString());
+                             }
+                         }));
+            }));
     }
 };
 
diff --git a/src/miner/gd-gdata-miner.c b/src/miner/gd-gdata-miner.c
index 96fd121..cbc4609 100644
--- a/src/miner/gd-gdata-miner.c
+++ b/src/miner/gd-gdata-miner.c
@@ -27,6 +27,7 @@
 #include "gd-gdata-miner.h"
 
 #define DATASOURCE_URN "urn:nepomuk:datasource:86ec9bc9-c242-427f-aa19-77b5a2c9b6f0"
+#define STARRED_CATEGORY_TERM "http://schemas.google.com/g/2005/labels#starred";
 
 G_DEFINE_TYPE (GdGDataMiner, gd_gdata_miner, G_TYPE_OBJECT)
 
@@ -56,6 +57,42 @@ _tracker_utils_format_into_graph (const gchar *graph)
 }
 
 static gboolean
+_tracker_sparql_connection_toggle_favorite (TrackerSparqlConnection *connection,
+                                            GCancellable *cancellable,
+                                            GError **error,
+                                            const gchar *resource,
+                                            gboolean favorite)
+{
+  GString *update;
+  const gchar *op_str = NULL;
+  gboolean retval = TRUE;
+
+  if (favorite)
+    op_str = "INSERT OR REPLACE";
+  else
+    op_str = "DELETE";
+
+  update = g_string_new (NULL);
+  g_string_append_printf 
+    (update,
+     "%s { <%s> nao:hasTag nao:predefined-tag-favorite }",
+     op_str, resource);
+
+  g_debug ("Toggle favorite: query %s", update->str);
+
+  tracker_sparql_connection_update (connection, update->str, 
+                                    G_PRIORITY_DEFAULT, cancellable,
+                                    error);
+
+  g_string_free (update, TRUE);
+
+  if (*error != NULL)
+    retval = FALSE;
+
+  return retval;
+}
+
+static gboolean
 _tracker_sparql_connection_insert_or_replace_triple (TrackerSparqlConnection *connection,
                                                      GCancellable *cancellable,
                                                      GError **error,
@@ -334,16 +371,18 @@ gd_gdata_miner_process_entry (GdGDataMiner *self,
   GDataEntry *entry = GDATA_ENTRY (doc_entry);
   gchar *resource;
   gchar *date, *resource_url;
+  const gchar *identifier, *class = NULL;
 
-  const gchar *path, *identifier, *class;
   GList *authors, *l;
   GDataAuthor *author;
-  TrackerSparqlBuilder *builder;
-  gint64 mtime;
 
   GDataLink *alternate;
   const gchar *alternate_uri;
 
+  GList *categories;
+  GDataCategory *category;
+  gboolean starred = FALSE;
+
   if (!GDATA_IS_DOCUMENTS_DOCUMENT (doc_entry))
     {
       /* TODO: folders */
@@ -383,6 +422,28 @@ gd_gdata_miner_process_entry (GdGDataMiner *self,
      identifier, resource,
      "nie:url", alternate_uri);
 
+  if (*error != NULL)
+    goto out;
+
+  categories = gdata_entry_get_categories (entry);
+  for (l = categories; l != NULL; l = l->next)
+    {
+      category = l->data;
+      if (g_strcmp0 (gdata_category_get_term (category), STARRED_CATEGORY_TERM) == 0)
+        {
+          starred = TRUE;
+          break;
+        }
+    }
+
+  _tracker_sparql_connection_toggle_favorite
+    (self->priv->connection, 
+     self->priv->cancellable, error,
+     resource, starred);
+
+  if (*error != NULL)
+    goto out;
+
   _tracker_sparql_connection_insert_or_replace_triple
     (self->priv->connection, 
      self->priv->cancellable, error,
diff --git a/src/view.js b/src/view.js
index 9381603..ebbf30a 100644
--- a/src/view.js
+++ b/src/view.js
@@ -59,7 +59,7 @@ ContextMenu.prototype = {
 
         favoriteItem.connect('activate', Lang.bind(this,
             function() {
-                TrackerUtils.setFavorite(urn, !isFavorite, null);
+                doc.setFavorite(!isFavorite);
             }));
 
         this.widget.show_all();



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