Skip to content

Commit

Permalink
Remove remote icons load as it makes get_icon a
Browse files Browse the repository at this point in the history
blocking method.

Also, ensure no spinner shows when an icon is not
available (instead of failing to load icon)
  • Loading branch information
italo-capasso committed Jan 1, 2025
1 parent 4c0048a commit aafd3ad
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 47 deletions.
55 changes: 11 additions & 44 deletions src/Core/Package.vala
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class AppCenterCore.Package : Object {
public GLib.Cancellable action_cancellable { public get; private set; }
public State state { public get; private set; default = State.NOT_INSTALLED; }
public bool uses_generic_icon { public get; private set; }
public bool icon_available { public get; private set; }

public double progress {
get {
Expand Down Expand Up @@ -652,17 +653,15 @@ public class AppCenterCore.Package : Object {
public GLib.Icon get_icon (uint size, uint scale_factor) {
uint pixel_size = size * scale_factor;

uint remote_current_size = 0;
uint remote_current_scale = 0;
uint cached_current_size = 0;
uint cached_current_scale = 0;

AppStream.Icon? stock_icon = null;
AppStream.Icon? cached_icon = null;
AppStream.Icon? local_icon = null;
AppStream.Icon? remote_icon = null;

uses_generic_icon = false;
icon_available = true;
unowned var all_icons = component.get_icons ();
foreach (var icon in all_icons) {
switch (icon.get_kind ()) {
Expand Down Expand Up @@ -692,23 +691,12 @@ public class AppCenterCore.Package : Object {
}
break;
case AppStream.IconKind.REMOTE:
var icon_scale = icon.get_scale ();
var icon_width = icon.get_width () * icon_scale;

bool is_bigger = icon_width > remote_current_size
&& remote_current_size < pixel_size;
bool has_better_dpi = icon_width == remote_current_size
&& remote_current_scale < icon_scale
&& scale_factor <= icon_scale;

if (is_bigger || has_better_dpi) {
remote_icon = icon;
remote_current_size = icon_width;
remote_current_scale = icon_scale;
}
debug ("'%s' is an unknown kind of AppStream icon", icon.get_name ());
// We are not loading remote icons for now due to blocking events downloading the file
// TODO: Dynamically load remote icons without blocking get_icon method
break;
case AppStream.IconKind.UNKNOWN:
message ("'%s' is an unknown kind of AppStream icon", icon.get_name ());
debug ("'%s' is an unknown kind of AppStream icon", icon.get_name ());
break;
}
}
Expand All @@ -723,34 +711,13 @@ public class AppCenterCore.Package : Object {
file = File.new_for_path (cached_icon.get_filename ());
} else if (local_icon != null) {
file = File.new_for_path (local_icon.get_filename ());
} else if (remote_icon != null) {
// We need to check if URL is valid and an actual image before loading
var url = remote_icon.get_url ();
try {
var session = new Soup.Session ();
session.set_timeout (2);
var msg = new Soup.Message ("GET", url);
session.send_and_read (msg);

var content_type = msg.response_headers.get_content_type (null);
if (msg.status_code == 200 && content_type != null && content_type.contains ("image")) {
file = File.new_for_uri (url);
} else {
warning ("Could not load remote_icon %s: Status error, bad url, or not an image", url);
}
session.abort ();
} catch (Error e) {
warning ("Could not load remote_icon %s: %s", url, e.message);
}
} else {
// Either a remote or unkonw icon type
icon_available = false;
}

try {
if (file != null && file.query_exists ()) {
// Using FileIcon for some remote icons causes a crash, BytesIcon works though!
return new BytesIcon (file.load_bytes ());
}
} catch (Error e) {
warning ("Failed to load icon %s: %s", get_name (), e.message);
if (file != null && file.query_exists ()) {
return new FileIcon (file);
}

uses_generic_icon = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Views/AppInfoView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public class AppCenter.Views.AppInfoView : Adw.NavigationPage {
valign = Gtk.Align.CENTER,
width_request = 104,
height_request = 104,
visible = package.uses_generic_icon
visible = package.uses_generic_icon && package.icon_available
};
spinner.start ();
spinner.add_css_class ("spinner");
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/AppContainers/ListPackageRowGrid.vala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class AppCenter.Widgets.ListPackageRowGrid : AbstractPackageRowGrid {
valign = Gtk.Align.CENTER,
width_request = 40,
height_request = 40,
visible = package.uses_generic_icon
visible = package.uses_generic_icon && package.icon_available
};
spinner.start ();
spinner.add_css_class ("spinner-small");
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Banner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class AppCenter.Widgets.Banner : Gtk.Button {
description: package.get_description (),
icon: pkg_icon,
brand_color: package.get_color_primary (),
uses_generic_icon: package.uses_generic_icon
uses_generic_icon: package.uses_generic_icon && package.icon_available
);
}

Expand Down

0 comments on commit aafd3ad

Please sign in to comment.