2007-05-14 Murray Cumming <murrayc@murrayc.com>
[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
35
36 typedef struct {
37         ModestProtocol   proto;
38         const gchar*     name;
39         const gchar*     display_name;
40 } ProtocolInfo;
41
42 static const ProtocolInfo ProtocolMap[] = {
43         { MODEST_PROTOCOL_TRANSPORT_SENDMAIL, "sendmail", N_("Sendmail") },
44         { MODEST_PROTOCOL_TRANSPORT_SMTP,     "smtp",     N_("SMTP Server") },
45         
46         { MODEST_PROTOCOL_STORE_POP,          "pop",      N_("POP3") },
47         { MODEST_PROTOCOL_STORE_IMAP,         "imap",     N_("IMAPv4") },
48         { MODEST_PROTOCOL_STORE_MAILDIR,      "maildir",  N_("Maildir") },
49         { MODEST_PROTOCOL_STORE_MBOX,         "mbox",     N_("MBox") }
50 };
51 const guint PROTOCOL_SECURITY_MAP_SIZE = sizeof(ProtocolMap)/sizeof(ProtocolInfo);
52
53 static const ProtocolInfo ProtocolSecurityMap[] = {
54         { MODEST_PROTOCOL_SECURITY_NONE,      "none",     N_("None") },   
55         { MODEST_PROTOCOL_SECURITY_SSL,       "ssl",      N_("SSL") },   
56         { MODEST_PROTOCOL_SECURITY_TLS,       "tls",      N_("TLS") },
57         { MODEST_PROTOCOL_SECURITY_TLS_OP,    "tls-op",   N_("TLS when possible") } /* op stands for optional */
58 };
59 const guint PROTOCOL_MAP_SIZE = sizeof(ProtocolSecurityMap)/sizeof(ProtocolInfo);
60
61 static const ProtocolInfo ProtocolAuthMap[] = {
62         { MODEST_PROTOCOL_AUTH_NONE,          "none",     N_("None") },
63         { MODEST_PROTOCOL_AUTH_PASSWORD,      "password", N_("Password") },
64         { MODEST_PROTOCOL_AUTH_CRAMMD5,       "cram-md5", N_("Cram MD5") }
65 };
66 const guint PROTOCOL_AUTH_MAP_SIZE = sizeof(ProtocolAuthMap)/sizeof(ProtocolInfo);
67
68
69
70 #if 0
71 GSList*
72 modest_protocol_info_get_protocol_list (ModestProtocolType type)
73 {
74         GSList *proto_list = NULL;
75         int i;
76         
77         g_return_val_if_fail (type > MODEST_PROTOCOL_TYPE_UNKNOWN &&
78                               type < MODEST_PROTOCOL_TYPE_NUM,
79                               NULL);
80                 
81         for (i = 0; i != PROTOCOL_MAP_SIZE; ++i) {
82                 ProtocolInfo info = (ProtocolInfo)ProtocolMap[i];
83                 if (modest_protocol_info_get_protocol_type(info.proto) == type)
84                         proto_list = g_slist_append (proto_list, GINT_TO_POINTER(info.proto));
85         }
86         return proto_list;
87 }
88 #endif
89
90 static ModestPairList*
91 modest_protocol_info_get_pair_list (const ProtocolInfo* map, guint size)
92 {
93         ModestPairList *proto_list = NULL;
94         int i;
95
96         for (i = 0; i != size; ++i) {
97                 const ProtocolInfo info = map[i];
98                 proto_list = g_slist_append (proto_list,
99                                                      (gpointer)modest_pair_new(
100                                                              (gpointer)info.name,
101                                                              (gpointer)info.display_name,
102                                                              FALSE));                   
103         }
104         return proto_list;
105 }
106
107 ModestPairList*
108 modest_protocol_info_get_protocol_pair_list ()
109 {
110         return modest_protocol_info_get_pair_list (ProtocolMap, PROTOCOL_MAP_SIZE);
111 }
112
113 ModestPairList*
114 modest_protocol_info_get_protocol_security_pair_list ()
115 {
116         return modest_protocol_info_get_pair_list (ProtocolSecurityMap, PROTOCOL_SECURITY_MAP_SIZE);
117 }
118
119
120 ModestPairList*
121 modest_protocol_info_get_protocol_auth_pair_list ()
122 {
123         return modest_protocol_info_get_pair_list (ProtocolAuthMap, PROTOCOL_AUTH_MAP_SIZE);
124 }
125
126
127
128 ModestProtocolType
129 modest_protocol_info_get_protocol_type (ModestProtocol proto)
130 {
131         switch (proto) {
132         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
133         case MODEST_PROTOCOL_TRANSPORT_SMTP:
134                 return MODEST_PROTOCOL_TYPE_TRANSPORT;
135                 
136         case MODEST_PROTOCOL_STORE_POP:
137         case MODEST_PROTOCOL_STORE_IMAP:
138         case MODEST_PROTOCOL_STORE_MAILDIR:
139         case MODEST_PROTOCOL_STORE_MBOX:
140                 return MODEST_PROTOCOL_TYPE_STORE;
141
142         default:
143                 return MODEST_PROTOCOL_TYPE_UNKNOWN;
144         }
145 }
146
147
148 ModestProtocol
149 modest_protocol_info_get_protocol (const gchar* name)
150 {
151         int i;
152         g_return_val_if_fail (name, MODEST_PROTOCOL_UNKNOWN);
153
154         for (i = 0; i != PROTOCOL_MAP_SIZE; ++i) {
155                 ProtocolInfo info = (ProtocolInfo)ProtocolMap[i];
156                 if (strcmp(name, info.name) == 0)
157                         return info.proto;
158         }
159         
160         return MODEST_PROTOCOL_UNKNOWN;
161 }
162
163
164
165
166 /* get either the name or the display_name for the protocol */
167 static const gchar*
168 get_protocol_string (ModestProtocol proto, gboolean get_name)
169 {
170         int i;
171         g_return_val_if_fail (modest_protocol_info_get_protocol_type(proto) !=
172                               MODEST_PROTOCOL_TYPE_UNKNOWN, NULL);
173         
174         for (i = 0; i != PROTOCOL_MAP_SIZE; ++i) {
175                 ProtocolInfo info = (ProtocolInfo)ProtocolMap[i];
176                 if (info.proto == proto)
177                         return get_name ? info.name : info.display_name;        
178         }
179         g_return_val_if_reached (NULL);
180 }
181
182 const gchar*
183 modest_protocol_info_get_protocol_name (ModestProtocol proto)
184 {
185         return get_protocol_string (proto, TRUE);
186 }
187
188
189 const gchar*
190 modest_protocol_info_get_protocol_display_name (ModestProtocol proto)
191 {
192         return get_protocol_string (proto, FALSE);
193 }
194
195
196 gboolean
197 modest_protocol_info_protocol_is_local_store (ModestProtocol proto)
198 {
199         g_return_val_if_fail (modest_protocol_info_get_protocol_type (proto) !=
200                               MODEST_PROTOCOL_TYPE_UNKNOWN, FALSE);
201
202         /* may add MH later */
203         return proto == MODEST_PROTOCOL_STORE_MBOX || proto == MODEST_PROTOCOL_STORE_MAILDIR;
204 }
205
206