* src/modest-mail-operation.c:
[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 #include "modest-debug.h"
35
36 /* 'private'/'protected' functions */
37 static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass);
38 static void modest_mail_operation_queue_init       (ModestMailOperationQueue *obj);
39 static void modest_mail_operation_queue_finalize   (GObject *obj);
40
41 static void
42 on_operation_finished (ModestMailOperation *mail_op,
43                        gpointer user_data);
44
45 /* list my signals  */
46 enum {
47         QUEUE_CHANGED_SIGNAL,
48         QUEUE_EMPTY_SIGNAL,
49         NUM_SIGNALS
50 };
51
52 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
53 struct _ModestMailOperationQueuePrivate {
54         GQueue *op_queue;
55         GMutex *queue_lock;
56         guint   op_id;
57 };
58 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59                                                          MODEST_TYPE_MAIL_OPERATION_QUEUE, \
60                                                          ModestMailOperationQueuePrivate))
61 /* globals */
62 static GObjectClass *parent_class = NULL;
63
64 static guint signals[NUM_SIGNALS] = {0};
65
66 GType
67 modest_mail_operation_queue_get_type (void)
68 {
69         static GType my_type = 0;
70         if (!my_type) {
71                 static const GTypeInfo my_info = {
72                         sizeof(ModestMailOperationQueueClass),
73                         NULL,           /* base init */
74                         NULL,           /* base finalize */
75                         (GClassInitFunc) modest_mail_operation_queue_class_init,
76                         NULL,           /* class finalize */
77                         NULL,           /* class data */
78                         sizeof(ModestMailOperationQueue),
79                         1,              /* n_preallocs */
80                         (GInstanceInitFunc) modest_mail_operation_queue_init,
81                         NULL
82                 };
83
84                 my_type = g_type_register_static (G_TYPE_OBJECT,
85                                                   "ModestMailOperationQueue",
86                                                   &my_info, 0);
87         }
88         return my_type;
89 }
90
91 static void
92 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
93 {
94         GObjectClass *gobject_class;
95
96         gobject_class = (GObjectClass*) klass;
97         parent_class  = g_type_class_peek_parent (klass);
98
99         gobject_class->finalize    = modest_mail_operation_queue_finalize;
100
101         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
102
103         /**
104          * ModestMailOperationQueue::queue-changed
105          * @self: the #ModestMailOperationQueue that emits the signal
106          * @mail_op: the #ModestMailOperation affected
107          * @type: the type of change in the queue
108          * @user_data: user data set when the signal handler was connected
109          *
110          * Emitted whenever the contents of the queue change
111          */
112         signals[QUEUE_CHANGED_SIGNAL] =
113                 g_signal_new ("queue-changed",
114                               G_TYPE_FROM_CLASS (gobject_class),
115                               G_SIGNAL_RUN_FIRST,
116                               G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
117                               NULL, NULL,
118                               modest_marshal_VOID__POINTER_INT,
119                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
120
121         /**
122          * ModestMailOperationQueue::queue-empty
123          * @self: the #ModestMailOperationQueue that emits the signal
124          * @user_data: user data set when the signal handler was connected
125          *
126          * Issued whenever the queue is empty
127          */
128         signals[QUEUE_EMPTY_SIGNAL] =
129                 g_signal_new ("queue-empty",
130                               G_TYPE_FROM_CLASS (gobject_class),
131                               G_SIGNAL_RUN_FIRST,
132                               G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_empty),
133                               NULL, NULL,
134                               g_cclosure_marshal_VOID__VOID,
135                               G_TYPE_NONE, 0);
136
137 }
138
139 static void
140 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
141 {
142         ModestMailOperationQueuePrivate *priv;
143
144         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
145
146         priv->op_queue   = g_queue_new ();
147         priv->queue_lock = g_mutex_new ();
148         priv->op_id = 0;
149 }
150
151 static void
152 print_queue_item (ModestMailOperation *op, const gchar* prefix)
153 {
154         gchar *op_str = modest_mail_operation_to_string (op);
155         g_debug ("%s: %s",
156                  prefix ? prefix : "",
157                  op_str);
158         g_free (op_str);
159 }
160
161 static void
162 on_finalize_foreach(gpointer op,
163                     gpointer user_data)
164 {
165         ModestMailOperationQueue *queue;
166         ModestMailOperationQueuePrivate *priv;
167         ModestMailOperation *mail_op;
168
169         queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
170         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (queue);
171         mail_op = MODEST_MAIL_OPERATION (op);
172
173         /* Simply remove from queue, but without emitting a
174          * QUEUE_CHANGED_SIGNAL because we are in finalize anyway and have
175          * the lock acquired. */
176         g_signal_handlers_disconnect_by_func (mail_op, G_CALLBACK (on_operation_finished), user_data);
177
178         MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel/remove"););
179         
180         modest_mail_operation_cancel (mail_op);
181         g_queue_remove (priv->op_queue, mail_op);
182         g_object_unref (G_OBJECT (mail_op));
183 }
184
185 static void
186 modest_mail_operation_queue_finalize (GObject *obj)
187 {
188         ModestMailOperationQueuePrivate *priv;
189
190         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
191
192         g_mutex_lock (priv->queue_lock);
193
194         MODEST_DEBUG_BLOCK (
195                 g_debug ("%s; items in queue: %d",
196                          __FUNCTION__, g_queue_get_length (priv->op_queue));
197                 g_queue_foreach (priv->op_queue, (GFunc)print_queue_item, "in queue");
198         );
199
200         
201         if (priv->op_queue) {
202                 /* Cancel all */
203                 if (!g_queue_is_empty (priv->op_queue)) {
204                         g_queue_foreach (priv->op_queue,
205                                          (GFunc)on_finalize_foreach,
206                                          MODEST_MAIL_OPERATION_QUEUE (obj));
207                 }
208
209                 g_queue_free (priv->op_queue);
210         }
211
212         g_mutex_unlock (priv->queue_lock);
213         g_mutex_free (priv->queue_lock);
214         
215         G_OBJECT_CLASS(parent_class)->finalize (obj);
216 }
217
218 ModestMailOperationQueue *
219 modest_mail_operation_queue_new (void)
220 {
221         ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
222
223         return MODEST_MAIL_OPERATION_QUEUE (self);
224 }
225
226 static void
227 on_operation_finished (ModestMailOperation *mail_op,
228                        gpointer user_data)
229 {
230         ModestMailOperationQueue *queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
231
232         /* Remove operation from queue when finished */
233         modest_mail_operation_queue_remove (queue, mail_op);
234 }
235
236 void 
237 modest_mail_operation_queue_add (ModestMailOperationQueue *self, 
238                                  ModestMailOperation *mail_op)
239 {
240         ModestMailOperationQueuePrivate *priv;
241
242         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
243         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
244         
245         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
246
247         g_mutex_lock (priv->queue_lock);
248         g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
249         g_mutex_unlock (priv->queue_lock);
250         
251         MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "add"););
252
253         /* Get notified when the operation ends to remove it from the
254            queue. We connect it using the *after* because we want to
255            let the other handlers for the finish function happen
256            before this */
257         g_signal_connect_after (G_OBJECT (mail_op), 
258                                 "operation-finished",
259                                 G_CALLBACK (on_operation_finished), 
260                                 self);
261
262         /* Notify observers */
263         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
264                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
265 }
266
267 void 
268 modest_mail_operation_queue_remove (ModestMailOperationQueue *self,
269                                     ModestMailOperation *mail_op)
270 {
271         ModestMailOperationQueuePrivate *priv;
272         ModestMailOperationStatus status;
273
274         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
275         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
276
277         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
278
279         g_mutex_lock (priv->queue_lock);
280         g_queue_remove (priv->op_queue, mail_op);
281         g_mutex_unlock (priv->queue_lock);
282
283         MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "remove"););
284
285         g_signal_handlers_disconnect_by_func (G_OBJECT (mail_op),
286                                               G_CALLBACK (on_operation_finished),
287                                               self);
288
289         /* Notify observers */
290         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
291                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
292
293         /* Check errors */
294         status = modest_mail_operation_get_status (mail_op);
295         if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED) {
296                 g_warning ("%s: operation canceled \n", __FUNCTION__);
297         } else if (status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
298                 /* This is a sanity check. Shouldn't be needed, but
299                    prevent possible application crashes. It's useful
300                    also for detecting mail operations with invalid
301                    status and error handling */
302                 if (modest_mail_operation_get_error (mail_op) != NULL) {
303                         modest_mail_operation_execute_error_handler (mail_op);
304                 } else {
305                         g_warning ("%s: possible error in a mail operation " \
306                                    "implementation. The status is not successful " \
307                                    "but the mail operation does not have any " \
308                                    "error set\n", __FUNCTION__);
309                 }
310         }
311
312         /* Free object */
313
314         /* We do not own the last reference when this operation is deleted
315          * as response to a progress changed signal from the mail operation
316          * itself, in which case the glib signal system owns a reference
317          * until the signal emission is complete. armin. */
318         /* modest_runtime_verify_object_last_ref (mail_op, ""); */
319         g_object_unref (G_OBJECT (mail_op));
320
321         /* Emit the queue empty-signal */
322         g_signal_emit (self, signals[QUEUE_EMPTY_SIGNAL], 0);
323 }
324
325 guint 
326 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
327 {
328         ModestMailOperationQueuePrivate *priv;
329         guint length = 0;
330
331         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
332         
333         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
334
335         g_mutex_lock (priv->queue_lock);
336         length = g_queue_get_length (priv->op_queue);
337         g_mutex_unlock (priv->queue_lock);
338
339         return length;
340 }
341
342 void 
343 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self, 
344                                     ModestMailOperation *mail_op)
345 {
346         ModestMailOperationQueuePrivate *priv;
347
348         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
349         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
350
351         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
352
353         MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel"););
354         
355         /* This triggers a progess_changed signal in which we remove
356          * the operation from the queue. */
357         modest_mail_operation_cancel (mail_op);
358 }
359
360 static void
361 on_cancel_all_foreach (gpointer op, gpointer list)
362 {
363         GSList **new_list;
364
365         new_list = (GSList**) list;
366         *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
367 }
368
369 void 
370 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
371 {
372         ModestMailOperationQueuePrivate *priv;
373         GSList* operations_to_cancel = NULL;
374         GSList* cur = NULL;
375
376         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
377
378         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
379
380         /* Remember which operations to cancel. This is the only thing that
381          * is done while holding the lock, so we do not need to call
382          * functions from other files while holding the lock, which could
383          * lead to a deadlock if such a call re-enters into this queue and
384          * tries to acquire another lock. */
385         g_mutex_lock (priv->queue_lock);
386         g_queue_foreach (priv->op_queue, (GFunc) on_cancel_all_foreach, &operations_to_cancel);
387         g_mutex_unlock (priv->queue_lock);
388         
389         /* TODO: Reverse the list, to remove operations in order? */
390
391         for(cur = operations_to_cancel; cur != NULL; cur = cur->next) {
392                 if (!MODEST_IS_MAIL_OPERATION(cur->data))
393                         g_printerr ("modest: cur->data is not a valid mail operation\n");
394                 else
395                         modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
396         }
397
398         g_slist_free(operations_to_cancel);
399 }
400
401 typedef struct
402 {
403         GSList **new_list;
404         GObject *source;
405 } FindBySourceInfo;
406
407 static void
408 on_find_by_source_foreach (gpointer op, gpointer data)
409 {
410         FindBySourceInfo *info = (FindBySourceInfo*) data; 
411
412         if ( info->source == modest_mail_operation_get_source (MODEST_MAIL_OPERATION (op))) {
413                 g_object_ref (G_OBJECT (op));
414                 *(info->new_list) = g_slist_prepend (*(info->new_list), MODEST_MAIL_OPERATION (op));
415         }
416 }
417
418 GSList*
419 modest_mail_operation_queue_get_by_source (
420                 ModestMailOperationQueue *self, 
421                 GObject *source)
422 {
423         ModestMailOperationQueuePrivate *priv;
424         GSList* found_operations= NULL;
425         FindBySourceInfo *info = g_new0 (FindBySourceInfo, 1);
426
427         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
428         g_return_val_if_fail (source != NULL, NULL);
429         
430         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
431
432         info->new_list = &found_operations;
433         info->source = source;
434         
435         g_mutex_lock (priv->queue_lock);
436         g_queue_foreach (priv->op_queue, (GFunc) on_find_by_source_foreach, info);
437         g_mutex_unlock (priv->queue_lock);
438         
439         g_free (info);
440         
441         return found_operations;
442 }
443
444 static void
445 accumulate_mail_op_strings (ModestMailOperation *op, gchar **str)
446 {
447         *str = g_strdup_printf ("%s\n%s", *str, modest_mail_operation_to_string (op));
448 }
449
450
451 gchar*
452 modest_mail_operation_queue_to_string (ModestMailOperationQueue *self)
453 {
454         gchar *str;
455         guint len;
456         ModestMailOperationQueuePrivate *priv;
457         
458         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
459         
460         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
461
462         len = g_queue_get_length (priv->op_queue);
463         str = g_strdup_printf ("mail operation queue (%02d)\n-------------------------", len);
464         if (len == 0)
465                 str = g_strdup_printf ("%s\n%s", str, "<empty>");
466         else {
467                 g_mutex_lock (priv->queue_lock);
468                 g_queue_foreach (priv->op_queue, (GFunc)accumulate_mail_op_strings, &str);
469                 g_mutex_unlock (priv->queue_lock);
470         }
471                 
472         return str;
473 }