aboutsummaryrefslogtreecommitdiff
path: root/logout-manager.py
blob: 890f5337113258834dfb85a5014ecf7026d4b678 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
# File: logout-manager.py
# License: CC-BY-SA 4.0
# Author: bgstack15
# Startdate: 2019-06-01
# Title: GTK3 based logout manager
# Purpose: Primarily for fluxbox, this tool provides a graphical menu for various session control commands like shutdown, logout, and reboot
# History:
# Usage:
#    This is a bit for reference, but also to provide myself a little shutdown options menu, like xfce4, because fluxbox doesn't really provide one.
# Reference:
#    https://www.linuxquestions.org/questions/slackware-14/how-do-i-run-menu-and-logout-from-the-command-line-in-fluxbox-864919/
#    /mnt/public/work/python/hotplug2/
#    icon handling https://python-gtk-3-tutorial.readthedocs.io/en/latest/iconview.html
#    accelerator keys https://askubuntu.com/questions/655452/python-gtk3-keyboard-accelerators
#    gtk3 widget signals https://developer.gnome.org/gtk3/unstable/GtkWidget.html#GtkWidget-button-press-event
#    /usr/share/wicd/gtk/gui.py netentry.py wicd.ui
# Improve:
#    read commands to execute from an ini file
#    actually execute the commands
#    far future: provide graphical way to change commands run
# Documentation:

import gi, warnings
gi.require_version("Gtk","3.0")
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository.GdkPixbuf import Pixbuf

logout_manager_version="2019-06-01a"
application_icon=("system-log-out")

# graphical classes
class MainWindow(Gtk.Window):
   def __init__(self):
      Gtk.Window.__init__(self, title="Log out options")
      # for window icon
      liststore = Gtk.ListStore(Pixbuf, str)
      iconview = Gtk.IconView.new()
      iconview.set_model(liststore)
      iconview.set_pixbuf_column(0)
      iconview.set_text_column(1)
      pixbuf24 = Gtk.IconTheme.get_default().load_icon(application_icon, 24, 0)
      pixbuf32 = Gtk.IconTheme.get_default().load_icon(application_icon, 32, 0)
      pixbuf48 = Gtk.IconTheme.get_default().load_icon(application_icon, 48, 0)
      pixbuf64 = Gtk.IconTheme.get_default().load_icon(application_icon, 64, 0)
      pixbuf96 = Gtk.IconTheme.get_default().load_icon(application_icon, 96, 0)
      self.set_icon_list([pixbuf24, pixbuf32, pixbuf48, pixbuf64, pixbuf96]);

      # accel is for when you are not using the "set_use_underline" function.
      #accel = Gtk.AccelGroup()
      #accel.connect(Gdk.keyval_from_name('D'), Gdk.ModifierType.MOD1_MASK, 0, self.on_button2_accel)
      #self.add_accel_group(accel)

      # buttons
      self.grid = Gtk.Grid()
      self.add(self.grid)

      self.button1 = Gtk.Button(label="_Logout")
      self.button1.connect("button-press-event", self.on_button1_press_event)
      self.button1.connect("activate", self.on_button1_press_event) # activate covers ALT+L action and spacebar when selected
      self.buttonicon1 = Gtk.Image()
      self.buttonicon1.set_from_icon_name("system-log-out",24)
      self.button1.set_image(self.buttonicon1)
      self.button1.set_tooltip_text("Close the current user session")
      self.button1.set_always_show_image(True)
      self.button1.set_use_underline(True)
      self.grid.add(self.button1)

      self.button2 = Gtk.Button(label="Shut_down")
      self.button2.connect("button-press-event", self.on_button2_press_event)
      self.button2.connect("activate", self.on_button2_accel)
      # unnecessary because the "activate" suffices above.
      #self.button2.connect("mnemonic-activate", self.on_button2_accel)
      self.buttonicon2 = Gtk.Image()
      self.buttonicon2.set_from_icon_name("system-shutdown",24)
      self.button2.set_image(self.buttonicon2)
      self.button2.set_tooltip_text("Power off the computer")
      self.button2.set_always_show_image(True)
      self.button2.set_use_underline(True)
      self.grid.add(self.button2)

      self.button3 = Gtk.Button(label="_Reboot")
      self.button3.connect("button-press-event", self.on_button3_press_event)
      self.button3.connect("activate", self.on_button3_press_event)
      self.buttonicon3 = Gtk.Image()
      # WORKS, when it was a box object self.box.pack_start(self.button3, True, True, 3)
      # WORKS self.buttonicon3.set_from_file("/usr/share/icons/Adwaita/48x48/actions/system-log-out.png")
      # WORKS self.buttonicon3.set_from_icon_name("system-log-out",24)
      self.buttonicon3.set_from_icon_name("system-reboot",24)
      self.button3.set_image(self.buttonicon3)
      self.button3.set_tooltip_text("Reboot the computer back to the login screen")
      self.button3.set_always_show_image(True)
      self.button3.set_use_underline(True)
      self.grid.add(self.button3)

      self.button4 = Gtk.Button(label="_Cancel")
      self.button4.connect("button-press-event", self.on_button4_press_event)
      self.button4.connect("activate", self.on_button4_press_event)
      self.button4.set_tooltip_text("Do nothing; just close this window")
      self.button4.set_use_underline(True)
      self.grid.attach(self.button4,0,1,3,1)

   # logout button
   def on_button1_press_event(self, *args):
      self.do_logout(self.button1)

   # shutdown button
   def on_button2_press_event(self, widget, event):
      # check if left or right click
      if event.type == Gdk.EventType.BUTTON_PRESS:
         if event.button == 1:
            self.do_shutdown(widget)
         # eventbutton == 3 is the right-click, and its reference is my hello3.py
         #elif event.button == 3:
         #   self.on_button1_right_clicked(widget)

   # global accelerator key, when not using the set_use_underline function
   ## shutdown button from accelerator key
   #def on_button2_accel(self, *args):
   #   self.do_shutdown(self.button2)

   # accelerator key from set_use_underline function
   # shutdown button from accelerator key
   def on_button2_accel(self, *args):
      self.do_shutdown(self.button2)

   # reboot button
   def on_button3_press_event(self, *args):
      self.do_reboot(self.button3)

   # cancel button
   def on_button4_press_event(self, *args):
      self.cancel(self.button4)

   def do_shutdown(self, *args):
      print("please shut yourself down!")
      print(dir(self.props))

   def do_logout(self, widget):
      print("please log out of current session!")

   def do_reboot(self, widget):
      print("please reboot.")

   def cancel(self, widget):
      print("Cancel any logout action.")
      Gtk.main_quit()

# MAIN LOOP
win = MainWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
bgstack15