~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/control/conf.h

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-04-14 23:42:12 UTC
  • Revision ID: james.westby@ubuntu.com-20110414234212-kuffcz5wiu18v6ra
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of darktable,
 
3
    copyright (c) 2009--2010 johannes hanika.
 
4
 
 
5
    darktable is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation, either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    darktable is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with darktable.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
#ifndef DT_USER_CONFIG_H
 
19
#define DT_USER_CONFIG_H
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <strings.h>
 
28
#include "common/darktable.h"
 
29
 
 
30
#ifdef HAVE_GCONF
 
31
#include <gconf/gconf-client.h>
 
32
#define DT_GCONF_DIR "/apps/darktable"
 
33
#endif
 
34
 
 
35
// silly gconf replacement for mac. very sucky altogether.
 
36
 
 
37
#define DT_CONF_MAX_VARS 512
 
38
#define DT_CONF_MAX_VAR_BUF 512
 
39
 
 
40
typedef struct dt_conf_t
 
41
{
 
42
#ifdef HAVE_GCONF
 
43
  GConfClient *gconf;
 
44
#else
 
45
  char filename[1024];
 
46
  dt_pthread_mutex_t mutex;
 
47
  int  num;
 
48
  char varname[DT_CONF_MAX_VARS][DT_CONF_MAX_VAR_BUF];
 
49
  char varval [DT_CONF_MAX_VARS][DT_CONF_MAX_VAR_BUF];
 
50
#endif
 
51
}
 
52
dt_conf_t;
 
53
 
 
54
#ifndef HAVE_GCONF
 
55
/** return slot for this variable or newly allocated slot. */
 
56
static inline int dt_conf_get_var_pos(const char *name)
 
57
{
 
58
  for(int i=0; i<darktable.conf->num; i++)
 
59
  {
 
60
    if(!strncmp(name, darktable.conf->varname[i], DT_CONF_MAX_VAR_BUF)) return i;
 
61
  }
 
62
  int num = darktable.conf->num++;
 
63
  snprintf(darktable.conf->varname[num], DT_CONF_MAX_VAR_BUF, "%s", name);
 
64
  memset(darktable.conf->varval[num], 0, DT_CONF_MAX_VAR_BUF);
 
65
  return num;
 
66
}
 
67
#endif
 
68
 
 
69
static inline void dt_conf_set_int(const char *name, int val)
 
70
{
 
71
#ifdef HAVE_GCONF
 
72
  char var[1024];
 
73
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
74
  gconf_client_set_int (darktable.conf->gconf, var, val, NULL);
 
75
#else
 
76
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
77
  const int num = dt_conf_get_var_pos(name);
 
78
  snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%d", val);
 
79
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
80
#endif
 
81
}
 
82
 
 
83
static inline void dt_conf_set_float(const char *name, float val)
 
84
{
 
85
#ifdef HAVE_GCONF
 
86
  char var[1024];
 
87
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
88
  gconf_client_set_float (darktable.conf->gconf, var, val, NULL);
 
89
#else
 
90
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
91
  const int num = dt_conf_get_var_pos(name);
 
92
  snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%f", val);
 
93
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
94
#endif
 
95
}
 
96
 
 
97
static inline void dt_conf_set_bool(const char *name, int val)
 
98
{
 
99
#ifdef HAVE_GCONF
 
100
  char var[1024];
 
101
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
102
  gconf_client_set_bool (darktable.conf->gconf, var, val, NULL);
 
103
#else
 
104
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
105
  const int num = dt_conf_get_var_pos(name);
 
106
  snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%s", val ? "TRUE" : "FALSE");
 
107
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
108
#endif
 
109
}
 
110
 
 
111
static inline void dt_conf_set_string(const char *name, const char *val)
 
112
{
 
113
#ifdef HAVE_GCONF
 
114
  char var[1024];
 
115
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
116
  gconf_client_set_string (darktable.conf->gconf, var, val, NULL);
 
117
#else
 
118
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
119
  const int num = dt_conf_get_var_pos(name);
 
120
  snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%s", val);
 
121
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
122
#endif
 
123
}
 
