| 1 |
/* |
|---|
| 2 |
* Sweep, a sound wave editor. |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (C) 2000 Conrad Parker |
|---|
| 5 |
* |
|---|
| 6 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 7 |
* it under the terms of the GNU General Public License as published by |
|---|
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 |
* (at your option) any later version. |
|---|
| 10 |
* |
|---|
| 11 |
* This program is distributed in the hope that it will be useful, |
|---|
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 |
* GNU General Public License for more details. |
|---|
| 15 |
* |
|---|
| 16 |
* You should have received a copy of the GNU General Public License |
|---|
| 17 |
* along with this program; if not, write to the Free Software |
|---|
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 |
*/ |
|---|
| 20 |
|
|---|
| 21 |
#ifdef HAVE_CONFIG_H |
|---|
| 22 |
# include <config.h> |
|---|
| 23 |
#endif |
|---|
| 24 |
|
|---|
| 25 |
#include <stdio.h> |
|---|
| 26 |
#include <stdlib.h> |
|---|
| 27 |
#include <string.h> |
|---|
| 28 |
#include <sys/types.h> |
|---|
| 29 |
#include <glib.h> |
|---|
| 30 |
#include <sys/stat.h> |
|---|
| 31 |
#include <unistd.h> |
|---|
| 32 |
#include <signal.h> |
|---|
| 33 |
#include <fcntl.h> |
|---|
| 34 |
#include <errno.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <sweep/sweep_i18n.h> |
|---|
| 37 |
#include "question_dialogs.h" |
|---|
| 38 |
|
|---|
| 39 |
#include "tdb.h" |
|---|
| 40 |
|
|---|
| 41 |
static TDB_CONTEXT * prefs_tdb = NULL; |
|---|
| 42 |
gboolean ignore_failed_tdb_lock = FALSE; |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
#define DIR_MODE (S_IRWXU) |
|---|
| 46 |
#define FILE_MODE (S_IRUSR | S_IWUSR) |
|---|
| 47 |
|
|---|
| 48 |
void |
|---|
| 49 |
prefs_init () |
|---|
| 50 |
{ |
|---|
| 51 |
G_CONST_RETURN char * prefs_path; |
|---|
| 52 |
struct stat sbuf; |
|---|
| 53 |
|
|---|
| 54 |
prefs_path = g_get_home_dir (); |
|---|
| 55 |
prefs_path = g_strconcat (prefs_path, "/.sweep", NULL); |
|---|
| 56 |
|
|---|
| 57 |
if (stat (prefs_path, &sbuf) == -1) { |
|---|
| 58 |
switch (errno) { |
|---|
| 59 |
case ENOENT: |
|---|
| 60 |
if (mkdir (prefs_path, DIR_MODE) == -1) { |
|---|
| 61 |
if (errno != EEXIST) { |
|---|
| 62 |
perror (_("Error creating ~/.sweep")); |
|---|
| 63 |
exit (1); |
|---|
| 64 |
} |
|---|
| 65 |
} else { |
|---|
| 66 |
printf (_("Created %s/ mode %04o\n"), prefs_path, DIR_MODE); |
|---|
| 67 |
} |
|---|
| 68 |
break; |
|---|
| 69 |
case EACCES: |
|---|
| 70 |
/* Directory exists, permission denied -- handled at access test */ |
|---|
| 71 |
break; |
|---|
| 72 |
default: |
|---|
| 73 |
perror (_("Error on ~/.sweep")); |
|---|
| 74 |
exit (1); |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
if (access (prefs_path, W_OK) == -1) { |
|---|
| 79 |
switch (errno) { |
|---|
| 80 |
case EACCES: |
|---|
| 81 |
if (chmod (prefs_path, DIR_MODE) == -1) { |
|---|
| 82 |
perror (_("Error setting permissions on ~/.sweep")); |
|---|
| 83 |
exit (1); |
|---|
| 84 |
} else { |
|---|
| 85 |
printf ("Changed mode of %s to %04o\n", prefs_path, DIR_MODE); |
|---|
| 86 |
} |
|---|
| 87 |
break; |
|---|
| 88 |
default: |
|---|
| 89 |
perror (_("Error accessing ~/.sweep")); |
|---|
| 90 |
exit (1); |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
prefs_path = g_strconcat (prefs_path, "/preferences.tdb", NULL); |
|---|
| 95 |
|
|---|
| 96 |
if (access (prefs_path, R_OK | W_OK) == -1) { |
|---|
| 97 |
switch (errno) { |
|---|
| 98 |
case EACCES: |
|---|
| 99 |
if (chmod (prefs_path, FILE_MODE) == -1) { |
|---|
| 100 |
perror ("Error setting permissions on ~/.sweep/preferences.tdb"); |
|---|
| 101 |
exit (1); |
|---|
| 102 |
} else { |
|---|
| 103 |
printf ("Changed mode of %s to %04o\n", prefs_path, FILE_MODE); |
|---|
| 104 |
} |
|---|
| 105 |
break; |
|---|
| 106 |
default: |
|---|
| 107 |
break; |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
prefs_tdb = tdb_open (prefs_path, 0, 0, O_RDWR | O_CREAT, FILE_MODE); |
|---|
| 112 |
|
|---|
| 113 |
if (prefs_tdb == NULL) { |
|---|
| 114 |
|
|---|
| 115 |
if (ignore_failed_tdb_lock == TRUE) |
|---|
| 116 |
{ |
|---|
| 117 |
prefs_tdb = tdb_open (prefs_path, 0, TDB_NOLOCK, O_RDWR | O_CREAT, FILE_MODE); |
|---|
| 118 |
if (prefs_tdb != NULL) { |
|---|
| 119 |
fprintf(stderr, "Warning: couldn't get lock to ~/.sweep/preferences.tdb.\n" |
|---|
| 120 |
" opened without locking\n"); |
|---|
| 121 |
return; |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
perror (_("Error opening ~/.sweep/preferences.tdb")); |
|---|
| 125 |
exit (1); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
int |
|---|
| 130 |
prefs_close (void) |
|---|
| 131 |
{ |
|---|
| 132 |
if (prefs_tdb == NULL) return 0; |
|---|
| 133 |
|
|---|
| 134 |
return tdb_close (prefs_tdb); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
int |
|---|
| 138 |
prefs_delete (char * key) |
|---|
| 139 |
{ |
|---|
| 140 |
TDB_DATA key_data; |
|---|
| 141 |
|
|---|
| 142 |
if (prefs_tdb == NULL) return -1; |
|---|
| 143 |
|
|---|
| 144 |
key_data.dptr = key; |
|---|
| 145 |
key_data.dsize = strlen (key); |
|---|
| 146 |
|
|---|
| 147 |
return tdb_delete (prefs_tdb, key_data); |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
int * |
|---|
| 151 |
prefs_get_int (char * key) |
|---|
| 152 |
{ |
|---|
| 153 |
TDB_DATA key_data, val_data; |
|---|
| 154 |
int nval, val; |
|---|
| 155 |
|
|---|
| 156 |
if (prefs_tdb == NULL) return NULL; |
|---|
| 157 |
|
|---|
| 158 |
key_data.dptr = key; |
|---|
| 159 |
key_data.dsize = strlen (key); |
|---|
| 160 |
|
|---|
| 161 |
val_data = tdb_fetch (prefs_tdb, key_data); |
|---|
| 162 |
|
|---|
| 163 |
if (val_data.dptr == NULL) { |
|---|
| 164 |
return NULL; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
nval = * (int *)val_data.dptr; |
|---|
| 168 |
val = g_ntohl (nval); |
|---|
| 169 |
|
|---|
| 170 |
* (int *)val_data.dptr = val; |
|---|
| 171 |
|
|---|
| 172 |
return (int *)val_data.dptr; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
int |
|---|
| 176 |
prefs_set_int (char * key, int val) |
|---|
| 177 |
{ |
|---|
| 178 |
TDB_DATA key_data, val_data; |
|---|
| 179 |
int nval; |
|---|
| 180 |
|
|---|
| 181 |
if (prefs_tdb == NULL) return -1; |
|---|
| 182 |
|
|---|
| 183 |
key_data.dptr = key; |
|---|
| 184 |
key_data.dsize = strlen (key); |
|---|
| 185 |
|
|---|
| 186 |
nval = g_htonl (val); |
|---|
| 187 |
|
|---|
| 188 |
val_data.dptr = (char *)&nval; |
|---|
| 189 |
val_data.dsize = sizeof (int); |
|---|
| 190 |
|
|---|
| 191 |
return tdb_store (prefs_tdb, key_data, val_data, TDB_REPLACE); |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
long * |
|---|
| 195 |
prefs_get_long (char * key) |
|---|
| 196 |
{ |
|---|
| 197 |
TDB_DATA key_data, val_data; |
|---|
| 198 |
long nval, val; |
|---|
| 199 |
|
|---|
| 200 |
if (prefs_tdb == NULL) return NULL; |
|---|
| 201 |
|
|---|
| 202 |
key_data.dptr = key; |
|---|
| 203 |
key_data.dsize = strlen (key); |
|---|
| 204 |
|
|---|
| 205 |
val_data = tdb_fetch (prefs_tdb, key_data); |
|---|
| 206 |
|
|---|
| 207 |
if (val_data.dptr == NULL) { |
|---|
| 208 |
return NULL; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
nval = * (long *)val_data.dptr; |
|---|
| 212 |
val = nval; |
|---|
| 213 |
|
|---|
| 214 |
* (long *)val_data.dptr = val; |
|---|
| 215 |
|
|---|
| 216 |
return (long *)val_data.dptr; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
int |
|---|
| 220 |
prefs_set_long (char * key, long val) |
|---|
| 221 |
{ |
|---|
| 222 |
TDB_DATA key_data, val_data; |
|---|
| 223 |
long nval; |
|---|
| 224 |
|
|---|
| 225 |
if (prefs_tdb == NULL) return -1; |
|---|
| 226 |
|
|---|
| 227 |
key_data.dptr = key; |
|---|
| 228 |
key_data.dsize = strlen (key); |
|---|
| 229 |
|
|---|
| 230 |
nval = val; |
|---|
| 231 |
|
|---|
| 232 |
val_data.dptr = (char *)&nval; |
|---|
| 233 |
val_data.dsize = sizeof (long); |
|---|
| 234 |
|
|---|
| 235 |
return tdb_store (prefs_tdb, key_data, val_data, TDB_REPLACE); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
float * |
|---|
| 239 |
prefs_get_float (char * key) |
|---|
| 240 |
{ |
|---|
| 241 |
TDB_DATA key_data, val_data; |
|---|
| 242 |
int nval, val; |
|---|
| 243 |
float fval; |
|---|
| 244 |
union { int ip; float fp;} fp; |
|---|
| 245 |
if (prefs_tdb == NULL) return NULL; |
|---|
| 246 |
|
|---|
| 247 |
key_data.dptr = key; |
|---|
| 248 |
key_data.dsize = strlen (key); |
|---|
| 249 |
|
|---|
| 250 |
val_data = tdb_fetch (prefs_tdb, key_data); |
|---|
| 251 |
|
|---|
| 252 |
if (val_data.dptr == NULL) { |
|---|
| 253 |
return NULL; |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
nval = *(int *)val_data.dptr; |
|---|
| 257 |
val = g_ntohl (nval); |
|---|
| 258 |
|
|---|
| 259 |
fp.ip = val; |
|---|
| 260 |
fval = fp.fp; |
|---|
| 261 |
|
|---|
| 262 |
#ifdef DEBUG |
|---|
| 263 |
printf ("preferences.c: got %s is %f\n", key, fval); |
|---|
| 264 |
#endif |
|---|
| 265 |
|
|---|
| 266 |
*(float *)val_data.dptr = fval; |
|---|
| 267 |
|
|---|
| 268 |
return (float *)val_data.dptr; |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
int |
|---|
| 272 |
prefs_set_float (char * key, float val) |
|---|
| 273 |
{ |
|---|
| 274 |
TDB_DATA key_data, val_data; |
|---|
| 275 |
int nval, ival; |
|---|
| 276 |
union { int ip; float fp;} fp; |
|---|
| 277 |
|
|---|
| 278 |
if (prefs_tdb == NULL) return -1; |
|---|
| 279 |
|
|---|
| 280 |
#ifdef DEBUG |
|---|
| 281 |
printf ("preferences.c: setting %s to %f\n", key, val); |
|---|
| 282 |
#endif |
|---|
| 283 |
|
|---|
| 284 |
key_data.dptr = key; |
|---|
| 285 |
key_data.dsize = strlen (key); |
|---|
| 286 |
|
|---|
| 287 |
fp.fp = val; |
|---|
| 288 |
ival = fp.ip; |
|---|
| 289 |
nval = g_htonl (ival); |
|---|
| 290 |
|
|---|
| 291 |
val_data.dptr = (char *)&nval; |
|---|
| 292 |
val_data.dsize = sizeof (float); |
|---|
| 293 |
|
|---|
| 294 |
return tdb_store (prefs_tdb, key_data, val_data, TDB_REPLACE); |
|---|
| 295 |
} |
|---|
| 296 |
|
|---|
| 297 |
char * |
|---|
| 298 |
prefs_get_string (char * key) |
|---|
| 299 |
{ |
|---|
| 300 |
TDB_DATA key_data, val_data; |
|---|
| 301 |
|
|---|
| 302 |
if (prefs_tdb == NULL) return NULL; |
|---|
| 303 |
|
|---|
| 304 |
key_data.dptr = key; |
|---|
| 305 |
key_data.dsize = strlen (key); |
|---|
| 306 |
|
|---|
| 307 |
val_data = tdb_fetch (prefs_tdb, key_data); |
|---|
| 308 |
|
|---|
| 309 |
return (char *)val_data.dptr; |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
int |
|---|
| 313 |
prefs_set_string (char * key, char * val) |
|---|
| 314 |
{ |
|---|
| 315 |
TDB_DATA key_data, val_data; |
|---|
| 316 |
|
|---|
| 317 |
if (prefs_tdb == NULL) return -1; |
|---|
| 318 |
|
|---|
| 319 |
key_data.dptr = key; |
|---|
| 320 |
key_data.dsize = strlen (key); |
|---|
| 321 |
|
|---|
| 322 |
val_data.dptr = val; |
|---|
| 323 |
val_data.dsize = strlen (val) + 1; |
|---|
| 324 |
|
|---|
| 325 |
return tdb_store (prefs_tdb, key_data, val_data, TDB_REPLACE); |
|---|
| 326 |
} |
|---|