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