Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default icons and refactor icon-lookup #1365

Merged
merged 3 commits into from
Jul 15, 2024
Merged
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
21 changes: 2 additions & 19 deletions src/icon-lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,28 +302,11 @@ char *find_icon_path(const char *name, int size) {
if (STR_EMPTY(name))
return NULL;

gchar *uri_path = NULL;

if (g_str_has_prefix(name, "file://")) {
uri_path = g_filename_from_uri(name, NULL, NULL);
if (is_readable_file(uri_path))
return uri_path;
else
return NULL;
}

/* absolute path? */
if (name[0] == '/' || name[0] == '~') {
if (is_readable_file(name))
return g_strdup(name);
else
return NULL;
}

if (!default_themes_index) {
LOG_W("No icon theme has been set.");
LOG_W("No icon theme has been set");
return NULL;
}

for (int i = 0; i < default_themes_count; i++) {
char *icon = find_icon_in_theme_with_inherit(name,
default_themes_index[i], size);
Expand Down
2 changes: 1 addition & 1 deletion src/icon-lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void add_default_theme(int theme_index);
* @retval NULL if the icon cannot be found or is not readable.
*/
char *find_icon_in_theme(const char *name, int theme_index, int size);
char *find_icon_path(const char *name, int size);

void set_default_theme(int theme_index);

/**
Expand Down
84 changes: 42 additions & 42 deletions src/icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,60 +214,60 @@ char *get_path_from_icon_name(const char *iconname, int size)
if (STR_EMPTY(iconname))
return NULL;

if (settings.enable_recursive_icon_lookup) {
char *uri_path = NULL;
if (g_str_has_prefix(iconname, "file://")) {
char *uri_path = g_filename_from_uri(iconname, NULL, NULL);
if (STR_EMPTY(uri_path)) {
LOG_W("Invalid file uri '%s'", iconname);
return NULL;
}
iconname = uri_path;
} else if (iconname[0] == '/' || iconname[0] == '~') {
// Return absolute path
return g_strdup(iconname);
} else if (settings.enable_recursive_icon_lookup) {
char *path = find_icon_path(iconname, size);
LOG_I("Found icon at %s", STR_NN(path));
if (STR_EMPTY(path))
LOG_W("Icon '%s' not found in themes", iconname);
else
LOG_I("Found icon '%s' at %s", iconname, STR_NN(path));
return path;
}

// Search icon_path
const char *suffixes[] = { ".svg", ".svgz", ".png", ".xpm", NULL };
gchar *uri_path = NULL;
char *new_name = NULL;
char *start = settings.icon_path, *end, *current_folder, *maybe_icon_path, *path = NULL;

if (g_str_has_prefix(iconname, "file://")) {
uri_path = g_filename_from_uri(iconname, NULL, NULL);
if (uri_path)
iconname = uri_path;
}
do {
end = strchr(start, ':');
if (!end) end = strchr(settings.icon_path, '\0'); /* end = end of string */

/* absolute path? */
if (iconname[0] == '/' || iconname[0] == '~') {
new_name = g_strdup(iconname);
} else {
/* search in icon_path */
char *start = settings.icon_path,
*end, *current_folder, *maybe_icon_path;
do {
end = strchr(start, ':');
if (!end) end = strchr(settings.icon_path, '\0'); /* end = end of string */

current_folder = g_strndup(start, end - start);

for (const char **suf = suffixes; *suf; suf++) {
gchar *name_with_extension = g_strconcat(iconname, *suf, NULL);
maybe_icon_path = g_build_filename(current_folder, name_with_extension, NULL);
if (is_readable_file(maybe_icon_path)) {
new_name = g_strdup(maybe_icon_path);
}
g_free(name_with_extension);
g_free(maybe_icon_path);

if (new_name)
break;
current_folder = g_strndup(start, end - start);

for (const char **suf = suffixes; *suf; suf++) {
gchar *name_with_extension = g_strconcat(iconname, *suf, NULL);
maybe_icon_path = g_build_filename(current_folder, name_with_extension, NULL);
if (is_readable_file(maybe_icon_path)) {
path = g_strdup(maybe_icon_path);
}
g_free(name_with_extension);
g_free(maybe_icon_path);

if (path) break;
}
g_free(current_folder);

g_free(current_folder);
if (new_name)
break;
if (path) break;
start = end + 1;
} while (STR_FULL(end));

start = end + 1;
} while (STR_FULL(end));
if (!new_name)
LOG_W("No icon found in path: '%s'", STR_NN(iconname));
}
if (STR_EMPTY(path))
LOG_W("Icon '%s' not found in icon_path", iconname);
else
LOG_I("Found icon '%s' at %s", iconname, path);

g_free(uri_path);
return new_name;
return path;
}

GdkPixbuf *icon_get_for_data(GVariant *data, char **id, double dpi_scale, int min_size, int max_size)
Expand Down
2 changes: 1 addition & 1 deletion src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#define DIE(...) do { LOG_C(__VA_ARGS__); exit(EXIT_FAILURE); } while (0)

// unified fopen() result messages
#define MSG_FOPEN_SUCCESS(path, fp) "'%s' open, fd: '%d'", path, fileno(fp)
#define MSG_FOPEN_SUCCESS(path, fp) "Opened '%s' (fd: '%d')", path, fileno(fp)
#define MSG_FOPEN_FAILURE(path) "Cannot open '%s': '%s'", path, strerror(errno)

/**
Expand Down
2 changes: 1 addition & 1 deletion src/option_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ bool set_from_string(void *target, struct setting setting, const char *value) {
case TYPE_LENGTH:
// Keep compatibility with old offset syntax
if (STR_EQ(setting.name, "offset") && string_parse_list(GINT_TO_POINTER(OFFSET_LIST), value, target)) {
LOG_I("Using legacy offset syntax NxN, you should switch to the new syntax (N, N)");
LOG_M("Using legacy offset syntax NxN, you should switch to the new syntax (N, N)");
return true;
}
return string_parse_length(target, value);
Expand Down
2 changes: 1 addition & 1 deletion src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ FILE *fopen_conf(char * const path)
FILE *f = NULL;
char *real_path = string_to_path(g_strdup(path));

if (is_readable_file(real_path) && (f = fopen(real_path, "r")))
if (is_readable_file(real_path) && NULL != (f = fopen(real_path, "r")))
LOG_I(MSG_FOPEN_SUCCESS(path, f));
else
LOG_W(MSG_FOPEN_FAILURE(path));
Expand Down
6 changes: 3 additions & 3 deletions src/settings_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ static const struct setting allowed_settings[] = {
.section = "urgency_low",
.description = "Icon for notifications with low urgency",
.type = TYPE_STRING,
.default_value = "dialog-information",
.default_value = "",
.value = &settings.icons[URG_LOW],
.parser = NULL,
.parser_data = NULL,
Expand All @@ -379,7 +379,7 @@ static const struct setting allowed_settings[] = {
.section = "urgency_normal",
.description = "Icon for notifications with normal urgency",
.type = TYPE_STRING,
.default_value = "dialog-information",
.default_value = "",
.value = &settings.icons[URG_NORM],
.parser = NULL,
.parser_data = NULL,
Expand All @@ -389,7 +389,7 @@ static const struct setting allowed_settings[] = {
.section = "urgency_critical",
.description = "Icon for notifications with critical urgency",
.type = TYPE_STRING,
.default_value = "dialog-warning",
.default_value = "",
.value = &settings.icons[URG_CRIT],
.parser = NULL,
.parser_data = NULL,
Expand Down
24 changes: 0 additions & 24 deletions test/icon-lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ int setup_test_theme(void) {
g_free(icon); \
}

#define find_path_test(path, ...) { \
char *icon = find_icon_path(path, 42); /* size doesn't matter */ \
char *expected = g_build_filename(base, ICONPREFIX, "theme", __VA_ARGS__, NULL); \
ASSERTm("Could not find icon", icon); \
ASSERT_STR_EQ(expected, icon); \
g_free(expected); \
g_free(icon); \
}

TEST test_load_theme_from_dir(void)
{
setup_test_theme();
Expand All @@ -55,20 +46,6 @@ TEST test_find_icon(void)
PASS();
}

TEST test_find_path(void)
bynect marked this conversation as resolved.
Show resolved Hide resolved
{
setup_test_theme();
char *path = g_build_filename(base, ICONPREFIX, "theme", "16x16", "actions", "edit.png", NULL);
find_path_test(path, "16x16", "actions", "edit.png");
char *path2 = g_strconcat("file://", path, NULL);
find_path_test(path2, "16x16", "actions", "edit.png");
g_free(path2);
g_free(path);
free_all_themes();
PASS();
}


TEST test_new_icon_overrides_raw_icon(void) {
setup_test_theme();

Expand Down Expand Up @@ -168,7 +145,6 @@ SUITE (suite_icon_lookup)
{
RUN_TEST(test_load_theme_from_dir);
RUN_TEST(test_find_icon);
RUN_TEST(test_find_path);
RUN_TEST(test_new_icon_overrides_raw_icon);
bool bench = false;
if (bench) {
Expand Down