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 )
75 char result[PATH_MAX];
80 e = result+sizeof(result)-1;
88 return g_strdup(input); /* expansion too long */
97 return g_strdup(input); /* unterminated variable */
101 char *tmp = g_strndup(s, (q-s));
103 char *value = getenv(tmp);
105 if( value != NULL ) {
106 int len = strlen(value);
108 return g_strdup(input);
111 } /* Else, empty string */
114 while( isalnum(*q) || *q == '_' ) {
120 char *tmp = g_strndup(s,q-s);
122 char *value = getenv(tmp);
124 if( value != NULL ) {
125 int len = strlen(value);
127 return g_strdup(input);
133 } else if( c == '\\' ) {
145 return g_strdup(result);
147 gchar *get_absolute_path( const gchar *in_path )
150 if( in_path == NULL ) {
153 if( in_path[0] == '/' || in_path[0] == 0 ) {
154 return g_strdup(in_path);
156 getcwd(tmp, sizeof(tmp));
157 return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
161 gchar *get_filename_at( const gchar *at, const gchar *filename )
164 char *p = strrchr( at, '/' );
166 /* No path at all, so just return filename */
167 return g_strdup(filename);
170 return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
174 const gchar *get_gui_path( int key )
176 if( gui_paths[key] == NULL ) {
177 gui_paths[key] = lxdream_get_global_config_path_value(key);
178 /* If no path defined, go with the current working directory */
179 if( gui_paths[key] == NULL ) {
180 gui_paths[key] = get_absolute_path(".");
183 return gui_paths[key];
186 void set_gui_path( int key, const gchar *path )
188 g_free(gui_paths[key]);
189 gui_paths[key] = g_strdup(path);
192 void reset_gui_paths()
195 for( i=0; i < CONFIG_KEY_MAX; i++ ) {
196 g_free(gui_paths[i]);
.