124
 
 
125
static inline int dt_conf_get_int(const char *name)
 
126
{
 
127
#ifdef HAVE_GCONF
 
128
  char var[1024];
 
129
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
130
  return gconf_client_get_int (darktable.conf->gconf, var, NULL);
 
131
#else
 
132
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
133
  const int num = dt_conf_get_var_pos(name);
 
134
  const int val = atol(darktable.conf->varval[num]);
 
135
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
136
  return val;
 
137
#endif
 
138
}
 
139
 
 
140
static inline float dt_conf_get_float(const char *name)
 
141
{
 
142
#ifdef HAVE_GCONF
 
143
  char var[1024];
 
144
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
145
  return gconf_client_get_float (darktable.conf->gconf, var, NULL);
 
146
#else
 
147
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
148
  const int num = dt_conf_get_var_pos(name);
 
149
  const float val = atof(darktable.conf->varval[num]);
 
150
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
151
  return val;
 
152
#endif
 
153
}
 
154
 
 
155
static inline int dt_conf_get_bool(const char *name)
 
156
{
 
157
#ifdef HAVE_GCONF
 
158
  char var[1024];
 
159
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
160
  return gconf_client_get_bool (darktable.conf->gconf, var, NULL);
 
161
#else
 
162
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
163
  const int num = dt_conf_get_var_pos(name);
 
164
  const int val = darktable.conf->varval[num][0] == 'T';
 
165
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
166
  return val;
 
167
#endif
 
168
}
 
169
 
 
170
static inline gchar *dt_conf_get_string(const char *name)
 
171
{
 
172
#ifdef HAVE_GCONF
 
173
  char var[1024];
 
174
  snprintf(var, 1024, "%s/%s", DT_GCONF_DIR, name);
 
175
  return gconf_client_get_string (darktable.conf->gconf, var, NULL);
 
176
#else
 
177
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
178
  const int num = dt_conf_get_var_pos(name);
 
179
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
180
  return g_strdup(darktable.conf->varval[num]);
 
181
#endif
 
182
}
 
183
 
 
184
typedef struct dt_conf_string_entry_t
 
185
{
 
186
  char *key;
 
187
  char *value;
 
188
} dt_conf_string_entry_t;
 
189
 
 
190
/** get all strings in */
 
191
static inline GSList *dt_conf_all_string_entries (const char *dir)
 
192
{
 
193
  GSList *result = NULL;
 
194
 
 
195
#ifdef HAVE_GCONF
 
196
  char var[1024];
 
197
  snprintf (var, 1024, "%s/%s", DT_GCONF_DIR, dir);
 
198
  GSList *slist = gconf_client_all_entries (darktable.conf->gconf, var, NULL);
 
199
  GSList *item=slist;
 
200
  if(slist)
 
201
    do
 
202
    {
 
203
      GConfEntry *entry = (GConfEntry *)item->data;
 
204
      dt_conf_string_entry_t *nv = (dt_conf_string_entry_t*)g_malloc (sizeof(dt_conf_string_entry_t));
 
205
 
 
206
      /* get the key name from path/key */
 
207
      gchar *p=entry->key+strlen (entry->key);
 
208
      while (*--p!='/');
 
209
      nv->key = g_strdup (++p);
 
210
 
 
211
      /* get the value from gconf entry */
 
212
      nv->value = g_strdup (gconf_value_get_string (entry->value));
 
213
      if (nv->key && nv->value)
 
214
        result = g_slist_append (result,nv);
 
215
      else
 
216
      {
 
217
        g_free (nv->key);
 
218
        g_free (nv);
 
219
      }
 
220
 
 
221
    }
 
222
    while ((item=g_slist_next (item))!=NULL);
 
223
 
 
224
#else
 
225
  dt_pthread_mutex_lock(&darktable.conf->mutex);
 
226
  for (int i=0; i<DT_CONF_MAX_VARS; i++)
 
227
  {
 
228
    if (strcmp(darktable.conf->varname[i],dir)==0)
 
229
    {
 
230
      dt_conf_string_entry_t *nv = (dt_conf_string_entry_t*)g_malloc (sizeof(dt_conf_string_entry_t));
 
231
      gchar *key = g_strdup (darktable.conf->varname[i]);
 
232
 
 
233
      /* get the key name from path/key */
 
234
      gchar *p = key+strlen (key);
 
235
      while (*--p!='/');
 
236
      nv->key = g_strdup (++p);
 
237
 
 
238
      /* get the value */
 
239
      nv->value = g_strdup(darktable.conf->varval[i]);
 
240
 
 
241
      result = g_slist_append (result,nv);
 
242
    }
 
243
  }
 
244
  dt_pthread_mutex_unlock(&darktable.conf->mutex);
 
245
 
 
246
#endif
 
247
  return result;
 
248
}
 
