#!/usr/bin/python

#-*- coding: iso-8859-1 -*-

# Where is Application
# Copyright (C) 2009 Palumbo Roberto
# Author: Roberto Palumbo <palumborobertomail@gmail.com>
# 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; version 3 of the License.
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import gtk, os

class Finestra:
	
	def verifica_start(self):
		if self.startup==1:
			linee = []
			f = open ('/etc/xdg/autostart/wia.desktop','w')
			linee.append('[Desktop Entry]\n')
			linee.append('Name=Where is Application?\n')
			linee.append('Type=Application\n')
			linee.append('Exec=wia\n')
			linee.append('Terminal=false\n')
			linee.append('Comment=f.write(\'Application to notify the menu tree after installing an application\n')
			linee.append('Categories=Utility;\n')
			f.writelines(linee)
			f.close
		else:
			if os.path.exists('/etc/xdg/autostart/wia.desktop')==True:
				os.remove('/etc/xdg/autostart/wia.desktop')

	def esci(self,widget,data=None):
		self.verifica_start()
		if os.path.exists(self.home+'wiaconfig.ini'):
			f = open(self.home+'wiaconfig.ini','w')
			if self.startup==1: 
				stringa='YES'
			else:
				stringa='NO'
			f.write('STARTUP='+stringa+'\n')
			if self.traybar==1: 
				stringa='YES'
			else:
				stringa='NO'
			f.write('TRAYBAR='+stringa+'\n')
			
			if self.killall==1:
				stringa='YES'
			else:
				stringa='NO'
			f.write('KILLALL='+stringa+'\n')
			f.write('WAVE='+self.editbox.get_text())
			f.close()
		gtk.main_quit()
	
	def get_traybar():
		return self.traybar
	
	def get_startup():
		return self.startup

	def carica_dati(self):
		# verifica l'esistenza di una configurazione
	
		if os.path.exists(self.home+'wiaconfig.ini')==False:
			f = open(self.home+'wiaconfig.ini','w')
			f.write('STARTUP=YES\n')
			f.write('TRAYBAR=YES\n')
			f.write('KILLALL=YES\n')
			f.close()
		
		f = open(self.home+'wiaconfig.ini','r')
		g = f.readlines()
		f.close()
		for str in g:
			stringa = str.split('=')
			par = stringa[0].strip()
			val = stringa[1].strip()
			val = val.replace('\n','')
			if (par=='STARTUP') and (val=='YES'): 
				self.startup=1
			elif (par=='STARTUP') and (val=='NO'):
				self.startup=0
			if (par=='TRAYBAR') and (val=='YES'): 
				self.traybar=1
			elif (par=='TRAYBAR') and (val=='NO'):
				self.traybar=0
			if (par=='KILLALL') and (val=='YES'): 
				self.killall=1
			elif (par=='KILLALL') and (val=='NO'):
				self.killall-=0
			if (par=='WAVE'):
				self.editbox.set_text(val)
	
			
	def selectwave(self,widget,data=None):
		if data=='OPEN':
			dialog_buttons = (gtk.STOCK_CANCEL
							, gtk.RESPONSE_CANCEL
							, gtk.STOCK_OPEN
							, gtk.RESPONSE_OK)
			apri = gtk.FileChooserDialog('Open Wave File',action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons=dialog_buttons)
			if (apri.run()==gtk.RESPONSE_OK):
				self.editbox.set_text(apri.get_filename())
			apri.destroy()
		if data=='CLEAR':
			self.editbox.set_text('None')
	
	def tog(self,widget,data=None):
		if data=='TRAYBAR':
			if widget.get_active()==True:
				self.traybar=1
			else:
				self.traybar=0
		if data=='STARTUP':
			if widget.get_active()==True:
				self.startup=1
			else:
				self.startup=0
		if data=='KILLALL':
			if widget.get_active()==True:
				self.killall=1
			else:
				self.killall=0
	def __init__(self):
		# Azzero i parametri
		self.startup=0
		self.traybar=0
		self.killall=0
		#
		self.editbox = gtk.Entry(256)
		self.editbox.set_property('editable',False)
		self.editbox.set_text('None')
		#
		self.home = '/etc/'
		self.carica_dati()
		
		self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.win.set_default_size(350,250)
		self.win.set_title('Where is Application?')
		self.title = 'App'
		self.vbox = gtk.VBox(False,3)
		self.soundbox = gtk.VBox(False,3)
		self.toolbar = gtk.Toolbar()
		self.quit = gtk.ToolButton(gtk.STOCK_QUIT)
		self.about = gtk.ToolButton(gtk.STOCK_ABOUT)
		self.label = gtk.Label('@2009 Palumbo Roberto')
	
		self.button_r1 = gtk.CheckButton('Start at login')
		self.button_r2 = gtk.CheckButton('Show traybar icon')
		self.button_r3 = gtk.CheckButton('Update panels when notify an application')
		
		self.button_r1.connect('toggled',self.tog,'STARTUP')
		self.button_r2.connect('toggled',self.tog,'TRAYBAR')
		self.button_r3.connect('toggled',self.tog,'KILLALL')
		
		if self.startup==1:
			self.button_r1.set_active(True)
		else:
			self.button_r1.set_active(False)
		
		if self.traybar==1:
			self.button_r2.set_active(True)
		else:
			self.button_r2.set_active(False)
				
		if self.killall==1:
			self.button_r3.set_active(True)
		else:
			self.button_r3.set_active(False)
			
		self.openbutton = gtk.Button('Select Wave File')
		self.clearbutton = gtk.Button(None,gtk.STOCK_CLEAR)
		self.okbutton = gtk.Button(None,gtk.STOCK_SAVE)
		self.cancelbutton = gtk.Button(None,gtk.STOCK_QUIT)
		self.okbutton.connect('clicked',self.esci)
		self.cancelbutton.connect('clicked',exit)
		
		self.cont = gtk.Frame('Sound Control')
		
		self.hbox = gtk.HBox()
		self.hbox.pack_start(self.openbutton)
		self.hbox.pack_start(self.clearbutton,False,False)
		
		
		self.end = gtk.HBox()
		self.end.pack_end(self.okbutton,False,False)
		self.end.pack_end(self.cancelbutton,False,False)
				
		self.openbutton.connect('clicked',self.selectwave,'OPEN')
		self.clearbutton.connect('clicked',self.selectwave,'CLEAR')
		
		#self.lang = gtk.ComboBox()
		path = '/etc/wia/'
		lingua = os.environ['LANG']
		
		trovato = 'not found. English set as default language'
		
		for filename in os.listdir(path):
			if os.path.isfile(path+filename): 
				nomecompleto = str(filename)
				nome = nomecompleto.split(".")
				if (nome[0][0]==lingua[0]) and (nome [0][1]==lingua[1]):
					trovato = 'found'
					
		self.langframe = gtk.Frame("Language info")
		self.langinfo = gtk.Label("Language "+lingua[0]+lingua[1]+' '+trovato)
		self.langframe.add(self.langinfo)
		
		self.vbox.pack_start(self.button_r1,False,False,0)
		self.vbox.pack_start(self.button_r2,False,False,0)
		self.vbox.pack_start(self.button_r3,False,False,0)
		self.soundbox.pack_start(self.hbox,False,False,0)
		self.soundbox.pack_start(self.editbox,False,False,0)
		self.cont.add(self.soundbox)
		self.vbox.pack_start(self.cont,False,False,0)
		self.vbox.pack_start(self.langframe)
		self.vbox.pack_start(self.label)
		self.vbox.pack_start(self.end,False,False,0)
			
		self.win.set_icon_from_file('/usr/share/icons/wia.png')
		self.win.set_position(gtk.WIN_POS_CENTER)
		self.win.add(self.vbox)
		self.win.connect('destroy',self.esci)
		self.win.connect('delete-event',self.esci)
		

fin = Finestra()
fin.win.show_all()
gtk.main()
