Include tooltip for Filter combo
[geeqie.git] / src / ui-fileops.cc
index 0ee8809..ca0e238 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <memory>
+
 #include <unistd.h>
 #include <utime.h>
 
@@ -46,7 +48,7 @@ void print_term(gboolean err, const gchar *text_utf8)
 {
        gchar *text_l;
 
-       text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
+       text_l = g_locale_from_utf8(text_utf8, -1, nullptr, nullptr, nullptr);
        if (err)
                {
                fputs((text_l) ? text_l : text_utf8, stderr);
@@ -74,8 +76,7 @@ static void encoding_dialog(const gchar *path)
        lc = getenv("LANG");
        bf = getenv("G_BROKEN_FILENAMES");
 
-       string = g_string_new("");
-       g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
+       string = g_string_new(_("One or more filenames are not encoded with the preferred locale character set.\n"));
        g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
        g_string_append(string, "\n");
        g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
@@ -88,11 +89,11 @@ static void encoding_dialog(const gchar *path)
        if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
                {
                gchar *name;
-               name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
+               name = g_convert(path, -1, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
                string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
                g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
 
-               if (g_utf8_validate(path, -1, NULL))
+               if (g_utf8_validate(path, -1, nullptr))
                        g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
                else
                        g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
@@ -101,10 +102,10 @@ static void encoding_dialog(const gchar *path)
                }
 
        gd = generic_dialog_new(_("Filename encoding locale mismatch"),
-                               "locale warning", NULL, TRUE, NULL, NULL);
-       generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
+                               "locale warning", nullptr, TRUE, nullptr, nullptr);
+       generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), nullptr, TRUE);
 
-       generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
+       generic_dialog_add_message(gd, GQ_ICON_DIALOG_WARNING,
                                   _("Filename encoding locale mismatch"), string->str, TRUE);
 
        gtk_widget_show(gd->dialog);
