Releasing debian version 0.5.3-1.
[debian/lxappearance.git] / src / lxappearance.c
CommitLineData
1469f737
DB
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
7f605299
DB
37#if ENABLE_DBUS
38#include <dbus/dbus.h>
39#endif
40
1469f737
DB
41#include "widget-theme.h"
42#include "color-scheme.h"
43#include "icon-theme.h"
44#include "cursor-theme.h"
e7f9874d 45#include "font.h"
1469f737
DB
46#include "other.h"
47#include "plugin.h"
48
49LXAppearance app = {0};
50
51Atom lxsession_atom = 0;
52static const char* lxsession_name = NULL;
53
7f605299
DB
54/* Dbus functions Copy from lxsession-logout
55 TODO Create a library fro this ?
56*/
57
58static gboolean check_lxde_dbus()
59{
60#if ENABLE_DBUS
61 DBusError error;
62 dbus_error_init(&error);
63 DBusConnection * connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
64 if (connection == NULL)
65 {
66 g_warning(G_STRLOC ": Failed to connect to the session message bus: %s", error.message);
67 dbus_error_free(&error);
68 return FALSE;
69 }
70
ae1f8f29 71 dbus_bool_t ret = dbus_bus_name_has_owner(connection,"org.lxde.SessionManager",NULL);
7f605299 72
ae1f8f29 73 if (ret == TRUE)
7f605299
DB
74 {
75 return TRUE;
76 }
77 else
78 {
79 return FALSE;
80 }
81#else
82 return FALSE;
83#endif
84}
85
1469f737
DB
86static void check_lxsession()
87{
e7f9874d 88 lxsession_atom = XInternAtom( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), "_LXSESSION", True );
1469f737
DB
89 if( lxsession_atom != None )
90 {
e7f9874d
DB
91 XGrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
92 if( XGetSelectionOwner( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), lxsession_atom ) )
1469f737
DB
93 {
94 app.use_lxsession = TRUE;
95 lxsession_name = g_getenv("DESKTOP_SESSION");
96 }
e7f9874d 97 XUngrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
1469f737 98 }
7f605299
DB
99
100 /* Check Lxsession also with dbus */
101 if (check_lxde_dbus())
102 {
103 app.use_lxsession = TRUE;
104 lxsession_name = g_getenv("DESKTOP_SESSION");
105 }
106
1469f737
DB
107}
108
109static GOptionEntry option_entries[] =
110{
111 { NULL }
112};
113
114static void save_cursor_theme_name()
115{
116 char* dir_path;
117 if(!app.cursor_theme || !g_strcmp0(app.cursor_theme, "default"))
118 return;
119
120 dir_path = g_build_filename(g_get_home_dir(), ".icons/default", NULL);
121 if(0 == g_mkdir_with_parents(dir_path, 0700))
122 {
123 char* index_theme = g_build_filename(dir_path, "index.theme", NULL);
124 char* content = g_strdup_printf(
125 "# This file is written by LXAppearance. Do not edit."
126 "[Icon Theme]\n"
127 "Name=Default\n"
128 "Comment=Default Cursor Theme\n"
129 "Inherits=%s\n", app.cursor_theme ? app.cursor_theme : "");
130 g_file_set_contents(index_theme, content, -1, NULL);
131 g_free(content);
132 g_free(index_theme);
133 }
134 g_free(dir_path);
135
136 /*
137 dir_path = g_build_filename(g_get_home_dir(), ".Xdefaults", NULL);
138 Xcursor.theme: name
139 Xcursor.size: [size]
140 g_file_set_contents(dir_path, "", -1, NULL);
141 g_free(dir_path);
142 */
143}
144
145static void reload_all_programs()
146{
e7f9874d
DB
147#if GTK_CHECK_VERSION(3, 0, 0)
148
149/* TODO Port this to something else than gdk_event_send_clientmessage_toall */
150
151#else
1469f737
DB
152 GdkEventClient event;
153 event.type = GDK_CLIENT_EVENT;
154 event.send_event = TRUE;
155 event.window = NULL;
156
157 if( app.use_lxsession )
158 {
159 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
160 event.data.b[0] = 0; /* LXS_RELOAD */
161 }
162 else
163 {
164 /* if( icon_only )
165 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
166 */
167 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
168 }
169 event.data_format = 8;
170 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
e7f9874d 171#endif
1469f737
DB
172}
173
174static const char* bool2str(gboolean val)
175{
176 return val ? "TRUE" : "FALSE";
177}
178
e7f9874d 179
1469f737
DB
180static void lxappearance_save_gtkrc()
181{
182 static const char* tb_styles[]={
183 "GTK_TOOLBAR_ICONS",
184 "GTK_TOOLBAR_TEXT",
185 "GTK_TOOLBAR_BOTH",
186 "GTK_TOOLBAR_BOTH_HORIZ"
187 };
188 static const char* tb_icon_sizes[]={
189 "GTK_ICON_SIZE_INVALID",
190 "GTK_ICON_SIZE_MENU",
191 "GTK_ICON_SIZE_SMALL_TOOLBAR",
192 "GTK_ICON_SIZE_LARGE_TOOLBAR",
193 "GTK_ICON_SIZE_BUTTON",
194 "GTK_ICON_SIZE_DND",
195 "GTK_ICON_SIZE_DIALOG"
196 };
197
198 char* file_path = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
199 GString* content = g_string_sized_new(512);
200 g_string_append(content,
201 "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
202 "# Any customization should be done in ~/.gtkrc-2.0.mine instead.\n\n");
203 if(app.widget_theme)
204 g_string_append_printf(content,
205 "gtk-theme-name=\"%s\"\n", app.widget_theme);
206 if(app.icon_theme)
207 g_string_append_printf(content,
208 "gtk-icon-theme-name=\"%s\"\n", app.icon_theme);
209 if(app.default_font)
210 g_string_append_printf(content,
211 "gtk-font-name=\"%s\"\n", app.default_font);
212 if(app.cursor_theme)
213 g_string_append_printf(content,
214 "gtk-cursor-theme-name=\"%s\"\n", app.cursor_theme);
215 save_cursor_theme_name();
216
217 g_string_append_printf(content,
218 "gtk-cursor-theme-size=%d\n"
219 "gtk-toolbar-style=%s\n"
220 "gtk-toolbar-icon-size=%s\n"
221 "gtk-button-images=%d\n"
222 "gtk-menu-images=%d\n"
223#if GTK_CHECK_VERSION(2, 14, 0)
224 "gtk-enable-event-sounds=%d\n"
225 "gtk-enable-input-feedback-sounds=%d\n"
226#endif
e7f9874d
DB
227 "gtk-xft-antialias=%d\n"
228 "gtk-xft-hinting=%d\n"
229
1469f737
DB
230 , app.cursor_theme_size,
231 tb_styles[app.toolbar_style],
232 tb_icon_sizes[app.toolbar_icon_size],
233 app.button_images ? 1 : 0,
234 app.menu_images ? 1 : 0,
235#if GTK_CHECK_VERSION(2, 14, 0)
236 app.enable_event_sound ? 1 : 0,
e7f9874d 237 app.enable_input_feedback ? 1 : 0,
1469f737 238#endif
e7f9874d
DB
239 app.enable_antialising ? 1 : 0,
240 app.enable_hinting ? 1 : 0
1469f737
DB
241 );
242
e7f9874d
DB
243 if(app.hinting_style)
244 g_string_append_printf(content,
245 "gtk-xft-hintstyle=\"%s\"\n", app.hinting_style);
246
247 if(app.font_rgba)
248 g_string_append_printf(content,
249 "gtk-xft-rgba=\"%s\"\n", app.font_rgba);
250
1469f737
DB
251 if(app.color_scheme)
252 {
253 char* escaped = g_strescape(app.color_scheme, NULL);
254 g_string_append_printf(content,
e7f9874d 255 "gtk-color-scheme=\"%s\"\n",
1469f737
DB
256 escaped);
257 g_free(escaped);
258 }
259
260 g_string_append_printf(content,
261 "include \"%s/.gtkrc-2.0.mine\"\n",
262 g_get_home_dir());
263
264 g_file_set_contents(file_path, content->str, content->len, NULL);
e7f9874d 265
7f605299
DB
266 /* Save also in GTK3 folder
267 Content shold be different from the gtk2 one
268 */
269 GString* content_gtk3 = g_string_sized_new(512);
270 char* file_path_gtk3 = g_build_filename(g_get_user_config_dir(), "gtk-3.0", NULL);
271 char* file_path_settings = g_build_filename(g_get_user_config_dir(), "gtk-3.0", "settings.ini", NULL);
e7f9874d
DB
272
273 if (!g_file_test(file_path_gtk3, G_FILE_TEST_IS_DIR))
274 {
275 g_mkdir_with_parents(file_path_gtk3, 0755);
276 }
277
7f605299
DB
278
279 g_string_append(content_gtk3, "[Settings] \n");
280
281 if(app.widget_theme)
282 g_string_append_printf(content_gtk3,
283 "gtk-theme-name=%s\n", app.widget_theme);
284 if(app.icon_theme)
285 g_string_append_printf(content_gtk3,
286 "gtk-icon-theme-name=%s\n", app.icon_theme);
287 if(app.default_font)
288 g_string_append_printf(content_gtk3,
289 "gtk-font-name=%s\n", app.default_font);
290 if(app.cursor_theme)
291 g_string_append_printf(content_gtk3,
292 "gtk-cursor-theme-name=%s\n", app.cursor_theme);
293 save_cursor_theme_name();
294
295 g_string_append_printf(content_gtk3,
296 "gtk-cursor-theme-size=%d\n"
297 "gtk-toolbar-style=%s\n"
298 "gtk-toolbar-icon-size=%s\n"
299 "gtk-button-images=%d\n"
300 "gtk-menu-images=%d\n"
301#if GTK_CHECK_VERSION(2, 14, 0)
302 "gtk-enable-event-sounds=%d\n"
303 "gtk-enable-input-feedback-sounds=%d\n"
304#endif
305 "gtk-xft-antialias=%d\n"
306 "gtk-xft-hinting=%d\n"
307
308 , app.cursor_theme_size,
309 tb_styles[app.toolbar_style],
310 tb_icon_sizes[app.toolbar_icon_size],
311 app.button_images ? 1 : 0,
312 app.menu_images ? 1 : 0,
313#if GTK_CHECK_VERSION(2, 14, 0)
314 app.enable_event_sound ? 1 : 0,
315 app.enable_input_feedback ? 1 : 0,
316#endif
317 app.enable_antialising ? 1 : 0,
318 app.enable_hinting ? 1 : 0
319 );
320
321 if(app.hinting_style)
322 g_string_append_printf(content_gtk3,
323 "gtk-xft-hintstyle=%s\n", app.hinting_style);
324
325 if(app.font_rgba)
326 g_string_append_printf(content_gtk3,
327 "gtk-xft-rgba=%s\n", app.font_rgba);
328
329 if(app.color_scheme)
330 {
331 char* escaped = g_strescape(app.color_scheme, NULL);
332 g_string_append_printf(content_gtk3,
333 "gtk-color-scheme=%s\n",
334 escaped);
335 g_free(escaped);
336 }
337
338 g_file_set_contents(file_path_settings, content_gtk3->str, content_gtk3->len, NULL);
e7f9874d 339
1469f737 340 g_string_free(content, TRUE);
7f605299 341 g_string_free(content_gtk3, TRUE);
1469f737
DB
342 g_free(file_path);
343}
344
345static void lxappearance_save_lxsession()
346{
347 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
348 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
349 char* buf;
350 int len;
351 GKeyFile* kf = g_key_file_new();
352
353 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
354 {
355 /* the user config file doesn't exist, create its parent dir */
356 len = strlen(user_config_file) - strlen("/desktop.conf");
357 user_config_file[len] = '\0';
358 g_debug("user_config_file = %s", user_config_file);
359 g_mkdir_with_parents(user_config_file, 0700);
360 user_config_file[len] = '/';
361
362 g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL,
363 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
364 }
365
366 g_free(rel_path);
367
368 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
369 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
370
371 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
372
373 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
374
375 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
376 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
377 save_cursor_theme_name();
378
379 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
380 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
381
382 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
383 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
384
385 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
386 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
387
388#if GTK_CHECK_VERSION(2, 14, 0)
389 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
390 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
391 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
392#endif
e7f9874d
DB
393 g_key_file_set_integer( kf, "GTK", "iXft/Antialias", app.enable_antialising);
394 g_key_file_set_integer( kf, "GTK", "iXft/Hinting", app.enable_hinting);
395 g_key_file_set_string( kf, "GTK", "sXft/HintStyle", app.hinting_style);
396 g_key_file_set_string( kf, "GTK", "sXft/RGBA", app.font_rgba);
1469f737
DB
397
398 buf = g_key_file_to_data( kf, &len, NULL );
399 g_key_file_free(kf);
400
401 g_file_set_contents(user_config_file, buf, len, NULL);
402 g_free(buf);
403 g_free(user_config_file);
404}
405
406static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
407{
408 switch(res)
409 {
410 case GTK_RESPONSE_APPLY:
411
412 if(app.use_lxsession)
413 lxappearance_save_lxsession();
414 else
415 lxappearance_save_gtkrc();
416
417 reload_all_programs();
418
419 app.changed = FALSE;
420 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, FALSE);
421 break;
422 case 1: /* about dialog */
423 {
424 GtkBuilder* b = gtk_builder_new();
425 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
426 {
427 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
428 gtk_dialog_run(GTK_DIALOG(dlg));
429 gtk_widget_destroy(dlg);
430 }
431 g_object_unref(b);
432 }
433 break;
434 default:
435 gtk_main_quit();
436 }
437}
438
439static void settings_init()
440{
441 GtkSettings* settings = gtk_settings_get_default();
442 g_object_get(settings,
443 "gtk-theme-name", &app.widget_theme,
444 "gtk-font-name", &app.default_font,
445 "gtk-icon-theme-name", &app.icon_theme,
446 "gtk-cursor-theme-name", &app.cursor_theme,
447 "gtk-cursor-theme-size", &app.cursor_theme_size,
448 "gtk-toolbar-style", &app.toolbar_style,
449 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
450 "gtk-button-images", &app.button_images,
451 "gtk-menu-images", &app.menu_images,
452#if GTK_CHECK_VERSION(2, 14, 0)
453 "gtk-enable-event-sounds", &app.enable_event_sound,
454 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
455#endif
e7f9874d
DB
456 "gtk-xft-antialias", &app.enable_antialising,
457 "gtk-xft-hinting", &app.enable_hinting,
458 "gtk-xft-hintstyle", &app.hinting_style,
459 "gtk-xft-rgba", &app.font_rgba,
1469f737
DB
460 NULL);
461 /* try to figure out cursor theme used. */
462 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
463 {
464 /* get the real theme name from default. */
465 GKeyFile* kf = g_key_file_new();
466 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
467 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
468 g_free(fpath);
469
470 if(!ret)
471 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
472
473 if(ret)
474 {
475 g_free(app.cursor_theme);
476 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
477 g_debug("cursor theme name: %s", app.cursor_theme);
478 }
479 g_key_file_free(kf);
480 }
481
482 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
483 /* try to load custom color scheme if available */
484 if(app.use_lxsession)
485 {
486 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
487 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
488 GKeyFile* kf = g_key_file_new();
489 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
490 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
491 else if(g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL, 0, NULL))
492 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
493 g_key_file_free(kf);
494 g_free(rel_path);
495 g_free(user_config_file);
496
497 if(app.color_scheme)
498 {
499 if(*app.color_scheme)
500 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
501 else
502 {
503 g_free(app.color_scheme);
504 app.color_scheme = NULL;
505 }
506 }
507 }
508 else
509 {
510 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
511 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
512 g_free(gtkrc_file);
513 if(g_hash_table_size(app.color_scheme_hash) > 0)
514 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
515 }
516}
517
518int main(int argc, char** argv)
519{
520 GError* err = NULL;
521 GtkBuilder* b;
522
523 /* gettext support */
524#ifdef ENABLE_NLS
525 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
526 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
527 textdomain ( GETTEXT_PACKAGE );
528#endif
529
530 g_thread_init(NULL);
531 /* initialize GTK+ and parse the command line arguments */
532 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
533 {
534 g_print( "Error: %s\n", err->message );
535 return 1;
536 }
537
538 app.abi_version = LXAPPEARANCE_ABI_VERSION;
539
540 /* check if we're under LXSession */
541 check_lxsession();
542
543 /* create GUI here */
544 b = gtk_builder_new();
545 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
546 return 1;
547
548 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
549 * Some properties of GtkSettings class are installed by some other gtk classes.
550 * For example, "gtk-menu-images" property is actually installed by initialization
551 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
552 * are created, this property gets correctly registered. So later it can be read. */
553
554 /* load config values */
555 settings_init();
556
557 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
558
559 widget_theme_init(b);
560 color_scheme_init(b);
561 icon_theme_init(b);
562 cursor_theme_init(b);
e7f9874d 563 font_init(b);
1469f737
DB
564 other_init(b);
565 /* the page for window manager plugins */
566 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
567
568 plugins_init(b);
569
570 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
571
572 gtk_window_present(GTK_WINDOW(app.dlg));
573 g_object_unref(b);
574
575 gtk_main();
576
577 plugins_finalize();
578
579 return 0;
580}
581
582void lxappearance_changed()
583{
584 if(!app.changed)
585 {
586 app.changed = TRUE;
587 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, TRUE);
588 }
589}
e7f9874d
DB
590
591void on_check_button_toggled(GtkToggleButton* btn, gpointer user_data)
592{
593 gboolean* val = (gboolean*)user_data;
594 gboolean new_val = gtk_toggle_button_get_active(btn);
595 if(new_val != *val)
596 {
597 *val = new_val;
598 lxappearance_changed();
599 }
600}