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