1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <string.h> /* for strcmp */
31 #include <modest-protocol-registry.h>
32 #include <modest-runtime.h>
33 #include "modest-presets.h"
36 /* Include config.h so that _() works: */
41 #define MODEST_PRESETS_KEY_NAME "Name"
42 #define MODEST_PRESETS_KEY_DOMAIN "Domain"
43 #define MODEST_PRESETS_KEY_MCC "MCC"
44 #define MODEST_PRESETS_KEY_INCOMING "IncomingMailServer"
45 #define MODEST_PRESETS_KEY_INCOMING_SECURITY "IncomingSecurity"
46 #define MODEST_PRESETS_KEY_OUTGOING "OutgoingMailServer"
47 #define MODEST_PRESETS_KEY_MAILBOX_TYPE "MailboxType"
48 #define MODEST_PRESETS_KEY_APOP "APOPSecureLogin"
49 #define MODEST_PRESETS_KEY_SECURE_SMTP "SecureSmtp"
50 #define MODEST_PRESETS_KEY_SMTP_PORT "SmtpPort"
54 modest_presets_new (const gchar *presetfile)
56 ModestPresets *presets = NULL;
59 g_return_val_if_fail (presetfile, NULL);
61 presets = g_new (ModestPresets, 1);
62 presets->keyfile = g_key_file_new ();
64 if (!presets->keyfile) {
65 g_printerr ("modest: cannot instantiate GKeyFile\n");
70 if (!g_key_file_load_from_file (presets->keyfile, presetfile,
71 G_KEY_FILE_NONE, &err)) {
72 g_printerr ("modest: cannot open keyfile from %s:\n %s\n", presetfile,
73 err ? err->message : "unknown reason");
82 /* cluster mcc's, based on the list
83 * http://en.wikipedia.org/wiki/Mobile_country_code
85 * This function will return the "effective mcc", which is the
86 * normalized mcc for a country - ie. even if the there are multiple
87 * entries for the United States with various mcc's, this function will
88 * always return 310, even if the real mcc parsed would be 314.
91 effective_mcc (gint mcc)
94 case 405: return 404; /* india */
95 case 441: return 440; /* japan */
96 case 348: /* NOTE: see below */
97 case 235: return 234; /* united kingdom */
98 case 289: return 282; /* georgia */
99 case 549: /* NOTE: see below */
105 case 316: return 310; /* united states */
108 /* NOTE: 348 for UK and 549 for US are not correct, but we do
109 a workaround here as changing operator-wizard package is
114 modest_presets_get_providers (ModestPresets *self, guint mcc,
115 gboolean include_globals, gchar ***provider_ids)
117 gchar **all_providers = NULL;
118 gchar **all_provider_ids = NULL;
119 gchar **filtered = NULL;
120 gchar **filtered_ids = NULL;
124 g_return_val_if_fail (self && self->keyfile, NULL);
126 /* Get all the provider IDs: */
127 all_provider_ids = g_key_file_get_groups (self->keyfile, NULL);
128 len = g_strv_length(all_provider_ids);
130 /* Get the names for all these providers: */
131 all_providers = g_new0(gchar*, len + 1); /* Provider names. */
132 for (i=0; i != len; ++i) {
133 const gchar * provider_id = all_provider_ids[i];
135 gchar* name = g_key_file_get_string(self->keyfile, provider_id,
136 MODEST_PRESETS_KEY_NAME, NULL);
138 /* Be forgiving of missing names.
139 * If we use NULL then we will null-terminate the array.
144 all_providers[i] = name;
147 all_providers[i] = NULL;
150 /* return *all* providers? */
152 if (mcc == 0 && include_globals) {
153 *provider_ids = all_provider_ids;
154 return all_providers;
158 /* nope: filter them */
160 filtered = g_new0(gchar*, len + 1); /* Provider names. */
161 filtered_ids = g_new0(gchar*, len + 1); /* Provider IDs */
163 for (i=0, j=0; i != len; ++i) {
166 this_mcc = g_key_file_get_integer (self->keyfile, all_provider_ids[i],
167 MODEST_PRESETS_KEY_MCC, &err);
169 g_strfreev (all_providers);
170 g_strfreev (all_provider_ids);
171 g_strfreev (filtered);
172 g_strfreev (filtered_ids);
174 g_printerr ("modest: error parsing keyfile: %s\n", err->message);
180 if (this_mcc == mcc ||
181 effective_mcc (this_mcc) == effective_mcc (mcc) ||
182 (this_mcc == 0 && include_globals)) {
183 filtered[j] = all_providers[i];
184 filtered_ids[j] = all_provider_ids[i];
186 filtered[j] = NULL; /* the array must be NULL-terminated */
187 filtered_ids[j] = NULL; /* the array must be NULL-terminated */
189 all_providers[i] = NULL; /* g_strfreev: leave it alone */
190 all_provider_ids[i] = NULL; /* g_strfreev: leave it alone */
193 g_strfreev (all_providers);
194 g_strfreev (all_provider_ids);
196 *provider_ids = filtered_ids;
202 modest_presets_get_server (ModestPresets *self, const gchar *provider_id,
203 gboolean incoming_server)
205 g_return_val_if_fail (self && self->keyfile, NULL);
206 g_return_val_if_fail (provider_id, NULL);
208 return g_key_file_get_string (self->keyfile, provider_id,
210 MODEST_PRESETS_KEY_INCOMING :
211 MODEST_PRESETS_KEY_OUTGOING,
216 modest_presets_get_domain (ModestPresets *self,
217 const gchar *provider_id)
219 g_return_val_if_fail (self && self->keyfile, NULL);
220 g_return_val_if_fail (provider_id, NULL);
222 return g_key_file_get_string (self->keyfile, provider_id,
223 MODEST_PRESETS_KEY_DOMAIN,
231 modest_presets_get_info_server_type (ModestPresets *self,
232 const gchar *provider_id,
233 gboolean incoming_server)
235 ModestProtocolType protocol_type = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
236 ModestProtocolRegistry *protocol_registry;
237 ModestProtocol *protocol;
240 g_return_val_if_fail (self && self->keyfile, 0);
241 protocol_registry = modest_runtime_get_protocol_registry ();
243 if (incoming_server) {
244 val = g_key_file_get_string (self->keyfile, provider_id,
245 MODEST_PRESETS_KEY_INCOMING, NULL);
247 return protocol_type;
250 val = g_key_file_get_string (self->keyfile, provider_id,
251 MODEST_PRESETS_KEY_MAILBOX_TYPE,NULL);
253 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS, val);
254 if (protocol == NULL)
255 return protocol_type;
256 protocol_type = modest_protocol_get_type_id (protocol);
258 val = g_key_file_get_string (self->keyfile, provider_id,
259 MODEST_PRESETS_KEY_OUTGOING, NULL);
261 return protocol_type;
263 protocol_type = MODEST_PROTOCOLS_TRANSPORT_SMTP;
268 /* g_debug ("provider id: %s, server type: %d", provider_id, info); */
269 return protocol_type;
275 modest_presets_get_info_server_security (ModestPresets *self, const gchar *provider_id,
276 gboolean incoming_server)
278 ModestProtocolType protocol_type = MODEST_PROTOCOLS_CONNECTION_NONE;
281 g_return_val_if_fail (self && self->keyfile, MODEST_PROTOCOLS_CONNECTION_NONE);
283 if (incoming_server) {
284 val = g_key_file_get_string (self->keyfile, provider_id,
285 MODEST_PRESETS_KEY_INCOMING, NULL);
289 val = g_key_file_get_string (self->keyfile, provider_id,
290 MODEST_PRESETS_KEY_INCOMING_SECURITY, NULL);
291 if (val && strcmp (val, "1") == 0) {
292 protocol_type = MODEST_PROTOCOLS_CONNECTION_TLS;
293 } else if (val && strcmp (val, "2") == 0) {
294 protocol_type = MODEST_PROTOCOLS_CONNECTION_SSL;
295 } else if (val && (strcmp (val, "tls") == 0)) {
296 protocol_type = MODEST_PROTOCOLS_CONNECTION_TLS;
297 } else if (val && (strcmp (val, "ssl") == 0)) {
298 protocol_type = MODEST_PROTOCOLS_CONNECTION_SSL;
302 } else /* outgoing: */ {
303 val = g_key_file_get_string (self->keyfile, provider_id,
304 MODEST_PRESETS_KEY_OUTGOING, NULL);
308 val = g_key_file_get_string (self->keyfile, provider_id,
309 MODEST_PRESETS_KEY_SECURE_SMTP, NULL);
310 if (val && strcmp(val,"true") == 0)
311 protocol_type = MODEST_PROTOCOLS_CONNECTION_SSL;
312 else if (val && strcmp (val, "ssl") == 0)
313 protocol_type = MODEST_PROTOCOLS_CONNECTION_SSL;
314 else if (val && strcmp (val, "2") == 0)
315 protocol_type = MODEST_PROTOCOLS_CONNECTION_SSL;
316 else if (val && strcmp (val, "tls") == 0)
317 protocol_type = MODEST_PROTOCOLS_CONNECTION_TLS;
318 else if (val && strcmp (val, "1") == 0)
319 protocol_type = MODEST_PROTOCOLS_CONNECTION_TLS;
324 return protocol_type;
328 modest_presets_get_info_server_use_alternate_port (ModestPresets *self, const gchar *provider_id,
329 gboolean incoming_server)
331 gboolean result = FALSE;
334 g_return_val_if_fail (self && self->keyfile, MODEST_PROTOCOLS_CONNECTION_NONE);
336 if (incoming_server) {
337 val = g_key_file_get_string (self->keyfile, provider_id,
338 MODEST_PRESETS_KEY_INCOMING, NULL);
342 val = g_key_file_get_string (self->keyfile, provider_id,
343 MODEST_PRESETS_KEY_INCOMING_SECURITY, NULL);
344 if (val && (strcmp (val, "2") == 0)) {
355 modest_presets_get_info_server_auth (ModestPresets *self, const gchar *provider_id,
356 gboolean incoming_server)
358 ModestProtocolType protocol_type = MODEST_PROTOCOLS_AUTH_NONE;
361 g_return_val_if_fail (self && self->keyfile, MODEST_PROTOCOLS_AUTH_NONE);
363 if (incoming_server) {
364 val = g_key_file_get_string (self->keyfile, provider_id,
365 MODEST_PRESETS_KEY_INCOMING, NULL);
368 val = g_key_file_get_string (self->keyfile, provider_id,
369 MODEST_PRESETS_KEY_APOP, NULL);
370 if (val && strcmp(val, "true") == 0)
371 protocol_type = MODEST_PROTOCOLS_AUTH_PASSWORD;
375 } else /* outgoing: */ {
376 val = g_key_file_get_string (self->keyfile, provider_id,
377 MODEST_PRESETS_KEY_OUTGOING, NULL);
381 val = g_key_file_get_string (self->keyfile, provider_id,
382 MODEST_PRESETS_KEY_SECURE_SMTP, NULL);
383 /* printf("debug: %s: provider_id=%s, secure-smtp val=%s\n", __FUNCTION__, provider_id, val); */
384 if (val && strcmp(val,"true") == 0)
385 protocol_type = MODEST_PROTOCOLS_AUTH_PASSWORD;
390 return protocol_type;
394 * at the moment, this only for mac.com, which have a special SMTP port
397 modest_presets_get_port (ModestPresets *self, const gchar* provider_id,
398 gboolean incoming_server)
402 g_return_val_if_fail (self && self->keyfile, 0);
405 port = 0; /* not used yet */
407 port = (guint)g_key_file_get_integer (self->keyfile, provider_id,
408 MODEST_PRESETS_KEY_SMTP_PORT, NULL);
418 modest_presets_destroy (ModestPresets *self)
423 g_key_file_free (self->keyfile);
424 self->keyfile = NULL;