[gnome-documents] factory: split the code creating and updating documents in a factory



commit 2063be89306bf7333fb4e35eec04e3a8caffdea1
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Aug 21 17:57:12 2011 +0200

    factory: split the code creating and updating documents in a factory
    
    This will allow future flexibility

 src/Makefile-js.am  |    1 +
 src/docFactory.js   |  194 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/trackerModel.js |  183 ++++--------------------------------------------
 src/utils.js        |   30 ++++++++
 4 files changed, 239 insertions(+), 169 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index c542dc8..962e109 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -2,6 +2,7 @@ jsdir = $(pkgdatadir)/js/
 dist_js_DATA = \
     accountsModel.js \
     application.js \
+    docFactory.js \
     iconView.js \
     listView.js \
     main.js \
diff --git a/src/docFactory.js b/src/docFactory.js
new file mode 100644
index 0000000..111aeeb
--- /dev/null
+++ b/src/docFactory.js
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2011 Red Hat, Inc.
+ *
+ * Gnome Documents is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * Gnome Documents 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 General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with Gnome Documents; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Cosimo Cecchi <cosimoc redhat com>
+ *
+ */
+
+const GdkPixbuf = imports.gi.GdkPixbuf;
+const Gio = imports.gi.Gio;
+const Gd = imports.gi.Gd;
+const Gtk = imports.gi.Gtk;
+
+const Lang = imports.lang;
+const Signals = imports.signals;
+
+const TrackerModel = imports.trackerModel;
+const Utils = imports.utils;
+
+function DocCommon(cursor) {
+    this._init(cursor);
+}
+
+DocCommon.prototype = {
+    _init: function(cursor) {
+        this.urn = cursor.get_string(TrackerModel.TrackerColumns.URN)[0];
+        this.title = cursor.get_string(TrackerModel.TrackerColumns.TITLE)[0];
+        this.author = cursor.get_string(TrackerModel.TrackerColumns.AUTHOR)[0];
+        this.mtime = cursor.get_string(TrackerModel.TrackerColumns.MTIME)[0];
+        this.resourceUrn = cursor.get_string(TrackerModel.TrackerColumns.RESOURCE_URN)[0];
+
+        // sanitize
+        if (!this.author)
+            this.author = '';
+
+        // might eventually get updated with a thumbnail by a subclass
+        let type = cursor.get_string(TrackerModel.TrackerColumns.TYPE)[0];
+        this.pixbuf = Utils.pixbufFromRdfType(type);
+
+        // overridden in subclasses
+        this.uri = null;
+    }
+};
+Signals.addSignalMethods(DocCommon.prototype);
+
+const _FILE_ATTRIBUTES = 'standard::icon,standard::content-type,thumbnail::path,time::modified';
+
+function LocalDocument(cursor) {
+    this._init(cursor);
+}
+
+LocalDocument.prototype = {
+    __proto__: DocCommon.prototype,
+
+    _init: function(cursor) {
+        DocCommon.prototype._init.call(this, cursor);
+
+        // overridden
+        this.uri = cursor.get_string(TrackerModel.TrackerColumns.URI)[0];
+
+        this._file = Gio.file_new_for_uri(this.uri);
+        this._file.query_info_async(_FILE_ATTRIBUTES,
+                                    0, 0, null,
+                                    Lang.bind(this, this._onFileQueryInfo));
+    },
+
+    _onFileQueryInfo: function(object, res) {
+        let info = null;
+        let haveNewIcon = false;
+
+        try {
+            info = object.query_info_finish(res);
+        } catch (e) {
+            log('Unable to query info for file at ' + this.uri + ': ' + e.toString());
+            return;
+        }
+
+        let thumbPath = info.get_attribute_byte_string(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH);
+        if (thumbPath) {
+            this.pixbuf =
+                GdkPixbuf.Pixbuf.new_from_file_at_size(thumbPath,
+                                                       Utils.getIconSize(),
+                                                       Utils.getIconSize());
+            haveNewIcon = true;
+        } else {
+            let icon = info.get_icon();
+
+            if (icon) {
+                let theme = Gtk.IconTheme.get_default();
+                let iconInfo = theme.lookup_by_gicon(icon, Utils.getIconSize(),
+                                                     Gtk.IconLookupFlags.FORCE_SIZE |
+                                                     Gtk.IconLookupFlags.GENERIC_FALLBACK);
+                try {
+                    this.pixbuf = iconInfo.load_icon();
+                    haveNewIcon = true;
+                } catch (e) {
+                    log('Unable to load an icon from theme for file at ' + this.uri + ': ' + e.toString());
+                }
+            }
+
+            // try to create the thumbnail
+            Gd.queue_thumbnail_job_for_file_async(this._file,
+                                                  Lang.bind(this, this._onQueueThumbnailJob));
+        }
+
+        if (haveNewIcon)
+            this.emit('icon-updated');
+    },
+
+    _onQueueThumbnailJob: function(object, res) {
+        let thumbnailed = Gd.queue_thumbnail_job_for_file_finish(res);
+
+        if (!thumbnailed)
+            return;
+
+        // get the new thumbnail path
+        this._file.query_info_async(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH,
+                                    0, 0, null,
+                                    Lang.bind(this, this._onThumbnailPathInfo));
+    },
+
+    _onThumbnailPathInfo: function(object, res) {
+        let info = null;
+
+        try {
+            info = object.query_info_finish(res);
+        } catch (e) {
+            log('Unable to query info for file at ' + this.uri + ': ' + e.toString());
+            return;
+        }
+
+        let thumbPath = info.get_attribute_byte_string(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH);
+
+        if (thumbPath) {
+            this.pixbuf =
+                GdkPixbuf.Pixbuf.new_from_file_at_size(thumbPath,
+                                                       Utils.getIconSize(),
+                                                       Utils.getIconSize());
+
+            this.emit('icon-updated');
+        }
+    }
+};
+
+function GoogleDocument(cursor) {
+    this._init(cursor);
+}
+
+GoogleDocument.prototype = {
+    __proto__: DocCommon.prototype,
+
+    _init: function(cursor) {
+        DocCommon.prototype._init.call(this, cursor);
+
+        // overridden
+        this.uri = cursor.get_string(TrackerModel.TrackerColumns.IDENTIFIER)[0];
+    }
+};
+
+function DocFactory() {
+    this._init();
+}
+
+DocFactory.prototype = {
+    _init: function() {
+    },
+
+    _identifierIsGoogle: function(identifier) {
+        return (identifier &&
+                (identifier.indexOf('https://docs.google.com') != -1));
+    },
+
+    newDocument: function(cursor) {
+        let identifier = cursor.get_string(TrackerModel.TrackerColumns.IDENTIFIER)[0];
+
+        if (this._identifierIsGoogle(identifier))
+            return new GoogleDocument(cursor);
+
+        return new LocalDocument(cursor);
+    }
+};
diff --git a/src/trackerModel.js b/src/trackerModel.js
index 348a31f..75bd471 100644
--- a/src/trackerModel.js
+++ b/src/trackerModel.js
@@ -28,8 +28,8 @@ const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const Tracker = imports.gi.Tracker;
 const Gd = imports.gi.Gd;
