~ubuntu-branches/ubuntu/precise/gnome-control-center/precise-updates

« back to all changes in this revision

Viewing changes to capplets/common/gtkrc-utils.c

Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2007 The GNOME Foundation
3
 
 * Written by Thomas Wood <thos@gnome.org>
4
 
 *            Jens Granseuer <jensgr@gmx.net>
5
 
 * All Rights Reserved
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License along
18
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 
 */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
 
26
 
#include <string.h>
27
 
#include <unistd.h>
28
 
#include <glib.h>
29
 
#include <glib/gstdio.h>
30
 
#include <fcntl.h>
31
 
#include <gtk/gtk.h>
32
 
 
33
 
#define INCLUDE_SYMBOL ((gpointer) 1)
34
 
#define ENGINE_SYMBOL ((gpointer) 2)
35
 
#define COLOR_SCHEME_SYMBOL ((gpointer) 3)
36
 
 
37
 
gchar *
38
 
gtkrc_find_named (const gchar *name)
39
 
{
40
 
        /* find the gtkrc of the named theme
41
 
         * taken from gtkrc.c (gtk_rc_parse_named)
42
 
         */
43
 
        gchar *path = NULL;
44
 
        const gchar *home_dir;
45
 
        const gchar *subpath = "gtk-2.0" G_DIR_SEPARATOR_S "gtkrc";
46
 
 
47
 
        /* First look in the users home directory
48
 
        */
49
 
        home_dir = g_get_home_dir ();
50
 
        if (home_dir)
51
 
        {
52
 
                path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
53
 
                if (!g_file_test (path, G_FILE_TEST_EXISTS))
54
 
                {
55
 
                        g_free (path);
56
 
                        path = NULL;
57
 
                }
58
 
        }
59
 
 
60
 
        if (!path)
61
 
        {
62
 
                gchar *theme_dir = gtk_rc_get_theme_dir ();
63
 
                path = g_build_filename (theme_dir, name, subpath, NULL);
64
 
                g_free (theme_dir);
65
 
 
66
 
                if (!g_file_test (path, G_FILE_TEST_EXISTS))
67
 
                {
68
 
                        g_free (path);
69
 
                        path = NULL;
70
 
                }
71
 
        }
72
 
 
73
 
        return path;
74
 
 
75
 
}
76
 
 
77
 
void
78
 
gtkrc_get_details (gchar *filename, GSList **engines, GSList **symbolic_colors)
79
 
{
80
 
        gint file = -1;
81
 
        GSList *files = NULL;
82
 
        GSList *read_files = NULL;
83
 
        GTokenType token;
84
 
        GScanner *scanner = g_scanner_new (NULL);
85
 
 
86
 
        g_scanner_scope_add_symbol (scanner, 0, "include", INCLUDE_SYMBOL);
87
 
        if (engines != NULL)
88
 
          g_scanner_scope_add_symbol (scanner, 0, "engine", ENGINE_SYMBOL);
89
 
 
90
 
        files = g_slist_prepend (files, g_strdup (filename));
91
 
        while (files != NULL)
92
 
        {
93
 
                filename = files->data;
94
 
                files = g_slist_delete_link (files, files);
95
 
 
96
 
                if (filename == NULL)
97
 
                        continue;
98
 
 
99
 
                if (g_slist_find_custom (read_files, filename, (GCompareFunc) strcmp))
100
 
                {
101
 
                        g_warning ("Recursion in the gtkrc detected!");
102
 
                        g_free (filename);
103
 
                        continue; /* skip this file since we've done it before... */
104
 
                }
105
 
 
106
 
                read_files = g_slist_prepend (read_files, filename);
107
 
 
108
 
                file = g_open (filename, O_RDONLY);
109
 
                if (file == -1)
110
 
                {
111
 
                        g_warning ("Could not open file \"%s\"", filename);
112
 
                }
113
 
                else
114
 
                {
115
 
                        g_scanner_input_file (scanner, file);
116
 
                        while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
117
 
                        {
118
 
                                GTokenType string_token;
119
 
                                if (token == '@')
120
 
                                {
121
 
                                        if (symbolic_colors == NULL)
122
 
                                                continue;
123
 
                                        token = g_scanner_get_next_token (scanner);
124
 
                                        if (token != G_TOKEN_IDENTIFIER)
125
 
                                                continue;
126
 
                                        if (!g_slist_find_custom (*symbolic_colors, scanner->value.v_identifier, (GCompareFunc) strcmp))
127
 
                                                *symbolic_colors = g_slist_append (*symbolic_colors, g_strdup (scanner->value.v_identifier));
128
 
                                        continue;
129
 
                                }
130
 
 
131
 
                                if (token != G_TOKEN_SYMBOL)
132
 
                                        continue;
133
 
 
134
 
                                if (scanner->value.v_symbol == INCLUDE_SYMBOL)
135
 
                                {
136
 
                                        string_token = g_scanner_get_next_token (scanner);
137
 
                                        if (string_token != G_TOKEN_STRING)
138
 
                                                continue;
139
 
                                        if (g_path_is_absolute (scanner->value.v_string))
140
 
                                        {
141
 
                                                files = g_slist_prepend (files, g_strdup (scanner->value.v_string));
142
 
                                        }
143
 
                                        else
144
 
                                        {
145
 
                                                gchar *basedir = g_path_get_dirname (filename);
146
 
                                                files = g_slist_prepend (files, g_build_path (G_DIR_SEPARATOR_S, basedir, scanner->value.v_string, NULL));
147
 
                                                g_free (basedir);
148
 
                                        }
149
 
                                }
150
 
                                else if (scanner->value.v_symbol == ENGINE_SYMBOL)
151
 
                                {
152
 
                                        string_token = g_scanner_get_next_token (scanner);
153
 
                                        if (string_token != G_TOKEN_STRING || scanner->value.v_string[0] == '\0')
154
 
                                                continue;
155
 
                                        if (!g_slist_find_custom (*engines, scanner->value.v_string, (GCompareFunc) strcmp))
156
 
                                                *engines = g_slist_append (*engines, g_strdup (scanner->value.v_string));
157
 
                                }
158
 
 
159
 
                        }
160
 
                        close (file);
161
 
                }
162
 
        }
163
 
 
164
 
        g_slist_foreach (read_files, (GFunc) g_free, NULL);
165
 
        g_slist_free (read_files);
166
 
 
167
 
        g_scanner_destroy (scanner);
168
 
}
169
 
 
170
 
 
171
 
