forked from fossfreedom/coverart-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverart_browser.py
281 lines (227 loc) · 9.56 KB
/
coverart_browser.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- 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 GObject
from gi.repository import Gtk
from gi.repository import RB
from gi.repository import Peas
from gi.repository import Gio
from gi.repository import GLib
import rb
from coverart_browser_prefs import GSetting
from coverart_browser_prefs import CoverLocale
from coverart_browser_prefs import Preferences
from coverart_browser_source import CoverArtBrowserSource
from coverart_listview import ListView
from coverart_queueview import QueueView
from coverart_toolbar import TopToolbar
class CoverArtBrowserEntryType(RB.RhythmDBEntryType):
'''
Entry type for our source.
'''
def __init__(self):
'''
Initializes the entry type.
'''
RB.RhythmDBEntryType.__init__(self, name='CoverArtBrowserEntryType')
class CoverArtBrowserPlugin(GObject.Object, Peas.Activatable):
'''
Main class of the plugin. Manages the activation and deactivation of the
plugin.
'''
__gtype_name = 'CoverArtBrowserPlugin'
object = GObject.property(type=GObject.Object)
def __init__(self):
'''
Initialises the plugin object.
'''
GObject.Object.__init__(self)
def do_activate(self):
'''
Called by Rhythmbox when the plugin is activated. It creates the
plugin's source and connects signals to manage the plugin's
preferences.
'''
print("CoverArtBrowser DEBUG - do_activate")
self.shell = self.object
self.db = self.shell.props.db
self.entry_type = CoverArtBrowserEntryType()
self.db.register_entry_type(self.entry_type)
cl = CoverLocale()
cl.switch_locale(cl.Locale.LOCALE_DOMAIN)
self.entry_type.category = RB.RhythmDBEntryCategory.NORMAL
group = RB.DisplayPageGroup.get_by_id('library')
# load plugin icon
try:
theme = Gtk.IconTheme.get_default()
rb.append_plugin_source_path(theme, '/icons') # prior to rb3.2
except:
rb.append_plugin_source_path(self, '/icons') # rb3.2
iconfile = Gio.File.new_for_path(
rb.find_plugin_file(self, 'img/covermgr_rb3.png'))
self.source = CoverArtBrowserSource(
shell=self.shell,
name=_("CoverArt"),
entry_type=self.entry_type,
plugin=self,
icon=Gio.FileIcon.new(iconfile),
query_model=self.shell.props.library_source.props.base_query_model)
self.shell.register_entry_type_for_source(self.source, self.entry_type)
self.shell.append_display_page(self.source, group)
self.source.props.query_model.connect('complete', self.load_complete)
self._externalmenu = ExternalPluginMenu(self)
cl.switch_locale(cl.Locale.RB)
print("CoverArtBrowser DEBUG - end do_activate")
def do_deactivate(self):
'''
Called by Rhythmbox when the plugin is deactivated. It makes sure to
free all the resources used by the plugin.
'''
print("CoverArtBrowser DEBUG - do_deactivate")
self.source.delete_thyself()
if self._externalmenu:
self._externalmenu.cleanup()
del self.shell
del self.db
del self.source
print("CoverArtBrowser DEBUG - end do_deactivate")
def load_complete(self, *args, **kwargs):
'''
Called by Rhythmbox when it has completed loading all data
Used to automatically switch to the browser if the user
has set in the preferences
'''
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
if setting[gs.PluginKey.AUTOSTART]:
GLib.idle_add(self.shell.props.display_page_tree.select,
self.source)
def _translation_helper(self):
'''
a method just to help out with translation strings
it is not meant to be called by itself
'''
# define .plugin text strings used for translation
plugin = _('CoverArt Browser')
desc = _('Browse and play your albums through their covers')
# . TRANSLATORS: This is the icon-grid view that the user sees
tile = _('Tiles')
#. TRANSLATORS: This is the cover-flow view the user sees - they can swipe album covers from side-to-side
artist = _('Flow')
#. TRANSLATORS: percentage size that the image will be expanded
scale = _('Scale by %:')
# stop PyCharm removing the Preference import on optimisation
pref = Preferences()
class ExternalPluginMenu(GObject.Object):
toolbar_pos = GObject.property(type=str, default=TopToolbar.name)
def __init__(self, plugin):
super(ExternalPluginMenu, self).__init__()
self.plugin = plugin
self.shell = plugin.shell
self.source = plugin.source
self.app_id = None
from coverart_browser_source import Views
self._views = Views(self.shell)
self._connect_properties()
self._connect_signals()
self._create_menu()
def _connect_signals(self):
self.connect('notify::toolbar-pos', self._on_notify_toolbar_pos)
self.shell.props.display_page_tree.connect(
"selected", self.on_page_change
)
def _connect_properties(self):
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
setting.bind(gs.PluginKey.TOOLBAR_POS, self, 'toolbar_pos',
Gio.SettingsBindFlags.GET)
def _on_notify_toolbar_pos(self, *args):
if self.toolbar_pos == TopToolbar.name:
self._create_menu()
else:
self.cleanup()
def cleanup(self):
if self.app_id:
app = Gio.Application.get_default()
for location in self.locations:
app.remove_plugin_menu_item(location, self.app_id)
self.app_id = None
def _create_menu(self):
app = Gio.Application.get_default()
self.app_id = 'coverart-browser'
self.locations = ['library-toolbar', 'queue-toolbar', 'playsource-toolbar']
action_name = 'coverart-browser-views'
self.action = Gio.SimpleAction.new_stateful(
action_name, GLib.VariantType.new('s'),
self._views.get_action_name(ListView.name)
)
self.action.connect("activate", self.view_change_cb)
app.add_action(self.action)
menu_item = Gio.MenuItem()
section = Gio.Menu()
menu = Gio.Menu()
toolbar_item = Gio.MenuItem()
for view_name in self._views.get_view_names():
menu_item.set_label(self._views.get_menu_name(view_name))
menu_item.set_action_and_target_value(
'app.' + action_name, self._views.get_action_name(view_name)
)
section.append_item(menu_item)
menu.append_section(None, section)
cl = CoverLocale()
cl.switch_locale(cl.Locale.LOCALE_DOMAIN)
toolbar_item.set_label('…')
cl.switch_locale(cl.Locale.RB)
toolbar_item.set_submenu(menu)
for location in self.locations:
app.add_plugin_menu_item(location, self.app_id, toolbar_item)
def on_page_change(self, display_page_tree, page):
'''
Called when the display page changes. Grabs query models and sets the
active view.
'''
if page == self.shell.props.library_source:
self.action.set_state(self._views.get_action_name(ListView.name))
elif page == self.shell.props.queue_source:
self.action.set_state(self._views.get_action_name(QueueView.name))
# elif page == self.source.playlist_source:
# self.action.set_state(self._views.get_action_name(PlaySourceView.name))
def view_change_cb(self, action, current):
'''
Called when the view state on a page is changed. Sets the new
state.
'''
action.set_state(current)
view_name = self._views.get_view_name_for_action(current)
if view_name != ListView.name and \
view_name != QueueView.name: # and \
# view_name != PlaySourceView.name:
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
setting[gs.PluginKey.VIEW_NAME] = view_name
player = self.shell.props.shell_player
player.set_selected_source(self.source.playlist_source)
GLib.idle_add(self.shell.props.display_page_tree.select,
self.source)
elif view_name == ListView.name:
GLib.idle_add(self.shell.props.display_page_tree.select,
self.shell.props.library_source)
elif view_name == QueueView.name:
GLib.idle_add(self.shell.props.display_page_tree.select,
self.shell.props.queue_source)