From 0601bc0f891bf92a28c00895ab3d5d9fd37f37aa Mon Sep 17 00:00:00 2001 From: Rory MacQueen Date: Fri, 26 Jul 2013 16:13:19 -0700 Subject: Added function to utils file to directly fetch resource [endlessm/eos-sdk#175] --- wikipedia/src/presenters/domain_wiki_presenter.js | 23 +++++++++++++++++------ wikipedia/src/utils.js | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/wikipedia/src/presenters/domain_wiki_presenter.js b/wikipedia/src/presenters/domain_wiki_presenter.js index 50346b3..a1ef9be 100644 --- a/wikipedia/src/presenters/domain_wiki_presenter.js +++ b/wikipedia/src/presenters/domain_wiki_presenter.js @@ -4,6 +4,16 @@ const GObject = imports.gi.GObject; //Local Libraries const Utils = imports.utils; +const CategoryModel = imports.models.category_model; +const ArticleModel = imports.models.article_model; + + +function _resourceUriToPath(uri) { + if(uri.startsWith('resource://')) + return uri.slice('resource://'.length); + throw new Error('Resource URI did not start with "resource://"'); +} + const DomainWikiPresenter = new Lang.Class({ Name: "DomainWikiPresenter", Extends: GObject.Object, @@ -35,21 +45,22 @@ const DomainWikiPresenter = new Lang.Class({ }, initFromJsonFile: function(filename) { - let app_content = JSON.parse(Utils.load_file (filename)); + //filename = "../data/brazil_categories.json"; + let app_content = JSON.parse(Utils.load_file_from_resource(filename)); this._application_name = app_content['app_name']; this._image_uri = app_content['image_uri']; this._lang_code = filename.substring(0, 2); let categories = app_content['categories']; let cat_length = categories.length - categories = new Array(); + let category_models = new Array(); for(let i = 0; i < cat_length; i++){ let category = categories[i]; - let categoryModel = initCategory(category); + let categoryModel = this.initCategory(category); let articles = category['articles']; - categoryModel.addArticles(initArticleModels(articles)); - categories.push(categoryModel); + categoryModel.addArticles(this.initArticleModels(articles)); + category_models.push(categoryModel); } - this._domain_wiki_model.addCategories(categories); + this._domain_wiki_model.addCategories(category_models); }, initCategory: function(category){ diff --git a/wikipedia/src/utils.js b/wikipedia/src/utils.js index 099daf8..08c372f 100644 --- a/wikipedia/src/utils.js +++ b/wikipedia/src/utils.js @@ -34,6 +34,21 @@ const range = function(a, b, step) { return A; } +/* + * ...Taken from utils.js from the eos-weather branch... + */ +function load_file_from_resource(filename) { + // Return the text stored in the file at filename + var file = Gio.file_new_for_uri(filename); + var fstream = file.read(null); + var dstream = new Gio.DataInputStream({ + base_stream: fstream + }); + var data = dstream.read_until("", null); + fstream.close(null); + return data[0]; +} + /* * ...Taken from utils.js from the eos-weather branch... */ -- cgit v1.2.3