Be sure there is image and thumb
[mussorgsky] / src / album_art_panel.py
1 #!/usr/bin/env python2.5
2 import hildon
3 import gtk, gobject
4 from album_art_spec import getCoverArtThumbFileName
5 from download_dialog import MussorgskyAlbumArtDownloadDialog
6 from utils import escape_html
7 from aa_selection_dialog import AlbumArtSelectionDialog
8
9 class MussorgskyAlbumArtPanel (hildon.StackableWindow):
10
11     def __init__ (self, album_artists):
12         hildon.StackableWindow.__init__ (self)
13         self.set_title ("Album art handling")
14         self.set_border_width (12)
15         self.__create_view ()
16         self.downloader = None
17         self.model = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str)
18
19         for p in album_artists:
20             if (not p[1]):
21                 continue
22             album_art_path = getCoverArtThumbFileName (p[1])
23             try:
24                 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (album_art_path, 64, 64)
25             except gobject.GError:
26                 pixbuf = None
27             t = ("<b>%s</b>\n<small>%s</small>" % (escape_html(p[1]), escape_html(p[0])), pixbuf, p[0], p[1])
28             self.model.append (t)
29             
30         self.treeview.set_model (self.model)
31
32     def __create_view (self):
33         vbox = gtk.VBox (spacing=12, homogeneous=False)
34
35         self.treeview = gtk.TreeView ()
36         self.treeview.connect ("row-activated", self.row_activated_cb)
37
38         artist_column = gtk.TreeViewColumn ("Artist", gtk.CellRendererText (), markup=0)
39         artist_column.set_expand (True)
40         self.treeview.append_column (artist_column)
41
42         album_art = gtk.TreeViewColumn ("Album art", gtk.CellRendererPixbuf (), pixbuf=1)
43         self.treeview.append_column (album_art)
44
45         #vbox.add (self.treeview)
46
47         pannable_area = hildon.PannableArea ()
48         pannable_area.add (self.treeview)
49         self.add (pannable_area)
50
51         # Menu
52         menu = hildon.AppMenu ()
53         automatic_retrieval = hildon.Button (hildon.BUTTON_STYLE_NORMAL,
54                                              hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
55         automatic_retrieval.set_title ("Automatic retrieval")
56         automatic_retrieval.connect ("clicked", self.get_all_album_art)
57         menu.append (automatic_retrieval)
58         menu.show_all ()
59         self.set_app_menu (menu)
60
61     def get_all_album_art (self, user_data):
62         dialog = MussorgskyAlbumArtDownloadDialog (self)
63         dialog.show_all ()
64         dialog.do_the_job (self.model)
65
66     def row_activated_cb (self, treeview, path, view_colum):
67         it = treeview.get_model ().get_iter (path)
68         album = treeview.get_model ().get_value (it, 3)
69         artist = treeview.get_model ().get_value (it, 2)
70
71         dialog = AlbumArtSelectionDialog (self, artist, album, 5)
72         dialog.show_all ()
73         
74         response = dialog.run ()
75         if (response > -1):
76             (img, thumb) = dialog.get_selection ()
77             if img and thumb:
78                 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 64, 64)
79                 treeview.get_model ().set (it, 1, pixbuf)
80         dialog.destroy ()
81             
82 if __name__ == "__main__":
83
84     artists_albums = [("Artist %d the greatest bolero singer in the universe" % i, "Album <%d>" % i) for i in range (0, 100)]
85
86
87     window = MussorgskyAlbumArtPanel (artists_albums)
88     window.connect ("destroy", gtk.main_quit )
89     window.show_all ()
90     gtk.main ()