# Bryan Schumaker (11/26/2010)

import ocarina
from ocarina import body
from ocarina import source
import menu

gtk       = ocarina.gtk
sources   = ocarina.libsaria.sources
library   = sources.library
visible   = library.is_visible
add2plist = None

libsaria = ocarina.libsaria
lib_page = source.Source()

def init():
	body.add_page("Library", lib_page)
	sources.init_src(library.init_bg, filler)
	libsaria.event.invite("POSTNEWSOURCE", refresh)

def filler():
	lib_page.init(filter, is_visible, right_click, library.play_id, reset)
	menu.add_lib_menu_item("Add to queue", add_selected_to_queue)
	menu.add_lib_menu_item("Add to playlist", add_selected_to_playlist)
	lib_page.fill(library.walk, True)

def filter(text):
	library.filter(text)
	lib_page.refilter()
	lib_page.goto()

def is_visible(list, iter):
	return visible(list[iter][0])

def reset():
	import playlist
	playlist.reset()
	lib_page.clear()
	library.reset()
	library.save()

def refresh(*args):
	lib_page.clear()
	lib_page.fill(library.walk)

def right_click(button, time):
	menu.make_lib_menu(button, time)

def add_selected_to_playlist(menu):
	import playlist
	lib_page.for_each_selected_id(sources.playlist.add_id)
	sources.playlist.save()
	playlist.refresh()

def add_selected_to_queue(menu):
	import queue
	lib_page.for_each_selected_id(sources.queue.add_id)
	queue.refresh()
