[now_listen] 1.4.2

This commit is contained in:
wurstsalat
2022-08-16 09:36:16 +02:00
committed by Philipp Hörist
parent 397e093ace
commit 5925b7ed2f
2 changed files with 41 additions and 23 deletions

View File

@@ -13,6 +13,10 @@
# 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 Gajim. If not, see <http://www.gnu.org/licenses/>. # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import cast
import sys import sys
import logging import logging
from functools import partial from functools import partial
@@ -20,6 +24,10 @@ from functools import partial
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GObject 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 import GajimPlugin
from gajim.plugins.plugins_i18n import _ from gajim.plugins.plugins_i18n import _
@@ -37,9 +45,9 @@ class NowListenPlugin(GajimPlugin):
self.description = _('Copy tune info of playing music to conversation ' self.description = _('Copy tune info of playing music to conversation '
'input box at cursor position (Alt + N)') 'input box at cursor position (Alt + N)')
self.config_dialog = partial(NowListenConfigDialog, self) self.config_dialog = partial(NowListenConfigDialog, self)
self.gui_extension_points = {'chat_control_base': self.gui_extension_points = {
(self._on_connect_chat_control, 'message_input': (self._on_message_input_created, None)
self._on_disconnect_chat_control)} }
self.config_default_values = { self.config_default_values = {
'format_string': 'format_string':
@@ -50,36 +58,46 @@ class NowListenPlugin(GajimPlugin):
self.available_text = _('Plugin only available for Linux') self.available_text = _('Plugin only available for Linux')
self.activatable = False self.activatable = False
self._event_ids = {} self._signal_id = None
self._message_input = None
def _on_connect_chat_control(self, control): def deactivate(self) -> None:
signal_id = control.msg_textview.connect('key-press-event', assert self._message_input is not None
self._on_insert) assert self._signal_id is not None
self._event_ids[control.control_id] = signal_id if GObject.signal_handler_is_connected(
self._message_input, self._signal_id):
self._message_input.disconnect(self._signal_id)
def _on_disconnect_chat_control(self, control): def _on_message_input_created(self,
signal_id = self._event_ids.pop(control.control_id) message_input: MessageInputTextView
if GObject.signal_handler_is_connected(control.msg_textview, signal_id): ) -> None:
control.msg_textview.disconnect(signal_id)
def _get_tune_string(self, info): self._message_input = message_input
format_string = self.config['format_string'] self._signal_id = message_input.connect(
tune_string = format_string.\ 'key-press-event', self._on_key_press)
replace('%artist', info.artist or '').\
replace('%title', info.title or '') 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 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 # Insert text to message input box, at cursor position
if event.keyval != Gdk.KEY_n: if event.keyval != Gdk.KEY_n:
return return False
if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N
return return False
info = MusicTrackListener.get().current_tune info = MusicTrackListener.get().current_tune
if info is None: if info is None:
log.info('No current tune available') log.info('No current tune available')
return return False
tune_string = self._get_tune_string(info) tune_string = self._get_tune_string(info)

View File

@@ -11,8 +11,8 @@
"linux" "linux"
], ],
"requirements": [ "requirements": [
"gajim>=1.4.0" "gajim>=1.5.0"
], ],
"short_name": "now_listen", "short_name": "now_listen",
"version": "1.4.1" "version": "1.4.2"
} }