ocarina/trunk/ocarina.py

179 lines
4.7 KiB
Python

import gobject
import os
import sys
import thread
import pygtk
pygtk.require('2.0')
import gtk
from song import Song
from duration import Duration
from library import Library
from operations import Operations
from playlist import Playlist
from songInfo import SongInfo
#gtk.gdk.threads_init()
gobject.threads_init()
class main:
def __init__(self,argv):
self.ops = Operations()
self.makeWindow()
self.library = Library()
self.plist = Playlist()
self.plist.insert(self.library.nonBanned())
self.ops.plist = self.plist
self.ops.library = self.library
song = None
# If we were given a song as input, check that it exists and begin playback
if len(argv) > 0:
split = argv[0].split(self.library.data.path)
if len(split) > 0:
index = self.library.has(split[len(split)-1])
#if index != -1:
#info = self.library.data.files[index]
self.plist.queueSong(index)
if index==-1:
file = os.path.expanduser(argv[0])
if os.path.exists(file):
info = SongInfo()
info.filename = file
song = Song(info,self.next)#,self.commands.printLines)
self.ops.song = song
self.ops.next("","")
#gobject.idle_add(self.markProgress,self.pbar,"progress")
# Call gtk main
gtk.main()
# Make the main window
def makeWindow(self):
# Make window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Ocarina")
self.window.connect("delete_event",self.delete_event)
self.window.set_border_width(0)
self.window.set_icon_from_file("images/ocarina.png")
# Make a control box for buttons
self.control = gtk.VBox(False,0)
self.inside = gtk.HBox(False,0)
self.window.add(self.control)
# Make buttons
self.playButton = self.makeButton("playButton","images/play.png",None,self.ops.play)
self.pauseButton = self.makeButton("pauseButton","images/pause.png",None,self.ops.pause)
self.nextButton = self.makeButton("nextButton","images/next.png",None,self.ops.next)
self.thisButton = self.makeButton("thisButton",None,"This",self.this)
self.infoButton = self.makeButton("infoButton",None,"Info",self.info)
# Add buttons to window
self.inside.pack_start(self.playButton,False,False,0)
self.inside.pack_start(self.pauseButton,False,False,0)
self.inside.pack_start(self.nextButton,False,False,0)
self.inside.pack_start(self.thisButton,False,False,0)
self.inside.pack_start(self.infoButton,False,False,0)
self.inside.show()
# Top row
self.control.pack_start(self.inside,False,False,0)
self.pbar = gtk.ProgressBar()
self.pbar.set_fraction(0)
self.pbar.show()
# Update the progress bar every 100 ms
gobject.timeout_add(100,self.ops.markProgress,self.pbar,"progress")
self.control.pack_start(self.pbar,False,False,0)
# Tray
self.statusIcon = gtk.StatusIcon()
self.statusIcon.set_from_file("images/ocarina.png")
self.statusIcon.set_tooltip("Ocarina")
#self.statusIcon.show()
self.control.show()
self.window.show()
# Called before exiting
def delete_event(self,widget,event,data=None):
print "Quitting..."
self.library.dump()
gtk.main_quit()
return False
# Use this to make a button
# Give the name, image path, text, and callback function
def makeButton(self,name,path,text,func):
button = gtk.Button()
box = gtk.HBox(False,0)
box.set_border_width(0)
if path != None:
image = gtk.Image()
image.set_from_file(path)
image.show()
box.pack_start(image,False,False,0)
if text != None:
label = gtk.Label(text)
label.show()
box.pack_start(label,False,False,0)
box.show()
button.add(box)
button.connect("clicked",func,name)
button.show()
return button
# Show running time info
def time(self,unused):
if self.song == None:
return
cur = self.song.curTime()
tot = self.song.info.length
self.commands.printLine(cur.toStr()+" / "+tot.toStr())
# Show detailed song info
def info(self,widget,data):
# Return if no song found
if self.song == None:
return
# Return if no tags found
if self.song.info.tags == None:
print "Could not find any tags"
return
for tag in self.song.info.tags.keys():
print tag+":",self.song.info.tags[tag]
print self.song.info.filename
# Show basic song info
#def this(self,unused):
def this(self,widget,data):
# Return if no song found
if self.song == None:
return
# Return if no tags found
if self.song.info.tags == None:
print "Could not find any tags"
return
fields = ["title","artist","track-number","track-count","album"]
for field in fields:
if (field in self.song.info.tags.keys()) == True:
print field+":",self.song.info.tags[field]
def scanLib(self,dir):
if dir == "":
print "Please include a library directory"
return
self.library.scan(dir)
def random(self,unused):
self.plist.random = not self.plist.random
if __name__=='__main__':main(sys.argv[1:])