Lazy loading of the album art when the row is visible
[mussorgsky] / src / album_art_panel.py
1 #!/usr/bin/env python2.5
2 import hildon
3 import gtk, gobject
4 import os
5 from album_art_spec import getCoverArtThumbFileName
6 from download_dialog import MussorgskyAlbumArtDownloadDialog
7 from utils import escape_html
8 from aa_selection_dialog import AlbumArtSelectionDialog, RESPONSE_CLICK
9
10 import time
11
12 EMPTY_PIXBUF = gtk.gdk.Pixbuf (gtk.gdk.COLORSPACE_RGB, False, 8, 64, 64)
13
14 class MussorgskyAlbumArtPanel (hildon.StackableWindow):
15
16     def __init__ (self, album_artists):
17         hildon.StackableWindow.__init__ (self)
18         self.set_title ("Album art handling")
19         self.set_border_width (12)
20         print "Create view       :",time.time ()
21         self.__create_view ()
22         print "Create view (DONE):", time.time ()
23         self.downloader = None
24         # Visible string, image, artist, album, painted!
25         self.model = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str, bool)
26         print "Populate model      :", time.time ()
27         for p in album_artists:
28             if (not p[0]):
29                 continue
30             t = ("".join (["<b>", escape_html (p[0]),"</b>\n<small>",
31                            escape_html(p[1]), "</small>"]), None, p[1], p[0], False)
32             self.model.append (t)
33         print "Populate model (DONE):", time.time ()
34             
35         self.treeview.set_model (self.model)
36
37     def __create_view (self):
38         self.treeview = gtk.TreeView ()
39         self.treeview.connect ("row-activated", self.row_activated_cb)
40
41         artist_column = gtk.TreeViewColumn ("Artist", gtk.CellRendererText (), markup=0)
42         artist_column.set_expand (True)
43         self.treeview.append_column (artist_column)
44
45         renderer = gtk.CellRendererPixbuf ()
46         album_art = gtk.TreeViewColumn ("Album art", renderer, pixbuf=1)
47         # This doesn't have real effect:
48         album_art.set_sizing (gtk.TREE_VIEW_COLUMN_FIXED)
49         album_art.set_fixed_width (64)
50         
51         album_art.set_cell_data_func (renderer, self.album_art_cell_data_cb)
52         self.treeview.append_column (album_art)
53
54         pannable_area = hildon.PannableArea ()
55         pannable_area.add (self.treeview)
56         self.add (pannable_area)
57
58         # Menu
59         menu = hildon.AppMenu ()
60         automatic_retrieval = hildon.Button (hildon.BUTTON_STYLE_NORMAL,
61                                              hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
62         automatic_retrieval.set_title ("Automatic retrieval")
63         automatic_retrieval.connect ("clicked", self.get_all_album_art)
64         menu.append (automatic_retrieval)
65         menu.show_all ()
66         self.set_app_menu (menu)
67
68     def album_art_cell_data_cb (self, column, cell, model, iter):
69         pixbuf, album, not_first_time = model.get (iter, 1, 3, 4)
70         if (not_first_time):
71             if (pixbuf == None):
72                 #print "Calling album art cell data cb", model.get (iter, 3)
73                 album_art_path = getCoverArtThumbFileName (album)
74                 if (os.path.exists (album_art_path)):
75                     pxb = gtk.gdk.pixbuf_new_from_file_at_size (album_art_path, 64, 64)
76                     model.set (iter, 1, pxb)
77                 else:
78                     #print "Cannot find thumbnail in '%s'" % (album_art_path)
79                     model.set (iter, 1, EMPTY_PIXBUF) 
80                     
81         else:
82             model.set (iter, 4, True)
83
84     def get_all_album_art (self, user_data):
85         dialog = MussorgskyAlbumArtDownloadDialog (self)
86         dialog.show_all ()
87         dialog.do_the_job (self.model)
88
89     def row_activated_cb (self, treeview, path, view_colum):
90         it = treeview.get_model ().get_iter (path)
91         album = treeview.get_model ().get_value (it, 3)
92         artist = treeview.get_model ().get_value (it, 2)
93
94         dialog = AlbumArtSelectionDialog (self, artist, album, 5)
95         dialog.show_all ()
96         
97         response = dialog.run ()
98         if (response == RESPONSE_CLICK):
99             (img, thumb) = dialog.get_selection ()
100             if img and thumb:
101                 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 64, 64)
102                 treeview.get_model ().set (it, 1, pixbuf)
103             else:
104                 treeview.get_model ().set (it, 1, EMPTY_PIXBUF)
105         dialog.destroy ()
106
107             
108 if __name__ == "__main__":
109     import random
110     
111     artists_albums = [("Artist %d the greatest bolero singer in the universe" % i, "Album <%d>" % i) for i in range (0, 10000)]
112
113     # Overwrite the get thumb path for testing
114     def local_file (path):
115         return "../thumb%d.124.jpeg" % (random.randint (0, 3))
116
117     global getCoverArtThumbFileName
118     getCoverArtThumbFileName = local_file
119
120     window = MussorgskyAlbumArtPanel (artists_albums)
121     window.connect ("destroy", gtk.main_quit )
122     window.show_all ()
123     gtk.main ()