7 import helldon as hildon
10 from settings import settings
11 from postoffice import postoffice
14 log = logging.getLogger(__name__)
16 class _BaseList(gtk.TreeView):
18 TODO: unify the different lists into one
23 gtk.TreeView.__init__(self)
25 self.default_pixbuf = util.find_resource('album.png')
26 self.connect('destroy', self.on_destroy)
28 def get_pixbuf(self, img):
30 return gtk.gdk.pixbuf_new_from_file_at_size(img,
33 except gobject.GError:
34 log.error("Broken image in cache: %s", img)
38 log.warning("Failed to unlink broken image.")
39 if img != self.default_pixbuf:
40 return self.get_default_pixbuf()
44 def get_default_pixbuf(self):
45 if self.default_pixbuf:
46 return self.get_pixbuf(self.default_pixbuf)
48 def on_destroy(self, wnd):
51 class MusicList(_BaseList):
53 _BaseList.__init__(self)
54 (self.COL_ICON, self.COL_NAME, self.COL_ID, self.COL_IMAGE) = range(4)
55 self.__store = gtk.ListStore(gtk.gdk.Pixbuf, str, int, str)
57 self.set_model(self.__store)
59 icon = gtk.TreeViewColumn('Icon')
60 self.append_column(icon)
61 cell = gtk.CellRendererPixbuf()
62 icon.pack_start(cell, True)
63 icon.add_attribute(cell, 'pixbuf', self.COL_ICON)
65 col = gtk.TreeViewColumn('Name')
66 self.append_column(col)
67 cell = gtk.CellRendererText()
68 col.pack_start(cell, True)
69 col.add_attribute(cell, 'text', self.COL_NAME)
70 self.set_search_column(self.COL_NAME)
71 col.set_sort_column_id(self.COL_NAME)
73 postoffice.connect('images', self, self.on_images)
75 def get_item_id(self, path):
76 return self.__store.get(self.__store.get_iter(path), self.COL_ID)[0]
78 def on_destroy(self, wnd):
79 postoffice.disconnect('images', self)
81 def on_images(self, images):
82 for url, image in images:
83 for row in self.__store:
84 if row[self.COL_IMAGE] == url:
85 pb = self.get_pixbuf(image)
87 row[self.COL_ICON] = pb
89 def add_items(self, items):
90 images = [x for x in (self.get_item_image(item) for item in items) if x]
92 txt = self.get_item_text(item)
93 self.__store.append([self.get_default_pixbuf(), txt, item.ID, self.get_item_image(item)])
95 postoffice.notify('request-images', images)
97 def get_item_text(self, item):
98 if isinstance(item, jamaendo.Album):
99 return "%s - %s" % (item.artist_name, item.name)
100 elif isinstance(item, jamaendo.Track):
101 return "%s - %s" % (item.artist_name, item.name)
105 def get_item_image(self, item):
107 if isinstance(item, jamaendo.Track):
108 ret = item.album_image
109 elif hasattr(item, 'image'):
112 ret = ret.replace('1.100.jpg', '1.%d.jpg'%(self.ICON_SIZE))
115 class AlbumList(_BaseList):
117 _BaseList.__init__(self)
118 (self.COL_ICON, self.COL_NAME, self.COL_ID) = range(3)
119 self.__store = gtk.ListStore(gtk.gdk.Pixbuf, str, int)
120 self.__show_artist = True
122 self.set_model(self.__store)
124 icon = gtk.TreeViewColumn('Icon')
125 self.append_column(icon)
126 cell = gtk.CellRendererPixbuf()
127 icon.pack_start(cell, True)
128 icon.add_attribute(cell, 'pixbuf', self.COL_ICON)
130 col = gtk.TreeViewColumn('Name')
131 self.append_column(col)
132 cell = gtk.CellRendererText()
133 col.pack_start(cell, True)
134 col.add_attribute(cell, 'text', self.COL_NAME)
135 self.set_search_column(self.COL_NAME)
136 col.set_sort_column_id(self.COL_NAME)
138 postoffice.connect('album-cover', self, self.on_album_cover)
140 def on_destroy(self, wnd):
141 _BaseList.on_destroy(self, wnd)
142 postoffice.disconnect('album-cover', self)
144 def on_album_cover(self, albumid, size, cover):
145 if size == self.ICON_SIZE:
146 for row in self.__store:
147 if row[self.COL_ID] == albumid:
148 row[self.COL_ICON] = self.get_pixbuf(cover)
150 def add_album(self, album):
151 if self.__show_artist:
152 txt = "%s - %s" % (album.artist_name, album.name)
155 self.__store.append([self.get_default_pixbuf(), txt, album.ID])
156 postoffice.notify('request-album-cover', album.ID, self.ICON_SIZE)
158 def get_album_id(self, path):
159 return self.__store.get(self.__store.get_iter(path), self.COL_ID)[0]
161 def show_artist(self, show):
162 self.__show_artist = show
164 class TrackList(_BaseList):
165 def __init__(self, numbers = True):
166 _BaseList.__init__(self)
167 self.track_numbers = numbers
168 self.__store = gtk.ListStore(int, str, int)
169 self.set_model(self.__store)
172 col0 = gtk.TreeViewColumn('Num')
173 self.append_column(col0)
174 cell0 = gtk.CellRendererText()
175 col0.pack_start(cell0, True)
176 col0.add_attribute(cell0, 'text', 0)
178 col = gtk.TreeViewColumn('Name')
179 self.append_column(col)
180 cell = gtk.CellRendererText()
181 col.pack_start(cell, True)
182 col.add_attribute(cell, 'text', 1)
184 self.set_search_column(1 if numbers else 0)
185 col.set_sort_column_id(0)
187 def add_track(self, track):
188 self.__store.append([track.numalbum, track.name, track.ID])
190 def get_track_id(self, path):
191 treeiter = self.__store.get_iter(path)
192 _, _, _id = self.__store.get(treeiter, 0, 1, 2)
195 class RadioList(_BaseList):
197 _BaseList.__init__(self)
198 (self.COL_ICON, self.COL_NAME, self.COL_ID, self.COL_IMAGE) = range(4)
199 self.__store = gtk.ListStore(gtk.gdk.Pixbuf, str, int, str)
200 self.set_model(self.__store)
202 icon = gtk.TreeViewColumn('Icon')
203 self.append_column(icon)
204 cell = gtk.CellRendererPixbuf()
205 icon.pack_start(cell, True)
206 icon.add_attribute(cell, 'pixbuf', self.COL_ICON)
208 col = gtk.TreeViewColumn('Name')
209 self.append_column(col)
210 cell = gtk.CellRendererText()
211 col.pack_start(cell, True)
212 col.add_attribute(cell, 'text', self.COL_NAME)
213 self.set_search_column(self.COL_NAME)
214 col.set_sort_column_id(self.COL_NAME)
216 postoffice.connect('images', self, self.on_images)
218 def on_destroy(self, wnd):
219 postoffice.disconnect('images', self)
221 def add_radios(self, radios):
223 self.__store.append([self.get_default_pixbuf(), self.radio_name(radio), radio.ID, radio.image])
224 postoffice.notify('request-images', [radio.image for radio in radios])
227 def get_radio_id(self, path):
228 treeiter = self.__store.get_iter(path)
229 name, _id = self.__store.get(treeiter, self.COL_NAME, self.COL_ID)
232 def on_images(self, images):
233 for url, image in images:
234 for row in self.__store:
235 if row[self.COL_IMAGE] == url:
236 pb = self.get_pixbuf(image)
238 row[self.COL_ICON] = pb
240 def radio_name(self, radio):
242 return radio.idstr.capitalize()
246 return "Radio #%s" % (radio.ID)