Remove unused variable
[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.artist = artist
58         self.album = album
59         self.size = size
60         self.__create_view (size)
61         self.cancel = False
62         self.connect ("response", self.handle_response)
63
64         if (downloader):
65             self.downloader = downloader
66         else:
67             self.downloader = MussorgskyAlbumArt ()
68
69         gobject.idle_add (self.__get_alternatives_async)
70         self.selection_img = None
71         self.selection_thumb = None
72         hildon.hildon_gtk_window_set_progress_indicator (self, 1)
73
74
75     def __create_view (self, size):
76         hbox = gtk.HBox (homogeneous=True)
77
78         self.images = []
79         for i in range (0, size):
80             image = ClickableImage ()
81             image.connect ("button-release-event", self.click_on_img)
82             self.images.append (image)
83             hbox.pack_start (image, expand=False, fill=True)
84             
85         # default empty option
86         image = ClickableImage (isRemoveOption=True)
87         image.connect ("button-release-event", self.click_on_img)
88         self.images.append (image)
89         hbox.pack_start (image, expand=False, fill=True)
90
91         self.vbox.add (hbox)
92
93     def __get_alternatives_async (self):
94         counter = 0
95         for (path, thumb) in self.downloader.get_alternatives (self.album,
96                                                                self.artist,
97                                                                self.size):
98             print path, thumb
99             if (self.cancel):
100                 return False
101             if (thumb):
102                 print "Setting", thumb, "as image"
103                 self.images[counter].set_image (path, thumb)
104             else:
105                 continue
106             counter += 1
107             while (gtk.events_pending()):
108                 gtk.main_iteration()
109
110         while (counter < self.size):
111                 self.images[counter].set_default_image ()
112                 counter += 1
113                 
114         hildon.hildon_gtk_window_set_progress_indicator (self, 0)
115
116
117     def click_on_img (self, image, event):
118         if (image.is_remove_option ()):
119             self.selection_img = None
120             self.selection_thumb = None
121             self.downloader.reset_alternative (self.artist, self.album)
122         else:
123             tmp_img, tmp_thumb = image.get_paths ()
124             img, thumb = self.downloader.save_alternative (self.artist,
125                                                            self.album,
126                                                            tmp_img,
127                                                            tmp_thumb)
128             self.selection_img, self.selection_thumb = img, thumb
129         self.response (RESPONSE_CLICK)
130
131     def get_selection (self):
132         return (self.selection_img, self.selection_thumb)
133     
134     def handle_response (self, widget, response_id):
135         self.cancel = True
136         # Return False to continue propagating the signal
137         return False
138
139 if __name__ == "__main__":
140
141     import time
142     class MockDownloader:
143         def __init__ (self):
144             self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
145                         ("../hoover.jpeg", "../hoover-thumb.jpeg"),
146                         ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
147                         ("../dylan.jpeg", "../dylan-thumb.jpeg")]
148         def get_alternatives (self, album, artist, amount):
149             for a in self.alt:
150                 time.sleep (1)
151                 yield a
152         def save_alternative (self, artist, album, img, thumb):
153             return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
154         def reset_alternative (self, artist, album):
155             print "Removing the album-art and the thumbnail"
156                               
157
158     def clicked_button (self):
159         aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader ())
160         aadd.show_all ()
161         response = aadd.run ()
162         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT:
163             print "Noooo"
164         else:
165             print "RESPONSE_CLICK", response == RESPONSE_CLICK
166             print "Selected", aadd.get_selection ()
167         aadd.hide ()
168         
169     w = gtk.Window ()
170     w.connect ("destroy", gtk.main_quit)
171     box = gtk.VBox ()
172
173     button = gtk.Button ("click")
174     button.connect ("clicked", clicked_button)
175     box.add (button)
176
177     w.add (box)
178     w.show_all ()
179
180
181     gtk.main ()
182