|
6 | 6 |
|
7 | 7 | import os
|
8 | 8 | import sys
|
| 9 | +from contextlib import redirect_stdout |
| 10 | +from io import StringIO |
| 11 | +from subprocess import run, PIPE |
| 12 | +import shutil |
9 | 13 | from threading import Thread
|
10 | 14 |
|
11 | 15 | 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 |
14 | 18 |
|
15 | 19 | if os.getenv("PKG_MARKER") == "SNAP":
|
16 | 20 | ICON_FILE = "/snap/auto-cpufreq/current/icon.png"
|
|
20 | 24 | CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css"
|
21 | 25 |
|
22 | 26 | HBOX_PADDING = 20
|
| 27 | +PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n" |
| 28 | + |
23 | 29 |
|
24 | 30 | class ToolWindow(Gtk.Window):
|
25 | 31 | def __init__(self):
|
@@ -72,6 +78,31 @@ def snap(self):
|
72 | 78 | box.pack_start(button, False, False, 0)
|
73 | 79 | self.add(box)
|
74 | 80 |
|
| 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 | + |
75 | 106 | def daemon_not_running(self):
|
76 | 107 | self.box = DaemonNotRunningView(self)
|
77 | 108 | self.add(self.box)
|
|
0 commit comments