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