r246: Added support for i18n. No translations yet, though!
[rox-filer/ma.git] / ROX-Filer / src / choices.c
blob6d2d3a754d0d900e33a1f01be7bc5b88c44d5239
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
21 /* choices.c - code for handling loading and saving of user choices */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <errno.h>
32 #include "choices.h"
34 static gboolean saving_disabled = TRUE;
35 static gchar **dir_list = NULL;
37 /* Static prototypes */
38 static gboolean exists(char *path);
41 /****************************************************************
42 * EXTERNAL INTERFACE *
43 ****************************************************************/
46 /* Reads in CHOICESPATH and constructs the directory list table.
47 * You must call this before using any other choices_* functions.
49 * If CHOICESPATH does not exist then a suitable default is used.
51 void choices_init(void)
53 char *choices;
55 g_return_if_fail(dir_list == NULL);
57 choices = getenv("CHOICESPATH");
59 if (choices)
61 if (*choices != ':' && *choices != '\0')
62 saving_disabled = FALSE;
64 while (*choices == ':')
65 choices++;
67 if (*choices == '\0')
69 dir_list = g_new(char *, 1);
70 dir_list[0] = NULL;
72 else
73 dir_list = g_strsplit(choices, ":", 0);
75 else
77 saving_disabled = FALSE;
79 dir_list = g_new(gchar *, 4);
80 dir_list[0] = g_strconcat(getenv("HOME"), "/Choices", NULL);
81 dir_list[1] = g_strdup("/usr/local/Choices");
82 dir_list[2] = g_strdup("/usr/Choices");
83 dir_list[3] = NULL;
86 #if 0
88 gchar **cdir = dir_list;
90 while (*cdir)
92 g_print("[ choices dir '%s' ]\n", *cdir);
93 cdir++;
96 g_print("[ saving is %s ]\n", saving_disabled ? "disabled"
97 : "enabled");
99 #endif
102 /* Returns an array of the directories in CHOICESPATH which contain
103 * a subdirectory called 'dir'.
105 * Lower-indexed results should override higher-indexed ones.
107 * Free the list using choices_free_list().
109 GPtrArray *choices_list_dirs(char *dir)
111 GPtrArray *list;
112 gchar **cdir = dir_list;
114 g_return_val_if_fail(dir_list != NULL, NULL);
116 list = g_ptr_array_new();
118 while (*cdir)
120 guchar *path;
122 path = g_strconcat(*cdir, "/", dir, NULL);
123 if (exists(path))
124 g_ptr_array_add(list, path);
125 else
126 g_free(path);
128 cdir++;
131 return list;
134 void choices_free_list(GPtrArray *list)
136 int i;
138 g_return_if_fail(list != NULL);
140 for (i = 0; i < list->len; i++)
141 g_free(g_ptr_array_index(list, i));
143 g_ptr_array_free(list, TRUE);
146 /* Get the pathname of a choices file to load. Eg:
148 * choices_find_path_load("menus", "ROX-Filer")
149 * -> "/usr/local/Choices/ROX-Filer/menus".
151 * The return values may be NULL - use built-in defaults - otherwise
152 * g_free() the result.
154 guchar *choices_find_path_load(char *leaf, char *dir)
156 gchar **cdir = dir_list;
158 g_return_val_if_fail(dir_list != NULL, NULL);
160 while (*cdir)
162 gchar *path;
164 path = g_strconcat(*cdir, "/", dir, "/", leaf, NULL);
166 if (exists(path))
167 return path;
169 g_free(path);
171 cdir++;
174 return NULL;
177 /* Returns the pathname of a file to save to, or NULL if saving is
178 * disabled. If 'create' is TRUE then intermediate directories will
179 * be created (set this to FALSE if you just want to find out where
180 * a saved file would go without actually altering the filesystem).
182 guchar *choices_find_path_save(char *leaf, char *dir, gboolean create)
184 static gchar *path = NULL;
186 g_return_val_if_fail(dir_list != NULL, NULL);
188 if (path)
190 g_free(path);
191 path = NULL;
194 if (saving_disabled)
195 return NULL;
197 if (create && !exists(dir_list[0]))
199 if (mkdir(dir_list[0], 0777))
200 g_warning("mkdir(%s): %s\n", dir_list[0],
201 g_strerror(errno));
204 path = g_strconcat(dir_list[0], "/", dir, NULL);
205 if (create && !exists(path))
207 if (mkdir(path, 0777))
208 g_warning("mkdir(%s): %s\n", path, g_strerror(errno));
211 path = g_strconcat(path, "/", leaf, NULL);
213 return path;
217 /****************************************************************
218 * INTERNAL FUNCTIONS *
219 ****************************************************************/
222 /* Returns TRUE if the object exists, FALSE if it doesn't */
223 static gboolean exists(char *path)
225 struct stat info;
227 return stat(path, &info) == 0;