import deskbar.interfaces.Action
import deskbar.interfaces.Match
import deskbar.core.Utils
import deskbar.interfaces.Module

import dbus
import dbus.glib

import logging

import gtk

HANDLERS = ["TasqueHandler"]
LOGGER = logging.getLogger(__name__)

TASQUE_ICON = [
"24 24 173 2",
"  	c None",
". 	c #629D28",
"+ 	c #6ABE18",
"@ 	c #659A36",
"# 	c #6CBB1F",
"$ 	c #509D07",
"% 	c #4E9A06",
"& 	c #509C07",
"* 	c #6B9444",
"= 	c #6DB726",
"- 	c #76D815",
"; 	c #7CC338",
"> 	c #A4DD6B",
", 	c #A6DE6F",
"' 	c #A5DD6D",
") 	c #8AC64F",
"! 	c #7C8F68",
"~ 	c #70B32F",
"{ 	c #77D815",
"] 	c #75D715",
"^ 	c #68B919",
"/ 	c #4F9A06",
"( 	c #9BD761",
"_ 	c #76CA25",
": 	c #6CC614",
"< 	c #70C121",
"[ 	c #B4BFAA",
"} 	c #85B554",
"| 	c #76D816",
"1 	c #76D716",
"2 	c #68B221",
"3 	c #9BD562",
"4 	c #70C41E",
"5 	c #69C113",
"6 	c #6CBC1D",
"7 	c #899978",
"8 	c #E5ECDC",
"9 	c #8ADC37",
"0 	c #75D617",
"a 	c #6BAB2C",
"b 	c #9AD360",
"c 	c #6EC01E",
"d 	c #67BD13",
"e 	c #6AB81D",
"f 	c #879878",
"g 	c #D5E1C8",
"h 	c #F4F4F1",
"i 	c #87DA36",
"j 	c #6DA33A",
"k 	c #669837",
"l 	c #98CE5A",
"m 	c #6BBB1C",
"n 	c #64B812",
"o 	c #67B31C",
"p 	c #879778",
"q 	c #D4E0C6",
"r 	c #EEEEEC",
"s 	c #F3F3EA",
"t 	c #8AA66D",
"u 	c #6B9544",
"v 	c #96CB54",
"w 	c #69B71B",
"x 	c #62B312",
"y 	c #71A340",
"z 	c #6BA82F",
"A 	c #65AF1C",
"B 	c #879678",
"C 	c #D4E0C5",
"D 	c #EDEEEB",
"E 	c #CBD3B4",
"F 	c #729650",
"G 	c #94C84F",
"H 	c #67B31A",
"I 	c #5FAF11",
"J 	c #749D4D",
"K 	c #B7C0AD",
"L 	c #9AA293",
"M 	c #6AA531",
"N 	c #63AC1B",
"O 	c #859677",
"P 	c #D2DFC5",
"Q 	c #AEBD9F",
"R 	c #AABD83",
"S 	c #539C0B",
"T 	c #92C44A",
"U 	c #65AE19",
"V 	c #5DAA11",
"W 	c #719D45",
"X 	c #B7C1AE",
"Y 	c #E6EBDF",
"Z 	c #99A291",
"` 	c #6D9E3C",
" .	c #869477",
"..	c #D2DFC4",
"+.	c #AEBCA1",
"@.	c #7C9D5D",
"#.	c #9AC855",
"$.	c #529C0B",
"%.	c #90C044",
"&.	c #62AA17",
"*.	c #5AA610",
"=.	c #719B47",
"-.	c #AFBAA3",
";.	c #ECEDE9",
">.	c #E5EBDE",
",.	c #9CA495",
"'.	c #D1DFC3",
").	c #AEBBA1",
"!.	c #7FA060",
"~.	c #63AB1D",
"{.	c #95C249",
"].	c #529C0A",
"^.	c #8FBD40",
"/.	c #60A516",
"(.	c #58A10F",
"_.	c #759D4D",
":.	c #A4AE9B",
"<.	c #EAEDE5",
"[.	c #AEBBA2",
"}.	c #83A264",
"|.	c #66A924",
"1.	c #93BF44",
"2.	c #8DB83A",
"3.	c #5DA115",
"4.	c #559C0F",
"5.	c #569D11",
"6.	c #77A14D",
"7.	c #9BA493",
"8.	c #E4EADD",
"9.	c #86A467",
"0.	c #6AA82C",
"a.	c #91BB3E",
"b.	c #529C09",
"c.	c #8BB634",
"d.	c #5A9D13",
"e.	c #52980E",
"f.	c #569A14",
"g.	c #7CA652",
"h.	c #949C8B",
"i.	c #DEE7D3",
"j.	c #AFB9A4",
"k.	c #89A76D",
"l.	c #6EA936",
"m.	c #8FB738",
"n.	c #89B12F",
"o.	c #589812",
"p.	c #50930E",
"q.	c #5A991B",
"r.	c #82AB5A",
"s.	c #89947E",
"t.	c #8DA971",
"u.	c #76AA42",
"v.	c #8EB432",
"w.	c #4E9906",
"x.	c #90BE2B",
"y.	c #68AE16",
"z.	c #5CA910",
"A.	c #6BB126",
"B.	c #87C050",
"C.	c #79B83A",
"D.	c #5DAA12",
"E.	c #5EAA11",
"F.	c #97C22F",
"G.	c #519C08",
"H.	c #79BB1A",
"I.	c #A0D02A",
"J.	c #A2D22B",
"K.	c #A2D02B",
"L.	c #84C01E",
"M.	c #4D9907",
"N.	c #488E06",
"O.	c #499006",
"P.	c #000000",
"                                                ",
"                                                ",
"                                                ",
"                                          . +   ",
"                                        @ #     ",
"    $ % % % % % % % % % % % % % % &   * = -     ",
"  $ ; > , , , , , , , , , , , , ' ) ! ~ { ] ^   ",
"  / ( _ : : : : : : : : : : : : < [ } | 1 2     ",
"  % 3 4 5 5 5 5 5 5 5 5 5 5 5 6 7 8 9 0 a       ",
"  % b c d d d d d d d d d d e f g h i j k       ",
"  % l m n n n n n n n n n o p q r s t u         ",
"  % v w x x x y z x x x A B C r D E F           ",
"  % G H I I J K L M I N O P r D Q R S           ",
"  % T U V W X r Y Z `  ...r D +.@.#.$.          ",
"  % %.&.*.=.-.;.r >.,.'.r D ).!.~.{.].          ",
"  % ^./.(.(._.:.<.r r r D [.}.|.(.1.].          ",
"  % 2.3.4.4.5.6.7.8.r D -.9.0.4.4.a.b.          ",
"  % c.d.e.e.e.f.g.h.i.j.k.l.e.e.e.m.b.          ",
"  % n.o.p.p.p.p.q.r.s.t.u.p.p.p.p.v.b.          ",
"  w.x.y.z.z.z.z.z.A.B.C.D.z.z.z.E.F.G.          ",
"  % H.I.J.J.J.J.J.J.J.J.J.J.J.J.K.L.M.          ",
"    N.% % % % % % % % % % % % % % O.            ",
"      P.P.P.P.P.P.P.P.P.P.P.P.P.P.              ",
"                                                "]


