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