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

import gtk, gobject, pango
from lib import cellrenderer

def get_str_size(size):
	unite = "o"
	if size >= 1024:
		size /= 1024.
		unite = "Ko"
		if size >= 1024:
			size /= 1024.
			unite = "Mo"
			if size >= 1024:
				size /= 1024.
				unite = "Go"
	return str(round(size, 2)) + " " + unite

class applist(gtk.TreeView):
	
	__gsignals__ = {
		"select-app": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (int, )),
	}
	
	def __init__(self, db):
		self.db = db
		self.store = store(db)
		self.current_id = 0

		gtk.TreeView.__init__(self, self.store)
		self.set_headers_visible(False)
		
		#self.toggle_cellrenderer = gtk.CellRendererToggle()
		#self.toggle_cellrenderer.set_property('activatable', True)
		
		self.toggle_cellrenderer = cellrenderer.CellRendererPackageToggle()
		
		self.icon_cellrenderer = gtk.CellRendererPixbuf()
		
		self.name_cellrenderer = gtk.CellRendererText()
		self.name_cellrenderer.set_property('ellipsize', pango.ELLIPSIZE_MIDDLE)
		
		self.icon_column = gtk.TreeViewColumn('')
		self.name_column = gtk.TreeViewColumn('Application', self.name_cellrenderer, markup=3)
		self.name_column.set_sort_order(gtk.SORT_ASCENDING)
		self.toggle_column = gtk.TreeViewColumn('')
		self.append_column(self.toggle_column)
		self.append_column(self.icon_column)
		self.append_column(self.name_column)
		
		self.toggle_column.pack_start(self.toggle_cellrenderer, True)
		#self.toggle_column.add_attribute(self.toggle_cellrenderer, 'active', 0)
		#self.toggle_column.add_attribute(self.toggle_cellrenderer, 'inconsistent', 1)
		self.toggle_column.add_attribute(self.toggle_cellrenderer, 'app_state', 0)
		self.toggle_column.add_attribute(self.toggle_cellrenderer, 'app_change', 1)
		self.icon_column.pack_start(self.icon_cellrenderer, True)
		self.icon_column.add_attribute(self.icon_cellrenderer, 'pixbuf', 2)
		self.name_column.add_attribute(self.name_cellrenderer, 'text', 3)
		
		self.connect("cursor-changed", self.change)
		self.toggle_cellrenderer.connect("toggled", self.toggle)
	
	def change(self, widget):
		(store, iter) = self.get_selection().get_selected()
		self.emit("select-app", store.get_value(iter, 4))
	
	def toggle(self, widget, path):
		app = self.db.get_app(self.store[path][4])
		change = app["change"]
		
		change += 1
		if change > 2:
			change = 0
		if change == 1 and app["state"] != 1:
			change = 2
		
		self.db.set_change(app["appid"], change)
		
		self.store[path][1] = change
	
	def display_apps(self, cat, filtre=""):
		self.current_id = cat
		self.store.display_apps(cat, filtre)
	
	def refresh(self, filtre=""):
		self.store.display_apps(self.current_id, filtre)

class store(gtk.ListStore):
	def __init__(self, db):
		self.db = db
		gtk.ListStore.__init__(self, gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT, gtk.gdk.Pixbuf, str, int)
	
	def display_apps(self, cat, filtre=""):
		self.clear()
		for app in self.db.get_apps(cat, filtre):
			try:
				icon = gtk.gdk.pixbuf_new_from_file(app["icon"]).scale_simple(24,24,gtk.gdk.INTERP_BILINEAR)
			except:
				icon = gtk.gdk.pixbuf_new_from_file("icons/default-app.png").scale_simple(24,24,gtk.gdk.INTERP_BILINEAR)
			try:
				version = self.db.get_downloads(app["appid"])[0]["version"]
			except:
				version = ""
			self.append([app["state"], app["change"], icon, "<big>" + app["parentname"] + "</big> <small>" + str(version) + "\n" + app["desc"] + " (" + get_str_size(app["size"]) + ")</small>", app["appid"]])
