#!/usr/bin/env python
#   LASH 
#
#   Copyright (C) 2008 Nedko Arnaudov <nedko@arnaudov.name>
#
#   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 of the License, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

control_interface_name = 'org.nongnu.LASH.Control'
service_name = 'org.nongnu.LASH'

import sys
import os
import time
from traceback import print_exc

import dbus

def bool_convert(str_value):
    if str_value.lower() == "false":
        return False

    if str_value.lower() == "off":
        return False

    if str_value.lower() == "no":
        return False

    if str_value == "0":
        return False

    if str_value.lower() == "(null)":
        return False

    return bool(str_value)

def dbus_type_to_python_type(dbus_value):
    if type(dbus_value) == dbus.Boolean:
        return bool(dbus_value)
    if type(dbus_value) == dbus.Int32 or type(dbus_value) == dbus.UInt32:
        return int(dbus_value)
    return dbus_value

def dbus_type_to_type_string(dbus_value):
    if type(dbus_value) == dbus.Boolean:
        return "bool"
    if type(dbus_value) == dbus.Int32:
        return "sint"
    if type(dbus_value) == dbus.UInt32:
        return "uint"
    if type(dbus_value) == dbus.Byte:
        return "char"
    if type(dbus_value) == dbus.String:
        return "str"

    return None                         # throw exception here?

def dbus_typesig_to_type_string(type_char):
    type_char = str(type_char)
    if type_char == 'i':
        return "sint"
    if type_char == 'u':
        return "uint"
    if type_char == 'y':
        return "char"
    if type_char == 's':
        return "str"
    if type_char == 'b':
        return "bool"

    print 'shit'
    return None                         # throw exception here?

def main():
    if len(sys.argv) == 1:
        print "Usage: %s [command] [command] ..." % os.path.basename(sys.argv[0])
        print "Commands:"
        print "    exit                 - exit lash dbus service"
        print "    list                 - list projects"
        print "    open <projectname>   - open project"
        print "    save                 - save all open projects"
        print "    close                - close all open projects"
        sys.exit(0)
    
    bus = dbus.SessionBus()
    lash = None

    # check arguments
    index = 1
    while index < len(sys.argv):
        arg = sys.argv[index]
        index += 1
        try:
	    if not lash:
		lash = bus.get_object(service_name, "/")
		control_iface = dbus.Interface(lash, control_interface_name)
	    
            if arg == "exit":
                print "--- exit"
                control_iface.Exit()
		time.sleep(1)
		# we have deactivated the object and we need to get new connection if there are more commands
		lash = None
		control_iface = None
            elif arg == 'list':
                print "--- projects list"
                projects = control_iface.ProjectsGetAvailable()
                for project in projects:
                    print project
            elif arg == 'open':
                if index >= len(sys.argv):
                    print "project open command requires project name argument"
                    sys.exit()

                arg = sys.argv[index]
                index += 1

		open_options = {}
		#open_options["option1"] = "asd"
		#open_options["option2"] = True

		control_iface.ProjectOpen(arg, open_options)
            elif arg == 'save':
		control_iface.ProjectsSaveAll()
            elif arg == 'close':
		control_iface.ProjectsCloseAll()
            else:
                print "Unknown command '%s'" % arg
        except dbus.DBusException, e:
            print "DBus exception: %s" % str(e)

if __name__ == '__main__':
    main()
