Simplify vflist_get_formatted()
[geeqie.git] / src / uri_utils.c
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 "main.h"
22 #include "uri_utils.h"
23
24 #include "filedata.h"
25 #include "ui_fileops.h"
26 #include "ui_utildlg.h"
27
28 void warning_dialog_dnd_uri_error(GList *uri_error_list)
29 {
30         GList *work = uri_error_list;
31         guint count = g_list_length(work);
32         gchar *msg = g_strdup_printf("Failed to convert %d dropped item(s) to files\n", count);
33         if(count < 10)
34                 {
35                 while (work)
36                         {
37                         gchar *prev = msg;
38                         msg = g_strdup_printf("%s\n%s", prev, (gchar *)work->data);
39                         work = work->next;
40                         g_free(prev);
41                         }
42                 }
43         warning_dialog(_("Drag and Drop failed"), msg, GTK_STOCK_DIALOG_WARNING, NULL);
44         g_free(msg);
45 }
46
47 gchar **uris_from_pathlist(GList *list)
48 {
49         GList *work;
50         guint i = 0;
51         guint num = g_list_length(list);
52         gchar **uris = g_new0(gchar *, num + 1);
53
54         work = list;
55         while (work)
56                 {
57                 const gchar *path = work->data;
58                 gchar *local_path = path_from_utf8(path);
59                 uris[i] = g_filename_to_uri(local_path, NULL, NULL);
60                 g_free(local_path);
61
62                 i++;
63                 work = work->next;
64                 }
65
66         uris[i] = NULL;
67         return uris;
68 }
69
70 gchar **uris_from_filelist(GList *list)
71 {
72         GList *path_list = filelist_to_path_list(list);
73         gchar **ret = uris_from_pathlist(path_list);
74         string_list_free(path_list);
75         return ret;
76 }
77
78 gboolean uri_selection_data_set_uris_from_filelist(GtkSelectionData *selection_data, GList *list)
79 {
80         gchar **uris = uris_from_filelist(list);
81         gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
82         if (!ret)
83                 {
84                 char *str = g_strjoinv("\r\n", uris);
85                 ret = gtk_selection_data_set_text(selection_data, str, -1);
86                 g_free(str);
87                 }
88
89         g_strfreev(uris);
90         return ret;
91 }
92
93 GList *uri_pathlist_from_uris(gchar **uris, GList **uri_error_list)
94 {
95         GList *list = NULL;
96         guint i = 0;
97         GError *error = NULL;
98
99         while (uris[i])
100                 {
101                 gchar *local_path = g_filename_from_uri(uris[i], NULL, &error);
102                 if (error)
103                         {
104                         DEBUG_1("g_filename_from_uri failed on uri \"%s\"", uris[i]);
105                         DEBUG_1("   error %d: %s", error->code, error->message);
106                         if (error->code == G_CONVERT_ERROR_BAD_URI)
107                                 {
108                                 GError *retry_error = NULL;
109                                 gchar *escaped = g_uri_escape_string(uris[i], ":/", TRUE);
110                                 local_path = g_filename_from_uri(escaped, NULL, &retry_error);
111                                 if(retry_error)
112                                         {
113                                         DEBUG_1("manually escaped uri \"%s\" also failed g_filename_from_uri", escaped);
114                                         DEBUG_1("   error %d: %s", retry_error->code, retry_error->message);
115                                         g_error_free(retry_error);
116                                         }
117                                 g_free(escaped);
118                                 }
119                         g_error_free(error);
120                         error = NULL;
121                         if (!local_path)
122                                 {
123                                 *uri_error_list = g_list_prepend(*uri_error_list, g_strdup(uris[i]));
124                                 i++;
125                                 continue;
126                                 }
127                         }
128                 gchar *path = path_to_utf8(local_path);
129                 g_free(local_path);
130                 list = g_list_prepend(list, path);
131                 i++;
132                 }
133
134         *uri_error_list = g_list_reverse(*uri_error_list);
135         return g_list_reverse(list);
136 }
137
138 GList *uri_filelist_from_uris(gchar **uris, GList **uri_error_list)
139 {
140         GList *path_list = uri_pathlist_from_uris(uris, uri_error_list);
141         GList *filelist = filelist_from_path_list(path_list);
142         string_list_free(path_list);
143         return filelist;
144 }
145
146 GList *uri_filelist_from_gtk_selection_data(GtkSelectionData *selection_data)
147 {
148         GList *errors = NULL;
149         gchar **uris = gtk_selection_data_get_uris(selection_data);
150         GList *ret = uri_filelist_from_uris(uris, &errors);
151         if(errors)
152                 {
153                 warning_dialog_dnd_uri_error(errors);
154                 string_list_free(errors);
155                 }
156         g_strfreev(uris);
157         return ret;
158 }
159
160
161
162 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */