Eanble plugin support.
[lxde/lxappearance.git] / src / lxappearance.c
1 /*
2 * lxappearance.c
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
26 #include "lxappearance.h"
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30
31 #include <X11/X.h>
32 #include <X11/Xatom.h>
33 #include <X11/Xlib.h>
34 #include <gdk/gdkx.h>
35 #include <string.h>
36
37 #include "widget-theme.h"
38 #include "color-scheme.h"
39 #include "icon-theme.h"
40 #include "cursor-theme.h"
41 #include "other.h"
42 #include "plugin.h"
43
44 LXAppearance app = {0};
45
46 Atom lxsession_atom = 0;
47 static const char* lxsession_name = NULL;
48
49 static 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 ) )
56 {
57 app.use_lxsession = TRUE;
58 lxsession_name = g_getenv("DESKTOP_SESSION");
59 }
60 XUngrabServer( GDK_DISPLAY() );
61 }
62 }
63
64 static GOptionEntry option_entries[] =
65 {
66 { NULL }
67 };
68
69 static 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
100 static void reload_all_programs()
101 {
102 GdkEventClient event;
103 event.type = GDK_CLIENT_EVENT;
104 event.send_event = TRUE;
105 event.window = NULL;
106
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
123 static const char* bool2str(gboolean val)
124 {
125 return val ? "TRUE" : "FALSE";
126 }
127
128 static 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
146 char* file_path = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
147 GString* content = g_string_sized_new(512);
148 g_string_append_printf(content,
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"
158 "gtk-button-images=%s\n"
159 "gtk-menu-images=%s\n"
160 #if GTK_CHECK_VERSION(2, 14, 0)
161 "gtk-enable-event-sounds=%s\n"
162 "gtk-enable-input-feedback-sounds=%s\n",
163 #endif
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,
171 bool2str(app.button_images),
172 bool2str(app.menu_images),
173 #if GTK_CHECK_VERSION(2, 14, 0)
174 bool2str(app.enable_event_sound),
175 bool2str(app.enable_input_feedback)
176 #endif
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",
190 g_get_home_dir());
191
192 g_file_set_contents(file_path, content->str, content->len, NULL);
193 g_string_free(content, TRUE);
194 g_free(file_path);
195 }
196
197 static void lxappearance_save_lxsession()
198 {
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);
218
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
222 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
223
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
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
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)
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
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);
251 }
252
253 static 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
264 reload_all_programs();
265
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
286 static void settings_init()
287 {
288 GtkSettings* settings = gtk_settings_get_default();
289 g_object_get(settings,
290 "gtk-theme-name", &app.widget_theme,
291 "gtk-font-name", &app.default_font,
292 "gtk-icon-theme-name", &app.icon_theme,
293 "gtk-cursor-theme-name", &app.cursor_theme,
294 "gtk-cursor-theme-size", &app.cursor_theme_size,
295 "gtk-toolbar-style", &app.toolbar_style,
296 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
297 "gtk-button-images", &app.button_images,
298 "gtk-menu-images", &app.menu_images,
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
303 NULL);
304 /* try to figure out cursor theme used. */
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 {
318 g_free(app.cursor_theme);
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 }
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 }
359 }
360
361 int main(int argc, char** argv)
362 {
363 GError* err = NULL;
364 GtkBuilder* b;
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
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
380 /* check if we're under LXSession */
381 check_lxsession();
382
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
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
397 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
398
399 widget_theme_init(b);
400 color_scheme_init(b);
401 icon_theme_init(b);
402 cursor_theme_init(b);
403 other_init(b);
404
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
410 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
411
412 gtk_window_present(GTK_WINDOW(app.dlg));
413 g_object_unref(b);
414
415 gtk_main();
416
417 plugins_finalize();
418
419 return 0;
420 }
421
422 void 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 }