Merged older cs.po file with newest pot file.
[gliv/czech_localization.git] / src / main.c
blobd321ef1a71d5a7a324f7991999a07ff352fc380f
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <guichaz@yahoo.fr>
21 /********************
22 * Main source file *
23 ********************/
25 #include <unistd.h> /* close(), dup(), dup2(), STDERR_FILENO */
26 #include <stdlib.h> /* exit() */
28 #include "gliv.h"
29 #include "main.h"
30 #include "cmdline.h"
31 #include "options.h"
32 #include "gliv-image.h"
33 #include "rcfile.h"
34 #include "files_list.h"
35 #include "messages.h"
36 #include "windows.h"
37 #include "ipc.h"
38 #include "collection.h"
39 #include "opengl.h"
40 #include "mnemonics.h"
42 rt_struct *rt;
43 options_struct *options;
44 GlivImage *current_image = NULL;
45 GtkWidget *gl_widget;
46 GtkMenuBar *menu_bar;
48 static gboolean option_add_all = FALSE;
50 /* May exit :) */
51 static gboolean get_on_off(const gchar * str)
53 if (str == NULL)
54 return TRUE;
56 while (*str == '=' || *str == ' ')
57 str++;
59 if (g_str_equal(str, "on"))
60 return TRUE;
62 if (g_str_equal(str, "off"))
63 return FALSE;
65 g_printerr(_("Command line flags should be on or off, not %s\n"), str);
66 quit(1);
69 typedef struct {
70 gboolean given;
71 gboolean *flag;
72 gchar *str;
73 gboolean not;
74 } ggo_flag;
76 typedef struct {
77 gboolean given;
78 gint *opt_val;
79 gint value;
80 gint minimum;
81 } ggo_int;
83 static void fill_options(struct gengetopt_args_info *ggo)
85 ggo_flag flags[] = {
86 /* *INDENT-OFF* */
87 { ggo->recursive_given, &options->recursive, ggo->recursive_arg, 0 },
88 { ggo->force_load_given, &options->force, ggo->force_load_arg, 0 },
89 { ggo->build_menus_given, &options->build_menus, ggo->build_menus_arg, 1 },
90 { ggo->slide_show_given, &options->start_show, ggo->slide_show_arg, 0 },
91 { ggo->add_all_given, &option_add_all, ggo->add_all_arg, 0 },
92 { FALSE, NULL, NULL, 0 }
93 /* *INDENT-ON* */
96 ggo_int ints[] = {
97 /* *INDENT-OFF* */
98 { FALSE, NULL, FALSE, 0 }
99 /* *INDENT-ON* */
102 ggo_flag *flag;
103 ggo_int *opt_int;
105 for (flag = flags; flag->flag != NULL; flag++)
106 if (flag->given) {
107 *flag->flag = get_on_off(flag->str);
108 if (flag->not)
109 *flag->flag ^= TRUE;
112 for (opt_int = ints; opt_int->opt_val != NULL; opt_int++)
113 if (opt_int->given && opt_int->value >= opt_int->minimum)
114 *opt_int->opt_val = opt_int->value;
117 #define FLAG_ON(flag) (flag##_given && get_on_off(flag##_arg))
120 * We temporarily close stderr because unknown and gtk arguments are handled
121 * afterwards.
123 static struct gengetopt_args_info *parse_cmdline(gint argc, gchar ** argv,
124 gboolean silent)
126 struct gengetopt_args_info *ggo = g_new(struct gengetopt_args_info, 1);
127 gint fd = -1;
129 if (silent) {
130 /* Silence stderr. */
131 fd = dup(STDERR_FILENO);
132 close(STDERR_FILENO);
135 if (cmdline_parser(argc, argv, ggo) != 0) {
136 cmdline_parser_free(ggo);
137 g_free(ggo);
138 ggo = NULL;
141 if (silent) {
142 /* Restore stderr. */
143 dup2(fd, STDERR_FILENO);
144 close(fd);
147 return ggo;
150 typedef enum {
151 INIT_BAD_CMDLINE,
152 INIT_OK,
153 INIT_NO_IMAGES
154 } init_res;
156 static init_res init_args(gint argc, gchar ** argv, gboolean gtk_initialized)
158 options_struct *rc_file;
159 gint nb_inserted = 0;
160 struct gengetopt_args_info *ggo =
161 parse_cmdline(argc, argv, !gtk_initialized);
162 init_res res = INIT_BAD_CMDLINE;
164 if (ggo == NULL)
165 return res;
167 if (!ggo->collection_given && !FLAG_ON(ggo->client) &&
168 !FLAG_ON(ggo->client_clear) && !gtk_initialized)
170 * We want a "graphical" gliv, so init it after gtk
171 * to be able to show the progress dialog when loading collections.
173 goto end;
176 res = INIT_OK;
178 /* Command line (some flags only). */
180 if (FLAG_ON(ggo->sort) && FLAG_ON(ggo->shuffle)) {
181 g_printerr(_("Cannot sort and shuffle at the same time\n"));
182 quit(1);
185 /* Configuration file. */
186 rc_file = load_rc(!ggo->glivrc_given, ggo->glivrc_arg);
187 options = g_memdup(rc_file, sizeof(options_struct));
189 /* Command line (remaining options). */
190 fill_options(ggo);
192 if (gtk_initialized) {
193 /* We use the (rt == NULL) check to see if gtk is initialized or not */
194 rt = g_new(rt_struct, 1);
196 rt->cursor_hidden = FALSE;
197 rt->help = FALSE;
198 rt->alpha_checks_changed = TRUE;
201 if (FLAG_ON(ggo->null))
202 nb_inserted =
203 init_from_null_filenames(FLAG_ON(ggo->sort), FLAG_ON(ggo->shuffle),
204 option_add_all);
206 if (ggo->inputs_num > 0) {
207 /* There are filenames on the command line. */
208 nb_inserted +=
209 init_list(ggo->inputs, ggo->inputs_num, FLAG_ON(ggo->sort),
210 FLAG_ON(ggo->shuffle), option_add_all);
212 if (nb_inserted == 0 && gtk_initialized)
213 res = INIT_NO_IMAGES;
216 if (ggo->collection_given)
217 quit(serialize_collection_nogui(ggo->collection_arg));
219 if (FLAG_ON(ggo->client) || FLAG_ON(ggo->client_clear)) {
220 if (FLAG_ON(ggo->client) && FLAG_ON(ggo->client_clear)) {
221 g_printerr(_("The --client and --client-clear command line options"
222 " are mutually exclusive\n"));
223 quit(1);
226 if (connect_server(FLAG_ON(ggo->client_clear)))
227 /* Successfully reused a GLiv window. */
228 quit(0);
231 options->initial_geometry = g_strdup(ggo->geometry_arg);
233 end:
234 cmdline_parser_free(ggo);
235 g_free(ggo);
236 return res;
239 G_GNUC_NORETURN void quit(gint code)
241 if (options != NULL && options->save_quit)
242 save_rc(options);
244 exit(code);
247 static GtkCheckButton *get_stop_confirm_button(void)
249 GtkCheckButton *confirm;
250 gchar *message;
252 if (options->save_quit)
253 message = g_strdup(_("Do not ask again"));
254 else
255 message =
256 g_strconcat(_("Do not ask again"),
257 " (", _("Options will be saved"), ")", NULL);
259 confirm =
260 GTK_CHECK_BUTTON(gtk_check_button_new_with_mnemonic
261 (add_mnemonic(message)));
263 gtk_widget_show_all(GTK_WIDGET(confirm));
264 g_free(message);
266 return confirm;
269 gboolean gui_quit(void)
271 GtkMessageDialog *dialog;
272 GtkCheckButton *stop_confirm;
273 gchar *msg;
274 gint response;
276 if (options->confirm_quit == FALSE)
277 quit(0);
279 msg = _("Do you really want to quit GLiv?");
281 dialog = GTK_MESSAGE_DIALOG(gtk_message_dialog_new(NULL,
282 GTK_DIALOG_MODAL,
283 GTK_MESSAGE_QUESTION,
284 GTK_BUTTONS_YES_NO,
285 "%s", msg));
287 stop_confirm = get_stop_confirm_button();
288 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),
289 GTK_WIDGET(stop_confirm));
291 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
293 response = run_modal_dialog(GTK_DIALOG(dialog));
295 if (response == GTK_RESPONSE_YES) {
296 options->confirm_quit =
297 !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(stop_confirm));
299 if (!options->confirm_quit && !options->save_quit)
300 save_rc(options);
302 quit(0);
305 gtk_widget_destroy(GTK_WIDGET(dialog));
306 return TRUE;
309 gint main(gint argc, gchar ** argv)
311 init_res res;
313 /* i18n */
314 #ifdef ENABLE_NLS
315 gtk_set_locale();
316 #ifdef LOCALEDIR
317 bindtextdomain(PACKAGE, LOCALEDIR);
318 #endif
319 textdomain(PACKAGE);
320 bind_textdomain_codeset(PACKAGE, "UTF-8");
321 #endif
323 g_thread_init(NULL);
324 g_type_init();
326 res = init_args(argc, argv, FALSE);
328 gtk_init(&argc, &argv);
329 gtk_gl_init(&argc, &argv);
331 if (res == INIT_BAD_CMDLINE)
332 /* Now that GTK may have removed some arguments */
333 res = init_args(argc, argv, TRUE);
335 switch (res) {
336 case INIT_NO_IMAGES:
337 DIALOG_MSG(_("No image found"));
338 break;
340 case INIT_BAD_CMDLINE:
341 quit(1);
342 break;
344 case INIT_OK:
345 /* OK */
346 break;
349 create_windows();
350 start_server();
352 gtk_main();
354 return 0;