def loadArtists(self, tree, name): """ Load the given library """ libPath = os.path.join(ROOT_PATH, name) # Make sure the version number is the good one if not os.path.exists(os.path.join(libPath, 'VERSION_%u' % VERSION)): logger.error('[%s] Version number does not match, loading of library "%s" aborted' % (MOD_INFO[modules.MODINFO_NAME], name)) error = _('This library is deprecated, please refresh it.') tree.replaceContent([(icons.errorMenuIcon(), None, error, TYPE_NONE, None, None)]) return rows = [] icon = icons.dirMenuIcon() prevChar = '' allArtists = pickleLoad(os.path.join(libPath, 'artists')) self.allGenres = pickleLoad(os.path.join(libPath, 'genres')) # Filter artists by genre if needed if self.currGenre is not None: allArtists = [artist for artist in allArtists if artist[ART_NAME] in self.allGenres[self.currGenre]] rows.append((icons.infoMenuIcon(), None, '<b>%s</b>' % self.currGenre.capitalize(), TYPE_GENRE_BANNER, None, None)) else: rows.append((icons.infoMenuIcon(), None, '<b>%s</b>' % _('All genres'), TYPE_GENRE_BANNER, None, None)) # Filter artists by favorites if needed if self.showOnlyFavs: allArtists = [artist for artist in allArtists if self.isArtistInFavorites(artist[ART_NAME])] rows.append((icons.starMenuIcon(), None, '<b>%s</b>' % _('My Favorites'), TYPE_FAVORITES_BANNER, None, None)) # Create the rows for artist in allArtists: if len(artist[ART_NAME]) != 0: currChar = unicode(artist[ART_NAME], errors='replace')[0].lower() else: currChar = prevChar if prevChar != currChar and not (prevChar.isdigit() and currChar.isdigit()): prevChar = currChar if currChar.isdigit(): rows.append((None, None, '<b>0 - 9</b>', TYPE_HEADER, None, None)) else: rows.append((None, None, '<b>%s</b>' % currChar.upper(), TYPE_HEADER, None, None)) rows.append((icon, None, htmlEscape(artist[ART_NAME]), TYPE_ARTIST, os.path.join(libPath, artist[ART_INDEX]), artist[ART_NAME])) # Insert all rows, and then add a fake child to each artist tree.replaceContent(rows) for node in tree.iterChildren(None): if tree.getItem(node, ROW_TYPE) == TYPE_ARTIST: tree.appendRow(FAKE_CHILD, node)
def showPopupMenu(self, tree, button, time, path): """ Show a popup menu """ popup = gtk.Menu() # Play play = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY) popup.append(play) if path is None: play.set_sensitive(False) else: play.connect('activate', lambda widget: self.playPaths(tree, None, True)) # Add add = gtk.ImageMenuItem(gtk.STOCK_ADD) popup.append(add) if path is None: add.set_sensitive(False) else: add.connect('activate', lambda widget: self.playPaths(tree, None, False)) # Separator popup.append(gtk.SeparatorMenuItem()) # Add to/remove from favorites favCpt = 0 nonFavCpt = 0 for node in tree.getSelectedPaths(): if tree.getItem(node, ROW_TYPE) != TYPE_ALBUM: favCpt = 1 nonFavCpt = 1 break elif tree.getItem(node, ROW_PIXBUF) == icons.mediaDirMenuIcon(): nonFavCpt += 1 else: favCpt += 1 if favCpt == 0: favorite = gtk.ImageMenuItem(_('Add to Favorites')) elif nonFavCpt == 0: favorite = gtk.ImageMenuItem(_('Remove from Favorites')) else: favorite = gtk.ImageMenuItem(_('Favorites')) favorite.get_image().set_from_pixbuf(icons.starMenuIcon()) popup.append(favorite) if TYPE_ALBUM in [tree.getItem(path, ROW_TYPE) for path in tree.getSelectedPaths()]: favorite.connect('activate', lambda widget: self.switchFavoriteStateOfSelectedItems(tree)) else: favorite.set_sensitive(False) # Show only favorites showFavorites = gtk.CheckMenuItem(_('Show Only Favorites')) showFavorites.set_active(self.showOnlyFavs) showFavorites.connect('toggled', lambda widget: self.switchFavoritesView(tree)) popup.append(showFavorites) # Separator popup.append(gtk.SeparatorMenuItem()) # Filter by genre genresMenu = gtk.Menu() filterGenre = gtk.MenuItem(_("Filter by Genre")); filterGenre.set_submenu(genresMenu) popup.append(filterGenre) # Need to keep the mapping widget -> genre to handle activate events self.genreItemToName = {} for genre in sorted(self.allGenres): genreItem = gtk.CheckMenuItem(genre.capitalize()) genreItem.set_active(genre == self.currGenre) self.genreItemToName[genreItem] = genre genreItem.connect('activate', lambda widget: self.filterByGenre(self.genreItemToName[widget])) genresMenu.append(genreItem) genresMenu.append(gtk.SeparatorMenuItem()) genreItem = gtk.CheckMenuItem(_('Unfiltered')) genreItem.set_active(self.currGenre == None) genreItem.connect('activate', lambda widget: self.filterByGenre(None)) genresMenu.append(genreItem) # Separator popup.append(gtk.SeparatorMenuItem()) # Collapse all nodes collapse = gtk.ImageMenuItem(_('Collapse all')) collapse.set_image(gtk.image_new_from_stock(gtk.STOCK_CLEAR, gtk.ICON_SIZE_MENU)) popup.append(collapse) enabled = False for child in self.tree.iterChildren(None): if self.tree.row_expanded(child): enabled = True break if enabled: collapse.connect('activate', lambda widget: self.tree.collapse_all()) else: collapse.set_sensitive(False) # Refresh the library refresh = gtk.ImageMenuItem(gtk.STOCK_REFRESH) refresh.connect('activate', lambda widget: idle_add(self.refreshLibrary(None, self.currLib, self.libraries[self.currLib][LIB_PATH]).next)) popup.append(refresh) # Randomness randomness = gtk.Menu() randomnessItem = gtk.ImageMenuItem(_('Randomness')) randomnessItem.get_image().set_from_icon_name('stock_shuffle', gtk.ICON_SIZE_MENU) randomnessItem.set_submenu(randomness) popup.append(randomnessItem) # Random album of the selected artist if path is not None and tree.getItem(path, ROW_TYPE) == TYPE_ARTIST: album = gtk.MenuItem(_('Pick an album of %(artist)s' % {'artist': tree.getItem(path, ROW_NAME).replace('&', '&')})) album.connect('activate', lambda widget: self.pickAlbumArtist(tree, path)) randomness.append(album) # Random album in the entire library album = gtk.MenuItem(_('Pick an album in the library')) album.connect('activate', lambda widget: self.pickAlbumLibrary(tree)) randomness.append(album) popup.show_all() popup.popup(None, None, None, button, time)