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

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

import catlist, details, config, addmenu, about

class main_window(gtk.Window):
	"""Fenêtre principale"""
	def __init__(self):
		"""Création de la fenêtre"""
		self.config = config.Config(self)
		
		# Configure l'affichage des icônes dans les boutons
		gtksettings = gtk.settings_get_default()
		gtksettings.set_long_property('gtk-button-images', 1, '')
		
		# Création de la fenetre
		gtk.Window.__init__(self)
		self.set_default_size(700, 500)
		self.set_title("FramaKioskTune")
		self.set_icon(gtk.gdk.pixbuf_new_from_file("img/FramaKioskTune.png"))
		self.connect("destroy", self.close)
		
		self.vboxprinc = gtk.VBox()
		self.add(self.vboxprinc)
		
		# Création des widgets
		self.create_left()
		self.create_toolbar()
		
		# Barre d'outils
		self.vboxprinc.pack_start(self.toolbar, False, False)
		
		# Milieu
		self.hpaned = gtk.HPaned()
		self.hpaned.set_position(220)
		self.vboxprinc.pack_start(self.hpaned, True, True)
		
		self.hpaned.add1(self.leftframe)
		
		self.details = details.details(self.config, self.catlist.update_selection)
		self.hpaned.add2(self.details)
		
		# Barre d'état
		self.statusbar = timedStatusbar()
		self.statusbar.set_has_resize_grip(True)
		self.statusid = self.statusbar.get_context_id("default")
		self.statusbar.push(self.statusid, "FramaKioskTune " + self.config.get("Data", "Version"))
		
		self.vboxprinc.pack_start(self.statusbar, False, False)
		
	def create_left(self):
		"""Crée le panneau de gauche"""
		self.leftframe = gtk.Frame()
		self.leftframe.set_shadow_type(gtk.SHADOW_ETCHED_IN)
		
		leftscroll = gtk.ScrolledWindow()
		leftscroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		self.leftframe.add(leftscroll)
		
		self.catlist = catlist.CatList(self.config)
		self.catlist.connect("cursor-changed", self.select_cat)
		self.catlist.connect("key-release-event", self.catlist.key_event)

		leftscroll.add(self.catlist)
	
	def create_toolbar(self):
		self.toolbar = gtk.Toolbar()
		self.toolbar.set_style(gtk.TOOLBAR_ICONS)
		
		item = gtk.ToolButton("gtk-refresh")
		item.connect("clicked", self.update)
		self.toolbar.insert(item, -1)
		
		self.toolbar.insert(gtk.SeparatorToolItem(), -1)
		
		self.addmenu = addmenu.addmenu(self.catlist.add_link)
		item = gtk.ToolButton("gtk-add")
		item.connect("clicked", self.addmenu.display_menu)
		self.toolbar.insert(item, -1)
		
		item = gtk.ToolButton("gtk-delete")
		item.connect("clicked", self.catlist.delete)
		self.toolbar.insert(item, -1)
		
		self.toolbar.insert(gtk.SeparatorToolItem(), -1)
		
		item = gtk.ToolButton("gtk-save")
		item.connect("clicked", self.create_kiosk)
		self.toolbar.insert(item, -1)
		
		self.toolbar.insert(gtk.SeparatorToolItem(), -1)
		
		item = gtk.ToolButton("gtk-about")
		item.connect("clicked", about.About, self.config)
		self.toolbar.insert(item, -1)
		
	def close(self, widget=None):
		gtk.main_quit()
	
	def update(self, widget=None):
		self.catlist.update()
	
	def select_cat(self, widget):
		(store, iter) = self.catlist.get_selection().get_selected()
		link = store[store.get_path(iter)][2]
		self.details.open(link)
		self.addmenu.select_cat(link.get("type"))
	
	def create_kiosk(self, widget=None):
		self.statusbar.output("Création du Kiosk en cours...", 1000)
		self.catlist.store.create_kiosk()
		self.statusbar.output("Création du Kiosk terminée...", 3000)
	
def _(str):
	return str

class timedStatusbar(gtk.Statusbar):
	_o = None
	def __init__(self):
		gtk.Statusbar.__init__(self)
		self._initialize()
		
	def _initialize(self):
		self.last_tag = None

	def output(self, msg,timeout):
		self.del_timer()
		self.pop(1)
		self.push(1,msg)
		self.set_timer(timeout)

	def del_timer(self):
		if self.last_tag:
			gobject.source_remove(self.last_tag)
	
	def set_timer(self, timeout):
		if timeout > 0:
			self.last_tag = gobject.timeout_add(timeout,self.clear)

	def clear(self):
		self.pop(1)
		self.push(1, "")
		return False