#!/usr/bin/python

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

# WIA (Where Installed App?) - @2009 Palumbo Roberto
# Email: palumborobertomail@gmail.com
# Web: http://ubuntuwin.blogspot.com
# This a GPL python utility check periodically for new installed apps.
# If there is a new app installed this utility show you a notification
# who show you the menu name of the installed apps
# Don't change these infos to keep the license

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')
			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.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
	
			
	
	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
		
	def __init__(self):
		# Azzero i parametri
		self.startup=0
		self.traybar=0
		#
		self.home = '/etc/'
		self.carica_dati()
		self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.win.set_default_size(350,200)
		self.win.set_title('Where is Application?')
		self.title = 'App'
		self.vbox = gtk.VBox(True,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_r1.connect('toggled',self.tog,'STARTUP')
		self.button_r2.connect('toggled',self.tog,'TRAYBAR')
		
		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)
				
		self.vbox.pack_start(self.button_r1)
		self.vbox.pack_start(self.button_r2)
		self.vbox.pack_start(self.label)
	
		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()
