#!/usr/bin/env python
# Copyright Todd Korody 2007
# Licensed under the GPLv2
# http://www.gnu.org/copyleft/gpl.html

import sys
import locale
import gettext
import os, core, subprocess

try:
 	import pygtk
  	pygtk.require("2.0")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

locale.setlocale(locale.LC_ALL, "")
gtk.glade.bindtextdomain("dupemusicmatch", "locale")
gettext.install("dupemusicmatch", "locale", unicode=1)

class WriteOut:
	"Class to pass to Find_Dup to make a generic interface between the GTK and console"
	def __init__(self,listappend):
		self.output = listappend
	def write(self,filename,bitrate,length,size):
		self.output.append([filename,str(bitrate / 1000),str(int(length)),str(round(size/1048576.,2))])
	def write_seperator(self):
		self.output.append(["-","-","-","-"])

class Dupmusicmatch:
	def __init__(self):
			
		#Set the Glade file
		self.gladefile = "dmm.glade"  
		self.dmm = gtk.glade.XML(self.gladefile, "dupmusicmatch") 
			
		#Create our dictionay and connect it
		dic = {"on_dupmusicmatch_destroy" : gtk.main_quit
			,"key_press" : self.key_press
			,"on_delete_clicked" : self.on_delete_clicked
			,"on_check_dir1_clicked" : self.on_check_dir1_clicked
			,"on_check_dir2_clicked" : self.on_check_dir2_clicked
			,"on_check_dir3_clicked" : self.on_check_dir3_clicked
			,"on_play_clicked" : self.on_play_clicked
			,"on_refresh_clicked" : self.on_refresh_clicked
			,"on_stop_clicked" : self.on_stop_clicked}

		self.dmm.signal_autoconnect(dic)
		
		self.view = self.dmm.get_widget("treeview")
		self.check_dir1 = self.dmm.get_widget("check_dir1")
		self.check_dir2 = self.dmm.get_widget("check_dir2")
		self.check_dir3 = self.dmm.get_widget("check_dir3")
		self.filechooserbutton1 = self.dmm.get_widget("filechooserbutton1")
		self.filechooserbutton2 = self.dmm.get_widget("filechooserbutton2")
		self.filechooserbutton3 = self.dmm.get_widget("filechooserbutton3")
		self.check_filesize = self.dmm.get_widget("check_filesize")
		self.check_countc = self.dmm.get_widget("check_countc")
		self.check_paranoid = self.dmm.get_widget("check_paranoid")
		self.check_recursive = self.dmm.get_widget("check_recursive")

		renderer = gtk.CellRendererText()
		column = gtk.TreeViewColumn( _('Filename') , renderer, text=0)
		column.set_resizable(True)
		column1 = gtk.TreeViewColumn(_('Kbps'), renderer, text=1)
		column2 = gtk.TreeViewColumn(_('Length'), renderer, text=2)
		column3 = gtk.TreeViewColumn(_('Size'), renderer, text=3)
		self.view.append_column(column)
		self.view.append_column(column1)
		self.view.append_column(column2)
		self.view.append_column(column3)
		model = gtk.ListStore(str,str,str,str)
		self.view.set_model(model)
		self.liststore = model
		
	def key_press(self, widget, event):
		keyname = gtk.gdk.keyval_name(event.keyval)
		if keyname == "Delete":
			model, old_iter = self.view.get_selection().get_selected()
			if old_iter:
				file_to_del = self.liststore.get_value(old_iter, 0)
				print _('Deleting ') + str(file_to_del)
				os.remove(file_to_del)
				model.remove(old_iter)
		
	def on_delete_clicked(self, widget, *args):
		model, old_iter = self.view.get_selection().get_selected()
		if old_iter:
			file_to_del = self.liststore.get_value(old_iter, 0)
			print _('Deleting ') + str(file_to_del)
			os.remove(file_to_del)
			model.remove(old_iter)
				
	def on_refresh_clicked(self, widget, *args):
		self.liststore.clear()

		output = WriteOut(self.liststore)
		find = core.DMMCore(output)		
		# Check Check Boxes
		if (self.check_recursive.get_active()):
			find.set_recursive(True)
		if (self.check_paranoid.get_active()):
			find.set_rate(0.3)
		if (self.check_countc.get_active()):
			find.set_count(True)
		if (self.check_filesize.get_active()):
			find.set_filesize(True)
	
		if (self.check_dir1.get_active() and os.path.isdir(self.filechooserbutton1.get_filename())):
			find.add_dir(self.filechooserbutton1.get_filename())
		if (self.check_dir2.get_active() and os.path.isdir(self.filechooserbutton2.get_filename())):
			find.add_dir(self.filechooserbutton2.get_filename())
		if (self.check_dir3.get_active() and os.path.isdir(self.filechooserbutton3.get_filename())):
			find.add_dir(self.filechooserbutton3.get_filename())

		find.run(output)


	def on_play_clicked(self, widget, *args):
		model, old_iter = self.view.get_selection().get_selected()
		if old_iter:
			file_to_play = self.liststore.get_value(old_iter, 0)
			subprocess.Popen([r"audacious","-p", str(file_to_play)])

	def on_stop_clicked(self, widget, *args):
		subprocess.Popen([r"audacious","-s"])

	def on_check_dir2_clicked(self, widget, *args):
		if (self.check_dir2.get_active() == True):
			self.filechooserbutton2.set_sensitive(True)
		else:
			self.filechooserbutton2.set_sensitive(False)

	def on_check_dir3_clicked(self, widget, *args):
		if (self.check_dir3.get_active() == True):
			self.filechooserbutton3.set_sensitive(True)
		else:
			self.filechooserbutton3.set_sensitive(False)

	def on_check_dir1_clicked(self, widget, *args):
		if (self.check_dir1.get_active() == True):
			self.filechooserbutton1.set_sensitive(True)
		else:
			self.filechooserbutton1.set_sensitive(False)


if __name__ == "__main__":
	dmm = Dupmusicmatch()
	gtk.main()