Skip to content

Commit

Permalink
0.2 version of openboxpm
Browse files Browse the repository at this point in the history
  • Loading branch information
esendjer committed Jul 25, 2020
1 parent 71722bb commit 5127809
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 93 deletions.
Binary file added icon/application-exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/gnome-session-hibernate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/gnome-session-reboot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/gnome-session-suspend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/system-lock-screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/system-log-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/system-shutdown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/view-refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/window-close.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
212 changes: 119 additions & 93 deletions openboxpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,139 +25,163 @@
# Example: pm_prop = dbus.Interface(pm, dbus_iface_prop)
# dobj_prop = pm_prop.Get(dbus_iface, 'ScheduledShutdown')
# pd_date = datetime.datetime.fromtimestamp(
# int(dobj_prop[1] / 1000000)).strftime('%Y-%m-%d %H:%M:%S')
# sched_sd = str(dobj_prop[0])
# int(dobj_prop[1] / 1000000)
# ).strftime('%Y-%m-%d %H:%M:%S')
# msg_sched = str(dobj_prop[0])
########################################################################################################################
# Url on to Docs of D-Bus item login1: https://www.freedesktop.org/wiki/Software/systemd/logind/
########################################################################################################################
'''

__author__ = 'esendjer'
__license__ = 'MIT'
__version__ = '0.1'
__version__ = '0.2'
__email__ = '[email protected]'
__status__ = 'Development'


from typing import Tuple
import dbus
import datetime
import tkinter as tk
from functools import partial
import os

dbus_item = 'org.freedesktop.login1' # This is D-Bus item name for login1
dbus_path = '/org/freedesktop/login1' # This is D-Bus item path for login1
dbus_iface = 'org.freedesktop.login1.Manager' # This is D-Bus item for interface Manager
dbus_iface_prop = 'org.freedesktop.DBus.Properties' # This is D-Bus item for get properties
dbus_ses_path = '/org/freedesktop/login1/session/self' # This is D-Bus item path for Session
dbus_ses_item = 'org.freedesktop.login1.Session' # This is D-Bus item name for Session

sysbus = dbus.SystemBus() # Create SystemBus object
pm = sysbus.get_object(dbus_item, dbus_path) # Create object for login1
pm_iface = dbus.Interface(pm, dbus_iface) # Create interface for login1

pm_prop = dbus.Interface(pm, dbus_iface_prop) # Create interface for get properties login1
try:
dobj_prop = pm_prop.Get(dbus_iface, 'ScheduledShutdown') # Get properties 'ScheduledShutdown' from login1
except:
dobj_prop='-1'
def create_dbus_session() -> Tuple[str, dbus.proxies.Interface, dbus.proxies.Interface, str]:
dbus_item = 'org.freedesktop.login1' # This is D-Bus item name for login1
dbus_path = '/org/freedesktop/login1' # This is D-Bus item path for login1
dbus_iface = 'org.freedesktop.login1.Manager' # This is D-Bus item for interface Manager
dbus_iface_prop = 'org.freedesktop.DBus.Properties' # This is D-Bus item for get properties
dbus_ses_path = '/org/freedesktop/login1/session/self' # This is D-Bus item path for Session
dbus_ses_item = 'org.freedesktop.login1.Session' # This is D-Bus item name for Session

pm_ses = sysbus.get_object(dbus_item, dbus_ses_path) # Create object for Session
pm_ses_iface = dbus.Interface(pm_ses, dbus_ses_item) # Create interface for Session
pm_ses_prop = dbus.Interface(pm_ses, dbus_iface_prop) # Create interface for get properties Session
sysbus = dbus.SystemBus() # Create SystemBus object
pm = sysbus.get_object(dbus_item, dbus_path) # Create object for login1
pm_iface = dbus.Interface(pm, dbus_iface) # Create interface for login1

id_seesion = pm_ses_prop.Get(dbus_ses_item, 'Id')[0]
pm_prop = dbus.Interface(pm, dbus_iface_prop) # Create interface for get properties login1
try:
dobj_prop = pm_prop.Get(dbus_iface, 'ScheduledShutdown') # Get properties 'ScheduledShutdown' from login1
except: # any exceptions
dobj_prop = '-1'

if str(dobj_prop[0]) == '':
sched_sd = 'Нет заплпнированных событий.'
elif dobj_prop == '-1':
sched_sd = 'Данные о запланированных\nсобытиях не получены.'
else:
pd_date = datetime.datetime.fromtimestamp(int(dobj_prop[1]/1000000)).strftime('%Y-%m-%d %H:%M:%S')
sched_sd = 'Заплпнирован {}, на {}.'.format(str(dobj_prop[0]), pd_date)
pm_ses = sysbus.get_object(dbus_item, dbus_ses_path) # Create object for Session
pm_ses_iface = dbus.Interface(pm_ses, dbus_ses_item) # Create interface for Session
pm_ses_prop = dbus.Interface(pm_ses, dbus_iface_prop) # Create interface for get properties Session

id_seesion = pm_ses_prop.Get(dbus_ses_item, 'Id')[0]

def get_state(oper_name):
if oper_name == 'PowerOff':
return 'disabled' if str(pm_iface.CanPowerOff()) != 'yes' else 'normal'
elif oper_name == 'Reboot':
return 'disabled' if str(pm_iface.CanReboot()) != 'yes' else 'normal'
elif oper_name == 'Suspend':
return 'disabled' if str(pm_iface.CanSuspend()) != 'yes' else 'normal'
elif oper_name == 'Hibernate':
return 'disabled' if str(pm_iface.CanHibernate()) != 'yes' else 'normal'
elif oper_name == 'HybridSleep':
return 'disabled' if str(pm_iface.CanHybridSleep()) != 'yes' else 'normal'
if str(dobj_prop[0]) == '':
msg_sched = 'The system has not scheduled tasks.'
elif dobj_prop == '-1':
msg_sched = 'The data of scheduled\ntasks was not got.'
else:
return 'disabled'


def cmd(name: object, act: str):
if act == 'Off':
pm_iface.PowerOff(True)
elif act == 'Reb':
pm_iface.Reboot(True)
elif act == 'Sus':
pm_iface.Suspend(True)
elif act == 'Hid':
pm_iface.Hibernate(True)
elif act == 'Hsl':
pm_iface.HybridSleep(True)
elif act == 'Out':
# pm_ses_iface.Terminate() # And it version is working
pm_iface.TerminateSession(str(id_seesion)) # And it version is working
else:
print('Wath?')
name.destroy()
pd_date = datetime.datetime.fromtimestamp(int(dobj_prop[1] / 1000000)).strftime('%Y-%m-%d %H:%M:%S')
msg_sched = 'The task {}, was scheduled on {}.'.format(str(dobj_prop[0]), pd_date)
return id_seesion, pm_ses_iface, pm_iface, msg_sched


def main():
wh = 400
def get_state(oper_name):
state = {
'Reboot': 'disabled' if str(pm_iface.CanReboot()) != 'yes' else 'normal',
'Suspend': 'disabled' if str(pm_iface.CanSuspend()) != 'yes' else 'normal',
'Hibernate': 'disabled' if str(pm_iface.CanHibernate()) != 'yes' else 'normal',
'HybridSleep': 'disabled' if str(pm_iface.CanHybridSleep()) != 'yes' else 'normal',
'PowerOff': 'disabled' if str(pm_iface.CanPowerOff()) != 'yes' else 'normal',
'Log Out': 'normal'
}
return state.get(oper_name) or 'disabled'


def cmd(pm_iface: dbus.proxies.Interface, tk_obj: object, act: str, id_seesion: str) -> None:
if act == 'Log Out':
# pm_ses_iface.Terminate() # And this version works
pm_iface.TerminateSession(id_seesion) # This version works
pm_actions = {
'Reboot': pm_iface.Reboot,
'Suspend': pm_iface.Suspend,
'Hibernate': pm_iface.Hibernate,
'HybridSleep': pm_iface.HybridSleep,
'PowerOff': pm_iface.PowerOff,
}
pm_actions.get(act)(True)
tk_obj.destroy()


def create_window(id_seesion: str,
# pm_ses_iface:dbus.proxies.Interface,
pm_iface: dbus.proxies.Interface,
msg_sched: str,
img_path: str) -> None:
wh = 350 # The height of window

# The list of actions
list_actions = [
'Log Out',
'PowerOff',
'Reboot',
'Suspend',
'Hibernate',
'HybridSleep'
]
dit_images = dict()

tk_root = tk.Tk()
tk_root.title('OpenBox PM')

# images of actions
icn_quit = tk.PhotoImage(file='{}/icon/application-exit.png'.format(img_path))
dit_images.update({'Log Out': tk.PhotoImage(file='{}/icon/system-log-out.png'.format(img_path))})
dit_images.update({'Reboot': tk.PhotoImage(file='{}/icon/gnome-session-reboot.png'.format(img_path))})
dit_images.update({'Suspend': tk.PhotoImage(file='{}/icon/gnome-session-suspend.png'.format(img_path))})
dit_images.update({'Hibernate': tk.PhotoImage(file='{}/icon/gnome-session-hibernate.png'.format(img_path))})
dit_images.update({'HybridSleep': tk.PhotoImage(file='{}/icon/gnome-session-hibernate.png'.format(img_path))})
dit_images.update({'PowerOff': tk.PhotoImage(file='{}/icon/system-shutdown.png'.format(img_path))})

tk_root.attributes('-zoomed', True) # Maximizing the window for detecting resolution of currently active monitor
tk_root.update_idletasks() # Updating parameters
tk_root.update_idletasks() # Updating parameters

ws = (tk_root.winfo_width() // 2) - 200 + tk_root.winfo_rootx() # Calculating position the window along the X axis
hs = (tk_root.winfo_height() // 2) - 200 + tk_root.winfo_rooty() # Calculating position the window along the Y axis
ws = (tk_root.winfo_width() // 2) - 200 + tk_root.winfo_rootx() # Calculating position the window along the X axis
hs = (tk_root.winfo_height() // 2) - 245 + tk_root.winfo_rooty() # Calculating position the window along the Y axis

tk_root.geometry('{}x{}+{}+{}'.format(wh, wh, ws, hs)) # Applying new geometry foe the window
tk_root.attributes('-zoomed', False) # Restoring size the window with new geometry
tk_root.resizable(width=False, height=False) # Disabling changes size of the window
tk_root.update_idletasks() # Updating parameters
tk_root.geometry('{}x490+{}+{}'.format(wh, ws, hs)) # Applying new geometry foe the window
tk_root.attributes('-zoomed', False) # Restoring size the window with new geometry
tk_root.resizable(width=False, height=False) # Disabling changes size of the window
tk_root.update_idletasks() # Updating parameters

tk_window = tk.Frame(tk_root)
tk_window.pack()

nbtn = partial(tk.Button, tk_window, height='2', width='30')

# button to exit
btn_quit = tk.Button(
tk_window, height='2', width='5', text="QUIT", fg="red", command=tk_root.destroy, state='active'
tk_window,
height='38',
width='100',
image=icn_quit,
text='Exit',
command=tk_root.destroy,
state='active',
compound='left'
)
btn_quit.focus_set()
btn_quit.pack(side=tk.BOTTOM)

btn_ses = nbtn(text='Log Out', command=lambda: cmd(tk_root, 'Out'))
btn_ses.pack(side=tk.BOTTOM)

btn_off = nbtn(text='PowerOff', command=lambda: cmd(tk_root, 'Off'), state=get_state('PowerOff'))
btn_off.pack(side=tk.BOTTOM)

btn_reb = nbtn(text='Reboot', command=lambda: cmd(tk_root, 'Reb'), state=get_state('Reboot'))
btn_reb.pack(side=tk.BOTTOM)

btn_sus = nbtn(text='Suspend', command=lambda: cmd(tk_root, 'Sus'), state=get_state('Suspend'))
btn_sus.pack(side=tk.BOTTOM)
btn_quit.pack(side=tk.BOTTOM, padx=10, pady=15)

btn_hib = nbtn(text='Hibernate', command=lambda: cmd(tk_root, 'Hib'), state=get_state('Hibernate'))
btn_hib.pack(side=tk.BOTTOM)
# template for actionable button
nbtn = partial(tk.Button, tk_window, height='44', width='50', compound='left')

btn_hsl = nbtn(text='HybridSleep', command=lambda: cmd(tk_root, 'Hsl'),state=get_state('HybridSleep'))
btn_hsl.pack(side=tk.BOTTOM)
# create actionable buttons
for action in list_actions:
btn = nbtn(
image=dit_images[action],
text=action,
command=lambda x=action: cmd(pm_iface, tk_root, x, id_seesion),
state=get_state(action)
)
btn.pack(side=tk.BOTTOM, padx=5, pady=2, fill='both')

label = tk.Label(tk_window, height='2', text=sched_sd)
label.pack(side=tk.BOTTOM)
# create label with info about scheduled tasks
label = tk.Label(tk_window, height='2', text=msg_sched)
label.pack(side=tk.BOTTOM, padx=5, pady=2, fill='both')

tk_root.mainloop()

Expand All @@ -168,4 +192,6 @@ def get_doc():


if __name__ == '__main__':
main()
dir_of_scrip = os.path.dirname(os.path.realpath(__file__))
id_seesion, _, pm_iface, msg_sched = create_dbus_session()
create_window(id_seesion, pm_iface, msg_sched, dir_of_scrip)
Binary file modified openboxpm_s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5127809

Please sign in to comment.