2
* Copyright (C) 2007 The GNOME Foundation
3
* Written by Thomas Wood <thos@gnome.org>
4
* Jens Granseuer <jensgr@gmx.net>
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.
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.
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.
29
#include <glib/gstdio.h>
33
#define INCLUDE_SYMBOL ((gpointer) 1)
34
#define ENGINE_SYMBOL ((gpointer) 2)
35
#define COLOR_SCHEME_SYMBOL ((gpointer) 3)
38
gtkrc_find_named (const gchar *name)
40
/* find the gtkrc of the named theme
41
* taken from gtkrc.c (gtk_rc_parse_named)
44
const gchar *home_dir;
45
const gchar *subpath = "gtk-2.0" G_DIR_SEPARATOR_S "gtkrc";
47
/* First look in the users home directory
49
home_dir = g_get_home_dir ();
52
path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
53
if (!g_file_test (path, G_FILE_TEST_EXISTS))
62
gchar *theme_dir = gtk_rc_get_theme_dir ();
63
path = g_build_filename (theme_dir, name, subpath, NULL);
66
if (!g_file_test (path, G_FILE_TEST_EXISTS))
78
gtkrc_get_details (gchar *filename, GSList **engines, GSList **symbolic_colors)
82
GSList *read_files = NULL;
84
GScanner *scanner = g_scanner_new (NULL);
86
g_scanner_scope_add_symbol (scanner, 0, "include", INCLUDE_SYMBOL);
88
g_scanner_scope_add_symbol (scanner, 0, "engine", ENGINE_SYMBOL);
90
files = g_slist_prepend (files, g_strdup (filename));
93
filename = files->data;
94
files = g_slist_delete_link (files, files);
99
if (g_slist_find_custom (read_files, filename, (GCompareFunc) strcmp))
101
g_warning ("Recursion in the gtkrc detected!");
103
continue; /* skip this file since we've done it before... */
106
read_files = g_slist_prepend (read_files, filename);
108
file = g_open (filename, O_RDONLY);
111
g_warning ("Could not open file \"%s\"", filename);
115
g_scanner_input_file (scanner, file);
116
while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
118
GTokenType string_token;
121
if (symbolic_colors == NULL)
123
token = g_scanner_get_next_token (scanner);
124
if (token != G_TOKEN_IDENTIFIER)
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));
131
if (token != G_TOKEN_SYMBOL)
134
if (scanner->value.v_symbol == INCLUDE_SYMBOL)
136
string_token = g_scanner_get_next_token (scanner);
137
if (string_token != G_TOKEN_STRING)
139
if (g_path_is_absolute (scanner->value.v_string))
141
files = g_slist_prepend (files, g_strdup (scanner->value.v_string));
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));
150
else if (scanner->value.v_symbol == ENGINE_SYMBOL)
152
string_token = g_scanner_get_next_token (scanner);
153
if (string_token != G_TOKEN_STRING || scanner->value.v_string[0] == '\0')
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));
164
g_slist_foreach (read_files, (GFunc) g_free, NULL);
165
g_slist_free (read_files);
167
g_scanner_destroy (scanner);
172
gtkrc_get_color_scheme (const gchar *gtkrc_file)
175
gchar *result = NULL;
176
GSList *files = NULL;
177
GSList *read_files = NULL;
179
GScanner *scanner = gtk_rc_scanner_new ();
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);
185
files = g_slist_prepend (files, g_strdup (gtkrc_file));
186
while (files != NULL)
188
gchar *filename = files->data;
189
files = g_slist_delete_link (files, files);
191
if (filename == NULL)
194
if (g_slist_find_custom (read_files, filename, (GCompareFunc) strcmp))
196
g_warning ("Recursion in the gtkrc detected!");
198
continue; /* skip this file since we've done it before... */
201
read_files = g_slist_prepend (read_files, filename);
203
file = g_open (filename, O_RDONLY);
206
g_warning ("Could not open file \"%s\"", filename);
210
g_scanner_input_file (scanner, file);
211
while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
213
if (GINT_TO_POINTER (token) == COLOR_SCHEME_SYMBOL)
215
if (g_scanner_get_next_token (scanner) == '=')
217
token = g_scanner_get_next_token (scanner);
218
if (token == G_TOKEN_STRING)
221
result = g_strdup (scanner->value.v_string);
230
g_slist_foreach (read_files, (GFunc) g_free, NULL);
231
g_slist_free (read_files);
233
g_scanner_destroy (scanner);
238
gtkrc_get_color_scheme_for_theme (const gchar *theme_name)
240
/* try to find the color scheme from the gtkrc */
242
gchar *scheme = NULL;
244
gtkrc_file = gtkrc_find_named (theme_name);
246
scheme = gtkrc_get_color_scheme (gtkrc_file);