Update media-service-upnp to version 0.3.0 ( ca17a69 )
[profile/ivi/media-service-upnp.git] / src / async.c
1 /*
2  * media-service-upnp
3  *
4  * Copyright (C) 2012 Intel Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU Lesser General Public License,
8  * version 2.1, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Mark Ryan <mark.d.ryan@intel.com>
20  *
21  */
22
23 #include "async.h"
24 #include "error.h"
25 #include "log.h"
26
27 msu_async_cb_data_t *msu_async_cb_data_new(msu_task_t *task,
28                                            msu_upnp_task_complete_t cb)
29 {
30         msu_async_cb_data_t *cb_data = g_new0(msu_async_cb_data_t, 1);
31
32         cb_data->type = task->type;
33         cb_data->task = task;
34         cb_data->cb = cb;
35
36         return cb_data;
37 }
38
39 void msu_async_cb_data_delete(msu_async_cb_data_t *cb_data)
40 {
41         if (cb_data) {
42                 switch (cb_data->type) {
43                 case MSU_TASK_GET_CHILDREN:
44                 case MSU_TASK_SEARCH:
45                         g_free(cb_data->ut.bas.root_path);
46                         if (cb_data->ut.bas.vbs)
47                                 g_ptr_array_unref(cb_data->ut.bas.vbs);
48                         break;
49                 case MSU_TASK_GET_PROP:
50                         g_free(cb_data->ut.get_prop.root_path);
51                         break;
52                 case MSU_TASK_GET_ALL_PROPS:
53                 case MSU_TASK_GET_RESOURCE:
54                         g_free(cb_data->ut.get_all.root_path);
55                         if (cb_data->ut.get_all.vb)
56                                 g_variant_builder_unref(cb_data->ut.get_all.vb);
57                         break;
58                 case MSU_TASK_UPLOAD_TO_ANY:
59                 case MSU_TASK_UPLOAD:
60                         g_free(cb_data->ut.upload.root_path);
61                         g_free(cb_data->ut.upload.mime_type);
62                         break;
63                 case MSU_TASK_CREATE_CONTAINER:
64                         g_free(cb_data->ut.create_container.root_path);
65                         break;
66                 case MSU_TASK_UPDATE_OBJECT:
67                         g_free(cb_data->ut.update.current_tag_value);
68                         g_free(cb_data->ut.update.new_tag_value);
69                         break;
70                 default:
71                         break;
72                 }
73
74                 g_free(cb_data->id);
75                 g_free(cb_data);
76         }
77 }
78
79 gboolean msu_async_complete_task(gpointer user_data)
80 {
81         msu_async_cb_data_t *cb_data = user_data;
82
83         MSU_LOG_DEBUG("Enter. Error %p", (void *) cb_data->error);
84         MSU_LOG_DEBUG_NL();
85
86         cb_data->cb(cb_data->task, cb_data->result, cb_data->error);
87         msu_async_cb_data_delete(cb_data);
88
89         return FALSE;
90 }
91
92 void msu_async_task_cancelled(GCancellable *cancellable, gpointer user_data)
93 {
94         msu_async_cb_data_t *cb_data = user_data;
95
96         gupnp_service_proxy_cancel_action(cb_data->proxy, cb_data->action);
97
98         if (!cb_data->error)
99                 cb_data->error = g_error_new(MSU_ERROR, MSU_ERROR_CANCELLED,
100                                              "Operation cancelled.");
101         (void) g_idle_add(msu_async_complete_task, cb_data);
102 }