Commit 82bd54b9 authored by Philipp Hörist's avatar Philipp Hörist
Browse files

[now_listen] Refactor plugin

parent 0e099e30
...@@ -5,7 +5,6 @@ from gi.repository import Gtk ...@@ -5,7 +5,6 @@ from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gajim.plugins import GajimPlugin from gajim.plugins import GajimPlugin
from gajim.plugins.helpers import log_calls
from gajim.plugins.gui import GajimPluginConfigDialog from gajim.plugins.gui import GajimPluginConfigDialog
from gajim.plugins.plugins_i18n import _ from gajim.plugins.plugins_i18n import _
...@@ -16,14 +15,14 @@ log = logging.getLogger('gajim.p.now_listen') ...@@ -16,14 +15,14 @@ log = logging.getLogger('gajim.p.now_listen')
class NowListenPlugin(GajimPlugin): class NowListenPlugin(GajimPlugin):
@log_calls('NowListenPlugin')
def init(self): def init(self):
# pylint: disable=attribute-defined-outside-init
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 = NowListenPluginConfigDialog(self) self.config_dialog = NowListenPluginConfigDialog(self)
self.gui_extension_points = {'chat_control_base': self.gui_extension_points = {'chat_control_base':
(self.connect_with_chat_control, (self._on_connect_chat_control,
self.disconnect_from_chat_control)} self._on_disconnect_chat_control)}
self.config_default_values = { self.config_default_values = {
'format_string': 'format_string':
...@@ -31,115 +30,66 @@ class NowListenPlugin(GajimPlugin): ...@@ -31,115 +30,66 @@ class NowListenPlugin(GajimPlugin):
'format_string_http': 'format_string_http':
(_('Now listening to: "%title" by %artist'), ''), } (_('Now listening to: "%title" by %artist'), ''), }
self.controls = []
self.first_run = True
self.music_track_changed_signal = None
if os.name == 'nt': if os.name == 'nt':
self.available_text = _('Plugin cannot be run under Windows.') self.available_text = _('Plugin cannot be run under Windows.')
self.activatable = False self.activatable = False
@log_calls('NowListenPlugin') self._event_ids = {}
def connect_with_chat_control(self, chat_control): self._track_changed_id = None
self.chat_control = chat_control self._music_track_info = None
control = Base(self, self.chat_control)
self.controls.append(control)
@log_calls('NowListenPlugin') def _on_connect_chat_control(self, control):
def disconnect_from_chat_control(self, chat_control): signal_id = control.msg_textview.connect('key-press-event',
for control in self.controls: self._on_insert)
control.disconnect_from_chat_control() self._event_ids[control.control_id] = signal_id
self.controls = []
def _on_disconnect_chat_control(self, control):
signal_id = self._event_ids.pop(control.control_id)
# Raises a warning because the textview is already destroyed
# But for the deactivate() case this method is called for all active
# controls and in this case the textview is not destroyed
# We need someway to detect if the textview is already destroyed
control.msg_textview.disconnect(signal_id)
@log_calls('NowListenPlugin')
def activate(self): def activate(self):
listener = MusicTrackListener.get() listener = MusicTrackListener.get()
if not self.music_track_changed_signal: self._track_changed_id = listener.connect(
self.music_track_changed_signal = listener.connect( 'music-track-changed',
'music-track-changed', self.music_track_changed) self._on_music_track_changed)
try:
listener.start()
except AttributeError:
track = listener.get_playing_track('org.mpris.MediaPlayer2')
self.music_track_changed(listener, track)
listener.start()
@log_calls('NowListenPlugin')
def deactivate(self): def deactivate(self):
listener = MusicTrackListener.get() listener = MusicTrackListener.get()
if self.music_track_changed_signal: if self._track_changed_id is not None:
listener.disconnect(self.music_track_changed_signal) listener.disconnect(self._track_changed_id)
self.music_track_changed_signal = None self._track_changed_id = None
try:
listener.stop() def _on_music_track_changed(self, _listener, music_track_info):
except AttributeError: self._music_track_info = music_track_info
pass
def _get_tune_string(self):
def music_track_changed(self, unused_listener, music_track_info, format_string = self.config['format_string']
account=None): tune_string = format_string.\
replace('%artist', self._music_track_info.artist).\
if music_track_info is None or music_track_info.paused: replace('%title', self._music_track_info.title).\
self.artist = self.title = self.album = '' replace('%album', self._music_track_info.album)
else: return tune_string
self.artist = music_track_info.artist
self.title = music_track_info.title def _on_insert(self, textview, event):
self.album = music_track_info.album # Insert text to message input box, at cursor position
log.debug("%s - %s", self.artist, self.title)
if hasattr(music_track_info, 'url'):
self.url = music_track_info.url
self.albumartist = music_track_info.albumartist
else:
self.url = ''
class Base(object):
def __init__(self, plugin, chat_control):
self.plugin = plugin
self.chat_control = chat_control
if not hasattr(self.plugin, 'title'):
self.plugin.artist = self.plugin.title = self.plugin.album = ''
self.plugin.url = self.plugin.albumartist = ''
self.id_ = self.chat_control.msg_textview.connect('key_press_event',
self.on_insert)
self.chat_control.handlers[self.id_] = self.chat_control.msg_textview
def disconnect_from_chat_control(self):
if self.id_ not in self.chat_control.handlers:
return
if self.chat_control.handlers[self.id_].handler_is_connected(self.id_):
self.chat_control.handlers[self.id_].disconnect(self.id_)
del self.chat_control.handlers[self.id_]
def on_insert(self, widget, event):
"""
Insert text to conversation input box, at cursor position
"""
if event.keyval != Gdk.KEY_n: if event.keyval != Gdk.KEY_n:
return return
if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N
return return
if self.plugin.artist == self.plugin.title == self.plugin.album == '': if self._music_track_info is None:
tune_string = _('No music playing') return
else:
format_string = self.plugin.config['format_string'] tune_string = self._get_tune_string()
if self.plugin.url and not self.plugin.url.startswith('file://'):
format_string = self.plugin.config['format_string_http'] textview.get_buffer().insert_at_cursor(tune_string)
textview.grab_focus()
if self.plugin.artist is None:
self.plugin.artist = _('unknown artist')
if self.plugin.album is None:
self.plugin.album = _('unknown album')
tune_string = format_string.\
replace('%artist', self.plugin.artist).\
replace('%title', self.plugin.title).\
replace('%album', self.plugin.album).\
replace('%url', self.plugin.url)
message_buffer = self.chat_control.msg_textview.get_buffer()
message_buffer.insert_at_cursor(tune_string)
self.chat_control.msg_textview.grab_focus()
return True return True
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment