Philipp Hörist pushed to branch master at gajim / gajim


Commits:
0ffb16e5 by wurstsalat at 2022-05-04T18:38:04+02:00
fix: Security labels: Improve requesting catalog

- - - - -


3 changed files:

- gajim/common/modules/security_labels.py
- gajim/gtk/controls/base.py
- gajim/gtk/security_label_selector.py


Changes:

=====================================
gajim/common/modules/security_labels.py
=====================================
@@ -14,8 +14,11 @@
 
 # XEP-0258: Security Labels in XMPP
 
-from nbxmpp.namespaces import Namespace
+from typing import Optional
+
 from nbxmpp.errors import is_error
+from nbxmpp.namespaces import Namespace
+from nbxmpp.modules.security_labels import Catalog
 
 from gajim.common import app
 from gajim.common.events import SecCatalogReceived
@@ -33,7 +36,7 @@ class SecLabels(BaseModule):
     def __init__(self, con):
         BaseModule.__init__(self, con)
 
-        self._catalogs = {}
+        self._catalogs: dict[str, Catalog] = {}
         self.supported = False
 
     def pass_disco(self, info):
@@ -44,7 +47,8 @@ def pass_disco(self, info):
         self._log.info('Discovered security labels: %s', info.jid)
 
     @as_task
-    def request_catalog(self, jid):
+    def request_catalog(self, jid: str):
+
         _task = yield
 
         catalog = yield self._nbxmpp('SecurityLabels').request_catalog(jid)
@@ -61,5 +65,12 @@ def request_catalog(self, jid):
                                                jid=jid,
                                                catalog=catalog))
 
-    def get_catalog(self, jid):
-        return self._catalogs.get(jid)
+    def get_catalog(self, jid: str) -> Optional[Catalog]:
+        if not self.supported:
+            return None
+
+        catalog = self._catalogs.get(jid)
+        if catalog is None:
+            self.request_catalog(jid)
+            return None
+        return catalog


=====================================
gajim/gtk/controls/base.py
=====================================
@@ -1329,6 +1329,7 @@ def set_control_active(self, state: bool) -> None:
 
         if state:
             self.set_emoticon_popover()
+            self._security_label_selector.update()
 
             if self.msg_textview.has_text():
                 self._client.get_module('Chatstate').set_chatstate(


=====================================
gajim/gtk/security_label_selector.py
=====================================
@@ -19,8 +19,12 @@
 from gi.repository import Gtk
 from gi.repository import Pango
 
+from nbxmpp.protocol import JID
+
 from gajim.common import app
 from gajim.common import ged
+from gajim.common.client import Client
+from gajim.common.const import SimpleClientState
 from gajim.common.events import SecCatalogReceived
 from gajim.common.types import ChatContactT
 from gajim.common.i18n import _
@@ -47,16 +51,20 @@ def __init__(self, account: str, contact: ChatContactT) -> 
None:
         app.ged.register_event_handler(
             'sec-catalog-received', ged.GUI1, self._sec_labels_received)
 
+        app.settings.connect_signal(
+            'enable_security_labels',
+            self._on_setting_changed,
+            account=self._account)
+        self._client.connect_signal(
+            'state-changed', self._on_client_state_changed)
         self.connect('changed', self._on_changed)
         self.connect('destroy', self._on_destroy)
-        jid = self._contact.jid.bare
-        if self._client.get_module('SecLabels').supported:
-            self._client.get_module('SecLabels').request_catalog(jid)
 
     def _on_destroy(self, _widget: SecurityLabelSelector) -> None:
         app.ged.remove_event_handler('sec-catalog-received',
                                      ged.GUI1,
                                      self._sec_labels_received)
+        app.check_finalize(self)
 
     def _on_changed(self, _combo: Gtk.ComboBox) -> None:
         iter_ = self.get_active_iter()
@@ -68,6 +76,24 @@ def _on_changed(self, _combo: Gtk.ComboBox) -> None:
         self.set_tooltip_text(
             _('Selected security label: %s') % f'\n{label_text}')
 
+    def _on_client_state_changed(self,
+                                 _client: Client,
+                                 _signal_name: str,
+                                 _state: SimpleClientState):
+        self.update()
+
+    def _on_setting_changed(self,
+                            state: bool,
+                            _name: str,
+                            account: str,
+                            _jid: Optional[JID]
+                            ) -> None:
+        self.set_no_show_all(not state)
+        if state:
+            self.show_all()
+        else:
+            self.hide()
+
     def _sec_labels_received(self, event: SecCatalogReceived) -> None:
         if event.account != self._account:
             return
@@ -101,7 +127,17 @@ def get_seclabel(self) -> Optional[str]:
 
         jid = self._contact.jid.bare
         catalog = self._client.get_module('SecLabels').get_catalog(jid)
+        if catalog is None:
+            return None
+
         labels, label_list = catalog.labels, catalog.get_label_names()
         label_name = label_list[index]
         label = labels[label_name]
         return label
+
+    def update(self) -> None:
+        chat_active = app.window.is_chat_active(
+            self._account, self._contact.jid)
+        if chat_active:
+            self._client.get_module('SecLabels').get_catalog(
+                self._contact.jid.bare)



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/0ffb16e5749b2cbb3eb7b0e2cc578afd6e7621ca

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/0ffb16e5749b2cbb3eb7b0e2cc578afd6e7621ca
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to