Additional debug features
[geeqie.git] / src / ui_fileops.c
index 33c6560..c4466fc 100644 (file)
@@ -1,13 +1,22 @@
 /*
- * (SLIK) SimpLIstic sKin functions
- * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2010 The Geeqie Team
+ * Copyright (C) 2006 John Ellis
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * Author: John Ellis
  *
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -32,6 +41,9 @@
 #include "ui_utildlg.h"        /* for locale warning dialog */
 #include "md5-util.h"
 
+#include "filefilter.h"
+#include "secure_save.h"
+
 /*
  *-----------------------------------------------------------------------------
  * generic file information and manipulation routines (public)
@@ -46,28 +58,11 @@ void print_term(const gchar *text_utf8)
 
        text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
        fputs((text_l) ? text_l : text_utf8, stderr);
+       if(command_line && command_line->ssi)
+               secure_fputs(command_line->ssi, (text_l) ? text_l : text_utf8);
        g_free(text_l);
 }
 
-static void encoding_dialog(const gchar *path);
-
-static gboolean encoding_dialog_idle(gpointer data)
-{
-       gchar *path = data;
-
-       encoding_dialog(path);
-       g_free(path);
-
-       return FALSE;
-}
-
-static gint encoding_dialog_delay(gpointer data)
-{
-       g_idle_add(encoding_dialog_idle, data);
-
-       return 0;
-}
-
 static void encoding_dialog(const gchar *path)
 {
        static gboolean warned_user = FALSE;
@@ -76,14 +71,6 @@ static void encoding_dialog(const gchar *path)
        const gchar *lc;
        const gchar *bf;
 
-       /* check that gtk is initialized (loop is level > 0) */
-       if (gtk_main_level() == 0)
-               {
-               /* gtk not initialized */
-               gtk_init_add(encoding_dialog_delay, g_strdup(path));
-               return;
-               }
-
        if (warned_user) return;
        warned_user = TRUE;
 
@@ -107,7 +94,7 @@ static void encoding_dialog(const gchar *path)
                name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
                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))
                        g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
                else
@@ -196,7 +183,7 @@ const gchar *homedir(void)
 
        if (!home)
                home = path_to_utf8(getenv("HOME"));
-       
+
        if (!home)
                home = path_to_utf8(g_get_home_dir());
 
@@ -211,7 +198,7 @@ static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
                {
                return g_build_filename(homedir(), fallback, NULL);
                }
-       
+
        return path_to_utf8(dir);
 }
 
@@ -220,7 +207,7 @@ const gchar *xdg_data_home_get(void)
        static const gchar *xdg_data_home = NULL;
 
        if (xdg_data_home) return xdg_data_home;
-       
+
        xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
 
        return xdg_data_home;
@@ -231,7 +218,7 @@ const gchar *xdg_config_home_get(void)
        static const gchar *xdg_config_home = NULL;
 
        if (xdg_config_home) return xdg_config_home;
-       
+
        xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
 
        return xdg_config_home;
@@ -242,7 +229,7 @@ const gchar *xdg_cache_home_get(void)
        static const gchar *xdg_cache_home = NULL;
 
        if (xdg_cache_home) return xdg_cache_home;
-       
+
        xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
 
        return xdg_cache_home;
@@ -251,7 +238,7 @@ const gchar *xdg_cache_home_get(void)
 const gchar *get_rc_dir(void)
 {
        static gchar *rc_dir = NULL;
-       
+
        if (rc_dir) return rc_dir;
 
        if (USE_XDG)
@@ -289,7 +276,7 @@ const gchar *get_trash_dir(void)
        static gchar *trash_dir = NULL;
 
        if (trash_dir) return trash_dir;
-       
+
        if (USE_XDG)
                {
                trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
@@ -545,15 +532,15 @@ gboolean copy_file(const gchar *s, const gchar *t)
 
        fi = fopen(sl, "rb");
        if (!fi) goto end;
-       
+
        /* 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;
-       
+
        fd = g_mkstemp(randname);
        if (fd == -1) goto end;
-       
+
        fo = fdopen(fd, "wb");
        if (!fo) {
                close(fd);
@@ -574,7 +561,7 @@ gboolean copy_file(const gchar *s, const gchar *t)
 
        if (rename(randname, tl) < 0) {
                unlink(randname);
-               goto end;       
+               goto end;
        }
 
        ret = copy_file_attributes(s, t, TRUE, TRUE);
@@ -715,7 +702,7 @@ gchar *unique_filename_simple(const gchar *path)
        name = filename_from_path(path);
        if (!name) return NULL;
 
-       ext = extension_from_path(name);
+       ext = registered_extension_from_path(name);
 
        if (!ext)
                {
@@ -747,24 +734,14 @@ const gchar *filename_from_path(const gchar *path)
 
 gchar *remove_level_from_path(const gchar *path)
 {
-       gint p = 0, n = -1;
+       const gchar *base;
 
        if (!path) return NULL;
 
-       while (path[p])
-               {
-               if (path[p] == G_DIR_SEPARATOR) n = p;
-               p++;
-               }
-       if (n <= 0) n++;
-
-       return g_strndup(path, (gsize) n);
-}
+       base = strrchr(path, G_DIR_SEPARATOR);
+       if (base) return g_strndup(path, strlen(path)-strlen(base));
 
-const gchar *extension_from_path(const gchar *path)
-{
-       if (!path) return NULL;
-       return strrchr(path, '.');
+       return NULL;
 }
 
 gboolean file_extension_match(const gchar *path, const gchar *ext)
@@ -784,18 +761,13 @@ gboolean file_extension_match(const gchar *path, const gchar *ext)
 
 gchar *remove_extension_from_path(const gchar *path)
 {
-       gint p = 0, n = -1;
+       const gchar *reg_ext;
 
        if (!path) return NULL;
 
-       while (path[p])
-               {
-               if (path[p] == '.') n = p;
-               p++;
-               }
-       if (n < 0) n = p;
+       reg_ext = registered_extension_from_path(path);
 
-       return g_strndup(path, (gsize) n);
+       return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
 }
 
 void parse_out_relatives(gchar *path)
@@ -828,7 +800,7 @@ void parse_out_relatives(gchar *path)
                                continue;
                                }
                        }
-       
+
                if (s != t) path[t] = path[s];
                t++;
                s++;
@@ -892,7 +864,7 @@ gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
                                        p[0] = '\0';
                                        end = FALSE;
                                        }
-                               
+
                                if (!isdir(npath))
                                        {
                                        DEBUG_1("creating sub dir:%s", npath);
@@ -903,7 +875,7 @@ gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
                                                return FALSE;
                                                }
                                        }
-                               
+
                                if (!end) p[0] = G_DIR_SEPARATOR;
                                }
                        }