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 "image-overlay.h"
23 #include "layout_image.h"
27 #include "ui_bookmark.h"
28 #include "ui_fileops.h"
29 #include "ui_utildlg.h"
30 #include "cache_maint.h"
32 #include <gdk/gdkkeysyms.h> /* for keyboard values */
41 static RemoteConnection *remote_connection = NULL;
45 *-----------------------------------------------------------------------------
47 *-----------------------------------------------------------------------------
51 gdouble get_zoom_increment(void)
53 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
56 gchar *utf8_validate_or_convert(const gchar *text)
60 if (!text) return NULL;
63 if (!g_utf8_validate(text, len, NULL))
64 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
66 return g_strdup(text);
69 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
71 gchar *s1_key, *s2_key;
75 g_assert(g_utf8_validate(s1, -1, NULL));
76 g_assert(g_utf8_validate(s2, -1, NULL));
80 s1_t = g_utf8_casefold(s1, -1);
81 s2_t = g_utf8_casefold(s2, -1);
84 s1_key = g_utf8_collate_key(s1_t, -1);
85 s2_key = g_utf8_collate_key(s2_t, -1);
87 ret = strcmp(s1_key, s2_key);
101 /* Borrowed from gtkfilesystemunix.c */
102 gchar *expand_tilde(const gchar *filename)
105 return g_strdup(filename);
111 if (filename[0] != '~')
112 return g_strdup(filename);
114 notilde = filename + 1;
115 slash = strchr(notilde, G_DIR_SEPARATOR);
116 if (slash == notilde || !*notilde)
118 home = g_get_home_dir();
120 return g_strdup(filename);
125 struct passwd *passwd;
128 username = g_strndup(notilde, slash - notilde);
130 username = g_strdup(notilde);
132 passwd = getpwnam(username);
136 return g_strdup(filename);
138 home = passwd->pw_dir;
142 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
144 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
150 *-----------------------------------------------------------------------------
152 *-----------------------------------------------------------------------------
155 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
157 static gint delta = 0;
158 static guint32 time_old = 0;
159 static guint keyval_old = 0;
161 if (event->state & GDK_CONTROL_MASK)
163 if (*x < 0) *x = G_MININT / 2;
164 if (*x > 0) *x = G_MAXINT / 2;
165 if (*y < 0) *y = G_MININT / 2;
166 if (*y > 0) *y = G_MAXINT / 2;
171 if (options->progressive_key_scrolling)
175 time_diff = event->time - time_old;
177 /* key pressed within 125ms ? (1/8 second) */
178 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
180 time_old = event->time;
181 keyval_old = event->keyval;
197 *-----------------------------------------------------------------------------
198 * command line parser (private) hehe, who needs popt anyway?
199 *-----------------------------------------------------------------------------
202 static gint startup_blank = FALSE;
203 static gint startup_full_screen = FALSE;
204 static gint startup_in_slideshow = FALSE;
205 static gint startup_command_line_collection = FALSE;
208 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
209 GList **list, GList **collection_list)
213 path_parsed = g_strdup(file_path);
214 parse_out_relatives(path_parsed);
216 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
218 *collection_list = g_list_append(*collection_list, path_parsed);
222 if (!*path) *path = remove_level_from_path(path_parsed);
223 if (!*file) *file = g_strdup(path_parsed);
224 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
228 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
235 path_parsed = g_strdup(dir);
236 parse_out_relatives(path_parsed);
237 dir_fd = file_data_new_simple(path_parsed);
240 if (filelist_read(dir_fd, &files, NULL))
244 files = filelist_filter(files, FALSE);
245 files = filelist_sort_path(files);
250 FileData *fd = work->data;
251 if (!*path) *path = remove_level_from_path(fd->path);
252 if (!*file) *file = g_strdup(fd->path);
253 *list = g_list_prepend(*list, fd);
262 file_data_unref(dir_fd);
265 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
266 GList **list, gchar **first_dir)
269 if (!*list && !*first_dir)
271 *first_dir = g_strdup(dir);
277 parse_command_line_add_dir(*first_dir, path, file, list);
281 parse_command_line_add_dir(dir, path, file, list);
285 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
286 GList **list, GList **collection_list, gchar **first_dir)
291 parse_command_line_add_dir(*first_dir, path, file, list);
295 parse_command_line_add_file(file_path, path, file, list, collection_list);
298 static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,
299 GList **cmd_list, GList **collection_list,
303 GList *remote_list = NULL;
304 GList *remote_errors = NULL;
305 gint remote_do = FALSE;
306 gchar *first_dir = NULL;
311 gchar *base_dir = get_current_dir();
315 const gchar *cmd_line = argv[i];
316 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
318 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
320 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
322 else if (isdir(cmd_all))
324 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
326 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
328 parse_command_line_process_file(cmd_line, path, file,
329 &list, collection_list, &first_dir);
331 else if (isfile(cmd_all))
333 parse_command_line_process_file(cmd_all, path, file,
334 &list, collection_list, &first_dir);
336 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
338 /* do nothing but do not produce warnings */
340 else if (strcmp(cmd_line, "+t") == 0 ||
341 strcmp(cmd_line, "--with-tools") == 0)
343 options->layout.tools_float = FALSE;
344 options->layout.tools_hidden = FALSE;
346 remote_list = g_list_append(remote_list, "+t");
348 else if (strcmp(cmd_line, "-t") == 0 ||
349 strcmp(cmd_line, "--without-tools") == 0)
351 options->layout.tools_hidden = TRUE;
353 remote_list = g_list_append(remote_list, "-t");
355 else if (strcmp(cmd_line, "-f") == 0 ||
356 strcmp(cmd_line, "--fullscreen") == 0)
358 startup_full_screen = TRUE;
360 else if (strcmp(cmd_line, "-s") == 0 ||
361 strcmp(cmd_line, "--slideshow") == 0)
363 startup_in_slideshow = TRUE;
365 else if (strcmp(cmd_line, "-l") == 0 ||
366 strcmp(cmd_line, "--list") == 0)
368 startup_command_line_collection = TRUE;
370 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
372 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
374 else if (strcmp(cmd_line, "-r") == 0 ||
375 strcmp(cmd_line, "--remote") == 0)
380 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
383 else if (strcmp(cmd_line, "-rh") == 0 ||
384 strcmp(cmd_line, "--remote-help") == 0)
389 else if (strcmp(cmd_line, "--blank") == 0)
391 startup_blank = TRUE;
393 else if (strcmp(cmd_line, "-v") == 0 ||
394 strcmp(cmd_line, "--version") == 0)
396 printf_term("%s %s\n", GQ_APPNAME, VERSION);
399 else if (strcmp(cmd_line, "--alternate") == 0)
401 /* enable faster experimental algorithm */
402 log_printf("Alternate similarity algorithm enabled\n");
403 image_sim_alternate_set(TRUE);
405 else if (strcmp(cmd_line, "-h") == 0 ||
406 strcmp(cmd_line, "--help") == 0)
408 printf_term("%s %s\n", GQ_APPNAME, VERSION);
409 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
410 print_term(_("valid options are:\n"));
411 print_term(_(" +t, --with-tools force show of tools\n"));
412 print_term(_(" -t, --without-tools force hide of tools\n"));
413 print_term(_(" -f, --fullscreen start in full screen mode\n"));
414 print_term(_(" -s, --slideshow start in slideshow mode\n"));
415 print_term(_(" -l, --list open collection window for command line\n"));
416 print_term(_(" --geometry=GEOMETRY set main window location\n"));
417 print_term(_(" -r, --remote send following commands to open window\n"));
418 print_term(_(" -rh,--remote-help print remote command list\n"));
420 print_term(_(" --debug[=level] turn on debug output\n"));
422 print_term(_(" -v, --version print version info\n"));
423 print_term(_(" -h, --help show this message\n\n"));
426 /* these options are not officially supported!
427 * only for testing new features, no need to translate them */
428 print_term( " --alternate use alternate similarity algorithm\n");
435 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
442 parse_out_relatives(*path);
443 parse_out_relatives(*file);
446 list = g_list_reverse(list);
448 if (!*path && first_dir)
453 parse_out_relatives(*path);
461 GList *work = remote_errors;
463 printf_term(_("Invalid or ignored remote options: "));
466 gchar *opt = work->data;
468 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
472 printf_term(_("\nUse --remote-help for valid remote options.\n"));
475 remote_control(argv[0], remote_list, *path, list, *collection_list);
477 g_list_free(remote_list);
479 if (list && list->next)
490 static void parse_command_line_for_debug_option(int argc, char *argv[])
493 const gchar *debug_option = "--debug";
494 gint len = strlen(debug_option);
500 for (i = 1; i < argc; i++)
502 const gchar *cmd_line = argv[i];
503 if (strncmp(cmd_line, debug_option, len) == 0)
505 gint cmd_line_len = strlen(cmd_line);
507 /* we now increment the debug state for verbosity */
508 if (cmd_line_len == len)
510 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
512 gint n = atoi(cmd_line + len + 1);
520 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
525 *-----------------------------------------------------------------------------
526 * startup, init, and exit
527 *-----------------------------------------------------------------------------
530 #define RC_HISTORY_NAME "history"
532 static void keys_load(void)
536 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
537 history_list_load(path);
541 static void keys_save(void)
545 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
546 history_list_save(path);
550 static void check_for_home_path(gchar *path)
554 buf = g_build_filename(homedir(), path, NULL);
557 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
559 if (!mkdir_utf8(buf, 0755))
561 log_printf(_("Could not create dir:%s\n"), buf);
568 static void exit_program_final(void)
572 LayoutWindow *lw = NULL;
574 remote_close(remote_connection);
576 collect_manager_flush();
578 sync_options_with_current_state(options);
579 save_options(options);
582 path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
583 pathl = path_from_utf8(path);
584 gtk_accel_map_save(pathl);
588 if (layout_valid(&lw))
596 static GenericDialog *exit_dialog = NULL;
598 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
601 generic_dialog_close(gd);
604 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
607 generic_dialog_close(gd);
608 exit_program_final();
611 static gint exit_confirm_dlg(void)
619 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
623 if (!collection_window_modified_exists()) return FALSE;
627 if (layout_valid(&lw))
632 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
633 exit_dialog = generic_dialog_new(msg,
634 GQ_WMCLASS, "exit", parent, FALSE,
635 exit_confirm_cancel_cb, NULL);
637 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
638 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
639 msg, _("Collections have been modified. Quit anyway?"));
641 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
643 gtk_widget_show(exit_dialog->dialog);
648 void exit_program(void)
650 layout_image_full_screen_stop(NULL);
652 if (exit_confirm_dlg()) return;
654 exit_program_final();
657 int main(int argc, char *argv[])
661 gchar *cmd_path = NULL;
662 gchar *cmd_file = NULL;
663 GList *cmd_list = NULL;
664 GList *collection_list = NULL;
665 CollectionData *first_collection = NULL;
666 gchar *geometry = NULL;
669 CollectionData *cd = NULL;
671 /* init execution time counter (debug only) */
674 /* setup locale, i18n */
678 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
679 bind_textdomain_codeset(PACKAGE, "UTF-8");
683 /* setup random seed for random slideshow */
687 log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
690 /* register global notify functions */
691 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
692 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
694 parse_command_line_for_debug_option(argc, argv);
696 options = init_options(NULL);
697 setup_default_options(options);
698 load_options(options);
700 parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
702 /* If a gtkrc file exists in the rc directory, add it to the
703 * list of files to be parsed at the end of gtk_init() */
704 buf = g_build_filename(homedir(), GQ_RC_DIR, "gtkrc", NULL);
705 bufl = path_from_utf8(buf);
706 if (access(bufl, R_OK) == 0)
707 gtk_rc_add_default_file(bufl);
711 gtk_init(&argc, &argv);
713 if (gtk_major_version < GTK_MAJOR_VERSION ||
714 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
716 log_printf("!!! This is a friendly warning.\n");
717 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
718 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
719 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
720 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
723 check_for_home_path(GQ_RC_DIR);
724 check_for_home_path(GQ_RC_DIR_COLLECTIONS);
725 check_for_home_path(GQ_CACHE_RC_THUMB);
726 check_for_home_path(GQ_CACHE_RC_METADATA);
729 filter_add_defaults();
732 buf = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
733 bufl = path_from_utf8(buf);
734 gtk_accel_map_load(bufl);
744 filelist_free(cmd_list);
746 string_list_free(collection_list);
747 collection_list = NULL;
753 path = g_strdup(cmd_path);
755 else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
757 path = g_strdup(options->startup.path);
761 path = get_current_dir();
764 lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
765 layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
767 if (collection_list && !startup_command_line_collection)
771 work = collection_list;
780 cw = collection_window_new(path);
781 if (!first_collection && cw) first_collection = cw->cd;
786 (startup_command_line_collection && collection_list))
790 if (startup_command_line_collection)
794 cw = collection_window_new("");
799 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
805 cd->name = g_strdup(_("Command line"));
807 collection_path_changed(cd);
812 collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
816 work = collection_list;
819 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
823 layout_set_path(lw, path);
824 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
826 /* mem leak, we never unref this collection when !startup_command_line_collection
827 * (the image view of the main window does not hold a ref to the collection)
828 * this is sort of unavoidable, for if it did hold a ref, next/back
829 * may not work as expected when closing collection windows.
831 * collection_unref(cd);
837 layout_set_path(lw, cmd_file);
841 layout_set_path(lw, path);
842 if (first_collection)
844 layout_image_set_collection(lw, first_collection,
845 collection_get_first(first_collection));
849 image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
854 filelist_free(cmd_list);
855 string_list_free(collection_list);
858 if (startup_full_screen) layout_image_full_screen_start(lw);
859 if (startup_in_slideshow) layout_image_slideshow_start(lw);
861 buf = g_build_filename(homedir(), GQ_RC_DIR, ".command", NULL);
862 remote_connection = remote_server_init(buf, cd);