-const GdkPixbuf = imports.gi.GdkPixbuf;
 
+const DocFactory = imports.docFactory;
 const Main = imports.main;
 const Utils = imports.utils;
 
@@ -58,87 +58,6 @@ const TrackerColumns = {
     RESOURCE_URN: 8
 };
 
-const _FILE_ATTRIBUTES = 'standard::icon,standard::content-type,thumbnail::path,time::modified';
-
-function LocalFileInfoLoader(uri) {
-    this._init(uri);
-}
-
-LocalFileInfoLoader.prototype = {
-    _init: function(uri) {
-        this._file = Gio.file_new_for_uri(uri);
-
-        this._file.query_info_async(_FILE_ATTRIBUTES,
-                                    0, 0, null,
-                                    Lang.bind(this, this._onFileQueryInfo));
-    },
-
-    _onFileQueryInfo: function(object, res) {
-        let info = null;
-        let treePath = null;
-
-        try {
-            info = object.query_info_finish(res);
-        } catch (e) {
-            log('Unable to query info for file at ' + this.file.get_uri() + ': ' + e.toString());
-        }
-
-        let thumbPath = info.get_attribute_byte_string(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH);
-        if (thumbPath) {
-            this.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(thumbPath,
-                                                                  Utils.getIconSize(),
-                                                                  Utils.getIconSize());
-        } else {
-            let icon = info.get_icon();
-
-            if (icon) {
-                let theme = Gtk.IconTheme.get_default();
-                let iconInfo = theme.lookup_by_gicon(icon, Utils.getIconSize(),
-                                                     Gtk.IconLookupFlags.FORCE_SIZE |
-                                                     Gtk.IconLookupFlags.GENERIC_FALLBACK);
-                this.pixbuf = iconInfo.load_icon();
-            }
-
-            // try to create the thumbnail
-            Gd.queue_thumbnail_job_for_file_async(this._file,
-                                                  Lang.bind(this, this._onQueueThumbnailJob));
-        }
-
-        this.emit('info-loaded');
-    },
-
-    _onQueueThumbnailJob: function(object, res) {
-        let thumbnailed = Gd.queue_thumbnail_job_for_file_finish(res);
-
-        if (!thumbnailed)
-            return;
-
-        // get the new thumbnail path
-        this._file.query_info_async(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH,
-                                    0, 0, null,
-                                    Lang.bind(this, function(object, res) {
-                                        let info = null;
-
-                                        try {
-                                            info = object.query_info_finish(res);
-                                        } catch (e) {
-                                            log('Unable to query info for file at ' + uri + ': ' + e.toString());
-                                            return;
-                                        }
-
-                                        let thumbPath = info.get_attribute_byte_string(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH);
-
-                                        if (thumbPath) {
-                                            this.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size
-                                                (thumbPath, Utils.getIconSize(), Utils.getIconSize());
-
-                                            this.emit('icon-updated');
-                                        }
-                                    }));
-    }
-};
-Signals.addSignalMethods(LocalFileInfoLoader.prototype);
-
 function QueryBuilder() {
     this._init();
 }
