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 GObject *modest_mail_operation_queue_constructor (GType type, guint n_construct_params,
39 GObjectConstructParam *construct_params);
41 static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
42 ModestMailOperationQueue *op_queue);
44 static void modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *op_queue,
45 ModestMailOperation *mail_op);
54 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
55 struct _ModestMailOperationQueuePrivate {
59 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
60 MODEST_TYPE_MAIL_OPERATION_QUEUE, \
61 ModestMailOperationQueuePrivate))
63 static GObjectClass *parent_class = NULL;
64 static ModestMailOperationQueue *singleton = NULL;
66 /* uncomment the following if you have defined any signals */
67 /* static guint signals[LAST_SIGNAL] = {0}; */
70 modest_mail_operation_queue_get_type (void)
72 static GType my_type = 0;
74 static const GTypeInfo my_info = {
75 sizeof(ModestMailOperationQueueClass),
77 NULL, /* base finalize */
78 (GClassInitFunc) modest_mail_operation_queue_class_init,
79 NULL, /* class finalize */
80 NULL, /* class data */
81 sizeof(ModestMailOperationQueue),
83 (GInstanceInitFunc) modest_mail_operation_queue_init,
87 my_type = g_type_register_static (G_TYPE_OBJECT,
88 "ModestMailOperationQueue",
95 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
97 GObjectClass *gobject_class;
99 gobject_class = (GObjectClass*) klass;
100 parent_class = g_type_class_peek_parent (klass);
102 gobject_class->finalize = modest_mail_operation_queue_finalize;
103 gobject_class->constructor = modest_mail_operation_queue_constructor;
105 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
109 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
111 ModestMailOperationQueuePrivate *priv;
113 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
115 priv->op_queue = g_queue_new ();
116 priv->queue_lock = g_mutex_new ();
120 modest_mail_operation_queue_constructor (GType type, guint n_construct_params,
121 GObjectConstructParam *construct_params)
126 object = G_OBJECT_CLASS (parent_class)->constructor (type,
127 n_construct_params, construct_params);
129 singleton = MODEST_MAIL_OPERATION_QUEUE (object);
131 object = G_OBJECT (singleton);
132 g_object_freeze_notify (G_OBJECT (singleton));
139 modest_mail_operation_queue_finalize (GObject *obj)
141 ModestMailOperationQueuePrivate *priv;
143 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
145 g_mutex_lock (priv->queue_lock);
147 if (priv->op_queue) {
148 if (!g_queue_is_empty (priv->op_queue))
149 g_queue_foreach (priv->op_queue, (GFunc) g_object_unref, NULL);
150 g_queue_free (priv->op_queue);
153 g_mutex_unlock (priv->queue_lock);
154 g_mutex_free (priv->queue_lock);
156 G_OBJECT_CLASS(parent_class)->finalize (obj);
159 ModestMailOperationQueue *
160 modest_mail_operation_queue_get_instance (void)
162 ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
164 return MODEST_MAIL_OPERATION_QUEUE (self);
168 modest_mail_operation_queue_add (ModestMailOperationQueue *op_queue,
169 ModestMailOperation *mail_op)
171 ModestMailOperationQueuePrivate *priv;
173 if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
174 !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
175 g_warning ("%s: bad parametters", G_GNUC_FUNCTION);
179 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
181 g_mutex_lock (priv->queue_lock);
182 g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
183 g_mutex_unlock (priv->queue_lock);
187 modest_mail_operation_queue_remove (ModestMailOperationQueue *op_queue,
188 ModestMailOperation *mail_op)
190 ModestMailOperationQueuePrivate *priv;
192 if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
193 !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
194 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
198 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
200 g_mutex_lock (priv->queue_lock);
201 g_queue_remove (priv->op_queue, mail_op);
202 g_mutex_unlock (priv->queue_lock);
204 g_object_unref (G_OBJECT (mail_op));
208 /* Utility function intended to be used with g_queue_foreach */
210 modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
211 ModestMailOperationQueue *op_queue)
213 modest_mail_operation_queue_cancel_no_block (op_queue, mail_op);
217 modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *op_queue,
218 ModestMailOperation *mail_op)
220 /* TODO: place here the cancel code */
224 modest_mail_operation_queue_cancel (ModestMailOperationQueue *op_queue,
225 ModestMailOperation *mail_op)
227 ModestMailOperationQueuePrivate *priv;
230 if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
231 !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
232 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
236 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
238 g_mutex_lock (priv->queue_lock);
239 modest_mail_operation_queue_cancel_no_block (op_queue, mail_op);
240 g_mutex_unlock (priv->queue_lock);
244 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *op_queue)
246 ModestMailOperationQueuePrivate *priv;
248 if (!MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
249 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
253 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
255 g_mutex_lock (priv->queue_lock);
256 g_queue_foreach (priv->op_queue,
257 (GFunc) modest_mail_operation_queue_cancel_no_block_wrapper,
259 g_mutex_unlock (priv->queue_lock);