From c1244b066f975e15099a7e72b7055a5024340e60 Mon Sep 17 00:00:00 2001 From: TonCherAmi Date: Sat, 5 Jun 2021 15:22:48 +0300 Subject: [PATCH] [FileBrowser] Allow setting startup directory (#1325) --- config/config.c | 3 +++ doc/rofi.1 | 13 ++++++++++ doc/rofi.1.markdown | 6 +++++ include/settings.h | 3 +++ source/dialogs/filebrowser.c | 49 +++++++++++++++++++++++++----------- source/xrmoptions.c | 3 +++ 6 files changed, 62 insertions(+), 15 deletions(-) diff --git a/config/config.c b/config/config.c index 420a7ab9..8c24895a 100644 --- a/config/config.c +++ b/config/config.c @@ -128,6 +128,9 @@ Settings config = { /** Desktop Link launch command */ .drun_url_launcher = "xdg-open", + /** Directory the file browser starts in */ + .file_browser_directory = NULL, + /** Window fields to match in window mode*/ .window_match_fields = "all", /** Monitor */ diff --git a/doc/rofi.1 b/doc/rofi.1 index 1ab82952..a7c217d1 100644 --- a/doc/rofi.1 +++ b/doc/rofi.1 @@ -1181,6 +1181,19 @@ Set ellipsize mode to start. So end of string is visible. Pops up a message dialog (used internally for showing errors) with \fImessage\fP\&. Message can be multi\-line. +.SS File browser settings +.PP +\fB\fC\-file\-browser\-directory\fR \fIdirectory\fP + +.PP +Directory the file browser starts in. + +.PP +.RS + +.fi +.RE + .SS Other .PP \fB\fC\-drun\-use\-desktop\-cache\fR diff --git a/doc/rofi.1.markdown b/doc/rofi.1.markdown index 433e6438..b365678e 100644 --- a/doc/rofi.1.markdown +++ b/doc/rofi.1.markdown @@ -721,6 +721,12 @@ Set ellipsize mode to start. So end of string is visible. Pops up a message dialog (used internally for showing errors) with *message*. Message can be multi-line. +### File browser settings + +`-file-browser-directory` *directory* + +Directory the file browser starts in. + ### Other `-drun-use-desktop-cache` diff --git a/include/settings.h b/include/settings.h index ba13d445..e0a84536 100644 --- a/include/settings.h +++ b/include/settings.h @@ -132,6 +132,9 @@ typedef struct /** Desktop Link launch command */ char * drun_url_launcher; + /** Directory the file browser starts in */ + char * file_browser_directory; + /** Search case sensitivity */ unsigned int case_sensitive; /** Cycle through in the element list */ diff --git a/source/dialogs/filebrowser.c b/source/dialogs/filebrowser.c index a1a2807e..5451a313 100644 --- a/source/dialogs/filebrowser.c +++ b/source/dialogs/filebrowser.c @@ -40,6 +40,7 @@ #include "mode-private.h" #include "dialogs/filebrowser.h" #include "rofi.h" +#include "settings.h" #include "history.h" #include @@ -193,6 +194,36 @@ static void get_file_browser ( Mode *sw ) g_qsort_with_data ( pd->array, pd->array_length, sizeof ( FBFile ), compare, NULL ); } +static void file_browser_mode_init_current_dir ( Mode *sw ) { + FileBrowserModePrivateData *pd = (FileBrowserModePrivateData *) mode_get_private_data ( sw ); + + gboolean config_has_valid_dir = config.file_browser_directory != NULL + && g_file_test ( config.file_browser_directory, G_FILE_TEST_IS_DIR ); + + if ( config_has_valid_dir ) { + pd->current_dir = g_file_new_for_path ( config.file_browser_directory ); + } else { + char *current_dir = NULL; + char *cache_file = g_build_filename ( cache_dir, FILEBROWSER_CACHE_FILE, NULL ); + + if ( g_file_get_contents ( cache_file, ¤t_dir, NULL, NULL ) ) { + if ( g_file_test ( current_dir, G_FILE_TEST_IS_DIR ) ) { + pd->current_dir = g_file_new_for_path ( current_dir ); + } + + g_free ( current_dir ); + } + + // Store it based on the unique identifiers (desktop_id). + g_free ( cache_file ); + } + + + if ( pd->current_dir == NULL ) { + pd->current_dir = g_file_new_for_path ( g_get_home_dir () ); + } +} + static int file_browser_mode_init ( Mode *sw ) { /** @@ -201,21 +232,9 @@ static int file_browser_mode_init ( Mode *sw ) if ( mode_get_private_data ( sw ) == NULL ) { FileBrowserModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) ); mode_set_private_data ( sw, (void *) pd ); - { - char *path = g_build_filename ( cache_dir, FILEBROWSER_CACHE_FILE, NULL ); - char *file = NULL; - if ( g_file_get_contents ( path, &file, NULL, NULL ) ) { - if ( g_file_test ( file, G_FILE_TEST_IS_DIR ) ) { - pd->current_dir = g_file_new_for_path ( file ); - } - g_free ( file ); - } - // Store it based on the unique identifiers (desktop_id). - g_free ( path ); - if ( pd->current_dir == NULL ) { - pd->current_dir = g_file_new_for_path ( g_get_home_dir () ); - } - } + + file_browser_mode_init_current_dir ( sw ); + // Load content. get_file_browser ( sw ); } diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 14b9a66a..5d397213 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -145,6 +145,9 @@ static XrmOption xrmOptions[] = { { xrm_String, "drun-url-launcher", { .str = &config.drun_url_launcher }, NULL, "Command to open an Desktop Entry that is a Link.", CONFIG_DEFAULT }, + { xrm_String, "file-browser-directory", { .str = &config.file_browser_directory }, NULL, + "Directory the file browser starts in", CONFIG_DEFAULT }, + { xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL, "Disable history in run/ssh", CONFIG_DEFAULT }, { xrm_String, "ignored-prefixes", { .str = &config.ignored_prefixes }, NULL,