Skip to content
Prev Previous commit
Next Next commit
Interact with system upgrade daemon
  • Loading branch information
meisenzahl committed Jan 29, 2022
commit fc920c6de92eb963a9f08cc9eb2918382b7e7de9
61 changes: 41 additions & 20 deletions src/Utils/SystemUpgrade.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,47 @@
* Authored by: Marius Meisenzahl <mariusmeisenzahl@gmail.com>
*/

public class About.SystemUpgrade {
public static void restart () {
get_system_instance ();

public class About.SystemUpgrade : Object {
public void restart () {
try {
system_instance.reboot (false);
} catch (GLib.Error e) {
critical (e.message);
}
}

public bool system_upgrade_available {
get {
if (system_upgrade_instance == null) {
return false;
}

return system_upgrade_instance.system_upgrade_available;
}
}

public void start_upgrade () {
if (system_upgrade_instance == null) {
system_upgrade_instance.start_upgrade ();
}
}

public signal void system_upgrade_progress (int percentage);

public signal void system_upgrade_finished ();

construct {
get_system_instance ();
get_system_upgrade_instance ();
}

[DBus (name = "org.freedesktop.login1.Manager")]
interface SystemInterface : Object {
public abstract void reboot (bool interactive) throws GLib.Error;
}

private static SystemInterface? system_instance;
private static void get_system_instance () {
private SystemInterface? system_instance;
private void get_system_instance () {
if (system_instance == null) {
try {
system_instance = Bus.get_proxy_sync (
Expand All @@ -50,32 +73,30 @@ public class About.SystemUpgrade {
}
}

public static bool system_upgrade_available {
get {
get_system_upgrade_instance ();

if (system_upgrade_instance == null) {
return false;
}

return system_upgrade_instance.system_upgrade_available;
}
}

[DBus (name = "io.elementary.SystemUpgrade")]
interface SystemUpgradeInterface : Object {
public abstract bool system_upgrade_available { get; }

public abstract void start_upgrade ();

public signal void system_upgrade_progress (int percentage);

public signal void system_upgrade_finished ();
}

private static SystemUpgradeInterface? system_upgrade_instance;
private static void get_system_upgrade_instance () {
private SystemUpgradeInterface? system_upgrade_instance;
private void get_system_upgrade_instance () {
if (system_upgrade_instance == null) {
try {
system_upgrade_instance = Bus.get_proxy_sync (
BusType.SESSION,
"io.elementary.settings-daemon",
"/io/elementary/settings_daemon"
);

system_upgrade_instance.system_upgrade_progress.connect ((percentage) => { system_upgrade_progress (percentage); });

system_upgrade_instance.system_upgrade_finished.connect (() => { system_upgrade_finished (); });
} catch (GLib.Error e) {
warning ("%s", e.message);
}
Expand Down
24 changes: 11 additions & 13 deletions src/Views/OperatingSystemView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ public class About.OperatingSystemView : Gtk.Grid {
}

private void show_upgrade_dialog () {
var system_upgrade = new SystemUpgrade ();

int seconds_remaining = RESTART_TIMEOUT;

var message_dialog = new Granite.MessageDialog.with_image_from_icon_name (
Expand Down Expand Up @@ -392,22 +394,18 @@ public class About.OperatingSystemView : Gtk.Grid {
suggested_button.visible = false;
stack.set_visible_child (progress_view);

progress_view.notify["fraction"].connect (() => {
if (progress_view.fraction >= 1.0) {
suggested_button.activate ();
}
});
progress_view.pulse ();

Timeout.add_seconds (1, () => {
progress_view.fraction += Random.next_double () * 0.1;
system_upgrade.system_upgrade_finished.connect (() => {
suggested_button.activate ();
});

if (progress_view.fraction >= 1.0) {
return Source.REMOVE;
}

return Source.CONTINUE;
system_upgrade.system_upgrade_progress.connect ((percentage) => {
progress_view.fraction = percentage / 100.0;
});

system_upgrade.start_upgrade ();

break;
case State.SUCCESS:
message_dialog.badge_icon = new ThemedIcon ("process-completed");
Expand Down Expand Up @@ -437,7 +435,7 @@ public class About.OperatingSystemView : Gtk.Grid {
break;
case State.RESTART:
message_dialog.destroy ();
SystemUpgrade.restart ();
system_upgrade.restart ();
break;
}
});
Expand Down