[now_listen] Rewrite Plugin

- Add new Configuration dialog
- Adapt to Gajim changes
This commit is contained in:
lovetox
2020-05-01 20:11:19 +02:00
parent c44fe6212d
commit 6c9288a10f
4 changed files with 77 additions and 172 deletions

View File

@@ -1,106 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="config_window">
<property name="width_request">400</property>
<property name="height_request">-1</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Now Listen Configuration</property>
<child type="titlebar">
<placeholder/>
</child>
<child>
<object class="GtkBox" id="now_listen_config">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="border_width">18</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Format string</property>
<style>
<class name="bold"/>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="format_string">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Format string for non-local files (MPRIS2 only)</property>
<style>
<class name="bold"/>
<class name="dim-label"/>
<class name="margin-top6"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="format_string_http">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">6</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">%title - title of the track being played
%artist - artist
%album - album
%url - URL of the file, local files use the file://-schema</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">7</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

42
now_listen/gtk/config.py Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
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

View File

@@ -1,15 +1,32 @@
import os # This file is part of Gajim.
import logging #
# 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 <http://www.gnu.org/licenses/>.
import sys
import logging
from functools import partial
from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GObject
from gajim.plugins import GajimPlugin from gajim.plugins import GajimPlugin
from gajim.plugins.gui import GajimPluginConfigDialog
from gajim.plugins.plugins_i18n import _ from gajim.plugins.plugins_i18n import _
from gajim.common.dbus.music_track import MusicTrackListener from gajim.common.dbus.music_track import MusicTrackListener
from now_listen.gtk.config import NowListenConfigDialog
log = logging.getLogger('gajim.p.now_listen') log = logging.getLogger('gajim.p.now_listen')
@@ -19,24 +36,21 @@ class NowListenPlugin(GajimPlugin):
# pylint: disable=attribute-defined-outside-init # 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 = partial(NowListenConfigDialog, self)
self.gui_extension_points = {'chat_control_base': self.gui_extension_points = {'chat_control_base':
(self._on_connect_chat_control, (self._on_connect_chat_control,
self._on_disconnect_chat_control)} self._on_disconnect_chat_control)}
self.config_default_values = { self.config_default_values = {
'format_string': 'format_string':
(_('Now listening to: "%title" by %artist from %album'), ''), (_('Now listening to: "%title" by %artist'), ''),
'format_string_http': }
(_('Now listening to: "%title" by %artist'), ''), }
if os.name == 'nt': if sys.platform != 'linux':
self.available_text = _('Plugin cannot be run under Windows.') self.available_text = _('Plugin only available for Linux')
self.activatable = False self.activatable = False
self._event_ids = {} self._event_ids = {}
self._track_changed_id = None
self._music_track_info = None
def _on_connect_chat_control(self, control): def _on_connect_chat_control(self, control):
signal_id = control.msg_textview.connect('key-press-event', signal_id = control.msg_textview.connect('key-press-event',
@@ -45,35 +59,14 @@ class NowListenPlugin(GajimPlugin):
def _on_disconnect_chat_control(self, control): def _on_disconnect_chat_control(self, control):
signal_id = self._event_ids.pop(control.control_id) signal_id = self._event_ids.pop(control.control_id)
# Raises a warning because the textview is already destroyed if GObject.signal_handler_is_connected(control.msg_textview, signal_id):
# But for the deactivate() case this method is called for all active control.msg_textview.disconnect(signal_id)
# 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)
def activate(self): def _get_tune_string(self, info):
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):
format_string = self.config['format_string'] format_string = self.config['format_string']
tune_string = format_string.\ tune_string = format_string.\
replace('%artist', self._music_track_info.artist).\ replace('%artist', info.artist or '').\
replace('%title', self._music_track_info.title).\ replace('%title', info.title or '')
replace('%album', self._music_track_info.album)
return tune_string return tune_string
def _on_insert(self, textview, event): def _on_insert(self, textview, event):
@@ -83,37 +76,13 @@ class NowListenPlugin(GajimPlugin):
if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N
return return
if self._music_track_info is None: info = MusicTrackListener.get().current_tune
if info is None:
log.info('No current tune available')
return return
tune_string = self._get_tune_string() tune_string = self._get_tune_string(info)
textview.get_buffer().insert_at_cursor(tune_string) textview.get_buffer().insert_at_cursor(tune_string)
textview.grab_focus() textview.grab_focus()
return True 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()