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"
31 #include <gdk/gdkkeysyms.h> /* for keyboard values */
40 static RemoteConnection *remote_connection = NULL;
44 *-----------------------------------------------------------------------------
46 *-----------------------------------------------------------------------------
50 gdouble get_zoom_increment(void)
52 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
55 gchar *utf8_validate_or_convert(const gchar *text)
59 if (!text) return NULL;
62 if (!g_utf8_validate(text, len, NULL))
63 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
65 return g_strdup(text);
68 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
70 gchar *s1_key, *s2_key;
74 g_assert(g_utf8_validate(s1, -1, NULL));
75 g_assert(g_utf8_validate(s2, -1, NULL));
79 s1_t = g_utf8_casefold(s1, -1);
80 s2_t = g_utf8_casefold(s2, -1);
83 s1_key = g_utf8_collate_key(s1_t, -1);
84 s2_key = g_utf8_collate_key(s2_t, -1);
86 ret = strcmp(s1_key, s2_key);
100 /* Borrowed from gtkfilesystemunix.c */
101 gchar *expand_tilde(const gchar *filename)
104 return g_strdup(filename);
110 if (filename[0] != '~')
111 return g_strdup(filename);
113 notilde = filename + 1;
114 slash = strchr(notilde, G_DIR_SEPARATOR);
115 if (slash == notilde || !*notilde)
117 home = g_get_home_dir();
119 return g_strdup(filename);
124 struct passwd *passwd;
127 username = g_strndup(notilde, slash - notilde);
129 username = g_strdup(notilde);
131 passwd = getpwnam(username);
135 return g_strdup(filename);
137 home = passwd->pw_dir;
141 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
143 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
149 *-----------------------------------------------------------------------------
151 *-----------------------------------------------------------------------------
154 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
156 static gint delta = 0;
157 static guint32 time_old = 0;
158 static guint keyval_old = 0;
160 if (event->state & GDK_CONTROL_MASK)
162 if (*x < 0) *x = G_MININT / 2;
163 if (*x > 0) *x = G_MAXINT / 2;
164 if (*y < 0) *y = G_MININT / 2;
165 if (*y > 0) *y = G_MAXINT / 2;
170 if (options->progressive_key_scrolling)
174 time_diff = event->time - time_old;
176 /* key pressed within 125ms ? (1/8 second) */
177 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
179 time_old = event->time;
180 keyval_old = event->keyval;
196 *-----------------------------------------------------------------------------
197 * command line parser (private) hehe, who needs popt anyway?
198 *-----------------------------------------------------------------------------
201 static gint startup_blank = FALSE;
202 static gint startup_full_screen = FALSE;
203 static gint startup_in_slideshow = FALSE;
204 static gint startup_command_line_collection = FALSE;
207 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
208 GList **list, GList **collection_list)
212 path_parsed = g_strdup(file_path);
213 parse_out_relatives(path_parsed);
215 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
217 *collection_list = g_list_append(*collection_list, path_parsed);
221 if (!*path) *path = remove_level_from_path(path_parsed);
222 if (!*file) *file = g_strdup(path_parsed);
223 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
227 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
234 path_parsed = g_strdup(dir);
235 parse_out_relatives(path_parsed);
236 dir_fd = file_data_new_simple(path_parsed);
239 if (filelist_read(dir_fd, &files, NULL))
243 files = filelist_filter(files, FALSE);
244 files = filelist_sort_path(files);
249 FileData *fd = work->data;
250 if (!*path) *path = remove_level_from_path(fd->path);
251 if (!*file) *file = g_strdup(fd->path);
252 *list = g_list_prepend(*list, fd);
261 file_data_unref(dir_fd);
264 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
265 GList **list, gchar **first_dir)
268 if (!*list && !*first_dir)
270 *first_dir = g_strdup(dir);
276 parse_command_line_add_dir(*first_dir, path, file, list);
280 parse_command_line_add_dir(dir, path, file, list);
284 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
285 GList **list, GList **collection_list, gchar **first_dir)
290 parse_command_line_add_dir(*first_dir, path, file, list);
294 parse_command_line_add_file(file_path, path, file, list, collection_list);
297 static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,
298 GList **cmd_list, GList **collection_list,
302 GList *remote_list = NULL;
303 GList *remote_errors = NULL;
304 gint remote_do = FALSE;
305 gchar *first_dir = NULL;
310 gchar *base_dir = get_current_dir();
314 const gchar *cmd_line = argv[i];
315 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
317 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
319 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
321 else if (isdir(cmd_all))
323 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
325 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
327 parse_command_line_process_file(cmd_line, path, file,
328 &list, collection_list, &first_dir);
330 else if (isfile(cmd_all))
332 parse_command_line_process_file(cmd_all, path, file,
333 &list, collection_list, &first_dir);
335 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
337 /* do nothing but do not produce warnings */
339 else if (strcmp(cmd_line, "+t") == 0 ||
340 strcmp(cmd_line, "--with-tools") == 0)
342 options->layout.tools_float = FALSE;
343 options->layout.tools_hidden = FALSE;
345 remote_list = g_list_append(remote_list, "+t");
347 else if (strcmp(cmd_line, "-t") == 0 ||
348 strcmp(cmd_line, "--without-tools") == 0)
350 options->layout.tools_hidden = TRUE;
352 remote_list = g_list_append(remote_list, "-t");
354 else if (strcmp(cmd_line, "-f") == 0 ||
355 strcmp(cmd_line, "--fullscreen") == 0)
357 startup_full_screen = TRUE;
359 else if (strcmp(cmd_line, "-s") == 0 ||
360 strcmp(cmd_line, "--slideshow") == 0)
362 startup_in_slideshow = TRUE;
364 else if (strcmp(cmd_line, "-l") == 0 ||
365 strcmp(cmd_line, "--list") == 0)
367 startup_command_line_collection = TRUE;
369 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
371 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
373 else if (strcmp(cmd_line, "-r") == 0 ||
374 strcmp(cmd_line, "--remote") == 0)
379 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
382 else if (strcmp(cmd_line, "-rh") == 0 ||
383 strcmp(cmd_line, "--remote-help") == 0)
388 else if (strcmp(cmd_line, "--blank") == 0)
390 startup_blank = TRUE;
392 else if (strcmp(cmd_line, "-v") == 0 ||
393 strcmp(cmd_line, "--version") == 0)
395 printf_term("%s %s\n", GQ_APPNAME, VERSION);
398 else if (strcmp(cmd_line, "--alternate") == 0)
400 /* enable faster experimental algorithm */
401 log_printf("Alternate similarity algorithm enabled\n");
402 image_sim_alternate_set(TRUE);
404 else if (strcmp(cmd_line, "-h") == 0 ||
405 strcmp(cmd_line, "--help") == 0)
407 printf_term("%s %s\n", GQ_APPNAME, VERSION);
408 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
409 print_term(_("valid options are:\n"));
410 print_term(_(" +t, --with-tools force show of tools\n"));
411 print_term(_(" -t, --without-tools force hide of tools\n"));
412 print_term(_(" -f, --fullscreen start in full screen mode\n"));
413 print_term(_(" -s, --slideshow start in slideshow mode\n"));
414 print_term(_(" -l, --list open collection window for command line\n"));
415 print_term(_(" --geometry=GEOMETRY set main window location\n"));
416 print_term(_(" -r, --remote send following commands to open window\n"));
417 print_term(_(" -rh,--remote-help print remote command list\n"));
419 print_term(_(" --debug[=level] turn on debug output\n"));
421 print_term(_(" -v, --version print version info\n"));
422 print_term(_(" -h, --help show this message\n\n"));
425 /* these options are not officially supported!
426 * only for testing new features, no need to translate them */
427 print_term( " --alternate use alternate similarity algorithm\n");
434 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
441 parse_out_relatives(*path);
442 parse_out_relatives(*file);
445 list = g_list_reverse(list);
447 if (!*path && first_dir)
452 parse_out_relatives(*path);
460 GList *work = remote_errors;
462 printf_term(_("Invalid or ignored remote options: "));
465 gchar *opt = work->data;
467 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
471 printf_term(_("\nUse --remote-help for valid remote options.\n"));
474 remote_control(argv[0], remote_list, *path, list, *collection_list);
476 g_list_free(remote_list);
478 if (list && list->next)
489 static void parse_command_line_for_debug_option(int argc, char *argv[])
492 const gchar *debug_option = "--debug";
493 gint len = strlen(debug_option);
499 for (i = 1; i < argc; i++)
501 const gchar *cmd_line = argv[i];
502 if (strncmp(cmd_line, debug_option, len) == 0)
504 gint cmd_line_len = strlen(cmd_line);
506 /* we now increment the debug state for verbosity */
507 if (cmd_line_len == len)
509 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
511 gint n = atoi(cmd_line + len + 1);
519 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
524 *-----------------------------------------------------------------------------
525 * startup, init, and exit
526 *-----------------------------------------------------------------------------
529 #define RC_HISTORY_NAME "history"
531 static void keys_load(void)
535 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
536 history_list_load(path);
540 static void keys_save(void)
544 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
545 history_list_save(path);
549 static void check_for_home_path(gchar *path)
553 buf = g_build_filename(homedir(), path, NULL);
556 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
558 if (!mkdir_utf8(buf, 0755))
560 log_printf(_("Could not create dir:%s\n"), buf);
567 static void exit_program_final(void)
572 remote_close(remote_connection);
574 collect_manager_flush();
576 sync_options_with_current_state(options);
577 save_options(options);
580 path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
581 pathl = path_from_utf8(path);
582 gtk_accel_map_save(pathl);
589 static GenericDialog *exit_dialog = NULL;
591 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
594 generic_dialog_close(gd);
597 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
600 generic_dialog_close(gd);
601 exit_program_final();
604 static gint exit_confirm_dlg(void)
612 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
616 if (!collection_window_modified_exists()) return FALSE;
620 if (layout_valid(&lw))
625 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
626 exit_dialog = generic_dialog_new(msg,
627 GQ_WMCLASS, "exit", parent, FALSE,
628 exit_confirm_cancel_cb, NULL);
630 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
631 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
632 msg, _("Collections have been modified. Quit anyway?"));
634 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
636 gtk_widget_show(exit_dialog->dialog);
641 void exit_program(void)
643 layout_image_full_screen_stop(NULL);
645 if (exit_confirm_dlg()) return;
647 exit_program_final();
650 int main(int argc, char *argv[])
654 gchar *cmd_path = NULL;
655 gchar *cmd_file = NULL;
656 GList *cmd_list = NULL;
657 GList *collection_list = NULL;
658 CollectionData *first_collection = NULL;
659 gchar *geometry = NULL;
662 CollectionData *cd = NULL;
664 /* init execution time counter (debug only) */
667 /* setup locale, i18n */
671 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
672 bind_textdomain_codeset(PACKAGE, "UTF-8");
676 /* setup random seed for random slideshow */
680 log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
682 parse_command_line_for_debug_option(argc, argv);
684 options = init_options(NULL);
685 setup_default_options(options);
686 load_options(options);
688 parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
690 /* If a gtkrc file exists in the rc directory, add it to the
691 * list of files to be parsed at the end of gtk_init() */
692 buf = g_build_filename(homedir(), GQ_RC_DIR, "gtkrc", NULL);
693 bufl = path_from_utf8(buf);
694 if (access(bufl, R_OK) == 0)
695 gtk_rc_add_default_file(bufl);
699 gtk_init(&argc, &argv);
701 if (gtk_major_version < GTK_MAJOR_VERSION ||
702 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
704 log_printf("!!! This is a friendly warning.\n");
705 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
706 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
707 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
708 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
711 check_for_home_path(GQ_RC_DIR);
712 check_for_home_path(GQ_RC_DIR_COLLECTIONS);
713 check_for_home_path(GQ_CACHE_RC_THUMB);
714 check_for_home_path(GQ_CACHE_RC_METADATA);
717 filter_add_defaults();
720 buf = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
721 bufl = path_from_utf8(buf);
722 gtk_accel_map_load(bufl);
732 filelist_free(cmd_list);
734 string_list_free(collection_list);
735 collection_list = NULL;
741 path = g_strdup(cmd_path);
743 else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
745 path = g_strdup(options->startup.path);
749 path = get_current_dir();
752 lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
753 layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
755 if (collection_list && !startup_command_line_collection)
759 work = collection_list;
768 cw = collection_window_new(path);
769 if (!first_collection && cw) first_collection = cw->cd;
774 (startup_command_line_collection && collection_list))
778 if (startup_command_line_collection)
782 cw = collection_window_new("");
787 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
793 cd->name = g_strdup(_("Command line"));
795 collection_path_changed(cd);
800 collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
804 work = collection_list;
807 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
811 layout_set_path(lw, path);
812 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
814 /* mem leak, we never unref this collection when !startup_command_line_collection
815 * (the image view of the main window does not hold a ref to the collection)
816 * this is sort of unavoidable, for if it did hold a ref, next/back
817 * may not work as expected when closing collection windows.
819 * collection_unref(cd);
825 layout_set_path(lw, cmd_file);
829 layout_set_path(lw, path);
830 if (first_collection)
832 layout_image_set_collection(lw, first_collection,
833 collection_get_first(first_collection));
837 image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
842 filelist_free(cmd_list);
843 string_list_free(collection_list);
846 if (startup_full_screen) layout_image_full_screen_start(lw);
847 if (startup_in_slideshow) layout_image_slideshow_start(lw);
849 buf = g_build_filename(homedir(), GQ_RC_DIR, ".command", NULL);
850 remote_connection = remote_server_init(buf, cd);