Move third-party sources to separate sub-directory
[geeqie.git] / src / main.cc
index 10edaba..b99f7d1 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "main.h"
+
+#include <unistd.h>
+
+#include <clocale>
 #include <csignal>
-#include <sys/mman.h>
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
 
-#ifdef G_OS_UNIX
-#include <pwd.h>
+#include <config.h>
+
+#if HAVE_CLUTTER
+#  include <clutter-gtk/clutter-gtk.h>
+#  include <clutter/clutter.h>
 #endif
-#include <clocale>
 
-#include "main.h"
+#if HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+
+#include <gio/gio.h>
+#include <glib-object.h>
+
+#ifdef ENABLE_NLS
+#  include <libintl.h>
+#endif
+
+#if HAVE_DEVELOPER
+#include "third-party/backward.h"
+#endif
 
+#include "cache-maint.h"
 #include "cache.h"
-#include "collect.h"
 #include "collect-io.h"
+#include "collect.h"
+#include "compat.h"
+#include "debug.h"
+#include "exif.h"
 #include "filedata.h"
 #include "filefilter.h"
+#include "glua.h"
+#include "histogram.h"
 #include "history-list.h"
 #include "image.h"
 #include "img-view.h"
+#include "intl.h"
 #include "layout-image.h"
 #include "layout-util.h"
+#include "layout.h"
+#include "main-defines.h"
+#include "metadata.h"
 #include "misc.h"
+#include "options.h"
+#include "pixbuf-util.h"
 #include "rcfile.h"
 #include "remote.h"
 #include "secure-save.h"
+#include "third-party/whereami.h"
+#include "thumb.h"
 #include "ui-fileops.h"
 #include "ui-utildlg.h"
-#include "cache-maint.h"
-#include "thumb.h"
-#include "metadata.h"
-#include "exif.h"
-#include "histogram.h"
-#include "pixbuf-util.h"
-#include "glua.h"
-#include "whereami.h"
-
-#ifdef HAVE_CLUTTER
-#include <clutter-gtk/clutter-gtk.h>
-#endif
-
-#ifdef HAVE_EXECINFO_H
-#include <execinfo.h>
-#endif
 
 gboolean thumb_format_changed = FALSE;
 static RemoteConnection *remote_connection = nullptr;
@@ -84,7 +104,7 @@ void sig_handler_cb(int signo, siginfo_t *info, void *)
        guint64 addr;
        guint64 char_index;
        ssize_t len;
-#ifdef HAVE_EXECINFO_H
+#if HAVE_EXECINFO_H
        gint bt_size;
        void *bt[1024];
 #endif
@@ -170,7 +190,7 @@ void sig_handler_cb(int signo, siginfo_t *info, void *)
                len = write(STDERR_FILENO, "\n", 1);
                }
 
-#ifdef HAVE_EXECINFO_H
+#if HAVE_EXECINFO_H
        bt_size = backtrace(bt, 1024);
        backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
 #endif
@@ -183,7 +203,7 @@ void sig_handler_cb(int signo, siginfo_t *info, void *)
 #else /* defined(SA_SIGINFO) */
 void sig_handler_cb(int)
 {
-#ifdef HAVE_EXECINFO_H
+#if HAVE_EXECINFO_H
        gint bt_size;
        void *bt[1024];
 #endif
@@ -191,7 +211,7 @@ void sig_handler_cb(int)
        write(STDERR_FILENO, "Geeqie fatal error\n", 19);
        write(STDERR_FILENO, "Signal: Segmentation fault\n", 27);
 
-#ifdef HAVE_EXECINFO_H
+#if HAVE_EXECINFO_H
        bt_size = backtrace(bt, 1024);
        backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
 #endif
@@ -503,12 +523,6 @@ static void parse_command_line(gint argc, gchar *argv[])
                                printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
                                exit(0);
                                }
-                       else if (strcmp(cmd_line, "--alternate") == 0)
-                               {
-                               /* enable faster experimental algorithm */
-                               log_printf("Alternate similarity algorithm enabled\n");
-                               image_sim_alternate_set(TRUE);
-                               }
                        else if (strcmp(cmd_line, "-h") == 0 ||
                                 strcmp(cmd_line, "--help") == 0)
                                {
@@ -522,8 +536,8 @@ static void parse_command_line(gint argc, gchar *argv[])
                                print_term(FALSE, _("      --geometry=WxH+XOFF+YOFF     set main window location\n"));
                                print_term(FALSE, _("  -h, --help                       show this message\n"));
                                print_term(FALSE, _("  -l, --list [files] [collections] open collection window for command line\n"));
-                               print_term(FALSE, _("  -n, --new-instance               open a new instance of Geeqie\n"));
-                               print_term(FALSE, _("  -o:, --log-file:<file>     save log data to file\n"));
+                               print_term(FALSE, _("  -n, --new-instance               open a new instance of Geeqie *\n"));
+                               print_term(FALSE, _("  -o:, --log-file:<file>           save log data to file\n"));
                                print_term(FALSE, _("  -r, --remote                     send following commands to open window\n"));
                                print_term(FALSE, _("  -rh, --remote-help               print remote command list\n"));
                                print_term(FALSE, _("  -s, --slideshow                  start in slideshow mode\n"));
@@ -533,15 +547,11 @@ static void parse_command_line(gint argc, gchar *argv[])
                                print_term(FALSE, _("  +w, --show-log-window            show log window\n"));
 #ifdef DEBUG
                                print_term(FALSE, _("      --debug[=level]              turn on debug output\n"));
-                               print_term(FALSE, _("  -g:, --grep:<regexp>     filter debug output\n"));
+                               print_term(FALSE, _("  -g:, --grep:<regexp>             filter debug output\n"));
 #endif
 
-#if 0
-                               /* these options are not officially supported!
-                                * only for testing new features, no need to translate them */
-                               print_term(FALSE, "  --alternate                use alternate similarity algorithm\n");
-#endif
                                print_term(FALSE, "\n");
+                               print_term(FALSE, "* Normally a single set of configuration files is used for all instances.\nHowever, the environment variables XDG_CONFIG_HOME, XDG_CACHE_HOME, XDG_DATA_HOME\ncan be used to modify this behavior on an individual basis e.g.\n\nXDG_CONFIG_HOME=/tmp/a XDG_CACHE_HOME=/tmp/b geeqie\n\n");
 
                                remote_help();
 
@@ -564,9 +574,9 @@ static void parse_command_line(gint argc, gchar *argv[])
                        dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
                        gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
                        gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
-                       gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
+                       gq_gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
                        gtk_dialog_run(GTK_DIALOG(dialog_warning));
-                       gtk_widget_destroy(dialog_warning);
+                       g_object_unref(dialog_warning);
                        g_string_free(command_line_errors, TRUE);
 
                        exit(EXIT_FAILURE);
@@ -625,9 +635,9 @@ static void parse_command_line(gint argc, gchar *argv[])
                        dialog_warning = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
                        gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
                        gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
-                       gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
+                       gq_gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
                        gtk_dialog_run(GTK_DIALOG(dialog_warning));
-                       gtk_widget_destroy(dialog_warning);
+                       g_object_unref(dialog_warning);
                        g_string_free(command_line_errors, TRUE);
 
                        exit(EXIT_FAILURE);
@@ -706,7 +716,7 @@ static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
 #endif
 }
 
-#ifdef HAVE_CLUTTER
+#if HAVE_CLUTTER
 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
 {
        const gchar *clutter_option = "--disable-clutter";
@@ -899,7 +909,8 @@ static void gq_accel_map_print(
 {
        GString *gstring = g_string_new(changed ? nullptr : "; ");
        auto ssi = static_cast<SecureSaveInfo *>(data);
-       gchar *tmp, *name;
+       gchar *tmp;
+       gchar *name;
 
        g_string_append(gstring, "(gtk_accel_path \"");
 
@@ -1086,6 +1097,7 @@ static gint exit_confirm_dlg()
        GtkWidget *parent;
        LayoutWindow *lw;
        gchar *msg;
+       GString *message;
 
        if (exit_dialog)
                {
@@ -1093,7 +1105,7 @@ static gint exit_confirm_dlg()
                return TRUE;
                }
 
-       if (!collection_window_modified_exists()) return FALSE;
+       if (!collection_window_modified_exists() && (layout_window_count() == 1)) return FALSE;
 
        parent = nullptr;
        lw = nullptr;
@@ -1108,13 +1120,29 @@ static gint exit_confirm_dlg()
                                exit_confirm_cancel_cb, nullptr);
        g_free(msg);
        msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
-       generic_dialog_add_message(exit_dialog, GQ_ICON_DIALOG_QUESTION,
-                                  msg, _("Collections have been modified. Quit anyway?"), TRUE);
+
+       message = g_string_new(nullptr);
+
+       if (collection_window_modified_exists())
+               {
+               message = g_string_append(message, _("Collections have been modified.\n"));
+               }
+
+       if (layout_window_count() > 1)
+               {
+               g_string_append_printf(message, _("%d windows are open.\n\n"), layout_window_count());
+               }
+
+       message = g_string_append(message, _("Quit anyway?"));
+
+       generic_dialog_add_message(exit_dialog, GQ_ICON_DIALOG_QUESTION, msg, message->str, TRUE);
        g_free(msg);
        generic_dialog_add_button(exit_dialog, GQ_ICON_QUIT, _("Quit"), exit_confirm_exit_cb, TRUE);
 
        gtk_widget_show(exit_dialog->dialog);
 
+       g_string_free(message, TRUE);
+
        return TRUE;
 }
 
@@ -1167,7 +1195,7 @@ static void sigbus_handler_cb_unused(int, siginfo_t *info, void *)
 }
 #endif
 
