Fixed a leak in modest_mail_operation_get_msgs_full()
[modest] / src / modest-protocol-info.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 <glib/gi18n.h>
31 #include <string.h> /* strcmp */
32 #include <modest-protocol-info.h>
33 #include <modest-pair.h>
34 #include <modest-defs.h>
35 #include <modest-text-utils.h>
36
37 typedef struct {
38         gint   proto;
39         const gchar*     name;
40         const gchar*     display_name;
41 } ProtocolInfo;
42
43 static const ProtocolInfo TransportStoreProtocolMap[] = {
44         { MODEST_PROTOCOL_TRANSPORT_SENDMAIL, "sendmail", N_("Sendmail") },
45         { MODEST_PROTOCOL_TRANSPORT_SMTP,     "smtp",     N_("SMTP Server") },
46         
47         { MODEST_PROTOCOL_STORE_POP,          "pop",      N_("POP3") },
48         { MODEST_PROTOCOL_STORE_IMAP,         "imap",     N_("IMAPv4") },
49         { MODEST_PROTOCOL_STORE_MAILDIR,      "maildir",  N_("Maildir") },
50         { MODEST_PROTOCOL_STORE_MBOX,         "mbox",     N_("MBox") }
51 };
52
53 static const ProtocolInfo ConnectionProtocolMap[] = {
54         { MODEST_PROTOCOL_CONNECTION_NORMAL,    "none",     N_("None") },   
55         { MODEST_PROTOCOL_CONNECTION_SSL,       "ssl",      N_("SSL") },   
56         { MODEST_PROTOCOL_CONNECTION_TLS,       "tls",      N_("TLS") },
57         { MODEST_PROTOCOL_CONNECTION_TLS_OP,    "tls-op",   N_("TLS when possible") }
58         /* op stands for optional */
59 };
60
61
62 /* FIXME: these names must match those of tny_camel_account_get_supported_secure_auth */
63 static const ProtocolInfo AuthProtocolMap[] = {
64         { MODEST_PROTOCOL_AUTH_NONE,          MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE,     N_("None") },
65         { MODEST_PROTOCOL_AUTH_PASSWORD,      MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD, N_("Password") },
66         { MODEST_PROTOCOL_AUTH_CRAMMD5,       MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5, N_("Cram MD5") }
67 };
68
69 static ModestPairList*
70 get_protocol_pair_list (const ProtocolInfo* map, guint size)
71 {
72         g_return_val_if_fail (map, NULL);
73
74         ModestPairList *proto_list = NULL;      
75         int i;
76         for (i = 0; i != size; ++i) {
77                 const ProtocolInfo info = map[i];
78                 proto_list = g_slist_append (proto_list,
79                                              (gpointer)modest_pair_new(
80                                                      (gpointer)info.name,
81                                                      (gpointer)info.display_name,
82                                                      FALSE));                   
83         }
84         return proto_list;
85 }
86
87 static gint
88 get_protocol_by_name (const ProtocolInfo* map,
89                       guint size,
90                       const gchar* query_name,
91                       gint default_value,
92                       gboolean case_sensitive)
93 {
94         guint i;
95
96         g_return_val_if_fail (map, default_value);
97         g_return_val_if_fail (query_name, default_value);
98         
99         for(i = 0; i < size; ++i)
100                 if (modest_text_utils_utf8_strcmp (map[i].name, query_name,
101                                                    !case_sensitive) == 0)
102                         return map[i].proto;
103         
104         return default_value;
105 }
106
107 ModestPairList*
108 modest_protocol_info_get_transport_store_protocol_pair_list (void)
109 {
110         return get_protocol_pair_list (TransportStoreProtocolMap,
111                 G_N_ELEMENTS(TransportStoreProtocolMap));
112 }
113
114 ModestPairList*
115 modest_protocol_info_get_auth_protocol_pair_list (void)
116 {
117         return get_protocol_pair_list (AuthProtocolMap,
118                 G_N_ELEMENTS(AuthProtocolMap));
119 }
120
121 ModestPairList*
122 modest_protocol_info_get_connection_protocol_pair_list (void)
123 {
124         return get_protocol_pair_list (ConnectionProtocolMap,
125                 G_N_ELEMENTS(ConnectionProtocolMap));
126 }
127         
128 ModestTransportStoreProtocol
129 modest_protocol_info_get_transport_store_protocol (const gchar* name)
130 {
131         g_return_val_if_fail (name, MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN);
132         
133         return get_protocol_by_name(TransportStoreProtocolMap,
134                                     G_N_ELEMENTS(TransportStoreProtocolMap),
135                                     name,
136                                     MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN,
137                                     TRUE);
138 }
139
140 ModestAuthProtocol
141 modest_protocol_info_get_auth_protocol (const gchar* name)
142 {
143         g_return_val_if_fail (name, MODEST_PROTOCOL_AUTH_NONE);
144         
145         return get_protocol_by_name(AuthProtocolMap,
146                                     G_N_ELEMENTS(AuthProtocolMap),
147                                     name,
148                                     MODEST_PROTOCOL_AUTH_NONE,
149                                     FALSE);
150 }
151
152 ModestConnectionProtocol
153 modest_protocol_info_get_connection_protocol (const gchar* name)
154 {
155         g_return_val_if_fail (name, MODEST_PROTOCOL_CONNECTION_NORMAL);
156         
157         return get_protocol_by_name(ConnectionProtocolMap,
158                                     G_N_ELEMENTS(ConnectionProtocolMap),
159                                     name,
160                                     MODEST_PROTOCOL_CONNECTION_NORMAL,
161                                     FALSE);
162 }
163
164
165 /* get either the name or the display_name for the protocol */
166 static const gchar*
167 get_protocol_string (gint proto, const ProtocolInfo* map, guint size, gboolean get_name)
168 {
169         g_return_val_if_fail (map, NULL);
170         
171         int i;
172         for (i = 0; i != size; ++i) {
173                 ProtocolInfo info = map[i];
174                 if (info.proto == proto)
175                         return get_name ? info.name : info.display_name;        
176         }
177         g_return_val_if_reached (NULL);
178 }
179
180 const gchar*
181 modest_protocol_info_get_transport_store_protocol_name (ModestTransportStoreProtocol proto)
182 {
183         return get_protocol_string (proto, TransportStoreProtocolMap,
184                 G_N_ELEMENTS(TransportStoreProtocolMap), TRUE);
185 }
186
187 const gchar*
188 modest_protocol_info_get_auth_protocol_name (ModestAuthProtocol proto)
189 {
190         return get_protocol_string (proto, AuthProtocolMap,
191                 G_N_ELEMENTS(AuthProtocolMap), TRUE);
192 }
193
194 const gchar*
195 modest_protocol_info_get_connection_protocol_name (ModestAuthProtocol proto)
196 {
197         return get_protocol_string (proto, ConnectionProtocolMap,
198                 G_N_ELEMENTS(ConnectionProtocolMap), TRUE);
199 }
200
201
202 gboolean
203 modest_protocol_info_protocol_is_local_store (ModestTransportStoreProtocol proto)
204 {
205         return proto == MODEST_PROTOCOL_STORE_MBOX || proto == MODEST_PROTOCOL_STORE_MAILDIR;
206 }
207
208
209
210 gboolean
211 modest_protocol_info_protocol_is_store (ModestTransportStoreProtocol proto)
212 {
213         return proto == MODEST_PROTOCOL_STORE_MBOX || proto == MODEST_PROTOCOL_STORE_MAILDIR ||
214                 proto == MODEST_PROTOCOL_STORE_POP || proto == MODEST_PROTOCOL_STORE_IMAP;
215 }
216
217 gboolean
218 modest_protocol_info_is_secure(ModestConnectionProtocol protocol)
219 {
220         return (protocol == MODEST_PROTOCOL_CONNECTION_SSL ||
221                                         protocol == MODEST_PROTOCOL_CONNECTION_TLS);
222 }
223
224 gboolean modest_protocol_info_auth_is_secure(ModestAuthProtocol protocol)
225 {
226         return (protocol == MODEST_PROTOCOL_AUTH_CRAMMD5);
227 }