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
|
# -*- coding: utf-8 -*-
import gtk
import gobject
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', '')}
self.controls = []
self.first_run = True
self.music_track_changed_signal = None
@log_calls('NowListenPlugin')
def connect_with_chat_control(self, chat_control):
#if not dbus_support.supported:
#return
if self.first_run:
# ALT + N
gtk.binding_entry_add_signal(chat_control.msg_textview,
gtk.keysyms.n, gtk.gdk.MOD1_MASK, 'mykeypress',
int, gtk.keysyms.n, gtk.gdk.ModifierType, gtk.gdk.MOD1_MASK)
self.first_run = False
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 hasattr(self, 'chat_control'):
gtk.binding_entry_remove(self.chat_control.msg_textview,
gtk.keysyms.n, gtk.gdk.MOD1_MASK)
self.first_run = True
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
class Base(object):
def __init__(self, plugin, chat_control):
self.plugin = plugin
self.chat_control = chat_control
self.id_ = chat_control.msg_textview.connect('mykeypress',
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_keyval, event_keymod):
"""
Insert text to conversation input box, at cursor position
"""
# construct event instance from binding
event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # it's always a key-press here
event.keyval = event_keyval
event.state = event_keymod
event.time = 0 # assign current time
if event.keyval != gtk.keysyms.n:
return
if event.state != gtk.gdk.MOD1_MASK: # ALT+N
return
if self.plugin.artist == self.plugin.title == self.plugin.source == '':
tune_string = 'paused or stopped'
else:
tune_string = self.plugin.config['format_string'].replace(
'%artist', self.plugin.artist).replace(
'%title', self.plugin.title).replace('%album',self.plugin.source)
message_buffer = self.chat_control.msg_textview.get_buffer()
message_buffer.insert_at_cursor(tune_string)
self.chat_control.msg_textview.grab_focus()
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.child.pack_start(self.config_vbox)
self.format_sting = self.xml.get_object('format_sting')
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'])
def on_hide(self, widget):
self.plugin.config['format_string'] = self.format_sting.get_text()
|