89baa117d6f140284170eac751a0389b6b87fef7
[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 TransportStoreProtocolMap[] = {
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
52 static const ProtocolInfo ConnectionProtocolMap[] = {
53         { MODEST_PROTOCOL_CONNECTION_NORMAL,    "none",     N_("None") },   
54         { MODEST_PROTOCOL_CONNECTION_SSL,       "ssl",      N_("SSL") },   
55         { MODEST_PROTOCOL_CONNECTION_TLS,       "tls",      N_("TLS") },
56         { MODEST_PROTOCOL_CONNECTION_TLS_OP,    "tls-op",   N_("TLS when possible") }
57         /* op stands for optional */
58 };
59 static const ProtocolInfo AuthProtocolMap[] = {
60         { MODEST_PROTOCOL_AUTH_NONE,          "none",     N_("None") },
61         { MODEST_PROTOCOL_AUTH_PASSWORD,      "password", N_("Password") },
62         { MODEST_PROTOCOL_AUTH_CRAMMD5,       "cram-md5", N_("Cram MD5") }
63 };
64
65
66
67 static const ProtocolInfo*
68 get_map (ModestProtocolType proto_type, guint *size)
69 {
70         const ProtocolInfo *map = NULL;
71         
72         switch (proto_type) {
73         case MODEST_TRANSPORT_STORE_PROTOCOL:
74                 map   = TransportStoreProtocolMap;
75                 *size = G_N_ELEMENTS(TransportStoreProtocolMap);
76                 break;
77         case MODEST_CONNECTION_PROTOCOL:
78                 map   = ConnectionProtocolMap;
79                 *size = G_N_ELEMENTS(ConnectionProtocolMap);
80                 break;
81         case MODEST_AUTH_PROTOCOL:
82                 map   = AuthProtocolMap;
83                 *size = G_N_ELEMENTS(AuthProtocolMap);
84                 break;
85         default:
86                 g_printerr ("modest: invalide protocol type %d", proto_type);
87         }
88         return map;
89 }
90
91
92 ModestPairList*
93 modest_protocol_info_get_protocol_pair_list (ModestProtocolType proto_type)
94 {
95         const ProtocolInfo *map;
96         ModestPairList *proto_list = NULL;
97         guint size;
98         int i;
99
100         map = get_map (proto_type, &size);
101         g_return_val_if_fail (map, NULL);
102         
103         for (i = 0; i != size; ++i) {
104                 const ProtocolInfo info = map[i];
105                 proto_list = g_slist_append (proto_list,
106                                              (gpointer)modest_pair_new(
107                                                      (gpointer)info.name,
108                                                      (gpointer)info.display_name,
109                                                      FALSE));                   
110         }
111         return proto_list;
112 }
113
114
115 ModestProtocol
116 modest_protocol_info_get_protocol (const gchar* name, ModestProtocolType proto_type)
117 {
118         const ProtocolInfo *map;
119         guint size;
120         int i;
121         
122         g_return_val_if_fail (name, MODEST_PROTOCOL_UNKNOWN);
123         
124         map = get_map (proto_type, &size);
125         g_return_val_if_fail (map, MODEST_PROTOCOL_UNKNOWN);
126
127         for (i = 0; i != size; ++i) {
128                 const ProtocolInfo info = map[i];
129                 if (strcmp(name, info.name) == 0)
130                         return info.proto;
131         }
132         
133         return MODEST_PROTOCOL_UNKNOWN;
134 }
135
136
137
138 /* get either the name or the display_name for the protocol */
139 static const gchar*
140 get_protocol_string (ModestProtocol proto, gboolean get_name, ModestProtocolType proto_type)
141 {
142         const ProtocolInfo *map;
143         guint size;
144         int i;
145                 
146         map = get_map (proto_type, &size);
147         g_return_val_if_fail (map, NULL);
148         
149         for (i = 0; i != size; ++i) {
150                 ProtocolInfo info = map[i];
151                 if (info.proto == proto)
152                         return get_name ? info.name : info.display_name;        
153         }
154         g_return_val_if_reached (NULL);
155 }
156
157 const gchar*
158 modest_protocol_info_get_protocol_name (ModestProtocol proto, ModestProtocolType proto_type)
159 {
160         return get_protocol_string (proto, TRUE, proto_type);
161 }
162
163
164 const gchar*
165 modest_protocol_info_get_protocol_display_name (ModestProtocol proto, ModestProtocolType proto_type)
166 {
167         return get_protocol_string (proto, FALSE, proto_type);
168 }
169
170
171 gboolean
172 modest_protocol_info_protocol_is_local_store (ModestTransportStoreProtocol proto)
173 {
174         return proto == MODEST_PROTOCOL_STORE_MBOX || proto == MODEST_PROTOCOL_STORE_MAILDIR;
175 }
176
177
178
179 gboolean
180 modest_protocol_info_protocol_is_store (ModestTransportStoreProtocol proto)
181 {
182         return proto == MODEST_PROTOCOL_STORE_MBOX || proto == MODEST_PROTOCOL_STORE_MAILDIR ||
183                 proto == MODEST_PROTOCOL_STORE_POP || proto == MODEST_PROTOCOL_STORE_IMAP;
184 }
185
186