Fix broken markup in Finnish user docs translation
[empathy-mirror.git] / libempathy / action-chain.c
blobb6bf25ab9eb8f8efeeda445f61d3054c0c23ddbd
1 /*
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>
21 #include "config.h"
22 #include "action-chain-internal.h"
24 typedef struct {
25 TplPendingAction action;
26 gpointer user_data;
27 } TplActionLink;
30 TplActionChain *
31 _tpl_action_chain_new_async (GObject *obj,
32 GAsyncReadyCallback cb,
33 gpointer user_data)
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);
43 return ret;
47 static void
48 link_free (TplActionLink *l)
50 g_slice_free (TplActionLink, l);
54 void
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)
67 GObject *obj;
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 */
74 return obj;
78 void
79 _tpl_action_chain_prepend (TplActionChain *self,
80 TplPendingAction func,
81 gpointer user_data)
83 TplActionLink *l;
85 l = g_slice_new0 (TplActionLink);
86 l->action = func;
87 l->user_data = user_data;
89 g_queue_push_head (self->chain, l);
93 void
94 _tpl_action_chain_append (TplActionChain *self,
95 TplPendingAction func,
96 gpointer user_data)
98 TplActionLink *l;
100 l = g_slice_new0 (TplActionLink);
101 l->action = func;
102 l->user_data = user_data;
104 g_queue_push_tail (self->chain, l);
107 void
108 _tpl_action_chain_start (TplActionChain *self)
110 g_return_if_fail (!g_queue_is_empty (self->chain));
112 if (self->running)
113 return;
115 _tpl_action_chain_continue (self);
118 void
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);
126 else
128 TplActionLink *l = g_queue_pop_head (self->chain);
130 self->running = TRUE;
131 l->action (self, l->user_data);
132 link_free (l);
133 if (g_queue_is_empty (self->chain))
134 self->running = FALSE;
139 void
140 _tpl_action_chain_clear (TplActionChain *self)
142 g_queue_foreach (self->chain, (GFunc) link_free, NULL);
143 g_queue_clear (self->chain);
146 void
147 _tpl_action_chain_terminate (TplActionChain *self,
148 const GError *error)
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.
172 gboolean
173 _tpl_action_chain_new_finish (GObject *source,
174 GAsyncResult *result,
175 GError **error)
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),
187 error))
188 return FALSE;
190 _tpl_action_chain_free (chain);
191 return TRUE;