~martijnbraam/numberstation

d8d52cb3349e57ccd335a61231345e8b20dff5eb — Kazutoshi Noguchi 2 years ago 9c36828
Improve behavior when launching multiple instances

The current implementation ignores a key passed to the second instance
and always tries to add a key passed to the first instance.

This patch fixes this bug by handling arguments with GApplication.

This patch also makes this app focusing the existing window instead of creating
a new one when launching the second instance.
2 files changed, 22 insertions(+), 17 deletions(-)

M numberstation/__main__.py
M numberstation/window.py
M numberstation/__main__.py => numberstation/__main__.py +18 -11
@@ 1,6 1,7 @@
import argparse
import os
import gi
import sys

from numberstation.window import NumberstationWindow



@@ 12,23 13,29 @@ from gi.repository import Handy


class NumberstationApplication(Gtk.Application):
    def __init__(self, application_id, flags, args, version):
    def __init__(self, application_id, flags, version):
        Gtk.Application.__init__(self, application_id=application_id, flags=flags)
        self.connect("activate", self.new_window)
        self.args = args
        self.connect("activate", self.open_window)
        self.connect("open", self.open_url)
        self.version = version
        self.window = None

    def new_window(self, *args):
        NumberstationWindow(self, self.args, self.version)
    def open_window(self, *args):
        if self.window is None:
            self.window = NumberstationWindow(self, self.version)
        self.window.present()

    def open_url(self, app, files, *hint):
        self.open_window()

        for file in files:
            self.window.import_url(file.get_uri())

        self.window.build_code_list()

def main(version):
    Handy.init()

    parser = argparse.ArgumentParser(description="Numberstation TOTP authenticator")
    parser.add_argument('url', help='TOTP url to register', nargs='?')
    args = parser.parse_args()

    if os.path.isfile('numberstation/numberstation.gresource'):
        print("Using resources from cwd/numberstation")
        resource = Gio.resource_load("numberstation/numberstation.gresource")


@@ 38,9 45,9 @@ def main(version):
        resource = Gio.resource_load("numberstation.gresource")
        Gio.Resource._register(resource)

    app = NumberstationApplication("org.postmarketos.Numberstation", Gio.ApplicationFlags.FLAGS_NONE, args=args,
    app = NumberstationApplication("org.postmarketos.Numberstation", Gio.ApplicationFlags.HANDLES_OPEN,
                                   version=version)
    app.run()
    app.run(sys.argv)


if __name__ == '__main__':

M numberstation/window.py => numberstation/window.py +4 -6
@@ 17,9 17,8 @@ from gi.repository import Handy


class NumberstationWindow:
    def __init__(self, application, args, version):
    def __init__(self, application, version):
        self.application = application
        self.args = args
        self.version = version

        Handy.init()


@@ 74,16 73,12 @@ class NumberstationWindow:
            print("Could not unlock the keyring")

        self.timers = []
        if args.url:
            self.import_url(args.url)

        self.build_code_list()
        GLib.timeout_add(1000, self.update_codes)

        self.window.show()

        Gtk.main()

    def apply_css(self, widget, provider):
        Gtk.StyleContext.add_provider(widget.get_style_context(),
                                      provider,


@@ 393,3 388,6 @@ class NumberstationWindow:
        widget.token.initial_count += 1
        self.save_keyring()
        self.update_code_label(widget, None, widget.token)

    def present(self):
        self.window.present()