Skip empty image
[mussorgsky] / src / aa_selection_dialog.py
1 import hildon
2 import gtk
3 import gobject
4 from album_art_thread import MussorgskyAlbumArt
5
6 class AlbumArtSelectionDialog (gtk.Dialog):
7
8     def __init__ (self, parent, artist, album, size, downloader=None):
9         """
10         parent window, amount of images to offer
11         Optionally downloader (for testing porpouses)
12         """
13         gtk.Dialog.__init__ (self,
14                              "Select album art", parent,
15                              gtk.DIALOG_DESTROY_WITH_PARENT,
16                              (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
17         self.artist = artist
18         self.album = album
19         self.size = size
20         self.paths = []
21         self.__create_view (size)
22         self.cancel = False
23         self.connect ("response", self.handle_response)
24
25         if (downloader):
26             self.downloader = downloader
27         else:
28             self.downloader = MussorgskyAlbumArt ()
29
30         gobject.idle_add (self.__get_alternatives_async)
31         self.selection_img = None
32         self.selection_thumb = None
33         hildon.hildon_gtk_window_set_progress_indicator (self, 1)
34
35
36     def __create_view (self, size):
37         hbox = gtk.HBox (homogeneous=True)
38
39         self.images = []
40         self.event_boxes = []
41         for i in range (0, size):
42             img = gtk.Image ()
43             img.set_size_request (124, 124)
44             self.images.append (img)
45
46             event_box = gtk.EventBox ()
47             event_box.add (img)
48             event_box.connect ("button-release-event", self.click_on_img, i)
49             event_box.set_sensitive (False)
50             self.event_boxes.append (event_box)
51             
52             hbox.pack_start (event_box, expand=False, fill=True)
53
54         self.vbox.add (hbox)
55
56     def __get_alternatives_async (self):
57         counter = 0
58         for (path, thumb) in self.downloader.get_alternatives (self.album, self.artist, self.size):
59             print path, thumb
60             if (self.cancel):
61                 return False
62             self.paths.insert (counter, (path, thumb))
63             if (thumb):
64                 print "Setting", thumb, "as image"
65                 self.images[counter].set_from_file (thumb)
66                 self.event_boxes [counter].set_sensitive (True)
67             else:
68                 continue
69             counter += 1
70             while (gtk.events_pending()):
71                 gtk.main_iteration()
72
73         while (counter < self.size):
74                 self.images[counter].set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
75                 counter += 1
76                 
77         hildon.hildon_gtk_window_set_progress_indicator (self, 0)
78
79
80     def click_on_img (self, widget, event, position):
81         img, thumb = self.paths[position]
82         self.selection_img, self.selection_thumb = self.downloader.save_alternative (self.artist,
83                                                                                      self.album,
84                                                                                      img, thumb)
85         self.response (position)
86
87     def get_selection (self):
88         return (self.selection_img, self.selection_thumb)
89
90     
91     def handle_response (self, widget, response_id):
92         self.cancel = True
93         # Return False to continue propagating the signal
94         return False
95
96 if __name__ == "__main__":
97
98     import time
99     class MockDownloader:
100         def __init__ (self):
101             self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
102                         ("../hoover.jpeg", "../hoover-thumb.jpeg"),
103                         ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
104                         ("../dylan.jpeg", "../dylan-thumb.jpeg")]
105         def get_alternatives (self, album, artist, amount):
106             for a in self.alt:
107                 time.sleep (1)
108                 yield a
109         def save_alternative (self, artist, album, img, thumb):
110             return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
111                               
112
113     def clicked_button (self):
114         aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader ())
115         aadd.show_all ()
116         response = aadd.run ()
117         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT:
118             print "Noooo"
119         else:
120             print "Selected", aadd.get_selection ()
121         aadd.hide ()
122         
123     w = gtk.Window ()
124     w.connect ("destroy", gtk.main_quit)
125     box = gtk.VBox ()
126
127     button = gtk.Button ("click")
128     button.connect ("clicked", clicked_button)
129     box.add (button)
130
131     w.add (box)
132     w.show_all ()
133
134
135     gtk.main ()
136