[acronyms] 1.4.5

This commit is contained in:
wurstsalat
2022-08-16 09:34:52 +02:00
committed by Philipp Hörist
parent 8fccd261e8
commit 4003411ae5
2 changed files with 47 additions and 27 deletions

View File

@@ -14,6 +14,7 @@
#
# You should have received a copy of the GNU General Public License
# along with Acronyms Expander. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import json
import logging
@@ -21,9 +22,14 @@ from pathlib import Path
from functools import partial
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gtk
from gajim.common import configpaths
from gajim.common import types
from gajim.common.modules.contacts import GroupchatContact
from gajim.gui.message_input import MessageInputTextView
from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _
@@ -35,21 +41,25 @@ log = logging.getLogger('gajim.p.acronyms')
class AcronymsExpanderPlugin(GajimPlugin):
def init(self):
def init(self) -> None:
self.description = _('Replaces acronyms (or other strings) '
'with given expansions/substitutes.')
self.config_dialog = partial(ConfigDialog, self)
self.gui_extension_points = {
'chat_control_base': (self._connect, self._disconnect)
'message_input': (self._connect, None),
'switch_contact': (self._on_switch_contact, None)
}
self._invoker = ' '
self._replace_in_progress = False
self._handler_ids = {}
self._signal_id = None
self._text_buffer = None
self._contact = None
self.acronyms = self._load_acronyms()
@staticmethod
def _load_acronyms():
def _load_acronyms() -> dict[str, str]:
try:
data_path = Path(configpaths.get('PLUGINS_DATA'))
except KeyError:
@@ -65,7 +75,7 @@ class AcronymsExpanderPlugin(GajimPlugin):
return acronyms
@staticmethod
def _save_acronyms(acronyms):
def _save_acronyms(acronyms: dict[str, str]) -> None:
try:
data_path = Path(configpaths.get('PLUGINS_DATA'))
except KeyError:
@@ -80,11 +90,14 @@ class AcronymsExpanderPlugin(GajimPlugin):
with filepath.open('w') as file:
json.dump(acronyms, file)
def set_acronyms(self, acronyms):
def set_acronyms(self, acronyms: dict[str, str]) -> None:
self.acronyms = acronyms
self._save_acronyms(acronyms)
def _on_buffer_changed(self, buffer_, contact, account):
def _on_buffer_changed(self,
buffer_: Gtk.TextBuffer
) -> None:
if self._replace_in_progress:
return
@@ -118,13 +131,14 @@ class AcronymsExpanderPlugin(GajimPlugin):
# Get last word and cut invoker
last_word = word_start_iter.get_slice(insert_iter)
if contact.is_groupchat:
if last_word in contact.get_user_nicknames():
assert self._contact is not None
if isinstance(self._contact, GroupchatContact):
if last_word in self._contact.get_user_nicknames():
log.info('Groupchat participant has same nick as acronym')
return
if contact.is_pm_contact:
if last_word == contact.name:
if self._contact.is_pm_contact:
if last_word == self._contact.name:
log.info('Contact name equals acronym')
return
@@ -139,23 +153,29 @@ class AcronymsExpanderPlugin(GajimPlugin):
insert_iter,
substitute)
def _replace_text(self, buffer_, start, end, substitute):
def _replace_text(self,
buffer_: Gtk.TextBuffer,
start: Gtk.TextIter,
end: Gtk.TextIter,
substitute: str
) -> None:
self._replace_in_progress = True
buffer_.delete(start, end)
buffer_.insert(start, substitute)
self._replace_in_progress = False
def _connect(self, chat_control):
text_buffer = chat_control.msg_textview.get_buffer()
handler_id = text_buffer.connect('changed',
self._on_buffer_changed,
chat_control.contact,
chat_control.account)
self._handler_ids[id(text_buffer)] = handler_id
def _on_switch_contact(self, contact: types.ChatContactT) -> None:
self._contact = contact
def _disconnect(self, chat_control):
text_buffer = chat_control.msg_textview.get_buffer()
handler_id = self._handler_ids.get(id(text_buffer))
if handler_id is not None:
text_buffer.disconnect(handler_id)
del self._handler_ids[id(text_buffer)]
def _connect(self, message_input: MessageInputTextView) -> None:
self._text_buffer = message_input.get_buffer()
self._signal_id = self._text_buffer.connect(
'changed', self._on_buffer_changed)
def deactivate(self) -> None:
assert self._text_buffer is not None
assert self._signal_id is not None
if GObject.signal_handler_is_connected(
self._text_buffer, self._signal_id):
self._text_buffer.disconnect(self._signal_id)