2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "uri-utils.h"
26 #include "main-defines.h"
27 #include "ui-fileops.h"
28 #include "ui-utildlg.h"
30 void warning_dialog_dnd_uri_error(GList *uri_error_list)
32 GList *work = uri_error_list;
33 guint count = g_list_length(work);
34 gchar *msg = g_strdup_printf("Failed to convert %d dropped item(s) to files\n", count);
40 msg = g_strdup_printf("%s\n%s", prev, static_cast<gchar *>(work->data));
45 warning_dialog(_("Drag and Drop failed"), msg, GQ_ICON_DIALOG_WARNING, nullptr);
49 gchar **uris_from_pathlist(GList *list)
53 guint num = g_list_length(list);
54 auto uris = g_new0(gchar *, num + 1);
59 auto path = static_cast<const gchar *>(work->data);
60 gchar *local_path = path_from_utf8(path);
61 uris[i] = g_filename_to_uri(local_path, nullptr, nullptr);
72 gchar **uris_from_filelist(GList *list)
74 GList *path_list = filelist_to_path_list(list);
75 gchar **ret = uris_from_pathlist(path_list);
76 g_list_free_full(path_list, g_free);
80 gboolean uri_selection_data_set_uris_from_filelist(GtkSelectionData *selection_data, GList *list)
82 gchar **uris = uris_from_filelist(list);
83 gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
86 char *str = g_strjoinv("\r\n", uris);
87 ret = gtk_selection_data_set_text(selection_data, str, -1);
95 GList *uri_pathlist_from_uris(gchar **uris, GList **uri_error_list)
97 GList *list = nullptr;
99 GError *error = nullptr;
103 gchar *local_path = g_filename_from_uri(uris[i], nullptr, &error);
106 DEBUG_1("g_filename_from_uri failed on uri \"%s\"", uris[i]);
107 DEBUG_1(" error %d: %s", error->code, error->message);
108 if (error->code == G_CONVERT_ERROR_BAD_URI)
110 GError *retry_error = nullptr;
111 gchar *escaped = g_uri_escape_string(uris[i], ":/", TRUE);
112 local_path = g_filename_from_uri(escaped, nullptr, &retry_error);
115 DEBUG_1("manually escaped uri \"%s\" also failed g_filename_from_uri", escaped);
116 DEBUG_1(" error %d: %s", retry_error->code, retry_error->message);
117 g_error_free(retry_error);
125 *uri_error_list = g_list_prepend(*uri_error_list, g_strdup(uris[i]));
130 gchar *path = path_to_utf8(local_path);
132 list = g_list_prepend(list, path);
136 *uri_error_list = g_list_reverse(*uri_error_list);
137 return g_list_reverse(list);
140 GList *uri_filelist_from_uris(gchar **uris, GList **uri_error_list)
142 GList *path_list = uri_pathlist_from_uris(uris, uri_error_list);
143 GList *filelist = filelist_from_path_list(path_list);
144 g_list_free_full(path_list, g_free);
148 GList *uri_filelist_from_gtk_selection_data(GtkSelectionData *selection_data)
150 GList *errors = nullptr;
151 gchar **uris = gtk_selection_data_get_uris(selection_data);
152 GList *ret = uri_filelist_from_uris(uris, &errors);
155 warning_dialog_dnd_uri_error(errors);
156 g_list_free_full(errors, g_free);
164 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */