# -*- coding: utf-8 -*-

import gtk

import adesk.plugin as Plg
import adesk.core as Core
import adesk.ui as UI

class Plugin(Plg.Plugin):
    def __init__(self, bar, settings):
        Plg.Plugin.__init__(self, bar, settings)
        self.settings = settings
        self.can_zoom = True
        
        # FIXME !! new in v0.3.9
        if not self.settings.has_key('show_label'):
            self.settings['show_label'] = True
        else:
            if self.settings['show_label'] in ('true','True','1','yes'):
                self.settings['show_label'] = True
            else:
                self.settings['show_label'] = False
        if not self.settings.has_key('icon_size'):
            self.settings['icon_size'] = 32
        else:
            self.settings['icon_size'] = int(self.settings['icon_size'])
            
        self.session = Session(self, bar)

    def onClick(self, widget, event):
        self.session.toggle()

    def resize(self):
        self.set_size_request(self.cfg['icon_size'], self.cfg['icon_size'])

class Session(UI.PopupWindow):
    def __init__(self, plugin, bar):
        UI.PopupWindow.__init__(self, bar, plugin)

        box = gtk.VBox(False, 0)
        box.set_border_width(0)
        self.add(box)

        session = { 'logout':('Log Out','log out the session.'),
                    'hibernate':('Hibernate','save session and power off.'),
                    'suspend':('Suspend','suspend session.'),
                    'reboot':('Reboot','restart computer.'),
                    'shutdown':('Shut Down','power off computer.'),
                    'lockscreen':('Lock Screen','lock screen to prevent unauthotised use.'),
                  }

        for action in ('lockscreen','logout','hibernate','suspend','reboot','shutdown'):
            if not plugin.settings[action] == '':
                image = 'images/plugins/session/%s.png' % action
                if plugin.settings['show_label']:
                    label = '%s\n%s' % (session[action][0], session[action][1])
                    button = Core.image_button(label, image, plugin.settings['icon_size'])
                else:
                    button = gtk.Button()
                    button.set_relief(gtk.RELIEF_NONE)
                    img = gtk.Image()
                    img.set_from_pixbuf(Core.pixbuf_from_file(image, plugin.settings['icon_size']))
                    button.add(img)
                button.connect("button-release-event", self.onClicked, plugin.settings[action])
                box.pack_start(button, False)

        box.show_all()

    def onClicked(self, widget, event, action):
        Core.launch_command(action)
        ## hide window
        self.toggle(self)