gchar *
172
 
gtkrc_get_color_scheme (const gchar *gtkrc_file)
173
 
{
174
 
        gint file = -1;
175
 
        gchar *result = NULL;
176
 
        GSList *files = NULL;
177
 
        GSList *read_files = NULL;
178
 
        GTokenType token;
179
 
        GScanner *scanner = gtk_rc_scanner_new ();
180
 
 
181
 
        g_scanner_scope_add_symbol (scanner, 0, "include", INCLUDE_SYMBOL);
182
 
        g_scanner_scope_add_symbol (scanner, 0, "gtk_color_scheme", COLOR_SCHEME_SYMBOL);
183
 
        g_scanner_scope_add_symbol (scanner, 0, "gtk-color-scheme", COLOR_SCHEME_SYMBOL);
184
 
 
185
 
        files = g_slist_prepend (files, g_strdup (gtkrc_file));
186
 
        while (files != NULL)
187
 
        {
188
 
                gchar *filename = files->data;
189
 
                files = g_slist_delete_link (files, files);
190
 
 
191
 
                if (filename == NULL)
192
 
                        continue;
193
 
 
194
 
                if (g_slist_find_custom (read_files, filename, (GCompareFunc) strcmp))
195
 
                {
196
 
                        g_warning ("Recursion in the gtkrc detected!");
197
 
                        g_free (filename);
198
 
                        continue; /* skip this file since we've done it before... */
199
 
                }
200
 
 
201
 
                read_files = g_slist_prepend (read_files, filename);
202
 
 
203
 
                file = g_open (filename, O_RDONLY);
204
 
                if (file == -1)
205
 
                {
206
 
                        g_warning ("Could not open file \"%s\"", filename);
207
 
                }
208
 
                else
209
 
                {
210
 
                        g_scanner_input_file (scanner, file);
211
 
                        while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
212
 
                        {
213
 
                                if (GINT_TO_POINTER (token) == COLOR_SCHEME_SYMBOL)
214
 
                                {
215
 
                                        if (g_scanner_get_next_token (scanner) == '=')
216
 
                                        {
217
 
                                                token = g_scanner_get_next_token (scanner);
218
 
                                                if (token == G_TOKEN_STRING)
219
 
                                                {
220
 
                                                        g_free (result);
221
 
                                                        result = g_strdup (scanner->value.v_string);
222
 
                                                }
223
 
                                        }
224
 
                                }
225
 
                        }
226
 
                        close (file);
227
 
                }
228
 
        }
229
 
 
230
 
        g_slist_foreach (read_files, (GFunc) g_free, NULL);
231
 
        g_slist_free (read_files);
232
 
 
233
 
        g_scanner_destroy (scanner);
234
 
        return result;
235
 
}
236
 
 
237
 
gchar *
238
 
gtkrc_get_color_scheme_for_theme (const gchar *theme_name)
239
 
{
240
 
        /* try to find the color scheme from the gtkrc */
241
 
        gchar *gtkrc_file;
242
 
        gchar *scheme = NULL;
243
 
 
244
 
        gtkrc_file = gtkrc_find_named (theme_name);
245
 
        if (gtkrc_file) {
246
 
                scheme = gtkrc_get_color_scheme (gtkrc_file);
247
 
                g_free (gtkrc_file);
248
 
        }
249
 
 
250
 
        return scheme;
251
 
}