rtcwake_gui/SettingsUi.py

222 lines
6.9 KiB
Python
Raw Permalink Normal View History

2016-10-22 22:13:50 +02:00
import os
import shlex
from datetime import datetime
from subprocess import call
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Form():
def __init__(self, window, page, devices):
self.devices = devices
self.window = window
self.infoLabel = Gtk.Label("This action may require root privileges")
self.deviceCombo = Gtk.ComboBoxText()
self.deviceCombo.set_entry_text_column(0)
self.deviceCombo.connect("changed", self.deviceComboChanged)
for name, device in devices.items():
self.deviceCombo.append_text(name)
self.lineDevice = line("Device:", self.deviceCombo)
self.modeCombo = Gtk.ComboBoxText()
self.modeCombo.set_entry_text_column(0)
self.lineMode = line("Mode:", self.modeCombo)
clockModes = ["default", "local", "UTC"]
self.clockCombo = Gtk.ComboBoxText()
self.clockCombo.set_entry_text_column(0)
for mode in clockModes:
self.clockCombo.append_text(mode)
self.clockCombo.set_active(0)
self.lineClock = line("RTC clock:", self.clockCombo)
self.dateEntry = Gtk.Entry(placeholder_text="HH:MM mm/dd/YYYY")
self.dateEntry.set_alignment(1)
self.dateCal = Gtk.Button.new_with_label("CAL")
self.dateCal.connect("clicked", self.onDateEntryClick)
self.dateBox = Gtk.Box(spacing=4)
self.pack(self.dateBox, self.dateEntry)
self.pack(self.dateBox, self.dateCal)
self.lineDate = line("Date:", self.dateBox)
createButton = Gtk.Button.new_with_label("Create")
createButton.connect("clicked", self.onCreateButtonClick)
self.pack(page, self.infoLabel)
self.pack(page, self.lineDevice.box)
self.pack(page, self.lineMode.box)
self.pack(page, self.lineClock.box)
self.pack(page, self.lineDate.box)
self.pack(page, createButton)
def deviceComboChanged(self, combo):
text = combo.get_active_text()
if text != None:
self.setModes(text)
def onDateEntryClick(self, button):
dialog = DateDialog(self.window)
response = dialog.run()
cal = {}
if response == Gtk.ResponseType.OK:
calendar = dialog.calendar.get_date()
cal["day"] = str(calendar.day)
cal["month"] = str(calendar.month + 1)
cal["year"] = str(calendar.year)
if len(cal["day"]) < 2:
cal["day"] = "0" + cal["day"]
if len(cal["month"]) < 2:
cal["month"] = "0" + cal["month"]
hour = str(dialog.hoursButton.get_value_as_int())
minute = str(dialog.minutesButton.get_value_as_int())
if len(hour) < 2:
hour = "0" + hour
if len(minute) < 2:
minute = "0" + minute
date = hour + ":" + minute + " " + cal["month"] + "/" + cal["day"] + "/" + cal["year"]
self.dateEntry.set_text(date)
elif response == Gtk.ResponseType.CANCEL:
print("The Cancel button was clicked")
dialog.destroy()
def onCreateButtonClick(self, button):
device = self.deviceCombo.get_active_text()
mode = self.modeCombo.get_active_text()
clock = self.clockCombo.get_active_text()
dateText = self.dateEntry.get_text()
message = ""
error = False
if device == None:
error = True
message += "Select device"
if mode == None:
if error:
message += "\n"
else:
error = True
message += "Select mode"
if len(dateText) > 0:
now = datetime.now()
date = datetime.strptime(dateText, "%H:%M %m/%d/%Y")
time = int(date.timestamp())
if now.timestamp() >= time:
if error:
message += "\n"
else:
error = True
message += "Date can't be set to the past"
else:
if error:
message += "\n"
else:
error = True
message += "Select date"
if len(message) > 0 and error:
dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "Wrong settings")
dialog.format_secondary_text(message)
dialog.run()
dialog.destroy()
else:
times = {
"default" : "-a",
"local" : "-l",
"UTC" : "-u"
}
cmd = "rtcwake " + times[clock] + " -m " + mode + " -d " + device + " -t " + str(time)
call(shlex.split(cmd))
def setModes(self, device):
self.modeCombo.remove_all()
for mode in self.devices[device].modes:
self.modeCombo.append_text(mode)
def pack(self, page, box):
page.pack_start(box, False, True, 0)
class DateDialog(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "Select date and time", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK), resizable=False)
self.set_default_size(150, 100)
box = self.get_content_area()
box.set_spacing(5)
labelDate = Gtk.Label("Date")
self.pack(box, labelDate)
self.calendar = Gtk.Calendar()
self.pack(box, self.calendar)
labelTime = Gtk.Label("Time")
self.pack(box, labelTime)
self.hbox = Gtk.Box(spacing=5)
now = datetime.now()
adjustmentHours = Gtk.Adjustment(0, 0, 23, 1, 10, 0)
self.hoursButton = Gtk.SpinButton()
self.hoursButton.set_numeric(True)
self.hoursButton.set_adjustment(adjustmentHours)
self.hoursButton.set_value(float(now.hour))
self.hrbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL ,spacing=5)
self.pack(self.hrbox, Gtk.Label("Hour"))
self.pack(self.hrbox, self.hoursButton)
adjustmentMins = Gtk.Adjustment(0, 0, 59, 1, 10, 0)
self.minutesButton = Gtk.SpinButton()
self.minutesButton.set_numeric(True)
self.minutesButton.set_adjustment(adjustmentMins)
self.minutesButton.set_value(float(now.minute))
self.minbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL ,spacing=5)
self.pack(self.minbox, Gtk.Label("Minute"))
self.pack(self.minbox, self.minutesButton)
self.pack(self.hbox, self.hrbox)
self.pack(self.hbox, self.minbox)
self.pack(box, self.hbox)
self.show_all()
def pack(self, box, item):
box.pack_start(item, False, False, 0)
class line():
def __init__(self, name, item2):
self.box = Gtk.Box(spacing=10)
self.item1 = Gtk.Label(name, halign=Gtk.Align.START)
self.item2 = item2
self.box.pack_start(self.item1, True, True, 0)
self.box.pack_start(self.item2, True, True, 0)