Handle exception when thumbnail doesnt exist
[mussorgsky] / src / download_dialog.py
1 #!/usr/bin/env python2.5
2 import gtk, gobject
3 from album_art import MussorgskyAlbumArt
4
5 class MussorgskyAlbumArtDownloadDialog (gtk.Dialog):
6
7     def __init__ (self, parent):
8         gtk.Dialog.__init__ (self,
9                              "Downloading album art", parent,
10                              gtk.DIALOG_DESTROY_WITH_PARENT,
11                              (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)
12                         )
13         self.downloader = MussorgskyAlbumArt ()
14         self.set_title ("Downloading album art")
15         self.connect ("response", self.handle_response)
16         self.create_view ()
17         self.cancel = False
18
19     def create_view (self):
20
21         hbox = gtk.HBox (homogeneous=False)
22
23         self.album_art = gtk.Image ()
24         self.album_art.set_size_request (124, 124)
25         
26         hbox.pack_start (self.album_art, expand=False, fill=True)
27
28         labels = gtk.VBox ()
29         self.status_label = gtk.Label ("")
30         labels.pack_start (self.status_label)
31         self.current_label = gtk.Label ("")
32         labels.pack_start (self.current_label)
33
34         hbox.pack_start (labels, expand=True, fill=False)
35         
36         self.vbox.add (hbox)
37
38
39     def do_the_job (self, artist_albums_model):
40         """
41         each row: ("Visible text", pixbuf, Artist, Album)
42         """
43         TOTAL = len (artist_albums_model)
44         current = 1
45
46         it = artist_albums_model.get_iter_first ()
47         while (it):
48
49             while (gtk.events_pending()):
50                 gtk.main_iteration()
51
52             if (self.cancel):
53                 break
54
55             artist = artist_albums_model.get_value (it, 2)
56             album = artist_albums_model.get_value (it, 3)
57             
58             try:
59                 (image, thumb) = self.downloader.get_album_art (artist, album)
60                 if thumb:
61                         pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 124, 124)
62                         artist_albums_model.set_value (it, 1, pixbuf)
63             except Exception, e:
64                 print "Error processing %s - %s" % (artist, album)
65                 print str(e)
66                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
67                 continue
68
69             self.status_label.set_text ("Retrieved (%d/%d)" % (current, TOTAL))
70             self.current_label.set_markup ("<b>%s - %s</b>" % (artist, album))
71               
72             if (thumb):
73                 self.album_art.set_from_file (thumb)
74             else:
75                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
76
77             current += 1
78             it = artist_albums_model.iter_next (it)
79
80         
81     def handle_response (self, widget, response_id):
82         if (response_id == gtk.RESPONSE_DELETE_EVENT):
83             print "Cancel the work!"
84         self.cancel = True
85         self.destroy ()
86
87 if __name__ == "__main__":
88
89     PAIRS_store = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str)
90     for i in range (0, 100):
91         PAIRS_store.append (("blablabal", None, "Artist %d" % i, "Album %d" %i))
92
93     def clicked_button (self):
94         aadd = MussorgskyAlbumArtDownloadDialog (w)
95         aadd.show_all ()
96         aadd.do_the_job (PAIRS_store)
97         
98     w = gtk.Window ()
99     box = gtk.VBox ()
100
101     button = gtk.Button ("click")
102     button.connect ("clicked", clicked_button)
103     box.add (button)
104
105     w.add (box)
106     w.show_all ()
107
108
109     gtk.main ()