Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 21 additions & 62 deletions src/Core/ChangeInformation.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,44 @@ public class AppCenterCore.ChangeInformation : Object {
UNKNOWN,
CANCELLED,
WAITING,
RUNNING,
FINISHED
RUNNING
}

/**
* This signal is likely to be fired from a non-main thread. Ensure any UI
* logic driven from this runs on the GTK thread
*/
public signal void status_changed ();

/**
* This signal is likely to be fired from a non-main thread. Ensure any UI
* logic driven from this runs on the GTK thread
*/
public signal void progress_changed ();

public Gee.ArrayList<string> updatable_packages { get; private set; }

public bool can_cancel { public get; private set; default=true; }
public double progress { public get; private set; default=0.0f; }
public Status status { public get; private set; default=Status.UNKNOWN; }
public string status_description { public get; private set; default=_("Waiting"); }
public uint64 size;

construct {
updatable_packages = new Gee.ArrayList<string> ();
size = 0;
}

public bool has_changes () {
return updatable_packages.size > 0;
}
public Cancellable cancellable { get; private set; }
public bool can_cancel { get; private set; default = true; }
public double progress { get; private set; default = 0.0f; }
public Status status { get; private set; default = Status.UNKNOWN; }
public string status_description { get; private set; default = _("Waiting"); }

public void start () {
progress = 0.0f;
cancellable = new Cancellable ();
can_cancel = true;
status = Status.WAITING;
status_description = _("Waiting");
status_changed ();
progress_changed ();
}

public void complete () {
status = Status.FINISHED;
status_description = _("Finished");
status_changed ();
reset_progress ();
}

public void cancel () {
progress = 0.0f;
status = Status.CANCELLED;
status_description = _("Cancelling");
reset_progress ();
status_changed ();
progress_changed ();
}

public void clear_update_info () {
updatable_packages.clear ();
size = 0;
}

public void reset_progress () {
public void complete () {
can_cancel = false;
progress = 0;
status = Status.UNKNOWN;
status_description = _("Starting");
progress = 0.0f;
status_description = _("Unknown");
}

public void callback (bool can_cancel, string status_description, double progress, Status status) {
if (this.can_cancel != can_cancel || this.status_description != status_description || this.status != status) {
this.can_cancel = can_cancel;
this.status_description = status_description;
this.status = status;
status_changed ();
}
public void callback (double progress, string status_description) {
Idle.add_once (() => idle_callback (progress, status_description));
}

if (this.progress != progress) {
this.progress = progress;
progress_changed ();
private void idle_callback (double progress, string status_description) {
if (status != RUNNING) {
status = RUNNING;
}

this.progress = progress;
this.status_description = status_description;
}
}
70 changes: 35 additions & 35 deletions src/Core/FlatpakBackend.vala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class AppCenterCore.FlatpakBackend : Object {
uint64 size = 0;
for (uint i = 0; i < updatable_packages.get_n_items (); i++) {
var package = (Package) updatable_packages.get_item (i);
size += package.change_information.size;
size += package.update_information.size;
}
return size;
}
Expand Down Expand Up @@ -1623,8 +1623,8 @@ public class AppCenterCore.FlatpakBackend : Object {
unowned var args = (InstallPackageArgs)job.args;
unowned var package = args.package;
unowned var fp_package = package as FlatpakPackage;
unowned ChangeInformation? change_info = args.change_info;
unowned var cancellable = args.cancellable;
unowned var change_info = args.change_info;
unowned var cancellable = change_info.cancellable;

var bundle = package.component.get_bundle (AppStream.BundleKind.FLATPAK);
if (bundle == null) {
Expand Down Expand Up @@ -1683,7 +1683,7 @@ public class AppCenterCore.FlatpakBackend : Object {
// Calculate the progress contribution of the previous operations not including the current, hence -1
double existing_progress = (double)(current_operation - 1) / (double)total_operations;
double this_op_progress = (double)progress.get_progress () / 100.0f / (double)total_operations;
change_info.callback (true, _("Installing"), existing_progress + this_op_progress, ChangeInformation.Status.RUNNING);
change_info.callback (existing_progress + this_op_progress, _("Installing"));
});
});

Expand All @@ -1692,7 +1692,6 @@ public class AppCenterCore.FlatpakBackend : Object {
transaction.operation_error.connect ((operation, e, detail) => {
warning ("Flatpak installation failed: %s (detail: %d)", e.message, detail);
if (e is GLib.IOError.CANCELLED) {
change_info.callback (false, _("Cancelling"), 1.0f, ChangeInformation.Status.CANCELLED);
success = true;
}

Expand All @@ -1716,7 +1715,6 @@ public class AppCenterCore.FlatpakBackend : Object {
success = transaction.run (cancellable);
} catch (Error e) {
if (e is GLib.IOError.CANCELLED) {
change_info.callback (false, _("Cancelling"), 1.0f, ChangeInformation.Status.CANCELLED);
success = true;
} else {
success = false;
Expand All @@ -1732,11 +1730,10 @@ public class AppCenterCore.FlatpakBackend : Object {
job.results_ready ();
}

public async bool install_package (Package package, ChangeInformation? change_info, Cancellable? cancellable) throws GLib.Error {
public async bool install_package (Package package, ChangeInformation change_info) throws GLib.Error {
var job_args = new InstallPackageArgs ();
job_args.package = package;
job_args.change_info = change_info;
job_args.cancellable = cancellable;

var job = yield launch_job (Job.Type.INSTALL_PACKAGE, job_args);
if (job.error != null) {
Expand All @@ -1750,8 +1747,8 @@ public class AppCenterCore.FlatpakBackend : Object {
unowned var args = (RemovePackageArgs)job.args;
unowned var package = args.package;
unowned var fp_package = package as FlatpakPackage;
unowned ChangeInformation? change_info = args.change_info;
unowned var cancellable = args.cancellable;
unowned var change_info = args.change_info;
unowned var cancellable = change_info.cancellable;

unowned var bundle = package.component.get_bundle (AppStream.BundleKind.FLATPAK);
if (bundle == null) {
Expand Down Expand Up @@ -1815,7 +1812,7 @@ public class AppCenterCore.FlatpakBackend : Object {
// Calculate the progress contribution of the previous operations not including the current, hence -1
double existing_progress = (double)(current_operation - 1) / (double)total_operations;
double this_op_progress = (double)progress.get_progress () / 100.0f / (double)total_operations;
change_info.callback (true, _("Uninstalling"), existing_progress + this_op_progress, ChangeInformation.Status.RUNNING);
change_info.callback (existing_progress + this_op_progress, _("Uninstalling"));
});
});

Expand All @@ -1824,7 +1821,6 @@ public class AppCenterCore.FlatpakBackend : Object {
transaction.operation_error.connect ((operation, e, detail) => {
warning ("Flatpak removal failed: %s (detail: %d)", e.message, detail);
if (e is GLib.IOError.CANCELLED) {
change_info.callback (false, _("Cancelling"), 1.0f, ChangeInformation.Status.CANCELLED);
success = true;
}

Expand All @@ -1848,7 +1844,6 @@ public class AppCenterCore.FlatpakBackend : Object {
success = transaction.run (cancellable);
} catch (Error e) {
if (e is GLib.IOError.CANCELLED) {
change_info.callback (false, _("Cancelling"), 1.0f, ChangeInformation.Status.CANCELLED);
success = true;
} else {
success = false;
Expand All @@ -1864,11 +1859,10 @@ public class AppCenterCore.FlatpakBackend : Object {
job.results_ready ();
}

public async bool remove_package (Package package, ChangeInformation? change_info, Cancellable? cancellable) throws GLib.Error {
public async bool remove_package (Package package, ChangeInformation change_info) throws GLib.Error {
var job_args = new RemovePackageArgs ();
job_args.package = package;
job_args.change_info = change_info;
job_args.cancellable = cancellable;

var job = yield launch_job (Job.Type.REMOVE_PACKAGE, job_args);
if (job.error != null) {
Expand All @@ -1881,8 +1875,8 @@ public class AppCenterCore.FlatpakBackend : Object {
private void update_package_internal (Job job) {
unowned var args = (UpdatePackageArgs)job.args;
unowned var package = args.package;
unowned ChangeInformation change_info = args.change_info;
unowned var cancellable = args.cancellable;
unowned var change_info = args.change_info;
unowned var cancellable = change_info.cancellable;

if (user_installation == null && system_installation == null) {
critical ("Error getting flatpak installation");
Expand All @@ -1894,7 +1888,7 @@ public class AppCenterCore.FlatpakBackend : Object {
string[] user_updates = {};
string[] system_updates = {};

foreach (var updatable in package.change_information.updatable_packages) {
foreach (var updatable in package.update_information.updatable_packages) {
bool system = false;
string bundle_id = "";

Expand Down Expand Up @@ -1997,7 +1991,7 @@ public class AppCenterCore.FlatpakBackend : Object {
// Calculate the progress contribution of the previous operations not including the current, hence -1
double existing_progress = (double)(current_operation - 1) / (double)total_operations;
double this_op_progress = (double)progress.get_progress () / 100.0f / (double)total_operations;
change_info.callback (true, _("Updating"), existing_progress + this_op_progress, ChangeInformation.Status.RUNNING);
change_info.callback (existing_progress + this_op_progress, _("Updating"));
});
});

Expand All @@ -2006,7 +2000,6 @@ public class AppCenterCore.FlatpakBackend : Object {
transaction.operation_error.connect ((operation, e, detail) => {
warning ("Flatpak installation failed: %s", e.message);
if (e is GLib.IOError.CANCELLED) {
change_info.callback (false, _("Cancelling"), 1.0f, ChangeInformation.Status.CANCELLED);
success = true;
}

Expand All @@ -2024,7 +2017,6 @@ public class AppCenterCore.FlatpakBackend : Object {
success = transaction.run (cancellable);
} catch (Error e) {
if (e is GLib.IOError.CANCELLED) {
change_info.callback (false, _("Cancelling"), 1.0f, ChangeInformation.Status.CANCELLED);
success = true;
} else {
throw e;
Expand All @@ -2034,11 +2026,10 @@ public class AppCenterCore.FlatpakBackend : Object {
return success;
}

public async bool update_package (Package package, ChangeInformation? change_info, Cancellable? cancellable) throws GLib.Error {
public async bool update_package (Package package, ChangeInformation change_info) throws GLib.Error {
var job_args = new UpdatePackageArgs ();
job_args.package = package;
job_args.change_info = change_info;
job_args.cancellable = cancellable;

var job = yield launch_job (Job.Type.UPDATE_PACKAGE, job_args);
if (job.error != null) {
Expand Down Expand Up @@ -2103,14 +2094,14 @@ public class AppCenterCore.FlatpakBackend : Object {
return;
}

private void fill_runtime_updates () {
if (!runtime_updates.update_available) {
private void fill_runtime_updates (UpdateInformation info) {
if (info.updatable_packages.is_empty) {
return;
}

string runtime_desc = "";

foreach (var update in runtime_updates.change_information.updatable_packages) {
foreach (var update in info.updatable_packages) {
string bundle_id;
if (!get_package_list_key_parts (update, null, null, out bundle_id)) {
continue;
Expand All @@ -2134,10 +2125,12 @@ public class AppCenterCore.FlatpakBackend : Object {
var latest_version = ngettext (
"%u runtime with updates",
"%u runtimes with updates",
runtime_updates.change_information.updatable_packages.size
).printf (runtime_updates.change_information.updatable_packages.size);
info.updatable_packages.size
).printf (info.updatable_packages.size);
runtime_updates.latest_version = latest_version;
runtime_updates.description = "%s\n%s\n".printf (GLib.Markup.printf_escaped (_("%s:"), latest_version), runtime_desc);

runtime_updates.update_information = info;
}

public async void get_updates (Cancellable? cancellable = null) {
Expand All @@ -2147,27 +2140,34 @@ public class AppCenterCore.FlatpakBackend : Object {
// Clear any packages previously marked as updatable
for (int i = (int) n_updatable_packages - 1; i >= 0; i--) {
var package = (Package) updatable_packages.get_item (i);
package.change_information.clear_update_info ();
package.update_state ();
package.update_information = null;
}

var job = yield launch_job (Job.Type.GET_UPDATES, job_args);

var runtime_update_information = new UpdateInformation ();

foreach (var update in (Gee.ArrayList<string>)job.result.get_object ()) {
var package = package_list[update] ?? runtime_updates;
if (!package_list.has_key (update)) {
runtime_update_information.updatable_packages.add (update);
continue;
}

var package = package_list[update];
var update_info = new UpdateInformation ();

package.change_information.updatable_packages.add (update);
update_info.updatable_packages.add (update);

try {
package.change_information.size += yield get_download_size (package, cancellable, true);
update_info.size = yield get_download_size (package, cancellable, true);
} catch (Error e) {
warning ("Error getting download size for package %s: %s", update, e.message);
}

package.update_state ();
package.update_information = update_info;
}

fill_runtime_updates ();
fill_runtime_updates (runtime_update_information);
}

private void repair_internal (Job job) {
Expand Down
9 changes: 3 additions & 6 deletions src/Core/Job.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,17 @@ public class AppCenterCore.GetInstalledPackagesArgs : JobArgs {

public class AppCenterCore.InstallPackageArgs : JobArgs {
public Package package;
public ChangeInformation? change_info;
public Cancellable? cancellable;
public ChangeInformation change_info;
}

public class AppCenterCore.UpdatePackageArgs : JobArgs {
public Package package;
public ChangeInformation? change_info;
public Cancellable? cancellable;
public ChangeInformation change_info;
}

public class AppCenterCore.RemovePackageArgs : JobArgs {
public Package package;
public ChangeInformation? change_info;
public Cancellable? cancellable;
public ChangeInformation change_info;
}

public class AppCenterCore.GetDownloadSizeArgs : JobArgs {
Expand Down
Loading