2007-06-03 Armin Burgmeier <armin@openismus.com>
[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-marshal.h"
32 #include "modest-mail-operation-queue.h"
33 #include "modest-runtime.h"
34
35 /* 'private'/'protected' functions */
36 static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass);
37 static void modest_mail_operation_queue_init       (ModestMailOperationQueue *obj);
38 static void modest_mail_operation_queue_finalize   (GObject *obj);
39
40 static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
41                                                                  ModestMailOperationQueue *op_queue);
42
43 static void modest_mail_operation_queue_cancel_no_block         (ModestMailOperationQueue *op_queue,
44                                                                  ModestMailOperation *mail_op);
45
46 static void
47 modest_mail_operation_queue_cancel_all_no_block (ModestMailOperationQueue *self);
48
49 /* list my signals  */
50 enum {
51         QUEUE_CHANGED_SIGNAL,
52         NUM_SIGNALS
53 };
54
55 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
56 struct _ModestMailOperationQueuePrivate {
57         GQueue *op_queue;
58         GMutex *queue_lock;
59         guint   op_id;
60 };
61 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62                                                          MODEST_TYPE_MAIL_OPERATION_QUEUE, \
63                                                          ModestMailOperationQueuePrivate))
64 /* globals */
65 static GObjectClass *parent_class = NULL;
66
67 static guint signals[NUM_SIGNALS] = {0};
68
69 GType
70 modest_mail_operation_queue_get_type (void)
71 {
72         static GType my_type = 0;
73         if (!my_type) {
74                 static const GTypeInfo my_info = {
75                         sizeof(ModestMailOperationQueueClass),
76                         NULL,           /* base init */
77                         NULL,           /* base finalize */
78                         (GClassInitFunc) modest_mail_operation_queue_class_init,
79                         NULL,           /* class finalize */
80                         NULL,           /* class data */
81                         sizeof(ModestMailOperationQueue),
82                         1,              /* n_preallocs */
83                         (GInstanceInitFunc) modest_mail_operation_queue_init,
84                         NULL
85                 };
86
87                 my_type = g_type_register_static (G_TYPE_OBJECT,
88                                                   "ModestMailOperationQueue",
89                                                   &my_info, 0);
90         }
91         return my_type;
92 }
93
94 static void
95 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
96 {
97         GObjectClass *gobject_class;
98
99         gobject_class = (GObjectClass*) klass;
100         parent_class  = g_type_class_peek_parent (klass);
101
102         gobject_class->finalize    = modest_mail_operation_queue_finalize;
103
104         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
105
106         /**
107          * ModestMailOperationQueue::queue-changed
108          * @self: the #ModestMailOperationQueue that emits the signal
109          * @mail_op: the #ModestMailOperation affected
110          * @type: the type of change in the queue
111          * @user_data: user data set when the signal handler was connected
112          *
113          * Emitted whenever the contents of the queue change
114          */
115         signals[QUEUE_CHANGED_SIGNAL] =
116                 g_signal_new ("queue-changed",
117                               G_TYPE_FROM_CLASS (gobject_class),
118                               G_SIGNAL_RUN_FIRST,
119                               G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
120                               NULL, NULL,
121                               modest_marshal_VOID__POINTER_INT,
122                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
123 }
124
125 static void
126 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
127 {
128         ModestMailOperationQueuePrivate *priv;
129
130         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
131
132         priv->op_queue   = g_queue_new ();
133         priv->queue_lock = g_mutex_new ();
134         priv->op_id = 0;
135 }
136
137 static void
138 modest_mail_operation_queue_finalize (GObject *obj)
139 {
140         ModestMailOperationQueuePrivate *priv;
141
142         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
143
144         g_mutex_lock (priv->queue_lock);
145
146         if (priv->op_queue) {
147                 /* Cancel all */
148                 if (!g_queue_is_empty (priv->op_queue))
149                         modest_mail_operation_queue_cancel_all_no_block (MODEST_MAIL_OPERATION_QUEUE (obj));
150                 g_queue_free (priv->op_queue);
151         }
152
153         g_mutex_unlock (priv->queue_lock);
154         g_mutex_free (priv->queue_lock);
155         
156         G_OBJECT_CLASS(parent_class)->finalize (obj);
157 }
158
159 ModestMailOperationQueue *
160 modest_mail_operation_queue_new (void)
161 {
162         ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
163
164         return MODEST_MAIL_OPERATION_QUEUE (self);
165 }
166
167 void 
168 modest_mail_operation_queue_add (ModestMailOperationQueue *self, 
169                                  ModestMailOperation *mail_op)
170 {
171         ModestMailOperationQueuePrivate *priv;
172
173         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
174         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
175         
176         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
177
178         g_mutex_lock (priv->queue_lock);
179         g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
180         modest_mail_operation_set_id (mail_op, priv->op_id++);
181         g_mutex_unlock (priv->queue_lock);
182
183         /* Notify observers */
184         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
185                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
186 }
187
188 void 
189 modest_mail_operation_queue_remove (ModestMailOperationQueue *self, 
190                                     ModestMailOperation *mail_op)
191 {
192         ModestMailOperationQueuePrivate *priv;
193         ModestMailOperationStatus status;
194
195         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
196         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
197
198         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
199
200         g_mutex_lock (priv->queue_lock);
201         g_queue_remove (priv->op_queue, mail_op);
202         g_mutex_unlock (priv->queue_lock);
203
204         /* Notify observers */
205         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
206                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
207
208         /* Check errors */
209         status = modest_mail_operation_get_status (mail_op);
210         if (status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
211                 /* This is a sanity check. Shouldn't be needed, but
212                    prevent possible application crashes. It's useful
213                    also for detecting mail operations with invalid
214                    status and error handling */
215                 if (modest_mail_operation_get_error (mail_op) != NULL)
216                         modest_mail_operation_execute_error_handler (mail_op);
217                 else {
218                         if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED) 
219                                 g_warning ("%s: operation canceled \n", __FUNCTION__);
220                         else
221                                 g_warning ("%s: possible error in a mail operation " \
222                                            "implementation. The status is not successful " \
223                                            "but the mail operation does not have any " \
224                                            "error set\n", __FUNCTION__);
225                 }
226         }
227
228         /* Free object */
229         modest_runtime_verify_object_last_ref (mail_op, "");
230         g_object_unref (G_OBJECT (mail_op));
231 }
232
233 guint 
234 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
235 {
236         ModestMailOperationQueuePrivate *priv;
237         guint length = 0;
238
239         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
240         
241         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
242
243         g_mutex_lock (priv->queue_lock);
244         length = g_queue_get_length (priv->op_queue);
245         g_mutex_unlock (priv->queue_lock);
246
247         return length;
248 }
249
250
251 /* Utility function intended to be used with g_queue_foreach */
252 static void
253 modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *self,
254                                                      ModestMailOperationQueue *op_queue)
255 {
256         modest_mail_operation_queue_cancel_no_block (op_queue, self);
257 }
258
259 static void 
260 modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *self,
261                                              ModestMailOperation *mail_op)
262 {
263         if (modest_mail_operation_is_finished (mail_op))
264                 return;
265
266         /* TODO: the implementation is still empty */
267         modest_mail_operation_cancel (mail_op);
268
269         /* Remove from the queue */
270         modest_mail_operation_queue_remove (self, mail_op);
271 }
272
273 static void
274 modest_mail_operation_queue_cancel_all_no_block (ModestMailOperationQueue *self)
275 {
276         ModestMailOperationQueuePrivate *priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (self);
277
278         g_queue_foreach (priv->op_queue, 
279                          (GFunc) modest_mail_operation_queue_cancel_no_block_wrapper, 
280                          self);
281 }
282
283 void 
284 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self, 
285                                     ModestMailOperation *mail_op)
286 {
287         ModestMailOperationQueuePrivate *priv;
288
289         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
290         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
291
292         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
293
294         g_mutex_lock (priv->queue_lock);
295         modest_mail_operation_queue_cancel_no_block (self, mail_op);
296         g_mutex_unlock (priv->queue_lock);
297 }
298
299 void 
300 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
301 {
302         ModestMailOperationQueuePrivate *priv;
303
304         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
305
306         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
307
308         g_mutex_lock (priv->queue_lock);
309         modest_mail_operation_queue_cancel_all_no_block (self);
310         g_mutex_unlock (priv->queue_lock);
311 }