4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
18 #include "collect-io.h"
20 #include "filefilter.h"
21 #include "history_list.h"
22 #include "image-overlay.h"
24 #include "layout_image.h"
28 #include "ui_fileops.h"
29 #include "ui_utildlg.h"
30 #include "cache_maint.h"
33 #include <gdk/gdkkeysyms.h> /* for keyboard values */
42 static RemoteConnection *remote_connection = NULL;
46 *-----------------------------------------------------------------------------
48 *-----------------------------------------------------------------------------
51 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
53 static gint delta = 0;
54 static guint32 time_old = 0;
55 static guint keyval_old = 0;
57 if (event->state & GDK_CONTROL_MASK)
59 if (*x < 0) *x = G_MININT / 2;
60 if (*x > 0) *x = G_MAXINT / 2;
61 if (*y < 0) *y = G_MININT / 2;
62 if (*y > 0) *y = G_MAXINT / 2;
67 if (options->progressive_key_scrolling)
71 time_diff = event->time - time_old;
73 /* key pressed within 125ms ? (1/8 second) */
74 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
76 time_old = event->time;
77 keyval_old = event->keyval;
93 *-----------------------------------------------------------------------------
94 * command line parser (private) hehe, who needs popt anyway?
95 *-----------------------------------------------------------------------------
98 static gint startup_blank = FALSE;
99 static gint startup_full_screen = FALSE;
100 static gint startup_in_slideshow = FALSE;
101 static gint startup_command_line_collection = FALSE;
104 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
105 GList **list, GList **collection_list)
109 path_parsed = g_strdup(file_path);
110 parse_out_relatives(path_parsed);
112 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
114 *collection_list = g_list_append(*collection_list, path_parsed);
118 if (!*path) *path = remove_level_from_path(path_parsed);
119 if (!*file) *file = g_strdup(path_parsed);
120 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
124 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
131 path_parsed = g_strdup(dir);
132 parse_out_relatives(path_parsed);
133 dir_fd = file_data_new_simple(path_parsed);
136 if (filelist_read(dir_fd, &files, NULL))
140 files = filelist_filter(files, FALSE);
141 files = filelist_sort_path(files);
146 FileData *fd = work->data;
147 if (!*path) *path = remove_level_from_path(fd->path);
148 if (!*file) *file = g_strdup(fd->path);
149 *list = g_list_prepend(*list, fd);
158 file_data_unref(dir_fd);
161 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
162 GList **list, gchar **first_dir)
165 if (!*list && !*first_dir)
167 *first_dir = g_strdup(dir);
173 parse_command_line_add_dir(*first_dir, path, file, list);
177 parse_command_line_add_dir(dir, path, file, list);
181 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
182 GList **list, GList **collection_list, gchar **first_dir)
187 parse_command_line_add_dir(*first_dir, path, file, list);
191 parse_command_line_add_file(file_path, path, file, list, collection_list);
194 static void parse_command_line(gint argc, gchar *argv[], gchar **path, gchar **file,
195 GList **cmd_list, GList **collection_list,
199 GList *remote_list = NULL;
200 GList *remote_errors = NULL;
201 gint remote_do = FALSE;
202 gchar *first_dir = NULL;
207 gchar *base_dir = get_current_dir();
211 const gchar *cmd_line = argv[i];
212 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
214 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
216 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
218 else if (isdir(cmd_all))
220 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
222 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
224 parse_command_line_process_file(cmd_line, path, file,
225 &list, collection_list, &first_dir);
227 else if (isfile(cmd_all))
229 parse_command_line_process_file(cmd_all, path, file,
230 &list, collection_list, &first_dir);
232 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
234 /* do nothing but do not produce warnings */
236 else if (strcmp(cmd_line, "+t") == 0 ||
237 strcmp(cmd_line, "--with-tools") == 0)
239 options->layout.tools_float = FALSE;
240 options->layout.tools_hidden = FALSE;
242 remote_list = g_list_append(remote_list, "+t");
244 else if (strcmp(cmd_line, "-t") == 0 ||
245 strcmp(cmd_line, "--without-tools") == 0)
247 options->layout.tools_hidden = TRUE;
249 remote_list = g_list_append(remote_list, "-t");
251 else if (strcmp(cmd_line, "-f") == 0 ||
252 strcmp(cmd_line, "--fullscreen") == 0)
254 startup_full_screen = TRUE;
256 else if (strcmp(cmd_line, "-s") == 0 ||
257 strcmp(cmd_line, "--slideshow") == 0)
259 startup_in_slideshow = TRUE;
261 else if (strcmp(cmd_line, "-l") == 0 ||
262 strcmp(cmd_line, "--list") == 0)
264 startup_command_line_collection = TRUE;
266 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
268 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
270 else if (strcmp(cmd_line, "-r") == 0 ||
271 strcmp(cmd_line, "--remote") == 0)
276 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
279 else if (strcmp(cmd_line, "-rh") == 0 ||
280 strcmp(cmd_line, "--remote-help") == 0)
285 else if (strcmp(cmd_line, "--blank") == 0)
287 startup_blank = TRUE;
289 else if (strcmp(cmd_line, "-v") == 0 ||
290 strcmp(cmd_line, "--version") == 0)
292 printf_term("%s %s\n", GQ_APPNAME, VERSION);
295 else if (strcmp(cmd_line, "--alternate") == 0)
297 /* enable faster experimental algorithm */
298 log_printf("Alternate similarity algorithm enabled\n");
299 image_sim_alternate_set(TRUE);
301 else if (strcmp(cmd_line, "-h") == 0 ||
302 strcmp(cmd_line, "--help") == 0)
304 printf_term("%s %s\n", GQ_APPNAME, VERSION);
305 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
306 print_term(_("valid options are:\n"));
307 print_term(_(" +t, --with-tools force show of tools\n"));
308 print_term(_(" -t, --without-tools force hide of tools\n"));
309 print_term(_(" -f, --fullscreen start in full screen mode\n"));
310 print_term(_(" -s, --slideshow start in slideshow mode\n"));
311 print_term(_(" -l, --list open collection window for command line\n"));
312 print_term(_(" --geometry=GEOMETRY set main window location\n"));
313 print_term(_(" -r, --remote send following commands to open window\n"));
314 print_term(_(" -rh,--remote-help print remote command list\n"));
316 print_term(_(" --debug[=level] turn on debug output\n"));
318 print_term(_(" -v, --version print version info\n"));
319 print_term(_(" -h, --help show this message\n\n"));
322 /* these options are not officially supported!
323 * only for testing new features, no need to translate them */
324 print_term( " --alternate use alternate similarity algorithm\n");
331 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
338 parse_out_relatives(*path);
339 parse_out_relatives(*file);
342 list = g_list_reverse(list);
344 if (!*path && first_dir)
349 parse_out_relatives(*path);
357 GList *work = remote_errors;
359 printf_term(_("Invalid or ignored remote options: "));
362 gchar *opt = work->data;
364 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
368 printf_term(_("\nUse --remote-help for valid remote options.\n"));
371 remote_control(argv[0], remote_list, *path, list, *collection_list);
373 g_list_free(remote_list);
375 if (list && list->next)
386 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
389 const gchar *debug_option = "--debug";
390 gint len = strlen(debug_option);
396 for (i = 1; i < argc; i++)
398 const gchar *cmd_line = argv[i];
399 if (strncmp(cmd_line, debug_option, len) == 0)
401 gint cmd_line_len = strlen(cmd_line);
403 /* we now increment the debug state for verbosity */
404 if (cmd_line_len == len)
406 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
408 gint n = atoi(cmd_line + len + 1);
416 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
421 *-----------------------------------------------------------------------------
422 * startup, init, and exit
423 *-----------------------------------------------------------------------------
426 #define RC_HISTORY_NAME "history"
428 static void keys_load(void)
432 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
433 history_list_load(path);
437 static void keys_save(void)
441 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
442 history_list_save(path);
446 static void check_for_home_path(gchar *path)
450 buf = g_build_filename(homedir(), path, NULL);
453 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
455 if (!mkdir_utf8(buf, 0755))
457 log_printf(_("Could not create dir:%s\n"), buf);
463 static void accel_map_save(void)
468 path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
469 pathl = path_from_utf8(path);
470 gtk_accel_map_save(pathl);
475 static void accel_map_load(void)
480 path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
481 pathl = path_from_utf8(path);
482 gtk_accel_map_load(pathl);
487 static void gtkrc_load(void)
492 /* If a gtkrc file exists in the rc directory, add it to the
493 * list of files to be parsed at the end of gtk_init() */
494 path = g_build_filename(homedir(), GQ_RC_DIR, "gtkrc", NULL);
495 pathl = path_from_utf8(path);
496 if (access(pathl, R_OK) == 0)
497 gtk_rc_add_default_file(pathl);
502 static void exit_program_final(void)
504 LayoutWindow *lw = NULL;
506 remote_close(remote_connection);
508 collect_manager_flush();
510 save_options(options);
514 if (layout_valid(&lw))
522 static GenericDialog *exit_dialog = NULL;
524 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
527 generic_dialog_close(gd);
530 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
533 generic_dialog_close(gd);
534 exit_program_final();
537 static gint exit_confirm_dlg(void)
545 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
549 if (!collection_window_modified_exists()) return FALSE;
553 if (layout_valid(&lw))
558 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
559 exit_dialog = generic_dialog_new(msg,
560 GQ_WMCLASS, "exit", parent, FALSE,
561 exit_confirm_cancel_cb, NULL);
563 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
564 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
565 msg, _("Collections have been modified. Quit anyway?"));
567 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
569 gtk_widget_show(exit_dialog->dialog);
574 void exit_program(void)
576 layout_image_full_screen_stop(NULL);
578 if (exit_confirm_dlg()) return;
580 exit_program_final();
583 gint main(gint argc, gchar *argv[])
587 gchar *cmd_path = NULL;
588 gchar *cmd_file = NULL;
589 GList *cmd_list = NULL;
590 GList *collection_list = NULL;
591 CollectionData *first_collection = NULL;
592 gchar *geometry = NULL;
594 CollectionData *cd = NULL;
597 g_thread_init (NULL);
602 /* init execution time counter (debug only) */
605 /* setup locale, i18n */
609 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
610 bind_textdomain_codeset(PACKAGE, "UTF-8");
614 /* setup random seed for random slideshow */
618 log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
621 /* register global notify functions */
622 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
623 file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
624 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
626 parse_command_line_for_debug_option(argc, argv);
628 options = init_options(NULL);
629 setup_default_options(options);
630 load_options(options);
632 parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
635 gtk_init(&argc, &argv);
637 if (gtk_major_version < GTK_MAJOR_VERSION ||
638 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
640 log_printf("!!! This is a friendly warning.\n");
641 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
642 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
643 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
644 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
647 check_for_home_path(GQ_RC_DIR);
648 check_for_home_path(GQ_RC_DIR_COLLECTIONS);
649 check_for_home_path(GQ_CACHE_RC_THUMB);
650 check_for_home_path(GQ_CACHE_RC_METADATA);
653 filter_add_defaults();
664 filelist_free(cmd_list);
666 string_list_free(collection_list);
667 collection_list = NULL;
673 path = g_strdup(cmd_path);
675 else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
677 path = g_strdup(options->startup.path);
681 path = get_current_dir();
684 lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
685 layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
687 if (collection_list && !startup_command_line_collection)
691 work = collection_list;
700 cw = collection_window_new(path);
701 if (!first_collection && cw) first_collection = cw->cd;
706 (startup_command_line_collection && collection_list))
710 if (startup_command_line_collection)
714 cw = collection_window_new("");
719 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
725 cd->name = g_strdup(_("Command line"));
727 collection_path_changed(cd);
732 collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
736 work = collection_list;
739 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
743 layout_set_path(lw, path);
744 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
746 /* mem leak, we never unref this collection when !startup_command_line_collection
747 * (the image view of the main window does not hold a ref to the collection)
748 * this is sort of unavoidable, for if it did hold a ref, next/back
749 * may not work as expected when closing collection windows.
751 * collection_unref(cd);
757 layout_set_path(lw, cmd_file);
761 layout_set_path(lw, path);
762 if (first_collection)
764 layout_image_set_collection(lw, first_collection,
765 collection_get_first(first_collection));
769 image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
774 filelist_free(cmd_list);
775 string_list_free(collection_list);
778 if (startup_full_screen) layout_image_full_screen_start(lw);
779 if (startup_in_slideshow) layout_image_slideshow_start(lw);
781 buf = g_build_filename(homedir(), GQ_RC_DIR, ".command", NULL);
782 remote_connection = remote_server_init(buf, cd);