005b9a5625a6a356c5f25d3758fbbc33d892658b
[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 = (None, None, p[1], p[0], False)
31             self.model.append (t)
32         print "Populate model (DONE):", time.time ()
33             
34         self.treeview.set_model (self.model)
35
36     def __create_view (self):
37         self.treeview = gtk.TreeView ()
38         self.treeview.connect ("row-activated", self.row_activated_cb)
39
40         artist_column = gtk.TreeViewColumn ("Artist", gtk.CellRendererText (), markup=0)
41         artist_column.set_expand (True)
42         self.treeview.append_column (artist_column)
43
44         renderer = gtk.CellRendererPixbuf ()
45         album_art = gtk.TreeViewColumn ("Album art", renderer, pixbuf=1)
46         # This doesn't have real effect:
47         album_art.set_sizing (gtk.TREE_VIEW_COLUMN_FIXED)
48         album_art.set_fixed_width (64)
49         
50         album_art.set_cell_data_func (renderer, self.album_art_cell_data_cb)
51         self.treeview.append_column (album_art)
52
53         pannable_area = hildon.PannableArea ()
54         pannable_area.add (self.treeview)
55         self.add (pannable_area)
56
57         # Menu
58         menu = hildon.AppMenu ()
59         automatic_retrieval = hildon.Button (hildon.BUTTON_STYLE_NORMAL,
60                                              hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
61         automatic_retrieval.set_title ("Automatic retrieval")
62         automatic_retrieval.connect ("clicked", self.get_all_album_art)
63         menu.append (automatic_retrieval)
64         menu.show_all ()
65         self.set_app_menu (menu)
66
67     def album_art_cell_data_cb (self, column, cell, model, iter):
68         text, pixbuf, artist, album, not_first_time = model.get (iter, 0, 1, 2, 3, 4)
69         if (not_first_time):
70             if (text == None):
71                 print "Setting text", album
72                 text = "".join (["<b>", escape_html (album),"</b>\n<small>",
73                                  escape_html(artist), "</small>"])
74                 model.set (iter, 0, text)
75             
76             if (pixbuf == None):
77                 #print "Calling album art cell data cb", model.get (iter, 3)
78                 album_art_path = getCoverArtThumbFileName (album)
79                 if (os.path.exists (album_art_path)):
80                     pxb = gtk.gdk.pixbuf_new_from_file_at_size (album_art_path, 64, 64)
81                     model.set (iter, 1, pxb)
82                 else:
83                     #print "Cannot find thumbnail in '%s'" % (album_art_path)
84                     model.set (iter, 1, EMPTY_PIXBUF) 
85                     
86         else:
87             model.set (iter, 4, True)
88
89     def get_all_album_art (self, user_data):
90         dialog = MussorgskyAlbumArtDownloadDialog (self)
91         dialog.show_all ()
92         dialog.do_the_job (self.model)
93
94     def row_activated_cb (self, treeview, path, view_colum):
95         it = treeview.get_model ().get_iter (path)
96         album = treeview.get_model ().get_value (it, 3)
97         artist = treeview.get_model ().get_value (it, 2)
98
99         dialog = AlbumArtSelectionDialog (self, artist, album, 5)
100         dialog.show_all ()
101         
102         response = dialog.run ()
103         if (response == RESPONSE_CLICK):
104             (img, thumb) = dialog.get_selection ()
105             if img and thumb:
106                 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 64, 64)
107                 treeview.get_model ().set (it, 1, pixbuf)
108             else:
109                 treeview.get_model ().set (it, 1, EMPTY_PIXBUF)
110         dialog.destroy ()
111
112             
113 if __name__ == "__main__":
114     import random
115     
116     artists_albums = [("Artist %d the greatest bolero singer in the universe" % i, "Album <%d>" % i) for i in range (0, 100)]
117
118     # Overwrite the get thumb path for testing
119     def local_file (path):
120         return "../thumb%d.124.jpeg" % (random.randint (0, 3))
121
122     global getCoverArtThumbFileName
123     getCoverArtThumbFileName = local_file
124
125     window = MussorgskyAlbumArtPanel (artists_albums)
126     window.connect ("destroy", gtk.main_quit )
127     window.show_all ()
128     gtk.main ()