* Added some unit tests for ModestTextUtils
[modest] / src / modest-mail-operation-queue.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include "config.h"
31 #include "modest-mail-operation-queue.h"
32
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);
37
38 static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
39                                                                  ModestMailOperationQueue *op_queue);
40
41 static void modest_mail_operation_queue_cancel_no_block         (ModestMailOperationQueue *op_queue,
42                                                                  ModestMailOperation *mail_op);
43
44 /* list my signals  */
45 enum {
46         /* MY_SIGNAL_1, */
47         /* MY_SIGNAL_2, */
48         LAST_SIGNAL
49 };
50
51 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
52 struct _ModestMailOperationQueuePrivate {
53         GQueue *op_queue;
54         GMutex *queue_lock;
55 };
56 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
57                                                          MODEST_TYPE_MAIL_OPERATION_QUEUE, \
58                                                          ModestMailOperationQueuePrivate))
59 /* globals */
60 static GObjectClass *parent_class = NULL;
61
62 /* uncomment the following if you have defined any signals */
63 /* static guint signals[LAST_SIGNAL] = {0}; */
64
65 GType
66 modest_mail_operation_queue_get_type (void)
67 {
68         static GType my_type = 0;
69         if (!my_type) {
70                 static const GTypeInfo my_info = {
71                         sizeof(ModestMailOperationQueueClass),
72                         NULL,           /* base init */
73                         NULL,           /* base finalize */
74                         (GClassInitFunc) modest_mail_operation_queue_class_init,
75                         NULL,           /* class finalize */
76                         NULL,           /* class data */
77                         sizeof(ModestMailOperationQueue),
78                         1,              /* n_preallocs */
79                         (GInstanceInitFunc) modest_mail_operation_queue_init,
80                         NULL
81                 };
82
83                 my_type = g_type_register_static (G_TYPE_OBJECT,
84                                                   "ModestMailOperationQueue",
85                                                   &my_info, 0);
86         }
87         return my_type;
88 }
89
90 static void
91 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
92 {
93         GObjectClass *gobject_class;
94
95         gobject_class = (GObjectClass*) klass;
96         parent_class  = g_type_class_peek_parent (klass);
97
98         gobject_class->finalize    = modest_mail_operation_queue_finalize;
99
100         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
101 }
102
103 static void
104 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
105 {
106         ModestMailOperationQueuePrivate *priv;
107
108         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
109
110         priv->op_queue   = g_queue_new ();
111         priv->queue_lock = g_mutex_new ();
112 }
113
114 static void
115 modest_mail_operation_queue_finalize (GObject *obj)
116 {
117         ModestMailOperationQueuePrivate *priv;
118
119         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
120
121         g_mutex_lock (priv->queue_lock);
122
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);
127         }
128
129         g_mutex_unlock (priv->queue_lock);
130         g_mutex_free (priv->queue_lock);
131         
132         G_OBJECT_CLASS(parent_class)->finalize (obj);
133 }
134
135 ModestMailOperationQueue *
136 modest_mail_operation_queue_new (void)
137 {
138         ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
139
140         return MODEST_MAIL_OPERATION_QUEUE (self);
141 }
142
143 void 
144 modest_mail_operation_queue_add (ModestMailOperationQueue *op_queue, 
145                                  ModestMailOperation *mail_op)
146 {
147         ModestMailOperationQueuePrivate *priv;
148
149         if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
150             !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
151                 g_warning ("%s: bad parametters", G_GNUC_FUNCTION);
152                 return;
153         }
154
155         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
156
157         g_mutex_lock (priv->queue_lock);
158         g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
159         g_mutex_unlock (priv->queue_lock);
160 }
161
162 void 
163 modest_mail_operation_queue_remove (ModestMailOperationQueue *op_queue, 
164                                     ModestMailOperation *mail_op)
165 {
166         ModestMailOperationQueuePrivate *priv;
167
168         if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
169             !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
170                 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
171                 return;
172         }
173
174         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
175
176         g_mutex_lock (priv->queue_lock);
177         g_queue_remove (priv->op_queue, mail_op);
178         g_mutex_unlock (priv->queue_lock);
179 }
180
181
182 /* Utility function intended to be used with g_queue_foreach */
183 static void
184 modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
185                                                      ModestMailOperationQueue *op_queue)
186 {
187         modest_mail_operation_queue_cancel_no_block (op_queue, mail_op);
188 }
189
190 static void 
191 modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *op_queue,
192                                              ModestMailOperation *mail_op)
193 {
194         if (modest_mail_operation_is_finished (mail_op))
195                 return;
196
197         /* TODO: the implementation is still empty */
198         modest_mail_operation_cancel (mail_op);
199
200         /* Remove from the queue */
201         modest_mail_operation_queue_remove (op_queue, mail_op);
202 }
203
204 void 
205 modest_mail_operation_queue_cancel (ModestMailOperationQueue *op_queue, 
206                                     ModestMailOperation *mail_op)
207 {
208         ModestMailOperationQueuePrivate *priv;
209         GList *iter;
210
211         if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
212             !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
213                 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
214                 return;
215         }
216
217         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
218
219         g_mutex_lock (priv->queue_lock);
220         modest_mail_operation_queue_cancel_no_block (op_queue, mail_op);
221         g_mutex_unlock (priv->queue_lock);
222 }
223
224 void 
225 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *op_queue)
226 {
227         ModestMailOperationQueuePrivate *priv;
228
229         if (!MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
230                 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
231                 return;
232         }
233
234         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
235
236         g_mutex_lock (priv->queue_lock);
237         g_queue_foreach (priv->op_queue, 
238                          (GFunc) modest_mail_operation_queue_cancel_no_block_wrapper, 
239                          op_queue);
240         g_mutex_unlock (priv->queue_lock);
241 }