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

# -----------------------------------------------------------------------------
# Tux Droid - Pidgin InterFace
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# -----------------------------------------------------------------------------
# $Id: $
# -----------------------------------------------------------------------------

"""
CHANGES
=======
2007/05/26 - version 0.2:
    - Tuxdroid speaks with TTS voices that say the contact on pidgin

2007/05/21 - version 0.1:
    - Initial version

TODO LIST
=========
- Make some actions when a conversation started
- Internationalization
- Improvement of conversion received message (html2text)

"""

__author__  = "Thomas CARPENTIER <carpentier.th@free.fr> & Julien Ruchaud"
__appname__ = "TuxPidgin"
__version__ = "0.2"
__date__    = "2007/05/26"
__license__ = "GPL"

# -----------------------------------------------
# Initalization of modules
# uses objects "tux" and "tss"
# -----------------------------------------------
import sys
sys.path.append('/opt/tuxdroid/api/python')
from tux import *

# Others
import dbus
import dbus.glib
import dbus.decorators
import gobject
import os
import re

# This method is excute when a message is receive
def receivedimmsg(account, name, message, conversation, flags):
    buddy = purple.PurpleFindBuddy(account, name)
    if buddy != 0:
        alias = purple.PurpleBuddyGetAlias(buddy)
    else:
        alias = name

    text = "%s says %s" % (alias, message)
    text = re.sub("<.*?>", "", text)#html2text
    tuxspeak(text)

# This method is excute when a buddy is sign on
def buddysignedon(buddyid):
    alias = purple.PurpleBuddyGetAlias(buddyid)

    text = "%s is online" % alias
    tuxspeak(text)

# Tux speak the text   
def tuxspeak(text):
    print text
    tux.cmd.mouth_open()
    tux.tts.select_voice(2,100)
    tux.tts.speak(text)
    tux.cmd.mouth_close()

# Main
bus = dbus.SessionBus()
obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")

bus.add_signal_receiver(receivedimmsg,
                        dbus_interface = "im.pidgin.purple.PurpleInterface",
                        signal_name = "ReceivedImMsg")

bus.add_signal_receiver(buddysignedon,
                        dbus_interface = "im.pidgin.purple.PurpleInterface",
                        signal_name = "BuddySignedOn")

print "TuxPidgin is started"
loop = gobject.MainLoop()
loop.run()
