def library_populate_data_songs(self, genre, artist, album, year): bd = [] if genre is not None: songs, _playtime, _num_songs = self.library_return_search_items(genre=genre, artist=artist, album=album, year=year) else: songs, _playtime, _num_songs = self.library_return_search_items(artist=artist, album=album, year=year) for song in songs: data = self.library_set_data(path=mpdh.get(song, 'file')) track = mpdh.getnum(song, 'track', '99', False, 2) disc = mpdh.getnum(song, 'disc', '99', False, 2) try: bd += [('f' + disc + track + misc.lower_no_the(mpdh.get(song, 'title')), [self.sonatapb, data, self.parse_formatting(self.config.libraryformat, song, True)])] except: bd += [('f' + disc + track + unicode(mpdh.get(song, 'file')).lower(), [self.sonatapb, data, self.parse_formatting(self.config.libraryformat, song, True)])] return bd
def sort(self, mode, column=None): if self.connected(): if not self.currentdata: return while gtk.events_pending(): gtk.main_iteration() songs = [] track_num = 0 if mode[0:3] == "col": col_num = int(mode.replace("col", "")) if column.get_sort_indicator(): # If this column was already sorted, reverse list: self.column_sorted = (column, self.column_sorted[1]) self.on_sort_reverse(None) return else: self.column_sorted = (column, gtk.SORT_DESCENDING) mode = "col" # If the first tag in the format is song length, we will make sure to compare # the same number of items in the song length string (e.g. always use # ##:##:##) and pad the first item to two (e.g. #:##:## -> ##:##:##) custom_sort = False if mode == "col": custom_sort, custom_pos = self.sort_get_first_format_tag(self.config.currentformat, col_num, "L") for track in self.current_songs: record = {} # Those items that don't have the specified tag will be put at # the end of the list (hence the 'zzzzzzz'): zzz = "zzzzzzzz" if mode == "artist": record["sortby"] = ( misc.lower_no_the(mpdh.get(track, "artist", zzz)), mpdh.get(track, "album", zzz).lower(), mpdh.getnum(track, "disc", "0", True, 0), mpdh.getnum(track, "track", "0", True, 0), ) elif mode == "album": record["sortby"] = ( mpdh.get(track, "album", zzz).lower(), mpdh.getnum(track, "disc", "0", True, 0), mpdh.getnum(track, "track", "0", True, 0), ) elif mode == "file": record["sortby"] = mpdh.get(track, "file", zzz).lower().split("/")[-1] elif mode == "dirfile": record["sortby"] = mpdh.get(track, "file", zzz).lower() elif mode == "col": # Sort by column: record["sortby"] = misc.unbold( self.currentdata.get_value(self.currentdata.get_iter((track_num, 0)), col_num).lower() ) if custom_sort: record["sortby"] = self.sanitize_songlen_for_sorting(record["sortby"], custom_pos) else: record["sortby"] = mpdh.get(track, mode, zzz).lower() record["id"] = int(track["id"]) songs.append(record) track_num = track_num + 1 songs.sort(key=lambda x: x["sortby"]) pos = 0 mpdh.call(self.client, "command_list_ok_begin") for item in songs: mpdh.call(self.client, "moveid", item["id"], pos) pos += 1 mpdh.call(self.client, "command_list_end") self.iterate_now() self.header_update_column_indicators()
def sort(self, mode, column=None): if self.connected(): if not self.currentdata: return while gtk.events_pending(): gtk.main_iteration() songs = [] track_num = 0 if mode[0:3] == 'col': col_num = int(mode.replace('col', '')) if column.get_sort_indicator(): # If this column was already sorted, reverse list: self.column_sorted = (column, self.column_sorted[1]) self.on_sort_reverse(None) return else: self.column_sorted = (column, gtk.SORT_DESCENDING) mode = "col" # If the first tag in the format is song length, we will make sure to compare # the same number of items in the song length string (e.g. always use # ##:##:##) and pad the first item to two (e.g. #:##:## -> ##:##:##) custom_sort = False if mode == 'col': custom_sort, custom_pos = self.sort_get_first_format_tag( self.config.currentformat, col_num, 'L') for track in self.current_songs: record = {} # Those items that don't have the specified tag will be put at # the end of the list (hence the 'zzzzzzz'): zzz = 'zzzzzzzz' if mode == 'artist': record["sortby"] = (misc.lower_no_the( mpdh.get(track, 'artist', zzz)), mpdh.get(track, 'album', zzz).lower(), mpdh.getnum(track, 'disc', '0', True, 0), mpdh.getnum(track, 'track', '0', True, 0)) elif mode == 'album': record["sortby"] = (mpdh.get(track, 'album', zzz).lower(), mpdh.getnum(track, 'disc', '0', True, 0), mpdh.getnum(track, 'track', '0', True, 0)) elif mode == 'file': record["sortby"] = mpdh.get(track, 'file', zzz).lower().split('/')[-1] elif mode == 'dirfile': record["sortby"] = mpdh.get(track, 'file', zzz).lower() elif mode == 'col': # Sort by column: record["sortby"] = misc.unbold( self.currentdata.get_value( self.currentdata.get_iter((track_num, 0)), col_num).lower()) if custom_sort: record["sortby"] = self.sanitize_songlen_for_sorting( record["sortby"], custom_pos) else: record["sortby"] = mpdh.get(track, mode, zzz).lower() record["id"] = int(track["id"]) songs.append(record) track_num = track_num + 1 songs.sort(key=lambda x: x["sortby"]) pos = 0 mpdh.call(self.client, 'command_list_ok_begin') for item in songs: mpdh.call(self.client, 'moveid', item["id"], pos) pos += 1 mpdh.call(self.client, 'command_list_end') self.iterate_now() self.header_update_column_indicators()
def get_path_child_filenames(self, return_root): # If return_root=True, return main directories whenever possible # instead of individual songs in order to reduce the number of # mpd calls we need to make. We won't want this behavior in some # instances, like when we want all end files for editing tags items = [] model, selected = self.library_selection.get_selected_rows() for path in selected: i = model.get_iter(path) pb = model.get_value(i, 0) data = model.get_value(i, 1) value = model.get_value(i, 2) if value != ".." and value != "/": album, artist, year, genre, path = self.library_get_data(data, 'album', 'artist', 'year', 'genre', 'path') if path is not None and album is None and artist is None and year is None and genre is None: if pb == self.sonatapb: # File items.append(path) else: # Directory if not return_root: items = items + self.library_get_path_files_recursive(path) else: items.append(path) else: results, _playtime, _num_songs = self.library_return_search_items(genre=genre, artist=artist, album=album, year=year) results.sort(key=lambda x: (unicode(mpdh.get(x, 'genre', 'zzz')).lower(), unicode(mpdh.get(x, 'artist', 'zzz')).lower(), unicode(mpdh.get(x, 'album', 'zzz')).lower(), mpdh.getnum(x, 'disc', '0', True, 0), mpdh.getnum(x, 'track', '0', True, 0), mpdh.get(x, 'file'))) for item in results: items.append(mpdh.get(item, 'file')) # Make sure we don't have any EXACT duplicates: items = misc.remove_list_duplicates(items, case=True) return items
def info_update(self, playing_or_paused, newbitrate, songinfo, update_all, blank_window=False, skip_lyrics=False): # update_all = True means that every tag should update. This is # only the case on song and status changes. Otherwise we only # want to update the minimum number of widgets so the user can # do things like select label text. if playing_or_paused: bitratelabel = self.info_labels[self.info_type[_("Bitrate")]] titlelabel = self.info_labels[self.info_type[_("Title")]] artistlabel = self.info_labels[self.info_type[_("Artist")]] albumlabel = self.info_labels[self.info_type[_("Album")]] datelabel = self.info_labels[self.info_type[_("Date")]] genrelabel = self.info_labels[self.info_type[_("Genre")]] tracklabel = self.info_labels[self.info_type[_("Track")]] filelabel = self.info_labels[self.info_type[_("File")]] if not self.last_info_bitrate or self.last_info_bitrate != newbitrate: bitratelabel.set_text(newbitrate) self.last_info_bitrate = newbitrate if update_all: # Use artist/album Wikipedia links? artist_use_link = False if 'artist' in songinfo: artist_use_link = True album_use_link = False if 'album' in songinfo: album_use_link = True titlelabel.set_text(mpdh.get(songinfo, 'title')) if artist_use_link: artistlabel.set_markup(misc.link_markup(misc.escape_html(mpdh.get(songinfo, 'artist')), False, False, self.linkcolor)) else: artistlabel.set_text(mpdh.get(songinfo, 'artist')) if album_use_link: albumlabel.set_markup(misc.link_markup(misc.escape_html(mpdh.get(songinfo, 'album')), False, False, self.linkcolor)) else: albumlabel.set_text(mpdh.get(songinfo, 'album')) datelabel.set_text(mpdh.get(songinfo, 'date')) genrelabel.set_text(mpdh.get(songinfo, 'genre')) if 'track' in songinfo: tracklabel.set_text(mpdh.getnum(songinfo, 'track', '0', False, 0)) else: tracklabel.set_text("") path = misc.file_from_utf8(self.config.musicdir[self.config.profile_num] + os.path.dirname(mpdh.get(songinfo, 'file'))) if os.path.exists(path): filelabel.set_text(self.config.musicdir[self.config.profile_num] + mpdh.get(songinfo, 'file')) self.info_editlabel.set_markup(misc.link_markup(_("edit tags"), True, True, self.linkcolor)) else: filelabel.set_text(mpdh.get(songinfo, 'file')) self.info_editlabel.set_text("") if 'album' in songinfo: # Update album info: artist, tracks = self.album_return_artist_and_tracks() trackinfo = "" album = mpdh.get(songinfo, 'album') year = mpdh.get(songinfo, 'date', None) if album is not None: albuminfo = album + "\n" playtime = 0 if len(tracks) > 0: for track in tracks: playtime += int(mpdh.get(track, 'time', 0)) if 'title' in track: trackinfo = trackinfo + mpdh.getnum(track, 'track', '0', False, 2) + '. ' + mpdh.get(track, 'title') + '\n' else: trackinfo = trackinfo + mpdh.getnum(track, 'track', '0', False, 2) + '. ' + mpdh.get(track, 'file').split('/')[-1] + '\n' if artist is not None: albuminfo += artist + "\n" if year is not None: albuminfo += year + "\n" albuminfo += misc.convert_time(playtime) + "\n" albuminfo += "\n" + trackinfo else: albuminfo = _("Album info not found.") self.albumText.set_markup(misc.escape_html(albuminfo)) else: self.albumText.set_text(_("Album name not set.")) # Update lyrics: if self.config.show_lyrics and not skip_lyrics: global ServiceProxy if ServiceProxy is None: try: from ZSI import ServiceProxy # Make sure we have the right version.. _test = ServiceProxy.ServiceProxy except: ServiceProxy = None if ServiceProxy is None: self.info_searchlabel.set_text("") self.info_show_lyrics(_("ZSI not found, fetching lyrics support disabled."), "", "", True) elif 'artist' in songinfo and 'title' in songinfo: self.get_lyrics_start(mpdh.get(songinfo, 'artist'), mpdh.get(songinfo, 'title'), mpdh.get(songinfo, 'artist'), mpdh.get(songinfo, 'title'), os.path.dirname(mpdh.get(songinfo, 'file'))) else: self.info_searchlabel.set_text("") self.info_show_lyrics(_("Artist or song title not set."), "", "", True) else: blank_window = True if blank_window: for label in self.info_labels: label.set_text("") self.info_editlabel.set_text("") if self.config.show_lyrics: self.info_searchlabel.set_text("") self.info_editlyricslabel.set_text("") self.info_show_lyrics("", "", "", True) self.albumText.set_text("") self.last_info_bitrate = ""
def execute_cmd(self, cmd): self.status = mpdh.status(self.client) if not self.status: print _("Unable to connect to MPD.\nPlease check your Sonata preferences or MPD_HOST/MPD_PORT environment variables.") sys.exit(1) self.songinfo = mpdh.currsong(self.client) if cmd == "play": mpdh.call(self.client, 'play') elif cmd == "pause": mpdh.call(self.client, 'pause', 1) elif cmd == "stop": mpdh.call(self.client, 'stop') elif cmd == "next": mpdh.call(self.client, 'next') elif cmd == "prev": mpdh.call(self.client, 'previous') elif cmd == "random": if self.status['random'] == '0': mpdh.call(self.client, 'random', 1) else: mpdh.call(self.client, 'random', 0) elif cmd == "repeat": if self.status['repeat'] == '0': mpdh.call(self.client, 'repeat', 1) else: mpdh.call(self.client, 'repeat', 0) elif cmd == "pp": if self.status['state'] in ['play']: mpdh.call(self.client, 'pause', 1) elif self.status['state'] in ['pause', 'stop']: mpdh.call(self.client, 'play') elif cmd == "info": if self.status['state'] in ['play', 'pause']: mpdh.conout (_("Title") + ": " + mpdh.get(self.songinfo, 'title')) mpdh.conout (_("Artist") + ": " + mpdh.get(self.songinfo, 'artist')) mpdh.conout (_("Album") + ": " + mpdh.get(self.songinfo, 'album')) mpdh.conout (_("Date") + ": " + mpdh.get(self.songinfo, 'date')) mpdh.conout (_("Track") + ": " + mpdh.getnum(self.songinfo, 'track', '0', False, 2)) mpdh.conout (_("Genre") + ": " + mpdh.get(self.songinfo, 'genre')) mpdh.conout (_("File") + ": " + os.path.basename(mpdh.get(self.songinfo, 'file'))) at, _length = [int(c) for c in self.status['time'].split(':')] at_time = misc.convert_time(at) try: time = misc.convert_time(int(mpdh.get(self.songinfo, 'time'))) print _("Time") + ": " + at_time + " / " + time except: print _("Time") + ": " + at_time print _("Bitrate") + ": " + self.status.get('bitrate', '') else: print _("MPD stopped") elif cmd == "status": try: if self.status['state'] == 'play': print _("State") + ": " + _("Playing") elif self.status['state'] == 'pause': print _("State") + ": " + _("Paused") elif self.status['state'] == 'stop': print _("State") + ": " + _("Stopped") if self.status['repeat'] == '0': print _("Repeat") + ": " + _("Off") else: print _("Repeat") + ": " + _("On") if self.status['random'] == '0': print _("Random") + ": " + _("Off") else: print _("Random") + ": " + _("On") print _("Volume") + ": " + self.status['volume'] + "/100" print _('Crossfade') + ": " + self.status['xfade'] + ' ' + gettext.ngettext('second', 'seconds', int(self.status['xfade'])) except: pass
def info_update(self, playing_or_paused, newbitrate, songinfo, update_all, blank_window=False, skip_lyrics=False): # update_all = True means that every tag should update. This is # only the case on song and status changes. Otherwise we only # want to update the minimum number of widgets so the user can # do things like select label text. if playing_or_paused: bitratelabel = self.info_labels[self.info_type[_("Bitrate")]] titlelabel = self.info_labels[self.info_type[_("Title")]] artistlabel = self.info_labels[self.info_type[_("Artist")]] albumlabel = self.info_labels[self.info_type[_("Album")]] datelabel = self.info_labels[self.info_type[_("Date")]] genrelabel = self.info_labels[self.info_type[_("Genre")]] tracklabel = self.info_labels[self.info_type[_("Track")]] filelabel = self.info_labels[self.info_type[_("File")]] if not self.last_info_bitrate or self.last_info_bitrate != newbitrate: bitratelabel.set_text(newbitrate) self.last_info_bitrate = newbitrate if update_all: # Use artist/album Wikipedia links? artist_use_link = False if 'artist' in songinfo: artist_use_link = True album_use_link = False if 'album' in songinfo: album_use_link = True titlelabel.set_text(mpdh.get(songinfo, 'title')) if artist_use_link: artistlabel.set_markup( misc.link_markup( misc.escape_html(mpdh.get(songinfo, 'artist')), False, False, self.linkcolor)) else: artistlabel.set_text(mpdh.get(songinfo, 'artist')) if album_use_link: albumlabel.set_markup( misc.link_markup( misc.escape_html(mpdh.get(songinfo, 'album')), False, False, self.linkcolor)) else: albumlabel.set_text(mpdh.get(songinfo, 'album')) datelabel.set_text(mpdh.get(songinfo, 'date')) genrelabel.set_text(mpdh.get(songinfo, 'genre')) if 'track' in songinfo: tracklabel.set_text( mpdh.getnum(songinfo, 'track', '0', False, 0)) else: tracklabel.set_text("") path = misc.file_from_utf8( self.config.musicdir[self.config.profile_num] + os.path.dirname(mpdh.get(songinfo, 'file'))) if os.path.exists(path): filelabel.set_text( self.config.musicdir[self.config.profile_num] + mpdh.get(songinfo, 'file')) self.info_editlabel.set_markup( misc.link_markup(_("edit tags"), True, True, self.linkcolor)) else: filelabel.set_text(mpdh.get(songinfo, 'file')) self.info_editlabel.set_text("") if 'album' in songinfo: # Update album info: artist, tracks = self.album_return_artist_and_tracks() trackinfo = "" album = mpdh.get(songinfo, 'album') year = mpdh.get(songinfo, 'date', None) if album is not None: albuminfo = album + "\n" playtime = 0 if len(tracks) > 0: for track in tracks: playtime += int(mpdh.get(track, 'time', 0)) if 'title' in track: trackinfo = trackinfo + mpdh.getnum( track, 'track', '0', False, 2) + '. ' + mpdh.get(track, 'title') + '\n' else: trackinfo = trackinfo + mpdh.getnum( track, 'track', '0', False, 2) + '. ' + mpdh.get( track, 'file').split('/')[-1] + '\n' if artist is not None: albuminfo += artist + "\n" if year is not None: albuminfo += year + "\n" albuminfo += misc.convert_time(playtime) + "\n" albuminfo += "\n" + trackinfo else: albuminfo = _("Album info not found.") self.albumText.set_markup(misc.escape_html(albuminfo)) else: self.albumText.set_text(_("Album name not set.")) # Update lyrics: if self.config.show_lyrics and not skip_lyrics: global ServiceProxy if ServiceProxy is None: try: from ZSI import ServiceProxy # Make sure we have the right version.. _test = ServiceProxy.ServiceProxy except: ServiceProxy = None if ServiceProxy is None: self.info_searchlabel.set_text("") self.info_show_lyrics( _("ZSI not found, fetching lyrics support disabled." ), "", "", True) elif 'artist' in songinfo and 'title' in songinfo: self.get_lyrics_start( mpdh.get(songinfo, 'artist'), mpdh.get(songinfo, 'title'), mpdh.get(songinfo, 'artist'), mpdh.get(songinfo, 'title'), os.path.dirname(mpdh.get(songinfo, 'file'))) else: self.info_searchlabel.set_text("") self.info_show_lyrics( _("Artist or song title not set."), "", "", True) else: blank_window = True if blank_window: for label in self.info_labels: label.set_text("") self.info_editlabel.set_text("") if self.config.show_lyrics: self.info_searchlabel.set_text("") self.info_editlyricslabel.set_text("") self.info_show_lyrics("", "", "", True) self.albumText.set_text("") self.last_info_bitrate = ""
def execute_cmd(self, cmd): self.status = mpdh.status(self.client) if not self.status: print _( "Unable to connect to MPD.\nPlease check your Sonata preferences or MPD_HOST/MPD_PORT environment variables." ) sys.exit(1) self.songinfo = mpdh.currsong(self.client) if cmd == "play": mpdh.call(self.client, 'play') elif cmd == "pause": mpdh.call(self.client, 'pause', 1) elif cmd == "stop": mpdh.call(self.client, 'stop') elif cmd == "next": mpdh.call(self.client, 'next') elif cmd == "prev": mpdh.call(self.client, 'previous') elif cmd == "random": if self.status['random'] == '0': mpdh.call(self.client, 'random', 1) else: mpdh.call(self.client, 'random', 0) elif cmd == "repeat": if self.status['repeat'] == '0': mpdh.call(self.client, 'repeat', 1) else: mpdh.call(self.client, 'repeat', 0) elif cmd == "pp": if self.status['state'] in ['play']: mpdh.call(self.client, 'pause', 1) elif self.status['state'] in ['pause', 'stop']: mpdh.call(self.client, 'play') elif cmd == "info": if self.status['state'] in ['play', 'pause']: mpdh.conout( _("Title") + ": " + mpdh.get(self.songinfo, 'title')) mpdh.conout( _("Artist") + ": " + mpdh.get(self.songinfo, 'artist')) mpdh.conout( _("Album") + ": " + mpdh.get(self.songinfo, 'album')) mpdh.conout(_("Date") + ": " + mpdh.get(self.songinfo, 'date')) mpdh.conout( _("Track") + ": " + mpdh.getnum(self.songinfo, 'track', '0', False, 2)) mpdh.conout( _("Genre") + ": " + mpdh.get(self.songinfo, 'genre')) mpdh.conout( _("File") + ": " + os.path.basename(mpdh.get(self.songinfo, 'file'))) at, _length = [int(c) for c in self.status['time'].split(':')] at_time = misc.convert_time(at) try: time = misc.convert_time( int(mpdh.get(self.songinfo, 'time'))) print _("Time") + ": " + at_time + " / " + time except: print _("Time") + ": " + at_time print _("Bitrate") + ": " + self.status.get('bitrate', '') else: print _("MPD stopped") elif cmd == "status": try: if self.status['state'] == 'play': print _("State") + ": " + _("Playing") elif self.status['state'] == 'pause': print _("State") + ": " + _("Paused") elif self.status['state'] == 'stop': print _("State") + ": " + _("Stopped") if self.status['repeat'] == '0': print _("Repeat") + ": " + _("Off") else: print _("Repeat") + ": " + _("On") if self.status['random'] == '0': print _("Random") + ": " + _("Off") else: print _("Random") + ": " + _("On") print _("Volume") + ": " + self.status['volume'] + "/100" print _('Crossfade') + ": " + self.status[ 'xfade'] + ' ' + gettext.ngettext( 'second', 'seconds', int(self.status['xfade'])) except: pass