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