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

import logging

import gtk

try:
    from xml.etree import ElementTree
except ImportError:
    from elementtree import ElementTree
import gdata.calendar.service
import gdata.calendar
import atom


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

GCAL_ICON = [
"16 16 59 1",
" 	c None",
".	c #A0B4B2",
"+	c #A0B5B2",
"@	c #9FB5B2",
"#	c #9FB4B2",
"$	c #9FB5B1",
"%	c #A0B5B1",
"&	c #9FB4B1",
"*	c #A0B4B1",
"=	c #9DB1AD",
"-	c #6CC1F4",
";	c #9DB0AD",
">	c #98AAA8",
",	c #0B7FE4",
"'	c #6FCCF4",
")	c #6ECDF4",
"!	c #0CA5FF",
"~	c #6ECEF4",
"{	c #0882F2",
"]	c #0A7CEB",
"^	c #98ABA8",
"/	c #94A5A2",
"(	c #087CF9",
"_	c #94A4A2",
":	c #909E9A",
"<	c #FEFEFF",
"[	c #909E9B",
"}	c #8B9794",
"|	c #FFFFFF",
"1	c #B9C5CD",
"2	c #8A9794",
"3	c #86908D",
"4	c #FCFDFF",
"5	c #FCFEFF",
"6	c #D08389",
"7	c #FF3F3F",
"8	c #EE3133",
"9	c #FFAAAA",
"0	c #86908C",
"a	c #808986",
"b	c #F8FAFF",
"c	c #CA949A",
"d	c #818985",
"e	c #7B827F",
"f	c #F4F8FE",
"g	c #FFC0C0",
"h	c #777B77",
"i	c #F0F5FE",
"j	c #767B77",
"k	c #737671",
"l	c #EBF4FD",
"m	c #737572",
"n	c #6F706C",
"o	c #E8F1FD",
"p	c #6E706C",
"q	c #6B6C67",
"r	c #E6F0FD",
"s	c #E5F0FD",
"t	c #696864",
"                ",
" .+@#$#%.#&*%%  ",
"=-------------; ",
">,')!~{{{{{{{]^ ",
"/(((((((((((((_ ",
":<<<<<<<<<<<<<[ ",
"}<|<<<1111111<2 ",
"3454441|6789140 ",
"ab1111118cc81bd ",
"ef1|1|1|8gc71fe ",
"hi11111168861ij ",
"kl1|1|1|1|1lllm ",
"no111111111ooop ",
"qrsrrrrrrrrrrrq ",
"ttttttttttttttt ",
"                "]

GCONF_GCAL_EMAIL = GconfStore.GCONF_DIR + "/gcal_email"
GCONF_GCAL_PASSWORD = GconfStore.GCONF_DIR + "/gcal_password"

class GCalAction(deskbar.interfaces.Action):
  """The action that handles event addition.
  """

  def __init__(self, text):
    deskbar.interfaces.Action.__init__(self, text)
    self.text = text
    
  def get_verb(self):
    return "New Google Calendar Event"
    
  def get_icon(self):
    return "gtk-add"

  def activate(self, text):
    # get configs from gconf
    client = GconfStore.get_instance().get_client()
    email = client.get_string(GCONF_GCAL_EMAIL)
    password = client.get_string(GCONF_GCAL_PASSWORD)

    try:
      # login into google calendar
      cal_client = gdata.calendar.service.CalendarService()
      cal_client.email = email
      cal_client.password = password
      cal_client.source = 'Google-Calendar_Deskbar_Plugin'
      cal_client.ProgrammaticLogin()

      event = gdata.calendar.CalendarEventEntry()
      event.content = atom.Content(text=text)
      event.quick_add = gdata.calendar.QuickAdd(value='true')

      # send the request
      cal_client.InsertEvent(event, '/calendar/feeds/default/private/full')
    except Exception, err:
      LOGGER.error(err)

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

  
class GCalMatch(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(GCAL_ICON),
      category="actions", **kwargs)
    self.text = text
    self.add_action(GCalAction(text), is_default=True)
    
  def get_hash(self):
    return "gcal-%s" % self.text


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

  INFOS = {'icon': gtk.gdk.pixbuf_new_from_xpm_data(GCAL_ICON),
    'name': "Google Calendar",
    'description': "Add quick google calendar events",
    'version': '0.1.0.0',
  }
  
  def __init__(self):
    deskbar.interfaces.Module.__init__(self)
    client = GconfStore.get_instance().get_client()
    if not client.get_string(GCONF_GCAL_EMAIL):
      client.set_string(GCONF_GCAL_EMAIL,
          "yourname@gmail.com")
    if not client.get_string(GCONF_GCAL_PASSWORD):
      client.set_string(GCONF_GCAL_PASSWORD,
          "your_high_secur_gmail_password")
    
  def query(self, text):
    """ Respond to a deskbar query
    """
    self._emit_query_ready(text, [GCalMatch(text)])
    
  def has_config(self):
    return False
