[openpgp] Port to Gtk4
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with OpenPGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
|
||||
@@ -22,8 +24,11 @@ from gi.repository import Gtk
|
||||
from gajim.common import app
|
||||
from gajim.gtk.dialogs import ConfirmationDialog
|
||||
from gajim.gtk.dialogs import DialogButton
|
||||
from gajim.gtk.util.misc import container_remove_all
|
||||
from gajim.gtk.widgets import GajimAppWindow
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
from openpgp.modules.key_store import KeyData
|
||||
from openpgp.modules.util import Trust
|
||||
|
||||
log = logging.getLogger("gajim.p.openpgp.keydialog")
|
||||
@@ -36,48 +41,60 @@ TRUST_DATA = {
|
||||
}
|
||||
|
||||
|
||||
class KeyDialog(Gtk.Dialog):
|
||||
def __init__(self, account, jid, transient):
|
||||
super().__init__(title=_("Public Keys for %s") % jid, destroy_with_parent=True)
|
||||
class KeyDialog(GajimAppWindow):
|
||||
def __init__(self, account: str, jid: str, transient: Gtk.Window) -> None:
|
||||
|
||||
self.set_transient_for(transient)
|
||||
self.set_resizable(True)
|
||||
self.set_default_size(500, 300)
|
||||
GajimAppWindow.__init__(
|
||||
self,
|
||||
name="PGPKeyDialog",
|
||||
title=_("Public Keys for %s") % jid,
|
||||
default_width=450,
|
||||
default_height=400,
|
||||
transient_for=transient,
|
||||
modal=True,
|
||||
)
|
||||
|
||||
self.get_style_context().add_class("openpgp-key-dialog")
|
||||
self.window.add_css_class("openpgp-key-dialog")
|
||||
|
||||
self._client = app.get_client(account)
|
||||
|
||||
self._listbox = Gtk.ListBox()
|
||||
self._listbox.set_selection_mode(Gtk.SelectionMode.NONE)
|
||||
|
||||
self._scrolled = Gtk.ScrolledWindow()
|
||||
self._scrolled = Gtk.ScrolledWindow(hexpand=True)
|
||||
self._scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
|
||||
self._scrolled.add(self._listbox)
|
||||
self._scrolled.set_child(self._listbox)
|
||||
|
||||
box = self.get_content_area()
|
||||
box.pack_start(self._scrolled, True, True, 0)
|
||||
self.set_child(self._scrolled)
|
||||
|
||||
keys = self._client.get_module("OpenPGP").get_keys(jid, only_trusted=False)
|
||||
for key in keys:
|
||||
log.info("Load: %s", key.fingerprint)
|
||||
self._listbox.add(KeyRow(key))
|
||||
self.show_all()
|
||||
self._listbox.append(KeyRow(key, self))
|
||||
|
||||
self.show()
|
||||
|
||||
def _cleanup(self) -> None:
|
||||
del self._client
|
||||
del self._listbox
|
||||
del self._scrolled
|
||||
|
||||
|
||||
class KeyRow(Gtk.ListBoxRow):
|
||||
def __init__(self, key):
|
||||
def __init__(self, key: KeyData, dialog: GajimAppWindow):
|
||||
Gtk.ListBoxRow.__init__(self)
|
||||
self.set_activatable(False)
|
||||
|
||||
self._dialog = self.get_toplevel()
|
||||
self._dialog = dialog
|
||||
self.key = key
|
||||
|
||||
box = Gtk.Box()
|
||||
box.set_spacing(12)
|
||||
|
||||
self._trust_button = TrustButton(self)
|
||||
box.add(self._trust_button)
|
||||
self._trust_button = Gtk.MenuButton()
|
||||
self._trust_button.set_popover(TrustPopver(self))
|
||||
self._update_button_state()
|
||||
box.append(self._trust_button)
|
||||
|
||||
label_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
||||
fingerprint = Gtk.Label(label=self._format_fingerprint(key.fingerprint))
|
||||
@@ -88,25 +105,37 @@ class KeyRow(Gtk.ListBoxRow):
|
||||
fingerprint.set_halign(Gtk.Align.START)
|
||||
fingerprint.set_valign(Gtk.Align.START)
|
||||
fingerprint.set_hexpand(True)
|
||||
label_box.add(fingerprint)
|
||||
label_box.append(fingerprint)
|
||||
|
||||
date = Gtk.Label(label=self._format_timestamp(key.timestamp))
|
||||
date.set_halign(Gtk.Align.START)
|
||||
date.get_style_context().add_class("openpgp-mono")
|
||||
if not key.active:
|
||||
date.get_style_context().add_class("openpgp-inactive-color")
|
||||
label_box.add(date)
|
||||
label_box.append(date)
|
||||
|
||||
box.add(label_box)
|
||||
box.append(label_box)
|
||||
self.set_child(box)
|
||||
|
||||
self.add(box)
|
||||
self.show_all()
|
||||
def _update_button_state(self) -> None:
|
||||
icon_name, tooltip, css_class = TRUST_DATA[self.key.trust]
|
||||
self._trust_button.set_icon_name(icon_name)
|
||||
|
||||
def delete_fingerprint(self, *args):
|
||||
for css_cls in self._trust_button.get_css_classes():
|
||||
if css_cls.startswith("openpgp"):
|
||||
self._trust_button.remove_css_class(css_cls)
|
||||
|
||||
if not self.key.active:
|
||||
css_class = "inactive-color"
|
||||
tooltip = "%s - %s" % (_("Inactive"), tooltip)
|
||||
|
||||
self._trust_button.add_css_class(f"openpgp-{css_class}")
|
||||
self._trust_button.set_tooltip_text(tooltip)
|
||||
|
||||
def delete_fingerprint(self):
|
||||
def _remove():
|
||||
self.get_parent().remove(self)
|
||||
self.key.delete()
|
||||
self.destroy()
|
||||
|
||||
ConfirmationDialog(
|
||||
_("Delete Public Key?"),
|
||||
@@ -117,98 +146,76 @@ class KeyRow(Gtk.ListBoxRow):
|
||||
],
|
||||
).show()
|
||||
|
||||
def set_trust(self, trust):
|
||||
icon_name, tooltip, css_class = TRUST_DATA[trust]
|
||||
image = self._trust_button.get_child()
|
||||
image.set_from_icon_name(icon_name, Gtk.IconSize.MENU)
|
||||
image.get_style_context().add_class(css_class)
|
||||
def set_trust(self, trust: Trust) -> None:
|
||||
self.key.trust = trust
|
||||
self._update_button_state()
|
||||
|
||||
@staticmethod
|
||||
def _format_fingerprint(fingerprint):
|
||||
def _format_fingerprint(fingerprint: str) -> str:
|
||||
fplen = len(fingerprint)
|
||||
wordsize = fplen // 8
|
||||
buf = ""
|
||||
for w in range(0, fplen, wordsize):
|
||||
buf += "{0} ".format(fingerprint[w : w + wordsize])
|
||||
buf += f"{fingerprint[w : w + wordsize]} "
|
||||
return buf.rstrip()
|
||||
|
||||
@staticmethod
|
||||
def _format_timestamp(timestamp):
|
||||
def _format_timestamp(timestamp: float) -> str:
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
|
||||
|
||||
|
||||
class TrustButton(Gtk.MenuButton):
|
||||
def __init__(self, row):
|
||||
Gtk.MenuButton.__init__(self)
|
||||
self._row = row
|
||||
self._css_class = ""
|
||||
self.set_popover(TrustPopver(row))
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
icon_name, tooltip, css_class = TRUST_DATA[self._row.key.trust]
|
||||
image = self.get_child()
|
||||
image.set_from_icon_name(icon_name, Gtk.IconSize.MENU)
|
||||
# remove old color from icon
|
||||
image.get_style_context().remove_class(self._css_class)
|
||||
|
||||
if not self._row.key.active:
|
||||
css_class = "openpgp-inactive-color"
|
||||
tooltip = "%s - %s" % (_("Inactive"), tooltip)
|
||||
|
||||
image.get_style_context().add_class(css_class)
|
||||
self._css_class = css_class
|
||||
self.set_tooltip_text(tooltip)
|
||||
|
||||
|
||||
class TrustPopver(Gtk.Popover):
|
||||
def __init__(self, row):
|
||||
def __init__(self, row: KeyRow):
|
||||
Gtk.Popover.__init__(self)
|
||||
self._row = row
|
||||
self._listbox = Gtk.ListBox()
|
||||
self._listbox.set_selection_mode(Gtk.SelectionMode.NONE)
|
||||
if row.key.trust != Trust.VERIFIED:
|
||||
self._listbox.add(VerifiedOption())
|
||||
self._listbox.append(VerifiedOption())
|
||||
if row.key.trust != Trust.NOT_TRUSTED:
|
||||
self._listbox.add(NotTrustedOption())
|
||||
self._listbox.add(DeleteOption())
|
||||
self.add(self._listbox)
|
||||
self._listbox.show_all()
|
||||
self._listbox.append(NotTrustedOption())
|
||||
self._listbox.append(DeleteOption())
|
||||
self.set_child(self._listbox)
|
||||
self._listbox.connect("row-activated", self._activated)
|
||||
self.get_style_context().add_class("openpgp-trust-popover")
|
||||
self.add_css_class("openpgp-trust-popover")
|
||||
|
||||
def _activated(self, listbox, row):
|
||||
def _activated(self, listbox: Gtk.ListBox, row: MenuOption) -> None:
|
||||
self.popdown()
|
||||
if row.type_ is None:
|
||||
self._row.delete_fingerprint()
|
||||
else:
|
||||
self._row.key.trust = row.type_
|
||||
self.get_relative_to().update()
|
||||
self._row.set_trust(row.type_)
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
self._listbox.foreach(lambda row: self._listbox.remove(row))
|
||||
container_remove_all(self._listbox)
|
||||
if self._row.key.trust != Trust.VERIFIED:
|
||||
self._listbox.add(VerifiedOption())
|
||||
self._listbox.append(VerifiedOption())
|
||||
if self._row.key.trust != Trust.NOT_TRUSTED:
|
||||
self._listbox.add(NotTrustedOption())
|
||||
self._listbox.add(DeleteOption())
|
||||
self._listbox.append(NotTrustedOption())
|
||||
self._listbox.append(DeleteOption())
|
||||
|
||||
|
||||
class MenuOption(Gtk.ListBoxRow):
|
||||
|
||||
type_: Trust | None
|
||||
icon: str
|
||||
label: str
|
||||
color: str
|
||||
|
||||
def __init__(self):
|
||||
Gtk.ListBoxRow.__init__(self)
|
||||
box = Gtk.Box()
|
||||
box.set_spacing(6)
|
||||
|
||||
image = Gtk.Image.new_from_icon_name(self.icon, Gtk.IconSize.MENU)
|
||||
label = Gtk.Label(label=self.label)
|
||||
image.get_style_context().add_class(self.color)
|
||||
image = Gtk.Image.new_from_icon_name(self.icon)
|
||||
if self.color:
|
||||
image.add_css_class(self.color)
|
||||
|
||||
box.add(image)
|
||||
box.add(label)
|
||||
self.add(box)
|
||||
self.show_all()
|
||||
label = Gtk.Label(label=self.label)
|
||||
box.append(image)
|
||||
box.append(label)
|
||||
self.set_child(box)
|
||||
|
||||
|
||||
class VerifiedOption(MenuOption):
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
.openpgp-inactive-color { color: @unfocused_borders; }
|
||||
|
||||
.openpgp-inactive-color button > box > image { color: @unfocused_borders; }
|
||||
.openpgp-error-color button > box > image { color: @error_color; }
|
||||
.openpgp-warning-color button > box > image { color: @warning_color; }
|
||||
.openpgp-encrypted-color button > box > image { color: rgb(75, 181, 67); }
|
||||
.openpgp-mono { font-size: 12px; font-family: monospace; }
|
||||
|
||||
.openpgp-key-dialog > box { margin: 12px; }
|
||||
|
||||
.openpgp-key-dialog scrolledwindow row {
|
||||
border-bottom: 1px solid;
|
||||
border-color: @unfocused_borders;
|
||||
padding: 10px 20px 10px 10px;
|
||||
}
|
||||
.openpgp-key-dialog scrolledwindow row:last-child { border-bottom: 0px}
|
||||
|
||||
.openpgp-key-dialog scrolledwindow row:last-child { border-bottom: 0px; }
|
||||
.openpgp-key-dialog scrolledwindow { border: 1px solid; border-color:@unfocused_borders; }
|
||||
|
||||
.openpgp-trust-popover row { padding: 10px 15px 10px 10px; }
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with OpenPGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from typing import cast
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from enum import IntEnum
|
||||
@@ -22,8 +24,12 @@ from gi.repository import GLib
|
||||
from gi.repository import Gtk
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.client import Client
|
||||
from gajim.gtk.control import ChatControl
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
from ..pgpplugin import OpenPGPPlugin
|
||||
|
||||
log = logging.getLogger("gajim.p.openpgp.wizard")
|
||||
|
||||
|
||||
@@ -35,7 +41,9 @@ class Page(IntEnum):
|
||||
|
||||
|
||||
class KeyWizard(Gtk.Assistant):
|
||||
def __init__(self, plugin, account, chat_control):
|
||||
def __init__(
|
||||
self, plugin: OpenPGPPlugin, account: str, chat_control: ChatControl
|
||||
) -> None:
|
||||
Gtk.Assistant.__init__(self)
|
||||
|
||||
self._client = app.get_client(account)
|
||||
@@ -48,7 +56,6 @@ class KeyWizard(Gtk.Assistant):
|
||||
self.set_application(app.app)
|
||||
self.set_transient_for(app.window)
|
||||
self.set_resizable(True)
|
||||
self.set_position(Gtk.WindowPosition.CENTER)
|
||||
|
||||
self.set_default_size(600, 400)
|
||||
self.get_style_context().add_class("dialog-margin")
|
||||
@@ -65,9 +72,9 @@ class KeyWizard(Gtk.Assistant):
|
||||
self.connect("close", self._on_cancel)
|
||||
|
||||
self._remove_sidebar()
|
||||
self.show_all()
|
||||
self.show()
|
||||
|
||||
def _add_page(self, page):
|
||||
def _add_page(self, page: Gtk.Box) -> None:
|
||||
self.append_page(page)
|
||||
self.set_page_type(page, page.type_)
|
||||
self.set_page_title(page, page.title)
|
||||
@@ -82,7 +89,7 @@ class KeyWizard(Gtk.Assistant):
|
||||
action = app.window.lookup_action("set-encryption")
|
||||
action.activate(GLib.Variant("s", self._plugin.encryption_name))
|
||||
|
||||
def _on_page_change(self, assistant, page):
|
||||
def _on_page_change(self, assistant: Gtk.Assistant, page: Page) -> None:
|
||||
if self.get_current_page() == Page.NEWKEY:
|
||||
if self._client.get_module("OpenPGP").secret_key_available:
|
||||
self.set_current_page(Page.SUCCESS)
|
||||
@@ -91,8 +98,8 @@ class KeyWizard(Gtk.Assistant):
|
||||
elif self.get_current_page() == Page.SUCCESS:
|
||||
self._activate_encryption()
|
||||
|
||||
def _on_cancel(self, widget):
|
||||
self.destroy()
|
||||
def _on_cancel(self, widget: Gtk.Assistant):
|
||||
self.close()
|
||||
|
||||
|
||||
class WelcomePage(Gtk.Box):
|
||||
@@ -106,8 +113,8 @@ class WelcomePage(Gtk.Box):
|
||||
self.set_spacing(18)
|
||||
title_label = Gtk.Label(label=_("Setup OpenPGP"))
|
||||
text_label = Gtk.Label(label=_("Gajim will now try to setup OpenPGP for you"))
|
||||
self.add(title_label)
|
||||
self.add(text_label)
|
||||
self.append(title_label)
|
||||
self.append(text_label)
|
||||
|
||||
|
||||
class RequestPage(Gtk.Box):
|
||||
@@ -120,7 +127,7 @@ class RequestPage(Gtk.Box):
|
||||
super().__init__(orientation=Gtk.Orientation.VERTICAL)
|
||||
self.set_spacing(18)
|
||||
spinner = Gtk.Spinner()
|
||||
self.pack_start(spinner, True, True, 0)
|
||||
self.append(spinner)
|
||||
spinner.start()
|
||||
|
||||
|
||||
@@ -148,7 +155,7 @@ class NewKeyPage(RequestPage):
|
||||
title = _("Generating new Key")
|
||||
complete = False
|
||||
|
||||
def __init__(self, assistant, client):
|
||||
def __init__(self, assistant: Gtk.Assistant, client: Client) -> None:
|
||||
super().__init__()
|
||||
self._assistant = assistant
|
||||
self._client = client
|
||||
@@ -167,14 +174,14 @@ class NewKeyPage(RequestPage):
|
||||
|
||||
GLib.idle_add(self.finished, text)
|
||||
|
||||
def finished(self, error):
|
||||
def finished(self, error: str | None) -> None:
|
||||
if error is None:
|
||||
self._client.get_module("OpenPGP").get_own_key_details()
|
||||
self._client.get_module("OpenPGP").set_public_key()
|
||||
self._client.get_module("OpenPGP").request_keylist()
|
||||
self._assistant.set_current_page(Page.SUCCESS)
|
||||
else:
|
||||
error_page = self._assistant.get_nth_page(Page.ERROR)
|
||||
error_page = cast(ErrorPage, self._assistant.get_nth_page(Page.ERROR))
|
||||
error_page.set_text(error)
|
||||
self._assistant.set_current_page(Page.ERROR)
|
||||
|
||||
@@ -206,17 +213,15 @@ class SuccessfulPage(Gtk.Box):
|
||||
self.set_spacing(12)
|
||||
self.set_homogeneous(True)
|
||||
|
||||
icon = Gtk.Image.new_from_icon_name(
|
||||
"object-select-symbolic", Gtk.IconSize.DIALOG
|
||||
)
|
||||
icon.get_style_context().add_class("success-color")
|
||||
icon = Gtk.Image.new_from_icon_name("object-select-symbolic")
|
||||
icon.add_css_class("success-color")
|
||||
icon.set_valign(Gtk.Align.END)
|
||||
label = Gtk.Label(label=_("Setup successful"))
|
||||
label.get_style_context().add_class("bold16")
|
||||
label.add_css_class("bold16")
|
||||
label.set_valign(Gtk.Align.START)
|
||||
|
||||
self.add(icon)
|
||||
self.add(label)
|
||||
self.append(icon)
|
||||
self.append(label)
|
||||
|
||||
|
||||
class ErrorPage(Gtk.Box):
|
||||
@@ -230,17 +235,15 @@ class ErrorPage(Gtk.Box):
|
||||
self.set_spacing(12)
|
||||
self.set_homogeneous(True)
|
||||
|
||||
icon = Gtk.Image.new_from_icon_name(
|
||||
"dialog-error-symbolic", Gtk.IconSize.DIALOG
|
||||
)
|
||||
icon = Gtk.Image.new_from_icon_name("dialog-error-symbolic")
|
||||
icon.get_style_context().add_class("error-color")
|
||||
icon.set_valign(Gtk.Align.END)
|
||||
self._label = Gtk.Label()
|
||||
self._label.get_style_context().add_class("bold16")
|
||||
self._label.set_valign(Gtk.Align.START)
|
||||
|
||||
self.add(icon)
|
||||
self.add(self._label)
|
||||
self.append(icon)
|
||||
self.append(self._label)
|
||||
|
||||
def set_text(self, text):
|
||||
def set_text(self, text: str) -> None:
|
||||
self._label.set_text(text)
|
||||
|
||||
Reference in New Issue
Block a user