Comment 'msg-sending' signal handler, waiting for patch approval
[modest] / src / modest-tny-send-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
31 #include <modest-tny-send-queue.h>
32 #include <tny-simple-list.h>
33 #include <tny-iterator.h>
34 #include <tny-folder.h>
35 #include <tny-camel-msg.h>
36 #include <modest-tny-account.h>
37
38 /* 'private'/'protected' functions */
39 static void modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass);
40 static void modest_tny_send_queue_finalize   (GObject *obj);
41 static void modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class);
42
43 /* Signal handlers */ 
44 static void _on_msg_start_sending (TnySendQueue *self, TnyMsg *msg, guint processed, guint total);
45 static void _on_msg_has_been_sent (TnySendQueue *self, TnyMsg *msg, guint processed, guint total);
46
47
48 /* list my signals  */
49 enum {
50         /* MY_SIGNAL_1, */
51         /* MY_SIGNAL_2, */
52         LAST_SIGNAL
53 };
54
55 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
56 struct _ModestTnySendQueuePrivate {
57         gchar *current_msg_id;
58
59 };
60
61 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62                                                    MODEST_TYPE_TNY_SEND_QUEUE, \
63                                                    ModestTnySendQueuePrivate))
64
65 /* globals */
66 static TnyCamelSendQueueClass *parent_class = NULL;
67
68 /* uncomment the following if you have defined any signals */
69 /* static guint signals[LAST_SIGNAL] = {0}; */
70
71 /*
72  * this thread actually tries to send all the mails in the outbox
73  */
74
75
76 static void
77 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
78 {       
79         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
80 }
81
82 static void
83 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
84 {
85         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
86         g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
87         
88         /* FIXME: do something smart here... */
89         
90         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
91 }
92
93 static TnyFolder*
94 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
95 {
96         TnyFolder *folder;
97         TnyCamelTransportAccount *account;
98
99         g_return_val_if_fail (self, NULL);
100
101         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
102         if (!account) {
103                 g_printerr ("modest: no account for send queue\n");
104                 return NULL;
105         }
106         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
107                                                          TNY_FOLDER_TYPE_SENT);
108         g_object_unref (G_OBJECT(account));
109
110         return folder;
111 }
112
113
114 static TnyFolder*
115 modest_tny_send_queue_get_outbox (TnySendQueue *self)
116 {
117         TnyFolder *folder;
118         TnyCamelTransportAccount *account;
119
120         g_return_val_if_fail (self, NULL);
121
122         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
123         if (!account) {
124                 g_printerr ("modest: no account for send queue\n");
125                 return NULL;
126         }
127         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
128                                                          TNY_FOLDER_TYPE_OUTBOX);
129         g_object_unref (G_OBJECT(account));
130
131         return folder;
132 }
133
134
135 GType
136 modest_tny_send_queue_get_type (void)
137 {
138         static GType my_type = 0;
139
140         if (my_type == 0) {
141                 static const GTypeInfo my_info = {
142                         sizeof(ModestTnySendQueueClass),
143                         NULL,           /* base init */
144                         NULL,           /* base finalize */
145                         (GClassInitFunc) modest_tny_send_queue_class_init,
146                         NULL,           /* class finalize */
147                         NULL,           /* class data */
148                         sizeof(ModestTnySendQueue),
149                         0,              /* n_preallocs */
150                         (GInstanceInitFunc) modest_tny_send_queue_instance_init,
151                         NULL
152                 };
153                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
154                                                   "ModestTnySendQueue",
155                                                   &my_info, 0);
156         }
157         return my_type;
158 }
159
160
161 static void
162 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
163 {
164         GObjectClass *gobject_class;
165
166         gobject_class = (GObjectClass*) klass;
167         
168         parent_class            = g_type_class_peek_parent (klass);
169         gobject_class->finalize = modest_tny_send_queue_finalize;
170
171         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
172         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
173         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
174         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
175
176         g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
177 }
178
179 static void
180 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
181 {
182         ModestTnySendQueuePrivate *priv;
183                 
184         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
185         priv->current_msg_id = NULL;
186         
187 }
188
189 static void
190 modest_tny_send_queue_finalize (GObject *obj)
191 {
192         ModestTnySendQueuePrivate *priv;
193                 
194         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
195         if (priv->current_msg_id != NULL)
196                 g_free(priv->current_msg_id);
197
198         G_OBJECT_CLASS(parent_class)->finalize (obj);
199 }
200
201 ModestTnySendQueue*
202 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
203 {
204         ModestTnySendQueue *self;
205         
206         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
207         
208         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
209         
210         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
211                                                     account); 
212
213         /* Connect signals to control when a msg is being or has been sent */
214         /* TODO: this signal was implemented in tinymail camel send queue, but im
215            waiting for implement some unit tests nbefore commited changes */
216         if (FALSE) {
217                 g_signal_connect (G_OBJECT(self), "msg-sending",
218                                   G_CALLBACK(_on_msg_start_sending), 
219                                   NULL);
220         }
221                           
222         g_signal_connect (G_OBJECT(self), "msg-sent",
223                           G_CALLBACK(_on_msg_has_been_sent), 
224                           NULL);
225         return self;
226 }
227
228
229
230 void
231 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
232 {
233         /* TODO: Rename this to tny_camel_send_queue_try_to_send() in tinymail 
234         and check that it works, without creating a second worker. */
235 /*      tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self)); */
236 }
237
238 gboolean
239 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
240                                          const gchar *msg_id)
241 {       
242         ModestTnySendQueuePrivate *priv;
243         
244         g_return_val_if_fail (msg_id != NULL, FALSE); 
245         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
246         
247         if (modest_tny_send_queue_sending_in_progress(self))
248                 return g_ascii_strcasecmp(priv->current_msg_id, msg_id);
249         else 
250                 return FALSE;
251 }
252
253 gboolean
254 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
255 {       
256         ModestTnySendQueuePrivate *priv;
257         
258         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
259         
260         return priv->current_msg_id != NULL;
261 }
262
263
264 static void 
265 _on_msg_start_sending (TnySendQueue *self,
266                        TnyMsg *msg, 
267                        guint processed,
268                        guint total)
269 {
270         ModestTnySendQueuePrivate *priv;
271         TnyHeader *header = NULL;
272
273         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
274
275         /* Delete previous msg_id */
276         if (priv->current_msg_id != NULL)
277                 g_free(priv->current_msg_id);
278         
279         /* Set current msg_id */
280         header = tny_msg_get_header(msg);
281         priv->current_msg_id = g_strdup(tny_header_get_message_id (header));
282 }
283
284 static void 
285 _on_msg_has_been_sent (TnySendQueue *self,
286                        TnyMsg *msg, 
287                        guint processed,
288                        guint total)
289 {
290         ModestTnySendQueuePrivate *priv;
291         
292         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
293         
294         /* Delete previous msg_id */
295         if (priv->current_msg_id != NULL)
296                 g_free(priv->current_msg_id);
297         
298         /* Unset current msg_id */
299         priv->current_msg_id = NULL;
300 }