# Bryan Schumaker (8/16/2010) import ocarina import list import menu libsaria = ocarina.libsaria from libsaria import collection event = ocarina.libsaria.event gtk = ocarina.gtk class Actions: def __init__(self): self.mouse_motion = None 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.list.connect("motion-notify-event", self.on_mouse_motion) self.mouse_motion = actions.mouse_motion 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 ins_next = 0 for track in func(): insert(ins_next, [track[3], track[2], track[5], track[0], track[1], track[4], ""]) ins_next += 1 self.list.thaw() def on_mouse_motion(self, widget, pos): list = self.list x,y = list.convert_bin_window_to_widget_coords(int(pos.x), int(pos.y)) row = list.get_dest_row_at_pos(x,y) list = list.list if row is not None: row = row[0] list_iter = list.get_iter(row) list_row = list[list_iter] items = self.mouse_motion(list_row) list.set_value(list_iter, len(list_row)-1, "Played: %s Score: %s" % items) def add_selected_to_playlist(self, *args): self.list.for_each_selected(collection.plist_add_libid) collection.plist_save() collection.plist_refresh() class Library(Collection): def __init__(self): actions = Actions() actions.mouse_motion = self.mouse_motion 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() Collection.populate(self, collection.walk_library) #after = datetime.datetime.now() #print "Populating took: %s" % (after - before) def refresh(self, arg): self.clear() self.populate() def mouse_motion(self, row): return (collection.lib_get_attr(row[0], "playcount"), collection.lib_get_attr(row[0], "score")) def select_row(self, row): collection.change_score() collection.lib_play_id(row[0]) collection.lib_inc_score(row[0], 1) def filter(self, text): collection.lib_filter(text) self.list.refilter() def refilter(self, list, iter): return collection.lib_is_visible(list[iter][0]) class Playlist(Collection): def __init__(self): actions = Actions() actions.mouse_motion = self.mouse_motion 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("POSTSTART", self.populate, bg=True) libsaria.event.invite("PREPLISTREFRESH", self.refresh) def refresh(self, arg=None): self.clear() self.populate() def populate(self): #import datetime #before = datetime.datetime.now() Collection.populate(self, collection.walk_plist) #after = datetime.datetime.now() #print "Populating took: %s" % (after - before) def mouse_motion(self, row): return (collection.lib_get_attr(row[0], "playcount"), collection.lib_get_attr(row[0], "score")) def select_row(self, row): collection.change_score() collection.lib_play_id(row[0]) collection.lib_inc_score(row[0], 1) def filter(self, text): collection.plist_filter(text) self.list.refilter() def refilter(self, list, iter): return collection.plist_is_visible(list[iter][0])