app no longer needs root to start, only asks when needed

This commit is contained in:
shadeyg56 2023-02-26 15:34:25 -06:00
parent 3a4fd1dc3f
commit 3a8eaaf03e
4 changed files with 30 additions and 20 deletions

View File

@ -15,7 +15,7 @@ CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css"
HBOX_PADDING = 20 HBOX_PADDING = 20
class MyWindow(Gtk.Window): class ToolWindow(Gtk.Window):
def __init__(self): def __init__(self):
super().__init__(title="auto-cpufreq") super().__init__(title="auto-cpufreq")
self.set_default_size(600, 480) self.set_default_size(600, 480)
@ -80,8 +80,7 @@ class MyWindow(Gtk.Window):
return True return True
win = ToolWindow()
win = MyWindow()
win.connect("destroy", Gtk.main_quit) win.connect("destroy", Gtk.main_quit)
win.show_all() win.show_all()
GLib.set_application_name("auto-cpufreq") GLib.set_application_name("auto-cpufreq")

View File

@ -9,11 +9,13 @@ import os
import platform as pl import platform as pl
sys.path.append("../../") sys.path.append("../../")
from subprocess import getoutput, call from subprocess import getoutput, run, PIPE
from auto_cpufreq.core import sysinfo, distro_info, set_override, get_override, get_formatted_version, dist_name, deploy_daemon, remove_daemon from auto_cpufreq.core import sysinfo, distro_info, set_override, get_override, get_formatted_version, dist_name, deploy_daemon, remove_daemon
from io import StringIO from io import StringIO
PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n"
if os.getenv("PKG_MARKER") == "SNAP": if os.getenv("PKG_MARKER") == "SNAP":
auto_cpufreq_stats_path = "/var/snap/auto-cpufreq/current/auto-cpufreq.stats" auto_cpufreq_stats_path = "/var/snap/auto-cpufreq/current/auto-cpufreq.stats"
else: else:
@ -32,7 +34,7 @@ def get_version():
return getoutput("echo \(Snap\) $SNAP_VERSION") return getoutput("echo \(Snap\) $SNAP_VERSION")
# aur package # aur package
elif dist_name in ["arch", "manjaro", "garuda"]: elif dist_name in ["arch", "manjaro", "garuda"]:
aur_pkg_check = call("pacman -Qs auto-cpufreq > /dev/null", shell=True) aur_pkg_check = run("pacman -Qs auto-cpufreq > /dev/null", shell=True)
if aur_pkg_check == 1: if aur_pkg_check == 1:
return get_formatted_version() return get_formatted_version()
else: else:
@ -77,7 +79,10 @@ class RadioButtonView(Gtk.Box):
def on_button_toggled(self, button, override): def on_button_toggled(self, button, override):
if button.get_active(): if button.get_active():
set_override(override) result = run(f"pkexec auto-cpufreq --force={override}", shell=True, stdout=PIPE, stderr=PIPE)
if result.stderr.decode() == PKEXEC_ERROR:
self.set_selected()
def set_selected(self): def set_selected(self):
override = get_override() override = get_override()
@ -170,7 +175,9 @@ class DropDownMenu(Gtk.MenuButton):
confirm.destroy() confirm.destroy()
if response == Gtk.ResponseType.YES: if response == Gtk.ResponseType.YES:
try: try:
remove_daemon() result = run("pkexec auto-cpufreq --remove", shell=True, stdout=PIPE, stderr=PIPE)
if result.stderr.decode() == PKEXEC_ERROR:
raise Exception("Authorization was cancelled")
dialog = Gtk.MessageDialog( dialog = Gtk.MessageDialog(
transient_for=parent, transient_for=parent,
message_type=Gtk.MessageType.INFO, message_type=Gtk.MessageType.INFO,
@ -250,7 +257,9 @@ class DaemonNotRunningView(Gtk.Box):
def install_daemon(self, button, parent): def install_daemon(self, button, parent):
try: try:
deploy_daemon() result = run("pkexec auto-cpufreq --install", shell=True, stdout=PIPE, stderr=PIPE)
if result.stderr.decode() == PKEXEC_ERROR:
raise Exception("Authorization was cancelled")
dialog = Gtk.MessageDialog( dialog = Gtk.MessageDialog(
transient_for=parent, transient_for=parent,
message_type=Gtk.MessageType.INFO, message_type=Gtk.MessageType.INFO,

View File

@ -12,8 +12,8 @@
<allow_inactive>auth_admin</allow_inactive> <allow_inactive>auth_admin</allow_inactive>
<allow_active>auth_admin</allow_active> <allow_active>auth_admin</allow_active>
</defaults> </defaults>
<annotate key="org.freedesktop.policykit.exec.path">/opt/auto-cpufreq/venv/bin/python</annotate> <annotate key="org.freedesktop.policykit.exec.path">/opt/auto-cpufreq/venv/bin/auto-cpufreq</annotate>
<annotate key="org.freedesktop.policykit.exec.argv1">/opt/auto-cpufreq/venv/bin/app.py</annotate> <!-- <annotate key="org.freedesktop.policykit.exec.argv1">/opt/auto-cpufreq/venv/bin/auto-cpufreq</annotate> -->
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate> <!-- <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate> -->
</action> </action>
</policyconfig> </policyconfig>

View File

@ -5,12 +5,14 @@ venv_dir=/opt/auto-cpufreq/venv
. "${venv_dir}/bin/activate" . "${venv_dir}/bin/activate"
python_command="${venv_dir}/bin/python ${venv_dir}/bin/app.py" python_command="${venv_dir}/bin/python ${venv_dir}/bin/app.py"
if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then # if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
# necessary for running on wayland # # necessary for running on wayland
xhost +SI:localuser:root # xhost +SI:localuser:root
pkexec ${python_command} # pkexec ${python_command}
xhost -SI:localuser:root # xhost -SI:localuser:root
xhost # xhost
else # else
pkexec ${python_command} # pkexec ${python_command}
fi # fi
${python_command}