2007-06-12 Murray Cumming <murrayc@murrayc.com>
[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 /* list my signals  */
44 enum {
45         /* MY_SIGNAL_1, */
46         /* MY_SIGNAL_2, */
47         LAST_SIGNAL
48 };
49
50 #if 0
51 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
52 struct _ModestTnySendQueuePrivate {
53         /* empty */
54 };
55 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
56                                                    MODEST_TYPE_TNY_SEND_QUEUE, \
57                                                    ModestTnySendQueuePrivate))
58 #endif
59
60 /* globals */
61 static TnyCamelSendQueueClass *parent_class = NULL;
62
63 /* uncomment the following if you have defined any signals */
64 /* static guint signals[LAST_SIGNAL] = {0}; */
65
66 /*
67  * this thread actually tries to send all the mails in the outbox
68  */
69
70
71 static void
72 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
73 {       
74         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
75 }
76
77 static void
78 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
79 {
80         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
81         g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
82         
83         /* FIXME: do something smart here... */
84         
85         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
86 }
87
88 static TnyFolder*
89 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
90 {
91         TnyFolder *folder;
92         TnyCamelTransportAccount *account;
93
94         g_return_val_if_fail (self, NULL);
95
96         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
97         if (!account) {
98                 g_printerr ("modest: no account for send queue\n");
99                 return NULL;
100         }
101         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
102                                                          TNY_FOLDER_TYPE_SENT);
103         g_object_unref (G_OBJECT(account));
104
105         return folder;
106 }
107
108
109 static TnyFolder*
110 modest_tny_send_queue_get_outbox (TnySendQueue *self)
111 {
112         TnyFolder *folder;
113         TnyCamelTransportAccount *account;
114
115         g_return_val_if_fail (self, NULL);
116
117         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
118         if (!account) {
119                 g_printerr ("modest: no account for send queue\n");
120                 return NULL;
121         }
122         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
123                                                          TNY_FOLDER_TYPE_OUTBOX);
124         g_object_unref (G_OBJECT(account));
125
126         return folder;
127 }
128
129
130 GType
131 modest_tny_send_queue_get_type (void)
132 {
133         static GType my_type = 0;
134
135         if (my_type == 0) {
136                 static const GTypeInfo my_info = {
137                         sizeof(ModestTnySendQueueClass),
138                         NULL,           /* base init */
139                         NULL,           /* base finalize */
140                         (GClassInitFunc) modest_tny_send_queue_class_init,
141                         NULL,           /* class finalize */
142                         NULL,           /* class data */
143                         sizeof(ModestTnySendQueue),
144                         0,              /* n_preallocs */
145                         (GInstanceInitFunc) modest_tny_send_queue_instance_init,
146                         NULL
147                 };
148                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
149                                                   "ModestTnySendQueue",
150                                                   &my_info, 0);
151         }
152         return my_type;
153 }
154
155
156 static void
157 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
158 {
159         GObjectClass *gobject_class;
160
161         gobject_class = (GObjectClass*) klass;
162         
163         parent_class            = g_type_class_peek_parent (klass);
164         gobject_class->finalize = modest_tny_send_queue_finalize;
165
166         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
167         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
168         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
169         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
170
171         /* g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate)); */
172 }
173
174 static void
175 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
176 {
177    
178 }
179
180 static void
181 modest_tny_send_queue_finalize (GObject *obj)
182 {
183         G_OBJECT_CLASS(parent_class)->finalize (obj);
184 }
185
186 ModestTnySendQueue*
187 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
188 {
189         ModestTnySendQueue *self;
190         
191         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
192         
193         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
194         
195         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
196                                                     account); 
197         return self;
198 }
199
200
201
202 void
203 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
204 {
205         /* TODO: Rename this to tny_camel_send_queue_try_to_send() in tinymail 
206         and check that it works, without creating a second worker. */
207 /*      tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self)); */
208 }