1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #include "modest-marshal.h"
32 #include "modest-mail-operation-queue.h"
33 #include "modest-runtime.h"
34 #include "modest-debug.h"
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);
42 on_operation_finished (ModestMailOperation *mail_op,
52 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
53 struct _ModestMailOperationQueuePrivate {
58 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59 MODEST_TYPE_MAIL_OPERATION_QUEUE, \
60 ModestMailOperationQueuePrivate))
62 static GObjectClass *parent_class = NULL;
64 static guint signals[NUM_SIGNALS] = {0};
67 modest_mail_operation_queue_get_type (void)
69 static GType my_type = 0;
71 static const GTypeInfo my_info = {
72 sizeof(ModestMailOperationQueueClass),
74 NULL, /* base finalize */
75 (GClassInitFunc) modest_mail_operation_queue_class_init,
76 NULL, /* class finalize */
77 NULL, /* class data */
78 sizeof(ModestMailOperationQueue),
80 (GInstanceInitFunc) modest_mail_operation_queue_init,
84 my_type = g_type_register_static (G_TYPE_OBJECT,
85 "ModestMailOperationQueue",
92 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
94 GObjectClass *gobject_class;
96 gobject_class = (GObjectClass*) klass;
97 parent_class = g_type_class_peek_parent (klass);
99 gobject_class->finalize = modest_mail_operation_queue_finalize;
101 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
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
110 * Emitted whenever the contents of the queue change
112 signals[QUEUE_CHANGED_SIGNAL] =
113 g_signal_new ("queue-changed",
114 G_TYPE_FROM_CLASS (gobject_class),
116 G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
118 modest_marshal_VOID__POINTER_INT,
119 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
122 * ModestMailOperationQueue::queue-empty
123 * @self: the #ModestMailOperationQueue that emits the signal
124 * @user_data: user data set when the signal handler was connected
126 * Issued whenever the queue is empty
128 signals[QUEUE_EMPTY_SIGNAL] =
129 g_signal_new ("queue-empty",
130 G_TYPE_FROM_CLASS (gobject_class),
132 G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_empty),
134 g_cclosure_marshal_VOID__VOID,
140 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
142 ModestMailOperationQueuePrivate *priv;
144 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
146 priv->op_queue = g_queue_new ();
147 priv->queue_lock = g_mutex_new ();
152 print_queue_item (ModestMailOperation *op, const gchar* prefix)
154 gchar *op_str = modest_mail_operation_to_string (op);
156 prefix ? prefix : "",
162 on_finalize_foreach(gpointer op,
165 ModestMailOperationQueue *queue;
166 ModestMailOperationQueuePrivate *priv;
167 ModestMailOperation *mail_op;
169 queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
170 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (queue);
171 mail_op = MODEST_MAIL_OPERATION (op);
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);
178 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel/remove"););
180 modest_mail_operation_cancel (mail_op);
181 g_queue_remove (priv->op_queue, mail_op);
182 g_object_unref (G_OBJECT (mail_op));
186 modest_mail_operation_queue_finalize (GObject *obj)
188 ModestMailOperationQueuePrivate *priv;
190 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
192 g_mutex_lock (priv->queue_lock);
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");
201 if (priv->op_queue) {
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));
209 g_queue_free (priv->op_queue);
212 g_mutex_unlock (priv->queue_lock);
213 g_mutex_free (priv->queue_lock);
215 G_OBJECT_CLASS(parent_class)->finalize (obj);
218 ModestMailOperationQueue *
219 modest_mail_operation_queue_new (void)
221 ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
223 return MODEST_MAIL_OPERATION_QUEUE (self);
227 on_operation_finished (ModestMailOperation *mail_op,
230 ModestMailOperationQueue *queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
232 /* Remove operation from queue when finished */
233 modest_mail_operation_queue_remove (queue, mail_op);
237 modest_mail_operation_queue_add (ModestMailOperationQueue *self,
238 ModestMailOperation *mail_op)
240 ModestMailOperationQueuePrivate *priv;
242 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
243 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
245 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
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);
251 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "add"););
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
257 g_signal_connect_after (G_OBJECT (mail_op),
258 "operation-finished",
259 G_CALLBACK (on_operation_finished),
262 /* Notify observers */
263 g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
264 mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
268 modest_mail_operation_queue_remove (ModestMailOperationQueue *self,
269 ModestMailOperation *mail_op)
271 ModestMailOperationQueuePrivate *priv;
272 ModestMailOperationStatus status;
274 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
275 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
277 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
279 g_mutex_lock (priv->queue_lock);
280 g_queue_remove (priv->op_queue, mail_op);
281 g_mutex_unlock (priv->queue_lock);
283 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "remove"););
285 g_signal_handlers_disconnect_by_func (G_OBJECT (mail_op),
286 G_CALLBACK (on_operation_finished),
289 /* Notify observers */
290 g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
291 mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
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);
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__);
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));
321 /* Emit the queue empty-signal */
322 g_signal_emit (self, signals[QUEUE_EMPTY_SIGNAL], 0);
326 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
328 ModestMailOperationQueuePrivate *priv;
331 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
333 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
335 g_mutex_lock (priv->queue_lock);
336 length = g_queue_get_length (priv->op_queue);
337 g_mutex_unlock (priv->queue_lock);
343 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self,
344 ModestMailOperation *mail_op)
346 ModestMailOperationQueuePrivate *priv;
348 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
349 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
351 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
353 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel"););
355 /* This triggers a progess_changed signal in which we remove
356 * the operation from the queue. */
357 modest_mail_operation_cancel (mail_op);
361 on_cancel_all_foreach (gpointer op, gpointer list)
365 new_list = (GSList**) list;
366 *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
370 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
372 ModestMailOperationQueuePrivate *priv;
373 GSList* operations_to_cancel = NULL;
376 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
378 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
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);
389 /* TODO: Reverse the list, to remove operations in order? */
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");
395 modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
398 g_slist_free(operations_to_cancel);
408 on_find_by_source_foreach (gpointer op, gpointer data)
410 FindBySourceInfo *info = (FindBySourceInfo*) data;
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));
419 modest_mail_operation_queue_get_by_source (
420 ModestMailOperationQueue *self,
423 ModestMailOperationQueuePrivate *priv;
424 GSList* found_operations= NULL;
425 FindBySourceInfo *info = g_new0 (FindBySourceInfo, 1);
427 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
428 g_return_val_if_fail (source != NULL, NULL);
430 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
432 info->new_list = &found_operations;
433 info->source = source;
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);
441 return found_operations;
445 accumulate_mail_op_strings (ModestMailOperation *op, gchar **str)
447 *str = g_strdup_printf ("%s\n%s", *str, modest_mail_operation_to_string (op));
452 modest_mail_operation_queue_to_string (ModestMailOperationQueue *self)
456 ModestMailOperationQueuePrivate *priv;
458 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
460 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
462 len = g_queue_get_length (priv->op_queue);
463 str = g_strdup_printf ("mail operation queue (%02d)\n-------------------------", len);
465 str = g_strdup_printf ("%s\n%s", str, "<empty>");
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);