4 from album_art_thread import MussorgskyAlbumArt
8 class ClickableImage (gtk.EventBox):
10 def __init__ (self, isRemoveOption=False):
11 gtk.EventBox.__init__ (self)
13 self.isRemoveOption = isRemoveOption
15 self.img = gtk.Image ()
16 self.img.set_size_request (124, 124)
18 self.set_sensitive (False)
21 self.thumb_path = None
23 if (self.isRemoveOption):
24 self.img.set_from_icon_name ("mediaplayer_default_album",
26 self.img.set_pixel_size (124)
27 self.set_sensitive (True)
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)
36 def set_default_image (self):
37 self.img.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
40 return self.img_path, self.thumb_path
42 def is_remove_option (self):
43 return self.isRemoveOption
46 class AlbumArtSelectionDialog (gtk.Dialog):
48 def __init__ (self, parent, artist, album, size, downloader=None):
50 parent window, amount of images to offer
51 Optionally downloader (for testing porpouses)
53 gtk.Dialog.__init__ (self,
54 "Select album art", parent,
55 gtk.DIALOG_DESTROY_WITH_PARENT,
56 (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
60 self.__create_view (size)
62 self.connect ("response", self.handle_response)
65 self.downloader = downloader
67 self.downloader = MussorgskyAlbumArt ()
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)
75 def __create_view (self, size):
76 hbox = gtk.HBox (homogeneous=True)
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)
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 self.vbox.pack_start (hbox, padding=24)
92 label = gtk.Label ("New search:")
93 self.entry = hildon.Entry (gtk.HILDON_SIZE_FINGER_HEIGHT)
94 self.entry.set_text (self.artist + " " + self.album)
97 img.set_from_icon_name ("general_search", gtk.ICON_SIZE_LARGE_TOOLBAR)
98 button = hildon.Button (gtk.HILDON_SIZE_FINGER_HEIGHT,
99 hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
100 button.set_image (img)
101 button.connect ("clicked", self.user_text_search_cb, self.entry)
102 self.hbox_research = gtk.HBox (homogeneous=False, spacing=6)
103 self.hbox_research.pack_start (label, expand=False)
104 self.hbox_research.pack_start (self.entry)
105 self.hbox_research.pack_start (button, expand=False)
106 self.hbox_research.set_sensitive (False)
107 self.vbox.pack_start (self.hbox_research, padding=6)
109 def __get_alternatives_async (self):
110 results = self.downloader.get_alternatives (self.album,
113 self.__show_results (results)
115 def __show_results (self, generator):
117 for (path, thumb) in generator:
122 print "Setting", thumb, "as image"
123 self.images[counter].set_image (path, thumb)
127 while (gtk.events_pending()):
130 while (counter < self.size):
131 self.images[counter].set_default_image ()
134 hildon.hildon_gtk_window_set_progress_indicator (self, 0)
135 self.hbox_research.set_sensitive (True)
136 self.entry.grab_focus ()
137 self.entry.select_region (0, -1)
139 def user_text_search_cb (self, w, entry):
140 user_text = entry.get_text ()
141 if user_text and len (user_text) > 0:
142 hildon.hildon_gtk_window_set_progress_indicator (self, 1)
143 for ev in self.images[:-1]:
144 ev.set_sensitive (False)
145 self.hbox_research.set_sensitive (False)
146 while (gtk.events_pending()):
149 results = self.downloader.get_alternatives_free_text (user_text,
151 self.__show_results (results)
154 def click_on_img (self, image, event):
155 if (image.is_remove_option ()):
156 self.selection_img = None
157 self.selection_thumb = None
158 self.downloader.reset_alternative (self.artist, self.album)
160 tmp_img, tmp_thumb = image.get_paths ()
161 img, thumb = self.downloader.save_alternative (self.artist,
165 self.selection_img, self.selection_thumb = img, thumb
166 self.response (RESPONSE_CLICK)
168 def get_selection (self):
169 return (self.selection_img, self.selection_thumb)
171 def handle_response (self, widget, response_id):
173 # Return False to continue propagating the signal
176 if __name__ == "__main__":
179 class MockDownloader:
181 self.alt = [("../hendrix.jpeg", "../thumb1.124.jpeg"),
182 ("../hoover.jpeg", "../thumb2.124.jpeg"),
183 ("../backbeat.jpeg", "../thumb3.124.jpeg"),
184 ("../dylan.jpeg", "../thumb5.jpeg")]
185 def get_alternatives (self, album, artist, amount):
189 def get_alternatives_free_text (self, user_text, amount=4):
190 for a in [("free%d" % i, "thumb%d" %i) for i in range (0, amount)]:
193 def save_alternative (self, artist, album, img, thumb):
194 return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
195 def reset_alternative (self, artist, album):
196 print "Removing the album-art and the thumbnail"
199 def clicked_button (self):
200 aadd = AlbumArtSelectionDialog (w, "joe henderson", "blue note", 4, MockDownloader ())
202 response = aadd.run ()
203 if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT:
206 print "RESPONSE_CLICK", response == RESPONSE_CLICK
207 print "Selected", aadd.get_selection ()
211 w.connect ("destroy", gtk.main_quit)
214 button = gtk.Button ("click")
215 button.connect ("clicked", clicked_button)