Eanble plugin support.
[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"
6b54b65a
HJYP
158 "gtk-button-images=%s\n"
159 "gtk-menu-images=%s\n"
9bb12c12
HJYP
160#if GTK_CHECK_VERSION(2, 14, 0)
161 "gtk-enable-event-sounds=%s\n"
c92e9c11 162 "gtk-enable-input-feedback-sounds=%s\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,
6b54b65a
HJYP
171 bool2str(app.button_images),
172 bool2str(app.menu_images),
9bb12c12
HJYP
173#if GTK_CHECK_VERSION(2, 14, 0)
174 bool2str(app.enable_event_sound),
c92e9c11 175 bool2str(app.enable_input_feedback)
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
214 g_key_file_load_from_dirs(kf, rel_path, g_get_system_config_dirs(), NULL, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
215 }
216
217 g_free(rel_path);
15c155a6 218
ae63f687
HJYP
219 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
220 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
221
c92e9c11 222 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
71ce58a7 223
ae63f687
HJYP
224 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
225
226 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
227 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
228 save_cursor_theme_name();
229
230 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
231 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
232
9bb12c12
HJYP
233 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
234 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
235
6b54b65a
HJYP
236 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
237 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
238
239#if GTK_CHECK_VERSION(2, 14, 0)
9bb12c12
HJYP
240 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
241 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
242 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
243#endif
244
ae63f687
HJYP
245 buf = g_key_file_to_data( kf, &len, NULL );
246 g_key_file_free(kf);
247
248 g_file_set_contents(user_config_file, buf, len, NULL);
249 g_free(buf);
250 g_free(user_config_file);
15c155a6
HJYP
251}
252
253static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
254{
255 switch(res)
256 {
257 case GTK_RESPONSE_APPLY:
258
259 if(app.use_lxsession)
260 lxappearance_save_lxsession();
261 else
262 lxappearance_save_gtkrc();
263
ae63f687
HJYP
264 reload_all_programs();
265
15c155a6
HJYP
266 app.changed = FALSE;
267 gtk_dialog_set_response_sensitive(app.dlg, GTK_RESPONSE_APPLY, FALSE);
268 break;
269 case 1: /* about dialog */
270 {
271 GtkBuilder* b = gtk_builder_new();
272 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
273 {
274 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
275 gtk_dialog_run(dlg);
276 gtk_widget_destroy(dlg);
277 }
278 g_object_unref(b);
279 }
280 break;
281 default:
282 gtk_main_quit();
283 }
284}
285
286static void settings_init()
287{
e597424e
HJYP
288 GtkSettings* settings = gtk_settings_get_default();
289 g_object_get(settings,
290 "gtk-theme-name", &app.widget_theme,
ae63f687 291 "gtk-font-name", &app.default_font,
e597424e
HJYP
292 "gtk-icon-theme-name", &app.icon_theme,
293 "gtk-cursor-theme-name", &app.cursor_theme,
4e409096 294 "gtk-cursor-theme-size", &app.cursor_theme_size,
e597424e
HJYP
295 "gtk-toolbar-style", &app.toolbar_style,
296 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
6b54b65a
HJYP
297 "gtk-button-images", &app.button_images,
298 "gtk-menu-images", &app.menu_images,
9bb12c12
HJYP
299#if GTK_CHECK_VERSION(2, 14, 0)
300 "gtk-enable-event-sounds", &app.enable_event_sound,
301 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
302#endif
e597424e 303 NULL);
15c155a6 304 /* try to figure out cursor theme used. */
15c155a6
HJYP
305 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
306 {
307 /* get the real theme name from default. */
308 GKeyFile* kf = g_key_file_new();
309 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
310 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
311 g_free(fpath);
312
313 if(!ret)
314 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
315
316 if(ret)
317 {
e597424e 318 g_free(app.cursor_theme);
15c155a6
HJYP
319 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
320 g_debug("cursor theme name: %s", app.cursor_theme);
321 }
322 g_key_file_free(kf);
323 }
c92e9c11
HJYP
324
325 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
326 /* try to load custom color scheme if available */
327 if(app.use_lxsession)
328 {
329 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
330 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
331 GKeyFile* kf = g_key_file_new();
332 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
333 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
334 else if(g_key_file_load_from_dirs(kf, rel_path, g_get_system_config_dirs(), NULL, 0, NULL))
335 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
336 g_key_file_free(kf);
337 g_free(rel_path);
338 g_free(user_config_file);
339
340 if(app.color_scheme)
341 {
342 if(*app.color_scheme)
343 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
344 else
345 {
346 g_free(app.color_scheme);
347 app.color_scheme = NULL;
348 }
349 }
350 }
351 else
352 {
353 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
354 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
355 g_free(gtkrc_file);
356 if(g_hash_table_size(app.color_scheme_hash) > 0)
357 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
358 }
15c155a6
HJYP
359}
360
0cd84ed6
HJYP
361int main(int argc, char** argv)
362{
363 GError* err = NULL;
364 GtkBuilder* b;
0cd84ed6
HJYP
365
366 /* gettext support */
367#ifdef ENABLE_NLS
368 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
369 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
370 textdomain ( GETTEXT_PACKAGE );
371#endif
372
0cd84ed6
HJYP
373 /* initialize GTK+ and parse the command line arguments */
374 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
375 {
376 g_print( "Error: %s\n", err->message );
377 return 1;
378 }
379
15c155a6
HJYP
380 /* check if we're under LXSession */
381 check_lxsession();
382
0cd84ed6
HJYP
383 /* create GUI here */
384 b = gtk_builder_new();
385 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
386 return 1;
387
6b54b65a
HJYP
388 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
389 * Some properties of GtkSettings class are installed by some other gtk classes.
390 * For example, "gtk-menu-images" property is actually installed by initialization
391 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
392 * are created, this property gets correctly registered. So later it can be read. */
393
394 /* load config values */
395 settings_init();
396
e597424e
HJYP
397 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
398
0cd84ed6 399 widget_theme_init(b);
71ce58a7 400 color_scheme_init(b);
0cd84ed6
HJYP
401 icon_theme_init(b);
402 cursor_theme_init(b);
e597424e 403 other_init(b);
0cd84ed6 404
756f0a66
HJYP
405 plugins_init(b);
406
407 /* the page for window manager plugins */
408 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
409
15c155a6 410 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
0cd84ed6 411
15c155a6 412 gtk_window_present(GTK_WINDOW(app.dlg));
0cd84ed6
HJYP
413 g_object_unref(b);
414
415 gtk_main();
416
756f0a66
HJYP
417 plugins_finalize();
418
0cd84ed6
HJYP
419 return 0;
420}
15c155a6
HJYP
421
422void lxappearance_changed()
423{
424 if(!app.changed)
425 {
426 app.changed = TRUE;
427 gtk_dialog_set_response_sensitive(app.dlg, GTK_RESPONSE_APPLY, TRUE);
428 }
429}