Get correctly album name (to save the image in the right location)
[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 import i18n
15 _ = i18n.language.gettext
16
17 class MussorgskyAlbumArtPanel (hildon.StackableWindow):
18
19     def __init__ (self, album_artists):
20         hildon.StackableWindow.__init__ (self)
21         self.set_title (_("Album art selection"))
22         self.set_border_width (12)
23         self.__create_view ()
24         self.downloader = None
25         # Visible string, image, artist, album, painted!
26         self.model = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str, bool)
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             
33         self.treeview.set_model (self.model)
34
35     def __create_view (self):
36         self.treeview = gtk.TreeView ()
37         self.treeview.connect ("row-activated", self.row_activated_cb)
38
39         artist_column = gtk.TreeViewColumn ("Artist", gtk.CellRendererText (), markup=0)
40         artist_column.set_expand (True)
41         self.treeview.append_column (artist_column)
42
43         renderer = gtk.CellRendererPixbuf ()
44         album_art = gtk.TreeViewColumn ("Album art", renderer, pixbuf=1)
45         # This doesn't have real effect:
46         album_art.set_sizing (gtk.TREE_VIEW_COLUMN_FIXED)
47         album_art.set_fixed_width (64)
48         
49         album_art.set_cell_data_func (renderer, self.album_art_cell_data_cb)
50         self.treeview.append_column (album_art)
51
52         pannable_area = hildon.PannableArea ()
53         pannable_area.add (self.treeview)
54         self.add (pannable_area)
55
56         # Menu
57         menu = hildon.AppMenu ()
58         automatic_retrieval = hildon.Button (hildon.BUTTON_STYLE_NORMAL,
59                                              hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
60         automatic_retrieval.set_title (_("Automatic download"))
61         automatic_retrieval.connect ("clicked", self.get_all_album_art)
62         menu.append (automatic_retrieval)
63         menu.show_all ()
64         self.set_app_menu (menu)
65
66     def album_art_cell_data_cb (self, column, cell, model, iter):
67         text, pixbuf, artist, album, not_first_time = model.get (iter, 0, 1, 2, 3, 4)
68         if (not_first_time):
69             if (text == None):
70                 text = "".join (["<b>", escape_html (album),"</b>\n<small>",
71                                  escape_html(artist), "</small>"])
72                 model.set (iter, 0, text)
73             
74             if (pixbuf == None):
75                 #print "Calling album art cell data cb", model.get (iter, 3)
76                 album_art_path = getCoverArtThumbFileName (album)
77                 if (os.path.exists (album_art_path)):
78                     pxb = gtk.gdk.pixbuf_new_from_file_at_size (album_art_path, 64, 64)
79                     model.set (iter, 1, pxb)
80                 else:
81                     #print "Cannot find thumbnail in '%s'" % (album_art_path)
82                     model.set (iter, 1, EMPTY_PIXBUF) 
83                     
84         else:
85             model.set (iter, 4, True)
86
87     def get_all_album_art (self, user_data):
88         dialog = MussorgskyAlbumArtDownloadDialog (self)
89         dialog.show_all ()
90         dialog.do_the_job (self.model)
91
92     def row_activated_cb (self, treeview, path, view_colum):
93         it = treeview.get_model ().get_iter (path)
94         artist = treeview.get_model ().get_value (it, 2)
95         album = treeview.get_model ().get_value (it, 3)
96         if (artist.find ('|') != -1):
97             artist = "Various artists"
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|artist Y" % i, "Album <%d>" % i) for i in range (0, 10)]
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 ()