#!/usr/bin/env python
#Copyright (C) 2012 - Nanakos Chrysostmos <chris@include.gr>
import sys,os,ConfigParser
from gi.repository import Gtk as gtk
#import pygtk
#import gtk
#import gtk.glade

ykey_config = os.path.expanduser('~/.local/share/cinnamon/applets/softyubikey@yubiserver.include.gr/yubisoft.cfg')

class SigHandler:
	def onDeleteWindow(self,*args):
		gtk.main_quit(*args)
	def onApplyPressed(self,button):
		pub_id=public_id.get_text()
		prv_id = private_id.get_text()
		aeskey = aes_key.get_text()
		if(len(pub_id) != 12):
			self.show_error_dlg("Public ID is not correct.\nIt should be only 12 characters long\nand it is {0}.".format(len(pub_id)))
			return
		if(len(prv_id) != 12):
			self.show_error_dlg("Private ID is not correct.\nIt should be only 12 characters long\nand it is {0}.".format(len(prv_id)))
			return
		if(len(aeskey) != 32):
			self.show_error_dlg("AES Key is not correct.\nIt should be only 32 characters long\nand it is {0}.".format(len(aeskey)))
			return
		self.show_confirm_dlg("Are you sure you want to apply the new settings?\nSoftYubikey counters will be resetted\nand your data will be lost?")
		if(show_response == "yes"):
			self.writeConfig(pub_id,prv_id,aeskey)
			gtk.main_quit()
		elif(show_response == "no"):
			return
	
	def writeConfig(self,public_id,private_id,aes_key):
		config = ConfigParser.RawConfigParser()
		config.read(ykey_config)
		config.set('Yubisoft','publicid',public_id)
		config.set('Yubisoft','privateid',private_id)
		config.set('Yubisoft','aeskey',aes_key)
		config.set('Yubisoft','counter',int(0))
		config.set('Yubisoft','session',int(0))
		config.set('Yubisoft','timestamp',int(0))
		with open(ykey_config,'wb') as configfile:
			config.write(configfile)
	
	def onCancelPressed(self,button):
		gtk.main_quit()

	def show_error_dlg(self,error_string):
		error_dlg = gtk.MessageDialog(None, gtk.DialogFlags.MODAL, gtk.MessageType.ERROR, gtk.ButtonsType.CLOSE,"SoftYubikey - Error")
		error_dlg.format_secondary_text(error_string)
		error_dlg.run()
		error_dlg.destroy()
	
	def show_confirm_dlg(self,confirm_string):
		global show_response
		confirm_dlg = gtk.MessageDialog(None, gtk.DialogFlags.MODAL, gtk.MessageType.ERROR, gtk.ButtonsType.YES_NO,"SoftYubikey - Confirmation")
		confirm_dlg.format_secondary_text(confirm_string)
		response = confirm_dlg.run()
		if(response == gtk.ResponseType.YES):
			show_response = "yes"
		elif(response == gtk.ResponseType.NO):
			show_response = "no"
		confirm_dlg.destroy()

class CreateConfigWin:
	def __init__(self):
		self.builder = gtk.Builder()
		self.builder.add_from_file(os.path.expanduser('~/.local/share/cinnamon/applets/softyubikey@yubiserver.include.gr/ui/softyubikey.ui'))
		self.builder.connect_signals(SigHandler())
		self.window = self.builder.get_object("window")
		self.window.show()

if __name__ == "__main__":
	confwin = CreateConfigWin()
	public_id = confwin.builder.get_object("entry1")
	private_id = confwin.builder.get_object("entry2")
	aes_key = confwin.builder.get_object("entry3")
	gtk.main()
