[gnome-documents] documents, properties: Replace uses of "instanceof" with virtual functions



commit 14acf23a2d7e2116817085527ef94d92ea886d47
Author: Alessandro Bono <shadow openaliasbox org>
Date:   Thu Aug 27 14:57:48 2015 +0200

    documents, properties: Replace uses of "instanceof" with virtual functions
    
    When adding support for new remote services, it helps to have
    all the service-specific code in one place instead of being
    scattered across the code base.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=754165

 libgd             |    2 +-
 src/documents.js  |   34 ++++++++++++++++++++++++++++++++++
 src/properties.js |   38 ++++++++------------------------------
 3 files changed, 43 insertions(+), 31 deletions(-)
---
diff --git a/libgd b/libgd
index 04b2480..7e0dd4b 160000
--- a/libgd
+++ b/libgd
@@ -1 +1 @@
-Subproject commit 04b2480259769709ec34d7ee48294878c94bbbb5
+Subproject commit 7e0dd4b15ea80673f62249ac092763b10fc226ad
diff --git a/src/documents.js b/src/documents.js
index a350a42..1f5cbb5 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -644,6 +644,11 @@ const DocCommon = new Lang.Class({
             }));
     },
 
+    getSourceLink: function() {
+        // This should return an array of URI and source name
+        log('Error: DocCommon implementations must override getSourceLink');
+    },
+
     getWhere: function() {
         let retval = '';
 
@@ -763,6 +768,17 @@ const LocalDocument = new Lang.Class({
                     log('Unable to trash ' + this.uri + ': ' + e.message);
                 }
             }));
+    },
+
+    getSourceLink: function() {
+        if (this.collection)
+            return [ null, this.sourceName ];
+
+        let sourceLink = Gio.file_new_for_uri(this.uri).get_parent();
+        let sourcePath = sourceLink.get_path();
+
+        let uri = sourceLink.get_uri();
+        return [ uri, sourcePath ];
     }
 });
 
@@ -929,6 +945,11 @@ const GoogleDocument = new Lang.Class({
 
     canTrash: function() {
         return false;
+    },
+
+    getSourceLink: function() {
+        let uri = 'http://docs.google.com/';
+        return [ uri, this.sourceName ];
     }
 });
 
@@ -992,6 +1013,14 @@ const OwncloudDocument = new Lang.Class({
 
     canTrash: function() {
         return false;
+    },
+
+    getSourceLink: function() {
+        let source = Application.sourceManager.getItemById(this.resourceUrn);
+        let account = source.object.get_account();
+        let presentationIdentity = account.presentation_identity;
+        let uri ='https://' + presentationIdentity + '/';
+        return [ uri, presentationIdentity ];
     }
 });
 
@@ -1094,6 +1123,11 @@ const SkydriveDocument = new Lang.Class({
 
     canTrash: function() {
         return false;
+    },
+
+    getSourceLink: function() {
+        let uri = 'https://onedrive.live.com';
+        return [ uri, this.sourceName ];
     }
 });
 
diff --git a/src/properties.js b/src/properties.js
index dc55d96..98b9da9 100644
--- a/src/properties.js
+++ b/src/properties.js
@@ -164,38 +164,16 @@ const PropertiesDialog = new Lang.Class({
         }
 
         // Source value
-        if (doc instanceof Documents.GoogleDocument) {
-            let uri = 'http://docs.google.com/';
-            this._sourceData = new Gtk.Label({ label: '<a href=\"' + uri + '\">' + doc.sourceName + '</a>',
-                                               use_markup: true,
-                                               halign: Gtk.Align.START });
-        } else if (doc instanceof Documents.OwncloudDocument) {
-            let source = Application.sourceManager.getItemById(doc.resourceUrn);
-            let account = source.object.get_account();
-            let presentation_identity = account.presentation_identity;
-            let uri ='https://' + presentation_identity + '/';
-            this._sourceData = new Gtk.Label({ label: '<a href=\"' + uri + '\">' + presentation_identity + 
'</a>',
-                                               use_markup: true,
-                                               halign: Gtk.Align.START });
-        } else if (doc instanceof Documents.SkydriveDocument) {
-            let uri = 'https://onedrive.live.com';
-            this._sourceData = new Gtk.Label({ label: '<a href=\"' + uri + '\">' + doc.sourceName + '</a>',
+        let [ uri, name ] = doc.getSourceLink();
+        if (uri) {
+            this._sourceData = new Gtk.Label({ label: '<a href=\"' + uri + '\">' + name + '</a>',
                                                use_markup: true,
+                                               halign: Gtk.Align.START,
+                                               ellipsize: Pango.EllipsizeMode.END });
+        } else {
+            // Collections don't have links
+            this._sourceData = new Gtk.Label({ label: name,
                                                halign: Gtk.Align.START });
-        } else { // local document
-            if (doc.collection) {
-                this._sourceData = new Gtk.Label({ label: doc.sourceName,
-                                                   halign: Gtk.Align.START });
-            } else {
-                let sourceLink = Gio.file_new_for_uri(doc.uri).get_parent();
-                let sourcePath = sourceLink.get_path();
-
-                let uri = sourceLink.get_uri();
-                this._sourceData = new Gtk.Label({ label: '<a href=\"' + uri + '\">' + sourcePath + '</a>',
-                                                   use_markup: true,
-                                                   halign: Gtk.Align.START,
-                                                   ellipsize: Pango.EllipsizeMode.END });
-            }
         }
 
         grid.attach_next_to (this._sourceData, this._source, Gtk.PositionType.RIGHT, 2, 1);


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