2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <gdk/gdkkeysyms.h> /* for keyboard values */
37 #include "collect-io.h"
39 #include "filefilter.h"
40 #include "history_list.h"
41 #include "image-overlay.h"
43 #include "layout_image.h"
44 #include "layout_util.h"
49 #include "secure_save.h"
51 #include "ui_fileops.h"
52 #include "ui_utildlg.h"
53 #include "cache_maint.h"
58 #include "histogram.h"
59 #include "pixbuf_util.h"
64 #include <clutter-gtk/clutter-gtk.h>
68 /** @FIXME see below */
73 * @page diagrams Diagrams
74 * @section options_overview Options Overview
76 * #_ConfOptions #_LayoutOptions
83 * options.h : ConfOptions
85 * options.h : Options applicable to **all** Layout Windows
86 * options.h : These are in the <global> section of geeqierc.xml
87 * options.h : Available to all modules via the global variable **options**
88 * typedefs.h : LayoutOptions
90 * typedefs.h : Options applicable to **each** Layout Window
91 * typedefs.h : These are in the <layout> section of geeqierc.xml
92 * typedefs.h : There is one <layout> section for each Layout Window displayed
93 * typedefs.h : Available via **<layout_window>->options**
99 * @page diagrams Diagrams
100 * @section options_diagrams_main Options - New Window From Main
103 * #layout_new_from_default
104 * #load_config_from_file
106 * #setup_default_options
112 * : **init_options()**
114 * Set **options** = ConfOptions from hard-coded init values;
118 * : **setup_default_options()**
120 * set hard-coded ConfOptions:
128 * OSD template string
130 * shell path and options
132 * help search engine;
137 * --new-instance) then (yes)
139 * : **load_options()**
143 * : GQ_SYSTEM_WIDE_DIR
150 * /.geeqie/geeqierc.xml;
154 * : **load_config_from_file()**
156 * set **options** from file
157 * and all <layout window>->options in file;
162 * if (broken config. file
164 * or no layout section loaded
165 * (i.e. session not saved)) then (yes)
167 * : **layout_new_from_default()**;
168 * if (default.xml exists) then (yes)
170 * layout_window default options
171 * from default.xml file;
174 * layout_window default options;
180 * : Send --new-window request to remote
181 * No return to this point
182 * This instance terminates;
186 * : Enter gtk main loop;
193 * @page diagrams Diagrams
194 * @section options_diagrams_remote Options - New Window From Remote
195 * #layout_new_from_default
201 * : **layout_new_from_default()**;
202 * if (default.xml exists) then (yes)
204 * layout_window default options
205 * from default.xml file;
208 * layout_window default options;
211 * : set path from PWD;
216 * @page diagrams Diagrams
217 * @section options_diagrams_menu Options - New Window From Menu
218 * #layout_menu_new_window_cb
219 * #layout_menu_window_from_current_cb
220 * #layout_new_from_default
223 * group layout_util.c
229 * : **layout_new_from_default()**;
230 * if (default.xml exists) then (yes)
232 * layout_window default options
233 * from default.xml file;
236 * layout_window default options;
243 * **layout_menu_window_from_current_cb()**
244 * copy layout_window options
245 * from current window;
250 * **layout_menu_new_window_cb()**
251 * load layout_window options
252 * from saved xml file list;
260 * @ref options_overview Options Overview
264 gboolean thumb_format_changed = FALSE;
265 static RemoteConnection *remote_connection = NULL;
273 gchar *gq_executable_path;
274 gchar *desktop_file_template;
275 gchar *instance_identifier;
278 *-----------------------------------------------------------------------------
280 *-----------------------------------------------------------------------------
283 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
285 static gint delta = 0;
286 static guint32 time_old = 0;
287 static guint keyval_old = 0;
289 if (event->state & GDK_CONTROL_MASK)
291 if (*x < 0) *x = G_MININT / 2;
292 if (*x > 0) *x = G_MAXINT / 2;
293 if (*y < 0) *y = G_MININT / 2;
294 if (*y > 0) *y = G_MAXINT / 2;
299 if (options->progressive_key_scrolling)
303 time_diff = event->time - time_old;
305 /* key pressed within 125ms ? (1/8 second) */
306 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
308 time_old = event->time;
309 keyval_old = event->keyval;
318 *x = *x * delta * options->keyboard_scroll_step;
319 *y = *y * delta * options->keyboard_scroll_step;
325 *-----------------------------------------------------------------------------
326 * command line parser (private) hehe, who needs popt anyway?
327 *-----------------------------------------------------------------------------
330 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
331 GList **list, GList **collection_list)
335 path_parsed = g_strdup(file_path);
336 parse_out_relatives(path_parsed);
338 if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
340 *collection_list = g_list_append(*collection_list, path_parsed);
344 if (!*path) *path = remove_level_from_path(path_parsed);
345 if (!*file) *file = g_strdup(path_parsed);
346 *list = g_list_prepend(*list, path_parsed);
350 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
354 /* This is broken because file filter is not initialized yet.
360 path_parsed = g_strdup(dir);
361 parse_out_relatives(path_parsed);
362 dir_fd = file_data_new_dir(path_parsed);
365 if (filelist_read(dir_fd, &files, NULL))
369 files = filelist_filter(files, FALSE);
370 files = filelist_sort_path(files);
375 FileData *fd = work->data;
376 if (!*path) *path = remove_level_from_path(fd->path);
377 if (!*file) *file = g_strdup(fd->path);
378 *list = g_list_prepend(*list, fd);
387 file_data_unref(dir_fd);
389 DEBUG_1("multiple directories specified, ignoring: %s", dir);
393 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
394 GList **list, gchar **first_dir)
397 if (!*list && !*first_dir)
399 *first_dir = g_strdup(dir);
405 parse_command_line_add_dir(*first_dir, path, file, list);
409 parse_command_line_add_dir(dir, path, file, list);
413 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
414 GList **list, GList **collection_list, gchar **first_dir)
419 parse_command_line_add_dir(*first_dir, path, file, list);
423 parse_command_line_add_file(file_path, path, file, list, collection_list);
426 static void parse_command_line(gint argc, gchar *argv[])
429 GList *remote_list = NULL;
430 GList *remote_errors = NULL;
431 gboolean remote_do = FALSE;
432 gchar *first_dir = NULL;
436 gchar *geometry = NULL;
437 GtkWidget *dialog_warning;
438 GString *command_line_errors = g_string_new(NULL);
440 command_line = g_new0(CommandLine, 1);
442 command_line->argc = argc;
443 command_line->argv = argv;
444 command_line->regexp = NULL;
449 gchar *base_dir = get_current_dir();
453 gchar *cmd_line = path_to_utf8(argv[i]);
454 gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
456 if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
458 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
460 else if (isdir(cmd_all))
462 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
464 else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
466 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
467 &list, &command_line->collection_list, &first_dir);
469 else if (isfile(cmd_all))
471 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
472 &list, &command_line->collection_list, &first_dir);
474 else if (download_web_file(cmd_line, FALSE, NULL))
477 else if (is_collection(cmd_line))
481 path = collection_path(cmd_line);
482 parse_command_line_process_file(path, &command_line->path, &command_line->file,
483 &list, &command_line->collection_list, &first_dir);
486 else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
488 /* do nothing but do not produce warnings */
490 else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
492 /* do nothing but do not produce warnings */
494 else if (strcmp(cmd_line, "+t") == 0 ||
495 strcmp(cmd_line, "--with-tools") == 0)
497 command_line->tools_show = TRUE;
499 remote_list = g_list_append(remote_list, "+t");
501 else if (strcmp(cmd_line, "-t") == 0 ||
502 strcmp(cmd_line, "--without-tools") == 0)
504 command_line->tools_hide = TRUE;
506 remote_list = g_list_append(remote_list, "-t");
508 else if (strcmp(cmd_line, "-f") == 0 ||
509 strcmp(cmd_line, "--fullscreen") == 0)
511 command_line->startup_full_screen = TRUE;
513 else if (strcmp(cmd_line, "-s") == 0 ||
514 strcmp(cmd_line, "--slideshow") == 0)
516 command_line->startup_in_slideshow = TRUE;
518 else if (strcmp(cmd_line, "-l") == 0 ||
519 strcmp(cmd_line, "--list") == 0)
521 command_line->startup_command_line_collection = TRUE;
523 else if (strncmp(cmd_line, "--geometry=", 11) == 0)
525 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
527 else if (strcmp(cmd_line, "-r") == 0 ||
528 strcmp(cmd_line, "--remote") == 0)
533 remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
536 else if ((strcmp(cmd_line, "+w") == 0) ||
537 strcmp(cmd_line, "--show-log-window") == 0)
539 command_line->log_window_show = TRUE;
541 else if (strncmp(cmd_line, "-o:", 3) == 0)
543 command_line->log_file = g_strdup(cmd_line + 3);
545 else if (strncmp(cmd_line, "--log-file:", 11) == 0)
547 command_line->log_file = g_strdup(cmd_line + 11);
549 else if (strncmp(cmd_line, "-g:", 3) == 0)
551 set_regexp(g_strdup(cmd_line+3));
553 else if (strncmp(cmd_line, "-grep:", 6) == 0)
555 set_regexp(g_strdup(cmd_line+3));
557 else if (strncmp(cmd_line, "-n", 2) == 0)
559 command_line->new_instance = TRUE;
561 else if (strncmp(cmd_line, "--new-instance", 14) == 0)
563 command_line->new_instance = TRUE;
565 else if (strcmp(cmd_line, "-rh") == 0 ||
566 strcmp(cmd_line, "--remote-help") == 0)
571 else if (strcmp(cmd_line, "--blank") == 0)
573 command_line->startup_blank = TRUE;
575 else if (strcmp(cmd_line, "-v") == 0 ||
576 strcmp(cmd_line, "--version") == 0)
578 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
581 else if (strcmp(cmd_line, "--alternate") == 0)
583 /* enable faster experimental algorithm */
584 log_printf("Alternate similarity algorithm enabled\n");
585 image_sim_alternate_set(TRUE);
587 else if (strcmp(cmd_line, "-h") == 0 ||
588 strcmp(cmd_line, "--help") == 0)
590 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
591 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
592 print_term(FALSE, _("valid options are:\n"));
593 print_term(FALSE, _(" +t, --with-tools force show of tools\n"));
594 print_term(FALSE, _(" -t, --without-tools force hide of tools\n"));
595 print_term(FALSE, _(" -f, --fullscreen start in full screen mode\n"));
596 print_term(FALSE, _(" -s, --slideshow start in slideshow mode\n"));
597 print_term(FALSE, _(" -l, --list [files] [collections] open collection window for command line\n"));
598 print_term(FALSE, _(" --blank start with blank file list\n"));
599 print_term(FALSE, _(" --geometry=XxY+XOFF+YOFF set main window location\n"));
600 print_term(FALSE, _(" -n, --new-instance open a new instance of Geeqie\n"));
601 print_term(FALSE, _(" -r, --remote send following commands to open window\n"));
602 print_term(FALSE, _(" -rh,--remote-help print remote command list\n"));
604 print_term(FALSE, _(" --debug[=level] turn on debug output\n"));
605 print_term(FALSE, _(" -g:<regexp>, --grep:<regexp> filter debug output\n"));
607 print_term(FALSE, _(" +w, --show-log-window show log window\n"));
608 print_term(FALSE, _(" -o:<file>, --log-file:<file> save log data to file\n"));
609 print_term(FALSE, _(" -v, --version print version info\n"));
610 print_term(FALSE, _(" -h, --help show this message\n"));
611 print_term(FALSE, _(" --disable-clutter disable use of Clutter library (i.e. GPU accel.)\n"));
612 print_term(FALSE, _(" --cache-maintenance <path> run cache maintenance in non-GUI mode\n\n"));
615 /* these options are not officially supported!
616 * only for testing new features, no need to translate them */
617 print_term(FALSE, " --alternate use alternate similarity algorithm\n");
625 command_line_errors = g_string_append(command_line_errors, cmd_line);
626 command_line_errors = g_string_append(command_line_errors, "\n");
634 if (command_line_errors->len > 0)
636 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
637 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
638 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
639 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
640 gtk_dialog_run(GTK_DIALOG(dialog_warning));
641 gtk_widget_destroy(dialog_warning);
642 g_string_free(command_line_errors, TRUE);
648 parse_out_relatives(command_line->path);
649 parse_out_relatives(command_line->file);
652 list = g_list_reverse(list);
654 if (!command_line->path && first_dir)
656 command_line->path = first_dir;
659 parse_out_relatives(command_line->path);
663 if (!command_line->new_instance)
665 /* If Geeqie is already running, prevent a second instance
666 * from being started. Open a new window instead.
668 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
669 if (remote_server_exists(app_lock) && !remote_do)
672 if (command_line->geometry)
674 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
675 remote_list = g_list_prepend(remote_list, geometry);
677 remote_list = g_list_prepend(remote_list, "--new-window");
686 GList *work = remote_errors;
690 gchar *opt = work->data;
692 command_line_errors = g_string_append(command_line_errors, opt);
693 command_line_errors = g_string_append(command_line_errors, "\n");
697 dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
698 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
699 gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
700 gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
701 gtk_dialog_run(GTK_DIALOG(dialog_warning));
702 gtk_widget_destroy(dialog_warning);
703 g_string_free(command_line_errors, TRUE);
708 /* prepend the current dir the remote command was made from,
709 * for use by any remote command that needs it
711 current_dir = g_get_current_dir();
712 pwd = g_strconcat("--PWD:", current_dir, NULL);
713 remote_list = g_list_prepend(remote_list, pwd);
715 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
716 /* There is no return to this point
722 g_list_free(remote_list);
724 if (list && list->next)
726 command_line->cmd_list = list;
730 string_list_free(list);
731 command_line->cmd_list = NULL;
734 if (command_line->startup_blank)
736 g_free(command_line->path);
737 command_line->path = NULL;
738 g_free(command_line->file);
739 command_line->file = NULL;
740 filelist_free(command_line->cmd_list);
741 command_line->cmd_list = NULL;
742 string_list_free(command_line->collection_list);
743 command_line->collection_list = NULL;
747 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
750 const gchar *debug_option = "--debug";
751 gint len = strlen(debug_option);
757 for (i = 1; i < argc; i++)
759 const gchar *cmd_line = argv[i];
760 if (strncmp(cmd_line, debug_option, len) == 0)
762 gint cmd_line_len = strlen(cmd_line);
764 /* we now increment the debug state for verbosity */
765 if (cmd_line_len == len)
767 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
769 gint n = atoi(cmd_line + len + 1);
777 DEBUG_1("debugging output enabled (level %d)", get_debug_level());
782 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
784 const gchar *clutter_option = "--disable-clutter";
785 gint len = strlen(clutter_option);
786 gboolean ret = FALSE;
792 for (i = 1; i < argc; i++)
794 const gchar *cmd_line = argv[i];
795 if (strncmp(cmd_line, clutter_option, len) == 0)
806 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
808 const gchar *cache_maintenance_option = "--cache-maintenance";
809 gint len = strlen(cache_maintenance_option);
810 gboolean ret = FALSE;
814 const gchar *cmd_line = argv[1];
815 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
824 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
827 gchar *folder_path = NULL;
830 gchar *buf_config_file;
835 folder_path = expand_tilde(argv[2]);
837 if (isdir(folder_path))
839 rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
843 if (g_file_get_contents(rc_path, &buf_config_file, &size, NULL))
847 diff_count = strncmp("</global>", &buf_config_file[i], 9);
854 /* Load only the <global> section */
855 load_config_from_buf(buf_config_file, i + 9, FALSE);
857 if (options->thumbnails.enable_caching)
859 cache_maintenance(folder_path);
863 print_term(TRUE, "Caching not enabled\n");
866 g_free(buf_config_file);
870 print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
876 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
883 print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
890 print_term(TRUE, _("No path parameter given\n"));
896 *-----------------------------------------------------------------------------
897 * startup, init, and exit
898 *-----------------------------------------------------------------------------
901 #define RC_HISTORY_NAME "history"
902 #define RC_MARKS_NAME "marks"
904 static void setup_env_path(void)
906 const gchar *old_path = g_getenv("PATH");
907 gchar *path = g_strconcat(gq_bin_dir, ":", old_path, NULL);
908 g_setenv("PATH", path, TRUE);
912 static void keys_load(void)
916 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
917 history_list_load(path);
921 static void keys_save(void)
925 path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
926 history_list_save(path);
930 static void marks_load(void)
934 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
935 marks_list_load(path);
939 static void marks_save(gboolean save)
943 path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
944 marks_list_save(path, save);
948 static void mkdir_if_not_exists(const gchar *path)
950 if (isdir(path)) return;
952 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
954 if (!recursive_mkdir_if_not_exists(path, 0755))
956 log_printf(_("Could not create dir:%s\n"), path);
961 /* We add to duplicate and modify gtk_accel_map_print() and gtk_accel_map_save()
962 * to improve the reliability in special cases (especially when disk is full)
963 * These functions are now using secure saving stuff.
965 static void gq_accel_map_print(
967 const gchar *accel_path,
969 GdkModifierType accel_mods,
972 GString *gstring = g_string_new(changed ? NULL : "; ");
973 SecureSaveInfo *ssi = data;
976 g_string_append(gstring, "(gtk_accel_path \"");
978 tmp = g_strescape(accel_path, NULL);
979 g_string_append(gstring, tmp);
982 g_string_append(gstring, "\" \"");
984 name = gtk_accelerator_name(accel_key, accel_mods);
985 tmp = g_strescape(name, NULL);
987 g_string_append(gstring, tmp);
990 g_string_append(gstring, "\")\n");
992 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
994 g_string_free(gstring, TRUE);
997 static gboolean gq_accel_map_save(const gchar *path)
1000 SecureSaveInfo *ssi;
1003 pathl = path_from_utf8(path);
1004 ssi = secure_open(pathl);
1008 log_printf(_("error saving file: %s\n"), path);
1012 gstring = g_string_new("; ");
1013 if (g_get_prgname())
1014 g_string_append(gstring, g_get_prgname());
1015 g_string_append(gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
1016 g_string_append(gstring, "; this file is an automated accelerator map dump\n");
1017 g_string_append(gstring, ";\n");
1019 secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
1021 g_string_free(gstring, TRUE);
1023 gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
1025 if (secure_close(ssi))
1027 log_printf(_("error saving file: %s\nerror: %s\n"), path,
1028 secsave_strerror(secsave_errno));
1035 static gchar *accep_map_filename(void)
1037 return g_build_filename(get_rc_dir(), "accels", NULL);
1040 static void accel_map_save(void)
1044 path = accep_map_filename();
1045 gq_accel_map_save(path);
1049 static void accel_map_load(void)
1054 path = accep_map_filename();
1055 pathl = path_from_utf8(path);
1056 gtk_accel_map_load(pathl);
1061 static void gtkrc_load(void)
1066 /* If a gtkrc file exists in the rc directory, add it to the
1067 * list of files to be parsed at the end of gtk_init() */
1068 path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
1069 pathl = path_from_utf8(path);
1070 if (access(pathl, R_OK) == 0)
1071 gtk_rc_add_default_file(pathl);
1076 static void exit_program_final(void)
1078 LayoutWindow *lw = NULL;
1080 LayoutWindow *tmp_lw;
1082 GFile *archive_file;
1084 /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
1085 layout_editors_reload_finish();
1087 remote_close(remote_connection);
1089 collect_manager_flush();
1091 /* Save the named windows */
1092 if (layout_window_list && layout_window_list->next)
1094 list = layout_window_list;
1097 tmp_lw = list->data;
1098 if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
1100 save_layout(list->data);
1106 save_options(options);
1110 if (layout_valid(&lw))
1115 /* Delete any files/folders in /tmp that have been created by the open archive function */
1116 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
1117 if (isdir(archive_dir))
1119 archive_file = g_file_new_for_path(archive_dir);
1120 rmdir_recursive(archive_file, NULL, NULL);
1121 g_free(archive_dir);
1122 g_object_unref(archive_file);
1125 /* If there are still sub-dirs created by another instance, this will fail
1126 * but that does not matter */
1127 archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
1128 if (isdir(archive_dir))
1130 archive_file = g_file_new_for_path(archive_dir);
1131 g_file_delete(archive_file, NULL, NULL);
1132 g_free(archive_dir);
1133 g_object_unref(archive_file);
1136 secure_close(command_line->ssi);
1141 static GenericDialog *exit_dialog = NULL;
1143 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
1146 generic_dialog_close(gd);
1149 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
1152 generic_dialog_close(gd);
1153 exit_program_final();
1156 static gint exit_confirm_dlg(void)
1164 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1168 if (!collection_window_modified_exists()) return FALSE;
1172 if (layout_valid(&lw))
1174 parent = lw->window;
1177 msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1178 exit_dialog = generic_dialog_new(msg,
1179 "exit", parent, FALSE,
1180 exit_confirm_cancel_cb, NULL);
1182 msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1183 generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
1184 msg, _("Collections have been modified. Quit anyway?"), TRUE);
1186 generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
1188 gtk_widget_show(exit_dialog->dialog);
1193 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
1195 if (success) exit_program();
1198 void exit_program(void)
1200 layout_image_full_screen_stop(NULL);
1202 if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
1204 options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1206 if (exit_confirm_dlg()) return;
1208 exit_program_final();
1211 /* This code is supposed to handle situation when a file mmaped by image_loader
1212 * or by exif loader is truncated by some other process.
1213 * This is probably not completely correct according to posix, because
1214 * mmap is not in the list of calls that can be used safely in signal handler,
1215 * but anyway, the handler is called in situation when the application would
1217 * Ideas for improvement are welcome ;)
1219 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1221 #if defined(SIGBUS) && defined(SA_SIGINFO)
1222 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
1224 unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
1225 DEBUG_1("SIGBUS %p", info->si_addr);
1226 mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1230 static void setup_sigbus_handler(void)
1232 #if defined(SIGBUS) && defined(SA_SIGINFO)
1233 struct sigaction sigbus_action;
1234 sigfillset(&sigbus_action.sa_mask);
1235 sigbus_action.sa_sigaction = sigbus_handler_cb;
1236 sigbus_action.sa_flags = SA_SIGINFO;
1238 sigaction(SIGBUS, &sigbus_action, NULL);
1243 * @brief Set up the application paths
1245 * This function is required for use of AppImages. AppImages are
1246 * relocatable, and therefore cannot use fixed paths to various components.
1247 * These paths were originally #defines created during compilation.
1248 * They are now variables, all defined relative to one level above the
1249 * directory that the executable is run from.
1251 static void create_application_paths(gchar *argv[])
1258 length = wai_getExecutablePath(NULL, 0, NULL);
1259 path = (gchar *)malloc(length + 1);
1260 wai_getExecutablePath(path, length, NULL);
1261 path[length] = '\0';
1263 gq_executable_path = g_strdup(path);
1264 dirname = g_path_get_dirname(gq_executable_path); // default is /usr/bin/
1265 gq_prefix = g_path_get_dirname(dirname);
1267 gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL);
1268 tmp = g_build_filename(gq_prefix, "share", "doc", NULL);
1269 gq_helpdir = g_strconcat(tmp, G_DIR_SEPARATOR_S, "geeqie-", VERSION, NULL);
1270 gq_htmldir = g_build_filename(gq_helpdir, "html", NULL);
1271 gq_app_dir = g_build_filename(gq_prefix, "share", "geeqie", NULL);
1272 gq_bin_dir = g_build_filename(gq_prefix, "lib", "geeqie", NULL);
1273 desktop_file_template = g_build_filename(gq_app_dir, "template.desktop", NULL);
1280 gint main(gint argc, gchar *argv[])
1282 CollectionData *first_collection = NULL;
1284 CollectionData *cd = NULL;
1285 gboolean disable_clutter = FALSE;
1286 gboolean single_dir = TRUE;
1290 #if !GLIB_CHECK_VERSION(2,32,0)
1291 g_thread_init(NULL);
1294 gdk_threads_enter();
1298 /* init execution time counter (debug only) */
1301 create_application_paths(argv);
1303 /* setup locale, i18n */
1304 setlocale(LC_ALL, "");
1307 bindtextdomain(PACKAGE, gq_localedir);
1308 bind_textdomain_codeset(PACKAGE, "UTF-8");
1309 textdomain(PACKAGE);
1318 /* setup random seed for random slideshow */
1321 setup_sigbus_handler();
1323 /* register global notify functions */
1324 file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1325 file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1326 file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1327 file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1328 file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1333 parse_command_line_for_debug_option(argc, argv);
1334 DEBUG_1("%s main: gtk_init", get_exec_time());
1336 if (parse_command_line_for_clutter_option(argc, argv))
1338 disable_clutter = TRUE;
1339 gtk_init(&argc, &argv);
1343 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1345 log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1346 runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1351 gtk_init(&argc, &argv);
1354 if (gtk_major_version < GTK_MAJOR_VERSION ||
1355 (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1357 log_printf("!!! This is a friendly warning.\n");
1358 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1359 log_printf("!!! compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1360 log_printf("!!! running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1361 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1364 DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1365 pixbuf_inline_register_stock_icons();
1367 DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1368 options = init_options(NULL);
1369 setup_default_options(options);
1370 if (disable_clutter)
1372 options->disable_gpu = TRUE;
1375 /* Generate a unique identifier used by the open archive function */
1376 instance_identifier = g_strdup_printf("%x", g_random_int());
1378 DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1379 /* these functions don't depend on config file */
1380 mkdir_if_not_exists(get_rc_dir());
1381 mkdir_if_not_exists(get_collections_dir());
1382 mkdir_if_not_exists(get_thumbnails_cache_dir());
1383 mkdir_if_not_exists(get_metadata_cache_dir());
1384 mkdir_if_not_exists(get_window_layouts_dir());
1388 if (parse_command_line_for_cache_maintenance_option(argc, argv))
1390 process_command_line_for_cache_maintenance_option(argc, argv);
1394 DEBUG_1("%s main: parse_command_line", get_exec_time());
1395 parse_command_line(argc, argv);
1400 /* restore session from the config file */
1403 DEBUG_1("%s main: load_options", get_exec_time());
1404 if (!load_options(options))
1406 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1407 filter_add_defaults();
1412 /** @FIXME For the background of this see:
1413 * https://github.com/BestImageViewer/geeqie/issues/397
1414 * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1415 * system is liable to exhibit this problem.
1416 * The user is provided with an override in Preferences/Behavior
1418 if (!options->override_disable_gpu && !options->disable_gpu)
1420 DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1421 if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1423 options->disable_gpu = TRUE;
1428 /* handle missing config file and commandline additions*/
1429 if (!layout_window_list)
1431 /* broken or no config file or no <layout> section */
1432 layout_new_from_default();
1435 layout_editors_reload_start();
1437 /* If no --list option, open a separate collection window for each
1438 * .gqv file on the command line
1440 if (command_line->collection_list && !command_line->startup_command_line_collection)
1444 work = command_line->collection_list;
1453 cw = collection_window_new(path);
1454 if (!first_collection && cw) first_collection = cw->cd;
1458 if (command_line->log_file)
1461 gchar *path = g_strdup(command_line->log_file);
1463 pathl = path_from_utf8(path);
1464 command_line->ssi = secure_open(pathl);
1467 /* If there is a files list on the command line and no --list option,
1468 * check if they are all in the same folder
1470 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1475 work = command_line->cmd_list;
1477 while (work && single_dir)
1481 dirname = g_path_get_dirname(work->data);
1484 path = g_strdup(dirname);
1488 if (g_strcmp0(path, dirname) != 0)
1499 /* Files from multiple folders, or --list option given
1500 * then open an unnamed collection and insert all files
1502 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1507 cw = collection_window_new(NULL);
1510 collection_path_changed(cd);
1512 work = command_line->cmd_list;
1517 fd = file_data_new_simple(work->data);
1518 collection_add(cd, fd, FALSE);
1519 file_data_unref(fd);
1523 work = command_line->collection_list;
1526 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
1530 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
1532 /* mem leak, we never unref this collection when !startup_command_line_collection
1533 * (the image view of the main window does not hold a ref to the collection)
1534 * this is sort of unavoidable, for if it did hold a ref, next/back
1535 * may not work as expected when closing collection windows.
1537 * collection_unref(cd);
1541 else if (first_collection)
1543 layout_image_set_collection(NULL, first_collection,
1544 collection_get_first(first_collection));
1547 /* If the files on the command line are from one folder, select those files
1548 * unless it is a command line collection - then leave focus on collection window
1553 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1560 work = command_line->cmd_list;
1563 fd = file_data_new_simple((gchar *)work->data);
1564 selected = g_list_append(selected, fd);
1565 file_data_unref(fd);
1568 layout_select_list(lw, selected);
1571 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1572 remote_connection = remote_server_init(buf, cd);
1578 DEBUG_1("%s main: gtk_main", get_exec_time());
1581 gdk_threads_leave();
1585 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */