Show current item/total items in edit_panel title
[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):
40         TOTAL = len (artist_albums)
41         current = 1
42         
43         for (artist, album) in artist_albums:
44             while (gtk.events_pending()):
45                 gtk.main_iteration()
46
47             if (self.cancel):
48                 break
49             
50             try:
51                 (image, thumb) = self.downloader.get_album_art (artist, album)
52             except LookupError, e:
53                 print "Error processing %s - %s" % (artist, album)
54                 print str(e)
55                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
56                 continue
57
58             self.status_label.set_text ("Retrieved (%d/%d)" % (current, TOTAL))
59             self.current_label.set_markup ("<b>%s - %s</b>" % (artist, album))
60               
61             if (thumb):
62                 self.album_art.set_from_file (thumb)
63             else:
64                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
65
66             current += 1
67
68         
69     def handle_response (self, widget, response_id):
70         if (response_id == gtk.RESPONSE_DELETE_EVENT):
71             print "Cancel the work!"
72         self.cancel = True
73         self.destroy ()
74
75 if __name__ == "__main__":
76
77     PAIRS_NO = [("Led Zeppelin", "Led Zeppelin IV"),
78              ("Pink Floyd", "The Wall"),
79              ("Deep purple", "Made in Japan"),
80              ("", "Freakin' out"),
81              ("Dinah Washington", "")]
82
83     PAIRS = [ ("Artist %d" % i, "Album %d" %i) for i in range (0, 100)]
84
85     def clicked_button (self):
86         aadd = MussorgskyAlbumArtDownloadDialog (w)
87         aadd.show_all ()
88         aadd.do_the_job (PAIRS)
89         
90     w = gtk.Window ()
91     box = gtk.VBox ()
92
93     button = gtk.Button ("click")
94     button.connect ("clicked", clicked_button)
95     box.add (button)
96
97     w.add (box)
98     w.show_all ()
99
100
101     gtk.main ()