Replace TRUE/FALSE with 1/0 in the generated gtkrc file.
[lxde/lxappearance.git] / src / lxappearance.c
CommitLineData
0cd84ed6 1/*
3a7d812e 2 * lxappearance.c
0cd84ed6
HJYP
3 *
4 * Copyright 2010 PCMan <pcman.tw@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
3a7d812e 26#include "lxappearance.h"
15c155a6 27
0cd84ed6
HJYP
28#include <gtk/gtk.h>
29#include <glib/gi18n.h>
15c155a6
HJYP
30
31#include <X11/X.h>
32#include <X11/Xatom.h>
33#include <X11/Xlib.h>
34#include <gdk/gdkx.h>
ae63f687 35#include <string.h>
15c155a6 36
0cd84ed6 37#include "widget-theme.h"
71ce58a7 38#include "color-scheme.h"
0cd84ed6
HJYP
39#include "icon-theme.h"
40#include "cursor-theme.h"
e597424e 41#include "other.h"
756f0a66 42#include "plugin.h"
0cd84ed6 43
15c155a6
HJYP
44LXAppearance app = {0};
45
46Atom lxsession_atom = 0;
ae63f687 47static const char* lxsession_name = NULL;
15c155a6
HJYP
48
49static void check_lxsession()
50{
51 lxsession_atom = XInternAtom( GDK_DISPLAY(), "_LXSESSION", True );
52 if( lxsession_atom != None )
53 {
54 XGrabServer( GDK_DISPLAY() );
55 if( XGetSelectionOwner( GDK_DISPLAY(), lxsession_atom ) )
ae63f687 56 {
15c155a6 57 app.use_lxsession = TRUE;
ae63f687
HJYP
58 lxsession_name = g_getenv("DESKTOP_SESSION");
59 }
15c155a6
HJYP
60 XUngrabServer( GDK_DISPLAY() );
61 }
62}
63
0cd84ed6
HJYP
64static GOptionEntry option_entries[] =
65{
66 { NULL }
67};
68
ae63f687
HJYP
69static void save_cursor_theme_name()
70{
71 char* dir_path;
72 if(!app.cursor_theme || !g_strcmp0(app.cursor_theme, "default"))
73 return;
74
75 dir_path = g_build_filename(g_get_home_dir(), ".icons/default", NULL);
76 if(0 == g_mkdir_with_parents(dir_path, 0700))
77 {
78 char* index_theme = g_build_filename(dir_path, "index.theme", NULL);
79 char* content = g_strdup_printf(
80 "# This file is written by LXAppearance. Do not edit."
81 "[Icon Theme]\n"
82 "Name=Default\n"
83 "Comment=Default Cursor Theme\n"
84 "Inherits=%s\n", app.cursor_theme);
85 g_file_set_contents(index_theme, content, -1, NULL);
86 g_free(content);
87 g_free(index_theme);
88 }
89 g_free(dir_path);
90
91 /*
92 dir_path = g_build_filename(g_get_home_dir(), ".Xdefaults", NULL);
93 Xcursor.theme: name
94 Xcursor.size: [size]
95 g_file_set_contents(dir_path, "", -1, NULL);
96 g_free(dir_path);
97 */
98}
99
100static void reload_all_programs()
15c155a6 101{
ae63f687
HJYP
102 GdkEventClient event;
103 event.type = GDK_CLIENT_EVENT;
104 event.send_event = TRUE;
105 event.window = NULL;
15c155a6 106
ae63f687
HJYP
107 if( app.use_lxsession )
108 {
109 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
110 event.data.b[0] = 0; /* LXS_RELOAD */
111 }
112 else
113 {
114 /* if( icon_only )
115 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
116 */
117 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
118 }
119 event.data_format = 8;
120 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
121}
122
9bb12c12
HJYP
123static const char* bool2str(gboolean val)
124{
125 return val ? "TRUE" : "FALSE";
126}
127
ae63f687
HJYP
128static void lxappearance_save_gtkrc()
129{
130 static const char* tb_styles[]={
131 "GTK_TOOLBAR_ICONS",
132 "GTK_TOOLBAR_TEXT",
133 "GTK_TOOLBAR_BOTH",
134 "GTK_TOOLBAR_BOTH_HORIZ"
135 };
136 static const char* tb_icon_sizes[]={
137 "GTK_ICON_SIZE_INVALID",
138 "GTK_ICON_SIZE_MENU",
139 "GTK_ICON_SIZE_SMALL_TOOLBAR",
140 "GTK_ICON_SIZE_LARGE_TOOLBAR",
141 "GTK_ICON_SIZE_BUTTON",
142 "GTK_ICON_SIZE_DND",
143 "GTK_ICON_SIZE_DIALOG"
144 };
145
3518acea 146 char* file_path = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
c92e9c11
HJYP
147 GString* content = g_string_sized_new(512);
148 g_string_append_printf(content,
ae63f687
HJYP
149 "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
150 "# Any customization should be done in ~/.gtkrc-2.0.mine instead.\n\n"
151 "gtk-theme-name=\"%s\"\n"
152 "gtk-icon-theme-name=\"%s\"\n"
153 "gtk-font-name=\"%s\"\n"
154 "gtk-toolbar-style=%s\n"
155 "gtk-toolbar-icon-size=%s\n"
156 "gtk-cursor-theme-name=\"%s\"\n"
157 "gtk-cursor-theme-size=%d\n"
36827f45
HJYP
158 "gtk-button-images=%d\n"
159 "gtk-menu-images=%d\n"
9bb12c12 160#if GTK_CHECK_VERSION(2, 14, 0)
36827f45
HJYP
161 "gtk-enable-event-sounds=%d\n"
162 "gtk-enable-input-feedback-sounds=%d\n",
9bb12c12 163#endif
ae63f687
HJYP
164 app.widget_theme,
165 app.icon_theme,
166 app.default_font,
167 tb_styles[app.toolbar_style],
168 tb_icon_sizes[app.toolbar_icon_size],
169 app.cursor_theme,
170 app.cursor_theme_size,
36827f45
HJYP
171 app.button_images ? 1 : 0,
172 app.menu_images ? 1 : 0,
9bb12c12 173#if GTK_CHECK_VERSION(2, 14, 0)
36827f45
HJYP
174 app.enable_event_sound ? 1 : 0,
175 app.enable_input_feedback ? 1 : 0
9bb12c12 176#endif
c92e9c11
HJYP
177 );
178
179 if(app.color_scheme)
180 {
181 char* escaped = g_strescape(app.color_scheme, NULL);
182 g_string_append_printf(content,
183 "gtk-color-scheme=\"\"\n",
184 escaped);
185 g_free(escaped);
186 }
187
188 g_string_append_printf(content,
189 "include \"%s/.gtkrc-2.0.mine\"\n",
ae63f687
HJYP
190 g_get_home_dir());
191
c92e9c11
HJYP
192 g_file_set_contents(file_path, content->str, content->len, NULL);
193 g_string_free(content, TRUE);
ae63f687 194 g_free(file_path);
15c155a6
HJYP
195}
196
197static void lxappearance_save_lxsession()
198{
ae63f687
HJYP
199 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
200 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
201 char* buf;
202 int len;
203 GKeyFile* kf = g_key_file_new();
204
205 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
206 {
207 /* the user config file doesn't exist, create its parent dir */
208 len = strlen(user_config_file) - strlen("/desktop.conf");
209 user_config_file[len] = '\0';
210 g_debug("user_config_file = %s", user_config_file);
211 g_mkdir_with_parents(user_config_file, 0700);
212 user_config_file[len] = '/';
213
df6716ee
HJYP
214 g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL,
215 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
ae63f687
HJYP
216 }
217
218 g_free(rel_path);
15c155a6 219
ae63f687
HJYP
220 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
221 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
222
c92e9c11 223 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
71ce58a7 224
ae63f687
HJYP
225 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
226
227 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
228 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
229 save_cursor_theme_name();
230
231 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
232 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
233
9bb12c12
HJYP
234 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
235 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
236
6b54b65a
HJYP
237 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
238 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
239
240#if GTK_CHECK_VERSION(2, 14, 0)
9bb12c12
HJYP
241 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
242 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
243 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
244#endif
245
ae63f687
HJYP
246 buf = g_key_file_to_data( kf, &len, NULL );
247 g_key_file_free(kf);
248
249 g_file_set_contents(user_config_file, buf, len, NULL);
250 g_free(buf);
251 g_free(user_config_file);
15c155a6
HJYP
252}
253
254static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
255{
256 switch(res)
257 {
258 case GTK_RESPONSE_APPLY:
259
260 if(app.use_lxsession)
261 lxappearance_save_lxsession();
262 else
263 lxappearance_save_gtkrc();
264
ae63f687
HJYP
265 reload_all_programs();
266
15c155a6 267 app.changed = FALSE;
df6716ee 268 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, FALSE);
15c155a6
HJYP
269 break;
270 case 1: /* about dialog */
271 {
272 GtkBuilder* b = gtk_builder_new();
273 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
274 {
275 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
df6716ee 276 gtk_dialog_run(GTK_DIALOG(dlg));
15c155a6
HJYP
277 gtk_widget_destroy(dlg);
278 }
279 g_object_unref(b);
280 }
281 break;
282 default:
283 gtk_main_quit();
284 }
285}
286
287static void settings_init()
288{
e597424e
HJYP
289 GtkSettings* settings = gtk_settings_get_default();
290 g_object_get(settings,
291 "gtk-theme-name", &app.widget_theme,
ae63f687 292 "gtk-font-name", &app.default_font,
e597424e
HJYP
293 "gtk-icon-theme-name", &app.icon_theme,
294 "gtk-cursor-theme-name", &app.cursor_theme,
4e409096 295 "gtk-cursor-theme-size", &app.cursor_theme_size,
e597424e
HJYP
296 "gtk-toolbar-style", &app.toolbar_style,
297 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
6b54b65a
HJYP
298 "gtk-button-images", &app.button_images,
299 "gtk-menu-images", &app.menu_images,
9bb12c12
HJYP
300#if GTK_CHECK_VERSION(2, 14, 0)
301 "gtk-enable-event-sounds", &app.enable_event_sound,
302 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
303#endif
e597424e 304 NULL);
15c155a6 305 /* try to figure out cursor theme used. */
15c155a6
HJYP
306 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
307 {
308 /* get the real theme name from default. */
309 GKeyFile* kf = g_key_file_new();
310 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
311 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
312 g_free(fpath);
313
314 if(!ret)
315 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
316
317 if(ret)
318 {
e597424e 319 g_free(app.cursor_theme);
15c155a6
HJYP
320 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
321 g_debug("cursor theme name: %s", app.cursor_theme);
322 }
323 g_key_file_free(kf);
324 }
c92e9c11
HJYP
325
326 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
327 /* try to load custom color scheme if available */
328 if(app.use_lxsession)
329 {
330 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
331 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
332 GKeyFile* kf = g_key_file_new();
333 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
334 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
df6716ee 335 else if(g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL, 0, NULL))
c92e9c11
HJYP
336 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
337 g_key_file_free(kf);
338 g_free(rel_path);
339 g_free(user_config_file);
340
341 if(app.color_scheme)
342 {
343 if(*app.color_scheme)
344 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
345 else
346 {
347 g_free(app.color_scheme);
348 app.color_scheme = NULL;
349 }
350 }
351 }
352 else
353 {
354 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
355 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
356 g_free(gtkrc_file);
357 if(g_hash_table_size(app.color_scheme_hash) > 0)
358 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
359 }
15c155a6
HJYP
360}
361
0cd84ed6
HJYP
362int main(int argc, char** argv)
363{
364 GError* err = NULL;
365 GtkBuilder* b;
0cd84ed6
HJYP
366
367 /* gettext support */
368#ifdef ENABLE_NLS
369 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
370 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
371 textdomain ( GETTEXT_PACKAGE );
372#endif
373
ff19d42c 374 g_thread_init(NULL);
0cd84ed6
HJYP
375 /* initialize GTK+ and parse the command line arguments */
376 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
377 {
378 g_print( "Error: %s\n", err->message );
379 return 1;
380 }
381
1123e741
HJYP
382 app.abi_version = LXAPPEARANCE_ABI_VERSION;
383
15c155a6
HJYP
384 /* check if we're under LXSession */
385 check_lxsession();
386
0cd84ed6
HJYP
387 /* create GUI here */
388 b = gtk_builder_new();
389 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
390 return 1;
391
6b54b65a
HJYP
392 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
393 * Some properties of GtkSettings class are installed by some other gtk classes.
394 * For example, "gtk-menu-images" property is actually installed by initialization
395 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
396 * are created, this property gets correctly registered. So later it can be read. */
397
398 /* load config values */
399 settings_init();
400
e597424e
HJYP
401 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
402
0cd84ed6 403 widget_theme_init(b);
71ce58a7 404 color_scheme_init(b);
0cd84ed6
HJYP
405 icon_theme_init(b);
406 cursor_theme_init(b);
e597424e 407 other_init(b);
756f0a66
HJYP
408 /* the page for window manager plugins */
409 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
410
ff19d42c
HJYP
411 plugins_init(b);
412
15c155a6 413 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
0cd84ed6 414
15c155a6 415 gtk_window_present(GTK_WINDOW(app.dlg));
0cd84ed6
HJYP
416 g_object_unref(b);
417
418 gtk_main();
419
756f0a66
HJYP
420 plugins_finalize();
421
0cd84ed6
HJYP
422 return 0;
423}
15c155a6
HJYP
424
425void lxappearance_changed()
426{
427 if(!app.changed)
428 {
429 app.changed = TRUE;
df6716ee 430 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, TRUE);
15c155a6
HJYP
431 }
432}