Add a menu entry instead of a switch stopgap [WIP]

This commit is contained in:
Bohdan Horbeshko 2022-11-09 03:36:24 +02:00
parent c745cb4b60
commit 75cc62153c

View file

@ -23,7 +23,8 @@ import logging
from gajim.plugins import GajimPlugin from gajim.plugins import GajimPlugin
from gajim.common import app from gajim.common import app
from gajim.common.modules.contacts import BareContact from gajim.common.modules.contacts import BareContact
from gi.repository import Gtk from gajim.gtk.util import make_menu_item
from gi.repository import Gtk, Gio
log = logging.getLogger('gajim.p.otr') log = logging.getLogger('gajim.p.otr')
@ -55,23 +56,25 @@ class OTRPlugin(GajimPlugin):
self.gui_extension_points = { self.gui_extension_points = {
'encrypt' + self.encryption_name: (self._encrypt_message, None), 'encrypt' + self.encryption_name: (self._encrypt_message, None),
'message_actions_box': (self._message_actions_box_activate, self._message_actions_box_deactivate), 'message_actions_box': (self._message_actions_box_activate, self._message_actions_box_deactivate),
'switch_contact': (self._switch_contact, None),
} }
self.switch = None self._menuitem = None
self.switch_box = None
@staticmethod @staticmethod
def _get_box(): def _get_grid():
return app.window.get_chat_stack().get_message_action_box()._ui.box return app.window.get_chat_stack().get_message_action_box()
@staticmethod
def _get_model(grid):
return grid._ui.encryption_menu_button.get_menu_model()
def activate(self): def activate(self):
if app.window is not None: if app.window is not None:
box = self._get_box() grid = self._get_grid()
self._actions_hack_activate(box) self._actions_hack_activate(grid)
def deactivate(self): def deactivate(self):
box = self._get_box() grid = self._get_grid()
self._actions_hack_deactivate(box) self._actions_hack_deactivate(grid)
@staticmethod @staticmethod
def get_otr(account): def get_otr(account):
@ -87,9 +90,6 @@ class OTRPlugin(GajimPlugin):
def encrypt_file(file, account, callback): def encrypt_file(file, account, callback):
callback(file) callback(file)
def toggle_otr(self):
print('toggle')
# encrypt message # # encrypt message #
def _encrypt_message(self,con,event,callback): def _encrypt_message(self,con,event,callback):
if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages
@ -97,37 +97,23 @@ class OTRPlugin(GajimPlugin):
otr.otr.encrypt(event,callback) otr.otr.encrypt(event,callback)
def _message_actions_box_activate(self, grid, box): def _message_actions_box_activate(self, grid, box):
if self.switch_box is None: if self._menuitem is None:
self._actions_hack_activate(box) self._actions_hack_activate(grid)
def _message_actions_box_deactivate(self, grid, box): def _message_actions_box_deactivate(self, grid, box):
if self.switch_box is not None: if self._menuitem is not None:
self._actions_hack_deactivate(box) self._actions_hack_deactivate(grid)
def _actions_hack_activate(self, box): def _actions_hack_activate(self, grid):
self.switch_box = Gtk.VBox() model = grid._ui.encryption_menu_button.get_menu_model()
label = Gtk.Label(label='OTR') menuitem = make_menu_item('OTR', 'win.set-encryption', '"OTR"')
self.switch = Gtk.Switch() self._menuitem = menuitem.get_attribute_value(Gio.MENU_ATTRIBUTE_LABEL)
self.switch_box.pack_start(label, False, True, 0) model.append_item(menuitem)
self.switch_box.pack_start(self.switch, False, True, 0)
self.switch_box.set_center_widget(self.switch)
box.pack_start(self.switch_box, False, True, 0)
self.switch.connect('activate', self.toggle_otr)
if app.window is not None and hasattr(app.window, '_chat_page'):
self._update_switch_visibility(app.window.get_chat_stack()._get_current_contact())
def _actions_hack_deactivate(self, box): def _actions_hack_deactivate(self, grid):
box.remove(self.switch_box) model = self._get_model(grid)
self.switch_box = None for i in range(model.get_n_items()):
self.switch = None if model.get_item_attribute_value(i, Gio.MENU_ATTRIBUTE_LABEL, None) == self._menuitem:
model.remove(i)
def _update_switch_visibility(self, contact): self._menuitem = None
if isinstance(contact, BareContact): break
otr = self.get_otr(contact._account)
self.switch.set_active(contact.jid.new_as_bare() in otr.otr.ctxs)
self.switch_box.show_all()
else:
self.switch_box.hide()
def _switch_contact(self, contact):
self._update_switch_visibility(contact)