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!
19 #include "collect-io.h"
23 #include "filefilter.h"
24 #include "fullscreen.h"
25 #include "image-overlay.h"
29 #include "layout_image.h"
31 #include "pixbuf_util.h"
32 #include "preferences.h"
36 #include "slideshow.h"
38 #include "ui_bookmark.h"
40 #include "ui_fileops.h"
41 #include "ui_tabcomp.h"
42 #include "ui_utildlg.h"
45 #include <gdk/gdkkeysyms.h> /* for keyboard values */
54 static RemoteConnection *remote_connection = NULL;
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
64 gdouble get_zoom_increment(void)
66 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
69 gchar *utf8_validate_or_convert(gchar *text)
73 if (!text) return NULL;
76 if (!g_utf8_validate(text, len, NULL))
80 conv_text = g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
88 /* Borrowed from gtkfilesystemunix.c */
89 gchar *expand_tilde(const gchar *filename)
92 return g_strdup(filename);
98 if (filename[0] != '~')
99 return g_strdup(filename);
101 notilde = filename + 1;
102 slash = strchr(notilde, G_DIR_SEPARATOR);
103 if (slash == notilde || !*notilde)
105 home = g_get_home_dir();
107 return g_strdup(filename);
112 struct passwd *passwd;
115 username = g_strndup(notilde, slash - notilde);
117 username = g_strdup(notilde);
119 passwd = getpwnam(username);
123 return g_strdup(filename);
125 home = passwd->pw_dir;
129 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
131 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
137 *-----------------------------------------------------------------------------
139 *-----------------------------------------------------------------------------
142 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
144 static gint delta = 0;
145 static guint32 time_old = 0;
146 static guint keyval_old = 0;
148 if (event->state & GDK_CONTROL_MASK)
150 if (*x < 0) *x = G_MININT / 2;
151 if (*x > 0) *x = G_MAXINT / 2;
152 if (*y < 0) *y = G_MININT / 2;
153 if (*y > 0) *y = G_MAXINT / 2;
158 if (options->progressive_key_scrolling)
162 time_diff = event->time - time_old;
164 /* key pressed within 125ms ? (1/8 second) */
165 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
167 time_old = event->time;
168 keyval_old = event->keyval;
184 *-----------------------------------------------------------------------------
185 * command line parser (private) hehe, who needs popt anyway?
186 *-----------------------------------------------------------------------------
189 static gint startup_blank = FALSE;
190 static gint startup_full_screen = FALSE;
191 static gint startup_in_slideshow = FALSE;
192 static gint startup_command_line_collection = FALSE;
195 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
196 GList **list, GList **collection_list)
200 path_parsed = g_strdup(file_path);
201 parse_out_relatives(path_parsed);
203 if (file_extension_match(path_parsed, ".gqv"))
205 *collection_list = g_list_append(*collection_list, path_parsed);
209 if (!*path) *path = remove_level_from_path(path_parsed);
210 if (!*file) *file = g_strdup(path_parsed);
211 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
215 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
221 path_parsed = g_strdup(dir);
222 parse_out_relatives(path_parsed);
224 if (filelist_read(path_parsed, &files, NULL))
228 files = filelist_filter(files, FALSE);
229 files = filelist_sort_path(files);
234 FileData *fd = work->data;
235 if (!*path) *path = remove_level_from_path(fd->path);
236 if (!*file) *file = g_strdup(fd->path);
237 *list = g_list_prepend(*list, fd);
248 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
249 GList **list, gchar **first_dir)
252 if (!*list && !*first_dir)
254 *first_dir = g_strdup(dir);
260 parse_command_line_add_dir(*first_dir, path, file, list);
264 parse_command_line_add_dir(dir, path, file, list);
268 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
269 GList **list, GList **collection_list, gchar **first_dir)
274 parse_command_line_add_dir(*first_dir, path, file, list);
278 parse_command_line_add_file(file_path, path, file, list, collection_list);
281 static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,
282 GList **cmd_list, GList **collection_list,
286 GList *remote_list = NULL;
287 GList *remote_errors = NULL;
288 gint remote_do = FALSE;
289 gchar *first_dir = NULL;
294 gchar *base_dir = get_current_dir();
298 const gchar *cmd_line = argv[i];
299 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
301 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
303 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
305 else if (isdir(cmd_all))
307 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
309 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
311 parse_command_line_process_file(cmd_line, path, file,
312 &list, collection_list, &first_dir);
314 else if (isfile(cmd_all))
316 parse_command_line_process_file(cmd_all, path, file,
317 &list, collection_list, &first_dir);
319 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
321 /* do nothing but do not produce warnings */
323 else if (strcmp(cmd_line, "+t") == 0 ||
324 strcmp(cmd_line, "--with-tools") == 0)
326 options->layout.tools_float = FALSE;
327 options->layout.tools_hidden = FALSE;
329 remote_list = g_list_append(remote_list, "+t");
331 else if (strcmp(cmd_line, "-t") == 0 ||
332 strcmp(cmd_line, "--without-tools") == 0)
334 options->layout.tools_hidden = TRUE;
336 remote_list = g_list_append(remote_list, "-t");
338 else if (strcmp(cmd_line, "-f") == 0 ||
339 strcmp(cmd_line, "--fullscreen") == 0)
341 startup_full_screen = TRUE;
343 else if (strcmp(cmd_line, "-s") == 0 ||
344 strcmp(cmd_line, "--slideshow") == 0)
346 startup_in_slideshow = TRUE;
348 else if (strcmp(cmd_line, "-l") == 0 ||
349 strcmp(cmd_line, "--list") == 0)
351 startup_command_line_collection = TRUE;
353 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
355 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
357 else if (strcmp(cmd_line, "-r") == 0 ||
358 strcmp(cmd_line, "--remote") == 0)
363 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
366 else if (strcmp(cmd_line, "-rh") == 0 ||
367 strcmp(cmd_line, "--remote-help") == 0)
372 else if (strcmp(cmd_line, "--blank") == 0)
374 startup_blank = TRUE;
376 else if (strcmp(cmd_line, "-v") == 0 ||
377 strcmp(cmd_line, "--version") == 0)
379 printf_term("%s %s\n", GQ_APPNAME, VERSION);
382 else if (strcmp(cmd_line, "--alternate") == 0)
384 /* enable faster experimental algorithm */
385 log_printf("Alternate similarity algorithm enabled\n");
386 image_sim_alternate_set(TRUE);
388 else if (strcmp(cmd_line, "-h") == 0 ||
389 strcmp(cmd_line, "--help") == 0)
391 printf_term("%s %s\n", GQ_APPNAME, VERSION);
392 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
393 print_term(_("valid options are:\n"));
394 print_term(_(" +t, --with-tools force show of tools\n"));
395 print_term(_(" -t, --without-tools force hide of tools\n"));
396 print_term(_(" -f, --fullscreen start in full screen mode\n"));
397 print_term(_(" -s, --slideshow start in slideshow mode\n"));
398 print_term(_(" -l, --list open collection window for command line\n"));
399 print_term(_(" --geometry=GEOMETRY set main window location\n"));
400 print_term(_(" -r, --remote send following commands to open window\n"));
401 print_term(_(" -rh,--remote-help print remote command list\n"));
403 print_term(_(" --debug[=level] turn on debug output\n"));
405 print_term(_(" -v, --version print version info\n"));
406 print_term(_(" -h, --help show this message\n\n"));
409 /* these options are not officially supported!
410 * only for testing new features, no need to translate them */
411 print_term( " --alternate use alternate similarity algorithm\n");
418 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
425 parse_out_relatives(*path);
426 parse_out_relatives(*file);
429 list = g_list_reverse(list);
431 if (!*path && first_dir)
436 parse_out_relatives(*path);
444 GList *work = remote_errors;
446 printf_term(_("Invalid or ignored remote options: "));
449 gchar *opt = work->data;
451 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
455 printf_term(_("\nUse --remote-help for valid remote options.\n"));
458 remote_control(argv[0], remote_list, *path, list, *collection_list);
460 g_list_free(remote_list);
462 if (list && list->next)
473 static void parse_command_line_for_debug_option(int argc, char *argv[])
476 const gchar *debug_option = "--debug";
477 gint len = strlen(debug_option);
483 for (i = 1; i < argc; i++)
485 const gchar *cmd_line = argv[i];
486 if (strncmp(cmd_line, debug_option, len) == 0)
488 gint cmd_line_len = strlen(cmd_line);
490 /* we now increment the debug state for verbosity */
491 if (cmd_line_len == len)
493 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
495 gint n = atoi(cmd_line + len + 1);
503 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
508 *-----------------------------------------------------------------------------
509 * startup, init, and exit
510 *-----------------------------------------------------------------------------
513 #define RC_HISTORY_NAME "history"
515 static void keys_load(void)
519 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
520 history_list_load(path);
524 static void keys_save(void)
528 path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
529 history_list_save(path);
533 static void check_for_home_path(gchar *path)
537 buf = g_build_filename(homedir(), path, NULL);
540 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
542 if (!mkdir_utf8(buf, 0755))
544 log_printf(_("Could not create dir:%s\n"), buf);
550 static void setup_default_options(void)
555 for (i = 0; i < GQ_EDITOR_SLOTS; i++)
557 options->editor[i].name = NULL;
558 options->editor[i].command = NULL;
561 editor_reset_defaults();
563 bookmark_add_default(_("Home"), homedir());
564 path = g_build_filename(homedir(), "Desktop", NULL);
565 bookmark_add_default(_("Desktop"), path);
567 path = g_build_filename(homedir(), GQ_RC_DIR_COLLECTIONS, NULL);
568 bookmark_add_default(_("Collections"), path);
571 g_free(options->file_ops.safe_delete_path);
572 options->file_ops.safe_delete_path = g_build_filename(homedir(), GQ_RC_DIR_TRASH, NULL);
574 for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
576 options->color_profile.input_file[i] = NULL;
577 options->color_profile.input_name[i] = NULL;
580 set_default_image_overlay_template_string(&options->image_overlay.common.template_string);
581 sidecar_ext_add_defaults();
582 options->layout.order = g_strdup("123");
583 options->properties.tabs_order = g_strdup(info_tab_default_order());
585 options->shell.path = g_strdup(GQ_DEFAULT_SHELL_PATH);
586 options->shell.options = g_strdup(GQ_DEFAULT_SHELL_OPTIONS);
588 for (i = 0; ExifUIList[i].key; i++)
589 ExifUIList[i].current = ExifUIList[i].default_value;
592 static void exit_program_final(void)
596 LayoutWindow *lw = NULL;
598 remote_close(remote_connection);
600 collect_manager_flush();
602 if (layout_valid(&lw))
604 options->layout.main_window.maximized = window_maximized(lw->window);
605 if (!options->layout.main_window.maximized)
607 layout_geometry_get(NULL, &options->layout.main_window.x, &options->layout.main_window.y,
608 &options->layout.main_window.w, &options->layout.main_window.h);
611 options->image_overlay.common.state = image_osd_get(lw->image);
614 layout_geometry_get_dividers(NULL, &options->layout.main_window.hdivider_pos, &options->layout.main_window.vdivider_pos);
616 layout_views_get(NULL, &options->layout.dir_view_type, &options->layout.file_view_type);
618 options->layout.show_thumbnails = layout_thumb_get(NULL);
619 options->layout.show_marks = layout_marks_get(NULL);
621 layout_sort_get(NULL, &options->file_sort.method, &options->file_sort.ascending);
623 layout_geometry_get_tools(NULL, &options->layout.float_window.x, &options->layout.float_window.y,
624 &options->layout.float_window.w, &options->layout.float_window.h, &options->layout.float_window.vdivider_pos);
625 layout_tools_float_get(NULL, &options->layout.tools_float, &options->layout.tools_hidden);
626 options->layout.toolbar_hidden = layout_toolbar_hidden(NULL);
628 options->color_profile.enabled = layout_image_color_profile_get_use(NULL);
629 layout_image_color_profile_get(NULL,
630 &options->color_profile.input_type,
631 &options->color_profile.screen_type,
632 &options->color_profile.use_image);
634 if (options->startup.restore_path && options->startup.use_last_path)
636 g_free(options->startup.path);
637 options->startup.path = g_strdup(layout_get_path(NULL));
643 path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
644 pathl = path_from_utf8(path);
645 gtk_accel_map_save(pathl);
652 static GenericDialog *exit_dialog = NULL;
654 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
657 generic_dialog_close(gd);
660 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
663 generic_dialog_close(gd);
664 exit_program_final();
667 static gint exit_confirm_dlg(void)
675 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
679 if (!collection_window_modified_exists()) return FALSE;
683 if (layout_valid(&lw))
688 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
689 exit_dialog = generic_dialog_new(msg,
690 GQ_WMCLASS, "exit", parent, FALSE,
691 exit_confirm_cancel_cb, NULL);
693 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
694 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
695 msg, _("Collections have been modified. Quit anyway?"));
697 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
699 gtk_widget_show(exit_dialog->dialog);
704 void exit_program(void)
706 layout_image_full_screen_stop(NULL);
708 if (exit_confirm_dlg()) return;
710 exit_program_final();
713 int main(int argc, char *argv[])
717 gchar *cmd_path = NULL;
718 gchar *cmd_file = NULL;
719 GList *cmd_list = NULL;
720 GList *collection_list = NULL;
721 CollectionData *first_collection = NULL;
722 gchar *geometry = NULL;
725 CollectionData *cd = NULL;
727 /* init execution time counter (debug only) */
730 /* setup locale, i18n */
734 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
735 bind_textdomain_codeset(PACKAGE, "UTF-8");
739 /* setup random seed for random slideshow */
743 log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
745 parse_command_line_for_debug_option(argc, argv);
747 options = init_options(NULL);
748 setup_default_options();
751 parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
753 gtk_init(&argc, &argv);
755 if (gtk_major_version < GTK_MAJOR_VERSION ||
756 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
758 log_printf("!!! This is a friendly warning.\n");
759 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
760 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
761 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
762 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
765 check_for_home_path(GQ_RC_DIR);
766 check_for_home_path(GQ_RC_DIR_COLLECTIONS);
767 check_for_home_path(GQ_CACHE_RC_THUMB);
768 check_for_home_path(GQ_CACHE_RC_METADATA);
771 filter_add_defaults();
774 buf = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
775 bufl = path_from_utf8(buf);
776 gtk_accel_map_load(bufl);
786 filelist_free(cmd_list);
788 string_list_free(collection_list);
789 collection_list = NULL;
795 path = g_strdup(cmd_path);
797 else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
799 path = g_strdup(options->startup.path);
803 path = get_current_dir();
806 lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
807 layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
809 if (collection_list && !startup_command_line_collection)
813 work = collection_list;
822 cw = collection_window_new(path);
823 if (!first_collection && cw) first_collection = cw->cd;
828 (startup_command_line_collection && collection_list))
832 if (startup_command_line_collection)
836 cw = collection_window_new("");
841 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
847 cd->name = g_strdup(_("Command line"));
849 collection_path_changed(cd);
854 collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
858 work = collection_list;
861 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
865 layout_set_path(lw, path);
866 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
868 /* mem leak, we never unref this collection when !startup_command_line_collection
869 * (the image view of the main window does not hold a ref to the collection)
870 * this is sort of unavoidable, for if it did hold a ref, next/back
871 * may not work as expected when closing collection windows.
873 * collection_unref(cd);
879 layout_set_path(lw, cmd_file);
883 layout_set_path(lw, path);
884 if (first_collection)
886 layout_image_set_collection(lw, first_collection,
887 collection_get_first(first_collection));
891 image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
896 filelist_free(cmd_list);
897 string_list_free(collection_list);
900 if (startup_full_screen) layout_image_full_screen_start(lw);
901 if (startup_in_slideshow) layout_image_slideshow_start(lw);
903 buf = g_build_filename(homedir(), GQ_RC_DIR, ".command", NULL);
904 remote_connection = remote_server_init(buf, cd);