[polari/wip/fmuellner/debug-instance: 1/3] app: Add --test-instance command line flag



commit 9686ae8dfeb6ff11f6f77c8c2a3c3a57b861689a
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Feb 19 12:45:26 2016 +0100

    app: Add --test-instance command line flag
    
    When provided, Polari will use a different application ID and not disconnect
    on exit if a regular instance is already running. In other words, it allows
    hacking on Polari while using it for chatting at the same time :-)
    
    Fixes https://gitlab.gnome.org/GNOME/polari/issues/24

 flatpak/org.gnome.Polari.json |  1 +
 src/accountsMonitor.js        |  6 ++++--
 src/application.js            | 37 +++++++++++++++++++++++++++++++++++--
 3 files changed, 40 insertions(+), 4 deletions(-)
---
diff --git a/flatpak/org.gnome.Polari.json b/flatpak/org.gnome.Polari.json
index bc10c46..a045da7 100644
--- a/flatpak/org.gnome.Polari.json
+++ b/flatpak/org.gnome.Polari.json
@@ -15,6 +15,7 @@
         "--share=network",
         /* Our client name */
         "--own-name=org.freedesktop.Telepathy.Client.Polari",
+        "--own-name=org.freedesktop.Telepathy.Client.Polari.*",
         /* Telepathy */
         "--talk-name=org.freedesktop.Telepathy.AccountManager",
         "--talk-name=org.freedesktop.Telepathy.ChannelDispatcher",
diff --git a/src/accountsMonitor.js b/src/accountsMonitor.js
index 70151be..b6c3288 100644
--- a/src/accountsMonitor.js
+++ b/src/accountsMonitor.js
@@ -16,8 +16,10 @@ var AccountsMonitor = class {
         this._accountSettings = new Map();
 
         this._app = Gio.Application.get_default();
-        this._app.connect('prepare-shutdown',
-                          this._onPrepareShutdown.bind(this));
+
+        if (!this._app.isTestInstance)
+            this._app.connect('prepare-shutdown',
+                              this._onPrepareShutdown.bind(this));
 
         let factory = new ClientFactory();
         factory.add_channel_features([Tp.Channel.get_feature_quark_group()]);
diff --git a/src/application.js b/src/application.js
index 9b80cba..4c3ea53 100644
--- a/src/application.js
+++ b/src/application.js
@@ -45,17 +45,24 @@ var Application = GObject.registerClass({
         this.add_main_option('start-client', 0,
                              GLib.OptionFlags.NONE, GLib.OptionArg.NONE,
                              _("Start Telepathy client"), null);
+        this.add_main_option('test-instance', 0,
+                             GLib.OptionFlags.NONE, GLib.OptionArg.NONE,
+                             _("Allow running alongside another instance"), null);
         this.add_main_option('version', 0,
                              GLib.OptionFlags.NONE, GLib.OptionArg.NONE,
                              _("Print version and exit"), null);
         this.connect('handle-local-options', (o, dict) => {
+            let v = dict.lookup_value('test-instance', null);
+            if (v && v.get_boolean())
+                this._maybeMakeNonUnique();
+
             try {
                 this.register(null);
             } catch(e) {
                 return 1;
             }
 
-            let v = dict.lookup_value('start-client', null);
+            v = dict.lookup_value('start-client', null);
             if (v && v.get_boolean()) {
                 this.activate_action('start-client', null);
                 return 0;
@@ -116,6 +123,28 @@ var Application = GObject.registerClass({
         }
     }
 
+    _maybeMakeNonUnique() {
+        let bus = Gio.BusType.SESSION;
+        let name = this.application_id;
+        let flags = Gio.BusNameWatcherFlags.NONE;
+
+        let handled = false;
+        let id = Gio.bus_watch_name(bus, name, flags, () => {
+                debug('Running as test instance alongside primary instance');
+                this.set_flags(this.flags | Gio.ApplicationFlags.NON_UNIQUE);
+                handled = true;
+        }, () => {
+            debug('No primary instance found, running normally');
+            handled = true;
+        });
+
+        // Evil-ish ...
+        let main = GLib.MainContext.default();
+        while (!handled)
+            main.iteration(true);
+        Gio.bus_unwatch_name(id);
+    }
+
     vfunc_dbus_register(conn, path) {
         if (!Utils.isFlatpakSandbox())
             return true;
@@ -462,6 +491,10 @@ var Application = GObject.registerClass({
         return savedRooms.n_children() == 0;
     }
 
+    get isTestInstance() {
+        return this.flags & Gio.ApplicationFlags.NON_UNIQUE;
+    }
+
     _updateUserListAction() {
         let room = this.active_window.active_room;
         let action = this.lookup_action('user-list');
@@ -734,7 +767,7 @@ var Application = GObject.registerClass({
         let params = {
             name: 'Polari',
             account_manager: this._accountsMonitor.accountManager,
-            uniquify_name: false
+            uniquify_name: this.isTestInstance
         };
         this._telepathyClient = new TelepathyClient(params);
     }


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