initial commit
[gconf-editor] / src / gconf-util.c
1 /*
2  * Copyright (C) 2001, 2002 Anders Carlsson <andersca@gnu.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20 #include "gconf-util.h"
21
22 #include <string.h>
23
24 enum {
25         GCONF_NOT_SET,
26         GCONF_CANNOT_EDIT,
27         GCONF_CAN_EDIT
28 };
29  
30 GType
31 gconf_value_get_type (void)
32 {
33         static GType type = 0;
34
35         if (type == 0) {
36                 type = g_boxed_type_register_static ("GConfValue",
37                                                      (GBoxedCopyFunc)gconf_value_copy,
38                                                      (GBoxedFreeFunc)gconf_value_free);
39         }
40
41         return type;
42 }
43
44 gchar *
45 gconf_get_key_name_from_path (const gchar *path)
46 {
47         const gchar *ptr;
48
49         /* FIXME:  VALIDATE KEY */
50         
51         ptr = path + strlen (path);
52
53         while (ptr[-1] != '/')
54                 ptr--;
55
56         return g_strdup (ptr);
57 }
58
59 gchar *
60 gconf_value_type_to_string (GConfValueType value_type)
61 {
62         switch (value_type) {
63         case GCONF_VALUE_STRING:
64                 return g_strdup ("String");
65                 break;
66         case GCONF_VALUE_INT:
67                 return g_strdup ("Integer");
68                 break;
69         case GCONF_VALUE_BOOL:
70                 return g_strdup ("Boolean");
71                 break;
72         case GCONF_VALUE_LIST:
73                 return g_strdup ("List");
74                 break;
75         default:
76                 return g_strdup_printf ("UNKNOWN, %d", value_type);
77         }
78 }
79
80 GConfSchema *
81 gconf_client_get_schema_for_key (GConfClient *client, const char *key)
82 {
83         GConfEntry *entry;
84         const char *schema_name;
85         GConfSchema *schema = NULL;
86
87         entry = gconf_client_get_entry (client, key, NULL, TRUE, NULL);
88         schema_name = gconf_entry_get_schema_name (entry);
89
90         if (schema_name == NULL)
91                 goto out;
92
93         schema = gconf_client_get_schema (client, schema_name, NULL);
94
95 out:
96         if (entry)
97                 gconf_entry_free (entry);
98
99         return schema;
100 }
101
102 static gboolean
103 can_edit_source (const char *source)
104 {
105         GConfEngine *engine;
106         GConfClient *client;
107         GConfEntry  *entry;
108         GError      *error;
109         gboolean     retval;
110
111         if (!(engine = gconf_engine_get_for_address (source, NULL)))
112                 return FALSE;
113
114         error = NULL;
115         client = gconf_client_get_for_engine (engine);
116         entry = gconf_client_get_entry (client,
117                                         "/apps/gconf-editor/can_edit_source",
118                                         NULL,
119                                         FALSE,
120                                         &error);
121         if (error != NULL) {
122                 g_assert (entry == NULL);
123                 g_error_free (error);
124                 g_object_unref (client);
125                 gconf_engine_unref (engine);
126                 return FALSE;
127         }
128
129         g_assert (entry != NULL);
130
131         retval = gconf_entry_get_is_writable (entry);
132
133         gconf_entry_unref (entry);
134         g_object_unref (client);
135         gconf_engine_unref (engine);
136
137         return retval;
138 }
139
140 gboolean
141 gconf_util_can_edit_defaults (void)
142 {
143         static int can_edit_defaults = GCONF_NOT_SET;
144
145         if (can_edit_defaults == GCONF_NOT_SET)
146                 can_edit_defaults = can_edit_source (GCONF_DEFAULTS_SOURCE);
147
148         return can_edit_defaults;
149 }
150
151 gboolean
152 gconf_util_can_edit_mandatory (void)
153 {
154         static int can_edit_mandatory = GCONF_NOT_SET;
155
156         if (can_edit_mandatory == GCONF_NOT_SET)
157                 can_edit_mandatory = can_edit_source (GCONF_MANDATORY_SOURCE);
158
159         return can_edit_mandatory;
160 }
161
162         
163