Move iter to next element also in exception
[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                 current += 1
68                 it = artist_albums_model.iter_next (it)
69                 continue
70
71             self.status_label.set_text ("Retrieved (%d/%d)" % (current, TOTAL))
72             self.current_label.set_markup ("<b>%s - %s</b>" % (artist, album))
73               
74             if (thumb):
75                 self.album_art.set_from_file (thumb)
76             else:
77                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
78
79             current += 1
80             it = artist_albums_model.iter_next (it)
81
82         
83     def handle_response (self, widget, response_id):
84         if (response_id == gtk.RESPONSE_DELETE_EVENT):
85             print "Cancel the work!"
86         self.cancel = True
87         self.destroy ()
88
89 if __name__ == "__main__":
90
91     PAIRS_store = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str)
92     for i in range (0, 100):
93         PAIRS_store.append (("blablabal", None, "Artist %d" % i, "Album %d" %i))
94
95     def clicked_button (self):
96         aadd = MussorgskyAlbumArtDownloadDialog (w)
97         aadd.show_all ()
98         aadd.do_the_job (PAIRS_store)
99         
100     w = gtk.Window ()
101     box = gtk.VBox ()
102
103     button = gtk.Button ("click")
104     button.connect ("clicked", clicked_button)
105     box.add (button)
106
107     w.add (box)
108     w.show_all ()
109
110
111     gtk.main ()