249
 
 
250
static inline void dt_conf_init(dt_conf_t *cf, const char *filename)
 
251
{
 
252
#ifdef HAVE_GCONF
 
253
  g_type_init();
 
254
  cf->gconf = gconf_client_get_default();
 
255
  return;
 
256
#else
 
257
  memset(cf->varname,0, DT_CONF_MAX_VARS*DT_CONF_MAX_VAR_BUF);
 
258
  memset(cf->varval, 0, DT_CONF_MAX_VARS*DT_CONF_MAX_VAR_BUF);
 
259
  dt_pthread_mutex_init(&darktable.conf->mutex, NULL);
 
260
  snprintf(darktable.conf->filename, 1024, "%s", filename);
 
261
  darktable.conf->num = 0;
 
262
  FILE *f = fopen(filename, "rb");
 
263
  char line[1024];
 
264
  int read = 0;
 
265
  if(!f)
 
266
  {
 
267
    char buf[1024], defaultrc[1024];
 
268
    dt_get_datadir(buf, 1024);
 
269
    snprintf(defaultrc, 1024, "%s/darktablerc", buf);
 
270
    f = fopen(defaultrc, "rb");
 
271
  }
 
272
  if(!f) return;
 
273
  while(!feof(f))
 
274
  {
 
275
    read = fscanf(f, "%[^\n]\n", line);
 
276
    char *c = line;
 
277
    while(*c != '=' && c < line + strlen(line)) c++;
 
278
    if(*c == '=')
 
279
    {
 
280
      *c = '\0';
 
281
      dt_conf_set_string(line, c+1);
 
282
    }
 
283
  }
 
284
  fclose(f);
 
285
  return;
 
286
#endif
 
287
}
 
288
 
 
289
static inline void dt_conf_cleanup(dt_conf_t *cf)
 
290
{
 
291
#ifdef HAVE_GCONF
 
292
  g_object_unref(cf->gconf);
 
293
#else
 
294
  FILE *f = fopen(cf->filename, "wb");
 
295
  if(!f) return;
 
296
  for(int i=0; i<cf->num; i++)
 
297
  {
 
298
    fprintf(f, "%s=%s\n", cf->varname[i], cf->varval[i]);
 
299
  }
 
300
  fclose(f);
 
301
  dt_pthread_mutex_destroy(&darktable.conf->mutex);
 
302
#endif
 
303
}
 
304
 
 
305
/** check if key exists, return 1 if lookup successed, 0 if failed..*/
 
306
static inline int dt_conf_key_exists (const char *key)
 
307
{
 
308
  int res = 0;
 
309
#ifdef HAVE_GCONF
 
310
  GError *error=NULL;
 
311
  GConfValue *value = gconf_client_get (darktable.conf->gconf,key,&error);
 
312
  if( value != NULL && error == NULL ) res = 1;
 
313
#else
 
314
  /* lookup in stringtable for match of key name */
 
315
  dt_pthread_mutex_lock (&darktable.conf->mutex);
 
316
  for (int i=0; i<darktable.conf->num; i++)
 
317
  {
 
318
    if (!strncmp (key, darktable.conf->varname[i], DT_CONF_MAX_VAR_BUF))
 
319
    {
 
320
      res=1;
 
321
      break;
 
322
    }
 
323
  }
 
324
  dt_pthread_mutex_unlock (&darktable.conf->mutex);
 
325
#endif
 
326
  return res;
 
327
}
 
328
 
 
329
#endif