#!/usr/bin/python

# Winki the Ripper - GTK frontend for mencoder, mplayer and lsdvd
# Copyright (C) 2004 Velko Hristov
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
This is the main entry point for WinkiTheRipper program.
The main() function creates an instance of the WinkiTheRipper class
and starts the gtk event loop.
"""

def error_quit(problem_id, do_quit):
	messages = {'broken_gtk' : 'Sorry. Your PyGTK installation does not seam to be correct',
		    'import_error' : 'Sorry. Cannot load the modules needed by winki the ripper',
		    'old_gtk' : 'Unsupported PyGTK version. Please consider an upgrade to PyGTK 2'}
	print messages[problem_id]
	if do_quit != 0:
		raise SystemExit

try:
	import pygtk
	pygtk.require('2.0')
except:
	error_quit('old_gtk', 1)

try:
	print 'Importing module gtk...'
	import gtk
except:
	error_quit('broken_gtk', 1)

try:
	print 'Importing module os...'
	import os

	print 'Importing module winki.druid...'
	import winki.druid

	print 'Importing module winki.lib...'
	import winki.lib

	print 'Importing module winki.project...'
	import winki.project

	print 'Importing module winki.command...'
	import winki.command

	print 'Importing module winki.file...'
	import winki.file

	print 'Importing module winki.winki_rc...'
	import winki.winki_rc

	print 'Importing module winki.tool_finder...'
	import winki.tool_finder

	print 'Importing module gnome...'
	import gnome

except:
	error_quit('import_error', 1)

class WinkiTheRipper:
	def __init__(self, meta_tools):
		self.druid = None
		self.select = None
		self.project = None
		self.cmd = None
		self.action = None
		self.meta_tools = meta_tools
		return

	def init(self):
		#gnome.init('eog', '2.5.6')
		gnome.program_init('winki', winki.lib.winki_version)
		self.druid = winki.druid.Druid(self)
		if not self.druid:
			print 'WinkiTheRipper.init(): no druid'
			raise SystemExit
		self.druid.check_widgets()

		self.select = winki.file.FileSelection(self, self.druid.get_window())
		if not self.select:
			print 'WinkiTheRipper.init(): no file selection dialog'
			raise SystemExit
		self.select.check_widgets()

		self.project = winki.project.Project()
		if not self.project:
			print 'WinkiTheRipper.init(): no project'
			raise SystemExit

		nice_value = self.project.get_nice_value()
		os.nice(nice_value)
		return

	def end(self):
		gtk.main_quit()
		return

	def cleanup(self):
		if self.cmd:
			self.cmd.stop()
		self.end()
		return

	def has_program(self, progname):
		return self.project.has_tool(progname)

	def select_source(self, file_name):
		self.select.reset()
		self.select.path_should_exist = True
		self.select.caption = _('Select source file')
		self.select.set_filename(file_name)

		self.select.show()
		self.action = 'select source'
		return

	def select_dest(self, file_name):
		self.select.reset()
		self.select.path_should_exist = False
		self.select.caption = _('Select output file')
		self.select.set_filename(file_name)

		self.select.show()
		self.action = 'select dest'
		return

	def select_external_audio(self, file_name, widget_name):
		self.select.reset()
		self.select.path_should_exist = True
		self.select.caption = _('Select external audio file')

		norm_path = winki.lib.normalize_path(file_name)
		if winki.lib.is_file(norm_path):
			self.select.file_name = norm_path

		self.select.show()
		self.action = widget_name
		return

	def select_external_subtitles(self, file_name, widget_name):
		self.select.reset()
		self.select.path_should_exist = True
		self.select.caption = _('Select external subtitle file')

		norm_path = winki.lib.normalize_path(file_name)
		if winki.lib.is_file(norm_path):
			self.select.file_name = norm_path

		self.select.show()
		self.action = widget_name
		return

	def file_selected(self, file_name):
		if not self.action:
			assert(0)
			return
			
		if self.action == 'select source':
			self.druid.source_selected(file_name)

		elif self.action == 'select dest':
			self.druid.dest_selected(file_name)

		elif self.action == 'button_audio1':
			self.druid.external_audio_selected(file_name, 1)

		elif self.action == 'button_audio2':
			self.druid.external_audio_selected(file_name, 2)

		elif self.action == 'button_audio3':
			self.druid.external_audio_selected(file_name, 3)

		elif self.action == 'button_sub1':
			self.druid.external_subtitles_selected(file_name, 1)

		elif self.action == 'button_sub2':
			self.druid.external_subtitles_selected(file_name, 2)

		elif self.action == 'button_sub3':
			self.druid.external_subtitles_selected(file_name, 3)

		else:
			# new action?
			assert(0)

		self.action = None
		return

	def cancel_file_selection(self):
		self.druid.cancel_file_selection(self.action)
		return

	def scan_dvd(self):
		cmd = self.scan
		return cmd.run()

	def success_rip(self):
		page_rip = self.druid.xml.get_widget('druidpage_rip')
		self.druid.next_rip(page_rip)
		return

def main():
	tool_list = ['mplayer', 'mencoder', 'lsdvd', 'vcdimager', 'Stream merger', 'Audio compressor', 'Multiplexer']
	obligatory_tools = ['mplayer', 'mencoder']
	meta_tools = {'Multiplexer' : ['dvb-mplex'],
				  'Stream merger' : ['mkvmerge', 'ogmmerge'],
				  'Audio compressor' : ['oggenc', 'lame']}	
		
	rc_file = winki.winki_rc.WinkiRC()
	if not rc_file.update_local_rc(tool_list, obligatory_tools, meta_tools):
		return

	application = WinkiTheRipper(meta_tools)
	if application.init() == False:
		application.cleanup()
		return

	warn_missing = application.project.get_use_warning('missing_tools')
	tool_finder = winki.tool_finder.ToolFinder()
	tool_paths = application.project.get_tool_locations(tool_list)
	tool_finder.check_listed_tools(tool_paths, tool_list, meta_tools, warn_missing)

	gtk.main()
	return

main()
