2 * Copyright (C) 2009 Collabora Ltd.
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.
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.
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
18 * Authors: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
22 #include "action-chain-internal.h"
25 TplPendingAction action
;
31 _tpl_action_chain_new_async (GObject
*obj
,
32 GAsyncReadyCallback cb
,
35 TplActionChain
*ret
= g_slice_new0 (TplActionChain
);
37 ret
->chain
= g_queue_new ();
38 ret
->simple
= g_simple_async_result_new (obj
, cb
, user_data
,
39 _tpl_action_chain_new_async
);
41 g_object_set_data (G_OBJECT (ret
->simple
), "chain", ret
);
48 link_free (TplActionLink
*l
)
50 g_slice_free (TplActionLink
, l
);
55 _tpl_action_chain_free (TplActionChain
*self
)
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
);
64 gpointer
// FIXME GObject *
65 _tpl_action_chain_get_object (TplActionChain
*self
)
69 g_return_val_if_fail (self
!= NULL
&& self
->simple
!= NULL
, NULL
);
71 obj
= g_async_result_get_source_object (G_ASYNC_RESULT (self
->simple
));
72 g_object_unref (obj
); /* don't want the extra ref */
79 _tpl_action_chain_prepend (TplActionChain
*self
,
80 TplPendingAction func
,
85 l
= g_slice_new0 (TplActionLink
);
87 l
->user_data
= user_data
;
89 g_queue_push_head (self
->chain
, l
);
94 _tpl_action_chain_append (TplActionChain
*self
,
95 TplPendingAction func
,
100 l
= g_slice_new0 (TplActionLink
);
102 l
->user_data
= user_data
;
104 g_queue_push_tail (self
->chain
, l
);
108 _tpl_action_chain_start (TplActionChain
*self
)
110 g_return_if_fail (!g_queue_is_empty (self
->chain
));
115 _tpl_action_chain_continue (self
);
119 _tpl_action_chain_continue (TplActionChain
*self
)
121 if (g_queue_is_empty (self
->chain
))
123 self
->running
= FALSE
;
124 g_simple_async_result_complete (self
->simple
);
128 TplActionLink
*l
= g_queue_pop_head (self
->chain
);
130 self
->running
= TRUE
;
131 l
->action (self
, l
->user_data
);
133 if (g_queue_is_empty (self
->chain
))
134 self
->running
= FALSE
;
140 _tpl_action_chain_clear (TplActionChain
*self
)
142 g_queue_foreach (self
->chain
, (GFunc
) link_free
, NULL
);
143 g_queue_clear (self
->chain
);
147 _tpl_action_chain_terminate (TplActionChain
*self
,
150 GSimpleAsyncResult
*simple
= self
->simple
;
152 g_assert (error
!= NULL
);
154 g_simple_async_result_set_from_error (simple
, error
);
155 g_simple_async_result_complete (simple
);
160 * _tpl_action_chain_new_finish:
161 * @source: the #GObject pass to _tpl_action_chain_new_async()
162 * @result: the #GAsyncResult pass in callback
163 * @error: a pointer to a #GError that will be set on error, or NULL to ignore
165 * Get the result from running the action chain (%TRUE if the chain completed
166 * successfully, %FALSE with @error set if it was terminated).
168 * This function also frees the chain.
170 * Returns: %TRUE on success, %FALSE with @error set on error.
173 _tpl_action_chain_new_finish (GObject
*source
,
174 GAsyncResult
*result
,
177 TplActionChain
*chain
;
179 g_return_val_if_fail (g_simple_async_result_is_valid (result
, source
,
180 _tpl_action_chain_new_async
), FALSE
);
182 chain
= g_object_get_data (G_OBJECT (result
), "chain");
184 g_return_val_if_fail (chain
!= NULL
, FALSE
);
186 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result
),
190 _tpl_action_chain_free (chain
);