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