Use gobject markup escape instead of a custom function
[mussorgsky] / src / aa_selection_dialog.py
1 import hildon
2 import gtk
3 import gobject
4 from album_art 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             if (self.cancel):
60                 return False
61             self.paths.insert (counter, (path, thumb))
62             print "Setting", thumb, "as image"
63             self.images[counter].set_from_file (thumb)
64             self.event_boxes [counter].set_sensitive (True)
65             counter += 1
66             while (gtk.events_pending()):
67                 gtk.main_iteration()
68
69         while (counter < self.size):
70                 self.images[counter].set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
71                 counter += 1
72                 
73         hildon.hildon_gtk_window_set_progress_indicator (self, 0)
74
75
76     def click_on_img (self, widget, event, position):
77         img, thumb = self.paths[position]
78         self.selection_img, self.selection_thumb = self.downloader.save_alternative (self.artist,
79                                                                                      self.album,
80                                                                                      img, thumb)
81         self.response (position)
82
83     def get_selection (self):
84         return (self.selection_img, self.selection_thumb)
85
86     
87     def handle_response (self, widget, response_id):
88         self.cancel = True
89         # Return False to continue propagating the signal
90         return False
91
92 if __name__ == "__main__":
93
94     import time
95     class MockDownloader:
96         def __init__ (self):
97             self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
98                         ("../hoover.jpeg", "../hoover-thumb.jpeg"),
99                         ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
100                         ("../dylan.jpeg", "../dylan-thumb.jpeg")]
101         def get_alternatives (self, album, artist, amount):
102             for a in self.alt:
103                 time.sleep (1)
104                 yield a
105         def save_alternative (self, artist, album, img, thumb):
106             return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
107                               
108
109     def clicked_button (self):
110         aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader ())
111         aadd.show_all ()
112         response = aadd.run ()
113         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT:
114             print "Noooo"
115         else:
116             print "Selected", aadd.get_selection ()
117         aadd.hide ()
118         
119     w = gtk.Window ()
120     w.connect ("destroy", gtk.main_quit)
121     box = gtk.VBox ()
122
123     button = gtk.Button ("click")
124     button.connect ("clicked", clicked_button)
125     box.add (button)
126
127     w.add (box)
128     w.show_all ()
129
130
131     gtk.main ()
132