-static void setup_sigbus_handler_unused(void)
+static void setup_sigbus_handler_unused()
 {
 #if defined(SIGBUS) && defined(SA_SIGINFO)
        struct sigaction sigbus_action;
@@ -1175,12 +1203,12 @@ static void setup_sigbus_handler_unused(void)
        sigbus_action.sa_sigaction = sigbus_handler_cb_unused;
        sigbus_action.sa_flags = SA_SIGINFO;
 
-       sigaction(SIGBUS, &sigbus_action, NULL);
+       sigaction(SIGBUS, &sigbus_action, nullptr);
 #endif
 }
 #pragma GCC diagnostic pop
 
-#ifndef HAVE_DEVELOPER
+#if !HAVE_DEVELOPER
 static void setup_sig_handler()
 {
        struct sigaction sigsegv_action;
@@ -1200,7 +1228,7 @@ static void setup_sig_handler()
 static void set_theme_bg_color()
 {
        GdkRGBA bg_color;
-       GdkColor theme_color;
+       GdkRGBA theme_color;
        GtkStyleContext *style_context;
        GList *work;
        LayoutWindow *lw;
@@ -1213,9 +1241,9 @@ static void set_theme_bg_color()
                style_context = gtk_widget_get_style_context(lw->window);
                gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
 
-               theme_color.red = bg_color.red * 65535;
-               theme_color.green = bg_color.green * 65535;
-               theme_color.blue = bg_color.blue * 65535;
+               theme_color.red = bg_color.red  ;
+               theme_color.green = bg_color.green  ;
+               theme_color.blue = bg_color.blue ;
 
                while (work)
                        {
@@ -1264,7 +1292,7 @@ static void create_application_paths()
        gq_htmldir = g_build_filename(gq_prefix, GQ_HTMLDIR, NULL);
        gq_appdir = g_build_filename(gq_prefix, GQ_APPDIR, NULL);
        gq_bindir = g_build_filename(gq_prefix, GQ_BINDIR, NULL);
-       desktop_file_template = g_build_filename(gq_appdir, "template.desktop", NULL);
+       desktop_file_template = g_build_filename(gq_appdir, "org.geeqie.template.desktop", NULL);
 
        g_free(dirname);
        g_free(path);
@@ -1272,13 +1300,15 @@ static void create_application_paths()
 
 gint main(gint argc, gchar *argv[])
 {
-       CollectionData *first_collection = nullptr;
-       gchar *buf;
        CollectionData *cd = nullptr;
+       CollectionData *first_collection = nullptr;
        gboolean disable_clutter = FALSE;
        gboolean single_dir = TRUE;
-       LayoutWindow *lw;
+       gchar *buf;
+       GdkScreen *screen;
+       GtkCssProvider *provider;
        GtkSettings *default_settings;
+       LayoutWindow *lw;
 
        gdk_set_allowed_backends("x11,*");
 
@@ -1286,7 +1316,7 @@ gint main(gint argc, gchar *argv[])
        gdk_threads_enter();
 
        /* seg. fault handler */
-#ifdef HAVE_DEVELOPER
+#if HAVE_DEVELOPER
        backward::SignalHandling sh{};
 #else
        setup_sig_handler();
@@ -1308,7 +1338,7 @@ gint main(gint argc, gchar *argv[])
 
        exif_init();
 
-#ifdef HAVE_LUA
+#if HAVE_LUA
        lua_init();
 #endif
 
@@ -1332,7 +1362,7 @@ gint main(gint argc, gchar *argv[])
 
        parse_command_line_for_debug_option(argc, argv);
        DEBUG_1("%s main: gtk_init", get_exec_time());
-#ifdef HAVE_CLUTTER
+#if HAVE_CLUTTER
        if (parse_command_line_for_clutter_option(argc, argv))
                {
                disable_clutter = TRUE;
@@ -1386,6 +1416,11 @@ gint main(gint argc, gchar *argv[])
 
        setup_env_path();
 
+       screen = gdk_screen_get_default();
+       provider = gtk_css_provider_new();
+       gtk_css_provider_load_from_resource(provider, GQ_RESOURCE_PATH_UI "/custom.css");
+       gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
        if (parse_command_line_for_cache_maintenance_option(argc, argv))
                {
                process_command_line_for_cache_maintenance_option(argc, argv);
@@ -1409,7 +1444,7 @@ gint main(gint argc, gchar *argv[])
                        filter_rebuild();
                        }
 
-       #ifdef HAVE_CLUTTER
+       #if HAVE_CLUTTER
        /** @FIXME For the background of this see:
         * https://github.com/BestImageViewer/geeqie/issues/397
         * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the