Let image loader backend decide how to process image buffer
[geeqie.git] / src / uri-utils.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "uri-utils.h"
22
23 #include "debug.h"
24 #include "filedata.h"
25 #include "intl.h"
26 #include "main-defines.h"
27 #include "ui-fileops.h"
28 #include "ui-utildlg.h"
29
30 void warning_dialog_dnd_uri_error(GList *uri_error_list)
31 {
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);
35         if(count < 10)
36                 {
37                 while (work)
38                         {
39                         gchar *prev = msg;
40                         msg = g_strdup_printf("%s\n%s", prev, static_cast<gchar *>(work->data));
41                         work = work->next;
42                         g_free(prev);
43                         }
44                 }
45         warning_dialog(_("Drag and Drop failed"), msg, GQ_ICON_DIALOG_WARNING, nullptr);
46         g_free(msg);
47 }
48
49 gchar **uris_from_pathlist(GList *list)
50 {
51         GList *work;
52         guint i = 0;
53         guint num = g_list_length(list);
54         auto uris = g_new0(gchar *, num + 1);
55
56         work = list;
57         while (work)
58                 {
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);
62                 g_free(local_path);
63
64                 i++;
65                 work = work->next;
66                 }
67
68         uris[i] = nullptr;
69         return uris;
70 }
71
72 gchar **uris_from_filelist(GList *list)
73 {
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);
77         return ret;
78 }
79
80 gboolean uri_selection_data_set_uris_from_filelist(GtkSelectionData *selection_data, GList *list)
81 {
82         gchar **uris = uris_from_filelist(list);
83         gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
84         if (!ret)
85                 {
86                 char *str = g_strjoinv("\r\n", uris);
87                 ret = gtk_selection_data_set_text(selection_data, str, -1);
88                 g_free(str);
89                 }
90
91         g_strfreev(uris);
92         return ret;
93 }
94
95 GList *uri_pathlist_from_uris(gchar **uris, GList **uri_error_list)
96 {
97         GList *list = nullptr;
98         guint i = 0;
99         GError *error = nullptr;
100
101         while (uris[i])
102                 {
103                 gchar *local_path = g_filename_from_uri(uris[i], nullptr, &error);
104                 if (error)
105                         {
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)
109                                 {
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);
113                                 if(retry_error)
114                                         {
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);
118                                         }
119                                 g_free(escaped);
120                                 }
121                         g_error_free(error);
122                         error = nullptr;
123                         if (!local_path)
124                                 {
125                                 *uri_error_list = g_list_prepend(*uri_error_list, g_strdup(uris[i]));
126                                 i++;
127                                 continue;
128                                 }
129                         }
130                 gchar *path = path_to_utf8(local_path);
131                 g_free(local_path);
132                 list = g_list_prepend(list, path);
133                 i++;
134                 }
135
136         *uri_error_list = g_list_reverse(*uri_error_list);
137         return g_list_reverse(list);
138 }
139
140 GList *uri_filelist_from_uris(gchar **uris, GList **uri_error_list)
141 {
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);
145         return filelist;
146 }
147
148 GList *uri_filelist_from_gtk_selection_data(GtkSelectionData *selection_data)
149 {
150         GList *errors = nullptr;
151         gchar **uris = gtk_selection_data_get_uris(selection_data);
152         GList *ret = uri_filelist_from_uris(uris, &errors);
153         if(errors)
154                 {
155                 warning_dialog_dnd_uri_error(errors);
156                 g_list_free_full(errors, g_free);
157                 }
158         g_strfreev(uris);
159         return ret;
160 }
161
162
163
164 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */