Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Fixed icon not found crash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart committed Jan 22, 2019
1 parent 89e1963 commit c31ead0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/FileManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class FileManager : Object {

FileManager() {
}

public static FileManager get_instance() {
if (instance == null) {
instance = new FileManager();
Expand All @@ -22,5 +22,10 @@ public class FileManager : Object {
public void setFile(File newFile){
this.file = newFile;
}

public bool file_exists (string filePath) {
var file = File.new_for_path (filePath);
return file.query_exists ();
}
}
}
28 changes: 26 additions & 2 deletions src/IconHandler.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Granite.Widgets;
namespace Application {
public class IconHandler {

private FileManager fileManager = FileManager.get_instance();
private int icon_size = 32;

private Gtk.Image backup_icon = new Gtk.Image.from_icon_name ("package", Gtk.IconSize.DND);
Expand All @@ -22,8 +23,14 @@ public class IconHandler {
image.set_from_file_async.begin(file_photo, icon_size, icon_size, false);
return image;
}

var pixbuf = new Gdk.Pixbuf.from_file_at_size ("/snap/" + package.getName() + "/current/" + package.getName() +".png", icon_size, icon_size);

var filePath = getLocalIconPath (package);

if(filePath == "") {
return backup_icon;
}

var pixbuf = new Gdk.Pixbuf.from_file_at_size (filePath, icon_size, icon_size);

var localImage = new Gtk.Image();
localImage.set_from_pixbuf(pixbuf);
Expand All @@ -38,5 +45,22 @@ public class IconHandler {
public void set_icon_size(int icon_size){
this.icon_size = icon_size;
}

private string getLocalIconPath (Package package) {
Array<string> possibleIconPaths = new Array<string> ();
possibleIconPaths.append_val (
"/snap/" + package.getName() + "/current/" + package.getName() +".png");
possibleIconPaths.append_val (
"/snap/" + package.getName() + "/current/usr/share/icons/hicolor/48x48/apps/"
+ package.getName() + ".png");

for (int i = 0; i < possibleIconPaths.length ; i++) {
if (fileManager.file_exists (possibleIconPaths.index (i))) {
return possibleIconPaths.index (i);
}
}

return "";
}
}
}

0 comments on commit c31ead0

Please sign in to comment.