@@ -119,11 +120,11 @@ gchar *path_to_utf8(const gchar *path)
 #endif
 {
        gchar *utf8;
-       GError *error = NULL;
+       GError *error = nullptr;
 
-       if (!path) return NULL;
+       if (!path) return nullptr;
 
-       utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
+       utf8 = g_filename_to_utf8(path, -1, nullptr, nullptr, &error);
        if (error)
                {
 #if GQ_DEBUG_PATH_UTF8
@@ -150,11 +151,11 @@ gchar *path_from_utf8(const gchar *utf8)
 #endif
 {
        gchar *path;
-       GError *error = NULL;
+       GError *error = nullptr;
 
-       if (!utf8) return NULL;
+       if (!utf8) return nullptr;
 
-       path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
+       path = g_filename_from_utf8(utf8, -1, nullptr, nullptr, &error);
        if (error)
                {
 #if GQ_DEBUG_PATH_UTF8
@@ -174,9 +175,9 @@ gchar *path_from_utf8(const gchar *utf8)
 }
 
 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
-const gchar *homedir(void)
+const gchar *homedir()
 {
-       static gchar *home = NULL;
+       static gchar *home = nullptr;
 
        if (!home)
                home = path_to_utf8(getenv("HOME"));
@@ -203,9 +204,9 @@ static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
        return path_to_utf8(dir);
 }
 
-const gchar *xdg_data_home_get(void)
+const gchar *xdg_data_home_get()
 {
-       static const gchar *xdg_data_home = NULL;
+       static const gchar *xdg_data_home = nullptr;
 
        if (xdg_data_home) return xdg_data_home;
 
@@ -214,9 +215,9 @@ const gchar *xdg_data_home_get(void)
        return xdg_data_home;
 }
 
-const gchar *xdg_config_home_get(void)
+const gchar *xdg_config_home_get()
 {
-       static const gchar *xdg_config_home = NULL;
+       static const gchar *xdg_config_home = nullptr;
 
        if (xdg_config_home) return xdg_config_home;
 
@@ -225,9 +226,9 @@ const gchar *xdg_config_home_get(void)
        return xdg_config_home;
 }
 
-const gchar *xdg_cache_home_get(void)
+const gchar *xdg_cache_home_get()
 {
-       static const gchar *xdg_cache_home = NULL;
+       static const gchar *xdg_cache_home = nullptr;
 
        if (xdg_cache_home) return xdg_cache_home;
 
@@ -236,9 +237,9 @@ const gchar *xdg_cache_home_get(void)
        return xdg_cache_home;
 }
 
-const gchar *get_rc_dir(void)
+const gchar *get_rc_dir()
 {
-       static gchar *rc_dir = NULL;
+       static gchar *rc_dir = nullptr;
 
        if (rc_dir) return rc_dir;
 
@@ -254,9 +255,9 @@ const gchar *get_rc_dir(void)
        return rc_dir;
 }
 
-const gchar *get_collections_dir(void)
+const gchar *get_collections_dir()
 {
-       static gchar *collections_dir = NULL;
+       static gchar *collections_dir = nullptr;
 
        if (collections_dir) return collections_dir;
 
@@ -272,9 +273,9 @@ const gchar *get_collections_dir(void)
        return collections_dir;
 }
 
-const gchar *get_trash_dir(void)
+const gchar *get_trash_dir()
 {
-       static gchar *trash_dir = NULL;
+       static gchar *trash_dir = nullptr;
 
        if (trash_dir) return trash_dir;
 
@@ -290,9 +291,9 @@ const gchar *get_trash_dir(void)
        return trash_dir;
 }
 
-const gchar *get_window_layouts_dir(void)
+const gchar *get_window_layouts_dir()
 {
-       static gchar *window_layouts_dir = NULL;
+       static gchar *window_layouts_dir = nullptr;
 
        if (window_layouts_dir) return window_layouts_dir;
 
@@ -431,24 +432,27 @@ gboolean unlink_file(const gchar *s)
        return ret;
 }
 
-//gboolean symlink_utf8(const gchar *source, const gchar *target)
-//{
-       //gchar *sl;
-       //gchar *tl;
-       //gboolean ret;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+gboolean symlink_utf8_unused(const gchar *source, const gchar *target)
+{
+       gchar *sl;
+       gchar *tl;
+       gboolean ret;
 
-       //if (!source || !target) return FALSE;
+       if (!source || !target) return FALSE;
 
-       //sl = path_from_utf8(source);
-       //tl = path_from_utf8(target);
+       sl = path_from_utf8(source);
+       tl = path_from_utf8(target);
 
-       //ret = (symlink(sl, tl) == 0);
+       ret = (symlink(sl, tl) == 0);
 
-       //g_free(sl);
-       //g_free(tl);
+       g_free(sl);
+       g_free(tl);
 
-       //return ret;
-//}
+       return ret;
+}
+#pragma GCC diagnostic pop
 
 gboolean mkdir_utf8(const gchar *s, gint mode)
 {
@@ -533,25 +537,23 @@ static gboolean hard_linked(const gchar *a, const gchar *b)
                sta.st_ino == stb.st_ino);
 }
 
+static void fclose_safe(FILE *fp)
+{
+       if (fp) fclose(fp);
+}
+
 gboolean copy_file(const gchar *s, const gchar *t)
 {
-       FILE *fi = NULL;
-       FILE *fo = NULL;
-       gchar *sl = NULL;
-       gchar *tl = NULL;
-       gchar *randname = NULL;
        gchar buf[16384];
        size_t b;
-       gint ret = FALSE;
        gint fd = -1;
 
-       sl = path_from_utf8(s);
-       tl = path_from_utf8(t);
+       std::unique_ptr<gchar, decltype(&g_free)> sl{path_from_utf8(s), g_free};
+       std::unique_ptr<gchar, decltype(&g_free)> tl{path_from_utf8(t), g_free};
 
-       if (hard_linked(sl, tl))
+       if (hard_linked(sl.get(), tl.get()))
                {
-               ret = TRUE;
-               goto end;
+               return TRUE;
                }
 
        /* Do not dereference absolute symlinks, but copy them "as is".
@@ -559,9 +561,14 @@ gboolean copy_file(const gchar *s, const gchar *t)
        * copied/moved to another dir to keep pointing it to same target as
        * a relative symlink, so we turn it into absolute symlink using
        * realpath() instead. */
-       struct stat st;
-       if (lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+       auto copy_symlink = [](const gchar *sl, const gchar *tl) -> gboolean
                {
+               struct stat st;
+               if (!lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+                       {
+                       return FALSE;
+                       }
+
                gchar *link_target;
                ssize_t i;
 
@@ -570,7 +577,7 @@ gboolean copy_file(const gchar *s, const gchar *t)
                if (i<0)
                        {
                        g_free(link_target);
-                       goto orig_copy;  // try a "normal" copy
+                       return FALSE;  // try a "normal" copy
                        }
                link_target[st.st_size] = '\0';
 
@@ -578,7 +585,7 @@ gboolean copy_file(const gchar *s, const gchar *t)
                        {
                        gchar *absolute;
 
-                       gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
+                       const gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
                        gint len = lastslash - sl + 1;
 
                        absolute = static_cast<gchar *>(g_malloc(len + st.st_size + 1));
@@ -588,9 +595,9 @@ gboolean copy_file(const gchar *s, const gchar *t)
                        link_target = absolute;
 
                        gchar *realPath;
-                       realPath = realpath(link_target, NULL);
+                       realPath = realpath(link_target, nullptr);
 
-                       if (realPath != NULL) // successfully resolved into an absolute path
+                       if (realPath != nullptr) // successfully resolved into an absolute path
                                {
                                g_free(link_target);
                                link_target = g_strdup(realPath);
@@ -599,7 +606,7 @@ gboolean copy_file(const gchar *s, const gchar *t)
                        else                 // could not get absolute path, got some error instead
                                {
                                g_free(link_target);
-                               goto orig_copy;  // so try a "normal" copy
+                               return FALSE;  // so try a "normal" copy
                                }
                        }
 
@@ -608,57 +615,56 @@ gboolean copy_file(const gchar *s, const gchar *t)
                gint success = (symlink(link_target, tl) == 0);
                g_free(link_target);
 
-               if (success)
-                       {
-                       ret = TRUE;
-                       goto end;
-                       }
-               } // if symlink did not succeed, continue on to try a copy procedure
-       orig_copy:
+               return success;
+               };
 
-       fi = fopen(sl, "rb");
-       if (!fi) goto end;
+       if (copy_symlink(sl.get(), tl.get()))
+               {
+               return TRUE;
+               }
+
+       // if symlink did not succeed, continue on to try a copy procedure
+       std::unique_ptr<FILE, decltype(&fclose_safe)> fi{fopen(sl.get(), "rb"), fclose_safe};
+       if (!fi)
+               {
+               return FALSE;
+               }
 
        /* First we write to a temporary file, then we rename it on success,
           and attributes from original file are copied */
-       randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
-       if (!randname) goto end;
+       std::unique_ptr<gchar, decltype(&g_free)> randname{g_strconcat(tl.get(), ".tmp_XXXXXX", NULL), g_free};
+       if (!randname)
+               {
+               return FALSE;
+               }
 
-       fd = g_mkstemp(randname);
-       if (fd == -1) goto end;
+       fd = g_mkstemp(randname.get());
+       if (fd == -1)
+               {
+               return FALSE;
+               }
 
-       fo = fdopen(fd, "wb");
+       std::unique_ptr<FILE, decltype(&fclose_safe)> fo{fdopen(fd, "wb"), fclose_safe};
        if (!fo) {
                close(fd);
-               goto end;
+               return FALSE;
        }
 
-       while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
+       while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi.get())) && b != 0)
                {
-               if (fwrite(buf, sizeof(gchar), b, fo) != b)
+               if (fwrite(buf, sizeof(gchar), b, fo.get()) != b)
                        {
-                       unlink(randname);
-                       goto end;
+                       unlink(randname.get());
+                       return FALSE;
                        }
                }
 
-       fclose(fi); fi = NULL;
-       fclose(fo); fo = NULL;
-
-       if (rename(randname, tl) < 0) {
-               unlink(randname);
-               goto end;
+       if (rename(randname.get(), tl.get()) < 0) {
+               unlink(randname.get());
+               return FALSE;
        }
 
-       ret = copy_file_attributes(s, t, TRUE, TRUE);
-
-end:
-       if (fi) fclose(fi);
-       if (fo) fclose(fo);
-       if (sl) g_free(sl);
-       if (tl) g_free(tl);
-       if (randname) g_free(randname);
-       return ret;
+       return copy_file_attributes(s, t, TRUE, TRUE);
 }
 
 gboolean move_file(const gchar *s, const gchar *t)
@@ -709,7 +715,7 @@ gboolean rename_file(const gchar *s, const gchar *t)
        return ret;
 }
 
-gchar *get_current_dir(void)
+gchar *get_current_dir()
 {
        gchar *pathl;
        gchar *path8;
@@ -721,20 +727,14 @@ gchar *get_current_dir(void)
        return path8;
 }
 
-void list_free_wrapper(void *data, void *UNUSED(userdata))
-{
-       g_free(data);
-}
-
 void string_list_free(GList *list)
 {
-       g_list_foreach(list, static_cast<GFunc>(list_free_wrapper), NULL);
-       g_list_free(list);
+       g_list_free_full(list, g_free);
 }
 
 GList *string_list_copy(const GList *list)
 {
-       GList *new_list = NULL;
+       GList *new_list = nullptr;
        auto work = const_cast<GList *>(list);
 
        while (work)
@@ -775,47 +775,50 @@ gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider
                        {
                        /* well, we tried */
                        g_free(unique);
-                       return NULL;
+                       return nullptr;
                        }
                }
 
        return unique;
 }
 
-//gchar *unique_filename_simple(const gchar *path)
-//{
-       //gchar *unique;
-       //const gchar *name;
-       //const gchar *ext;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+gchar *unique_filename_simple_unused(const gchar *path)
+{
+       gchar *unique;
+       const gchar *name;
+       const gchar *ext;
 
-       //if (!path) return NULL;
+       if (!path) return NULL;
 
-       //name = filename_from_path(path);
-       //if (!name) return NULL;
+       name = filename_from_path(path);
+       if (!name) return NULL;
 
-       //ext = registered_extension_from_path(name);
+       ext = registered_extension_from_path(name);
 
-       //if (!ext)
-               //{
-               //unique = unique_filename(path, NULL, "_", TRUE);
-               //}
-       //else
-               //{
-               //gchar *base;
+       if (!ext)
+               {
+               unique = unique_filename(path, NULL, "_", TRUE);
+               }
+       else
+               {
+               gchar *base;
 
-               //base = remove_extension_from_path(path);
-               //unique = unique_filename(base, ext, "_", TRUE);
-               //g_free(base);
-               //}
+               base = remove_extension_from_path(path);
+               unique = unique_filename(base, ext, "_", TRUE);
+               g_free(base);
+               }
 
-       //return unique;
-//}
+       return unique;
+}
+#pragma GCC diagnostic pop
 
 const gchar *filename_from_path(const gchar *path)
 {
        const gchar *base;
 
-       if (!path) return NULL;
+       if (!path) return nullptr;
 
        base = strrchr(path, G_DIR_SEPARATOR);
        if (base) return base + 1;
@@ -856,11 +859,11 @@ gchar *remove_extension_from_path(const gchar *path)
 {
        const gchar *reg_ext;
 
-       if (!path) return NULL;
+       if (!path) return nullptr;
 
        reg_ext = registered_extension_from_path(path);
 
-       return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
+       return g_strndup(path, strlen(path) - (reg_ext == nullptr ? 0 : strlen(reg_ext)));
 }
 
 void parse_out_relatives(gchar *path)
@@ -994,17 +997,16 @@ gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
 
 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
 {
-       guchar digest[16];
+       std::unique_ptr<gchar, decltype(&g_free)> pathl{path_from_utf8(path), g_free};
 
-       if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
+       auto md5_text = md5_get_string_from_file(pathl.get());
 
-       return md5_digest_to_text(digest);
+       return md5_text ? md5_text : g_strdup(error_text);
 }
 
 /* Download web file
  */
-typedef struct _WebData WebData;
-struct _WebData
+struct WebData
 {
        GenericDialog *gd;
        GCancellable *cancellable;
@@ -1017,7 +1019,7 @@ struct _WebData
 
 static void web_file_async_ready_cb(GObject *source_object, GAsyncResult *res, gpointer data)
 {
-       GError *error = NULL;
+       GError *error = nullptr;
        auto web = static_cast<WebData *>(data);
        gchar *tmp_filename;
 
@@ -1034,11 +1036,11 @@ static void web_file_async_ready_cb(GObject *source_object, GAsyncResult *res, g
                }
        else
                {
-               file_util_warning_dialog(_("Web file download failed"), error->message, GTK_STOCK_DIALOG_ERROR, NULL);
+               file_util_warning_dialog(_("Web file download failed"), error->message, GQ_ICON_DIALOG_ERROR, nullptr);
                }
 
        g_object_unref(web->tmp_g_file);
-       web->tmp_g_file = NULL;
+       web->tmp_g_file = nullptr;
        g_object_unref(web->cancellable);
        g_object_unref(web->web_file);
 }
@@ -1053,7 +1055,7 @@ static void web_file_progress_cb(goffset current_num_bytes, goffset total_num_by
                }
 }
 
-static void download_web_file_cancel_button_cb(GenericDialog *UNUSED(gd), gpointer data)
+static void download_web_file_cancel_button_cb(GenericDialog *, gpointer data)
 {
        auto web = static_cast<WebData *>(data);
 
@@ -1065,7 +1067,7 @@ gboolean download_web_file(const gchar *text, gboolean minimized, gpointer data)
        gchar *scheme;
        auto lw = static_cast<LayoutWindow *>(data);
        gchar *tmp_dir;
-       GError *error = NULL;
+       GError *error = nullptr;
        WebData *web;
        gchar *base;
        gboolean ret = FALSE;
@@ -1084,7 +1086,7 @@ gboolean download_web_file(const gchar *text, gboolean minimized, gpointer data)
                                {
                                log_printf("Error: could not create temporary file n%s\n", error->message);
                                g_error_free(error);
-                               error = NULL;
+                               error = nullptr;
                                ret = TRUE;
                                }
                        else
@@ -1097,13 +1099,13 @@ gboolean download_web_file(const gchar *text, gboolean minimized, gpointer data)
                                base = g_strdup(g_file_get_basename(web->web_file));
                                web->tmp_g_file = g_file_new_for_path(g_build_filename(tmp_dir, base, NULL));
 
-                               web->gd = generic_dialog_new(_("Download web file"), "download_web_file", NULL, TRUE, download_web_file_cancel_button_cb, web);
+                               web->gd = generic_dialog_new(_("Download web file"), "download_web_file", nullptr, TRUE, download_web_file_cancel_button_cb, web);
 
                                message = g_strconcat(_("Downloading "), base, NULL);
-                               generic_dialog_add_message(web->gd, GTK_STOCK_DIALOG_INFO, message, NULL, FALSE);
+                               generic_dialog_add_message(web->gd, GQ_ICON_DIALOG_INFO, message, nullptr, FALSE);
 
                                web->progress = gtk_progress_bar_new();
-                               gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
+                               gq_gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
                                gtk_widget_show(web->progress);
                                if (minimized)
                                        {
@@ -1132,17 +1134,17 @@ gboolean download_web_file(const gchar *text, gboolean minimized, gpointer data)
 
 gboolean rmdir_recursive(GFile *file, GCancellable *cancellable, GError **error)
 {
-       g_autoptr(GFileEnumerator) enumerator = NULL;
+       g_autoptr(GFileEnumerator) enumerator = nullptr;
 
-       enumerator = g_file_enumerate_children(file, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, NULL);
+       enumerator = g_file_enumerate_children(file, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, nullptr);
 
-       while (enumerator != NULL)
+       while (enumerator != nullptr)
                {
                 GFile *child;
 
-               if (!g_file_enumerator_iterate(enumerator, NULL, &child, cancellable, error))
+               if (!g_file_enumerator_iterate(enumerator, nullptr, &child, cancellable, error))
                        return FALSE;
-               if (child == NULL)
+               if (child == nullptr)
                        break;
                if (!rmdir_recursive(child, cancellable, error))
                        return FALSE;
@@ -1155,12 +1157,12 @@ gboolean rmdir_recursive(GFile *file, GCancellable *cancellable, GError **error)
  * @brief Retrieves the internal scale factor that maps from window coordinates to the actual device pixels
  * @param  -
  * @returns scale factor
- * 
- * 
+ *
+ *
  */
 gint scale_factor()
 {
-       LayoutWindow *lw = NULL;
+       LayoutWindow *lw = nullptr;
 
        layout_valid(&lw);
        return gtk_widget_get_scale_factor(lw->window);