Bump version number.
[lxde/lxappearance.git] / src / lxappearance2.c
CommitLineData
0cd84ed6
HJYP
1/*
2 * lxappearance2.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
15c155a6
HJYP
26#include "lxappearance2.h"
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
HJYP
37#include "widget-theme.h"
38#include "icon-theme.h"
39#include "cursor-theme.h"
e597424e 40#include "other.h"
0cd84ed6 41
15c155a6
HJYP
42LXAppearance app = {0};
43
44Atom lxsession_atom = 0;
ae63f687 45static const char* lxsession_name = NULL;
15c155a6
HJYP
46
47static void check_lxsession()
48{
49 lxsession_atom = XInternAtom( GDK_DISPLAY(), "_LXSESSION", True );
50 if( lxsession_atom != None )
51 {
52 XGrabServer( GDK_DISPLAY() );
53 if( XGetSelectionOwner( GDK_DISPLAY(), lxsession_atom ) )
ae63f687 54 {
15c155a6 55 app.use_lxsession = TRUE;
ae63f687
HJYP
56 lxsession_name = g_getenv("DESKTOP_SESSION");
57 }
15c155a6
HJYP
58 XUngrabServer( GDK_DISPLAY() );
59 }
60}
61
0cd84ed6
HJYP
62static GOptionEntry option_entries[] =
63{
64 { NULL }
65};
66
ae63f687
HJYP
67static void save_cursor_theme_name()
68{
69 char* dir_path;
70 if(!app.cursor_theme || !g_strcmp0(app.cursor_theme, "default"))
71 return;
72
73 dir_path = g_build_filename(g_get_home_dir(), ".icons/default", NULL);
74 if(0 == g_mkdir_with_parents(dir_path, 0700))
75 {
76 char* index_theme = g_build_filename(dir_path, "index.theme", NULL);
77 char* content = g_strdup_printf(
78 "# This file is written by LXAppearance. Do not edit."
79 "[Icon Theme]\n"
80 "Name=Default\n"
81 "Comment=Default Cursor Theme\n"
82 "Inherits=%s\n", app.cursor_theme);
83 g_file_set_contents(index_theme, content, -1, NULL);
84 g_free(content);
85 g_free(index_theme);
86 }
87 g_free(dir_path);
88
89 /*
90 dir_path = g_build_filename(g_get_home_dir(), ".Xdefaults", NULL);
91 Xcursor.theme: name
92 Xcursor.size: [size]
93 g_file_set_contents(dir_path, "", -1, NULL);
94 g_free(dir_path);
95 */
96}
97
98static void reload_all_programs()
15c155a6 99{
ae63f687
HJYP
100 GdkEventClient event;
101 event.type = GDK_CLIENT_EVENT;
102 event.send_event = TRUE;
103 event.window = NULL;
15c155a6 104
ae63f687
HJYP
105 if( app.use_lxsession )
106 {
107 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
108 event.data.b[0] = 0; /* LXS_RELOAD */
109 }
110 else
111 {
112 /* if( icon_only )
113 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
114 */
115 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
116 }
117 event.data_format = 8;
118 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
119}
120
121static void lxappearance_save_gtkrc()
122{
123 static const char* tb_styles[]={
124 "GTK_TOOLBAR_ICONS",
125 "GTK_TOOLBAR_TEXT",
126 "GTK_TOOLBAR_BOTH",
127 "GTK_TOOLBAR_BOTH_HORIZ"
128 };
129 static const char* tb_icon_sizes[]={
130 "GTK_ICON_SIZE_INVALID",
131 "GTK_ICON_SIZE_MENU",
132 "GTK_ICON_SIZE_SMALL_TOOLBAR",
133 "GTK_ICON_SIZE_LARGE_TOOLBAR",
134 "GTK_ICON_SIZE_BUTTON",
135 "GTK_ICON_SIZE_DND",
136 "GTK_ICON_SIZE_DIALOG"
137 };
138
139 char* file_path = g_build_filename(g_get_home_dir(), "~/.gtkrc-2.0", NULL);
140 char* content = g_strdup_printf(
141 "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
142 "# Any customization should be done in ~/.gtkrc-2.0.mine instead.\n\n"
143 "gtk-theme-name=\"%s\"\n"
144 "gtk-icon-theme-name=\"%s\"\n"
145 "gtk-font-name=\"%s\"\n"
146 "gtk-toolbar-style=%s\n"
147 "gtk-toolbar-icon-size=%s\n"
148 "gtk-cursor-theme-name=\"%s\"\n"
149 "gtk-cursor-theme-size=%d\n"
150 "include \"%s/.gtkrc-2.0.mine\"\n",
151 app.widget_theme,
152 app.icon_theme,
153 app.default_font,
154 tb_styles[app.toolbar_style],
155 tb_icon_sizes[app.toolbar_icon_size],
156 app.cursor_theme,
157 app.cursor_theme_size,
158 g_get_home_dir());
159
160 g_file_set_contents(file_path, content, -1, NULL);
161 g_free(content);
162 g_free(file_path);
15c155a6
HJYP
163}
164
165static void lxappearance_save_lxsession()
166{
ae63f687
HJYP
167 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
168 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
169 char* buf;
170 int len;
171 GKeyFile* kf = g_key_file_new();
172
173 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
174 {
175 /* the user config file doesn't exist, create its parent dir */
176 len = strlen(user_config_file) - strlen("/desktop.conf");
177 user_config_file[len] = '\0';
178 g_debug("user_config_file = %s", user_config_file);
179 g_mkdir_with_parents(user_config_file, 0700);
180 user_config_file[len] = '/';
181
182 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);
183 }
184
185 g_free(rel_path);
15c155a6 186
ae63f687
HJYP
187 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
188 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
189
190 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
191
192 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
193 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
194 save_cursor_theme_name();
195
196 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
197 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
198
199 buf = g_key_file_to_data( kf, &len, NULL );
200 g_key_file_free(kf);
201
202 g_file_set_contents(user_config_file, buf, len, NULL);
203 g_free(buf);
204 g_free(user_config_file);
15c155a6
HJYP
205}
206
207static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
208{
209 switch(res)
210 {
211 case GTK_RESPONSE_APPLY:
212
213 if(app.use_lxsession)
214 lxappearance_save_lxsession();
215 else
216 lxappearance_save_gtkrc();
217
ae63f687
HJYP
218 reload_all_programs();
219
15c155a6
HJYP
220 app.changed = FALSE;
221 gtk_dialog_set_response_sensitive(app.dlg, GTK_RESPONSE_APPLY, FALSE);
222 break;
223 case 1: /* about dialog */
224 {
225 GtkBuilder* b = gtk_builder_new();
226 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
227 {
228 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
229 gtk_dialog_run(dlg);
230 gtk_widget_destroy(dlg);
231 }
232 g_object_unref(b);
233 }
234 break;
235 default:
236 gtk_main_quit();
237 }
238}
239
240static void settings_init()
241{
e597424e
HJYP
242 GtkSettings* settings = gtk_settings_get_default();
243 g_object_get(settings,
244 "gtk-theme-name", &app.widget_theme,
ae63f687 245 "gtk-font-name", &app.default_font,
e597424e
HJYP
246 "gtk-icon-theme-name", &app.icon_theme,
247 "gtk-cursor-theme-name", &app.cursor_theme,
4e409096 248 "gtk-cursor-theme-size", &app.cursor_theme_size,
e597424e
HJYP
249 "gtk-toolbar-style", &app.toolbar_style,
250 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
251 NULL);
15c155a6 252 /* try to figure out cursor theme used. */
15c155a6
HJYP
253 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
254 {
255 /* get the real theme name from default. */
256 GKeyFile* kf = g_key_file_new();
257 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
258 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
259 g_free(fpath);
260
261 if(!ret)
262 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
263
264 if(ret)
265 {
e597424e 266 g_free(app.cursor_theme);
15c155a6
HJYP
267 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
268 g_debug("cursor theme name: %s", app.cursor_theme);
269 }
270 g_key_file_free(kf);
271 }
272}
273
0cd84ed6
HJYP
274int main(int argc, char** argv)
275{
276 GError* err = NULL;
277 GtkBuilder* b;
0cd84ed6
HJYP
278
279 /* gettext support */
280#ifdef ENABLE_NLS
281 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
282 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
283 textdomain ( GETTEXT_PACKAGE );
284#endif
285
0cd84ed6
HJYP
286 /* initialize GTK+ and parse the command line arguments */
287 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
288 {
289 g_print( "Error: %s\n", err->message );
290 return 1;
291 }
292
15c155a6
HJYP
293 /* check if we're under LXSession */
294 check_lxsession();
295
296 /* load config values */
297 settings_init();
298
0cd84ed6
HJYP
299 /* create GUI here */
300 b = gtk_builder_new();
301 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
302 return 1;
303
e597424e
HJYP
304 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
305
0cd84ed6
HJYP
306 widget_theme_init(b);
307 icon_theme_init(b);
308 cursor_theme_init(b);
e597424e 309 other_init(b);
0cd84ed6 310
15c155a6 311 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
0cd84ed6 312
15c155a6 313 gtk_window_present(GTK_WINDOW(app.dlg));
0cd84ed6
HJYP
314 g_object_unref(b);
315
316 gtk_main();
317
318 return 0;
319}
15c155a6
HJYP
320
321void lxappearance_changed()
322{
323 if(!app.changed)
324 {
325 app.changed = TRUE;
326 gtk_dialog_set_response_sensitive(app.dlg, GTK_RESPONSE_APPLY, TRUE);
327 }
328}