]> icculus.org git repositories - dana/obconf.git/blob - src/main.c
update the year in the copyright
[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-2007   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
45 static gchar *obc_theme_install = NULL;
46 static gchar *obc_theme_archive = NULL;
47
48 void obconf_error(gchar *msg)
49 {
50     GtkWidget *d;
51
52     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
53                                GTK_DIALOG_DESTROY_WITH_PARENT,
54                                GTK_MESSAGE_ERROR,
55                                GTK_BUTTONS_CLOSE,
56                                "%s", msg);
57     g_signal_connect_swapped(GTK_OBJECT(d), "response",
58                              G_CALLBACK(gtk_widget_destroy),
59                              GTK_OBJECT(d));
60     gtk_widget_show(d);
61 }
62
63 static void print_version()
64 {
65     g_print("ObConf %s\n", PACKAGE_VERSION);
66     g_print(_("Copyright (c)"));
67     g_print(" 2003-2008   Dana Jansens\n");
68     g_print(_("Copyright (c)"));
69     g_print(" 2003        Tim Riley\n");
70     g_print(_("Copyright (c)"));
71     g_print(" 2007        Javeed Shaikh\n\n");
72     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
73     g_print("This is free software, and you are welcome to redistribute it\n");
74     g_print("under certain conditions. See the file COPYING for details.\n\n");
75
76     exit(EXIT_SUCCESS);
77 }
78
79 static void print_help()
80 {
81     g_print(_("Syntax: obconf [options] [ARCHIVE.obt]\n"));
82     g_print(_("\nOptions:\n"));
83     g_print(_("  --help                Display this help and exit\n"));
84     g_print(_("  --version             Display the version and exit\n"));
85     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
86     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
87     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
88     
89     exit(EXIT_SUCCESS);
90 }
91
92 static void parse_args(int argc, char **argv)
93 {
94     int i;
95
96     for (i = 1; i < argc; ++i) {
97         if (!strcmp(argv[i], "--help"))
98             print_help();
99         if (!strcmp(argv[i], "--version"))
100             print_version();
101         else if (!strcmp(argv[i], "--install")) {
102             if (i == argc - 1) /* no args left */
103                 g_printerr(_("--install requires an argument\n"));
104             else
105                 obc_theme_install = argv[++i];
106         }
107         else if (!strcmp(argv[i], "--archive")) {
108             if (i == argc - 1) /* no args left */
109                 g_printerr(_("--archive requires an argument\n"));
110             else
111                 obc_theme_archive = argv[++i];
112         } else
113             obc_theme_install = argv[i];
114     }
115 }
116
117 int main(int argc, char **argv)
118 {
119     gchar *p;
120
121     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
122     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
123     textdomain(PACKAGE_NAME);
124
125     gtk_init(&argc, &argv);
126     parse_args(argc, argv);
127
128     if (obc_theme_archive) {
129         archive_create(obc_theme_archive);
130         return;
131     }
132
133     p = g_build_filename(GLADEDIR, "obconf.glade", NULL);
134     glade = glade_xml_new(p, NULL, NULL);
135     g_free(p);
136
137     if (!glade) {
138         obconf_error("Failed to load the obconf.glade interface file. You "
139                      "have probably failed to install ObConf properly.");
140         return 1;
141     }
142
143     parse_paths_startup();
144     rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
145
146     xmlIndentTreeOutput = 1;
147     if (!parse_load_rc(NULL, &doc, &root)) {
148         obconf_error("Failed to load an rc.xml. You have probably failed to "
149                      "install Openbox properly.");
150         return 1;
151     }
152
153     glade_xml_signal_autoconnect(glade);
154
155     {
156         gchar *s = g_strdup_printf
157             ("<span weight=\"bold\" size=\"xx-large\">ObConf %s</span>",
158              PACKAGE_VERSION);
159         gtk_label_set_markup(GTK_LABEL
160                              (glade_xml_get_widget(glade, "title_label")), s);
161         g_free(s);
162     }
163
164     theme_setup_tab();
165     appearance_setup_tab();
166     windows_setup_tab();
167     mouse_setup_tab();
168     desktops_setup_tab();
169     margins_setup_tab();
170     dock_setup_tab();
171
172     mainwin = get_widget("main_window");
173
174     if (obc_theme_install)
175         theme_install(obc_theme_install);
176     else
177         theme_load_all();
178
179     /* the main window is not shown here ! it is shown when the theme previews
180        are completed */
181     gtk_main();
182
183     preview_update_set_active_font(NULL);
184     preview_update_set_inactive_font(NULL);
185     preview_update_set_menu_header_font(NULL);
186     preview_update_set_menu_item_font(NULL);
187     preview_update_set_osd_font(NULL);
188     preview_update_set_title_layout(NULL);
189
190     RrInstanceFree(rrinst);
191     parse_paths_shutdown();
192
193     xmlFreeDoc(doc);
194     return 0;
195 }
196
197 gboolean on_main_window_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
198 {
199     gtk_main_quit();
200     return FALSE;
201 }
202
203 void on_close_clicked()
204 {
205     gtk_main_quit();
206 }
207
208 void obconf_show_main()
209 {
210     SnDisplay *sn_d;
211     SnLauncheeContext *sn_cx;
212
213     if (GTK_WIDGET_VISIBLE(mainwin)) return;
214
215     gtk_widget_show_all(mainwin);
216
217     sn_d = sn_display_new(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
218                           NULL, NULL);
219
220     sn_cx = sn_launchee_context_new_from_environment
221         (sn_d, gdk_screen_get_number(gdk_display_get_default_screen
222                                      (gdk_display_get_default())));
223
224     if (sn_cx)
225         sn_launchee_context_setup_window
226             (sn_cx, GDK_WINDOW_XWINDOW(GDK_WINDOW(mainwin->window)));
227
228     if (sn_cx)
229         sn_launchee_context_complete(sn_cx);
230
231     if (sn_cx)
232         sn_launchee_context_unref(sn_cx);
233     sn_display_unref(sn_d);
234 }