]> icculus.org git repositories - dana/obconf.git/blob - src/main.c
Make the ObConf Error dialog title be translatable
[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 GtkWidget *tabstrip = NULL;
40
41 GladeXML *glade;
42 xmlDocPtr doc;
43 xmlNodePtr root;
44 RrInstance *rrinst;
45 gchar *obc_config_file = NULL;
46 gint obc_tab;
47 ObtPaths *paths;
48 ObtXmlInst *parse_i;
49
50 static gchar *obc_theme_install = NULL;
51 static gchar *obc_theme_archive = NULL;
52
53 void obconf_error(gchar *msg, gboolean modal)
54 {
55     GtkWidget *d;
56
57     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
58                                GTK_DIALOG_DESTROY_WITH_PARENT,
59                                GTK_MESSAGE_ERROR,
60                                GTK_BUTTONS_CLOSE,
61                                "%s", msg);
62     gtk_window_set_title(GTK_WINDOW(d), _("ObConf Error"));
63     if (modal)
64         gtk_dialog_run(GTK_DIALOG(d));
65     else {
66         g_signal_connect_swapped(GTK_OBJECT(d), "response",
67                                  G_CALLBACK(gtk_widget_destroy),
68                                  GTK_OBJECT(d));
69         gtk_widget_show(d);
70     }
71 }
72
73 static void print_version()
74 {
75     g_print("ObConf %s\n", PACKAGE_VERSION);
76     g_print(_("Copyright (c)"));
77     g_print(" 2003-2008   Dana Jansens\n");
78     g_print(_("Copyright (c)"));
79     g_print(" 2003        Tim Riley\n");
80     g_print(_("Copyright (c)"));
81     g_print(" 2007        Javeed Shaikh\n\n");
82     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
83     g_print("This is free software, and you are welcome to redistribute it\n");
84     g_print("under certain conditions. See the file COPYING for details.\n\n");
85
86     exit(EXIT_SUCCESS);
87 }
88
89 static void print_help()
90 {
91     g_print(_("Syntax: obconf [options] [ARCHIVE.obt]\n"));
92     g_print(_("\nOptions:\n"));
93     g_print(_("  --help                Display this help and exit\n"));
94     g_print(_("  --version             Display the version and exit\n"));
95     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
96     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
97     g_print(_("  --config-file FILE    Specify the path to the config file to use\n"));
98     g_print(_("  --tab NUMBER          Switch to tab number NUMBER on startup\n"));
99     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
100
101     exit(EXIT_SUCCESS);
102 }
103
104 static void parse_args(int argc, char **argv)
105 {
106     int i;
107
108     for (i = 1; i < argc; ++i) {
109         if (!strcmp(argv[i], "--help"))
110             print_help();
111         if (!strcmp(argv[i], "--version"))
112             print_version();
113         else if (!strcmp(argv[i], "--install")) {
114             if (i == argc - 1) /* no args left */
115                 g_printerr(_("--install requires an argument\n"));
116             else
117                 obc_theme_install = argv[++i];
118         }
119         else if (!strcmp(argv[i], "--archive")) {
120             if (i == argc - 1) /* no args left */
121                 g_printerr(_("--archive requires an argument\n"));
122             else
123                 obc_theme_archive = argv[++i];
124         }
125         else if (!strcmp(argv[i], "--config-file")) {
126             if (i == argc - 1) /* no args left */
127                 g_printerr(_("--config-file requires an argument\n"));
128             else
129                 obc_config_file = argv[++i];
130         }
131         else if (!strcmp(argv[i], "--tab")) {
132             if (i == argc - 1) /* no args left */
133                 g_printerr(_("--tab requires an argument\n"));
134             else
135                 obc_tab = MAX(atoi(argv[++i]) - 1, 0);
136         }
137         else
138             obc_theme_install = argv[i];
139     }
140 }
141
142 static gboolean get_all(Window win, Atom prop, Atom type, gint size,
143                         guchar **data, guint *num)
144 {
145     gboolean ret = FALSE;
146     gint res;
147     guchar *xdata = NULL;
148     Atom ret_type;
149     gint ret_size;
150     gulong ret_items, bytes_left;
151
152     res = XGetWindowProperty(GDK_DISPLAY(), win, prop, 0l, G_MAXLONG,
153                              FALSE, type, &ret_type, &ret_size,
154                              &ret_items, &bytes_left, &xdata);
155     if (res == Success) {
156         if (ret_size == size && ret_items > 0) {
157             guint i;
158
159             *data = g_malloc(ret_items * (size / 8));
160             for (i = 0; i < ret_items; ++i)
161                 switch (size) {
162                 case 8:
163                     (*data)[i] = xdata[i];
164                     break;
165                 case 16:
166                     ((guint16*)*data)[i] = ((gushort*)xdata)[i];
167                     break;
168                 case 32:
169                     ((guint32*)*data)[i] = ((gulong*)xdata)[i];
170                     break;
171                 default:
172                     g_assert_not_reached(); /* unhandled size */
173                 }
174             *num = ret_items;
175             ret = TRUE;
176         }
177         XFree(xdata);
178     }
179     return ret;
180 }
181
182 static gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
183 {
184     gchar *raw;
185     gchar *str;
186     guint num;
187
188     if (get_all(win, prop,
189                 gdk_x11_get_xatom_by_name("UTF8_STRING"),
190                 8,(guchar**)&raw, &num))
191     {
192         str = g_strndup(raw, num); /* grab the first string from the list */
193         g_free(raw);
194         if (g_utf8_validate(str, -1, NULL)) {
195             *ret = str;
196             return TRUE;
197         }
198         g_free(str);
199     }
200     return FALSE;
201 }
202
203 int main(int argc, char **argv)
204 {
205     gchar *p;
206     gboolean exit_with_error = FALSE;
207
208     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
209     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
210     textdomain(PACKAGE_NAME);
211
212     gtk_init(&argc, &argv);
213     parse_args(argc, argv);
214
215     if (obc_theme_archive) {
216         archive_create(obc_theme_archive);
217         return 0;
218     }
219
220     p = g_build_filename(GLADEDIR, "obconf.glade", NULL);
221     glade = glade_xml_new(p, NULL, NULL);
222     g_free(p);
223
224     if (!glade) {
225         obconf_error(_("Failed to load the obconf.glade interface file. You have probably failed to install ObConf properly."), TRUE);
226         exit_with_error = TRUE;
227     }
228
229     paths = obt_paths_new();
230     parse_i = obt_xml_instance_new();
231     rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
232
233     if (!obc_config_file) {
234         gchar *p;
235
236         if (prop_get_string_utf8(GDK_ROOT_WINDOW(),
237                                  gdk_x11_get_xatom_by_name("_OB_CONFIG_FILE"),
238                                  &p))
239         {
240             obc_config_file = g_filename_from_utf8(p, -1, NULL, NULL, NULL);
241             g_free(p);
242         }
243     }
244
245     xmlIndentTreeOutput = 1;
246     if (!((obc_config_file &&
247            obt_xml_load_file(parse_i, obc_config_file, "openbox_config")) ||
248           obt_xml_load_config_file(parse_i, "openbox", "rc.xml",
249                                    "openbox_config")))
250     {
251         obconf_error(_("Failed to load an rc.xml. You have probably failed to install Openbox properly."), TRUE);
252         exit_with_error = TRUE;
253     }
254     else {
255         doc = obt_xml_doc(parse_i);
256         root = obt_xml_root(parse_i);
257     }
258
259     /* look for parsing errors */
260     {
261         xmlErrorPtr e = xmlGetLastError();
262         if (e) {
263             char *a = g_strdup_printf
264                 (_("Error while parsing the Openbox configuration file.  Your configuration file is not valid XML.\n\nMessage: %s"),
265                  e->message);
266             obconf_error(a, TRUE);
267             g_free(a);
268             exit_with_error = TRUE;
269         }
270     }
271
272     if (!exit_with_error) {
273         glade_xml_signal_autoconnect(glade);
274
275         {
276             gchar *s = g_strdup_printf
277                 ("<span weight=\"bold\" size=\"xx-large\">ObConf %s</span>",
278                  PACKAGE_VERSION);
279             gtk_label_set_markup(GTK_LABEL
280                                  (glade_xml_get_widget(glade, "title_label")),
281                                  s);
282             g_free(s);
283         }
284
285         theme_setup_tab();
286         appearance_setup_tab();
287         windows_setup_tab();
288         moveresize_setup_tab();
289         mouse_setup_tab();
290         desktops_setup_tab();
291         margins_setup_tab();
292         dock_setup_tab();
293
294         mainwin = get_widget("main_window");
295         tabstrip = glade_xml_get_widget(glade, "tabstrip");
296
297         if (obc_theme_install)
298             theme_install(obc_theme_install);
299         else
300             theme_load_all();
301
302         /* the main window is not shown here ! it is shown when the theme
303            previews are completed */
304         gtk_main();
305
306         preview_update_set_active_font(NULL);
307         preview_update_set_inactive_font(NULL);
308         preview_update_set_menu_header_font(NULL);
309         preview_update_set_menu_item_font(NULL);
310         preview_update_set_osd_active_font(NULL);
311         preview_update_set_osd_inactive_font(NULL);
312         preview_update_set_title_layout(NULL);
313     }
314
315     RrInstanceFree(rrinst);
316     obt_xml_instance_unref(parse_i);
317     obt_paths_unref(paths);
318
319     xmlFreeDoc(doc);
320     return 0;
321 }
322
323 gboolean on_main_window_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
324 {
325     gtk_main_quit();
326     return FALSE;
327 }
328
329 void on_close_clicked()
330 {
331     gtk_main_quit();
332 }
333
334 void obconf_show_main()
335 {
336     SnDisplay *sn_d;
337     SnLauncheeContext *sn_cx;
338
339     if (GTK_WIDGET_VISIBLE(mainwin)) return;
340
341     gtk_widget_show_all(mainwin);
342
343     /* Focus on the tab number specified by --tab. */
344     if (obc_tab)
345         gtk_notebook_set_current_page(GTK_NOTEBOOK(tabstrip), obc_tab);
346
347
348     sn_d = sn_display_new(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
349                           NULL, NULL);
350
351     sn_cx = sn_launchee_context_new_from_environment
352         (sn_d, gdk_screen_get_number(gdk_display_get_default_screen
353                                      (gdk_display_get_default())));
354
355     if (sn_cx)
356         sn_launchee_context_setup_window
357             (sn_cx, GDK_WINDOW_XWINDOW(GDK_WINDOW(mainwin->window)));
358
359     if (sn_cx)
360         sn_launchee_context_complete(sn_cx);
361
362     if (sn_cx)
363         sn_launchee_context_unref(sn_cx);
364     sn_display_unref(sn_d);
365 }