[gnome-maps/wip/mlundblad/transit-routing: 6/30] Break out reading of the service file



commit 5bc79b3f006dce1eba40a20b8be907c1ff5c4edc
Author: Marcus Lundblad <ml update uu se>
Date:   Tue Nov 1 22:28:24 2016 +0100

    Break out reading of the service file
    
    This will be needed to use the service definition
    file to read service URL for OpenTripPlanner.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=755808

 src/org.gnome.Maps.src.gresource.xml |    1 +
 src/service.js                       |   76 ++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 0 deletions(-)
---
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 7ebb1f8..0e16722 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -65,6 +65,7 @@
     <file>serviceBackend.js</file>
     <file>settings.js</file>
     <file>sendToDialog.js</file>
+    <file>service.js</file>
     <file>shapeLayer.js</file>
     <file>shortPrintLayout.js</file>
     <file>sidebar.js</file>
diff --git a/src/service.js b/src/service.js
new file mode 100644
index 0000000..5cf9535
--- /dev/null
+++ b/src/service.js
@@ -0,0 +1,76 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2016 Marcus Lundblad.
+ * GNOME Maps 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 Maps 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 Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ *         Jonas Danielsson <jonas threetimestwo org>
+ */
+
+const Gio = imports.gi.Gio;
+const GLib = imports.gi.GLib;
+const Soup = imports.gi.Soup;
+
+const Utils = imports.utils;
+
+let _service = null;
+
+const _SERVICE_URL = 'https://gis.gnome.org/services/v1/service.json';
+const _DEFAULT_SERVICE_FILE = 'maps-service.json';
+
+function _getServiceFromFile(filename) {
+    let data = Utils.readFile(filename);
+    if (!data) {
+        log('Failed to open service file: ' + filename);
+        System.exit(1);
+    }
+    _service = JSON.parse(data);
+    return _service;
+}
+
+function _createDefaultService() {
+    let filename = GLib.build_filenamev([pkg.pkgdatadir,
+                                         _DEFAULT_SERVICE_FILE]);
+    return _getServiceFromFile(filename);
+}
+
+function getService() {
+    if (_service)
+        return _service;
+
+    let serviceOverride = GLib.getenv('MAPS_SERVICE');
+    if (serviceOverride)
+        return _getServiceFromFile(serviceOverride);
+
+    let user_agent = 'gnome-maps/' + pkg.version;
+    let session = new Soup.Session({ user_agent : user_agent });
+    let msg = Soup.Message.new('GET', _SERVICE_URL);
+    try {
+        let stream = Gio.DataInputStream.new(session.send(msg, null));
+
+        let lines = "";
+        while(true) {
+            let [line, _] = stream.read_line_utf8(null);
+            if (line === null)
+                break;
+            lines += line;
+        }
+        _service = JSON.parse(lines);
+        return _service;
+    } catch(e) {
+        Utils.debug(e);
+        return _createDefaultService();
+    }
+}


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