1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "modest-mail-operation-queue.h"
33 /* 'private'/'protected' functions */
34 static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass);
35 static void modest_mail_operation_queue_init (ModestMailOperationQueue *obj);
36 static void modest_mail_operation_queue_finalize (GObject *obj);
38 static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
39 ModestMailOperationQueue *op_queue);
41 static void modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *op_queue,
42 ModestMailOperation *mail_op);
51 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
52 struct _ModestMailOperationQueuePrivate {
56 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
57 MODEST_TYPE_MAIL_OPERATION_QUEUE, \
58 ModestMailOperationQueuePrivate))
60 static GObjectClass *parent_class = NULL;
62 /* uncomment the following if you have defined any signals */
63 /* static guint signals[LAST_SIGNAL] = {0}; */
66 modest_mail_operation_queue_get_type (void)
68 static GType my_type = 0;
70 static const GTypeInfo my_info = {
71 sizeof(ModestMailOperationQueueClass),
73 NULL, /* base finalize */
74 (GClassInitFunc) modest_mail_operation_queue_class_init,
75 NULL, /* class finalize */
76 NULL, /* class data */
77 sizeof(ModestMailOperationQueue),
79 (GInstanceInitFunc) modest_mail_operation_queue_init,
83 my_type = g_type_register_static (G_TYPE_OBJECT,
84 "ModestMailOperationQueue",
91 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
93 GObjectClass *gobject_class;
95 gobject_class = (GObjectClass*) klass;
96 parent_class = g_type_class_peek_parent (klass);
98 gobject_class->finalize = modest_mail_operation_queue_finalize;
100 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
104 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
106 ModestMailOperationQueuePrivate *priv;
108 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
110 priv->op_queue = g_queue_new ();
111 priv->queue_lock = g_mutex_new ();
115 modest_mail_operation_queue_finalize (GObject *obj)
117 ModestMailOperationQueuePrivate *priv;
119 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
121 g_mutex_lock (priv->queue_lock);
123 if (priv->op_queue) {
124 if (!g_queue_is_empty (priv->op_queue))
125 g_queue_foreach (priv->op_queue, (GFunc) g_object_unref, NULL);
126 g_queue_free (priv->op_queue);
129 g_mutex_unlock (priv->queue_lock);
130 g_mutex_free (priv->queue_lock);
132 G_OBJECT_CLASS(parent_class)->finalize (obj);
135 ModestMailOperationQueue *
136 modest_mail_operation_queue_new (void)
138 ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
140 return MODEST_MAIL_OPERATION_QUEUE (self);
144 modest_mail_operation_queue_add (ModestMailOperationQueue *self,
145 ModestMailOperation *mail_op)
147 ModestMailOperationQueuePrivate *priv;
149 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
150 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
152 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
154 g_mutex_lock (priv->queue_lock);
155 g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
156 g_mutex_unlock (priv->queue_lock);
160 modest_mail_operation_queue_remove (ModestMailOperationQueue *self,
161 ModestMailOperation *mail_op)
163 ModestMailOperationQueuePrivate *priv;
165 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
166 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
168 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
170 g_mutex_lock (priv->queue_lock);
171 g_queue_remove (priv->op_queue, mail_op);
172 g_mutex_unlock (priv->queue_lock);
176 /* Utility function intended to be used with g_queue_foreach */
178 modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *self,
179 ModestMailOperationQueue *op_queue)
181 modest_mail_operation_queue_cancel_no_block (op_queue, self);
185 modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *self,
186 ModestMailOperation *mail_op)
188 if (modest_mail_operation_is_finished (mail_op))
191 /* TODO: the implementation is still empty */
192 modest_mail_operation_cancel (mail_op);
194 /* Remove from the queue */
195 modest_mail_operation_queue_remove (self, mail_op);
199 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self,
200 ModestMailOperation *mail_op)
202 ModestMailOperationQueuePrivate *priv;
204 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
205 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
207 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
209 g_mutex_lock (priv->queue_lock);
210 modest_mail_operation_queue_cancel_no_block (self, mail_op);
211 g_mutex_unlock (priv->queue_lock);
215 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
217 ModestMailOperationQueuePrivate *priv;
219 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
221 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
223 g_mutex_lock (priv->queue_lock);
224 g_queue_foreach (priv->op_queue,
225 (GFunc) modest_mail_operation_queue_cancel_no_block_wrapper,
227 g_mutex_unlock (priv->queue_lock);