Fix for bug NB#74675. Now we limit the retrieval size for texts shown,
[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
41 on_operation_finished (ModestMailOperation *mail_op,
42                        gpointer user_data);
43
44 /* list my signals  */
45 enum {
46         QUEUE_CHANGED_SIGNAL,
47         NUM_SIGNALS
48 };
49
50 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
51 struct _ModestMailOperationQueuePrivate {
52         GQueue *op_queue;
53         GMutex *queue_lock;
54         guint   op_id;
55 };
56 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
57                                                          MODEST_TYPE_MAIL_OPERATION_QUEUE, \
58                                                          ModestMailOperationQueuePrivate))
59 /* globals */
60 static GObjectClass *parent_class = NULL;
61
62 static guint signals[NUM_SIGNALS] = {0};
63
64 GType
65 modest_mail_operation_queue_get_type (void)
66 {
67         static GType my_type = 0;
68         if (!my_type) {
69                 static const GTypeInfo my_info = {
70                         sizeof(ModestMailOperationQueueClass),
71                         NULL,           /* base init */
72                         NULL,           /* base finalize */
73                         (GClassInitFunc) modest_mail_operation_queue_class_init,
74                         NULL,           /* class finalize */
75                         NULL,           /* class data */
76                         sizeof(ModestMailOperationQueue),
77                         1,              /* n_preallocs */
78                         (GInstanceInitFunc) modest_mail_operation_queue_init,
79                         NULL
80                 };
81
82                 my_type = g_type_register_static (G_TYPE_OBJECT,
83                                                   "ModestMailOperationQueue",
84                                                   &my_info, 0);
85         }
86         return my_type;
87 }
88
89 static void
90 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
91 {
92         GObjectClass *gobject_class;
93
94         gobject_class = (GObjectClass*) klass;
95         parent_class  = g_type_class_peek_parent (klass);
96
97         gobject_class->finalize    = modest_mail_operation_queue_finalize;
98
99         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
100
101         /**
102          * ModestMailOperationQueue::queue-changed
103          * @self: the #ModestMailOperationQueue that emits the signal
104          * @mail_op: the #ModestMailOperation affected
105          * @type: the type of change in the queue
106          * @user_data: user data set when the signal handler was connected
107          *
108          * Emitted whenever the contents of the queue change
109          */
110         signals[QUEUE_CHANGED_SIGNAL] =
111                 g_signal_new ("queue-changed",
112                               G_TYPE_FROM_CLASS (gobject_class),
113                               G_SIGNAL_RUN_FIRST,
114                               G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
115                               NULL, NULL,
116                               modest_marshal_VOID__POINTER_INT,
117                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
118 }
119
120 static void
121 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
122 {
123         ModestMailOperationQueuePrivate *priv;
124
125         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
126
127         priv->op_queue   = g_queue_new ();
128         priv->queue_lock = g_mutex_new ();
129         priv->op_id = 0;
130 }
131
132 static void
133 on_finalize_foreach(gpointer op,
134                     gpointer user_data)
135 {
136         ModestMailOperationQueue *queue;
137         ModestMailOperationQueuePrivate *priv;
138         ModestMailOperation *mail_op;
139
140         queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
141         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (queue);
142         mail_op = MODEST_MAIL_OPERATION (op);
143
144         /* Simply remove from queue, but without emitting a
145          * QUEUE_CHANGED_SIGNAL because we are in finalize anyway and have
146          * the lock acquired. */
147         g_signal_handlers_disconnect_by_func (mail_op, G_CALLBACK (on_operation_finished), user_data);
148
149         modest_mail_operation_cancel (mail_op);
150         g_queue_remove (priv->op_queue, mail_op);
151         g_object_unref (G_OBJECT (mail_op));
152 }
153
154 static void
155 modest_mail_operation_queue_finalize (GObject *obj)
156 {
157         ModestMailOperationQueuePrivate *priv;
158
159         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
160
161         g_mutex_lock (priv->queue_lock);
162
163         if (priv->op_queue) {
164                 /* Cancel all */
165                 if (!g_queue_is_empty (priv->op_queue)) {
166                         g_queue_foreach (priv->op_queue,
167                                          (GFunc)on_finalize_foreach,
168                                          MODEST_MAIL_OPERATION_QUEUE (obj));
169                 }
170
171                 g_queue_free (priv->op_queue);
172         }
173
174         g_mutex_unlock (priv->queue_lock);
175         g_mutex_free (priv->queue_lock);
176         
177         G_OBJECT_CLASS(parent_class)->finalize (obj);
178 }
179
180 ModestMailOperationQueue *
181 modest_mail_operation_queue_new (void)
182 {
183         ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
184
185         return MODEST_MAIL_OPERATION_QUEUE (self);
186 }
187
188 static void
189 on_operation_finished (ModestMailOperation *mail_op,
190                        gpointer user_data)
191 {
192         ModestMailOperationQueue *queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
193
194         /* Remove operation from queue when finished */
195         modest_mail_operation_queue_remove (queue, mail_op);
196 }
197
198 void 
199 modest_mail_operation_queue_add (ModestMailOperationQueue *self, 
200                                  ModestMailOperation *mail_op)
201 {
202         ModestMailOperationQueuePrivate *priv;
203
204         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
205         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
206         
207         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
208
209         g_mutex_lock (priv->queue_lock);
210         g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
211         g_mutex_unlock (priv->queue_lock);
212
213         /* Get notified when the operation ends to remove it from the
214            queue. We connect it using the *after* because we want to
215            let the other handlers for the finish function happen
216            before this */
217         g_signal_connect_after (G_OBJECT (mail_op), 
218                                 "operation-finished",
219                                 G_CALLBACK (on_operation_finished), 
220                                 self);
221
222         /* Notify observers */
223         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
224                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
225 }
226
227 void 
228 modest_mail_operation_queue_remove (ModestMailOperationQueue *self,
229                                     ModestMailOperation *mail_op)
230 {
231         ModestMailOperationQueuePrivate *priv;
232         ModestMailOperationStatus status;
233
234         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
235         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
236
237         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
238
239         g_mutex_lock (priv->queue_lock);
240         g_queue_remove (priv->op_queue, mail_op);
241         g_mutex_unlock (priv->queue_lock);
242
243         g_signal_handlers_disconnect_by_func (G_OBJECT (mail_op),
244                                               G_CALLBACK (on_operation_finished),
245                                               self);
246
247         /* Notify observers */
248         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
249                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
250
251         /* Check errors */
252         status = modest_mail_operation_get_status (mail_op);
253         if (status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
254                 /* This is a sanity check. Shouldn't be needed, but
255                    prevent possible application crashes. It's useful
256                    also for detecting mail operations with invalid
257                    status and error handling */
258                 if (modest_mail_operation_get_error (mail_op) != NULL) {
259                         modest_mail_operation_execute_error_handler (mail_op);
260                 } else {
261                         if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED) 
262                                 g_warning ("%s: operation canceled \n", __FUNCTION__);
263                         else
264                                 g_warning ("%s: possible error in a mail operation " \
265                                            "implementation. The status is not successful " \
266                                            "but the mail operation does not have any " \
267                                            "error set\n", __FUNCTION__);
268                 }
269         }
270
271         /* Free object */
272
273         /* We do not own the last reference when this operation is deleted
274          * as response to a progress changed signal from the mail operation
275          * itself, in which case the glib signal system owns a reference
276          * until the signal emission is complete. armin. */
277         /* modest_runtime_verify_object_last_ref (mail_op, ""); */
278         g_object_unref (G_OBJECT (mail_op));
279 }
280
281 guint 
282 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
283 {
284         ModestMailOperationQueuePrivate *priv;
285         guint length = 0;
286
287         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
288         
289         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
290
291         g_mutex_lock (priv->queue_lock);
292         length = g_queue_get_length (priv->op_queue);
293         g_mutex_unlock (priv->queue_lock);
294
295         return length;
296 }
297
298 void 
299 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self, 
300                                     ModestMailOperation *mail_op)
301 {
302         ModestMailOperationQueuePrivate *priv;
303
304         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
305         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
306
307         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
308
309         /* This triggers a progess_changed signal in which we remove
310          * the operation from the queue. */
311         modest_mail_operation_cancel (mail_op);
312 }
313
314 static void
315 on_cancel_all_foreach (gpointer op, gpointer list)
316 {
317         GSList **new_list;
318
319         new_list = (GSList**) list;
320         *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
321 }
322
323 void 
324 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
325 {
326         ModestMailOperationQueuePrivate *priv;
327         GSList* operations_to_cancel = NULL;
328         GSList* cur = NULL;
329
330         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
331
332         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
333
334         /* Remember which operations to cancel. This is the only thing that
335          * is done while holding the lock, so we do not need to call
336          * functions from other files while holding the lock, which could
337          * lead to a deadlock if such a call re-enters into this queue and
338          * tries to acquire another lock. */
339         g_mutex_lock (priv->queue_lock);
340         g_queue_foreach (priv->op_queue, (GFunc) on_cancel_all_foreach, &operations_to_cancel);
341         g_mutex_unlock (priv->queue_lock);
342         
343         /* TODO: Reverse the list, to remove operations in order? */
344
345         for(cur = operations_to_cancel; cur != NULL; cur = cur->next) {
346                 if (!MODEST_IS_MAIL_OPERATION(cur->data))
347                         g_printerr ("modest: cur->data is not a valid mail operation\n");
348                 else
349                         modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
350         }
351
352         g_slist_free(operations_to_cancel);
353 }