summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2023-08-15 19:12:14 +0200
committerDave Davenport <qball@gmpclient.org>2023-08-15 19:12:14 +0200
commitad06fb95161a6b140ec7329ef2bbae6837dc4d45 (patch)
tree9a8f79e28089e6b6355b0ea0c9150efa464fa4fe
parent528419269e7939e851ea426e34fb7ae2eec0a561 (diff)
[Theme] Try to fix importing of theme.
- Fix the two place of resolving into one method. - Do not accept file in CWD. - Prefer file if it exists in same directory as parent file that imported it. fixes: #1889
-rw-r--r--doc/rofi-theme.56
-rw-r--r--doc/rofi-theme.5.markdown4
-rw-r--r--include/helper.h5
-rw-r--r--include/theme.h3
-rw-r--r--lexer/theme-lexer.l8
-rw-r--r--source/helper.c41
-rw-r--r--source/rofi-icon-fetcher.c2
-rw-r--r--source/theme.c13
-rw-r--r--test/box-test.c3
-rw-r--r--test/scrollbar-test.c3
10 files changed, 56 insertions, 32 deletions
diff --git a/doc/rofi-theme.5 b/doc/rofi-theme.5
index 0c800092..28d2413c 100644
--- a/doc/rofi-theme.5
+++ b/doc/rofi-theme.5
@@ -2408,6 +2408,10 @@ If a filename is provided, it will try to resolve it in the following order:
.RS
.IP \(bu 2
+If path is absolute and file exists, it will pick open the file. This includes expansion of '~' or '~user'
+.IP \(bu 2
+On an \fB\fC@import\fR or \fB\fC@theme\fR it looks in the directory of the file that tried to import it.
+.IP \(bu 2
\fB\fC${XDG_CONFIG_HOME}/rofi/themes/\fR
.IP \(bu 2
\fB\fC${XDG_CONFIG_HOME}/rofi/\fR
@@ -2419,7 +2423,7 @@ If a filename is provided, it will try to resolve it in the following order:
.RE
.PP
-A name is resolved as a filename by appending the \fB\fC\&.rasi\fR extension.
+A name is resolved (if it has no valid extension) as a filename by appending the \fB\fC\&.rasi\fR extension.
.SH Examples
.PP
diff --git a/doc/rofi-theme.5.markdown b/doc/rofi-theme.5.markdown
index eef8777f..d4c8a056 100644
--- a/doc/rofi-theme.5.markdown
+++ b/doc/rofi-theme.5.markdown
@@ -1626,12 +1626,14 @@ The specified file can either by *name*, *filename*,*full path*.
If a filename is provided, it will try to resolve it in the following order:
+- If path is absolute and file exists, it will pick open the file. This includes expansion of '~' or '~user'
+- On an `@import` or `@theme` it looks in the directory of the file that tried to import it.
- `${XDG_CONFIG_HOME}/rofi/themes/`
- `${XDG_CONFIG_HOME}/rofi/`
- `${XDG_DATA_HOME}/rofi/themes/`
- `${INSTALL PREFIX}/share/rofi/themes/`
-A name is resolved as a filename by appending the `.rasi` extension.
+A name is resolved (if it has no valid extension) as a filename by appending the `.rasi` extension.
## Examples
diff --git a/include/helper.h b/include/helper.h
index 52af0600..5790f370 100644
--- a/include/helper.h
+++ b/include/helper.h
@@ -397,8 +397,9 @@ char *helper_string_replace_if_exists(char *string, ...);
*
* @returns path to theme or copy of filename if not found.
*/
-char *helper_get_theme_path(const char *file, const char **ext)
- __attribute__((nonnull));
+char *helper_get_theme_path(const char *file, const char **ext,
+ const char *parent_dir)
+ __attribute__((nonnull(1, 2)));
/**
* @param name The name of the element to find.
diff --git a/include/theme.h b/include/theme.h
index e934f621..242e3e43 100644
--- a/include/theme.h
+++ b/include/theme.h
@@ -327,13 +327,12 @@ void rofi_theme_reset(void);
/**
* @param file File name to prepare.
- * @param parent_file Filename of parent file.
*
* Tries to find full path relative to parent file.
*
* @returns full path to file.
*/
-char *rofi_theme_parse_prepare_file(const char *file, const char *parent_file);
+char *rofi_theme_parse_prepare_file(const char *file);
/**
* Process conditionals.
diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l
index 2d9117dd..0074b861 100644
--- a/lexer/theme-lexer.l
+++ b/lexer/theme-lexer.l
@@ -415,8 +415,8 @@ if ( queue == NULL ) {
yytext[yyleng-1] = '\0';
ParseObject *top = g_queue_peek_head ( file_queue );
g_assert ( top != NULL );
- char *file2 = helper_get_theme_path ( &yytext[1], rasi_theme_file_extensions );
- char *filename = rofi_theme_parse_prepare_file ( file2, top->filename );
+ char *file2 = helper_get_theme_path ( &yytext[1], rasi_theme_file_extensions, top->filename );
+ char *filename = rofi_theme_parse_prepare_file ( file2 );
g_free ( file2 );
FILE *f = fopen ( filename, "rb" );
if ( f ) {
@@ -883,8 +883,8 @@ if ( queue == NULL ) {
gboolean rofi_theme_parse_file ( const char *file )
{
- char *file2 = helper_get_theme_path ( file, rasi_theme_file_extensions );
- char *filename = rofi_theme_parse_prepare_file ( file2, NULL );
+ char *file2 = helper_get_theme_path ( file, rasi_theme_file_extensions, NULL );
+ char *filename = rofi_theme_parse_prepare_file ( file2 );
g_free ( file2 );
yyin = fopen ( filename, "rb" );
diff --git a/source/helper.c b/source/helper.c
index 267e06ca..19f5da89 100644
--- a/source/helper.c
+++ b/source/helper.c
@@ -1064,13 +1064,8 @@ gboolean helper_execute_command(const char *wd, const char *cmd,
return helper_execute(wd, args, "", cmd, context);
}
-char *helper_get_theme_path(const char *file, const char **ext) {
- char *filename = rofi_expand_path(file);
- g_debug("Opening theme, testing: %s\n", filename);
- if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
- return filename;
- }
- g_free(filename);
+char *helper_get_theme_path(const char *file, const char **ext,
+ const char *parent_file) {
gboolean ext_found = FALSE;
for (const char **i = ext; *i != NULL; i++) {
@@ -1079,13 +1074,41 @@ char *helper_get_theme_path(const char *file, const char **ext) {
break;
}
}
+ char *filename = NULL;
if (ext_found) {
- filename = g_strdup(file);
+ filename = rofi_expand_path(file);
} else {
g_assert_nonnull(ext[0]);
+ char *temp = rofi_expand_path(file);
// TODO: Pick the first extension. needs fixing.
- filename = g_strconcat(file, ext[0], NULL);
+ filename = g_strconcat(temp, ext[0], NULL);
+ g_free(temp);
+ }
+ g_debug("Opening theme, testing: %s\n", filename);
+ if (g_path_is_absolute(filename)) {
+ g_debug("Opening theme, path is absolute: %s", filename);
+ if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
+ return filename;
+ }
+ g_debug("Opening theme, path is absolute but does not exists: %s",
+ filename);
+ } else {
+ if (parent_file != NULL) {
+ // If no absolute path specified, expand it.
+ char *basedir = g_path_get_dirname(parent_file);
+ char *path = g_build_filename(basedir, filename, NULL);
+ g_free(basedir);
+ g_debug("Opening theme, check in dir where file is included: %s", path);
+ if (g_file_test(path, G_FILE_TEST_EXISTS)) {
+ g_free(filename);
+ return path;
+ }
+ g_debug("Opening theme, file does not exists in dir where file is "
+ "included: %s\n",
+ filename);
+ }
}
+
// Check config's themes directory.
const char *cpath = g_get_user_config_dir();
if (cpath) {
diff --git a/source/rofi-icon-fetcher.c b/source/rofi-icon-fetcher.c
index 120ebee1..c4243c59 100644
--- a/source/rofi-icon-fetcher.c
+++ b/source/rofi-icon-fetcher.c
@@ -334,7 +334,7 @@ static void rofi_icon_fetcher_worker(thread_state *sdata,
if (ext) {
const char *exts2[2] = {ext, NULL};
icon_path = icon_path_ =
- helper_get_theme_path(sentry->entry->name, exts2);
+ helper_get_theme_path(sentry->entry->name, exts2, NULL);
}
if (icon_path_ == NULL) {
sentry->query_done = TRUE;
diff --git a/source/theme.c b/source/theme.c
index 615c6fb9..fe70574c 100644
--- a/source/theme.c
+++ b/source/theme.c
@@ -1434,16 +1434,9 @@ void distance_get_linestyle(RofiDistance d, cairo_t *draw) {
}
}
-char *rofi_theme_parse_prepare_file(const char *file, const char *parent_file) {
- char *filename = rofi_expand_path(file);
- // If no absolute path specified, expand it.
- if (parent_file != NULL && !g_path_is_absolute(filename)) {
- char *basedir = g_path_get_dirname(parent_file);
- char *path = g_build_filename(basedir, filename, NULL);
- g_free(filename);
- filename = path;
- g_free(basedir);
- }
+char *rofi_theme_parse_prepare_file(const char *file) {
+ char *filename = g_strdup(file);
+ // TODO: Why did I write this code? I think it was to get full path.
GFile *gf = g_file_new_for_path(filename);
parsed_config_files = g_list_append(parsed_config_files, filename);
filename = g_file_get_path(gf);
diff --git a/test/box-test.c b/test/box-test.c
index 36bbaaf1..7faa654b 100644
--- a/test/box-test.c
+++ b/test/box-test.c
@@ -92,7 +92,8 @@ gboolean config_parse_set_property(G_GNUC_UNUSED const Property *p,
}
char *rofi_expand_path(G_GNUC_UNUSED const char *path) { return NULL; }
-char *helper_get_theme_path(const char *file, G_GNUC_UNUSED const char **ext) {
+char *helper_get_theme_path(const char *file, G_GNUC_UNUSED const char **ext,
+ G_GNUC_UNUSED const char *parent_file) {
return g_strdup(file);
}
void rofi_add_error_message(G_GNUC_UNUSED GString *msg) {}
diff --git a/test/scrollbar-test.c b/test/scrollbar-test.c
index cb58bdcf..fa4d0df3 100644
--- a/test/scrollbar-test.c
+++ b/test/scrollbar-test.c
@@ -77,7 +77,8 @@ cairo_surface_t *rofi_icon_fetcher_get(G_GNUC_UNUSED const uint32_t uid) {
int monitor_active(G_GNUC_UNUSED workarea *mon) { return 0; }
-char *helper_get_theme_path(const char *file, G_GNUC_UNUSED const char **ext) {
+char *helper_get_theme_path(const char *file, G_GNUC_UNUSED const char **ext,
+ G_GNUC_UNUSED const char *parent_file) {
return g_strdup(file);
}
gboolean config_parse_set_property(G_GNUC_UNUSED const Property *p,