diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 12fc8c5c..76d73a6e 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -8,9 +8,12 @@ import path from map import Map # Variables are not saved across sessions, but preferences are -vars = None +vars = None prefs = None +plugin = None +music = None + # Initialize helpful variables def init(): @@ -29,4 +32,14 @@ def init_pref(key, value): def startup(): - pass + global music + global plugin + import music + import plugin + music.init() + plugin.load_all() + + +def shutdown(): + event.start("PREQUIT") + event.start("POSTQUIT") diff --git a/libsaria/path.py b/libsaria/path.py index 4830e73c..377fc7d2 100644 --- a/libsaria/path.py +++ b/libsaria/path.py @@ -2,13 +2,15 @@ import os -exists = os.path.exists -expand = os.path.expanduser -join = os.path.join -mkdir = os.mkdir -rm = os.remove -walk = os.walk -sep = os.sep +exists = os.path.exists +expand = os.path.expanduser +splitext = os.path.splitext +join = os.path.join +mkdir = os.mkdir +rm = os.remove +ls = os.listdir +walk = os.walk +sep = os.sep cp = None saria_dir = None diff --git a/libsaria/plugin.py b/libsaria/plugin.py index 7690b729..3937643a 100644 --- a/libsaria/plugin.py +++ b/libsaria/plugin.py @@ -1,12 +1,91 @@ # Bryan Schumaker (8/15/2010) -path = None +# Lazy loaded modules +path = None +imp = None -def install(filepath): +# Runtime variables +p_dir = None +loaded = dict() + +# Function variables +install = None +load_all = None +load_plugin = None + + +def init(): global path + global p_dir if path == None: from libsaria import path + if p_dir == None: + p_dir = path.plugin_dir() + + +def install_once(filepath): + global install + init() + install = install_rest + install(filepath) +def install_rest(filepath): + global p_dir + global path split = filepath.rsplit(path.sep, 1) file = split[len(split)-1] - dest = path.join(path.plugin_dir(), file) + dest = path.join(p_dir, file) path.cp(filepath, dest) + split = path.splitext(file) + if split[len(split)-1] == ".py": + name = split[0] + load_plugin(name, dest) +install = install_once + + +def load_all_once(): + global load_all + init() + load_all = load_all_rest + load_all() +def load_all_rest(): + global load_plugin + global path + global p_dir + for file in path.ls(p_dir): + split = path.splitext(file) + if split[len(split)-1] == ".py": + load_plugin(split[0], path.join(p_dir, file)) +load_all = load_all_once + + +def load_plugin_once(name, file): + global load_plugin + global imp + import imp + load_plugin = load_plugin_rest + load_plugin(name, file) +def load_plugin_rest(name, file): + global imp + global path + global loaded + + if name in loaded: + mod = loaded[name] + if hasattr(mod, "stop"): + loaded[name].stop() + del globals()[name] + + try: + mod = imp.load_source(name, file) + except Exception, e: + print "Error loading plugin: %s" % file + print e + + try: + globals()[name] = mod + loaded[name] = mod + mod.start() + except Exception, e: + print "Error starting plugin: %s" % file + print e +load_plugin = load_plugin_once diff --git a/ocarina.py b/ocarina.py index a1b4a5cf..056667b4 100755 --- a/ocarina.py +++ b/ocarina.py @@ -14,8 +14,7 @@ label = gtk.Label("Ocarina 4.1") label.show() win = ocarina.get_window(prefs["window_size"]) -#win.move(0,0) +ocarina.set_window_title("Ocarina 4.1") ocarina.add_tab("Test", label) -libsaria.startup() ocarina.startup() diff --git a/ocarina/__init__.py b/ocarina/__init__.py index 834551c1..5b6d9959 100644 --- a/ocarina/__init__.py +++ b/ocarina/__init__.py @@ -21,10 +21,12 @@ main_tabs = None def startup(): global gtk import gtk + libsaria.startup() gtk.main() def exit(widget, event): gtk.main_quit() + libsaria.shutdown() def get_tabs_once(): @@ -45,6 +47,12 @@ def add_tab(text, content): tabs = get_tabs() tabs.append_page(content, text) +def remove_tab(content): + tabs = get_tabs() + n = tabs.page_num(content) + if n > 0: + tabs.remove_page(n) + def get_window_once(size): global window @@ -61,3 +69,7 @@ def get_window_rest(size=None): main_window.resize(size[0], size[1]) return main_window get_window = get_window_once + +def set_window_title(text): + window = get_window() + window.set_title(text) diff --git a/ocarina/tabs.py b/ocarina/tabs.py index 143e72c3..a489878d 100644 --- a/ocarina/tabs.py +++ b/ocarina/tabs.py @@ -9,6 +9,8 @@ class Tabs(gtk.Notebook): def __init__(self): gtk.Notebook.__init__(self) self.set_tab_pos(gtk.POS_LEFT) + self.connect("switch-page", self.switch_page) + self.cur_page = 0 self.show() @@ -17,3 +19,13 @@ class Tabs(gtk.Notebook): label.set_angle(90) gtk.Notebook.append_page(self, content, label) self.set_tab_label_packing(content, True, True, gtk.PACK_START) + + + def switch_page(self, notebook, page, page_num): + old = notebook.get_nth_page(self.cur_page) + if hasattr(old, "invisible"): + old.invisible() + new = notebook.get_nth_page(page_num) + if hasattr(new, "visible"): + new.visible() + self.cur_page = page_num diff --git a/plugins/web_radio.py b/plugins/web_radio.py index e69de29b..0b40503b 100644 --- a/plugins/web_radio.py +++ b/plugins/web_radio.py @@ -0,0 +1,46 @@ +# Bryan Schumaker (8/14/2010) + +import ocarina +music = ocarina.libsaria.music +gtk = ocarina.gtk +webkit = None + +children = [] + + +class WebRadio(gtk.ScrolledWindow): + def __init__(self, url): + gtk.ScrolledWindow.__init__(self) + self.url = url + self.web = None + self.show() + + def visible(self): + if self.web == None: + self.web = webkit.WebView() + self.web.open(self.url) + #self.web.connect("load-finished", self.load_finished) + self.web.connect("document-load-finished", self.doc_load_finished) + self.add(self.web) + self.web.show() + + def doc_load_finished(self, view, webframe): + music.pause() + +def start(): + global webkit + global children + import webkit + + pandora = WebRadio("http://www.pandora.com") + children.append(pandora) + ocarina.add_tab("Pandora", pandora) + + groove = WebRadio("http://www.grooveshark.com") + children.append(groove) + ocarina.add_tab("Groove Shark", groove) + + +def stop(): + for child in children: + ocarina.remove_tab(child)