4 * GUI helper functions that aren't specific to any particular implementation.
6 * Copyright (c) 2009 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include <glib/gstrfuncs.h>
23 #include <glib/gutils.h>
28 static gchar *gui_paths[CONFIG_KEY_MAX];
31 * Test if we need to escape a path to prevent substitution mangling.
32 * @return TRUE if the input value contains any character that doesn't
33 * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
36 static gboolean path_needs_escaping( const gchar *value )
38 const gchar *p = value;
40 if( !isalnum(*p) && *p != '.' && *p != '_' &&
41 *p != '@' && *p != '%' && *p != '/' ) {
49 gchar *get_escaped_path( const gchar *value )
51 if( value != NULL && path_needs_escaping(value) ) {
52 /* Escape with "", and backslash the remaining characters:
55 char buf[strlen(value)*2+3];
56 const char *s = value;
60 if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
69 return g_strdup(value);
73 gchar *get_expanded_path( const gchar *input )
79 memset(&we,0,sizeof(we));
80 int result = wordexp(input, &we, WRDE_NOCMD);
81 if( result != 0 || we.we_wordc == 0 ) {
82 /* On failure, return the original input unchanged */
83 return g_strdup(input);
85 /* On success, concatenate all 'words' together into a single
86 * space-separated string
88 int length = we.we_wordc, i;
91 for( i=0; i<we.we_wordc; i++ ) {
92 length += strlen(we.we_wordv[i]);
94 p = result = g_malloc(length);
95 for( i=0; i<we.we_wordc; i++ ) {
98 strcpy( p, we.we_wordv[i] );
106 gchar *get_absolute_path( const gchar *in_path )
109 if( in_path == NULL ) {
112 if( in_path[0] == '/' || in_path[0] == 0 ) {
113 return g_strdup(in_path);
115 getcwd(tmp, sizeof(tmp));
116 return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
120 gchar *get_filename_at( const gchar *at, const gchar *filename )
123 char *p = strrchr( at, '/' );
125 /* No path at all, so just return filename */
126 return g_strdup(filename);
129 return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
133 const gchar *get_gui_path( int key )
135 if( gui_paths[key] == NULL ) {
136 gui_paths[key] = lxdream_get_global_config_path_value(key);
137 /* If no path defined, go with the current working directory */
138 if( gui_paths[key] == NULL ) {
139 gui_paths[key] = get_absolute_path(".");
142 return gui_paths[key];
145 void set_gui_path( int key, const gchar *path )
147 g_free(gui_paths[key]);
148 gui_paths[key] = g_strdup(path);
151 void reset_gui_paths()
154 for( i=0; i < CONFIG_KEY_MAX; i++ ) {
155 g_free(gui_paths[i]);
.