diff --git a/now_listen/config_dialog.ui b/now_listen/config_dialog.ui deleted file mode 100644 index 27f2e96..0000000 --- a/now_listen/config_dialog.ui +++ /dev/null @@ -1,106 +0,0 @@ - - - - - - 400 - -1 - False - Now Listen Configuration - - - - - - True - False - 5 - 5 - 5 - 5 - 18 - vertical - 6 - - - True - False - start - Format string - - - - False - True - 3 - - - - - True - True - - - - True - True - 4 - - - - - True - False - start - Format string for non-local files (MPRIS2 only) - - - - False - True - 5 - - - - - True - True - - - - False - False - 6 - - - - - True - False - start - %title - title of the track being played -%artist - artist -%album - album - -%url - URL of the file, local files use the file://-schema - - - - False - True - 7 - - - - - - diff --git a/now_listen/gtk/__init__.py b/now_listen/gtk/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/now_listen/gtk/config.py b/now_listen/gtk/config.py new file mode 100644 index 0000000..b62f19f --- /dev/null +++ b/now_listen/gtk/config.py @@ -0,0 +1,42 @@ +# This file is part of Gajim. +# +# Gajim is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Gajim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gajim. If not, see . + +from gi.repository import Gtk + +from gajim.gtk.settings import SettingsDialog +from gajim.gtk.settings import SettingKind +from gajim.gtk.const import Setting +from gajim.gtk.const import SettingType + +from gajim.plugins.plugins_i18n import _ + + +class NowListenConfigDialog(SettingsDialog): + def __init__(self, plugin, parent): + + self.plugin = plugin + settings = [ + Setting(SettingKind.ENTRY, + _('Format string'), + SettingType.VALUE, + self.plugin.config['format_string'], + callback=self.on_setting, data='format_string') + ] + + SettingsDialog.__init__(self, parent, _('Now Listen Configuration'), + Gtk.DialogFlags.MODAL, settings, None) + + def on_setting(self, value, data): + self.plugin.config[data] = value diff --git a/now_listen/now_listen.py b/now_listen/now_listen.py index 947778e..e307da3 100644 --- a/now_listen/now_listen.py +++ b/now_listen/now_listen.py @@ -1,15 +1,32 @@ -import os -import logging +# This file is part of Gajim. +# +# Gajim is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Gajim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gajim. If not, see . + +import sys +import logging +from functools import partial -from gi.repository import Gtk from gi.repository import Gdk +from gi.repository import GObject from gajim.plugins import GajimPlugin -from gajim.plugins.gui import GajimPluginConfigDialog from gajim.plugins.plugins_i18n import _ from gajim.common.dbus.music_track import MusicTrackListener +from now_listen.gtk.config import NowListenConfigDialog + log = logging.getLogger('gajim.p.now_listen') @@ -19,24 +36,21 @@ class NowListenPlugin(GajimPlugin): # pylint: disable=attribute-defined-outside-init self.description = _('Copy tune info of playing music to conversation ' 'input box at cursor position (Alt + N)') - self.config_dialog = NowListenPluginConfigDialog(self) + self.config_dialog = partial(NowListenConfigDialog, self) self.gui_extension_points = {'chat_control_base': (self._on_connect_chat_control, self._on_disconnect_chat_control)} self.config_default_values = { 'format_string': - (_('Now listening to: "%title" by %artist from %album'), ''), - 'format_string_http': - (_('Now listening to: "%title" by %artist'), ''), } + (_('Now listening to: "%title" by %artist'), ''), + } - if os.name == 'nt': - self.available_text = _('Plugin cannot be run under Windows.') + if sys.platform != 'linux': + self.available_text = _('Plugin only available for Linux') self.activatable = False self._event_ids = {} - self._track_changed_id = None - self._music_track_info = None def _on_connect_chat_control(self, control): signal_id = control.msg_textview.connect('key-press-event', @@ -45,35 +59,14 @@ class NowListenPlugin(GajimPlugin): 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) + if GObject.signal_handler_is_connected(control.msg_textview, signal_id): + control.msg_textview.disconnect(signal_id) - def activate(self): - listener = MusicTrackListener.get() - self._track_changed_id = listener.connect( - 'music-track-changed', - self._on_music_track_changed) - - listener.start() - - def deactivate(self): - listener = MusicTrackListener.get() - if self._track_changed_id is not None: - listener.disconnect(self._track_changed_id) - self._track_changed_id = None - - def _on_music_track_changed(self, _listener, music_track_info): - self._music_track_info = music_track_info - - def _get_tune_string(self): + def _get_tune_string(self, info): format_string = self.config['format_string'] tune_string = format_string.\ - replace('%artist', self._music_track_info.artist).\ - replace('%title', self._music_track_info.title).\ - replace('%album', self._music_track_info.album) + replace('%artist', info.artist or '').\ + replace('%title', info.title or '') return tune_string def _on_insert(self, textview, event): @@ -83,37 +76,13 @@ class NowListenPlugin(GajimPlugin): if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N return - if self._music_track_info is None: + info = MusicTrackListener.get().current_tune + if info is None: + log.info('No current tune available') return - tune_string = self._get_tune_string() + tune_string = self._get_tune_string(info) textview.get_buffer().insert_at_cursor(tune_string) textview.grab_focus() return True - - -class NowListenPluginConfigDialog(GajimPluginConfigDialog): - def init(self): - self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path( - 'config_dialog.ui') - self.xml = Gtk.Builder() - self.xml.set_translation_domain('gajim_plugins') - self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH, - ['now_listen_config']) - - self.config_vbox = self.xml.get_object('now_listen_config') - self.get_child().pack_start(self.config_vbox, True, True, 0) - - self.format_string = self.xml.get_object('format_string') - self.format_string_http = self.xml.get_object('format_string_http') - self.xml.connect_signals(self) - self.connect('hide', self.on_hide) - - def on_run(self): - self.format_string.set_text(self.plugin.config['format_string']) - self.format_string_http.set_text(self.plugin.config['format_string_http']) - - def on_hide(self, widget): - self.plugin.config['format_string'] = self.format_string.get_text() - self.plugin.config['format_string_http'] = self.format_string_http.get_text()