Philipp Hörist pushed to branch master at gajim / gajim
Commits:
-
0ffb16e5
by wurstsalat at 2022-05-04T18:38:04+02:00
3 changed files:
- gajim/common/modules/security_labels.py
- gajim/gtk/controls/base.py
- gajim/gtk/security_label_selector.py
Changes:
| ... | ... | @@ -14,8 +14,11 @@ |
| 14 | 14 | |
| 15 | 15 | # XEP-0258: Security Labels in XMPP
|
| 16 | 16 | |
| 17 | -from nbxmpp.namespaces import Namespace
|
|
| 17 | +from typing import Optional
|
|
| 18 | + |
|
| 18 | 19 | from nbxmpp.errors import is_error
|
| 20 | +from nbxmpp.namespaces import Namespace
|
|
| 21 | +from nbxmpp.modules.security_labels import Catalog
|
|
| 19 | 22 | |
| 20 | 23 | from gajim.common import app
|
| 21 | 24 | from gajim.common.events import SecCatalogReceived
|
| ... | ... | @@ -33,7 +36,7 @@ class SecLabels(BaseModule): |
| 33 | 36 | def __init__(self, con):
|
| 34 | 37 | BaseModule.__init__(self, con)
|
| 35 | 38 | |
| 36 | - self._catalogs = {}
|
|
| 39 | + self._catalogs: dict[str, Catalog] = {}
|
|
| 37 | 40 | self.supported = False
|
| 38 | 41 | |
| 39 | 42 | def pass_disco(self, info):
|
| ... | ... | @@ -44,7 +47,8 @@ def pass_disco(self, info): |
| 44 | 47 | self._log.info('Discovered security labels: %s', info.jid)
|
| 45 | 48 | |
| 46 | 49 | @as_task
|
| 47 | - def request_catalog(self, jid):
|
|
| 50 | + def request_catalog(self, jid: str):
|
|
| 51 | + |
|
| 48 | 52 | _task = yield
|
| 49 | 53 | |
| 50 | 54 | catalog = yield self._nbxmpp('SecurityLabels').request_catalog(jid)
|
| ... | ... | @@ -61,5 +65,12 @@ def request_catalog(self, jid): |
| 61 | 65 | jid=jid,
|
| 62 | 66 | catalog=catalog))
|
| 63 | 67 | |
| 64 | - def get_catalog(self, jid):
|
|
| 65 | - return self._catalogs.get(jid) |
|
| 68 | + def get_catalog(self, jid: str) -> Optional[Catalog]:
|
|
| 69 | + if not self.supported:
|
|
| 70 | + return None
|
|
| 71 | + |
|
| 72 | + catalog = self._catalogs.get(jid)
|
|
| 73 | + if catalog is None:
|
|
| 74 | + self.request_catalog(jid)
|
|
| 75 | + return None
|
|
| 76 | + return catalog |
| ... | ... | @@ -1329,6 +1329,7 @@ def set_control_active(self, state: bool) -> None: |
| 1329 | 1329 | |
| 1330 | 1330 | if state:
|
| 1331 | 1331 | self.set_emoticon_popover()
|
| 1332 | + self._security_label_selector.update()
|
|
| 1332 | 1333 | |
| 1333 | 1334 | if self.msg_textview.has_text():
|
| 1334 | 1335 | self._client.get_module('Chatstate').set_chatstate(
|
| ... | ... | @@ -19,8 +19,12 @@ |
| 19 | 19 | from gi.repository import Gtk
|
| 20 | 20 | from gi.repository import Pango
|
| 21 | 21 | |
| 22 | +from nbxmpp.protocol import JID
|
|
| 23 | + |
|
| 22 | 24 | from gajim.common import app
|
| 23 | 25 | from gajim.common import ged
|
| 26 | +from gajim.common.client import Client
|
|
| 27 | +from gajim.common.const import SimpleClientState
|
|
| 24 | 28 | from gajim.common.events import SecCatalogReceived
|
| 25 | 29 | from gajim.common.types import ChatContactT
|
| 26 | 30 | from gajim.common.i18n import _
|
| ... | ... | @@ -47,16 +51,20 @@ def __init__(self, account: str, contact: ChatContactT) -> None: |
| 47 | 51 | app.ged.register_event_handler(
|
| 48 | 52 | 'sec-catalog-received', ged.GUI1, self._sec_labels_received)
|
| 49 | 53 | |
| 54 | + app.settings.connect_signal(
|
|
| 55 | + 'enable_security_labels',
|
|
| 56 | + self._on_setting_changed,
|
|
| 57 | + account=self._account)
|
|
| 58 | + self._client.connect_signal(
|
|
| 59 | + 'state-changed', self._on_client_state_changed)
|
|
| 50 | 60 | self.connect('changed', self._on_changed)
|
| 51 | 61 | self.connect('destroy', self._on_destroy)
|
| 52 | - jid = self._contact.jid.bare
|
|
| 53 | - if self._client.get_module('SecLabels').supported:
|
|
| 54 | - self._client.get_module('SecLabels').request_catalog(jid)
|
|
| 55 | 62 | |
| 56 | 63 | def _on_destroy(self, _widget: SecurityLabelSelector) -> None:
|
| 57 | 64 | app.ged.remove_event_handler('sec-catalog-received',
|
| 58 | 65 | ged.GUI1,
|
| 59 | 66 | self._sec_labels_received)
|
| 67 | + app.check_finalize(self)
|
|
| 60 | 68 | |
| 61 | 69 | def _on_changed(self, _combo: Gtk.ComboBox) -> None:
|
| 62 | 70 | iter_ = self.get_active_iter()
|
| ... | ... | @@ -68,6 +76,24 @@ def _on_changed(self, _combo: Gtk.ComboBox) -> None: |
| 68 | 76 | self.set_tooltip_text(
|
| 69 | 77 | _('Selected security label: %s') % f'\n{label_text}')
|
| 70 | 78 | |
| 79 | + def _on_client_state_changed(self,
|
|
| 80 | + _client: Client,
|
|
| 81 | + _signal_name: str,
|
|
| 82 | + _state: SimpleClientState):
|
|
| 83 | + self.update()
|
|
| 84 | + |
|
| 85 | + def _on_setting_changed(self,
|
|
| 86 | + state: bool,
|
|
| 87 | + _name: str,
|
|
| 88 | + account: str,
|
|
| 89 | + _jid: Optional[JID]
|
|
| 90 | + ) -> None:
|
|
| 91 | + self.set_no_show_all(not state)
|
|
| 92 | + if state:
|
|
| 93 | + self.show_all()
|
|
| 94 | + else:
|
|
| 95 | + self.hide()
|
|
| 96 | + |
|
| 71 | 97 | def _sec_labels_received(self, event: SecCatalogReceived) -> None:
|
| 72 | 98 | if event.account != self._account:
|
| 73 | 99 | return
|
| ... | ... | @@ -101,7 +127,17 @@ def get_seclabel(self) -> Optional[str]: |
| 101 | 127 | |
| 102 | 128 | jid = self._contact.jid.bare
|
| 103 | 129 | catalog = self._client.get_module('SecLabels').get_catalog(jid)
|
| 130 | + if catalog is None:
|
|
| 131 | + return None
|
|
| 132 | + |
|
| 104 | 133 | labels, label_list = catalog.labels, catalog.get_label_names()
|
| 105 | 134 | label_name = label_list[index]
|
| 106 | 135 | label = labels[label_name]
|
| 107 | 136 | return label
|
| 137 | + |
|
| 138 | + def update(self) -> None:
|
|
| 139 | + chat_active = app.window.is_chat_active(
|
|
| 140 | + self._account, self._contact.jid)
|
|
| 141 | + if chat_active:
|
|
| 142 | + self._client.get_module('SecLabels').get_catalog(
|
|
| 143 | + self._contact.jid.bare) |