#!/usr/bin/python
#
# NX Applet
# Copyright (C) Marcelo Boveto shima 2009 <marceloshima@gmail.com>
# 
# NX Applet 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.
# 
# NX Applet 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, see <http://www.gnu.org/licenses/>.

import gtk
import os
import sys
import string
import gobject

from tacix.gtk.GTKNotification import GTKNotification
from tacix.gtk.GConfKeyboard import GConfKeyboard
from tacix.resources.TXSound import TXSound

class GTKApplet:
    def __init__(self):
        from dbus.mainloop.glib import DBusGMainLoop
        DBusGMainLoop(set_as_default=True)
        self.loop = gobject.MainLoop()

        self.icon = gtk.StatusIcon( )
        self.icon.set_from_file( "/usr/share/pixmaps/tacix-applet.png" )
        self.menu = gtk.Menu( )

        nxsessionid = os.getenv('NXSESSIONID')
        self.gtknotification = GTKNotification(nxsessionid)
        self.gtknotification.setup_signal()

        self.soundsupport = TXSound(nxsessionid)
        self.soundsupport.setup_signal()

        self.keyboardsupport = GConfKeyboard(sessionid=nxsessionid)
        self.keyboardsupport.setup_signal()

        self.draw_menu( )

    def show_menu(self, icon, button, time):
        self.menu.popup(None, None, gtk.status_icon_position_menu, button, time, icon)

    def suspend(self, menu):
        pass
        #session.suspend( )

    def terminate(self, menu):
        pass
        #session.terminate( )

    def quit(self, menu):
        self.loop.quit()

    def reapply_gnome_keyboard_settings(self, menu):
        self.keyboardsupport.workaround_gnome_keyboard()

    def draw_menu(self):
        quit = gtk.MenuItem( "Quit applet" )
        quit.connect("activate", self.quit)
        quit.show( )
        self.menu.append( quit )

        fixkeyboard = gtk.MenuItem( "Reapply gconf keyboard settings" )
        fixkeyboard.connect("activate", self.reapply_gnome_keyboard_settings)
        fixkeyboard.show( )
        self.menu.append( fixkeyboard )

        separator2 = gtk.SeparatorMenuItem( )
        separator2.show( )
        self.menu.append( separator2 )

        term_menu = gtk.MenuItem( "Terminate session" )
        term_menu.connect( "activate", self.terminate )
        term_menu.show( )
        self.menu.append( term_menu )

        susp_menu = gtk.MenuItem( "Suspend session" )
        susp_menu.connect( "activate", self.suspend )
        susp_menu.show( )
        self.menu.append( susp_menu )

        self.icon.connect( "popup-menu", self.show_menu )

    def run(self):
        self.loop.run()
 
if __name__ == "__main__":
    gtkapplet = GTKApplet()
    gtkapplet.run()

