| 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 <string.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#include "transfer.h" |
| 28 |
#include "main.h" |
| 29 |
|
| 30 |
typedef struct |
| 31 |
{ |
| 32 |
GUPnPServiceProxy *cds_proxy; |
| 33 |
guint transfer_id; |
| 34 |
|
| 35 |
guint timeout_id; |
| 36 |
|
| 37 |
GUPnPServiceProxyAction *action; |
| 38 |
} TrackTransferData; |
| 39 |
|
| 40 |
static void |
| 41 |
get_transfer_progress_cb (GObject *object, |
| 42 |
GAsyncResult *result, |
| 43 |
gpointer user_data) |
| 44 |
{ |
| 45 |
g_autoptr (GError) error = NULL; |
| 46 |
TrackTransferData *data; |
| 47 |
guint64 total = 0, length = 0; |
| 48 |
g_autofree gchar *status = NULL; |
| 49 |
GUPnPServiceProxyAction *action; |
| 50 |
|
| 51 |
data = (TrackTransferData *) user_data; |
| 52 |
|
| 53 |
total = length = 0; |
| 54 |
status = NULL; |
| 55 |
action = gupnp_service_proxy_call_action_finish ( |
| 56 |
GUPNP_SERVICE_PROXY (object), |
| 57 |
result, |
| 58 |
&error); |
| 59 |
if (error == NULL) { |
| 60 |
gupnp_service_proxy_action_get_result (action, &error, NULL); |
| 61 |
} |
| 62 |
|
| 63 |
if (error != NULL) { |
| 64 |
g_critical ("Failed to track file transfer: %s", |
| 65 |
error->message); |
| 66 |
g_clear_error (&error); |
| 67 |
|
| 68 |
transfer_completed (); |
| 69 |
|
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (!gupnp_service_proxy_action_get_result (data->action, |
| 74 |
&error, |
| 75 |
"TransferStatus", |
| 76 |
G_TYPE_STRING, |
| 77 |
&status, |
| 78 |
"TransferLength", |
| 79 |
G_TYPE_UINT64, |
| 80 |
&length, |
| 81 |
"TransferTotal", |
| 82 |
G_TYPE_UINT64, |
| 83 |
&total, |
| 84 |
NULL)) { |
| 85 |
g_critical ("Failed to track file transfer: %s", |
| 86 |
error->message); |
| 87 |
|
| 88 |
g_error_free (error); |
| 89 |
|
| 90 |
transfer_completed (); |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if (g_strcmp0 (status, "IN_PROGRESS") == 0) { |
| 96 |
if (total > 0) { |
| 97 |
guint64 progress = length * 100 / total; |
| 98 |
|
| 99 |
g_print ("\b\b\b%2" G_GUINT64_FORMAT "%%", progress); |
| 100 |
} |
| 101 |
|
| 102 |
data->action = NULL; |
| 103 |
|
| 104 |
g_free (status); |
| 105 |
} else { |
| 106 |
g_print ("\b\b\bDone.\n"); |
| 107 |
|
| 108 |
g_free (status); |
| 109 |
g_source_remove (data->timeout_id); |
| 110 |
g_slice_free (TrackTransferData, data); |
| 111 |
|
| 112 |
transfer_completed (); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static gboolean |
| 117 |
track_transfer (gpointer user_data) |
| 118 |
{ |
| 119 |
TrackTransferData *data = (TrackTransferData *) user_data; |
| 120 |
|
| 121 |
if (data->action != NULL) { |
| 122 |
/* Previous action call still in progress */ |
| 123 |
return TRUE; |
| 124 |
} |
| 125 |
|
| 126 |
data->action = gupnp_service_proxy_action_new ("GetTransferProgress", |
| 127 |
"TransferID", |
| 128 |
G_TYPE_UINT, |
| 129 |
data->transfer_id, |
| 130 |
NULL); |
| 131 |
|
| 132 |
gupnp_service_proxy_call_action_async (data->cds_proxy, |
| 133 |
data->action, |
| 134 |
NULL, |
| 135 |
get_transfer_progress_cb, |
| 136 |
data); |
| 137 |
|
| 138 |
gupnp_service_proxy_action_unref (data->action); |
| 139 |
|
| 140 |
return TRUE; |
| 141 |
} |
| 142 |
|
| 143 |
static void |
| 144 |
start_tracking_transfer (GUPnPServiceProxy *cds_proxy, |
| 145 |
guint transfer_id) |
| 146 |
{ |
| 147 |
TrackTransferData *data; |
| 148 |
|
| 149 |
data = g_slice_new0 (TrackTransferData); |
| 150 |
data->cds_proxy = cds_proxy; |
| 151 |
data->transfer_id = transfer_id; |
| 152 |
|
| 153 |
/* Check progress every second */ |
| 154 |
data->timeout_id = g_timeout_add_seconds (1, |
| 155 |
track_transfer, |
| 156 |
data); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
import_resource_cb (GObject *object, GAsyncResult *result, gpointer user_data) |
| 161 |
{ |
| 162 |
GError *error; |
| 163 |
guint transfer_id; |
| 164 |
char *file_name; |
| 165 |
GUPnPServiceProxyAction *action; |
| 166 |
GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object); |
| 167 |
|
| 168 |
file_name = (gchar *) user_data; |
| 169 |
|
| 170 |
error = NULL; |
| 171 |
action = gupnp_service_proxy_call_action_finish (proxy, result, &error); |
| 172 |
if (error != NULL) { |
| 173 |
g_critical ("Failed to start file transfer: %s", |
| 174 |
error->message); |
| 175 |
|
| 176 |
g_free (file_name); |
| 177 |
g_error_free (error); |
| 178 |
|
| 179 |
transfer_completed (); |
| 180 |
|
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
if (!gupnp_service_proxy_action_get_result (action, |
| 185 |
&error, |
| 186 |
"TransferID", |
| 187 |
G_TYPE_UINT, |
| 188 |
&transfer_id, |
| 189 |
NULL)) { |
| 190 |
g_critical ("Failed to start file transfer: %s", |
| 191 |
error->message); |
| 192 |
|
| 193 |
g_free (file_name); |
| 194 |
g_error_free (error); |
| 195 |
|
| 196 |
transfer_completed (); |
| 197 |
|
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
g_print ("Uploading %s: 00%%", file_name); |
| 202 |
start_tracking_transfer (GUPNP_SERVICE_PROXY (object), transfer_id); |
| 203 |
|
| 204 |
g_free (file_name); |
| 205 |
} |
| 206 |
|
| 207 |
void |
| 208 |
start_transfer (const char *file_path, |
| 209 |
const char *dest_uri, |
| 210 |
GUPnPServiceProxy *cds_proxy, |
| 211 |
GUPnPContext *context) |
| 212 |
{ |
| 213 |
char *source_uri; |
| 214 |
char *file_name; |
| 215 |
GUPnPServiceProxyAction *action; |
| 216 |
|
| 217 |
if (!g_path_is_absolute (file_path)) { |
| 218 |
g_critical ("Given file path '%s' is not absolute.", file_path); |
| 219 |
|
| 220 |
transfer_completed (); |
| 221 |
|
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
/* Time to host the file */ |
| 226 |
gupnp_context_host_path (context, file_path, file_path); |
| 227 |
|
| 228 |
source_uri = g_strdup_printf ("http://%s:%u%s", |
| 229 |
gssdp_client_get_host_ip (GSSDP_CLIENT (context)), |
| 230 |
gupnp_context_get_port (context), |
| 231 |
file_path); |
| 232 |
|
| 233 |
file_name = g_path_get_basename (file_path); |
| 234 |
action = gupnp_service_proxy_action_new ("ImportResource", |
| 235 |
"SourceURI", |
| 236 |
G_TYPE_STRING, |
| 237 |
source_uri, |
| 238 |
"DestinationURI", |
| 239 |
G_TYPE_STRING, |
| 240 |
dest_uri, |
| 241 |
NULL); |
| 242 |
|
| 243 |
gupnp_service_proxy_call_action_async (cds_proxy, |
| 244 |
action, |
| 245 |
NULL, |
| 246 |
import_resource_cb, |
| 247 |
file_name); |
| 248 |
|
| 249 |
gupnp_service_proxy_action_unref (action); |
| 250 |
|
| 251 |
g_free (source_uri); |
| 252 |
} |
| 253 |
|