Debian Codegraph @

xlgui/panel/lyrics.py

raw
1 # Copyright (C) 2009-2010 Aren Olson
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 #
18 # The developers of the Exaile media player hereby grant permission
19 # for non-GPL compatible GStreamer and Exaile plugins to be used and
20 # distributed together with GStreamer and Exaile. This permission is
21 # above and beyond the permissions granted by the GPL license by which
22 # Exaile is covered. If you modify this code, you may extend this
23 # exception to your version of the code, but you are not obligated to
24 # do so. If you do not wish to do so, delete this exception statement
25 # from your version.
26
27 from gi.repository import Gtk
28 from gi.repository import GLib
29
30 from xl.nls import gettext as _
31 from xl import common, event, player, providers
32 from xl import settings as xl_settings
33 from xl import lyrics as xl_lyrics
34 from xlgui import guiutil, panel
35 from xlgui.preferences import lyrics as lyricsprefs
36
37
38 class LyricsPanel(panel.Panel):
39 # public variable for xlgui.panel.Panel
40 ui_info = ('lyrics.ui', 'LyricsPanel')
41
42 def __init__(self, parent, name):
43 panel.Panel.__init__(self, parent, name, _('Lyrics'))
44
45 self.__lyrics_found = []
46 self.__css_provider = Gtk.CssProvider()
47
48 WIDGET_LIST = [
49 'lyrics_top_box',
50 'refresh_button',
51 'refresh_button_stack',
52 'refresh_icon',
53 'refresh_spinner',
54 'track_text',
55 'scrolled_window',
56 'lyrics_text',
57 'lyrics_source_label',
58 'track_text_buffer',
59 'lyrics_text_buffer',
60 ]
61 for name in WIDGET_LIST:
62 setattr(
63 self,
64 '_' + LyricsPanel.__name__ + '__' + name,
65 self.builder.get_object(name),
66 )
67 self.__initialize_widgets()
68
69 event.add_ui_callback(self.__on_playback_track_start, 'playback_track_start')
70 event.add_ui_callback(self.__on_track_tags_changed, 'track_tags_changed')
71 event.add_ui_callback(self.__on_playback_player_end, 'playback_player_end')
72 event.add_ui_callback(
73 self.__on_lyrics_search_method_added, 'lyrics_search_method_added'
74 )
75 event.add_ui_callback(self.__on_option_set, 'plugin_lyricsviewer_option_set')
76
77 self.__update_lyrics()
78
79 def __initialize_widgets(self):
80 lyrics_methods_combo = LyricsMethodsComboBox()
81 self.__lyrics_top_box.pack_start(lyrics_methods_combo, True, True, 0)
82 lyrics_methods_combo.connect('changed', self.__on_combo_active_changed)
83 lyrics_methods_combo.show()
84 self.__lyrics_methods_combo = lyrics_methods_combo
85
86 track_text_style = Gtk.CssProvider()
87 style_context = self.__track_text.get_style_context()
88 style_context.add_provider(
89 track_text_style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
90 )
91 track_text_style.load_from_data(b"textview {font-weight: bold; }")
92
93 style_context = self.__lyrics_text.get_style_context()
94 style_context.add_provider(
95 self.__css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
96 )
97
98 lyricsprefs.DEFAULT_FONT = (
99 self.__lyrics_text.get_default_attributes().font.to_string()
100 )
101 # trigger initial setup through options
102 self.__on_option_set(None, xl_settings, 'plugin/lyricsviewer/lyrics_font')
103
104 self.__refresh_button.connect('clicked', self.__on_refresh_button_clicked)
105
106 def __on_option_set(self, _event, settings, option):
107 if option == 'plugin/lyricsviewer/lyrics_font':
108 pango_font_str = settings.get_option(option)
109 css_from_pango = guiutil.css_from_pango_font_description(pango_font_str)
110 data_str = "textview { " + css_from_pango + "; }\n"
111 self.__css_provider.load_from_data(data_str.encode('utf-8'))
112
113 def __on_lyrics_search_method_added(self, _eventtype, _lyrics, _provider):
114 self.__update_lyrics()
115
116 def __on_track_tags_changed(self, _eventtype, track, tags):
117 if player.PLAYER.current == track and tags & {"artist", "title"}:
118 self.__update_lyrics()
119
120 def __on_playback_track_start(self, _eventtype, _player, _data):
121 self.__update_lyrics()
122
123 def __on_playback_player_end(self, _eventtype, _player, _data):
124 self.__update_lyrics()
125
126 def __on_refresh_button_clicked(self, _button):
127 """
128 Called when the refresh button is clicked
129 """
130 self.__update_lyrics(refresh=True)
131
132 def __on_combo_active_changed(self, _combobox):
133 """
134 Catches when the user selects an item of the combo.
135 """
136 if self.__lyrics_found:
137 self.__update_lyrics_text()
138
139 def __update_lyrics(self, refresh=False):
140 self.__track_text_buffer.set_text("")
141 self.__lyrics_text_buffer.set_text("")
142 self.__lyrics_source_label.set_text("")
143 self.__lyrics_found = []
144 if player.PLAYER.current:
145 self.__set_top_box_widgets(False)
146 self.__get_lyrics(player.PLAYER.current, refresh)
147 else:
148 self.__lyrics_text_buffer.set_text(_('Not playing.'))
149 self.__set_top_box_widgets(False, True)
150
151 @common.threaded
152 def __get_lyrics(self, track, refresh=False):
153 lyrics_found = []
154 track_text = ''
155 try:
156 try:
157 track_text = (
158 track.get_tag_raw('artist')[0]
159 + " - "
160 + track.get_tag_raw('title')[0]
161 )
162 except Exception:
163 raise xl_lyrics.LyricsNotFoundException
164 lyrics_found = xl_lyrics.MANAGER.find_all_lyrics(track, refresh)
165 except xl_lyrics.LyricsNotFoundException:
166 lyrics_found = []
167 finally:
168 self.__get_lyrics_finish(track, track_text, lyrics_found)
169
170 @common.idle_add()
171 def __get_lyrics_finish(self, track, track_text, lyrics_found):
172 '''Only called from __get_lyrics thread, thunk to ui thread'''
173
174 if track != player.PLAYER.current:
175 return
176
177 self.__lyrics_found = lyrics_found
178
179 self.__track_text_buffer.set_text(track_text)
180 self.__update_lyrics_text()
181 self.__set_top_box_widgets(True)
182
183 def __update_lyrics_text(self):
184 lyrics = _("No lyrics found.")
185 source = ""
186 url = ""
187 if self.__lyrics_found:
188 (index, selected_method) = self.__lyrics_methods_combo.get_active_item()
189 for name, i_lyrics, i_source, i_url in self.__lyrics_found:
190 if name == selected_method or index == 0:
191 lyrics, source, url = i_lyrics, i_source, i_url
192 break
193 self.__lyrics_text_buffer.set_text(lyrics)
194
195 if url != "":
196 url_text = '<a href="' + url + '">' + source + '</a>'
197 self.__lyrics_source_label.set_markup(_("Source: ") + url_text)
198 else:
199 self.__lyrics_source_label.set_text("")
200
201 def __set_top_box_widgets(self, state, init=False):
202 if state or init:
203 self.__refresh_spinner.stop()
204 self.__refresh_button_stack.set_visible_child(self.__refresh_icon)
205 else:
206 self.__refresh_button_stack.set_visible_child(self.__refresh_spinner)
207 self.__refresh_spinner.start()
208
209 self.__refresh_button.set_sensitive(state)
210 self.__lyrics_methods_combo.set_sensitive(state)
211
212
213 class LyricsMethodsComboBox(Gtk.ComboBoxText, providers.ProviderHandler):
214 """
215 An extended Gtk.ComboBox class.
216 Shows lyrics methods search registered
217 """
218
219 def __init__(self):
220 Gtk.ComboBoxText.__init__(self)
221 providers.ProviderHandler.__init__(self, 'lyrics')
222
223 self.model = self.get_model()
224 # Default value, any registered lyrics provider
225 self.append_text(_("Any source"))
226
227 for provider in self.get_providers():
228 self.append_text(provider.display_name)
229
230 self.set_active(0)
231
232 def remove_item(self, name):
233 index = self.search_item(name)
234 if index:
235 GLib.idle_add(self.remove, index)
236 return True
237 return False
238
239 def append_item(self, name):
240 if not self.search_item(name):
241 GLib.idle_add(self.append_text, name)
242 return True
243 return False
244
245 def search_item(self, name):
246 index = 0
247 for item in self.model:
248 if item[0] == name:
249 return index
250 index += 1
251 return False
252
253 def get_active_item(self):
254 return self.get_active(), self.get_active_text()
255
256 def on_provider_added(self, provider):
257 self.append_item(provider.display_name)
258
259 def on_provider_removed(self, provider):
260 self.remove_item(provider.display_name)