/
coverart_play_source.py
258 lines (200 loc) · 8.83 KB
/
coverart_play_source.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2012 - fossfreedom
# Copyright (C) 2012 - Agustin Carrasco
#
# This program 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 2, or (at your option)
# any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
# define plugin
from gi.repository import Gtk
from gi.repository import RB
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gdk
from coverart_rb3compat import Menu
from coverart_external_plugins import CreateExternalPluginMenu
from coverart_entryview import CoverArtEntryView
from coverart_rb3compat import ActionGroup
from coverart_rb3compat import ApplicationShell
from coverart_browser_prefs import CoverLocale
from coverart_widgets import PressButton
from coverart_utils import create_button_image
import xml.etree.ElementTree as ET
import rb
import os
class CoverArtPlayEntryView(CoverArtEntryView):
__hash__ = GObject.__hash__
def __init__(self, shell, source):
'''
Initializes the entryview.
'''
super(CoverArtPlayEntryView, self).__init__(shell, source)
def define_menu(self):
popup = Menu(self.plugin, self.shell)
popup.load_from_file('N/A',
'ui/coverart_play_pop_rb3.ui')
signals = {
'remove_from_playlist_menu_item': self.remove_from_playlist_menu_item_callback
}
popup.connect_signals(signals)
popup.connect('pre-popup', self.pre_popup_menu_callback)
self.popup = popup
def pre_popup_menu_callback(self, *args):
'''
Callback when the popup menu is about to be displayed
'''
if not self.external_plugins:
self.external_plugins = \
CreateExternalPluginMenu("playlist_entry_view", 1, self.popup)
self.external_plugins.create_menu('play_popup_menu')
def remove_from_playlist_menu_item_callback(self, *args):
print("remove_from_playlist_menu_item_callback")
entries = self.get_selected_entries()
for entry in entries:
print(entry)
self.source.source_query_model.remove_entry(entry)
def do_show_popup(self, over_entry):
if over_entry:
print("CoverArtBrowser DEBUG - do_show_popup()")
self.popup.popup(self.source,
'play_popup_menu', 0, Gtk.get_current_event_time())
return over_entry
def play_track_menu_item_callback(self, *args):
print("CoverArtBrowser DEBUG - play_track_menu_item_callback()")
selected = self.get_selected_entries()
entry = selected[0]
# Start the music
player = self.shell.props.shell_player
player.play_entry(entry, self.source)
print("CoverArtBrowser DEBUG - play_track_menu_item_callback()")
class CoverArtPlaySource(RB.BrowserSource):
def __init__(self, **kwargs):
'''
Initializes the source.
'''
super(CoverArtPlaySource, self).__init__(**kwargs)
#self.external_plugins = None
self.hasActivated = False
self.save_in_progress = False
self.save_interrupt = False
self.filename = RB.user_cache_dir() + "/coverart_browser/playlist.xml"
def do_selected(self):
'''
Called by Rhythmbox when the source is selected. It makes sure to
create the ui the first time the source is showed.
'''
print("CoverArtBrowser DEBUG - do_selected")
# first time of activation -> add graphical stuff
if not self.hasActivated:
self.do_impl_activate()
# indicate that the source was activated before
self.hasActivated = True
print("CoverArtBrowser DEBUG - end do_selected")
def do_impl_activate(self):
'''
Called by do_selected the first time the source is activated.
It creates all the source ui and connects the necessary signals for it
correct behavior.
'''
print('do_impl_activate')
self.plugin = self.props.plugin
self.shell = self.props.shell
player = self.shell.props.shell_player
player.set_playing_source(self)
player.set_selected_source(self)
# define a query model that we'll use for playing
self.source_query_model = self.plugin.source_query_model
grid = Gtk.Grid()
self.entryview = self.get_entry_view()
child = self.get_children()
print (child)
grid = child[0]
self.rbsourcetoolbar = grid.get_children()[1] # need to remember the reference to stop crashes when python cleans up unlinked objects
grid.remove(grid.get_children()[1])
self.get_entry_view().set_model(self.source_query_model)
'''
# enable sorting on the entryview
entryview.set_columns_clickable(True)
self.shell.props.library_source.get_entry_view().set_columns_clickable(
True)
'''
cl = CoverLocale()
cl.switch_locale(cl.Locale.LOCALE_DOMAIN)
location = rb.find_plugin_file(self.plugin, 'ui/playsource-toolbar.ui')
ui = Gtk.Builder()
ui.set_translation_domain(cl.Locale.RB)
ui.add_from_file(location)
toolbar_menu = ui.get_object('playsource-toolbar')
app = self.shell.props.application
app.link_shared_menus(toolbar_menu)
self.toolbar = RB.ButtonBar.new(toolbar_menu, toolbar_menu)
self.toolbar.props.hexpand_set = False
grid.attach(self.toolbar, 0, 0, 1, 1)
grid.show_all()
appshell = ApplicationShell(self.shell)
action_group = ActionGroup(self.shell, 'PlaySourceActions')
action_group.add_action(func=self.clear_playsource,
action_name='playsource-clear', action_state=ActionGroup.STANDARD,
action_type='app')
action_group.add_action(func=self.shuffle_playsource,
action_name='playsource-shuffle', action_state=ActionGroup.STANDARD,
action_type='app')
appshell.insert_action_group(action_group)
# if the alternative-toolbar is loaded then lets connect to the toolbar-visibility signal
# to control our sources toolbar visibility
#if hasattr(self.shell, 'alternative_toolbar'):
# self.shell.alternative_toolbar.connect('toolbar-visibility', self._visibility)
self._load_model()
self.source_query_model.connect('row-inserted', self.save_changed_model)
self.source_query_model.connect('row-changed', self.save_changed_model)
self.source_query_model.connect('row-deleted', self.save_changed_model)
def _load_model(self):
if not os.path.isfile(self.filename):
return
parser = ET.XMLParser(encoding="utf-8")
tree = ET.parse(self.filename, parser=parser)
root = tree.getroot()
for child in root.findall('./entry/text'):
location = child.text
entry = self.shell.props.db.entry_lookup_by_location(location)
if entry:
self.source_query_model.add_entry(entry, -1)
self.props.query_model = self.source_query_model
def clear_playsource(self, *args):
for row in self.get_entry_view().props.model:
self.get_entry_view().props.model.remove_entry(row[0])
def shuffle_playsource(self, *args):
self.get_entry_view().props.model.shuffle_entries()
self._save_model()
def save_changed_model(self, *args):
if self.save_in_progress:
self.save_interrupt = True
return
self.save_in_progress = True
Gdk.threads_add_timeout_seconds(GLib.PRIORITY_DEFAULT_IDLE, 1, self._save_model, None)
def _save_model(self, *args):
if self.save_interrupt:
self.save_interrupt = False
return True
root = ET.Element('root')
element = ET.SubElement(root, 'entry')
for row in self.source_query_model:
location = row[0].get_string(RB.RhythmDBPropType.LOCATION)
subelement = ET.SubElement(element, 'text')
subelement.text = location
tree = ET.ElementTree(root)
tree.write(self.filename)
self.save_in_progress = False
return False
GObject.type_register(CoverArtPlayEntryView)