#!/usr/bin/python
# -*- coding: latin-1 -*-

__author__ = "Jérôme Sautret <jerome @ sautret.org>"
__version__ = "1.0"
__date__ = "2008/08/16"
__license__ = "GPL v3"


"""
(c) 2007 Jérôme Sautret <jerome @ sautret.org>
Licensed under the GNU General Public License, version 3.
See the file http://www.gnu.org/licenses/gpl-3.0.txt

Gajim plugin to forward messages to TuxDroid

"""

TUX_DROID_JID="jru@jabber.debux.org"

SEND_COMMAND="gajim-remote send_single_message %(jid)s '%(subject)s' '%(message)s'"

FAKE_USER_TAG=u"%%@@%% "

import sys
sys.path.append('/opt/tuxdroid/api/python')
from tux import *

import os
import gobject
import dbus


"""
[u'myjabber.net', [u'zubrow@gmail.com/Laptop4A180FC5', u'message', 0, 0, u'normal', u'suject', 0, 0, 0, u'', 0]]
[u'myjabber.net', [u'zubrow@gmail.com/Laptop4A180FC5', u'message', 0, 0, u'chat', 0, u'active', 71513, u'JEP-0085', u'', 0]]
"""

if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
    import dbus.glib

GAJIM_SERVICE = 'org.gajim.dbus'
GAJIM_OBJ_PATH = '/org/gajim/dbus/RemoteObject'
GAJIM_INTERFACE = 'org.gajim.dbus.RemoteInterface'

# callback function to be informed about new messages in gajim
def new_message(details):
	print details

	message = details[1]
	user = message[0].split(u'@')[0]
	text = message[1].replace("'", "'\\''").replace("~", " euros")
	msgtype = message[4]

	say = "%s dit %s" % (user, text)
	say = say.encode('utf-8')
	tux_speak(say)

# Tux speak the text   
def tux_speak(text):
    tux.cmd.mouth_open()
    tux.tts.select_voice(SPK_FR_MALE,100)
    tux.tts.speak(text)
    tux.cmd.mouth_close()

def main():
        # magic starts here
        bus = dbus.SessionBus()

        # connect to NewMessage signal from gajim
        gajim_proxy_obj = bus.get_object(GAJIM_SERVICE, GAJIM_OBJ_PATH)
        gajim_dbus_iface = dbus.Interface(gajim_proxy_obj, GAJIM_INTERFACE)
        gajim_dbus_iface.connect_to_signal('NewMessage', new_message)

        print "Started"
        # get things going
        mainloop = gobject.MainLoop()
        mainloop.run()

if __name__ == "__main__":
    main()
