From 5925b7ed2f3694cb51fe59e80da1fd9983395b65 Mon Sep 17 00:00:00 2001 From: wurstsalat Date: Tue, 16 Aug 2022 09:36:16 +0200 Subject: [now_listen] 1.4.2 --- now_listen/now_listen.py | 66 ++++++++++++++++++++++++++--------------- now_listen/plugin-manifest.json | 4 +-- 2 files changed, 44 insertions(+), 26 deletions(-) (limited to 'now_listen') diff --git a/now_listen/now_listen.py b/now_listen/now_listen.py index e307da3..3e43aaf 100644 --- a/now_listen/now_listen.py +++ b/now_listen/now_listen.py @@ -13,6 +13,10 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see . +from __future__ import annotations + +from typing import cast + import sys import logging from functools import partial @@ -20,6 +24,10 @@ from functools import partial from gi.repository import Gdk from gi.repository import GObject +from nbxmpp.structs import TuneData + +from gajim.gui.message_input import MessageInputTextView + from gajim.plugins import GajimPlugin from gajim.plugins.plugins_i18n import _ @@ -37,9 +45,9 @@ class NowListenPlugin(GajimPlugin): self.description = _('Copy tune info of playing music to conversation ' 'input box at cursor position (Alt + N)') self.config_dialog = partial(NowListenConfigDialog, self) - self.gui_extension_points = {'chat_control_base': - (self._on_connect_chat_control, - self._on_disconnect_chat_control)} + self.gui_extension_points = { + 'message_input': (self._on_message_input_created, None) + } self.config_default_values = { 'format_string': @@ -50,36 +58,46 @@ class NowListenPlugin(GajimPlugin): self.available_text = _('Plugin only available for Linux') self.activatable = False - self._event_ids = {} - - def _on_connect_chat_control(self, control): - signal_id = control.msg_textview.connect('key-press-event', - self._on_insert) - self._event_ids[control.control_id] = signal_id - - def _on_disconnect_chat_control(self, control): - signal_id = self._event_ids.pop(control.control_id) - if GObject.signal_handler_is_connected(control.msg_textview, signal_id): - control.msg_textview.disconnect(signal_id) - - def _get_tune_string(self, info): - format_string = self.config['format_string'] - tune_string = format_string.\ - replace('%artist', info.artist or '').\ - replace('%title', info.title or '') + self._signal_id = None + self._message_input = None + + def deactivate(self) -> None: + assert self._message_input is not None + assert self._signal_id is not None + if GObject.signal_handler_is_connected( + self._message_input, self._signal_id): + self._message_input.disconnect(self._signal_id) + + def _on_message_input_created(self, + message_input: MessageInputTextView + ) -> None: + + self._message_input = message_input + self._signal_id = message_input.connect( + 'key-press-event', self._on_key_press) + + def _get_tune_string(self, info: TuneData) -> str: + format_string = cast(str, self.config['format_string']) + tune_string = format_string.replace( + '%artist', info.artist or '').replace( + '%title', info.title or '') return tune_string - def _on_insert(self, textview, event): + def _on_key_press(self, + textview: MessageInputTextView, + event: Gdk.EventKey + ) -> bool: + # Insert text to message input box, at cursor position if event.keyval != Gdk.KEY_n: - return + return False if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N - return + return False info = MusicTrackListener.get().current_tune if info is None: log.info('No current tune available') - return + return False tune_string = self._get_tune_string(info) diff --git a/now_listen/plugin-manifest.json b/now_listen/plugin-manifest.json index 875db36..7e7bef9 100644 --- a/now_listen/plugin-manifest.json +++ b/now_listen/plugin-manifest.json @@ -11,8 +11,8 @@ "linux" ], "requirements": [ - "gajim>=1.4.0" + "gajim>=1.5.0" ], "short_name": "now_listen", - "version": "1.4.1" + "version": "1.4.2" } \ No newline at end of file -- cgit v1.2.3