Added connection reattempts to get_password function
[modest] / src / modest-transport-account-decorator.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-transport-account-decorator.h>
32 #include <modest-tny-account-store.h>
33 #include <modest-tny-account.h>
34 #include <modest-runtime.h>
35 #include <tny-simple-list.h>
36 #include <tny-iterator.h>
37 #include <tny-folder.h>
38 #include <modest-marshal.h>
39 #include <string.h> /* strcmp */
40
41 /* 'private'/'protected' functions */
42 static void modest_transport_account_decorator_class_init (ModestTransportAccountDecoratorClass *klass);
43 static void modest_transport_account_decorator_finalize   (GObject *obj);
44 static void modest_transport_account_decorator_instance_init (GTypeInstance *instance, gpointer g_class);
45 static void modest_transport_account_decorator_send (TnyTransportAccount *self, TnyMsg *msg, GError **err);
46 static void modest_transport_account_decorator_cancel (TnyAccount *self);
47
48 /* list my signals  */
49 /* enum { */
50 /*      /\* MY_SIGNAL_1, *\/ */
51 /*      /\* MY_SIGNAL_2, *\/ */
52 /*      LAST_SIGNAL */
53 /* }; */
54
55 /* typedef struct _ModestTransportAccountDecoratorPrivate ModestTransportAccountDecoratorPrivate; */
56 /* struct _ModestTransportAccountDecoratorPrivate { */
57 /* }; */
58
59 /* #define MODEST_TRANSPORT_ACCOUNT_DECORATOR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \ */
60 /*                                                    MODEST_TYPE_TRANSPORT_ACCOUNT_DECORATOR, \ */
61 /*                                                    ModestTransportAccountDecoratorPrivate)) */
62
63 /* globals */
64 static TnyCamelTransportAccountClass *parent_class = NULL;
65
66 /* uncomment the following if you have defined any signals */
67 /* static guint signals[LAST_SIGNAL] = {0}; */
68
69 /*
70  * this thread actually tries to send all the mails in the outbox and keeps
71  * track of their state.
72  */
73 static void
74 modest_transport_account_decorator_send (TnyTransportAccount *self, TnyMsg *msg, GError **err)
75 {
76         TnyTransportAccount *connection_specific_account = NULL;
77 /*      ModestTransportAccountDecoratorPrivate *priv = MODEST_TRANSPORT_ACCOUNT_DECORATOR_GET_PRIVATE (self); */
78         ModestTnyAccountStore *store = modest_runtime_get_account_store ();
79         const gchar *account_name;
80
81         account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (TNY_ACCOUNT (self));
82         if (account_name) {
83                 connection_specific_account = TNY_TRANSPORT_ACCOUNT 
84                         (modest_tny_account_store_get_smtp_specific_transport_account_for_open_connection (store, account_name));
85         }
86
87         if (connection_specific_account) {
88                 tny_transport_account_send (connection_specific_account, msg, err);
89                 g_object_unref (connection_specific_account);
90         } else {
91                 TNY_CAMEL_TRANSPORT_ACCOUNT_CLASS(parent_class)->send (self, msg, err);
92         }
93 }
94
95 static void 
96 modest_transport_account_decorator_cancel (TnyAccount *self) 
97 {
98         TnyTransportAccount *conn_specific_account = NULL;
99         ModestTnyAccountStore *store = modest_runtime_get_account_store ();
100         const gchar *account_name;
101
102         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (self));
103
104         account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (self);
105         if (account_name) {
106                 conn_specific_account = (TnyTransportAccount *)
107                         modest_tny_account_store_get_smtp_specific_transport_account_for_open_connection (store, account_name);
108         }
109
110         /* Cancel sending in the connection specific transport account */
111         if (conn_specific_account) {
112                 tny_account_cancel (TNY_ACCOUNT (conn_specific_account));
113                 g_object_unref (conn_specific_account);
114         }
115
116         /* Also cancel the transport account, we could not be sure
117            which one was used to send each email */
118         TNY_CAMEL_ACCOUNT_CLASS(parent_class)->cancel (self);
119 }
120
121 static void
122 modest_transport_account_decorator_class_init (ModestTransportAccountDecoratorClass *klass)
123 {
124         GObjectClass *gobject_class;
125         TnyCamelTransportAccountClass *transport_class;
126         TnyCamelAccountClass *account_class;
127
128         gobject_class = (GObjectClass*) klass;
129         transport_class = (TnyCamelTransportAccountClass *) klass;
130         account_class = (TnyCamelAccountClass *) klass;
131         
132         parent_class            = g_type_class_peek_parent (klass);
133         gobject_class->finalize = modest_transport_account_decorator_finalize;
134
135         transport_class->send = modest_transport_account_decorator_send;
136
137         account_class->cancel = modest_transport_account_decorator_cancel;
138 /*      g_type_class_add_private (gobject_class, sizeof(ModestTransportAccountDecoratorPrivate)); */
139 }
140
141 static void
142 modest_transport_account_decorator_instance_init (GTypeInstance *instance, gpointer g_class)
143 {
144 /*      ModestTransportAccountDecoratorPrivate *priv; */
145
146 /*      priv = MODEST_TRANSPORT_ACCOUNT_DECORATOR_GET_PRIVATE (instance); */
147 }
148
149 static void
150 modest_transport_account_decorator_finalize (GObject *obj)
151 {
152 /*      ModestTransportAccountDecoratorPrivate *priv; */
153                 
154 /*      priv = MODEST_TRANSPORT_ACCOUNT_DECORATOR_GET_PRIVATE (obj); */
155
156         G_OBJECT_CLASS(parent_class)->finalize (obj);
157 }
158
159 ModestTransportAccountDecorator*
160 modest_transport_account_decorator_new (void)
161 {
162         return g_object_new (MODEST_TYPE_TRANSPORT_ACCOUNT_DECORATOR, NULL);
163 }
164
165 GType
166 modest_transport_account_decorator_get_type (void)
167 {
168         static GType my_type = 0;
169
170         if (my_type == 0) {
171                 static const GTypeInfo my_info = {
172                         sizeof(ModestTransportAccountDecoratorClass),
173                         NULL,           /* base init */
174                         NULL,           /* base finalize */
175                         (GClassInitFunc) modest_transport_account_decorator_class_init,
176                         NULL,           /* class finalize */
177                         NULL,           /* class data */
178                         sizeof(ModestTransportAccountDecorator),
179                         0,              /* n_preallocs */
180                         (GInstanceInitFunc) modest_transport_account_decorator_instance_init,
181                         NULL
182                 };
183                 
184                 my_type = g_type_register_static (TNY_TYPE_CAMEL_TRANSPORT_ACCOUNT,
185                                                   "ModestTransportAccountDecorator",
186                                                   &my_info, 0);
187         }
188         return my_type;
189 }
190