[length_notifier] Parse JIDs or domains and update counters completely

This commit is contained in:
Daniel Brötzmann
2020-05-01 13:32:33 +02:00
parent 5cabc7ad66
commit d68cde51c2
2 changed files with 43 additions and 26 deletions

View File

@@ -70,7 +70,7 @@ class LengthNotifierConfigDialog(SettingsDialog):
if isinstance(value, str): if isinstance(value, str):
value.strip() value.strip()
self.plugin.config[data] = value self.plugin.config[data] = value
self.plugin.update_settings() self.plugin.update()
class SizeSpinSetting(SpinSetting): class SizeSpinSetting(SpinSetting):

View File

@@ -28,6 +28,10 @@ from functools import partial
from gi.repository import Gtk from gi.repository import Gtk
from nbxmpp.protocol import JID
from gajim.common import app
from gajim.plugins import GajimPlugin from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _ from gajim.plugins.plugins_i18n import _
@@ -45,8 +49,8 @@ class LengthNotifierPlugin(GajimPlugin):
self.gui_extension_points = { self.gui_extension_points = {
'chat_control_base': ( 'chat_control_base': (
self._on_connect_chat_control, self._connect_chat_control,
self._on_disconnect_chat_control self._disconnect_chat_control
) )
} }
@@ -65,7 +69,7 @@ class LengthNotifierPlugin(GajimPlugin):
self._counters = {} self._counters = {}
def _on_connect_chat_control(self, chat_control): def _connect_chat_control(self, chat_control):
jid = chat_control.contact.jid jid = chat_control.contact.jid
if self._check_jid(jid): if self._check_jid(jid):
counter = Counter(chat_control, self.config) counter = Counter(chat_control, self.config)
@@ -74,25 +78,41 @@ class LengthNotifierPlugin(GajimPlugin):
actions_hbox.pack_start(counter, False, False, 0) actions_hbox.pack_start(counter, False, False, 0)
counter.show() counter.show()
def _on_disconnect_chat_control(self, chat_control): def _disconnect_chat_control(self, chat_control):
counter = self._counters.get(chat_control.control_id) counter = self._counters.get(chat_control.control_id)
if counter is not None: if counter is not None:
counter.reset()
counter.destroy() counter.destroy()
self._counters.pop(chat_control.control_id, None) self._counters.pop(chat_control.control_id, None)
def _check_jid(self, jid): def _check_jid(self, jid):
allowed_jids = [] if not self.config['JIDS']:
if len(self.config['JIDS']) > 0: # Not restricted to any JIDs
allowed_jids = self.config['JIDS'].split(',') return True
jid_allowed = jid in allowed_jids or not self.config['JIDS'] current_jid = JID(jid)
if not jid_allowed: allowed_jids = self.config['JIDS'].split(',')
log.debug('No counter for JID %s' % jid) for allowed_jid in allowed_jids:
return jid_allowed try:
address = JID(allowed_jid.strip())
except Exception as error:
log.debug('Error parsing JID: %s (%s)' % (error, allowed_jid))
continue
if address.isDomain:
if current_jid.getDomain() == address:
log.debug('Add counter for Domain %s' % address)
return True
if current_jid == address:
log.debug('Add counter for JID %s' % address)
return True
def update_settings(self): def update(self):
for counter in self._counters.values(): if not app.plugin_manager.get_active_plugin('length_notifier'):
counter.update_settings(self.config) # Dont update if the plugin is disabled
return
for control in app.interface.msg_win_mgr.get_controls():
self._disconnect_chat_control(control)
self._connect_chat_control(control)
class Counter(Gtk.Label): class Counter(Gtk.Label):
@@ -107,11 +127,11 @@ class Counter(Gtk.Label):
self._textview = self._control.msg_textview self._textview = self._control.msg_textview
self._textbuffer = self._textview.get_buffer() self._textbuffer = self._textview.get_buffer()
self._textbuffer.connect('changed', self._text_changed) self._textbuffer.connect('changed', self._update)
self._provider = None self._provider = None
self._set_count('0')
self._set_css() self._set_css()
self._update()
def _set_css(self): def _set_css(self):
self._context = self._textview.get_style_context() self._context = self._textview.get_style_context()
@@ -127,7 +147,10 @@ class Counter(Gtk.Label):
self._context.add_provider( self._context.add_provider(
self._provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) self._provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
def _text_changed(self, *args): def _set_count(self, count):
self.set_label(str(count))
def _update(self, *args):
if self._textview.has_text(): if self._textview.has_text():
text = self._textbuffer.get_text( text = self._textbuffer.get_text(
self._textbuffer.get_start_iter(), self._textbuffer.get_start_iter(),
@@ -143,11 +166,5 @@ class Counter(Gtk.Label):
self._set_count('0') self._set_count('0')
self._context.remove_class('length-warning') self._context.remove_class('length-warning')
def _set_count(self, count): def reset(self):
self.set_label(str(count)) self._context.remove_class('length-warning')
def update_settings(self, new_config):
self._max_length = new_config['MESSAGE_WARNING_LENGTH']
self._color = new_config['WARNING_COLOR']
self._set_css()
self._text_changed()