ceec7569692e402ce02899b88abd69dab7f142d3
[geeqie.git] / src / uri_utils.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 - 2012 The Geeqie Team
4  *
5  * Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
6  *
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include "main.h"
14 #include "uri_utils.h"
15
16 #include "filedata.h"
17 #include "ui_fileops.h"
18 #include "ui_utildlg.h"
19
20 void warning_dialog_dnd_uri_error(GList *uri_error_list)
21 {
22         GList *work = uri_error_list;
23         guint count = g_list_length(work);
24         gchar *msg = g_strdup_printf("Failed to convert %d dropped item(s) to files\n", count);
25         if(count < 10)
26                 {
27                 while (work)
28                         {
29                         gchar *prev = msg;
30                         msg = g_strdup_printf("%s\n%s", prev, (gchar *)work->data);
31                         work = work->next;
32                         g_free(prev);
33                         }
34                 }
35         warning_dialog(_("Drag and Drop failed"), msg, GTK_STOCK_DIALOG_WARNING, NULL);
36         g_free(msg);
37 }
38
39 gchar **uris_from_pathlist(GList *list)
40 {
41         GList *work;
42         guint i = 0;
43         guint num = g_list_length(list);
44         gchar **uris = g_new0(gchar *, num + 1);
45
46         work = list;
47         while (work)
48                 {
49                 const gchar *path = work->data;
50                 gchar *local_path = path_from_utf8(path);
51                 uris[i] = g_filename_to_uri(local_path, NULL, NULL);
52                 g_free(local_path);
53
54                 i++;
55                 work = work->next;
56                 }
57
58         uris[i] = NULL;
59         return uris;
60 }
61
62 gchar **uris_from_filelist(GList *list)
63 {
64         GList *path_list = filelist_to_path_list(list);
65         gchar **ret = uris_from_pathlist(path_list);
66         string_list_free(path_list);
67         return ret;
68 }
69
70 gboolean uri_selection_data_set_uris_from_filelist(GtkSelectionData *selection_data, GList *list)
71 {
72         gchar **uris = uris_from_filelist(list);
73         gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
74         if (!ret)
75                 {
76                 char *str = g_strjoinv("\r\n", uris);
77                 ret = gtk_selection_data_set_text(selection_data, str, -1);
78                 g_free(str);
79                 }
80
81         g_strfreev(uris);
82         return ret;
83 }
84
85 GList *uri_pathlist_from_uris(gchar **uris, GList **uri_error_list)
86 {
87         GList *list = NULL;
88         guint i = 0;
89         GError *error = NULL;
90
91         while (uris[i])
92                 {
93                 gchar *local_path = g_filename_from_uri(uris[i], NULL, &error);
94                 if (error)
95                         {
96                         DEBUG_1("g_filename_from_uri failed on uri \"%s\"", uris[i]);
97                         DEBUG_1("   error %d: %s", error->code, error->message);
98                         if (error->code == G_CONVERT_ERROR_BAD_URI)
99                                 {
100                                 GError *retry_error = NULL;
101                                 gchar *escaped = g_uri_escape_string(uris[i], ":/", TRUE);
102                                 local_path = g_filename_from_uri(escaped, NULL, &retry_error);
103                                 if(retry_error)
104                                         {
105                                         DEBUG_1("manually escaped uri \"%s\" also failed g_filename_from_uri", escaped);
106                                         DEBUG_1("   error %d: %s", retry_error->code, retry_error->message);
107                                         g_error_free(retry_error);
108                                         }
109                                 g_free(escaped);
110                                 }
111                         g_error_free(error);
112                         error = NULL;
113                         if (!local_path)
114                                 {
115                                 *uri_error_list = g_list_prepend(*uri_error_list, g_strdup(uris[i]));
116                                 i++;
117                                 continue;
118                                 }
119                         }
120                 gchar *path = path_to_utf8(local_path);
121                 g_free(local_path);
122                 list = g_list_prepend(list, path);
123                 i++;
124                 }
125
126         *uri_error_list = g_list_reverse(*uri_error_list);
127         return g_list_reverse(list);
128 }
129
130 GList *uri_filelist_from_uris(gchar **uris, GList **uri_error_list)
131 {
132         GList *path_list = uri_pathlist_from_uris(uris, uri_error_list);
133         GList *filelist = filelist_from_path_list(path_list);
134         string_list_free(path_list);
135         return filelist;
136 }
137
138 GList *uri_filelist_from_gtk_selection_data(GtkSelectionData *selection_data)
139 {
140         GList *errors = NULL;
141         gchar **uris = gtk_selection_data_get_uris(selection_data);
142         GList *ret = uri_filelist_from_uris(uris, &errors);
143         if(errors)
144                 {
145                 warning_dialog_dnd_uri_error(errors);
146                 string_list_free(errors);
147                 }
148         g_strfreev(uris);
149         return ret;
150 }
151
152
153
154 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */