Debian Codegraph @

src/upload/item-creation.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 *
4 * Authors: Zeeshan Ali (Khattak) <[email protected]>
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,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include <libgupnp/gupnp.h>
24 #include <libgupnp-av/gupnp-av.h>
25 #include <gio/gio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "item-creation.h"
30 #include "main.h"
31
32 static const char *
33 guess_upnp_class_for_mime_type (const char *mime_type)
34 {
35 const char *upnp_class = NULL;
36
37 if (g_pattern_match_simple ("audio/*", mime_type)) {
38 upnp_class = "object.item.audioItem.musicTrack";
39 } else if (g_pattern_match_simple ("video/*", mime_type)) {
40 upnp_class = "object.item.videoItem";
41 } else if (g_pattern_match_simple ("image/*", mime_type)) {
42 upnp_class = "object.item.imageItem";
43 }
44
45 return upnp_class;
46 }
47
48 static const char *
49 create_res_for_file (const char *file_path,
50 GUPnPDIDLLiteObject *object)
51 {
52 GUPnPDIDLLiteResource *res;
53 GUPnPProtocolInfo *info;
54 char *content_type;
55 char *mime_type;
56 const char *upnp_class;
57
58 res = gupnp_didl_lite_object_add_resource (object);
59 gupnp_didl_lite_resource_set_uri (res, "");
60
61 content_type = g_content_type_guess (file_path, NULL, 0, NULL);
62 mime_type = g_content_type_get_mime_type (content_type);
63 upnp_class = guess_upnp_class_for_mime_type (mime_type);
64
65 info = gupnp_protocol_info_new ();
66 gupnp_protocol_info_set_mime_type (info, mime_type);
67 gupnp_protocol_info_set_protocol (info, "*");
68
69 gupnp_didl_lite_resource_set_protocol_info (res, info);
70
71 g_object_unref (info);
72 g_object_unref (res);
73 g_free (mime_type);
74 g_free (content_type);
75
76 return upnp_class;
77 }
78
79 static char *
80 create_didl_for_file (const char *file_path,
81 const char *title,
82 const char *parent_id)
83 {
84 GUPnPDIDLLiteWriter *writer;
85 GUPnPDIDLLiteObject *item;
86 char *didl;
87 char *new_title;
88 const char *upnp_class;
89
90 writer = gupnp_didl_lite_writer_new (NULL);
91
92 item = GUPNP_DIDL_LITE_OBJECT
93 (gupnp_didl_lite_writer_add_item (writer));
94 gupnp_didl_lite_object_set_parent_id (item, parent_id);
95 gupnp_didl_lite_object_set_id (item, "");
96 gupnp_didl_lite_object_set_restricted (item, FALSE);
97
98 if (title == NULL) {
99 new_title = g_path_get_basename (file_path);
100 } else {
101 new_title = g_strdup (title);
102 }
103 gupnp_didl_lite_object_set_title (item, new_title);
104 g_free (new_title);
105
106 upnp_class = create_res_for_file (file_path, item);
107 if (upnp_class == NULL) {
108 g_critical ("Failed to guess UPnP class for file '%s'",
109 file_path);
110
111 g_object_unref (writer);
112
113 return NULL;
114 } else
115 gupnp_didl_lite_object_set_upnp_class (item, upnp_class);
116
117 didl = gupnp_didl_lite_writer_get_string (writer);
118
119 g_object_unref (item);
120 g_object_unref (writer);
121
122 return didl;
123 }
124
125 static void
126 didl_item_available_cb (GUPnPDIDLLiteParser *parser,
127 GUPnPDIDLLiteObject *object,
128 gpointer user_data)
129 {
130 GList *resources;
131 GUPnPDIDLLiteResource *resource;
132 const char **import_uri = (const char **) user_data;
133
134 if (*import_uri != NULL) {
135 /* This means we've already found importURI. */
136 /* No need to search further. */
137 return;
138 }
139
140 resources = gupnp_didl_lite_object_get_resources (object);
141 if (resources == NULL) {
142 return;
143 }
144
145 resource = (GUPnPDIDLLiteResource *) resources->data;
146
147 *import_uri = gupnp_didl_lite_resource_get_import_uri(resource);
148 if (*import_uri != NULL) {
149 *import_uri = g_strdup (*import_uri);
150 }
151
152 while (resources) {
153 g_object_unref ((GUPnPDIDLLiteResource *) resources->data);
154 resources = g_list_delete_link (resources, resources);
155 }
156 }
157
158 static char *
159 parse_result (const char *result)
160 {
161 GUPnPDIDLLiteParser *parser;
162 GError *error;
163 const char *import_uri;
164
165 parser = gupnp_didl_lite_parser_new ();
166
167 g_signal_connect (parser,
168 "item-available",
169 G_CALLBACK (didl_item_available_cb),
170 &import_uri);
171
172 import_uri = NULL;
173 error = NULL;
174 if (!gupnp_didl_lite_parser_parse_didl (parser, result, &error)) {
175 g_critical ("Failed to parse result DIDL from MediaServer: %s",
176 error->message);
177
178 g_error_free (error);
179 }
180
181 g_object_unref (parser);
182
183 return g_strdup (import_uri);
184 }
185
186 static void
187 create_object_cb (GObject *object, GAsyncResult *res, gpointer user_data)
188 {
189 GError *error = NULL;
190 char *result;
191 const char *import_uri;
192 GUPnPServiceProxyAction *action;
193 GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object);
194
195 error = NULL;
196
197 action = gupnp_service_proxy_call_action_finish (proxy, res, &error);
198 if (error != NULL) {
199 g_critical ("CreateObject call failed: %s", error->message);
200 g_clear_error (&error);
201 item_created (NULL);
202
203 return;
204 }
205
206 if (!gupnp_service_proxy_action_get_result (action,
207 &error,
208 "Result",
209 G_TYPE_STRING,
210 &result,
211 NULL)) {
212 g_critical ("Failed to create new item on remote container: %s",
213 error->message);
214
215 g_error_free (error);
216
217 item_created (NULL);
218
219 return;
220 }
221
222 if (result == NULL) {
223 g_critical ("Failed to create new item on remote container."
224 "No reasons given by MediaServer.");
225
226 item_created (NULL);
227
228 return;
229 }
230
231 import_uri = parse_result (result);
232
233 item_created (import_uri);
234
235 g_free (result);
236 }
237
238 void
239 create_item (const char *file_path,
240 const char *title,
241 GUPnPServiceProxy *cds_proxy,
242 const char *container_id)
243 {
244 char *didl;
245 GUPnPServiceProxyAction *action;
246
247 didl = create_didl_for_file (file_path, title, container_id);
248 if (didl == NULL) {
249 item_created (NULL);
250
251 return;
252 }
253
254 action = gupnp_service_proxy_action_new ("CreateObject",
255 "ContainerID",
256 G_TYPE_STRING,
257 container_id,
258 "Elements",
259 G_TYPE_STRING,
260 didl,
261 NULL);
262
263 gupnp_service_proxy_call_action_async (cds_proxy,
264 action,
265 NULL,
266 create_object_cb,
267 NULL);
268
269 gupnp_service_proxy_action_unref (action);
270 }