forked from fossfreedom/coverart-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverart_browser_source.py
223 lines (166 loc) · 8.21 KB
/
coverart_browser_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
# -*- 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.
import rb
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import RB
from gi.repository import GdkPixbuf
from coverart_album import AlbumLoader
from coverart_album import Album
class CoverArtBrowserSource(RB.Source):
def __init__( self ):
self.hasActivated = False
RB.Source.__init__( self,name="CoverArtBrowserPlugin" )
def do_set_property( self, property, value ):
if property.name == 'plugin':
self.plugin = value
def do_selected( self ):
self.do_impl_activate()
""" on source activation """
def do_impl_activate( self ):
# first time of activation -> add graphical stuff
if self.hasActivated:
return
# initialise some variables
self.plugin = self.props.plugin
self.shell = self.props.shell
#indicate that the source was activated before
self.hasActivated = True
# dialog has not been created so lets do so.
ui = Gtk.Builder()
ui.set_translation_domain('coverart_browser')
ui.add_from_file(rb.find_plugin_file(self.plugin, "coverart_browser.ui"))
ui.connect_signals( self )
# load the page and put it in the source
self.page = ui.get_object( 'main_box' )
self.pack_start( self.page, True, True, 0 )
# get widgets
self.status_label = ui.get_object( 'status_label' )
self.covers_view = ui.get_object( 'covers_view' )
self.search_entry = ui.get_object( 'search_entry' )
# set the model for the icon view
self.covers_model_store = Gtk.ListStore( GObject.TYPE_STRING,
GdkPixbuf.Pixbuf,
object )
self.covers_model = self.covers_model_store.filter_new()
self.covers_model.set_visible_func( self.visible_covers_callback )
self.covers_view.set_model( self.covers_model )
# size pixbuf updated workaround
self.covers_model_store.connect( 'row-changed',
self.update_iconview_callback )
# load the albums
self.loader = AlbumLoader( self.plugin, self.covers_model_store )
self.loader.load_albums()
print "CoverArtBrowser DEBUG - end show_browser_dialog"
def visible_covers_callback( self, model, iter, data ):
searchtext = self.search_entry.get_text()
if searchtext == "":
return True
return model[iter][2].contains( searchtext )
def icon_press_callback( self, entry, pos, event ):
if pos is Gtk.EntryIconPosition.SECONDARY:
entry.set_text( '' )
self.searchchanged_callback( entry )
def searchchanged_callback( self, gtk_entry ):
print "CoverArtBrowser DEBUG - searchchanged_callback"
self.covers_model.refilter()
print "CoverArtBrowser DEBUG - end searchchanged_callback"
def update_iconview_callback( self, *args ):
self.covers_view.set_columns( 0 )
self.covers_view.set_columns( -1 )
def play_menu_callback(self, _, album):
# callback when play an album
print "CoverArtBrowser DEBUG - play_menu_callback"
# clear the queue
play_queue = self.shell.props.queue_source
for row in play_queue.props.query_model:
play_queue.remove_entry(row[0])
self.queue_album( album )
# Start the music
player = self.shell.props.shell_player
player.stop()
player.set_playing_source( self.shell.props.queue_source )
player.playpause( True )
print "CoverArtBrowser DEBUG - end play_menu_callback"
def queue_album( self, album ):
# Retrieve and sort the entries of the album
songs = sorted( album.entries,
key=lambda song: song.get_ulong(
RB.RhythmDBPropType.TRACK_NUMBER) )
# Add the songs to the play queue
for song in songs:
self.shell.props.queue_source.add_entry( song, -1 )
def mouseclick_callback(self, iconview, event):
print "CoverArtBrowser DEBUG - mouseclick_callback()"
if event.button == 3:
x = int( event.x )
y = int( event.y )
time = event.time
pthinfo = iconview.get_path_at_pos( x, y )
if pthinfo is None:
return
iconview.grab_focus()
model = iconview.get_model()
album = model[pthinfo][2]
self.popup_menu = Gtk.Menu()
play_album_menu = Gtk.MenuItem( _("Play Album") )
play_album_menu.connect( "activate", self.play_menu_callback, album )
self.popup_menu.append( play_album_menu )
queue_album_menu = Gtk.MenuItem( _("Queue Album") )
queue_album_menu.connect( "activate", self.queue_menu_callback, album )
self.popup_menu.append( queue_album_menu )
cover_search_menu = Gtk.MenuItem( _("Search for covers") )
cover_search_menu.connect( "activate", self.cover_search_menu_callback, album )
self.popup_menu.append( cover_search_menu )
self.popup_menu.show_all()
self.popup_menu.popup( None, None, None, None, event.button, time )
print "CoverArtBrowser DEBUG - end mouseclick_callback()"
return
def cover_search_menu_callback( self, _, album ):
print "CoverArtBrowser DEBUG - cover_search_menu_callback()"
self.loader.search_cover_for_album( album )
print "CoverArtBrowser DEBUG - cover_search_menu_callback()"
def queue_menu_callback( self, _, album ):
print "CoverArtBrowser DEBUG - queue_menu_callback()"
self.queue_album( album )
print "CoverArtBrowser DEBUG - queue_menu_callback()"
def selectionchanged_callback( self, widget ):
print "CoverArtBrowser DEBUG - selectionchanged_callback"
# callback when focus had changed on an album
model = widget.get_model()
try:
album = model[widget.get_selected_items()[0]][2]
except:
self.status_label.set_label( '' )
return
# now lets build up a status label containing some 'interesting stuff' about the album
label = ('%s - %s' % ( album.name, album.artist )).decode('UTF-8')
# Calculate duration and number of tracks from that album
track_count = album.get_track_count()
duration = album.calculate_duration_in_mins()
if track_count == 1:
label += (_(' has 1 track')).decode('UTF-8')
else:
label+= (_(' has %d tracks') % track_count).decode('UTF-8')
if duration == 1:
label += (_(' and a duration of 1 minute')).decode('UTF-8')
else:
label += (_(' and a duration of %d minutes') % duration).decode('UTF-8')
self.status_label.set_label( label )
GObject.type_register(CoverArtBrowserSource)