5b81223c298e8e4054b13a5556e3c8cfbc13c8c4
[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
19 gchar **uris_from_pathlist(GList *list)
20 {
21         GList *work;
22         guint i = 0;
23         guint num = g_list_length(list);
24         gchar **uris = g_new0(gchar *, num + 1);
25
26         work = list;
27         while (work)
28                 {
29                 const gchar *path = work->data;
30                 gchar *local_path = path_from_utf8(path);
31                 uris[i] = g_filename_to_uri(local_path, NULL, NULL);
32                 g_free(local_path);
33                 
34                 i++;
35                 work = work->next;
36                 }
37
38         uris[i] = NULL;
39         return uris;
40 }
41
42 gchar **uris_from_filelist(GList *list)
43 {
44         GList *path_list = filelist_to_path_list(list);
45         gchar **ret = uris_from_pathlist(path_list);
46         string_list_free(path_list);
47         return ret;
48 }
49
50 gboolean uri_selection_data_set_uris_from_filelist(GtkSelectionData *selection_data, GList *list)
51 {
52         gchar **uris = uris_from_filelist(list);
53         gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
54         if (!ret) 
55                 {
56                 char *str = g_strjoinv("\r\n", uris);
57                 ret = gtk_selection_data_set_text(selection_data, str, -1);
58                 g_free(str);
59                 }
60
61         g_strfreev(uris);
62         return ret;
63 }
64
65 GList *uri_pathlist_from_uris(gchar **uris)
66 {
67         GList *list = NULL;
68         guint i = 0;
69
70         while (uris[i])
71                 {
72                 gchar *local_path = g_filename_from_uri(uris[i], NULL, NULL);
73                 gchar *path = path_to_utf8(local_path);
74                 g_free(local_path);
75                 list = g_list_prepend(list, path);
76                 i++;
77                 }
78
79         return g_list_reverse(list);
80 }
81
82
83
84 GList *uri_filelist_from_uris(gchar **uris)
85 {
86         GList *path_list = uri_pathlist_from_uris(uris);
87         GList *filelist = filelist_from_path_list(path_list);
88         string_list_free(path_list);
89         return filelist;
90 }
91
92 GList *uri_filelist_from_gtk_selection_data(GtkSelectionData *selection_data)
93 {
94         gchar **uris = gtk_selection_data_get_uris(selection_data);
95         GList *ret = uri_filelist_from_uris(uris);
96         g_strfreev(uris);
97         return ret;
98 }
99
100
101
102 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */