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"
22 #include "filefilter.h"
23 #include "fullscreen.h"
24 #include "image-overlay.h"
27 #include "layout_image.h"
29 #include "pixbuf_util.h"
30 #include "preferences.h"
34 #include "slideshow.h"
36 #include "ui_bookmark.h"
38 #include "ui_fileops.h"
39 #include "ui_tabcomp.h"
40 #include "ui_utildlg.h"
43 #include <gdk/gdkkeysyms.h> /* for keyboard values */
52 static RemoteConnection *remote_connection = NULL;
56 *-----------------------------------------------------------------------------
58 *-----------------------------------------------------------------------------
62 gdouble get_zoom_increment(void)
64 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
67 gchar *utf8_validate_or_convert(gchar *text)
71 if (!text) return NULL;
74 if (!g_utf8_validate(text, len, NULL))
78 conv_text = g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
86 /* Borrowed from gtkfilesystemunix.c */
87 gchar *expand_tilde(const gchar *filename)
90 return g_strdup(filename);
96 if (filename[0] != '~')
97 return g_strdup(filename);
99 notilde = filename + 1;
100 slash = strchr(notilde, G_DIR_SEPARATOR);
101 if (slash == notilde || !*notilde)
103 home = g_get_home_dir();
105 return g_strdup(filename);
110 struct passwd *passwd;
113 username = g_strndup(notilde, slash - notilde);
115 username = g_strdup(notilde);
117 passwd = getpwnam(username);
121 return g_strdup(filename);
123 home = passwd->pw_dir;
127 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
129 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
135 *-----------------------------------------------------------------------------
137 *-----------------------------------------------------------------------------
140 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
142 static gint delta = 0;
143 static guint32 time_old = 0;
144 static guint keyval_old = 0;
146 if (event->state & GDK_CONTROL_MASK)
148 if (*x < 0) *x = G_MININT / 2;
149 if (*x > 0) *x = G_MAXINT / 2;
150 if (*y < 0) *y = G_MININT / 2;
151 if (*y > 0) *y = G_MAXINT / 2;
156 if (options->progressive_key_scrolling)
160 time_diff = event->time - time_old;
162 /* key pressed within 125ms ? (1/8 second) */
163 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
165 time_old = event->time;
166 keyval_old = event->keyval;
182 *-----------------------------------------------------------------------------
183 * command line parser (private) hehe, who needs popt anyway?
184 *-----------------------------------------------------------------------------
187 static gint startup_blank = FALSE;
188 static gint startup_full_screen = FALSE;
189 static gint startup_in_slideshow = FALSE;
190 static gint startup_command_line_collection = FALSE;
193 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
194 GList **list, GList **collection_list)
198 path_parsed = g_strdup(file_path);
199 parse_out_relatives(path_parsed);
201 if (file_extension_match(path_parsed, ".gqv"))
203 *collection_list = g_list_append(*collection_list, path_parsed);
207 if (!*path) *path = remove_level_from_path(path_parsed);
208 if (!*file) *file = g_strdup(path_parsed);
209 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
213 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
219 path_parsed = g_strdup(dir);
220 parse_out_relatives(path_parsed);
222 if (filelist_read(path_parsed, &files, NULL))
226 files = filelist_filter(files, FALSE);
227 files = filelist_sort_path(files);
232 FileData *fd = work->data;
233 if (!*path) *path = remove_level_from_path(fd->path);
234 if (!*file) *file = g_strdup(fd->path);
235 *list = g_list_prepend(*list, fd);
246 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
247 GList **list, gchar **first_dir)
250 if (!*list && !*first_dir)
252 *first_dir = g_strdup(dir);
258 parse_command_line_add_dir(*first_dir, path, file, list);
262 parse_command_line_add_dir(dir, path, file, list);
266 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
267 GList **list, GList **collection_list, gchar **first_dir)
272 parse_command_line_add_dir(*first_dir, path, file, list);
276 parse_command_line_add_file(file_path, path, file, list, collection_list);
279 static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,
280 GList **cmd_list, GList **collection_list,
284 GList *remote_list = NULL;
285 GList *remote_errors = NULL;
286 gint remote_do = FALSE;
287 gchar *first_dir = NULL;
292 gchar *base_dir = get_current_dir();
296 const gchar *cmd_line = argv[i];
297 gchar *cmd_all = concat_dir_and_file(base_dir, cmd_line);
299 if (cmd_line[0] == '/' && isdir(cmd_line))
301 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
303 else if (isdir(cmd_all))
305 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
307 else if (cmd_line[0] == '/' && isfile(cmd_line))
309 parse_command_line_process_file(cmd_line, path, file,
310 &list, collection_list, &first_dir);
312 else if (isfile(cmd_all))
314 parse_command_line_process_file(cmd_all, path, file,
315 &list, collection_list, &first_dir);
317 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
319 /* do nothing but do not produce warnings */
321 else if (strcmp(cmd_line, "+t") == 0 ||
322 strcmp(cmd_line, "--with-tools") == 0)
324 options->layout.tools_float = FALSE;
325 options->layout.tools_hidden = FALSE;
327 remote_list = g_list_append(remote_list, "+t");
329 else if (strcmp(cmd_line, "-t") == 0 ||
330 strcmp(cmd_line, "--without-tools") == 0)
332 options->layout.tools_hidden = TRUE;
334 remote_list = g_list_append(remote_list, "-t");
336 else if (strcmp(cmd_line, "-f") == 0 ||
337 strcmp(cmd_line, "--fullscreen") == 0)
339 startup_full_screen = TRUE;
341 else if (strcmp(cmd_line, "-s") == 0 ||
342 strcmp(cmd_line, "--slideshow") == 0)
344 startup_in_slideshow = TRUE;
346 else if (strcmp(cmd_line, "-l") == 0 ||
347 strcmp(cmd_line, "--list") == 0)
349 startup_command_line_collection = TRUE;
351 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
353 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
355 else if (strcmp(cmd_line, "-r") == 0 ||
356 strcmp(cmd_line, "--remote") == 0)
361 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
364 else if (strcmp(cmd_line, "-rh") == 0 ||
365 strcmp(cmd_line, "--remote-help") == 0)
370 else if (strcmp(cmd_line, "--blank") == 0)
372 startup_blank = TRUE;
374 else if (strcmp(cmd_line, "-v") == 0 ||
375 strcmp(cmd_line, "--version") == 0)
377 log_printf("%s %s\n", GQ_APPNAME, VERSION);
380 else if (strcmp(cmd_line, "--alternate") == 0)
382 /* enable faster experimental algorithm */
383 log_printf("Alternate similarity algorithm enabled\n");
384 image_sim_alternate_set(TRUE);
386 else if (strcmp(cmd_line, "-h") == 0 ||
387 strcmp(cmd_line, "--help") == 0)
389 log_printf("%s %s\n", GQ_APPNAME, VERSION);
390 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
391 print_term(_("valid options are:\n"));
392 print_term(_(" +t, --with-tools force show of tools\n"));
393 print_term(_(" -t, --without-tools force hide of tools\n"));
394 print_term(_(" -f, --fullscreen start in full screen mode\n"));
395 print_term(_(" -s, --slideshow start in slideshow mode\n"));
396 print_term(_(" -l, --list open collection window for command line\n"));
397 print_term(_(" --geometry=GEOMETRY set main window location\n"));
398 print_term(_(" -r, --remote send following commands to open window\n"));
399 print_term(_(" -rh,--remote-help print remote command list\n"));
401 print_term(_(" --debug[=level] turn on debug output\n"));
403 print_term(_(" -v, --version print version info\n"));
404 print_term(_(" -h, --help show this message\n\n"));
407 /* these options are not officially supported!
408 * only for testing new features, no need to translate them */
409 print_term( " --alternate use alternate similarity algorithm\n");
416 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
423 parse_out_relatives(*path);
424 parse_out_relatives(*file);
427 list = g_list_reverse(list);
429 if (!*path && first_dir)
434 parse_out_relatives(*path);
442 GList *work = remote_errors;
444 printf_term(_("Invalid or ignored remote options: "));
447 gchar *opt = work->data;
449 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
453 printf_term(_("\nUse --remote-help for valid remote options.\n"));
456 remote_control(argv[0], remote_list, *path, list, *collection_list);
458 g_list_free(remote_list);
460 if (list && list->next)
471 static void parse_command_line_for_debug_option(int argc, char *argv[])
474 const gchar *debug_option = "--debug";
475 gint len = strlen(debug_option);
481 for (i = 1; i < argc; i++)
483 const gchar *cmd_line = argv[i];
484 if (strncmp(cmd_line, debug_option, len) == 0)
486 gint cmd_line_len = strlen(cmd_line);
488 /* we now increment the debug state for verbosity */
489 if (cmd_line_len == len)
491 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
493 gint n = atoi(cmd_line + len + 1);
501 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
506 *-----------------------------------------------------------------------------
507 * startup, init, and exit
508 *-----------------------------------------------------------------------------
511 #define RC_HISTORY_NAME "history"
513 static void keys_load(void)
517 path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/", RC_HISTORY_NAME, NULL);
518 history_list_load(path);
522 static void keys_save(void)
526 path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/", RC_HISTORY_NAME, NULL);
527 history_list_save(path);
531 static void check_for_home_path(gchar *path)
535 buf = g_strconcat(homedir(), "/", path, NULL);
538 printf_term(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
540 if (!mkdir_utf8(buf, 0755))
542 printf_term(_("Could not create dir:%s\n"), buf);
548 static void setup_default_options(void)
553 for (i = 0; i < GQ_EDITOR_SLOTS; i++)
555 options->editor_name[i] = NULL;
556 options->editor_command[i] = NULL;
559 editor_reset_defaults();
561 bookmark_add_default(_("Home"), homedir());
562 path = concat_dir_and_file(homedir(), "Desktop");
563 bookmark_add_default(_("Desktop"), path);
565 path = concat_dir_and_file(homedir(), GQ_RC_DIR_COLLECTIONS);
566 bookmark_add_default(_("Collections"), path);
569 g_free(options->file_ops.safe_delete_path);
570 options->file_ops.safe_delete_path = concat_dir_and_file(homedir(), GQ_RC_DIR_TRASH);
572 for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
574 options->color_profile.input_file[i] = NULL;
575 options->color_profile.input_name[i] = NULL;
578 set_default_image_overlay_template_string(options);
579 sidecar_ext_add_defaults();
580 options->layout.order = g_strdup("123");
583 static void exit_program_final(void)
587 LayoutWindow *lw = NULL;
589 remote_close(remote_connection);
591 collect_manager_flush();
593 if (layout_valid(&lw))
595 options->layout.main_window.maximized = window_maximized(lw->window);
596 if (!options->layout.main_window.maximized)
598 layout_geometry_get(NULL, &options->layout.main_window.x, &options->layout.main_window.y,
599 &options->layout.main_window.w, &options->layout.main_window.h);
602 options->image_overlay.common.state = image_osd_get(lw->image);
605 layout_geometry_get_dividers(NULL, &options->layout.main_window.hdivider_pos, &options->layout.main_window.vdivider_pos);
607 layout_views_get(NULL, &options->layout.dir_view_type, &options->layout.file_view_type);
609 options->layout.show_thumbnails = layout_thumb_get(NULL);
610 options->layout.show_marks = layout_marks_get(NULL);
612 layout_sort_get(NULL, &options->file_sort.method, &options->file_sort.ascending);
614 layout_geometry_get_tools(NULL, &options->layout.float_window.x, &options->layout.float_window.y,
615 &options->layout.float_window.w, &options->layout.float_window.h, &options->layout.float_window.vdivider_pos);
616 layout_tools_float_get(NULL, &options->layout.tools_float, &options->layout.tools_hidden);
617 options->layout.toolbar_hidden = layout_toolbar_hidden(NULL);
619 options->color_profile.enabled = layout_image_color_profile_get_use(NULL);
620 layout_image_color_profile_get(NULL,
621 &options->color_profile.input_type,
622 &options->color_profile.screen_type,
623 &options->color_profile.use_image);
625 if (options->startup.restore_path && options->startup.use_last_path)
627 g_free(options->startup.path);
628 options->startup.path = g_strdup(layout_get_path(NULL));
634 path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/accels", NULL);
635 pathl = path_from_utf8(path);
636 gtk_accel_map_save(pathl);
643 static GenericDialog *exit_dialog = NULL;
645 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
648 generic_dialog_close(gd);
651 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
654 generic_dialog_close(gd);
655 exit_program_final();
658 static gint exit_confirm_dlg(void)
666 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
670 if (!collection_window_modified_exists()) return FALSE;
674 if (layout_valid(&lw))
679 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
680 exit_dialog = generic_dialog_new(msg,
681 GQ_WMCLASS, "exit", parent, FALSE,
682 exit_confirm_cancel_cb, NULL);
684 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
685 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
686 msg, _("Collections have been modified. Quit anyway?"));
688 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
690 gtk_widget_show(exit_dialog->dialog);
695 void exit_program(void)
697 layout_image_full_screen_stop(NULL);
699 if (exit_confirm_dlg()) return;
701 exit_program_final();
704 int main(int argc, char *argv[])
708 gchar *cmd_path = NULL;
709 gchar *cmd_file = NULL;
710 GList *cmd_list = NULL;
711 GList *collection_list = NULL;
712 CollectionData *first_collection = NULL;
713 gchar *geometry = NULL;
716 CollectionData *cd = NULL;
718 /* init execution time counter (debug only) */
721 /* setup locale, i18n */
723 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
724 bind_textdomain_codeset(PACKAGE, "UTF-8");
727 /* setup random seed for random slideshow */
731 log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
733 parse_command_line_for_debug_option(argc, argv);
735 options = init_options(NULL);
736 setup_default_options();
739 parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
741 gtk_init(&argc, &argv);
743 if (gtk_major_version < GTK_MAJOR_VERSION ||
744 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
746 printf_term("!!! This is a friendly warning.\n");
747 printf_term("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
748 printf_term("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
749 printf_term("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
750 printf_term("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
753 check_for_home_path(GQ_RC_DIR);
754 check_for_home_path(GQ_RC_DIR_COLLECTIONS);
755 check_for_home_path(GQ_CACHE_RC_THUMB);
756 check_for_home_path(GQ_CACHE_RC_METADATA);
759 filter_add_defaults();
762 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/accels", NULL);
763 bufl = path_from_utf8(buf);
764 gtk_accel_map_load(bufl);
774 filelist_free(cmd_list);
776 string_list_free(collection_list);
777 collection_list = NULL;
783 path = g_strdup(cmd_path);
785 else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
787 path = g_strdup(options->startup.path);
791 path = get_current_dir();
794 lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
795 layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
797 if (collection_list && !startup_command_line_collection)
801 work = collection_list;
810 cw = collection_window_new(path);
811 if (!first_collection && cw) first_collection = cw->cd;
816 (startup_command_line_collection && collection_list))
820 if (startup_command_line_collection)
824 cw = collection_window_new("");
829 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
835 cd->name = g_strdup(_("Command line"));
837 collection_path_changed(cd);
842 collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
846 work = collection_list;
849 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
853 layout_set_path(lw, path);
854 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
856 /* mem leak, we never unref this collection when !startup_command_line_collection
857 * (the image view of the main window does not hold a ref to the collection)
858 * this is sort of unavoidable, for if it did hold a ref, next/back
859 * may not work as expected when closing collection windows.
861 * collection_unref(cd);
867 layout_set_path(lw, cmd_file);
871 layout_set_path(lw, path);
872 if (first_collection)
874 layout_image_set_collection(lw, first_collection,
875 collection_get_first(first_collection));
879 image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
884 filelist_free(cmd_list);
885 string_list_free(collection_list);
888 if (startup_full_screen) layout_image_full_screen_start(lw);
889 if (startup_in_slideshow) layout_image_slideshow_start(lw);
891 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
892 remote_connection = remote_server_init(buf, cd);