Merge pull request #521 from bebehei/paths-for-icons

Implement paths for icons
This commit is contained in:
Nikos Tsipinakis 2018-06-24 11:19:06 +03:00 committed by GitHub
commit 74bbc96db2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 10 deletions

View File

@ -8,6 +8,7 @@
#include "log.h" #include "log.h"
#include "notification.h" #include "notification.h"
#include "settings.h" #include "settings.h"
#include "utils.h"
static bool is_readable_file(const char *filename) static bool is_readable_file(const char *filename)
{ {
@ -57,17 +58,21 @@ cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf)
return icon_surface; return icon_surface;
} }
static GdkPixbuf *get_pixbuf_from_file(const char *filename) GdkPixbuf *get_pixbuf_from_file(const char *filename)
{ {
GdkPixbuf *pixbuf = NULL; GdkPixbuf *pixbuf = NULL;
if (is_readable_file(filename)) { char *path = string_to_path(g_strdup(filename));
if (is_readable_file(path)) {
GError *error = NULL; GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error); pixbuf = gdk_pixbuf_new_from_file(path, &error);
if (!pixbuf) { if (!pixbuf) {
LOG_W("%s", error->message); LOG_W("%s", error->message);
g_error_free(error); g_error_free(error);
} }
} }
g_free(path);
return pixbuf; return pixbuf;
} }
@ -89,10 +94,8 @@ GdkPixbuf *get_pixbuf_from_icon(const char *iconname)
/* absolute path? */ /* absolute path? */
if (iconname[0] == '/' || iconname[0] == '~') { if (iconname[0] == '/' || iconname[0] == '~') {
pixbuf = get_pixbuf_from_file(iconname); pixbuf = get_pixbuf_from_file(iconname);
} } else {
/* search in icon_path */ /* search in icon_path */
if (!pixbuf) {
char *start = settings.icon_path, char *start = settings.icon_path,
*end, *current_folder, *maybe_icon_path; *end, *current_folder, *maybe_icon_path;
do { do {

View File

@ -8,6 +8,14 @@
cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf); cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf);
/** Retrieve an icon by its full filepath.
*
* @param filename A string representing a readable file path
*
* @return an instance of `GdkPixbuf` or `NULL` if file does not exist
*/
GdkPixbuf *get_pixbuf_from_file(const char *filename);
/** Retrieve an icon by its name sent via the notification bus /** Retrieve an icon by its name sent via the notification bus
* *
* @param iconname A string describing a `file://` URL, an arbitary filename * @param iconname A string describing a `file://` URL, an arbitary filename

View File

@ -5,7 +5,7 @@
#include <gdk-pixbuf/gdk-pixbuf.h> #include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib.h> #include <glib.h>
#define ICONPREFIX "./data/icons/path" #define ICONPREFIX "/data/icons/path"
/* As there are no hints to test if the loaded GdkPixbuf is /* As there are no hints to test if the loaded GdkPixbuf is
* read from a PNG or an SVG file, the sample icons in the * read from a PNG or an SVG file, the sample icons in the
@ -14,6 +14,49 @@
#define IS_ICON_PNG(pb) 4 == gdk_pixbuf_get_width(pb) #define IS_ICON_PNG(pb) 4 == gdk_pixbuf_get_width(pb)
#define IS_ICON_SVG(pb) 16 == gdk_pixbuf_get_width(pb) #define IS_ICON_SVG(pb) 16 == gdk_pixbuf_get_width(pb)
TEST test_get_pixbuf_from_file_tilde(void)
{
char *cwd = g_get_current_dir();
const char *home = g_get_home_dir();
const char *iconpath = ICONPREFIX;
if (0 != strncmp(home, cwd, strlen(home))) {
g_free(cwd);
SKIPm("Current directory is not a subdirectory from user's home."
" Cannot test iconpath tilde expansion.\n");
}
gchar *path = g_build_filename(cwd, iconpath, "valid", "icon1.svg", NULL);
path = string_replace_at(path, 0, strlen(home), "~");
GdkPixbuf *pixbuf = get_pixbuf_from_file(path);
g_clear_pointer(&path, g_free);
g_clear_pointer(&cwd, g_free);
ASSERT(pixbuf);
ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(pixbuf));
g_clear_pointer(&pixbuf, g_object_unref);
PASS();
}
TEST test_get_pixbuf_from_file_absolute(void)
{
char *cwd = g_get_current_dir();
const char *iconpath = ICONPREFIX;
gchar *path = g_build_filename(cwd, iconpath, "valid", "icon1.svg", NULL);
GdkPixbuf *pixbuf = get_pixbuf_from_file(path);
g_clear_pointer(&path, g_free);
g_clear_pointer(&cwd, g_free);
ASSERT(pixbuf);
ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(pixbuf));
g_clear_pointer(&pixbuf, g_object_unref);
PASS();
}
TEST test_get_pixbuf_from_icon_invalid(void) TEST test_get_pixbuf_from_icon_invalid(void)
{ {
GdkPixbuf *pixbuf = get_pixbuf_from_icon("invalid"); GdkPixbuf *pixbuf = get_pixbuf_from_icon("invalid");
@ -82,10 +125,12 @@ TEST test_get_pixbuf_from_icon_fileuri(void)
SUITE(suite_icon) SUITE(suite_icon)
{ {
settings.icon_path = settings.icon_path =
ICONPREFIX "/invalid" "." ICONPREFIX "/invalid"
":" ICONPREFIX "/valid" ":." ICONPREFIX "/valid"
":" ICONPREFIX "/both"; ":." ICONPREFIX "/both";
RUN_TEST(test_get_pixbuf_from_file_tilde);
RUN_TEST(test_get_pixbuf_from_file_absolute);
RUN_TEST(test_get_pixbuf_from_icon_invalid); RUN_TEST(test_get_pixbuf_from_icon_invalid);
RUN_TEST(test_get_pixbuf_from_icon_both); RUN_TEST(test_get_pixbuf_from_icon_both);
RUN_TEST(test_get_pixbuf_from_icon_onlysvg); RUN_TEST(test_get_pixbuf_from_icon_onlysvg);