summaryrefslogtreecommitdiffstats
path: root/python/shutdown.py
blob: 5f350bb24711069af2cd53da2f58239484cd4b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
#----------------------------------------------------#
# File: 	  shutdown.py
# Version:	0.1
# Date: 	  2009-01-17
# Author:	  Florian "Bluewind" Pritz <f-p@gmx.at>
# Display a small shutdown Dialog
# Please change the image paths
#----------------------------------------------------#
# 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 3 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, see <http://www.gnu.org/licenses/>.

import gtk
import os

def create_bbox(title=None, spacing=0, layout=gtk.BUTTONBOX_SPREAD):
    bbox = gtk.VButtonBox()

    bbox.set_spacing(spacing)

    buttons = (('Cancel', 'gtk-cancel'),
               ('Suspend', 'gnome-session-suspend'),
               ('Reboot', 'gnome-session-reboot'),
               ('Halt', 'gnome-session-halt')
              )
    for text, stock_id in buttons:
      image = gtk.Image()
      image.set_from_stock(stock_id, gtk.ICON_SIZE_LARGE_TOOLBAR)
      b = gtk.Button(text)
      b.set_image(image)
      b.set_data("user_data", text)
      b.connect("clicked", click, b)
      b.set_size_request(100, -1)
      b.set_alignment(0,1)
      bbox.add(b)

    return bbox

def click(self, button):
  ret = button.get_data("user_data")
  self.hide()
  self.destroy()
  gtk.main_quit()
  
  if ret == "Reboot":
    os.system("sudo shutdown -r now")
  if ret == "Suspend":
    os.system("sudo pm-suspend")
  if ret == "Halt":
    os.system("sudo shutdown -h now")

class ButtonBox(gtk.Window):
    def __init__(self, parent=None):
        # Create the toplevel window
        gtk.Window.__init__(self)
        try:
            self.set_screen(parent.get_screen())
        except AttributeError:
            self.connect('destroy', lambda *w: gtk.main_quit())

        self.set_title("Shutdown")
        self.set_decorated(False)
        self.set_position(gtk.WIN_POS_CENTER)

        hbox = gtk.HBox()
        hbox.set_border_width(10)
        
        self.add(hbox)
        hbox.pack_start(create_bbox(None, 8, gtk.BUTTONBOX_START),
                padding=0)

        self.show_all()

def main():
  ButtonBox()
  gtk.main()

def register_iconsets(icon_info):
      iconfactory = gtk.IconFactory()
      stock_ids = gtk.stock_list_ids()
      for stock_id, file in icon_info:
          # only load image files when our stock_id is not present
          if stock_id not in stock_ids:
              pixbuf = gtk.gdk.pixbuf_new_from_file(file)
              iconset = gtk.IconSet(pixbuf)
              iconfactory.add(stock_id, iconset)
      iconfactory.add_default()

register_iconsets([('gnome-session-suspend', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-suspend.png'),
                   ('gnome-session-halt', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-halt.png'),
                   ('gnome-session-reboot', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-reboot.png')
                 ])


if __name__ == '__main__':
    main()