Skip to content

Commit 68ce70b

Browse files
committed
Feature: Allow GUI to detect available update and install it (AdnanHodzic#587)
* rename verify_update and give return value * change verify_update call * GUI: add update feature
1 parent 30f2035 commit 68ce70b

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

auto_cpufreq/bin/auto_cpufreq.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ def config_info_dialog():
248248
elif subprocess.run(["bash", "-c", "command -v pacman >/dev/null 2>&1"]).returncode == 0 and subprocess.run(["bash", "-c", "pacman -Q auto-cpufreq >/dev/null 2>&1"]).returncode == 0:
249249
print("Arch-based distribution with AUR support detected. Please refresh auto-cpufreq using your AUR helper.")
250250
else:
251-
verify_update()
251+
is_new_update = check_for_update()
252+
if not is_new_update:
253+
return
252254
ans = input("Do you want to update auto-cpufreq to the latest release? [Y/n]: ").strip().lower()
253255
if not os.path.exists(custom_dir):
254256
os.makedirs(custom_dir)

auto_cpufreq/bin/auto_cpufreq_gtk.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
from auto_cpufreq.gui.app import ToolWindow
1212

1313
def main():
14+
GLib.set_prgname("auto-cpufreq")
1415
win = ToolWindow()
1516
win.connect("destroy", Gtk.main_quit)
1617
win.show_all()
17-
GLib.set_prgname("auto-cpufreq")
18+
win.handle_update()
1819
Gtk.main()
1920

2021
if __name__ == "__main__":

auto_cpufreq/core.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ def app_version():
165165
except Exception as e:
166166
print(repr(e))
167167
pass
168-
def verify_update():
168+
169+
def check_for_update():
170+
# returns True if a new release is available from the GitHub repo
171+
169172
# Specify the repository and package name
170173
# IT IS IMPORTANT TO THAT IF THE REPOSITORY STRUCTURE IS CHANGED, THE FOLLOWING FUNCTION NEEDS TO BE UPDATED ACCORDINGLY
171174
# Fetch the latest release information from GitHub API
@@ -192,10 +195,11 @@ def verify_update():
192195
# Compare the latest version with the installed version and perform update if necessary
193196
if latest_version == installed_version:
194197
print("auto-cpufreq is up to date")
195-
exit(0)
198+
return False
196199
else:
197200
print(f"Updates are available,\nCurrent version: {installed_version}\nLatest version: {latest_version}")
198201
print("Note that your previous custom settings might be erased with the following update")
202+
return True
199203

200204
def new_update(custom_dir):
201205
os.chdir(custom_dir)

auto_cpufreq/gui/app.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
import os
88
import sys
9+
from contextlib import redirect_stdout
10+
from io import StringIO
11+
from subprocess import run, PIPE
12+
import shutil
913
from threading import Thread
1014

1115
sys.path.append("../")
12-
from auto_cpufreq.core import is_running
13-
from auto_cpufreq.gui.objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox, DropDownMenu, DaemonNotRunningView
16+
from auto_cpufreq.core import is_running, check_for_update, remove_daemon, new_update
17+
from auto_cpufreq.gui.objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox, DropDownMenu, DaemonNotRunningView, UpdateDialog
1418

1519
if os.getenv("PKG_MARKER") == "SNAP":
1620
ICON_FILE = "/snap/auto-cpufreq/current/icon.png"
@@ -20,6 +24,8 @@
2024
CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css"
2125

2226
HBOX_PADDING = 20
27+
PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n"
28+
2329

2430
class ToolWindow(Gtk.Window):
2531
def __init__(self):
@@ -72,6 +78,31 @@ def snap(self):
7278
box.pack_start(button, False, False, 0)
7379
self.add(box)
7480

81+
def handle_update(self):
82+
new_stdout = StringIO()
83+
with redirect_stdout(new_stdout):
84+
is_new_update = check_for_update()
85+
if not is_new_update:
86+
return
87+
captured_output = new_stdout.getvalue().splitlines()
88+
dialog = UpdateDialog(self, captured_output[1], captured_output[2])
89+
response = dialog.run()
90+
dialog.destroy()
91+
if response != Gtk.ResponseType.YES:
92+
return
93+
updater = run(["pkexec", "auto-cpufreq", "--update"], input="y\n", encoding="utf-8", stderr=PIPE)
94+
if updater.stderr == PKEXEC_ERROR:
95+
error = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Error updating")
96+
error.format_secondary_text("Authorization Failed")
97+
error.run()
98+
error.destroy()
99+
return
100+
success = Gtk.MessageDialog(self, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Update successful")
101+
success.format_secondary_text("The app will now close. Please reopen to apply changes")
102+
success.run()
103+
success.destroy()
104+
exit(0)
105+
75106
def daemon_not_running(self):
76107
self.box = DaemonNotRunningView(self)
77108
self.add(self.box)

auto_cpufreq/gui/objects.py

+17
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ def __init__(self, parent):
242242
self.box.pack_start(self.love, False, False, 0)
243243
self.show_all()
244244

245+
class UpdateDialog(Gtk.Dialog):
246+
def __init__(self, parent, current_version: str, latest_version: str):
247+
super().__init__(title="Update Available", transient_for=parent)
248+
self.box = self.get_content_area()
249+
self.set_default_size(400, 100)
250+
self.add_buttons("Update", Gtk.ResponseType.YES, "Cancel", Gtk.ResponseType.NO)
251+
self.label = Gtk.Label(label="An update is available\n")
252+
self.current_version = Gtk.Label(label=current_version + "\n")
253+
self.latest_version = Gtk.Label(label=latest_version + "\n")
254+
255+
self.box.pack_start(self.label, True, False, 0)
256+
self.box.pack_start(self.current_version, True, False, 0)
257+
self.box.pack_start(self.latest_version, True, False, 0)
258+
259+
self.show_all()
260+
261+
245262
class ConfirmDialog(Gtk.Dialog):
246263
def __init__(self, parent, message: str):
247264
super().__init__(title="Confirmation", transient_for=parent)

0 commit comments

Comments
 (0)