Skip to content

Commit

Permalink
Refactor internal icon loading code
Browse files Browse the repository at this point in the history
  * Legacy icon.png is no longer supported.
  * Legacy nuvolaplayer3_XXX icons are also no longer supported.
  * eu.tiliado.NuvolaAppXxx is used everywhere.

Issue: #353

Signed-off-by: Jiří Janoušek <[email protected]>
  • Loading branch information
jiri-janousek committed Jun 23, 2017
1 parent c4ca42c commit 75bad79
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 103 deletions.
45 changes: 12 additions & 33 deletions src/nuvolakit-runner/AppRunnerController.vala
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,6 @@ namespace Actions
public const string ZOOM_RESET = "zoom-reset";
}

public static string build_camel_id(string web_app_id)
{
return build_uid(Nuvola.get_app_uid() + "App", web_app_id);
}

public static string build_dbus_id(string web_app_id)
{
return build_uid(Nuvola.get_dbus_id() + "App", web_app_id);
}

private static string build_uid(string base_id, string web_app_id)
{
var buffer = new StringBuilder(base_id);
foreach (var part in web_app_id.split("_"))
{
buffer.append_c(part[0].toupper());
if (part.length > 1)
buffer.append(part.substring(1));
}
return buffer.str;
}

public string build_ui_runner_ipc_id(string web_app_id)
{
Expand All @@ -94,15 +73,16 @@ public abstract class RunnerApplication: Diorite.Application
public string dbus_id {get; private set;}


public RunnerApplication(string web_app_id, string web_app_name, string version, Diorite.Storage storage)
public RunnerApplication(WebApp web_app, Diorite.Storage storage)
{
var uid = build_camel_id(web_app_id);
var dbus_id = build_dbus_id(web_app_id);
base(uid, web_app_name, dbus_id);
var uid = web_app.get_uid();
var dbus_id = web_app.get_dbus_id();
base(uid, web_app.name, dbus_id);
this.web_app = web_app;
this.storage = storage;
this.dbus_id = dbus_id;
icon = uid;
this.version = version;
this.icon = web_app.get_icon_name();
this.version = "%d.%d".printf(web_app.version_major, web_app.version_minor);
}
}

Expand Down Expand Up @@ -130,9 +110,8 @@ public class AppRunnerController : RunnerApplication
Diorite.Storage storage, WebApp web_app, WebAppStorage app_storage,
string? api_token, bool use_nuvola_dbus=false)
{
base(web_app.id, web_app.name, "%d.%d".printf(web_app.version_major, web_app.version_minor), storage);
base(web_app, storage);
this.app_storage = app_storage;
this.web_app = web_app;
this.api_token = api_token;
this.use_nuvola_dbus = use_nuvola_dbus;
}
Expand Down Expand Up @@ -551,8 +530,8 @@ public class AppRunnerController : RunnerApplication
title,
message + "\n\nThe application has reached an inconsistent state and will quit for that reason.",
markup);
dialog.run();
dialog.destroy();
dialog.present();
//~ dialog.destroy();
}

private void on_show_error(string title, string message, bool markup)
Expand All @@ -561,8 +540,8 @@ public class AppRunnerController : RunnerApplication
title,
message + "\n\nThe application might not function properly.",
markup);
dialog.run();
dialog.destroy();
dialog.present();
//~ dialog.destroy();
}

private void on_show_warning(string title, string message)
Expand Down
100 changes: 30 additions & 70 deletions src/nuvolakit-runner/core/WebApp.vala
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ public class WebApp : GLib.Object
user_agent = meta.get_string_or("user_agent");
}

public string get_uid()
{
return build_uid(Nuvola.get_app_uid() + "App");
}

public string get_dbus_id()
{
return build_uid(Nuvola.get_dbus_id() + "App");
}

public string get_icon_name()
{
return get_uid();
}

private string build_uid(string base_id)
{
var buffer = new StringBuilder(base_id);
foreach (var part in this.id.split("_"))
{
buffer.append_c(part[0].toupper());
if (part.length > 1)
buffer.append(part.substring(1));
}
return buffer.str;
}

/**
* Returns true if web app belongs to given category
*
Expand Down Expand Up @@ -271,50 +298,13 @@ public class WebApp : GLib.Object
return result;
}

public string? get_icon_name(int size)
{
return lookup_theme_icon(size) != null ? "nuvolaplayer3_" + id : null;
}

public string? get_icon_name_or_path(int size)
{
return get_icon_name(size) ?? get_icon_path(size);
}

/**
* Returns icon path for the given size.
*
* @param size minimal size of the icon or `0` for the largest (scalable) icon
* @return path of the icon
*/
public string? get_icon_path(int size)
{
var theme_icon = lookup_theme_icon(size);
if (theme_icon != null)
{
var path = theme_icon.get_filename();
if (path != null && path[0] != '\0')
return path;
}

lookup_icons();
if (size <= 0)
return icons != null ? icons.last().data.path : get_old_main_icon();

foreach (var icon in icons)
if (icon.size <= 0 || icon.size >= size)
return icon.path;

return get_old_main_icon();
}

/**
* Returns icon pixbuf for the given size.
*
* @return pixbuf with icon scaled to the given size
*/
public Gdk.Pixbuf? get_icon_pixbuf(int size) requires (size > 0)
{
{
var info = lookup_theme_icon(size, Gtk.IconLookupFlags.FORCE_SIZE);
if (info != null)
{
Expand Down Expand Up @@ -346,22 +336,6 @@ public class WebApp : GLib.Object
}
}
}

var default_icon = get_old_main_icon();
if (default_icon != null)
{
try
{
var pixbuf = new Gdk.Pixbuf.from_file_at_scale(default_icon, size, size, false);
if (pixbuf != null)
return pixbuf;
}
catch (GLib.Error e)
{
warning("Failed to load icon from file %s: %s", default_icon, e.message);
}
}

return Diorite.Icons.load_theme_icon({Nuvola.get_app_icon()}, size);
}

Expand All @@ -374,9 +348,9 @@ public class WebApp : GLib.Object
else if (size <= 32)
flags |= Gtk.IconLookupFlags.NO_SVG;

var icon = Gtk.IconTheme.get_default().lookup_icon("nuvolaplayer3_" + id, size, flags);
var icon = Gtk.IconTheme.get_default().lookup_icon(get_icon_name(), size, flags);
if (icon == null)
debug("Theme icon %s %d not found.", "nuvolaplayer3_" + id, size);
debug("Theme icon %s %d not found.", get_icon_name(), size);
return icon;
}

Expand Down Expand Up @@ -425,20 +399,6 @@ public class WebApp : GLib.Object
return builder.end();
}

private string? get_old_main_icon()
{
// TODO: get rid of old main icon
if (data_dir == null)
return null;
var file = data_dir.get_child("icon.svg");
if (file.query_file_type(0) == FileType.REGULAR)
return file.get_path();
file = data_dir.get_child("icon.png");
if (file.query_file_type(0) == FileType.REGULAR)
return file.get_path();
return null;
}

public static inline int cmp_by_name(WebApp a, WebApp b)
{
return strcmp(a.name, b.name);
Expand Down

0 comments on commit 75bad79

Please sign in to comment.