[gnome-maps/wip/mlundblad/remaining_cmd_options] application: Handle command line args with GApplication



commit 71ab01b7810dd84ed9f52615e4b5e560ceb9c0b8
Author: Marcus Lundblad <ml update uu se>
Date:   Sat Jul 11 11:55:19 2020 +0200

    application: Handle command line args with GApplication
    
    Use GApplication::command_line to handle "remaining" args.
    This way we can get a proper description for the file|URI
    parameters.

 src/application.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
---
diff --git a/src/application.js b/src/application.js
index 0cd91640..165069dd 100644
--- a/src/application.js
+++ b/src/application.js
@@ -84,7 +84,8 @@ var Application = GObject.registerClass({
         _ensuredTypes.forEach((type) => GObject.type_ensure(type));
 
         super._init({ application_id: 'org.gnome.Maps',
-                      flags: Gio.ApplicationFlags.HANDLES_OPEN });
+                      flags: Gio.ApplicationFlags.HANDLES_OPEN |
+                             Gio.ApplicationFlags.HANDLES_COMMAND_LINE });
         this._connected = false;
 
         this.add_main_option('local',
@@ -109,8 +110,16 @@ var Application = GObject.registerClass({
                              GLib.OptionArg.NONE,
                              _("Ignore network availability"),
                              null);
+        this.add_main_option(GLib.OPTION_REMAINING,
+                             0,
+                             0,
+                             GLib.OptionArg.STRING_ARRAY,
+                             _("FILE…|URI"),
+                             _("FILE…|URI"));
 
         this.connect('handle-local-options', (app, options) => {
+            Utils.debug('has remaining: ' + options.contains(GLib.OPTION_REMAINING));
+
             if (options.contains('local')) {
                 let variant = options.lookup_value('local', null);
                 this.local_tile_path = variant.deep_unpack();
@@ -320,6 +329,8 @@ var Application = GObject.registerClass({
         let uri = files[0].get_uri();
         let scheme = GLib.uri_parse_scheme(uri);
 
+        Utils.debug('uri: ' + uri);
+
         if (scheme === 'geo') {
             /* we get an uri that looks like geo:///lat,lon, remove slashes */
             let geoURI = uri.replace(/\//g, '');
@@ -343,6 +354,52 @@ var Application = GObject.registerClass({
                                  this._openInternal.bind(this, files));
     }
 
+    vfunc_command_line(cmdline) {
+        Utils.debug('command line');
+        let options = cmdline.get_options_dict();
+
+        if (options.contains('local')) {
+            let variant = options.lookup_value('local', null);
+            this.local_tile_path = variant.deep_unpack();
+            normalStartup = false;
+            if (options.contains('local-tile-size')) {
+                variant = options.lookup_value('local-tile-size', null);
+                this.local_tile_size = variant.deep_unpack();
+            }
+        } else if (options.contains('version')) {
+            print(pkg.version);
+            /* quit the invoked process after printing the version number
+             * leaving the running instance unaffected
+             */
+            return 0;
+        } else if (options.contains('force-online')) {
+            this._forceOnline = true;
+        }
+
+        let remaining = options.lookup(GLib.OPTION_REMAINING, null);
+
+        if (remaining) {
+            let files = [];
+
+            remaining.forEach((r) => {
+                let path = r.get_string()[0];
+
+                if (path.startsWith('geo:') || path.startsWith('http://') ||
+                    path.startsWith('https://')) {
+                    files.push(Gio.File.new_for_uri(path));
+                } else {
+                    files.push(Gio.File.new_for_path(path));
+                }
+            });
+
+            this.open(files, '');
+        } else {
+            this.activate();
+        }
+
+        return 0;
+    }
+
     _onWindowDestroy(window) {
         this._mainWindow = null;
     }


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