1 /*
2 * Copyright (C) 2009 Collabora Ltd.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
19 */
20
21 #include "config.h"
22 #include "action-chain-internal.h"
23
24 typedef struct {
25 TplPendingAction action;
26 gpointer user_data;
27 } TplActionLink;
28
29
30 TplActionChain *
_tpl_action_chain_new_async(GObject * obj,GAsyncReadyCallback cb,gpointer user_data)31 _tpl_action_chain_new_async (GObject *obj,
32 GAsyncReadyCallback cb,
33 gpointer user_data)
34 {
35 TplActionChain *ret = g_slice_new0 (TplActionChain);
36
37 ret->chain = g_queue_new ();
38 ret->simple = g_simple_async_result_new (obj, cb, user_data,
39 _tpl_action_chain_new_async);
40
41 g_object_set_data (G_OBJECT (ret->simple), "chain", ret);
42
43 return ret;
44 }
45
46
47 static void
link_free(TplActionLink * l)48 link_free (TplActionLink *l)
49 {
50 g_slice_free (TplActionLink, l);
51 }
52
53
54 void
_tpl_action_chain_free(TplActionChain * self)55 _tpl_action_chain_free (TplActionChain *self)
56 {
57 g_queue_foreach (self->chain, (GFunc) link_free, NULL);
58 g_queue_free (self->chain);
59 g_object_unref (self->simple);
60 g_slice_free (TplActionChain, self);
61 }
62
63
64 gpointer // FIXME GObject *
_tpl_action_chain_get_object(TplActionChain * self)65 _tpl_action_chain_get_object (TplActionChain *self)
66 {
67 GObject *obj;
68
69 g_return_val_if_fail (self != NULL && self->simple != NULL, NULL);
70
71 obj = g_async_result_get_source_object (G_ASYNC_RESULT (self->simple));
72 g_object_unref (obj); /* don't want the extra ref */
73
74 return obj;
75 }
76
77
78 void
_tpl_action_chain_prepend(TplActionChain * self,TplPendingAction func,gpointer user_data)79 _tpl_action_chain_prepend (TplActionChain *self,
80 TplPendingAction func,
81 gpointer user_data)
82 {
83 TplActionLink *l;
84
85 l = g_slice_new0 (TplActionLink);
86 l->action = func;
87 l->user_data = user_data;
88
89 g_queue_push_head (self->chain, l);
90 }
91
92
93 void
_tpl_action_chain_append(TplActionChain * self,TplPendingAction func,gpointer user_data)94 _tpl_action_chain_append (TplActionChain *self,
95 TplPendingAction func,
96 gpointer user_data)
97 {
98 TplActionLink *l;
99
100 l = g_slice_new0 (TplActionLink);
101 l->action = func;
102 l->user_data = user_data;
103
104 g_queue_push_tail (self->chain, l);
105 }
106
107
108 void
_tpl_action_chain_continue(TplActionChain * self)109 _tpl_action_chain_continue (TplActionChain *self)
110 {
111 if (g_queue_is_empty (self->chain))
112 {
113 g_simple_async_result_complete (self->simple);
114 _tpl_action_chain_free (self);
115 }
116 else
117 {
118 TplActionLink *l = g_queue_pop_head (self->chain);
119
120 l->action (self, l->user_data);
121 link_free (l);
122 }
123 }
124
125
126 void
_tpl_action_chain_terminate(TplActionChain * self,const GError * error)127 _tpl_action_chain_terminate (TplActionChain *self,
128 const GError *error)
129 {
130 GSimpleAsyncResult *simple = self->simple;
131
132 g_assert (error != NULL);
133
134 g_simple_async_result_set_from_error (simple, error);
135 g_simple_async_result_complete (simple);
136 _tpl_action_chain_free (self);
137 }
138
139
140 /**
141 * _tpl_action_chain_new_finish:
142 * @source: the #GObject pass to _tpl_action_chain_new_async()
143 * @result: the #GAsyncResult pass in callback
144 * @error: a pointer to a #GError that will be set on error, or NULL to ignore
145 *
146 * Get the result from running the action chain (%TRUE if the chain completed
147 * successfully, %FALSE with @error set if it was terminated).
148 *
149 * This function also frees the chain.
150 *
151 * Returns: %TRUE on success, %FALSE with @error set on error.
152 */
153 gboolean
_tpl_action_chain_new_finish(GObject * source,GAsyncResult * result,GError ** error)154 _tpl_action_chain_new_finish (GObject *source,
155 GAsyncResult *result,
156 GError **error)
157 {
158 TplActionChain *chain;
159
160 g_return_val_if_fail (g_simple_async_result_is_valid (result, source,
161 _tpl_action_chain_new_async), FALSE);
162
163 chain = g_object_get_data (G_OBJECT (result), "chain");
164
165 g_return_val_if_fail (chain != NULL, FALSE);
166
167 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
168 error))
169 return FALSE;
170
171 return TRUE;
172 }
173