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

import gtk, gobject
from lib import about, confirm, descappbox, applist, preferences, cattree, hboxrighttop, scrollframe, hbboxbottom, progress_dialog, modifs

class main_window(gtk.Window):
	"""Fenêtre principale"""
	def __init__(self, db):
		"""Création de la fenêtre"""
		self.db = db
		self.filtre = ""
			
		# Création de la fenetre
		gtk.Window.__init__(self)
		self.set_title("SynApps")
		self.set_icon_list(gtk.gdk.pixbuf_new_from_file("icons/SynApps16.png"), gtk.gdk.pixbuf_new_from_file("icons/SynApps24.png"), gtk.gdk.pixbuf_new_from_file("icons/SynApps32.png"), gtk.gdk.pixbuf_new_from_file("icons/SynApps64.png"), gtk.gdk.pixbuf_new_from_file("icons/SynApps128.png"))
		self.set_default_size(700, 500)
		self.connect("destroy", self.close)
		self.set_border_width(5)
		
		self.princ_vbox = gtk.VBox()
		self.add(self.princ_vbox)
		
		# Création de la zone du haut
		self.hbox_righttop = hboxrighttop.hboxrighttop(self.db)
		self.hbox_righttop.search_button.connect("clicked", self.search)
		self.hbox_righttop.search_entry.connect("activate", self.search)
		self.hbox_righttop.srclist.connect("toggle-src", self.refresh_display)
		self.princ_vbox.pack_start(self.hbox_righttop, False, True)
		
		self.middle_hpaned = gtk.HPaned()
		self.middle_hpaned.set_position(175)
		self.middle_hpaned.set_border_width(5)
		self.princ_vbox.pack_start(self.middle_hpaned, True, True)
		
		# Création de la liste des catégories
		frame = scrollframe.scrollframe(hpolicy=gtk.POLICY_NEVER)
		self.cattree = cattree.cattree(self.db)
		self.cattree.connect("select-cat", self.select_cat)
		frame.add_with_viewport(self.cattree)
		self.middle_hpaned.add1(frame)
		
		self.vpaned_right = gtk.VPaned()
		self.vpaned_right.set_position(250)
		self.middle_hpaned.add2(self.vpaned_right)
		
		# Création de la liste des applications
		frame = scrollframe.scrollframe(hpolicy=gtk.POLICY_NEVER)
		self.applist = applist.applist(self.db)
		self.applist.connect("select-app", self.select_app)
		frame.add_with_viewport(self.applist)
		self.vpaned_right.add1(frame)
		
		# Création de la description de l'application
		frame = scrollframe.scrollframe()
		self.descappbox = descappbox.DescAppBox(self.db)
		frame.add_with_viewport(self.descappbox)
		self.vpaned_right.add2(frame)
		
		#self.descappbox = descview.DescView(self.db)
		#self.vpaned_right.add2(self.descappbox)
		
		# Création de la zone du bas
		self.hbbox_bottom = hbboxbottom.hbboxbottom()
		
		self.hbbox_bottom.about.connect("clicked", about.about, self.db)
		self.hbbox_bottom.pref.connect("clicked", self.preferences)
		self.hbbox_bottom.refresh.connect("clicked", self.refresh)
		self.hbbox_bottom.cancel.connect("clicked", self.cancel)
		self.hbbox_bottom.apply.connect("clicked", self.apply)
		
		self.princ_vbox.pack_start(self.hbbox_bottom, False, False)
		
		self.show_all()
		self.descappbox.hide_all()
		self.cattree.expand_all()
		
		self.cattree.select((0,))
	
	def preferences(self, widget):
		dialog = preferences.preferences(self.db, self)
		response = dialog.run()
		
		if response == gtk.RESPONSE_OK:
			self.refresh()
			
		dialog.destroy()
	
	def apply(self, widget):
		"""Applique les modifications."""
		dialog = confirm.confirm_dialog(self, self.db)
		response = dialog.run()
		dialog.destroy()
		
		if response == True:
			dialog = modifs.modifs(self, self.db)
			dialog.connect("destroy", self.refresh)
			dialog.run()
		
	def cancel(self, widget):
		"""Quitte l'application."""
		self.destroy()
		
	def close(self, widget):
		"""Quitte gtk quand l'application est fermée."""
		gtk.main_quit()
	
	def refresh_display(self, widget=None):
		self.cattree.store.display_cats()
	
	def refresh(self, widget=None):
		"""Actualise les listes."""
		self.refresh_finished = False
		dialog = progress_dialog.progress_dialog()
		dialog.run()
		dialog.connect("destroy", self.refresh_end)
		
		gobject.idle_add(self.thread_refresh, dialog)

	def thread_refresh(self, dialog):
		try:
			self.db.update(True)
		except IOError as message:
			errordialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, str(message))
			errordialog.run()
			errordialog.destroy()
			
		self.refresh_finished = True
		dialog.destroy()
		
		return False

	def refresh_end(self, widget):
		if self.refresh_finished:
			self.descappbox.hide_all()
			
			self.hbox_righttop.srclist.refresh()
			self.cattree.refresh()
			self.cattree.expand_all()
	
	def refresh_display(self, widget):
		self.descappbox.hide_all()
		
		self.cattree.refresh()
		self.cattree.expand_all()
	
	def search(self, widget):
		"""Applique les modifications."""
		self.filtre = self.hbox_righttop.search_entry.get_text()
		self.applist.refresh(self.filtre)
		
	def select_app(self, widget, id):
		"""Activé quand on selectionne une application."""
		self.descappbox.display_app(id)
		
	def select_cat(self, widget, id):
		"""Activé quand on selectionne une catégorie."""
		self.applist.display_apps(id, self.filtre)
