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>
25 #include <unistd.h> /* close(), dup(), dup2(), STDERR_FILENO */
26 #include <stdlib.h> /* exit() */
32 #include "gliv-image.h"
34 #include "files_list.h"
38 #include "collection.h"
40 #include "mnemonics.h"
43 options_struct
*options
;
44 GlivImage
*current_image
= NULL
;
48 static gboolean option_add_all
= FALSE
;
51 static gboolean
get_on_off(const gchar
* str
)
56 while (*str
== '=' || *str
== ' ')
59 if (g_str_equal(str
, "on"))
62 if (g_str_equal(str
, "off"))
65 g_printerr(_("Command line flags should be on or off, not %s\n"), str
);
83 static void fill_options(struct gengetopt_args_info
*ggo
)
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 }
98 { FALSE
, NULL
, FALSE
, 0 }
105 for (flag
= flags
; flag
->flag
!= NULL
; flag
++)
107 *flag
->flag
= get_on_off(flag
->str
);
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
123 static struct gengetopt_args_info
*parse_cmdline(gint argc
, gchar
** argv
,
126 struct gengetopt_args_info
*ggo
= g_new(struct gengetopt_args_info
, 1);
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
);
142 /* Restore stderr. */
143 dup2(fd
, STDERR_FILENO
);
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
;
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.
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"));
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). */
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
;
198 rt
->alpha_checks_changed
= TRUE
;
201 if (FLAG_ON(ggo
->null
))
203 init_from_null_filenames(FLAG_ON(ggo
->sort
), FLAG_ON(ggo
->shuffle
),
206 if (ggo
->inputs_num
> 0) {
207 /* There are filenames on the command line. */
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"));
226 if (connect_server(FLAG_ON(ggo
->client_clear
)))
227 /* Successfully reused a GLiv window. */
231 options
->initial_geometry
= g_strdup(ggo
->geometry_arg
);
234 cmdline_parser_free(ggo
);
239 G_GNUC_NORETURN
void quit(gint code
)
241 if (options
!= NULL
&& options
->save_quit
)
247 static GtkCheckButton
*get_stop_confirm_button(void)
249 GtkCheckButton
*confirm
;
252 if (options
->save_quit
)
253 message
= g_strdup(_("Do not ask again"));
256 g_strconcat(_("Do not ask again"),
257 " (", _("Options will be saved"), ")", NULL
);
260 GTK_CHECK_BUTTON(gtk_check_button_new_with_mnemonic
261 (add_mnemonic(message
)));
263 gtk_widget_show_all(GTK_WIDGET(confirm
));
269 gboolean
gui_quit(void)
271 GtkMessageDialog
*dialog
;
272 GtkCheckButton
*stop_confirm
;
276 if (options
->confirm_quit
== FALSE
)
279 msg
= _("Do you really want to quit GLiv?");
281 dialog
= GTK_MESSAGE_DIALOG(gtk_message_dialog_new(NULL
,
283 GTK_MESSAGE_QUESTION
,
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
)
305 gtk_widget_destroy(GTK_WIDGET(dialog
));
309 gint
main(gint argc
, gchar
** argv
)
317 bindtextdomain(PACKAGE
, LOCALEDIR
);
320 bind_textdomain_codeset(PACKAGE
, "UTF-8");
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
);
337 DIALOG_MSG(_("No image found"));
340 case INIT_BAD_CMDLINE
: