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.
---
numberstation/__main__.py | 29 ++++++++++++++++++-----------
numberstation/window.py | 10 ++++------
2 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/numberstation/__main__.py b/numberstation/__main__.py
index b22ad66..af3e4c0 100644
--- a/numberstation/__main__.py
+++ b/numberstation/__main__.py
@@ -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__':
diff --git a/numberstation/window.py b/numberstation/window.py
index b529070..d38a639 100644
--- a/numberstation/window.py
+++ b/numberstation/window.py
@@ -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()
--
2.25.1
This is great! Thanks, I've applied it and released 1.1.0 with this and
your previous patch.
On 2/2/22 16:59, Kazutoshi Noguchi wrote:
> 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.
> ---
> numberstation/__main__.py | 29 ++++++++++++++++++-----------
> numberstation/window.py | 10 ++++------
> 2 files changed, 22 insertions(+), 17 deletions(-)
>
> diff --git a/numberstation/__main__.py b/numberstation/__main__.py
> index b22ad66..af3e4c0 100644
> --- a/numberstation/__main__.py
> +++ b/numberstation/__main__.py
> @@ -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__':
> diff --git a/numberstation/window.py b/numberstation/window.py
> index b529070..d38a639 100644
> --- a/numberstation/window.py
> +++ b/numberstation/window.py
> @@ -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()