class TasqueAction(deskbar.interfaces.Action):
  """The action that handles task addition.
  """
  def __init__(self, category, description):
    deskbar.interfaces.Action.__init__(self, description)
    self.cat = category
    self.desc = description
    
  def get_verb(self):
    return "New Task in " + self.cat
    
  def get_icon(self):
    return "gtk-add"

  def get_hash(self):
    return "%s-%s" % (self.cat, self.desc)
    
  def activate(self, text):
    try:
      # Get the D-Bus Session Bus
      bus = dbus.SessionBus()
      # Access the ICEcore Daemon Object
      obj = bus.get_object("org.gnome.Tasque", "/org/gnome/Tasque/RemoteControl")
      # Access the remote control interface
      tasque = dbus.Interface(obj, "org.gnome.Tasque.RemoteControl")
      # send new task via dbus to tasque
      tasque.CreateTask(self.cat, text, False)
    except dbus.exceptions.DBusException, e:
      LOGGER.exception(e)

  def skip_history(self):
    """Dont save this action please.
    """
    return True

  
class TasqueMatch(deskbar.interfaces.Match):
  """match search string and return actions
  """
  def __init__(self, text, **kwargs):
    deskbar.interfaces.Match.__init__(self,
      name=text,
      pixbuf=gtk.gdk.pixbuf_new_from_xpm_data(TASQUE_ICON),
      category="actions", **kwargs)
    self.text = text
    try:
      # Get the D-Bus Session Bus
      bus = dbus.SessionBus()
      # Access the ICEcore Daemon Object
      obj = bus.get_object("org.gnome.Tasque", "/org/gnome/Tasque/RemoteControl")
      # Access the remote control interface
      tasque = dbus.Interface(obj, "org.gnome.Tasque.RemoteControl")
      # get all categories
      for num, cat in enumerate(tasque.GetCategoryNames()):
        if num == 0:
          self.add_action(TasqueAction(cat, text), is_default=True)
        else:
          self.add_action(TasqueAction(cat, text))
    except dbus.exceptions.DBusException, e:
      LOGGER.exception(e)
    
  def get_hash(self):
    return "tasque-%s" % self.text


class TasqueHandler(deskbar.interfaces.Module):
  """The handler that will find, return a respond to queries.
  """

  INFOS = {'icon': gtk.gdk.pixbuf_new_from_xpm_data(TASQUE_ICON),
    'name': "Tasque",
    'description': "Add quick tasks into tasque via dbus",
    'version': '0.1.0.0',
  }
  
  def __init__(self):
    deskbar.interfaces.Module.__init__(self)
    
  def query(self, text):
    """ Respond to a deskbar query
    """
    self._emit_query_ready(text, [TasqueMatch(text)])
    
  def has_config(self):
    return False
