1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# -*- coding: utf-8 -*-
from gi.repository import Gtk
from gi.repository import Gdk
import os
from common import gajim
from plugins import GajimPlugin
from plugins.helpers import log_calls
from plugins.gui import GajimPluginConfigDialog
from plugins.plugin import GajimPluginException
from common import dbus_support
if dbus_support.supported:
from music_track_listener import MusicTrackListener
class NowListenPlugin(GajimPlugin):
@log_calls('NowListenPlugin')
def init(self):
self.description = _('Copy tune info to conversation input box '
'(alt + n) at cursor position')
self.config_dialog = NowListenPluginConfigDialog(self)
self.gui_extension_points = {'chat_control_base':
(self.connect_with_chat_control, self.disconnect_from_chat_control)}
self.config_default_values = {
'format_string': ('Now listen:"%title" by %artist from %album', ''),
'format_string_http': ('Now listen:"%title" by %artist', ''),}
self.controls = []
self.first_run = True
self.music_track_changed_signal = None
if os.name == 'nt':
self.available_text = _('Plugin can\'t be run under Windows.')
self.activatable = False
@log_calls('NowListenPlugin')
def connect_with_chat_control(self, chat_control):
self.chat_control = chat_control
control = Base(self, self.chat_control)
self.controls.append(control)
@log_calls('NowListenPlugin')
def disconnect_from_chat_control(self, chat_control):
for control in self.controls:
control.disconnect_from_chat_control()
self.controls = []
@log_calls('NowListenPlugin')
def activate(self):
if not dbus_support.supported:
raise GajimPluginException(_('python-dbus is missing!'))
listener = MusicTrackListener.get()
if not self.music_track_changed_signal:
self.music_track_changed_signal = listener.connect(
'music-track-changed', self.music_track_changed)
track = listener.get_playing_track()
self.music_track_changed(listener, track)
@log_calls('NowListenPlugin')
def deactivate(self):
if dbus_support.supported:
listener = MusicTrackListener.get()
if self.music_track_changed_signal:
listener.disconnect(self.music_track_changed_signal)
self.music_track_changed_signal = None
def music_track_changed(self, unused_listener, music_track_info,
account=None):
is_paused = hasattr(music_track_info, 'paused') and \
music_track_info.paused == 0
if not music_track_info or is_paused:
self.artist = self.title = self.source = ''
else:
self.artist = music_track_info.artist
self.title = music_track_info.title
self.source = music_track_info.album
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
self.plugin.artist = self.plugin.title = self.plugin.source = ''
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:
return
if not event.state & Gdk.ModifierType.MOD1_MASK: # ALT+N
return
if self.plugin.artist == self.plugin.title == self.plugin.source == '':
tune_string = 'paused or stopped'
else:
format_string = self.plugin.config['format_string']
if self.plugin.url and not self.plugin.url.startswith('file://'):
format_string = self.plugin.config['format_string_http']
tune_string = format_string.replace(
'%artist', self.plugin.artist).replace(
'%title', self.plugin.title).replace('%album',self.plugin.source).\
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
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_vbox'])
self.config_vbox = self.xml.get_object('now_listen_config_vbox')
self.get_child().pack_start(self.config_vbox, True, True, 0)
self.format_sting = self.xml.get_object('format_sting')
self.format_sting_http = self.xml.get_object('format_sting_http')
self.xml.connect_signals(self)
self.connect('hide', self.on_hide)
def on_run(self):
self.format_sting.set_text(self.plugin.config['format_string'])
self.format_sting_http.set_text(self.plugin.config['format_string_http'])
def on_hide(self, widget):
self.plugin.config['format_string'] = self.format_sting.get_text()
self.plugin.config['format_string_http'] = self.format_sting_http.get_text()
|