#!/usr/bin/python
# -*- coding: UTF-8 -*-


# PyGS SMS (Python Gammu Send SMS) -- Send SMS with a cell phone using Gammu
#
# Copyright (C) 2011  Fabien LOISON <http://www.flogisoft.com/>
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.


SMS_MAX_LEN = 1600


import pygtk
pygtk.require("2.0")
import gtk
import gammu


class GUI(object):

    """ Implement the GUI

    glade_file -- path of the glade file for building the GUI

    """

    def __init__(self, glade_file):
        self.win = gtk.Builder()
        self.win.add_from_file(glade_file)
        #
        self.txtbuf = self.win.get_object("txtbuf_message")
        self.sndbtn = self.win.get_object("btn_send")
        self.charlb = self.win.get_object("lb_chars")
        self.charlb.set_label("0 / %i" % SMS_MAX_LEN)
        self.phonenumber = self.win.get_object("entry_number")
        #
        self.win.connect_signals(self)
        # GTK main loop
        gtk.main()

    def _on_winMain_destroy(self, widget, datas=None):
        """ Called when the window is closed """
        gtk.main_quit()

    def on_txtbuf_message_changed(self, widget):
        msglen = self.txtbuf.get_char_count()
        self.charlb.set_label("%i / %i" % (msglen, SMS_MAX_LEN))
        if msglen == 0 or msglen > SMS_MAX_LEN:
            self.sndbtn.set_sensitive(False)
        else:
            self.sndbtn.set_sensitive(True)

    def on_btn_send_clicked(self, widget):
        if self.phonenumber.get_text() != "":
            sms.send(self.txtbuf.get_text(
                        self.txtbuf.get_start_iter(),
                        self.txtbuf.get_end_iter(),
                        ),
                    self.phonenumber.get_text(),
                    )
            self.txtbuf.set_text("")
            self.sndbtn.set_sensitive(False)


class SMS(object):
    def __init__(self):
        self._sm = gammu.StateMachine()
        self.connected = False

    def connect(self):
        if not self.connected:
            self._sm.ReadConfig()
            self._sm.Init()
            self.connected = True

    def send(self, message, phonenumber):
        if not self.connected:
            self.connect()
        smsinfos = {
                'Class': 1,
                'Text': message[0:SMS_MAX_LEN],
                'SMSC': {'Location': 1},
                'Number': self._check_phonenumber(phonenumber),
                }
        self._sm.SendSMS(smsinfos)

    def _check_phonenumber(self, phonenumber):
        #FIXME
        phonenumber = phonenumber.replace(" ", "")
        phonenumber = phonenumber.replace(".", "")
        phonenumber = phonenumber.replace("-", "")
        return phonenumber


if __name__ == "__main__":
    sms = SMS()
    gui = GUI("pygs_sms.glade")


