Skip to content

Commit

Permalink
AppListUpdateView: DRY Update All (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Jul 3, 2024
1 parent 80ab9ea commit e31bf55
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
49 changes: 31 additions & 18 deletions src/Core/UpdateManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,7 @@ public class AppCenterCore.UpdateManager : Object {
runtime_updates.update_state ();

if (AppCenter.App.settings.get_boolean ("automatic-updates")) {
debug ("Update Flatpaks");
for (int i = 0; i < updates_liststore.n_items; i++) {
var package = (Package) updates_liststore.get_item (i);
if (!package.should_pay) {
debug ("Update: %s", package.get_name ());
try {
yield package.update (false);

updates_liststore.remove (i);
i--;

updates_size -= package.change_information.size;
} catch (Error e) {
warning ("Updating %s failed: %s", package.get_name (), e.message);
}
}
}
yield update_all (cancellable);
} else {
var application = Application.get_default ();
if (updates_number > 0) {
Expand Down Expand Up @@ -201,6 +185,35 @@ public class AppCenterCore.UpdateManager : Object {
return updates_number;
}

public async void update_all (Cancellable? cancellable) throws Error {
for (int i = 0; i < updates_liststore.n_items; i++) {
if (cancellable != null && cancellable.is_cancelled ()) {
return;
}

var package = (Package) updates_liststore.get_item (i);
if (!package.should_pay) {
debug ("Update: %s", package.get_name ());
try {
yield package.update (false);
} catch (Error e) {
// If one package update was cancelled, drop out of the loop of updating the rest
if (e is GLib.IOError.CANCELLED) {
break;
}

warning ("Updating %s failed: %s", package.get_name (), e.message);
throw (e);
}

updates_liststore.remove (i);
i--;

updates_size -= package.change_information.size;
}
}
}

public void cancel_updates (bool cancel_timeout) {
cancellable.cancel ();

Expand Down Expand Up @@ -280,7 +293,7 @@ public class AppCenterCore.UpdateManager : Object {
return GLib.Source.REMOVE;
});

get_updates ();
get_updates (cancellable);
}

private int compare_package_func (Object object1, Object object2) {
Expand Down
55 changes: 22 additions & 33 deletions src/Views/AppListUpdateView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -318,51 +318,40 @@ namespace AppCenter.Views {
}

private void on_update_all () {
perform_all_updates.begin ();
}

private async void perform_all_updates () {
if (updating_all_apps) {
return;
}

update_all_button.sensitive = false;
updating_all_apps = true;
set_actions_enabled (false);

unowned var update_manager = AppCenterCore.UpdateManager.get_default ();
update_manager.update_all.begin (null, (obj, res) => {
try {
update_manager.update_all.end (res);
} catch (Error e) {
var fail_dialog = new UpgradeFailDialog (null, e.message) {
modal = true,
transient_for = (Gtk.Window) get_root ()
};
fail_dialog.present ();
}

set_actions_enabled (true);
});
}

private void set_actions_enabled (bool enabled) {
updating_all_apps = !enabled;
update_all_button.sensitive = enabled;

var row = list_box.get_first_child ();
while (row != null) {
if (row is Gtk.ListBoxRow) {
((Widgets.InstalledPackageRowGrid) row.get_child ()).action_sensitive = false;
((Widgets.InstalledPackageRowGrid) row.get_child ()).action_sensitive = enabled;
}

row = row.get_next_sibling ();
}

unowned var update_manager = AppCenterCore.UpdateManager.get_default ();
for (int i = 0; i < update_manager.updates_liststore.get_n_items (); i++) {
var package = (AppCenterCore.Package) update_manager.updates_liststore.get_item (i);
if (package.update_available && !package.should_pay) {
try {
yield package.update (false);
} catch (Error e) {
// If one package update was cancelled, drop out of the loop of updating the rest
if (e is GLib.IOError.CANCELLED) {
break;
} else {
var fail_dialog = new UpgradeFailDialog (package, e.message) {
modal = true,
transient_for = (Gtk.Window) get_root ()
};
fail_dialog.present ();
break;
}
}
}
}

yield AppCenterCore.UpdateManager.get_default ().get_updates ();

updating_all_apps = false;
}

private int compare_installed_func (Object object1, Object object2) {
Expand Down

0 comments on commit e31bf55

Please sign in to comment.