bd6daa341c4c9a8e0a34b59a6fe86f168926a8f7
[mussorgsky] / src / aa_selection_dialog.py
1 import hildon
2 import gtk
3 import gobject
4 from album_art_thread import MussorgskyAlbumArt
5
6 RESPONSE_CLICK = 1
7
8 class ClickableImage (gtk.EventBox):
9
10     def __init__ (self, isRemoveOption=False):
11         gtk.EventBox.__init__ (self)
12
13         self.isRemoveOption = isRemoveOption
14
15         self.img = gtk.Image ()
16         self.img.set_size_request (124, 124)
17         self.add (self.img)
18         self.set_sensitive (False)
19
20         self.img_path = None
21         self.thumb_path = None
22
23         if (self.isRemoveOption):
24             self.img.set_from_icon_name ("mediaplayer_default_album",
25                                          gtk.ICON_SIZE_MENU)
26             self.img.set_pixel_size (124)
27             self.set_sensitive (True)
28             
29     def set_image (self, tmp_img, tmp_thumb):
30         assert not self.isRemoveOption
31         self.img_path = tmp_img
32         self.thumb_path = tmp_thumb
33         self.img.set_from_file (self.thumb_path)
34         self.set_sensitive (True)
35
36     def set_default_image (self):
37         self.img.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
38
39     def get_paths (self):
40         return self.img_path, self.thumb_path
41
42     def is_remove_option (self):
43         return self.isRemoveOption
44         
45
46 class AlbumArtSelectionDialog (gtk.Dialog):
47
48     def __init__ (self, parent, artist, album, size, downloader=None):
49         """
50         parent window, amount of images to offer
51         Optionally downloader (for testing porpouses)
52         """
53         gtk.Dialog.__init__ (self,
54                              "Select album art", parent,
55                              gtk.DIALOG_DESTROY_WITH_PARENT,
56                              (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
57         self.DEFAULT_ALBUM = size+1
58         self.artist = artist
59         self.album = album
60         self.size = size
61         self.__create_view (size)
62         self.cancel = False
63         self.connect ("response", self.handle_response)
64
65         if (downloader):
66             self.downloader = downloader
67         else:
68             self.downloader = MussorgskyAlbumArt ()
69
70         gobject.idle_add (self.__get_alternatives_async)
71         self.selection_img = None
72         self.selection_thumb = None
73         hildon.hildon_gtk_window_set_progress_indicator (self, 1)
74
75
76     def __create_view (self, size):
77         hbox = gtk.HBox (homogeneous=True)
78
79         self.images = []
80         for i in range (0, size):
81             image = ClickableImage ()
82             image.connect ("button-release-event", self.click_on_img)
83             self.images.append (image)
84             hbox.pack_start (image, expand=False, fill=True)
85             
86         # default empty option
87         image = ClickableImage (isRemoveOption=True)
88         image.connect ("button-release-event", self.click_on_img)
89         self.images.append (image)
90         hbox.pack_start (image, expand=False, fill=True)
91
92         self.vbox.add (hbox)
93
94     def __get_alternatives_async (self):
95         counter = 0
96         for (path, thumb) in self.downloader.get_alternatives (self.album,
97                                                                self.artist,
98                                                                self.size):
99             print path, thumb
100             if (self.cancel):
101                 return False
102             if (thumb):
103                 print "Setting", thumb, "as image"
104                 self.images[counter].set_image (path, thumb)
105             else:
106                 continue
107             counter += 1
108             while (gtk.events_pending()):
109                 gtk.main_iteration()
110
111         while (counter < self.size):
112                 self.images[counter].set_default_image ()
113                 counter += 1
114                 
115         hildon.hildon_gtk_window_set_progress_indicator (self, 0)
116
117
118     def click_on_img (self, image, event):
119         if (image.is_remove_option ()):
120             self.selection_img = None
121             self.selection_thumb = None
122             self.downloader.reset_alternative (self.artist, self.album)
123         else:
124             tmp_img, tmp_thumb = image.get_paths ()
125             img, thumb = self.downloader.save_alternative (self.artist,
126                                                            self.album,
127                                                            tmp_img,
128                                                            tmp_thumb)
129             self.selection_img, self.selection_thumb = img, thumb
130         self.response (RESPONSE_CLICK)
131
132     def get_selection (self):
133         return (self.selection_img, self.selection_thumb)
134     
135     def handle_response (self, widget, response_id):
136         self.cancel = True
137         # Return False to continue propagating the signal
138         return False
139
140 if __name__ == "__main__":
141
142     import time
143     class MockDownloader:
144         def __init__ (self):
145             self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
146                         ("../hoover.jpeg", "../hoover-thumb.jpeg"),
147                         ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
148                         ("../dylan.jpeg", "../dylan-thumb.jpeg")]
149         def get_alternatives (self, album, artist, amount):
150             for a in self.alt:
151                 time.sleep (1)
152                 yield a
153         def save_alternative (self, artist, album, img, thumb):
154             return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
155         def reset_alternative (self, artist, album):
156             print "Removing the album-art and the thumbnail"
157                               
158
159     def clicked_button (self):
160         aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader ())
161         aadd.show_all ()
162         response = aadd.run ()
163         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT:
164             print "Noooo"
165         else:
166             print "RESPONSE_CLICK", response == RESPONSE_CLICK
167             print "Selected", aadd.get_selection ()
168         aadd.hide ()
169         
170     w = gtk.Window ()
171     w.connect ("destroy", gtk.main_quit)
172     box = gtk.VBox ()
173
174     button = gtk.Button ("click")
175     button.connect ("clicked", clicked_button)
176     box.add (button)
177
178     w.add (box)
179     w.show_all ()
180
181
182     gtk.main ()
183