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