#! /usr/bin/python

###############################################
# "Phrozen's PyMPEG player" v.0.1a
#  by PhrozenSmoke@yahoo.com
#  March 2002
#  A BASIC, lightweight mpeg player, ideal
#  for quick previewing of MPEG files from a 
#  file manager. It's really a 
#  frontend to the plaympeg program.
#
#  License: Open Source, free for non-commercial use
#  Please leave these credits intact.
#
#  This program and it's source may be freely 
#  modified and redistributed for non-commercial, 
#  non-profit use only.
#
#  This software comes with NO WARRANTY 
#  whatsoever!
#
#  This program requires Python and TCL.
#  Tested only on: Mandrake 8.1, Kernel 2.4.8, 
#  Glibc 2.2, Python 2.2, TCL 8.3.4 
###############################################

import os
from Tkinter import *
import tkMessageBox
import sys
import tkFileDialog

class MpegPlayer(Frame):
  def __init__(self, *args, **params):
    apply(Frame.__init__, (self,) + args, params)
    self._parent = None
    if len(args) != 0: self._parent = args[0]
    self.mpeg_file=""
    self.pid=3987
    self.addWidgets()
    self.killed=1

  def addWidgets(self):
    self._widgets = {}
    self.bgcolor="#DEE2EE"
    self["bg"]=self.bgcolor
    self._widgets['title'] = Label(self,background=self.bgcolor,text="PyMPEG: "+self.mpeg_file,font='-*-MS Sans Serif-Bold-R-Normal-*-*-140-*-*-*-*-*-*')
    self._widgets['title'].grid(column=1, row=1,sticky='new',pady=5)

    self._widgets['buttframe'] = Frame(self,background=self.bgcolor)
    self._widgets['buttframe'].grid(column=1, row=2,sticky='new',pady=5)

    self._widgets['play'] = Button(self._widgets['buttframe'],background="#12A62D",foreground="#FFFFFF",text="PLAY",command=self.plaympeg)
    self._widgets['play'].grid(column=1, row=1,sticky='new',pady=5)

    self._widgets['stop'] = Button(self._widgets['buttframe'],background="#A25843",foreground="#FFFFFF",text="STOP",command=self.stopmpeg)
    self._widgets['stop'].grid(column=2, row=1,sticky='new',pady=5)

    self._widgets['about'] = Button(self._widgets['buttframe'],background=self.bgcolor,text="About",command=self.about)
    self._widgets['about'].grid(column=3, row=1,sticky='new',pady=5,padx=12)

    self.bind("<Destroy>",self.stopmpeg)

    #Menus
    self.men=Menu(self,background="#BDC6CD")
    self.dropdown=Menu(self.men,background="#BDC6CD")
    self.dropdown.add_command(label="Open...",command=self.doOpen,background="#BDC6CD")
    self.dropdown.add_command(label="Exit",command=self.doClose,background="#BDC6CD")
    self.men.add_cascade(label="File", menu=self.dropdown,background="#BDC6CD")
    self._parent.config(menu=self.men)




  def plaympeg(self):
    if self.mpeg_file:
      try:
        self.stopmpeg()
      except:
        pass
      try:
        l=["","-l",self.mpeg_file]
        self.pid=os.spawnvp(os.P_NOWAIT,"plaympeg",l)
        self.killed=0
      except:
        tkMessageBox.showinfo("PyMPEG","Error playing the selected MPEG file!")
    else:
      self.doOpen()

  def stopmpeg(self,xarg=None):
    try:
      if self.killed: 
        return
      f=os.popen("kill "+str(self.pid))
      self.killed=1
      f.close()
    except:
      pass

  def about(self):
    tkMessageBox.showinfo("About PyMPEG","PyMPEG v.0.1a\nA BASIC, lightweight frontend to the Linux plaympeg utility.\nWritten with Python/Tkinter.\nIdeal for quick previewing of MPEG movies from a file manager.\nWritten: March 2002\nBy: PhrozenSmoke@yahoo.com\nLicense: Open souce, free for non-commercial use.\nThis software comes with NO WARRANTY whatsoever.") 
 
  def doClose(self,xargs=None):
    self.stopmpeg()
    sys.exit(0)

  def doOpen(self,xargs=None):
    self.setMPEGFile(tkFileDialog.askopenfilename())
    self.plaympeg()

  def setMPEGFile(self,mpegf):
    self.mpeg_file=str(mpegf).strip()
    m=str(mpegf).strip()
    if m.find("/") > -1:
      m=m[m.rfind("/")+1:len(m)]
    self._widgets['title']["text"]=" PyMPEG:  \""+m+"\" "


def openWindow(mpegf=None,doopen=1):
  root=Tk()
  m=MpegPlayer(root)  
  m.pack(fill=BOTH,expand=1)
  if doopen:
    if mpegf:
      m.setMPEGFile(mpegf)
      m.plaympeg()
  root.title("PyMPEG")
  root["bg"]="#DEE2EE"
  root.mainloop()

if __name__ == "__main__":
  if not len(sys.argv) == 2:
    openWindow("",0)
  else:
    mpeg=str(sys.argv[1])
    openWindow(mpeg)