@@ -270,6 +189,7 @@ function TrackerModel(callback) {
 TrackerModel.prototype = {
     _init: function(callback) {
         this._builder = new QueryBuilder();
+        this._factory = new DocFactory.DocFactory();
         this._initCallback = callback;
         Main.settings.connect('changed::list-view', Lang.bind(this, this._onSettingsChanged));
 
@@ -296,102 +216,27 @@ TrackerModel.prototype = {
         this._performCurrentQuery();
     },
 
-    _rowIsGoogle: function(identifier) {
-        return (identifier &&
-                (identifier.indexOf('https://docs.google.com') != -1));
-    },
-
-    _addLocalRowFromCursor: function(cursor) {
-        let urn = cursor.get_string(TrackerColumns.URN)[0];
-        let uri = cursor.get_string(TrackerColumns.URI)[0];
-        let title = cursor.get_string(TrackerColumns.TITLE)[0];
-        let author = cursor.get_string(TrackerColumns.AUTHOR)[0];
-        let mtime = cursor.get_string(TrackerColumns.MTIME)[0];
-        let resourceUrn = cursor.get_string(TrackerColumns.RESOURCE_URN)[0];
-
+    _addRowFromCursor: function(cursor) {
         this.itemCount = cursor.get_integer(TrackerColumns.TOTAL_COUNT);
 
-        if (!author)
-            author = '';
-
-        let treePath = null;
-        let loader = new LocalFileInfoLoader(uri);
-
-        loader.connect('info-loaded', Lang.bind(this,
-            function(loader) {
-                let iter = this.model.append();
-                treePath = this.model.get_path(iter);
+        let newDoc = this._factory.newDocument(cursor);
+        let iter = this.model.append();
+        let treePath = this.model.get_path(iter);
 
-                Gd.store_set(this.model, iter,
-                             urn, uri, title, author, mtime, loader.pixbuf, resourceUrn);
-            }));
+        Gd.store_set(this.model, iter,
+                     newDoc.urn, newDoc.uri,
+                     newDoc.title, newDoc.author,
+                     newDoc.mtime, newDoc.pixbuf,
+                     newDoc.resourceUrn);
 
-        loader.connect('icon-updated', Lang.bind(this,
-            function(loader) {
+        newDoc.connect('icon-updated', Lang.bind(this,
+            function() {
                 let objectIter = this.model.get_iter(treePath)[1];
                 if (objectIter)
-                    Gd.store_update_icon(this.model, objectIter, loader.pixbuf);
+                    Gd.store_update_icon(this.model, objectIter, newDoc.pixbuf);
             }));
     },
 
-    _pixbufFromRdfType: function(type) {
-        let iconName;
-        let iconInfo = null;
-        let pixbuf = null;
-
-        if (type.indexOf('nfo#Spreadsheet') != -1)
-            iconName = 'x-office-spreadsheet';
-        else if (type.indexOf('nfo#Presentation') != -1)
-            iconName = 'x-office-presentation';
-        else
-            iconName = 'x-office-document';
-
-        iconInfo =
-            Gtk.IconTheme.get_default().lookup_icon(iconName, Utils.getIconSize(),
-                                                    Gtk.IconLookupFlags.FORCE_SIZE |
-                                                    Gtk.IconLookupFlags.GENERIC_FALLBACK);
-
-        if (iconInfo != null) {
-            try {
-                pixbuf = iconInfo.load_icon();
-            } catch (e) {
-                log('Unable to load pixbuf: ' + e.toString());
-            }
-        }
-
-        return pixbuf;
-    },
-
-    _addGoogleRowFromCursor: function(cursor) {
-        let urn = cursor.get_string(TrackerColumns.URN)[0];
-        let title = cursor.get_string(TrackerColumns.TITLE)[0];
-        let author = cursor.get_string(TrackerColumns.AUTHOR)[0];
-        let mtime = cursor.get_string(TrackerColumns.MTIME)[0];
-        let identifier = cursor.get_string(TrackerColumns.IDENTIFIER)[0];
-        let type = cursor.get_string(TrackerColumns.TYPE)[0];
-        let resourceUrn = cursor.get_string(TrackerColumns.RESOURCE_URN)[0];
-
-        this.itemCount = cursor.get_integer(TrackerColumns.TOTAL_COUNT);
-
-        if (!author)
-            author = '';
-
-        let pixbuf = this._pixbufFromRdfType(type);
-
-        let iter = this.model.append();
-        Gd.store_set(this.model, iter,
-                     urn, identifier, title, author, mtime, pixbuf, resourceUrn);
-    },
-
-    _addRowFromCursor: function(cursor) {
-        let identifier = cursor.get_string(TrackerColumns.IDENTIFIER)[0];
-
-        if (this._rowIsGoogle(identifier))
-            this._addGoogleRowFromCursor(cursor);
-        else
-            this._addLocalRowFromCursor(cursor);
-    },
-
     _onCursorNext: function(cursor, res) {
         try {
             let valid = cursor.next_finish(res);
diff --git a/src/utils.js b/src/utils.js
index 600ba9e..0330fee 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -19,6 +19,8 @@
  *
  */
 
+const Gtk = imports.gi.Gtk;
+
 const Main = imports.main;
 
 const _ICON_VIEW_SIZE = 128;
@@ -27,3 +29,31 @@ const _LIST_VIEW_SIZE = 48;
 function getIconSize() {
     return Main.settings.get_boolean('list-view') ? _LIST_VIEW_SIZE : _ICON_VIEW_SIZE;
 }
+
+function pixbufFromRdfType(type) {
+    let iconName;
+    let iconInfo = null;
+    let pixbuf = null;
+
+    if (type.indexOf('nfo#Spreadsheet') != -1)
+        iconName = 'x-office-spreadsheet';
+    else if (type.indexOf('nfo#Presentation') != -1)
+    iconName = 'x-office-presentation';
+    else
+        iconName = 'x-office-document';
+
+    iconInfo =
+        Gtk.IconTheme.get_default().lookup_icon(iconName, getIconSize(),
+                                                Gtk.IconLookupFlags.FORCE_SIZE |
+                                                Gtk.IconLookupFlags.GENERIC_FALLBACK);
+
+    if (iconInfo != null) {
+        try {
+            pixbuf = iconInfo.load_icon();
+        } catch (e) {
+            log('Unable to load pixbuf: ' + e.toString());
+        }
+    }
+
+    return pixbuf;
+}



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