]> git.0d.be Git - empathy.git/blob - libempathy/action-chain.c
Updated Czech translation
[empathy.git] / libempathy / action-chain.c
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 *
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
48 link_free (TplActionLink *l)
49 {
50   g_slice_free (TplActionLink, l);
51 }
52
53
54 void
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 *
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
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
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 void
108 _tpl_action_chain_start (TplActionChain *self)
109 {
110   g_return_if_fail (!g_queue_is_empty (self->chain));
111
112   if (self->running)
113     return;
114
115   _tpl_action_chain_continue (self);
116 }
117
118 void
119 _tpl_action_chain_continue (TplActionChain *self)
120 {
121   if (g_queue_is_empty (self->chain))
122     {
123       self->running = FALSE;
124       g_simple_async_result_complete (self->simple);
125     }
126   else
127     {
128       TplActionLink *l = g_queue_pop_head (self->chain);
129
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;
135     }
136 }
137
138
139 void
140 _tpl_action_chain_clear (TplActionChain *self)
141 {
142   g_queue_foreach (self->chain, (GFunc) link_free, NULL);
143   g_queue_clear (self->chain);
144 }
145
146 void
147 _tpl_action_chain_terminate (TplActionChain *self,
148     const GError *error)
149 {
150   GSimpleAsyncResult *simple = self->simple;
151
152   g_assert (error != NULL);
153
154   g_simple_async_result_set_from_error (simple, error);
155   g_simple_async_result_complete (simple);
156 }
157
158
159 /**
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
164  *
165  * Get the result from running the action chain (%TRUE if the chain completed
166  * successfully, %FALSE with @error set if it was terminated).
167  *
168  * This function also frees the chain.
169  *
170  * Returns: %TRUE on success, %FALSE with @error set on error.
171  */
172 gboolean
173 _tpl_action_chain_new_finish (GObject *source,
174     GAsyncResult *result,
175     GError **error)
176 {
177   TplActionChain *chain;
178
179   g_return_val_if_fail (g_simple_async_result_is_valid (result, source,
180         _tpl_action_chain_new_async), FALSE);
181
182   chain = g_object_get_data (G_OBJECT (result), "chain");
183
184   g_return_val_if_fail (chain != NULL, FALSE);
185
186   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
187         error))
188     return FALSE;
189
190   _tpl_action_chain_free (chain);
191   return TRUE;
192 }