* src/modest-mail-operation.[ch]:
[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             status != MODEST_MAIL_OPERATION_STATUS_INVALID) {
333                 /* This is a sanity check. Shouldn't be needed, but
334                    prevent possible application crashes. It's useful
335                    also for detecting mail operations with invalid
336                    status and error handling */
337                 if (modest_mail_operation_get_error (mail_op) != NULL) {
338                         modest_mail_operation_execute_error_handler (mail_op);
339                 } else {
340                         if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED) 
341                                 g_warning ("%s: operation canceled \n", __FUNCTION__);
342                         else
343                                 g_warning ("%s: possible error in a mail operation " \
344                                            "implementation. The status is not successful " \
345                                            "but the mail operation does not have any " \
346                                            "error set\n", __FUNCTION__);
347                 }
348         }
349
350         /* Free object */
351         g_object_unref (G_OBJECT (mail_op));
352
353         /* Emit the queue empty-signal. See the function to know why
354            we emit it in an idle */
355         if (num_elements == 0) {
356                 if (priv->queue_empty_handler) {
357                         g_source_remove (priv->queue_empty_handler);
358                         priv->queue_empty_handler = 0;
359                 }
360                 priv->queue_empty_handler = g_idle_add_full (G_PRIORITY_LOW, 
361                                                              notify_queue_empty, 
362                                                              self, NULL);
363         }
364 }
365
366 guint 
367 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
368 {
369         ModestMailOperationQueuePrivate *priv;
370         guint length = 0;
371
372         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
373         
374         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
375
376         g_mutex_lock (priv->queue_lock);
377         length = g_queue_get_length (priv->op_queue);
378         g_mutex_unlock (priv->queue_lock);
379
380         return length;
381 }
382
383 void 
384 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self, 
385                                     ModestMailOperation *mail_op)
386 {
387         ModestMailOperationQueuePrivate *priv;
388
389         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
390         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
391
392         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
393
394         MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel"););
395         
396         /* This triggers a progess_changed signal in which we remove
397          * the operation from the queue. */
398         modest_mail_operation_cancel (mail_op);
399 }
400
401 static void
402 on_cancel_all_foreach (gpointer op, gpointer list)
403 {
404         GSList **new_list;
405
406         new_list = (GSList**) list;
407         *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
408 }
409
410 void 
411 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
412 {
413         ModestMailOperationQueuePrivate *priv;
414         GSList* operations_to_cancel = NULL;
415         GSList* cur = NULL;
416
417         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
418
419         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
420
421         /* Remember which operations to cancel. This is the only thing that
422          * is done while holding the lock, so we do not need to call
423          * functions from other files while holding the lock, which could
424          * lead to a deadlock if such a call re-enters into this queue and
425          * tries to acquire another lock. */
426         g_mutex_lock (priv->queue_lock);
427         g_queue_foreach (priv->op_queue, (GFunc) on_cancel_all_foreach, &operations_to_cancel);
428         g_mutex_unlock (priv->queue_lock);
429         
430         /* TODO: Reverse the list, to remove operations in order? */
431
432         for(cur = operations_to_cancel; cur != NULL; cur = cur->next) {
433                 if (!MODEST_IS_MAIL_OPERATION(cur->data))
434                         g_printerr ("modest: cur->data is not a valid mail operation\n");
435                 else
436                         modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
437         }
438
439         g_slist_free(operations_to_cancel);
440 }
441
442 typedef struct
443 {
444         GSList **new_list;
445         GObject *source;
446 } FindBySourceInfo;
447
448 static void
449 on_find_by_source_foreach (gpointer op, gpointer data)
450 {
451         FindBySourceInfo *info = (FindBySourceInfo*) data; 
452
453         if ( info->source == modest_mail_operation_get_source (MODEST_MAIL_OPERATION (op))) {
454                 g_object_ref (G_OBJECT (op));
455                 *(info->new_list) = g_slist_prepend (*(info->new_list), MODEST_MAIL_OPERATION (op));
456         }
457 }
458
459 GSList*
460 modest_mail_operation_queue_get_by_source (
461                 ModestMailOperationQueue *self, 
462                 GObject *source)
463 {
464         ModestMailOperationQueuePrivate *priv;
465         GSList* found_operations= NULL;
466         FindBySourceInfo *info = g_new0 (FindBySourceInfo, 1);
467
468         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
469         g_return_val_if_fail (source != NULL, NULL);
470         
471         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
472
473         info->new_list = &found_operations;
474         info->source = source;
475         
476         g_mutex_lock (priv->queue_lock);
477         g_queue_foreach (priv->op_queue, (GFunc) on_find_by_source_foreach, info);
478         g_mutex_unlock (priv->queue_lock);
479         
480         g_free (info);
481         
482         return found_operations;
483 }
484
485 static void
486 accumulate_mail_op_strings (ModestMailOperation *op, gchar **str)
487 {
488         *str = g_strdup_printf ("%s\n%s", *str, modest_mail_operation_to_string (op));
489 }
490
491
492 gchar*
493 modest_mail_operation_queue_to_string (ModestMailOperationQueue *self)
494 {
495         gchar *str;
496         guint len;
497         ModestMailOperationQueuePrivate *priv;
498         
499         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
500         
501         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
502
503         len = g_queue_get_length (priv->op_queue);
504         str = g_strdup_printf ("mail operation queue (%02d)\n-------------------------", len);
505         if (len == 0)
506                 str = g_strdup_printf ("%s\n%s", str, "<empty>");
507         else {
508                 g_mutex_lock (priv->queue_lock);
509                 g_queue_foreach (priv->op_queue, (GFunc)accumulate_mail_op_strings, &str);
510                 g_mutex_unlock (priv->queue_lock);
511         }
512                 
513         return str;
514 }