# -*- coding: utf-8 -*-

import os
import gtk
from subprocess import Popen

# Only for debugging : False / True
DEBUG = 0

ICON_THEME = gtk.icon_theme_get_default()

# FIXME! quick hack
ID_ICON  = 0

def logINFO(msg, from_mod=""):
    if DEBUG:
        print "[ADesk.%s] %s" % (from_mod, msg)

def launch_command(cmd):
    if cmd != ' ' and cmd != None and len(cmd)!=0:
        os.chdir(os.environ['HOME'])
        logINFO("Exec. | %s |" % cmd)
        ## FIXME! replace os.system with subprocess.Popen
        #~ os.system('sh -c "%s" &' % cmd)
        p = Popen(cmd, shell=True).pid
        #~ sts = os.waitpid(p.pid, 0)[1]
        realpath = os.path.dirname(os.path.realpath( __file__ ))
        os.chdir(realpath + '/..')

def hex2rgb(color_hex):
    ## convert Hex color to RGB
    hexcolor = color_hex.strip()
    hexcolor = hexcolor[1:]
    if len(hexcolor) != 6:
        logINFO('Invalid hex color, use #RRGGBB format.')
        return (0.0, 0.0, 0.0)
    r, g, b = hexcolor[:2], hexcolor[2:4], hexcolor[4:]
    r, g, b = [int(n, 16) for n in (r, g, b)]
    return (float(r)/256, float(g)/256, float(b)/256)

def pixbuf_from_file(file, width=None, height=None):
    pixbuf = None

    if file != None or file == '':
        if os.path.isfile(file):
            try:
                
                if not width and not height:
                    pixbuf = gtk.gdk.pixbuf_new_from_file(file)
                else:
                    if width and not height:
                        height = width
                    width, height = int(width), int(height)
                    pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(file, width, height)
            except:
                logINFO("is a image ? => %s" % file)
                pixbuf = None
        else:
            logINFO("is a image ? => %s" % file)

    return pixbuf

def ask_question(msg='Ok .. ?'):
    """  """
    message = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, msg)
    resp = message.run()
    message.destroy()

    if resp == gtk.RESPONSE_YES:
        return True
    else:
        return False

def show_msg(msg=' .. '):
    """  """
    message = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, msg)
    resp = message.run()
    message.destroy()

def image_button(label, image, size):
    bt = gtk.Button()
    bt.set_relief(gtk.RELIEF_NONE)
    bt.set_border_width(0)
    bt.set_focus_on_click(False)
    bt.set_property('can-focus', False)

    if image == None:
        bt_image = gtk.Image()
        pixbuf = pixbuf_from_file('images/def_icon.png', size, size)
        bt_image.set_from_pixbuf(pixbuf)
    elif type(image) == gtk.Image:
        bt_image = image
    elif type(image) == gtk.gdk.Pixbuf:
        bt_image = gtk.Image()
        bt_image.set_from_pixbuf(image)
    else:
        bt_image = gtk.Image()
        try:
            pixbuf = pixbuf_from_file(image, size, size)
            bt_image.set_from_pixbuf(pixbuf)
        except:
            pixbuf = pixbuf_from_file('images/def_icon.png', size, size)
            bt_image.set_from_pixbuf(pixbuf)
        
    bt_image.show()
    bt_image.set_size_request(size, size)

    bt_label = gtk.Label()
    bt_label.set_use_markup(True)
    
    if not label:
        label = ''
        
    bt_label.set_markup(label)
    bt_label.set_alignment(0.2, 1)
    align = gtk.Alignment(1, 0, 0.5, 0.5)
    align.show()
    align.add(bt_label)

    box = gtk.HBox(False, 6)
    box.set_border_width(2)
    box.show()
    box.pack_start(bt_image, False, False)
    box.pack_start(align, False, False)
    bt.add(box)
    bt.show_all()
    return bt

def set_icon(icon, dst, item=None, size=None):
    
    # update icon for gtk.Image
    if type(dst) == gtk.Image:
        
        if not size:
            size = 64
        
        if os.path.isfile(icon) and '.' in icon:
            # Update property image:
            try:
                dst.set_from_pixbuf(pixbuf_from_file(icon, size, size))
            except:
                dst.set_from_stock(gtk.STOCK_MISSING_IMAGE, gtk.ICON_SIZE_DIALOG)
                
        elif ICON_THEME.has_icon(icon):
            pixbuf = ICON_THEME.load_icon(icon, size, gtk.ICON_LOOKUP_USE_BUILTIN)
            dst.set_from_pixbuf(pixbuf)
        else:
            dst.set_from_stock(gtk.STOCK_MISSING_IMAGE, gtk.ICON_SIZE_DIALOG)
            
    # update icon for "view"
    else:

        # Update icon
        try:
            if os.path.exists(icon) and '.' in icon:
                dst.set_value(item, ID_ICON, pixbuf_from_file(icon, 32, 32) )
            elif ICON_THEME.has_icon(icon):
                pixbuf = ICON_THEME.load_icon(icon, 32, gtk.ICON_LOOKUP_USE_BUILTIN)
                dst.set_value(item, ID_ICON, pixbuf)
            else:
                dst.set_value(item, ID_ICON, pixbuf_from_file('images/def_icon.png', 32, 32) )
                
        except:
            print '* Listview Error while adding pixbuf  .. !'
