[gnome-documents] all: add an Open entry to the context menu



commit 4dbbc27629394ee5b94330b118725fa03b205d1e
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Aug 28 16:54:47 2011 -0400

    all: add an Open entry to the context menu

 src/documents.js  |   22 ++++++++++++++++++++++
 src/mainWindow.js |    8 +++++---
 src/view.js       |   30 +++++++++++++++++++-----------
 3 files changed, 46 insertions(+), 14 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 7922b7c..13c35a5 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -23,6 +23,7 @@ const GdkPixbuf = imports.gi.GdkPixbuf;
 const Gio = imports.gi.Gio;
 const Gd = imports.gi.Gd;
 const Gtk = imports.gi.Gtk;
+const _ = imports.gettext.gettext;
 
 const Lang = imports.lang;
 const Signals = imports.signals;
@@ -47,6 +48,7 @@ DocCommon.prototype = {
         this.favorite = null;
         this._type = null;
         this.pixbuf = null;
+        this.defaultAppName = null;
 
         this._populateFromCursor(cursor);
 
@@ -138,6 +140,10 @@ DocCommon.prototype = {
     destroy: function() {
         Global.settings.disconnect(this._refreshIconId);
         Global.changeMonitor.disconnect(this._changesId);
+    },
+
+    open: function(screen, timestamp) {
+        Gtk.show_uri(screen, this.uri, timestamp);
     }
 };
 Signals.addSignalMethods(DocCommon.prototype);
@@ -176,6 +182,9 @@ LocalDocument.prototype = {
             return;
         }
 
+        let defaultApp = Gio.app_info_get_default_for_type(info.get_content_type(), true);
+        this.defaultAppName = defaultApp.get_name();
+
         let thumbPath = info.get_attribute_byte_string(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH);
         if (thumbPath) {
             this.pixbuf =
@@ -255,6 +264,7 @@ GoogleDocument.prototype = {
 
         // overridden
         this.identifier = cursor.get_string(Query.QueryColumns.IDENTIFIER)[0];
+        this.defaultAppName = _("Google Docs");
     }
 };
 
@@ -295,6 +305,18 @@ DocumentManager.prototype = {
 
     getDocuments: function() {
         return this._docs;
+    },
+
+    lookupDocument: function(urn) {
+        let matched = this._docs.filter(
+            function(doc) {
+                return (doc.urn == urn);
+            });
+
+        if (!matched.length)
+            return null;
+
+        return matched[0];
     }
 };
 Signals.addSignalMethods(DocumentManager.prototype);
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 66ab631..6b7c0c6 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -181,17 +181,19 @@ MainWindow.prototype = {
         Global.application.quit();
     },
 
-    _onViewItemActivated: function(view, uri, resource) {
+    _onViewItemActivated: function(view, urn) {
         if (this._loaderTimeout != 0) {
             Mainloop.source_remove(this._loaderTimeout);
             this._loaderTimeout = 0;
         }
 
-        TrackerUtils.sourceIdFromResourceUrn(resource, Lang.bind(this,
+        let doc = Global.documentManager.lookupDocument(urn);
+
+        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(uri, this._loaderCancellable, Lang.bind(this, this._onDocumentLoaded));
+                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));
diff --git a/src/view.js b/src/view.js
index 191fa92..f507e2d 100644
--- a/src/view.js
+++ b/src/view.js
@@ -31,17 +31,27 @@ const Global = imports.global;
 const TrackerUtils = imports.trackerUtils;
 const Utils = imports.utils;
 
-function ContextMenu(urn, isFavorite) {
-    this._init(urn, isFavorite);
+function ContextMenu(urn) {
+    this._init(urn);
 }
 
 ContextMenu.prototype = {
-    _init: function(urn, isFavorite) {
-        this._urn = urn;
-        this._isFavorite = isFavorite;
+    _init: function(urn) {
+        let doc = Global.documentManager.lookupDocument(urn);
+        let isFavorite = doc.favorite;
 
         this.widget = new Gtk.Menu();
 
+        let openLabel = (doc.defaultAppName) ? _("Open with %s").format(doc.defaultAppName) : _("Open");
+        let openItem = new Gtk.MenuItem({ label: openLabel });
+        openItem.show();
+        this.widget.append(openItem);
+
+        openItem.connect('activate', Lang.bind(this,
+            function(item) {
+                doc.open(item.get_screen(), Gtk.get_current_event_time());
+            }));
+
         let favoriteLabel = (isFavorite) ? _("Remove from favorites") : _("Add to favorites");
         let favoriteItem = new Gtk.MenuItem({ label: favoriteLabel });
         favoriteItem.show();
@@ -49,7 +59,7 @@ ContextMenu.prototype = {
 
         favoriteItem.connect('activate', Lang.bind(this,
             function() {
-                TrackerUtils.setFavorite(this._urn, !this._isFavorite, null);
+                TrackerUtils.setFavorite(urn, !isFavorite, null);
             }));
 
         this.widget.show_all();
@@ -128,9 +138,8 @@ View.prototype = {
         let iter = this._treeModel.get_iter(path)[1];
 
         let urn = this._treeModel.get_value(iter, Documents.ModelColumns.URN);
-        let isFavorite = this._treeModel.get_value(iter, Documents.ModelColumns.FAVORITE);
+        let menu = new ContextMenu(urn);
 
-        let menu = new ContextMenu(urn, isFavorite);
         menu.widget.popup_for_device(null, null, null, null, null, null, button, timestamp);
 
         return true;
@@ -143,10 +152,9 @@ View.prototype = {
 
     activateItem: function(path) {
         let iter = this._treeModel.get_iter(path)[1];
-        let uri = this._treeModel.get_value(iter, Documents.ModelColumns.URI);
-        let resource = this._treeModel.get_value(iter, Documents.ModelColumns.RESOURCE_URN);
+        let urn = this._treeModel.get_value(iter, Documents.ModelColumns.URN);
 
-        this.emit('item-activated', uri, resource);
+        this.emit('item-activated', urn);
     }
 };
 Signals.addSignalMethods(View.prototype);



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