summaryrefslogtreecommitdiff
path: root/engine/dconf-engine-profile.c
blob: 896cd7c54b9fc20ab52786389f6e92ec073a8675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright © 2010 Codethink Limited
 * Copyright © 2012 Canonical Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "dconf-engine.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "dconf-engine-source.h"

static DConfEngineSource **
dconf_engine_null_profile (gint *n_sources)
{
  *n_sources = 0;

  return NULL;
}

static DConfEngineSource **
dconf_engine_default_profile (gint *n_sources)
{
  DConfEngineSource **sources;

  sources = g_new (DConfEngineSource *, 1);
  sources[0] = dconf_engine_source_new ("user-db:user");
  *n_sources = 1;

  return sources;
}

static DConfEngineSource **
dconf_engine_read_profile_file (FILE *file,
                                gint *n_sources)
{
  DConfEngineSource **sources;
  gchar line[80];
  gint n = 0, a;

  sources = g_new (DConfEngineSource *, (a = 16));

  while (fgets (line, sizeof line, file))
    {
      DConfEngineSource *source;
      gchar *end;

      end = strchr (line, '\n');

      if (end == NULL)
        {
          g_warning ("ignoring long or unterminated line in dconf profile");

          /* skip until we find the newline or EOF */
          while (fgets (line, sizeof line, file) && !strchr (line, '\n'));

          continue;
        }

      if (end == line)
        continue;

      if (line[0] == '#')
        continue;

      *end = '\0';

      source = dconf_engine_source_new (line);

      if (source != NULL)
        {
          if (n == a)
            sources = g_renew (DConfEngineSource *, sources, a *= 2);

          sources[n++] = source;
        }
    }

  return sources;
}

DConfEngineSource **
dconf_engine_profile_get_default (gint *n_sources)
{
  DConfEngineSource **sources;
  const gchar *profile;
  FILE *file;

  profile = getenv ("DCONF_PROFILE");

  if (profile == NULL)
    return dconf_engine_default_profile (n_sources);

  if (profile[0] != '/')
    {
      gchar *filename = g_build_filename ("/etc/dconf/profile", profile, NULL);
      file = fopen (filename, "r");
      g_free (filename);
    }
  else
    file = fopen (profile, "r");

  if (file != NULL)
    {
      sources = dconf_engine_read_profile_file (file, n_sources);
      fclose (file);
    }
  else
    {
      g_warning ("unable to open named profile (%s): using the null configuration.", profile);
      sources = dconf_engine_null_profile (n_sources);
    }

  return sources;
}