ocarina/ocarina/collection.py

135 lines
3.1 KiB
Python

# Bryan Schumaker (8/16/2010)
import ocarina
import list
import menu
libsaria = ocarina.libsaria
from libsaria import sources
library = sources.library
playlist = sources.playlist
event = ocarina.libsaria.event
gtk = ocarina.gtk
class Actions:
def __init__(self):
self.selected_row = None
self.refilter = None
self.show_rc_menu = None
class Collection(gtk.ScrolledWindow):
def __init__(self, actions):
gtk.ScrolledWindow.__init__(self)
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.list = list.List(actions)
self.selected_row = actions.selected_row
self.add(self.list)
self.show()
def clear(self):
self.list.clear()
def populate(self, func):
self.list.freeze()
insert = self.list.list.insert
get_attrs = library.get_attrs
ins_next = 0
for id in func():
insert(ins_next, get_attrs(id, "id", "title", "lenstr", "artist", "album", "year"))
ins_next += 1
self.list.thaw()
def add_selected_to_playlist(self, *args):
self.list.for_each_selected(playlist.add_id)
playlist.save()
sources.plist_refresh()
class Library(Collection):
def __init__(self):
actions = Actions()
actions.selected_row = self.select_row
actions.refilter = self.refilter
actions.show_rc_menu = menu.make_lib_menu
Collection.__init__(self, actions)
libsaria.event.invite("POSTSTART", self.populate, bg=True)
libsaria.event.invite("POSTNEWSOURCE", self.refresh, bg=True)
menu.add_lib_menu_item("Add to playlist", self.add_selected_to_playlist)
def populate(self):
#import datetime
#before = datetime.datetime.now()
library.load()
Collection.populate(self, library.walk)
#after = datetime.datetime.now()
#print "Populating took: %s" % (after - before)
def refresh(self, arg):
self.clear()
Collection.populate(self, library.walk)
#self.populate()
def reset(self):
sources.reset()
self.clear()
def select_row(self, row):
sources.change_score()
sources.play_id(row[0])
sources.inc_score(row[0], 1)
def filter(self, text):
library.filter(text)
self.list.refilter()
def refilter(self, list, iter):
return library.is_visible(list[iter][0])
class Playlist(Collection):
def __init__(self):
actions = Actions()
actions.selected_row = self.select_row
actions.refilter = self.refilter
actions.show_rc_menu = menu.make_plist_menu
Collection.__init__(self, actions)
libsaria.event.invite("POSTLIBLOAD", self.populate, bg=True)
libsaria.event.invite("PREPLISTREFRESH", self.refresh)
def refresh(self, arg=None):
self.clear()
Collection.populate(self, playlist.walk)
def reset(self):
playlist.reset()
playlist.save()
self.clear()
def populate(self):
#import datetime
#before = datetime.datetime.now()
#Collection.populate(self, collection.walk_plist)
playlist.load()
Collection.populate(self, playlist.walk)
#after = datetime.datetime.now()
#print "Populating took: %s" % (after - before)
def select_row(self, row):
sources.change_score()
sources.play_id(row[0])
sources.inc_score(row[0], 1)
def filter(self, text):
playlist.filter(text)
self.list.refilter()
def refilter(self, list, iter):
return playlist.is_visible(list[iter][0])