[PATCH hiprompt-gtk-py] Support remembering consent
Export this patch
Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
---
hiprompt_gtk/proto.py | 37 +++++++++++++++++++++++++++++++++++++
hiprompt_gtk/window.py | 34 +++++++++++++++++++++++++++++++++-
2 files changed, 70 insertions(+), 1 deletion(-)
diff --git a/hiprompt_gtk/proto.py b/hiprompt_gtk/proto.py
index fecaf6a..bcd1d4c 100644
--- a/hiprompt_gtk/proto.py
+++ b/hiprompt_gtk/proto.py
@@ -38,10 +38,23 @@ class PromptMode(Enum):
disclose = "disclose"
delete = "delete"
+class RememberKind(Enum):
+ never = "never"
+ infinite = "infinite"
+ timeout = "timeout"
+
+class Remember:
+ def __init__(self, kind, value=None):
+ self.kind = RememberKind(kind)
+ self.value = None
+
+ if self.kind == RememberKind("timeout"):
+ self.value = value
class Session:
def __init__(self):
self.keys = []
+ self.remembers = []
self.unlock = False
self.status = 127
self.mode = None
@@ -76,6 +89,18 @@ class Session:
self.mode = PromptMode(args)
return False
+ def _remember(self, args):
+ parts = args.strip().partition(" ")
+ if (parts[0] == "never"):
+ self.remembers.append(Remember("never"))
+ elif (parts[0] == "infinite"):
+ self.remembers.append(Remember("infinite"))
+ elif (parts[0] == "timeout"):
+ self.remembers.append(Remember("timeout", int(parts[2])))
+ else:
+ sys.stderr.write(f"Not supported remember '{parts[0]}'\n")
+ exit(127)
+
def password(self, pw, callback):
"""
Sends a password to the daemon in response to an unlock command and
@@ -99,3 +124,15 @@ class Session:
return False
gio_lines_async(self.stream, _callback)
+
+ def remember(self, remember):
+ """
+ Sends the choosen remember to the daemon
+ """
+ if remember.kind == RememberKind("never"):
+ sys.stdout.write(f"remember never\n")
+ elif remember.kind == RememberKind("infinite"):
+ sys.stdout.write(f"remember infinite\n")
+ elif remember.kind == RememberKind("timeout"):
+ sys.stdout.write(f"remember timeout {remember.value}\n")
+ sys.stdout.flush()
diff --git a/hiprompt_gtk/window.py b/hiprompt_gtk/window.py
index 840dbcc..fa1df46 100644
--- a/hiprompt_gtk/window.py
+++ b/hiprompt_gtk/window.py
@@ -1,7 +1,7 @@
import gi
from gi.repository import Gtk, Gio, Gdk, GLib
-from hiprompt_gtk.proto import PromptMode
+from hiprompt_gtk.proto import PromptMode, RememberKind
class Window:
@@ -28,6 +28,8 @@ class Window:
self.entry = None
self.buttons = []
+ self.remember = None
+
self.accel = Gtk.AccelGroup()
self.accel.connect(Gdk.keyval_from_name('Escape'), 0, 0, self.on_deny)
self.window.add_accel_group(self.accel)
@@ -91,6 +93,8 @@ class Window:
self.session.password(password, _callback)
def on_disclose_grant(self, *args):
+ if self.remember:
+ self.session.remember(self.remember)
self.session.status = 0
Gtk.main_quit()
@@ -190,6 +194,11 @@ class Window:
box.add(label)
box.add(self.make_key_box())
+ if len(self.session.remembers) != 0:
+ label = Gtk.Label("The daemon can remember your consent:")
+ box.add(label)
+ box.add(self.make_remember_box())
+
def make_delete_ui(self):
for child in self.box:
self.box.remove(child)
@@ -264,3 +273,26 @@ class Window:
row.pack_start(value, True, True, 0)
return box
+
+ def make_remember_box(self):
+ box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=8)
+
+ radio = Gtk.RadioButton.new_with_label_from_widget(None, "None")
+ radio.connect("toggled", self.on_remember_toggled, None)
+ box.pack_start(radio, True, True, 0)
+
+ for remember in self.session.remembers:
+ if remember.kind == RememberKind("never"):
+ label = "Never"
+ elif remember.kind == RememberKind("infinite"):
+ label = "Indefinitely"
+ elif remember.kind == RememberKind("timeout"):
+ label = f"For {remember.value} seconds"
+ radio = Gtk.RadioButton.new_with_label_from_widget(radio, label)
+ radio.connect("toggled", self.on_remember_toggled, remember)
+ box.pack_start(radio, True, True, 0)
+
+ return box
+
+ def on_remember_toggled(self, radio, remember):
+ self.remember = remember
--
2.43.0