Update dLeyna to v0.2.1
[profile/ivi/dLeyna.git] / dleyna-server / libdleyna / server / async.c
1 /*
2  * dLeyna
3  *
4  * Copyright (C) 2012-2013 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 <libdleyna/core/error.h>
24 #include <libdleyna/core/log.h>
25
26 #include "async.h"
27 #include "server.h"
28
29 void dls_async_task_delete(dls_async_task_t *cb_data)
30 {
31         switch (cb_data->task.type) {
32         case DLS_TASK_GET_CHILDREN:
33         case DLS_TASK_SEARCH:
34                 if (cb_data->ut.bas.vbs)
35                         g_ptr_array_unref(cb_data->ut.bas.vbs);
36                 break;
37         case DLS_TASK_MANAGER_GET_ALL_PROPS:
38         case DLS_TASK_GET_ALL_PROPS:
39         case DLS_TASK_GET_RESOURCE:
40                 if (cb_data->ut.get_all.vb)
41                         g_variant_builder_unref(cb_data->ut.get_all.vb);
42                 break;
43         case DLS_TASK_UPLOAD_TO_ANY:
44         case DLS_TASK_UPLOAD:
45                 g_free(cb_data->ut.upload.mime_type);
46                 break;
47         case DLS_TASK_UPDATE_OBJECT:
48                 g_free(cb_data->ut.update.current_tag_value);
49                 g_free(cb_data->ut.update.new_tag_value);
50                 break;
51         default:
52                 break;
53         }
54
55         if (cb_data->cancellable)
56                 g_object_unref(cb_data->cancellable);
57 }
58
59 gboolean dls_async_task_complete(gpointer user_data)
60 {
61         dls_async_task_t *cb_data = user_data;
62
63         DLEYNA_LOG_DEBUG("Enter. Error %p", (void *)cb_data->error);
64         DLEYNA_LOG_DEBUG_NL();
65
66         if (cb_data->proxy != NULL)
67                 g_object_remove_weak_pointer((G_OBJECT(cb_data->proxy)),
68                                              (gpointer *)&cb_data->proxy);
69
70         cb_data->cb(&cb_data->task, cb_data->error);
71
72         return FALSE;
73 }
74
75 void dls_async_task_cancelled_cb(GCancellable *cancellable, gpointer user_data)
76 {
77         dls_async_task_t *cb_data = user_data;
78
79         if (cb_data->proxy != NULL)
80                 gupnp_service_proxy_cancel_action(cb_data->proxy,
81                                                   cb_data->action);
82
83         if (!cb_data->error)
84                 cb_data->error = g_error_new(DLEYNA_SERVER_ERROR,
85                                              DLEYNA_ERROR_CANCELLED,
86                                              "Operation cancelled.");
87         (void) g_idle_add(dls_async_task_complete, cb_data);
88 }
89
90 void dls_async_task_cancel(dls_async_task_t *cb_data)
91 {
92         if (cb_data->cancellable)
93                 g_cancellable_cancel(cb_data->cancellable);
94 }