#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib
import sys
import pygtk
if not sys.platform == 'win32':
	pygtk.require('2.0')
import gtk
import gobject

class confirm():
	def __init__(self, config, modifs):
		self.config = config
		self.modifs = modifs
		self.fenetre = gtk.Window()
		self.fenetre.set_title("Modifications à effectuer")
		self.fenetre.set_icon(gtk.gdk.pixbuf_new_from_file("img/synapps/SynApps.ico"))
		self.fenetre.set_default_size(325, 350)
		self.fenetre.set_border_width(10)
		
		self.LabelModifs = gtk.Label("Les modifications ci-dessous vont être effectuées. Continuer?")
		
		self.BordListeModifs = gtk.Frame()
		self.DefilListeModifs = gtk.ScrolledWindow()
		self.DefilListeModifs.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		
		self.TreeStoreModifs = gtk.TreeStore(gtk.gdk.Pixbuf, str)
		
		infoinstsize = 0
		infoinst = 0
		infouninstsize = 0
		infouninst = 0
		
		if len(self.modifs.get("install")) > 0:
			IterInstaller = self.TreeStoreModifs.append(None, [gtk.gdk.pixbuf_new_from_file('img/synapps/Installer.png'), "Installer"])
			for app in self.modifs.get("install"):
				infoinstsize += app.get("zipsize")
				infoinst += 1
				self.TreeStoreModifs.append(IterInstaller, [gtk.gdk.pixbuf_new_from_file(app.get("icon")).scale_simple(32,32,gtk.gdk.INTERP_BILINEAR), "<big>" + app.get("parentname") + "</big> <small>" + app.get("version") + "\n" + app.get("desc") + " (" + app.get("strzipsize") + ")</small>"])
		
		if len(self.modifs.get("uninstall")) > 0:
			IterDesinstaller = self.TreeStoreModifs.append(None, [gtk.gdk.pixbuf_new_from_file('img/synapps/Desinstaller.png'), "Désinstaller"])
			for app in self.modifs.get("uninstall"):
				infouninstsize += app.get("size")
				infouninst += 1
				self.TreeStoreModifs.append(IterDesinstaller, [gtk.gdk.pixbuf_new_from_file(app.get("icon")).scale_simple(32,32,gtk.gdk.INTERP_BILINEAR), "<big>" + app.get("parentname") + "</big> <small>" + app.get("version") + "\n" + app.get("desc") + " (" + app.get("strsize") + ")</small>"])
		
		self.Modifs = gtk.TreeView(self.TreeStoreModifs)
		self.Modifs.set_headers_visible(False)
		
		self.CellIcone = gtk.CellRendererPixbuf()
		self.CellApp = gtk.CellRendererText()
		
		self.ColonneIcone = gtk.TreeViewColumn('Icone')
		self.ColonneApp = gtk.TreeViewColumn('App', self.CellApp, markup=1)
		
		self.Modifs.append_column(self.ColonneIcone)
		self.Modifs.append_column(self.ColonneApp)
		
		self.ColonneIcone.pack_start(self.CellIcone, True)
		self.ColonneIcone.add_attribute(self.CellIcone, 'pixbuf', 0)
		
		self.Modifs.expand_all()
		
		self.DefilListeModifs.add(self.Modifs)
		self.BordListeModifs.add(self.DefilListeModifs)
		
		unitinst = "o"
		if infoinstsize >= 1024:
			infoinstsize /= 1024.
			unitinst = "Ko"
			if infoinstsize >= 1024:
				infoinstsize /= 1024.
				unitinst = "Mo"
				if infoinstsize >= 1024:
					infoinstsize /= 1024.
					unitinst = "Go"
					
		unituninst = "o"
		if infouninstsize >= 1024:
			infouninstsize /= 1024.
			unituninst = "Ko"
			if infouninstsize >= 1024:
				infouninstsize /= 1024.
				unituninst = "Mo"
				if infouninstsize >= 1024:
					infouninstsize /= 1024.
					unituninst = "Go"
		
		self.Infos = gtk.Label("<small>" + str(infoinst) + "application(s) (" + str(round(infoinstsize, 2)) + " " + unitinst + ") à installer\n" + str(infouninst) + "application(s) (" + str(round(infouninstsize, 2)) + " " + unituninst + ") à désinstaller</small>")
		self.Infos.set_line_wrap(True)
		self.Infos.set_justify(gtk.JUSTIFY_CENTER)
		self.Infos.set_use_markup(True)
		
		self.HBBoxBas = gtk.HButtonBox()
		self.HBBoxBas.set_layout(gtk.BUTTONBOX_END)
		
		self.BoutonAnnuler = gtk.Button(stock = gtk.STOCK_CANCEL)
		self.BoutonAnnuler.connect("clicked", self.on_BoutonAnnuler_clicked)
		self.HBBoxBas.pack_start(self.BoutonAnnuler, False, False)
		self.HBBoxBas.set_child_secondary(self.BoutonAnnuler, True)
		self.BoutonAppliquer = gtk.Button(stock = gtk.STOCK_APPLY)
		self.BoutonAppliquer.connect("clicked", self.on_BoutonAppliquer_clicked)
		self.HBBoxBas.pack_start(self.BoutonAppliquer, False, False)
		
		self.VBox = gtk.VBox()
		self.VBox.pack_start(self.LabelModifs, False, True)
		self.VBox.pack_start(self.BordListeModifs, True, True)
		self.VBox.pack_start(self.Infos, False, True)
		self.VBox.pack_start(self.HBBoxBas, False, True)	
		self.fenetre.add(self.VBox)
		
		self.fenetre.show_all()
		
	def on_BoutonAnnuler_clicked(self, widget):
		self.fenetre.destroy()
		return
		
	def on_BoutonAppliquer_clicked(self, widget):
		self.fenetre.destroy()
		self.modifs.execute()
		return
