]> icculus.org git repositories - dana/obconf.git/blob - src/main.c
show an error when the rc config file cannot be parsed, and refuse to load
[dana/obconf.git] / src / main.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    main.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2003-2008   Dana Jansens
5    Copyright (c) 2003        Tim Riley
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "main.h"
21 #include "archive.h"
22 #include "theme.h"
23 #include "appearance.h"
24 #include "windows.h"
25 #include "margins.h"
26 #include "mouse.h"
27 #include "desktops.h"
28 #include "dock.h"
29 #include "preview_update.h"
30 #include "gettext.h"
31
32 #include <gdk/gdkx.h>
33 #define SN_API_NOT_YET_FROZEN
34 #include <libsn/sn.h>
35 #undef SN_API_NOT_YET_FROZEN
36 #include <stdlib.h>
37
38 GtkWidget *mainwin = NULL;
39
40 GladeXML *glade;
41 xmlDocPtr doc;
42 xmlNodePtr root;
43 RrInstance *rrinst;
44 gchar *obc_config_file = NULL;
45
46 static gchar *obc_theme_install = NULL;
47 static gchar *obc_theme_archive = NULL;
48
49 void obconf_error(gchar *msg, gboolean modal)
50 {
51     GtkWidget *d;
52
53     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
54                                GTK_DIALOG_DESTROY_WITH_PARENT,
55                                GTK_MESSAGE_ERROR,
56                                GTK_BUTTONS_CLOSE,
57                                "%s", msg);
58     gtk_window_set_title(GTK_WINDOW(d), "ObConf Error");
59     if (modal)
60         gtk_dialog_run(GTK_DIALOG(d));
61     else {
62         g_signal_connect_swapped(GTK_OBJECT(d), "response",
63                                  G_CALLBACK(gtk_widget_destroy),
64                                  GTK_OBJECT(d));
65         gtk_widget_show(d);
66     }
67 }
68
69 static void print_version()
70 {
71     g_print("ObConf %s\n", PACKAGE_VERSION);
72     g_print(_("Copyright (c)"));
73     g_print(" 2003-2008   Dana Jansens\n");
74     g_print(_("Copyright (c)"));
75     g_print(" 2003        Tim Riley\n");
76     g_print(_("Copyright (c)"));
77     g_print(" 2007        Javeed Shaikh\n\n");
78     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
79     g_print("This is free software, and you are welcome to redistribute it\n");
80     g_print("under certain conditions. See the file COPYING for details.\n\n");
81
82     exit(EXIT_SUCCESS);
83 }
84
85 static void print_help()
86 {
87     g_print(_("Syntax: obconf [options] [ARCHIVE.obt]\n"));
88     g_print(_("\nOptions:\n"));
89     g_print(_("  --help                Display this help and exit\n"));
90     g_print(_("  --version             Display the version and exit\n"));
91     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
92     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
93     g_print(_("  --config-file FILE    Specify the path to the config file to use\n"));
94     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
95     
96     exit(EXIT_SUCCESS);
97 }
98
99 static void parse_args(int argc, char **argv)
100 {
101     int i;
102
103     for (i = 1; i < argc; ++i) {
104         if (!strcmp(argv[i], "--help"))
105             print_help();
106         if (!strcmp(argv[i], "--version"))
107             print_version();
108         else if (!strcmp(argv[i], "--install")) {
109             if (i == argc - 1) /* no args left */
110                 g_printerr(_("--install requires an argument\n"));
111             else
112                 obc_theme_install = argv[++i];
113         }
114         else if (!strcmp(argv[i], "--archive")) {
115             if (i == argc - 1) /* no args left */
116                 g_printerr(_("--archive requires an argument\n"));
117             else
118                 obc_theme_archive = argv[++i];
119         }
120         else if (!strcmp(argv[i], "--config-file")) {
121             if (i == argc - 1) /* no args left */
122                 g_printerr(_("--config-file requires an argument\n"));
123             else
124                 obc_config_file = argv[++i];
125         } else
126             obc_theme_install = argv[i];
127     }
128 }
129
130 static gboolean get_all(Window win, Atom prop, Atom type, gint size,
131                         guchar **data, guint *num)
132 {
133     gboolean ret = FALSE;
134     gint res;
135     guchar *xdata = NULL;
136     Atom ret_type;
137     gint ret_size;
138     gulong ret_items, bytes_left;
139
140     res = XGetWindowProperty(GDK_DISPLAY(), win, prop, 0l, G_MAXLONG,
141                              FALSE, type, &ret_type, &ret_size,
142                              &ret_items, &bytes_left, &xdata);
143     if (res == Success) {
144         if (ret_size == size && ret_items > 0) {
145             guint i;
146
147             *data = g_malloc(ret_items * (size / 8));
148             for (i = 0; i < ret_items; ++i)
149                 switch (size) {
150                 case 8:
151                     (*data)[i] = xdata[i];
152                     break;
153                 case 16:
154                     ((guint16*)*data)[i] = ((gushort*)xdata)[i];
155                     break;
156                 case 32:
157                     ((guint32*)*data)[i] = ((gulong*)xdata)[i];
158                     break;
159                 default:
160                     g_assert_not_reached(); /* unhandled size */
161                 }
162             *num = ret_items;
163             ret = TRUE;
164         }
165         XFree(xdata);
166     }
167     return ret;
168 }
169
170 static gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
171 {
172     gchar *raw;
173     gchar *str;
174     guint num;
175
176     if (get_all(win, prop,
177                 gdk_x11_get_xatom_by_name("UTF8_STRING"),
178                 8,(guchar**)&raw, &num))
179     {
180         str = g_strndup(raw, num); /* grab the first string from the list */
181         g_free(raw);
182         if (g_utf8_validate(str, -1, NULL)) {
183             *ret = str;
184             return TRUE;
185         }
186         g_free(str);
187     }
188     return FALSE;
189 }
190
191 int main(int argc, char **argv)
192 {
193     gchar *p;
194     gboolean exit_with_error = FALSE;
195
196     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
197     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
198     textdomain(PACKAGE_NAME);
199
200     gtk_init(&argc, &argv);
201     parse_args(argc, argv);
202
203     if (obc_theme_archive) {
204         archive_create(obc_theme_archive);
205         return;
206     }
207
208     p = g_build_filename(GLADEDIR, "obconf.glade", NULL);
209     glade = glade_xml_new(p, NULL, NULL);
210     g_free(p);
211
212     if (!glade) {
213         obconf_error(_("Failed to load the obconf.glade interface file. You have probably failed to install ObConf properly."), TRUE);
214         exit_with_error = TRUE;
215     }
216
217     parse_paths_startup();
218     rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
219
220     if (!obc_config_file) {
221         gchar *p;
222
223         if (prop_get_string_utf8(GDK_ROOT_WINDOW(),
224                                  gdk_x11_get_xatom_by_name("_OB_CONFIG_FILE"),
225                                  &p))
226         {
227             obc_config_file = g_filename_from_utf8(p, -1, NULL, NULL, NULL);
228             g_free(p);
229         }
230     }
231
232     xmlIndentTreeOutput = 1;
233     if (!parse_load_rc(obc_config_file, &doc, &root)) {
234         obconf_error(_("Failed to load an rc.xml. You have probably failed to install Openbox properly."), TRUE);
235         exit_with_error = TRUE;
236     }
237
238     /* look for parsing errors */
239     {
240         xmlErrorPtr e = xmlGetLastError();
241         if (e) {
242             char *a = g_strdup_printf
243                 (_("Error while parsing the Openbox configuration file.  Your configuration file is not valid XML.\n\nMessage: %s"),
244                  e->message);
245             obconf_error(a, TRUE);
246             g_free(a);
247             exit_with_error = TRUE;
248         }
249     }
250
251     if (!exit_with_error) {
252         glade_xml_signal_autoconnect(glade);
253
254         {
255             gchar *s = g_strdup_printf
256                 ("<span weight=\"bold\" size=\"xx-large\">ObConf %s</span>",
257                  PACKAGE_VERSION);
258             gtk_label_set_markup(GTK_LABEL
259                                  (glade_xml_get_widget(glade, "title_label")),
260                                  s);
261             g_free(s);
262         }
263
264         theme_setup_tab();
265         appearance_setup_tab();
266         windows_setup_tab();
267         moveresize_setup_tab();
268         mouse_setup_tab();
269         desktops_setup_tab();
270         margins_setup_tab();
271         dock_setup_tab();
272
273         mainwin = get_widget("main_window");
274
275         if (obc_theme_install)
276             theme_install(obc_theme_install);
277         else
278             theme_load_all();
279
280         /* the main window is not shown here ! it is shown when the theme
281            previews are completed */
282         gtk_main();
283
284         preview_update_set_active_font(NULL);
285         preview_update_set_inactive_font(NULL);
286         preview_update_set_menu_header_font(NULL);
287         preview_update_set_menu_item_font(NULL);
288         preview_update_set_osd_font(NULL);
289         preview_update_set_title_layout(NULL);
290     }
291
292     RrInstanceFree(rrinst);
293     parse_paths_shutdown();
294
295     xmlFreeDoc(doc);
296     return 0;
297 }
298
299 gboolean on_main_window_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
300 {
301     gtk_main_quit();
302     return FALSE;
303 }
304
305 void on_close_clicked()
306 {
307     gtk_main_quit();
308 }
309
310 void obconf_show_main()
311 {
312     SnDisplay *sn_d;
313     SnLauncheeContext *sn_cx;
314
315     if (GTK_WIDGET_VISIBLE(mainwin)) return;
316
317     gtk_widget_show_all(mainwin);
318
319     sn_d = sn_display_new(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
320                           NULL, NULL);
321
322     sn_cx = sn_launchee_context_new_from_environment
323         (sn_d, gdk_screen_get_number(gdk_display_get_default_screen
324                                      (gdk_display_get_default())));
325
326     if (sn_cx)
327         sn_launchee_context_setup_window
328             (sn_cx, GDK_WINDOW_XWINDOW(GDK_WINDOW(mainwin->window)));
329
330     if (sn_cx)
331         sn_launchee_context_complete(sn_cx);
332
333     if (sn_cx)
334         sn_launchee_context_unref(sn_cx);
335     sn_display_unref(sn_d);
336 }