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"
23 #include "filefilter.h"
24 #include "fullscreen.h"
25 #include "image-overlay.h"
28 #include "layout_image.h"
30 #include "pixbuf_util.h"
31 #include "preferences.h"
35 #include "slideshow.h"
37 #include "ui_bookmark.h"
39 #include "ui_fileops.h"
40 #include "ui_tabcomp.h"
41 #include "ui_utildlg.h"
44 #include <gdk/gdkkeysyms.h> /* for keyboard values */
50 static RemoteConnection *remote_connection = NULL;
54 *-----------------------------------------------------------------------------
56 *-----------------------------------------------------------------------------
60 gdouble get_zoom_increment(void)
62 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
65 gchar *utf8_validate_or_convert(gchar *text)
69 if (!text) return NULL;
72 if (!g_utf8_validate(text, len, NULL))
76 conv_text = g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
85 *-----------------------------------------------------------------------------
87 *-----------------------------------------------------------------------------
90 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
92 static gint delta = 0;
93 static guint32 time_old = 0;
94 static guint keyval_old = 0;
96 if (event->state & GDK_CONTROL_MASK)
98 if (*x < 0) *x = G_MININT / 2;
99 if (*x > 0) *x = G_MAXINT / 2;
100 if (*y < 0) *y = G_MININT / 2;
101 if (*y > 0) *y = G_MAXINT / 2;
106 if (options->progressive_key_scrolling)
110 time_diff = event->time - time_old;
112 /* key pressed within 125ms ? (1/8 second) */
113 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
115 time_old = event->time;
116 keyval_old = event->keyval;
132 *-----------------------------------------------------------------------------
133 * command line parser (private) hehe, who needs popt anyway?
134 *-----------------------------------------------------------------------------
137 static gint startup_blank = FALSE;
138 static gint startup_full_screen = FALSE;
139 static gint startup_in_slideshow = FALSE;
140 static gint startup_command_line_collection = FALSE;
143 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
144 GList **list, GList **collection_list)
148 path_parsed = g_strdup(file_path);
149 parse_out_relatives(path_parsed);
151 if (file_extension_match(path_parsed, ".gqv"))
153 *collection_list = g_list_append(*collection_list, path_parsed);
157 if (!*path) *path = remove_level_from_path(path_parsed);
158 if (!*file) *file = g_strdup(path_parsed);
159 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
163 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
169 path_parsed = g_strdup(dir);
170 parse_out_relatives(path_parsed);
172 if (filelist_read(path_parsed, &files, NULL))
176 files = filelist_filter(files, FALSE);
177 files = filelist_sort_path(files);
182 FileData *fd = work->data;
183 if (!*path) *path = remove_level_from_path(fd->path);
184 if (!*file) *file = g_strdup(fd->path);
185 *list = g_list_prepend(*list, fd);
196 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
197 GList **list, gchar **first_dir)
200 if (!*list && !*first_dir)
202 *first_dir = g_strdup(dir);
208 parse_command_line_add_dir(*first_dir, path, file, list);
212 parse_command_line_add_dir(dir, path, file, list);
216 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
217 GList **list, GList **collection_list, gchar **first_dir)
222 parse_command_line_add_dir(*first_dir, path, file, list);
226 parse_command_line_add_file(file_path, path, file, list, collection_list);
229 static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,
230 GList **cmd_list, GList **collection_list,
234 GList *remote_list = NULL;
235 GList *remote_errors = NULL;
236 gint remote_do = FALSE;
237 gchar *first_dir = NULL;
242 gchar *base_dir = get_current_dir();
246 const gchar *cmd_line = argv[i];
247 gchar *cmd_all = concat_dir_and_file(base_dir, cmd_line);
249 if (cmd_line[0] == '/' && isdir(cmd_line))
251 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
253 else if (isdir(cmd_all))
255 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
257 else if (cmd_line[0] == '/' && isfile(cmd_line))
259 parse_command_line_process_file(cmd_line, path, file,
260 &list, collection_list, &first_dir);
262 else if (isfile(cmd_all))
264 parse_command_line_process_file(cmd_all, path, file,
265 &list, collection_list, &first_dir);
267 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
269 /* do nothing but do not produce warnings */
271 else if (strcmp(cmd_line, "+t") == 0 ||
272 strcmp(cmd_line, "--with-tools") == 0)
274 options->layout.tools_float = FALSE;
275 options->layout.tools_hidden = FALSE;
277 remote_list = g_list_append(remote_list, "+t");
279 else if (strcmp(cmd_line, "-t") == 0 ||
280 strcmp(cmd_line, "--without-tools") == 0)
282 options->layout.tools_hidden = TRUE;
284 remote_list = g_list_append(remote_list, "-t");
286 else if (strcmp(cmd_line, "-f") == 0 ||
287 strcmp(cmd_line, "--fullscreen") == 0)
289 startup_full_screen = TRUE;
291 else if (strcmp(cmd_line, "-s") == 0 ||
292 strcmp(cmd_line, "--slideshow") == 0)
294 startup_in_slideshow = TRUE;
296 else if (strcmp(cmd_line, "-l") == 0 ||
297 strcmp(cmd_line, "--list") == 0)
299 startup_command_line_collection = TRUE;
301 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
303 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
305 else if (strcmp(cmd_line, "-r") == 0 ||
306 strcmp(cmd_line, "--remote") == 0)
311 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
314 else if (strcmp(cmd_line, "-rh") == 0 ||
315 strcmp(cmd_line, "--remote-help") == 0)
320 else if (strcmp(cmd_line, "--blank") == 0)
322 startup_blank = TRUE;
324 else if (strcmp(cmd_line, "-v") == 0 ||
325 strcmp(cmd_line, "--version") == 0)
327 printf("%s %s\n", GQ_APPNAME, VERSION);
330 else if (strcmp(cmd_line, "--alternate") == 0)
332 /* enable faster experimental algorithm */
333 printf("Alternate similarity algorithm enabled\n");
334 image_sim_alternate_set(TRUE);
336 else if (strcmp(cmd_line, "-h") == 0 ||
337 strcmp(cmd_line, "--help") == 0)
339 printf("%s %s\n", GQ_APPNAME, VERSION);
340 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
341 print_term(_("valid options are:\n"));
342 print_term(_(" +t, --with-tools force show of tools\n"));
343 print_term(_(" -t, --without-tools force hide of tools\n"));
344 print_term(_(" -f, --fullscreen start in full screen mode\n"));
345 print_term(_(" -s, --slideshow start in slideshow mode\n"));
346 print_term(_(" -l, --list open collection window for command line\n"));
347 print_term(_(" --geometry=GEOMETRY set main window location\n"));
348 print_term(_(" -r, --remote send following commands to open window\n"));
349 print_term(_(" -rh,--remote-help print remote command list\n"));
351 print_term(_(" --debug[=level] turn on debug output\n"));
353 print_term(_(" -v, --version print version info\n"));
354 print_term(_(" -h, --help show this message\n\n"));
357 /* these options are not officially supported!
358 * only for testing new features, no need to translate them */
359 print_term( " --alternate use alternate similarity algorithm\n");
366 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
373 parse_out_relatives(*path);
374 parse_out_relatives(*file);
377 list = g_list_reverse(list);
379 if (!*path && first_dir)
384 parse_out_relatives(*path);
392 GList *work = remote_errors;
394 printf_term(_("Invalid or ignored remote options: "));
397 gchar *opt = work->data;
399 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
403 printf_term(_("\nUse --remote-help for valid remote options.\n"));
406 remote_control(argv[0], remote_list, *path, list, *collection_list);
408 g_list_free(remote_list);
410 if (list && list->next)
421 static void parse_command_line_for_debug_option(int argc, char *argv[])
424 const gchar *debug_option = "--debug";
425 gint len = strlen(debug_option);
431 for (i = 1; i < argc; i++)
433 const gchar *cmd_line = argv[i];
434 if (strncmp(cmd_line, debug_option, len) == 0)
436 gint cmd_line_len = strlen(cmd_line);
438 /* we now increment the debug state for verbosity */
439 if (cmd_line_len == len)
441 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
443 gint n = atoi(cmd_line + len + 1);
451 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
456 *-----------------------------------------------------------------------------
457 * startup, init, and exit
458 *-----------------------------------------------------------------------------
461 #define RC_HISTORY_NAME "history"
463 static void keys_load(void)
467 path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/", RC_HISTORY_NAME, NULL);
468 history_list_load(path);
472 static void keys_save(void)
476 path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/", RC_HISTORY_NAME, NULL);
477 history_list_save(path);
481 static void check_for_home_path(gchar *path)
485 buf = g_strconcat(homedir(), "/", path, NULL);
488 printf_term(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
490 if (!mkdir_utf8(buf, 0755))
492 printf_term(_("Could not create dir:%s\n"), buf);
498 static void setup_default_options(void)
503 for (i = 0; i < GQ_EDITOR_SLOTS; i++)
505 options->editor_name[i] = NULL;
506 options->editor_command[i] = NULL;
509 editor_reset_defaults();
511 bookmark_add_default(_("Home"), homedir());
512 path = concat_dir_and_file(homedir(), "Desktop");
513 bookmark_add_default(_("Desktop"), path);
515 path = concat_dir_and_file(homedir(), GQ_RC_DIR_COLLECTIONS);
516 bookmark_add_default(_("Collections"), path);
519 g_free(options->file_ops.safe_delete_path);
520 options->file_ops.safe_delete_path = concat_dir_and_file(homedir(), GQ_RC_DIR_TRASH);
522 for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
524 options->color_profile.input_file[i] = NULL;
525 options->color_profile.input_name[i] = NULL;
528 set_default_image_overlay_template_string(options);
529 sidecar_ext_add_defaults();
530 options->layout.order = g_strdup("123");
533 static void exit_program_final(void)
537 LayoutWindow *lw = NULL;
539 remote_close(remote_connection);
541 collect_manager_flush();
543 if (layout_valid(&lw))
545 options->layout.main_window.maximized = window_maximized(lw->window);
546 if (!options->layout.main_window.maximized)
548 layout_geometry_get(NULL, &options->layout.main_window.x, &options->layout.main_window.y,
549 &options->layout.main_window.w, &options->layout.main_window.h);
552 options->image_overlay.common.state = image_osd_get(lw->image);
555 layout_geometry_get_dividers(NULL, &options->layout.main_window.hdivider_pos, &options->layout.main_window.vdivider_pos);
557 layout_views_get(NULL, &options->layout.dir_view_type, &options->layout.file_view_type);
559 options->layout.show_thumbnails = layout_thumb_get(NULL);
560 options->layout.show_marks = layout_marks_get(NULL);
562 layout_sort_get(NULL, &options->file_sort.method, &options->file_sort.ascending);
564 layout_geometry_get_tools(NULL, &options->layout.float_window.x, &options->layout.float_window.y,
565 &options->layout.float_window.w, &options->layout.float_window.h, &options->layout.float_window.vdivider_pos);
566 layout_tools_float_get(NULL, &options->layout.tools_float, &options->layout.tools_hidden);
567 options->layout.toolbar_hidden = layout_toolbar_hidden(NULL);
569 options->color_profile.enabled = layout_image_color_profile_get_use(NULL);
570 layout_image_color_profile_get(NULL,
571 &options->color_profile.input_type,
572 &options->color_profile.screen_type,
573 &options->color_profile.use_image);
575 if (options->startup.restore_path && options->startup.use_last_path)
577 g_free(options->startup.path);
578 options->startup.path = g_strdup(layout_get_path(NULL));
584 path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/accels", NULL);
585 pathl = path_from_utf8(path);
586 gtk_accel_map_save(pathl);
593 static GenericDialog *exit_dialog = NULL;
595 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
598 generic_dialog_close(gd);
601 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
604 generic_dialog_close(gd);
605 exit_program_final();
608 static gint exit_confirm_dlg(void)
616 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
620 if (!collection_window_modified_exists()) return FALSE;
624 if (layout_valid(&lw))
629 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
630 exit_dialog = generic_dialog_new(msg,
631 GQ_WMCLASS, "exit", parent, FALSE,
632 exit_confirm_cancel_cb, NULL);
634 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
635 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
636 msg, _("Collections have been modified. Quit anyway?"));
638 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
640 gtk_widget_show(exit_dialog->dialog);
645 void exit_program(void)
647 layout_image_full_screen_stop(NULL);
649 if (exit_confirm_dlg()) return;
651 exit_program_final();
654 int main(int argc, char *argv[])
658 gchar *cmd_path = NULL;
659 gchar *cmd_file = NULL;
660 GList *cmd_list = NULL;
661 GList *collection_list = NULL;
662 CollectionData *first_collection = NULL;
663 gchar *geometry = NULL;
666 CollectionData *cd = NULL;
668 /* init execution time counter (debug only) */
671 /* setup locale, i18n */
673 bindtextdomain(PACKAGE, GQ_LOCALEDIR);
674 bind_textdomain_codeset(PACKAGE, "UTF-8");
677 /* setup random seed for random slideshow */
681 printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
683 parse_command_line_for_debug_option(argc, argv);
685 options = init_options(NULL);
686 setup_default_options();
689 parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
691 gtk_init(&argc, &argv);
693 if (gtk_major_version < GTK_MAJOR_VERSION ||
694 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
696 printf_term("!!! This is a friendly warning.\n");
697 printf_term("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
698 printf_term("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
699 printf_term("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
700 printf_term("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
703 check_for_home_path(GQ_RC_DIR);
704 check_for_home_path(GQ_RC_DIR_COLLECTIONS);
705 check_for_home_path(GQ_CACHE_RC_THUMB);
706 check_for_home_path(GQ_CACHE_RC_METADATA);
709 filter_add_defaults();
712 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/accels", NULL);
713 bufl = path_from_utf8(buf);
714 gtk_accel_map_load(bufl);
724 filelist_free(cmd_list);
726 string_list_free(collection_list);
727 collection_list = NULL;
733 path = g_strdup(cmd_path);
735 else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
737 path = g_strdup(options->startup.path);
741 path = get_current_dir();
744 lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
745 layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
747 if (collection_list && !startup_command_line_collection)
751 work = collection_list;
760 cw = collection_window_new(path);
761 if (!first_collection && cw) first_collection = cw->cd;
766 (startup_command_line_collection && collection_list))
770 if (startup_command_line_collection)
774 cw = collection_window_new("");
779 cd = collection_new(""); /* if we pass NULL, untitled counter is falsely increm. */
785 cd->name = g_strdup(_("Command line"));
787 collection_path_changed(cd);
792 collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
796 work = collection_list;
799 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
803 layout_set_path(lw, path);
804 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
806 /* mem leak, we never unref this collection when !startup_command_line_collection
807 * (the image view of the main window does not hold a ref to the collection)
808 * this is sort of unavoidable, for if it did hold a ref, next/back
809 * may not work as expected when closing collection windows.
811 * collection_unref(cd);
817 layout_set_path(lw, cmd_file);
821 layout_set_path(lw, path);
822 if (first_collection)
824 layout_image_set_collection(lw, first_collection,
825 collection_get_first(first_collection));
829 image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
834 filelist_free(cmd_list);
835 string_list_free(collection_list);
838 if (startup_full_screen) layout_image_full_screen_start(lw);
839 if (startup_in_slideshow) layout_image_slideshow_start(lw);
841 buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
842 remote_connection = remote_server_init(buf, cd);