Include a Other Software section in Help file
[geeqie.git] / src / ui_fileops.c
index 99a8216..d14c0ce 100644 (file)
@@ -42,6 +42,8 @@
 #include "md5-util.h"
 
 #include "filefilter.h"
+#include "layout.h"
+#include "utilops.h"
 #include "secure_save.h"
 
 /*
 
 
 
-void print_term(const gchar *text_utf8)
+void print_term(gboolean err, const gchar *text_utf8)
 {
        gchar *text_l;
 
        text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
-       fputs((text_l) ? text_l : text_utf8, stderr);
+       if (err)
+               {
+               fputs((text_l) ? text_l : text_utf8, stderr);
+               }
+       else
+               {
+               fputs((text_l) ? text_l : text_utf8, stdout);
+               }
        if(command_line && command_line->ssi)
                secure_fputs(command_line->ssi, (text_l) ? text_l : text_utf8);
        g_free(text_l);
@@ -169,7 +178,7 @@ gchar *path_from_utf8(const gchar *utf8)
                }
        if (!path)
                {
-               /* if invalid UTF-8, text probaby still in original form, so just copy it */
+               /* if invalid UTF-8, text probably still in original form, so just copy it */
                path = g_strdup(utf8);
                }
 
@@ -187,6 +196,8 @@ const gchar *homedir(void)
        if (!home)
                home = path_to_utf8(g_get_home_dir());
 
+       DEBUG_1("Home directory: %s", home);
+
        return home;
 }
 
@@ -199,6 +210,8 @@ static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
                return g_build_filename(homedir(), fallback, NULL);
                }
 
+       DEBUG_1("Got xdg %s: %s", key, dir);
+
        return path_to_utf8(dir);
 }
 
@@ -289,6 +302,24 @@ const gchar *get_trash_dir(void)
        return trash_dir;
 }
 
+const gchar *get_window_layouts_dir(void)
+{
+       static gchar *window_layouts_dir = NULL;
+
+       if (window_layouts_dir) return window_layouts_dir;
+
+       if (USE_XDG)
+               {
+               window_layouts_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, GQ_WINDOW_LAYOUTS_DIR, NULL);
+               }
+       else
+               {
+               window_layouts_dir = g_build_filename(get_rc_dir(), GQ_WINDOW_LAYOUTS_DIR, NULL);
+               }
+
+       return window_layouts_dir;
+}
+
 gboolean stat_utf8(const gchar *s, struct stat *st)
 {
        gchar *sl;
@@ -535,6 +566,68 @@ gboolean copy_file(const gchar *s, const gchar *t)
                goto end;
                }
 
+       /* Do not dereference absolute symlinks, but copy them "as is".
+       * For a relative symlink, we don't know how to properly change it when
+       * 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))
+               {
+               gchar *link_target;
+               ssize_t i;
+
+               link_target = g_malloc(st.st_size + 1);
+               i = readlink(sl, link_target, st.st_size);
+               if (i<0)
+                       {
+                       g_free(link_target);
+                       goto orig_copy;  // try a "normal" copy
+                       }
+               link_target[st.st_size] = '\0';
+
+               if (link_target[0] != G_DIR_SEPARATOR) // if it is a relative symlink
+                       {
+                       gchar *absolute;
+
+                       gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
+                       gint len = lastslash - sl + 1;
+
+                       absolute = g_malloc(len + st.st_size + 1);
+                       strncpy(absolute, sl, len);
+                       strcpy(absolute + len, link_target);
+                       g_free(link_target);
+                       link_target = absolute;
+
+                       gchar *realPath;
+                       realPath = realpath(link_target, NULL);
+
+                       if (realPath != NULL) // successfully resolved into an absolute path
+                               {
+                               g_free(link_target);
+                               link_target = g_strdup(realPath);
+                               g_free(realPath);
+                               }
+                       else                 // could not get absolute path, got some error instead
+                               {
+                               g_free(link_target);
+                               goto orig_copy;  // so try a "normal" copy
+                               }
+                       }
+
+               if (stat_utf8(tl, &st)) unlink(tl); // first try to remove directory entry in destination directory if such entry exists
+
+               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:
+
        fi = fopen(sl, "rb");
        if (!fi) goto end;
 
@@ -640,9 +733,14 @@ gchar *get_current_dir(void)
        return path8;
 }
 
+void list_free_wrapper(void *data, void *userdata)
+{
+       g_free(data);
+}
+
 void string_list_free(GList *list)
 {
-       g_list_foreach(list, (GFunc)g_free, NULL);
+       g_list_foreach(list, (GFunc)list_free_wrapper, NULL);
        g_list_free(list);
 }
 
@@ -741,12 +839,14 @@ gchar *remove_level_from_path(const gchar *path)
 {
        const gchar *base;
 
-       if (!path) return NULL;
+       if (!path) return g_strdup("");
 
        base = strrchr(path, G_DIR_SEPARATOR);
-       if (base) return g_strndup(path, strlen(path)-strlen(base));
+       /* Take account of a file being in the root ( / ) folder - ensure the returned value
+        * is at least one character long */
+       if (base) return g_strndup(path, (strlen(path)-strlen(base)) == 0 ? 1 : (strlen(path)-strlen(base)));
 
-       return NULL;
+       return g_strdup("");
 }
 
 gboolean file_extension_match(const gchar *path, const gchar *ext)
@@ -760,7 +860,7 @@ gboolean file_extension_match(const gchar *path, const gchar *ext)
        p = strlen(path);
        e = strlen(ext);
 
-       /* FIXME: utf8 */
+       /** @FIXME utf8 */
        return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
 }
 
@@ -787,7 +887,7 @@ void parse_out_relatives(gchar *path)
                {
                if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
                        {
-                       /* /. occurence, let's see more */
+                       /* /. occurrence, let's see more */
                        gint p = s + 2;
 
                        if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
@@ -913,5 +1013,132 @@ gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
        return md5_digest_to_text(digest);
 }
 
+/* Download web file
+ */
+typedef struct _WebData WebData;
+struct _WebData
+{
+       GenericDialog *gd;
+       GCancellable *cancellable;
+       LayoutWindow *lw;
+
+       GtkWidget *progress;
+       GFile *tmp_g_file;
+       GFile *web_file;
+};
+
+static void web_file_async_ready_cb(GObject *source_object, GAsyncResult *res, gpointer data)
+{
+       GError *error = NULL;
+       WebData* web = data;
+       gchar *tmp_filename;
+
+       if (!g_cancellable_is_cancelled(web->cancellable))
+               {
+               generic_dialog_close(web->gd);
+               }
+
+       if (g_file_copy_finish(G_FILE(source_object), res, &error))
+               {
+               tmp_filename = g_file_get_parse_name(web->tmp_g_file);
+               g_free(tmp_filename);
+               layout_set_path(web->lw, g_file_get_path(web->tmp_g_file));
+               }
+       else
+               {
+               file_util_warning_dialog(_("Web file download failed"), error->message, GTK_STOCK_DIALOG_ERROR, NULL);
+               }
+
+       g_object_unref(web->tmp_g_file);
+       web->tmp_g_file = NULL;
+       g_object_unref(web->cancellable);
+       g_object_unref(web->web_file);
+}
+
+static void web_file_progress_cb(goffset current_num_bytes, goffset total_num_bytes, gpointer data)
+{
+       WebData* web = data;
+
+       if (!g_cancellable_is_cancelled(web->cancellable))
+               {
+               gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(web->progress), (gdouble)current_num_bytes / total_num_bytes);
+               }
+}
+
+static void download_web_file_cancel_button_cb(GenericDialog *gd, gpointer data)
+{
+       WebData* web = data;
+
+       g_cancellable_cancel(web->cancellable);
+}
+
+gboolean download_web_file(const gchar *text, gboolean minimized, gpointer data)
+{
+       gchar *scheme;
+       LayoutWindow *lw = data;
+       gchar *tmp_dir;
+       GError *error = NULL;
+       WebData *web;
+       gchar *base;
+       gboolean ret;
+       gchar *message;
+       FileFormatClass format_class;
+
+       scheme = g_uri_parse_scheme(text);
+       if (g_strcmp0("http", scheme) == 0 || g_strcmp0("https", scheme) == 0)
+               {
+               format_class = filter_file_get_class(text);
+
+               if (format_class == FORMAT_CLASS_IMAGE || format_class == FORMAT_CLASS_RAWIMAGE || format_class == FORMAT_CLASS_VIDEO || format_class == FORMAT_CLASS_DOCUMENT)
+                       {
+                       tmp_dir = g_dir_make_tmp("geeqie_XXXXXX", &error);
+                       if (error)
+                               {
+                               log_printf("Error: could not create temporary file n%s\n", error->message);
+                               g_error_free(error);
+                               error = NULL;
+                               ret = TRUE;
+                               }
+                       else
+                               {
+                               web = g_new0(WebData, 1);
+                               web->lw = lw;
+
+                               web->web_file = g_file_new_for_uri(text);
+
+                               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);
+
+                               message = g_strconcat(_("Downloading "), base, NULL);
+                               generic_dialog_add_message(web->gd, GTK_STOCK_DIALOG_INFO, message, NULL, FALSE);
+
+                               web->progress = gtk_progress_bar_new();
+                               gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
+                               gtk_widget_show(web->progress);
+                               if (minimized)
+                                       {
+                                       gtk_window_iconify(GTK_WINDOW(web->gd->dialog));
+                                       }
 
+                               gtk_widget_show(web->gd->dialog);
+                               web->cancellable = g_cancellable_new();
+                               g_file_copy_async(web->web_file, web->tmp_g_file, G_FILE_COPY_OVERWRITE, G_PRIORITY_LOW, web->cancellable, web_file_progress_cb, web, web_file_async_ready_cb, web);
+
+                               g_free(base);
+                               g_free(message);
+                               ret = TRUE;
+                               }
+                       }
+               }
+       else
+               {
+               ret = FALSE;
+               }
+
+       g_free(scheme);
+       return ret;
+
+}
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */