#!/usr/bin/python
#-*- coding: utf-8 -*-
#  
#  Copyright (c) 2007,2008 Canonical
#  
#  Author: Oliver Grawert <ogra@canonical.com>
# 
#  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import gtk
import egg.trayicon
import subprocess

# constant string
tipstring = 'Click for %s screen'

# variable strings
bigtxt = 'extended'
smalltxt = 'normal'

class SwitchTrayIcon(egg.trayicon.TrayIcon):
    def __init__(self, name):
        super(SwitchTrayIcon, self).__init__(name)

        subprocess.Popen(['/usr/bin/xrandr', '-s', '800x480'])

        self.box = gtk.EventBox()
        self.tips = gtk.Tooltips()
        self.image = gtk.Image()

        self.tips.enable()

        self.box.add(self.image)
        self.box.connect("button_press_event", self.click_handler)

        self.add(self.box)

        if self.isbig() == True:
            self.switch_ui(smalltxt, 'gtk-leave-fullscreen')
        else:
            self.switch_ui(bigtxt, 'gtk-fullscreen')

    def click_handler(self, widget, event):
        if self.isbig() == True:
            size = '800x480'
            self.switch_ui(bigtxt, 'gtk-fullscreen')
        else:
            self.switch_ui(smalltxt, 'gtk-leave-fullscreen')
            size = '800x600'

        subprocess.Popen(['/usr/bin/xrandr', '-s', size])

    def isbig(self):
        data = subprocess.Popen(['xrandr -q|grep "*"'], shell=True, stdout=subprocess.PIPE)
        if int(data.stdout.read().split('x')[1].split()[0]) == 600:
            return True

    def switch_ui(self, txt, img):
        self.tips.set_tip(self, tipstring % txt)
        self.image.set_from_stock(img, gtk.ICON_SIZE_MENU)

if __name__ == "__main__":
    icon = SwitchTrayIcon("Screen Status")
    icon.show_all()
    gtk.main()
