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

import gtk, htmltextview

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 PackageDetails(gtk.EventBox):
	def __init__(self, db):
		self.db = db
		
		gtk.EventBox.__init__(self)
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
		self.set_border_width(2)
		
		self.vbox = gtk.VBox()
		self.add(self.vbox)
		
		self.HBBFramakey = gtk.HButtonBox()
		self.HBBFramakey.set_layout(gtk.BUTTONBOX_START)
		self.HBBFramasoft = gtk.HButtonBox()
		self.HBBFramasoft.set_layout(gtk.BUTTONBOX_START)
		self.HBBWebsite = gtk.HButtonBox()
		self.HBBWebsite.set_layout(gtk.BUTTONBOX_START)
		
		self.Desc = gtk.Label("")
		self.Desc.set_padding(2, 2)
		self.Desc.set_line_wrap(True)
		self.Desc.set_use_markup(True)
		self.Desc.set_alignment(0, 0.5)
		self.Desc.set_selectable(True)
		
		#self.LongDesc = gtk.Label("")
		#self.LongDesc.set_padding(2, 2)
		#self.LongDesc.set_line_wrap(True)
		#self.LongDesc.set_use_markup(True)
		#self.LongDesc.set_alignment(0, 0.5)
		
		self.LongDesc = htmltextview.HtmlTextView()
		self.LongDesc.connect("url-clicked", self.url_clicked)
		
		self.FramakeyLink = gtk.LinkButton("http://www.framakey.org/", "Fiche Framakey")
		self.FramasoftLink = gtk.LinkButton("http://www.framasoft.net/", "Fiche Framasoft")
		self.Website = gtk.LinkButton("http://www.roromis.fr.nf/", "Site")
		self.Licence = gtk.Label("")
		self.Licence.set_padding(2, 2)
		self.Licence.set_alignment(0, 0.5)
		self.Size = gtk.Label("")
		self.Size.set_padding(2, 2)
		self.Size.set_alignment(0, 0.5)
		self.Size.set_selectable(True)
		self.ZipSize = gtk.Label("")
		self.ZipSize.set_padding(2, 2)
		self.ZipSize.set_alignment(0, 0.5)
		self.ZipSize.set_selectable(True)
		
		self.vbox.pack_start(self.Desc, True, True)
		self.vbox.pack_start(self.LongDesc, False, False)
		
		self.HBBFramakey.pack_start(self.FramakeyLink, False, False)
		self.HBBFramasoft.pack_start(self.FramasoftLink, False, False)
		self.HBBWebsite.pack_start(self.Website, False, False)
		
		self.vbox.pack_start(self.HBBFramakey, False, False)
		self.vbox.pack_start(self.HBBFramasoft, False, False)
		self.vbox.pack_start(self.HBBWebsite, False, False)
		self.vbox.pack_start(self.Licence, False, False)
		self.vbox.pack_start(self.Size, False, False)
		self.vbox.pack_start(self.ZipSize, False, False)
	
	def url_clicked(self, widget, url, type):
		webbrowser.open_new_tab(url)
	
	def display_app(self, app):
		if app['desc'] != "" and app['desc'] != None:
			self.Desc.set_markup("<b>" + app['desc'] + "</b>")
			self.Desc.show()
		else:
			self.Desc.hide()
		
		self.LongDesc.get_buffer().set_text("")
		if app['longdesc'] != "" and app['longdesc'] != None:
			self.LongDesc.display_html("<p>" + app['longdesc'] + "</p>")
			self.LongDesc.show()
		else:
			self.LongDesc.hide()
		
		if app['framakey']!= "" and app['framakey'] != None:
			self.FramakeyLink.set_uri(app['framakey'])
			self.FramakeyLink.show()
		else:
			self.FramakeyLink.hide()
		

		if app['framasoft'] != "" and app['framasoft'] != None:
			self.FramasoftLink.set_uri(app['framasoft'])
			self.FramasoftLink.show()
		else:
			self.FramasoftLink.hide()
		

		if app['website'] != "" and app['website'] != None:
			self.Website.set_uri(app['website'])
			self.Website.show()
		else:
			self.Website.hide()
			
		if app['licence'] != "" and app['licence'] != None:
			self.Licence.set_markup("<b>Licence:</b> " + app['licence'])
			self.Licence.show()
		else:
			self.Licence.hide()
			
		if app['size'] != 0:
			self.Size.set_markup("<b>Taille décompressée:</b> " + get_str_size(app['size']))
			self.Size.show()
		else:
			self.Size.hide()
			
		if app['zipsize'] != 0:
			self.ZipSize.set_markup("<b>Taille de l'archive:</b> " + get_str_size(app['zipsize']))
			self.ZipSize.show()
		else:
			self.ZipSize.hide()

	def hide_all(self):
		self.Desc.hide()
		self.LongDesc.hide()
		self.FramakeyLink.hide()
		self.FramasoftLink.hide()
		self.Website.hide()
		self.Licence.hide()
		self.Size.hide()
		self.ZipSize.hide()
