| 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 "container-search.h" |
| 30 |
#include "main.h" |
| 31 |
|
| 32 |
static void |
| 33 |
didl_container_available_cb (GUPnPDIDLLiteParser *parser, |
| 34 |
GUPnPDIDLLiteObject *container, |
| 35 |
gpointer user_data) |
| 36 |
{ |
| 37 |
char **container_id = (char **) user_data; |
| 38 |
|
| 39 |
if (!gupnp_didl_lite_object_get_restricted (container)) { |
| 40 |
/* Seems like we found a suitable container */ |
| 41 |
*container_id = g_strdup (gupnp_didl_lite_object_get_id |
| 42 |
(container)); |
| 43 |
g_print ("Automatically choosing '%s' (%s) as destination.\n", |
| 44 |
gupnp_didl_lite_object_get_title (container), |
| 45 |
*container_id); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
static char * |
| 50 |
parse_result (const char *result) |
| 51 |
{ |
| 52 |
GUPnPDIDLLiteParser *parser; |
| 53 |
char *container_id; |
| 54 |
GError *error; |
| 55 |
|
| 56 |
parser = gupnp_didl_lite_parser_new (); |
| 57 |
g_signal_connect (parser, |
| 58 |
"container-available", |
| 59 |
G_CALLBACK (didl_container_available_cb), |
| 60 |
&container_id); |
| 61 |
|
| 62 |
container_id = NULL; |
| 63 |
error = NULL; |
| 64 |
if (!gupnp_didl_lite_parser_parse_didl (parser, result, &error)) { |
| 65 |
g_critical ("Failed to parse result DIDL from MediaServer: %s", |
| 66 |
error->message); |
| 67 |
|
| 68 |
g_error_free (error); |
| 69 |
} |
| 70 |
|
| 71 |
g_object_unref (parser); |
| 72 |
|
| 73 |
return container_id; |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
browse_cb (GObject *object, GAsyncResult *res, gpointer user_data) |
| 78 |
{ |
| 79 |
g_autoptr (GError) error = NULL; |
| 80 |
g_autofree char *result = NULL; |
| 81 |
g_autofree char *container_id = NULL; |
| 82 |
GUPnPServiceProxyAction *action = NULL; |
| 83 |
GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object); |
| 84 |
|
| 85 |
action = gupnp_service_proxy_call_action_finish (proxy, res, &error); |
| 86 |
|
| 87 |
if (error != NULL) { |
| 88 |
g_critical ("Failed to browse root container: %s", |
| 89 |
error->message); |
| 90 |
application_exit (); |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
gupnp_service_proxy_action_get_result (action, |
| 96 |
&error, |
| 97 |
"Result", |
| 98 |
G_TYPE_STRING, |
| 99 |
&result, |
| 100 |
NULL); |
| 101 |
if (error != NULL) { |
| 102 |
g_critical ("Failed to browse root container: %s", |
| 103 |
error->message); |
| 104 |
application_exit (); |
| 105 |
|
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
g_assert (result != NULL); |
| 110 |
|
| 111 |
container_id = parse_result (result); |
| 112 |
if (container_id == NULL) { |
| 113 |
g_critical ("Failed to find a suitable container for upload."); |
| 114 |
|
| 115 |
application_exit (); |
| 116 |
} else { |
| 117 |
container_found (container_id); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
search_container (GUPnPServiceProxy *cds_proxy) |
| 123 |
{ |
| 124 |
GUPnPServiceProxyAction *action = NULL; |
| 125 |
action = gupnp_service_proxy_action_new ("Browse", |
| 126 |
/* IN args */ |
| 127 |
"ObjectID", |
| 128 |
G_TYPE_STRING, |
| 129 |
"0", |
| 130 |
"BrowseFlag", |
| 131 |
G_TYPE_STRING, |
| 132 |
"BrowseDirectChildren", |
| 133 |
"Filter", |
| 134 |
G_TYPE_STRING, |
| 135 |
"*", |
| 136 |
"StartingIndex", |
| 137 |
G_TYPE_UINT, |
| 138 |
0, |
| 139 |
"RequestedCount", |
| 140 |
G_TYPE_UINT, |
| 141 |
0, |
| 142 |
"SortCriteria", |
| 143 |
G_TYPE_STRING, |
| 144 |
"", |
| 145 |
NULL); |
| 146 |
|
| 147 |
gupnp_service_proxy_call_action_async (cds_proxy, |
| 148 |
action, |
| 149 |
NULL, |
| 150 |
browse_cb, |
| 151 |
NULL); |
| 152 |
|
| 153 |
gupnp_service_proxy_action_unref (action); |
| 154 |
} |
| 155 |
|