/*
- * Geeqie
- * Copyright (C) 2008 - 2010 The Geeqie Team
+ * Copyright (C) 2008 - 2016 The Geeqie Team
*
* Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
*
+ * 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 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 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.
*/
#include "main.h"
#include "filedata.h"
#include "ui_fileops.h"
+#include "ui_utildlg.h"
-/*
- *-----------------------------------------------------------------------------
- * drag and drop uri utils
- *-----------------------------------------------------------------------------
- */
-
-/* the following characters are allowed to be unencoded for pathnames:
- * $ & + , / : = @
- */
-static gint escape_char_list[] = {
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
-/* spc ! " # $ % & ' */
- 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
-/* ( ) * + , - . / 0 1 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
-/* 2 3 4 5 6 7 8 9 : ; */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
-/* < = > ? @ A B C D E */
- 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
-/* Z [ \ ] ^ _ ` a b c */
- 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
-/* x y z { | } ~ del */
- 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
-};
-
-static gchar *hex_char = "0123456789ABCDEF";
-
-static gboolean escape_test(guchar c)
-{
- if (c < 32 || c > 127) return TRUE;
- return (escape_char_list[c] != 0);
-}
-
-static const gchar *escape_code(guchar c)
+void warning_dialog_dnd_uri_error(GList *uri_error_list)
{
- static gchar text[4];
-
- text[0] = '%';
- text[1] = hex_char[c>>4];
- text[2] = hex_char[c%16];
- text[3] = '\0';
-
- return text;
-}
-
-gchar *uri_text_escape(const gchar *text)
-{
- GString *string;
- gchar *result;
- const gchar *p;
-
- if (!text) return NULL;
-
- string = g_string_new("");
-
- p = text;
- while (*p != '\0')
+ GList *work = uri_error_list;
+ guint count = g_list_length(work);
+ gchar *msg = g_strdup_printf("Failed to convert %d dropped item(s) to files\n", count);
+ if(count < 10)
{
- if (escape_test(*p))
+ while (work)
{
- g_string_append(string, escape_code(*p));
+ gchar *prev = msg;
+ msg = g_strdup_printf("%s\n%s", prev, (gchar *)work->data);
+ work = work->next;
+ g_free(prev);
}
- else
- {
- g_string_append_c(string, *p);
- }
- p++;
}
+ warning_dialog(_("Drag and Drop failed"), msg, GTK_STOCK_DIALOG_WARNING, NULL);
+ g_free(msg);
+}
- result = string->str;
- g_string_free(string, FALSE);
+gchar **uris_from_pathlist(GList *list)
+{
+ GList *work;
+ guint i = 0;
+ guint num = g_list_length(list);
+ gchar **uris = g_new0(gchar *, num + 1);
- /* dropped filenames are expected to be utf-8 compatible */
- if (!g_utf8_validate(result, -1, NULL))
+ work = list;
+ while (work)
{
- gchar *tmp;
+ const gchar *path = work->data;
+ gchar *local_path = path_from_utf8(path);
+ uris[i] = g_filename_to_uri(local_path, NULL, NULL);
+ g_free(local_path);
- tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
- if (tmp)
- {
- g_free(result);
- result = tmp;
- }
+ i++;
+ work = work->next;
}
- return result;
+ uris[i] = NULL;
+ return uris;
}
-/* this operates on the passed string, decoding escaped characters */
-void uri_text_decode(gchar *text)
+gchar **uris_from_filelist(GList *list)
{
- if (strchr(text, '%'))
- {
- gchar *w;
- gchar *r;
-
- w = r = text;
-
- while (*r != '\0')
- {
- if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
- {
- gchar t[3];
- gint n;
-
- r++;
- t[0] = *r;
- r++;
- t[1] = *r;
- t[2] = '\0';
- n = (gint)strtol(t, NULL, 16);
- if (n > 0 && n < 256)
- {
- *w = (gchar)n;
- }
- else
- {
- /* invalid number, rewind and ignore this escape */
- r -= 2;
- *w = *r;
- }
- }
- else if (w != r)
- {
- *w = *r;
- }
- r++;
- w++;
- }
- if (*w != '\0') *w = '\0';
- }
+ GList *path_list = filelist_to_path_list(list);
+ gchar **ret = uris_from_pathlist(path_list);
+ string_list_free(path_list);
+ return ret;
}
-static void uri_list_parse_encoded_chars(GList *list)
+gboolean uri_selection_data_set_uris_from_filelist(GtkSelectionData *selection_data, GList *list)
{
- GList *work = list;
-
- while (work)
+ gchar **uris = uris_from_filelist(list);
+ gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
+ if (!ret)
{
- gchar *text = work->data;
-
- uri_text_decode(text);
-
- work = work->next;
+ char *str = g_strjoinv("\r\n", uris);
+ ret = gtk_selection_data_set_text(selection_data, str, -1);
+ g_free(str);
}
+
+ g_strfreev(uris);
+ return ret;
}
-GList *uri_list_from_text(gchar *data, gboolean files_only)
+GList *uri_pathlist_from_uris(gchar **uris, GList **uri_error_list)
{
GList *list = NULL;
- gint b, e;
-
- b = e = 0;
+ guint i = 0;
+ GError *error = NULL;
- while (data[b] != '\0')
+ while (uris[i])
{
- while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
- if (strncmp(data + b, "file:", 5) == 0)
- {
- gchar *path;
- b += 5;
- while (data[b] == '/' && data[b+1] == '/') b++;
- path = g_strndup(data + b, e - b);
- list = g_list_append(list, path_to_utf8(path));
- g_free(path);
- }
- else if (!files_only && strncmp(data + b, "http:", 5) == 0)
- {
- list = g_list_append(list, g_strndup(data + b, e - b));
- }
- else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
+ gchar *local_path = g_filename_from_uri(uris[i], NULL, &error);
+ if (error)
{
- list = g_list_append(list, g_strndup(data + b, e - b));
+ DEBUG_1("g_filename_from_uri failed on uri \"%s\"", uris[i]);
+ DEBUG_1(" error %d: %s", error->code, error->message);
+ if (error->code == G_CONVERT_ERROR_BAD_URI)
+ {
+ GError *retry_error = NULL;
+ gchar *escaped = g_uri_escape_string(uris[i], ":/", TRUE);
+ local_path = g_filename_from_uri(escaped, NULL, &retry_error);
+ if(retry_error)
+ {
+ DEBUG_1("manually escaped uri \"%s\" also failed g_filename_from_uri", escaped);
+ DEBUG_1(" error %d: %s", retry_error->code, retry_error->message);
+ g_error_free(retry_error);
+ }
+ g_free(escaped);
+ }
+ g_error_free(error);
+ error = NULL;
+ if (!local_path)
+ {
+ *uri_error_list = g_list_prepend(*uri_error_list, g_strdup(uris[i]));
+ i++;
+ continue;
+ }
}
- while (data[e] == '\r' || data[e] == '\n') e++;
- b = e;
+ gchar *path = path_to_utf8(local_path);
+ g_free(local_path);
+ list = g_list_prepend(list, path);
+ i++;
}
- uri_list_parse_encoded_chars(list);
-
- return list;
+ *uri_error_list = g_list_reverse(*uri_error_list);
+ return g_list_reverse(list);
}
-GList *uri_filelist_from_text(gchar *data, gboolean files_only)
+GList *uri_filelist_from_uris(gchar **uris, GList **uri_error_list)
{
- GList *path_list = uri_list_from_text(data, files_only);
+ GList *path_list = uri_pathlist_from_uris(uris, uri_error_list);
GList *filelist = filelist_from_path_list(path_list);
string_list_free(path_list);
return filelist;
}
-gchar *uri_text_from_list(GList *list, gint *len, gboolean plain_text)
+GList *uri_filelist_from_gtk_selection_data(GtkSelectionData *selection_data)
{
- gchar *uri_text = NULL;
- GString *string;
- GList *work;
-
- if (!list)
+ GList *errors = NULL;
+ gchar **uris = gtk_selection_data_get_uris(selection_data);
+ GList *ret = uri_filelist_from_uris(uris, &errors);
+ if(errors)
{
- if (len) *len = 0;
- return NULL;
- }
-
- string = g_string_new("");
-
- work = list;
- while (work)
- {
- const gchar *name8; /* dnd filenames are in utf-8 */
-
- name8 = work->data;
-
- if (!plain_text)
- {
- gchar *escaped;
-
- escaped = uri_text_escape(name8);
- g_string_append(string, "file:");
- g_string_append(string, escaped);
- g_free(escaped);
-
- g_string_append(string, "\r\n");
- }
- else
- {
- g_string_append(string, name8);
- if (work->next) g_string_append(string, "\n");
- }
-
- work = work->next;
+ warning_dialog_dnd_uri_error(errors);
+ string_list_free(errors);
}
+ g_strfreev(uris);
+ return ret;
+}
- uri_text = string->str;
- if (len) *len = string->len;
- g_string_free(string, FALSE);
- return uri_text;
-}
-gchar *uri_text_from_filelist(GList *list, gint *len, gboolean plain_text)
-{
- GList *path_list = filelist_to_path_list(list);
- gchar *ret = uri_text_from_list(path_list, len, plain_text);
- string_list_free(path_list);
- return ret;
-}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */