Show remote folders in "Move to" dialog when viewing archive folder
[modest] / src / modest-presets.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 <string.h> /* for strcmp */
31 #include <modest-protocol-registry.h>
32 #include <modest-runtime.h>
33 #include "modest-presets.h"
34 #include <stdio.h>
35
36 /* Include config.h so that _() works: */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
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"
51                                                     
52
53 ModestPresets*
54 modest_presets_new (const gchar *presetfile)
55 {
56         ModestPresets *presets = NULL;
57         GError        *err     = NULL;
58         
59         g_return_val_if_fail (presetfile, NULL);
60         
61         presets = g_new (ModestPresets, 1);
62         presets->keyfile = g_key_file_new ();
63
64         if (!presets->keyfile) {
65                 g_printerr ("modest: cannot instantiate GKeyFile\n");
66                 g_free (presets);
67                 return NULL;
68         }
69         
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");
74                 g_error_free (err);
75                 g_free (presets);
76                 return NULL;
77         }
78
79         return presets;
80 }
81
82 /* cluster mcc's, based on the list
83  * http://en.wikipedia.org/wiki/Mobile_country_code
84  *
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.
89  */
90 static int
91 effective_mcc (gint mcc)
92 {
93         switch (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 */
100         case 311:
101         case 312:
102         case 313:
103         case 314:
104         case 315:
105         case 316: return 310; /* united states */
106         default:  return mcc;
107         }
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
110            more difficult */
111 }
112
113 gchar**
114 modest_presets_get_providers  (ModestPresets *self, guint mcc,
115                                gboolean include_globals, gchar ***provider_ids)
116 {
117         gchar **all_providers = NULL;
118         gchar **all_provider_ids = NULL;
119         gchar **filtered  = NULL;
120         gchar **filtered_ids = NULL;
121         GError *err       = NULL;
122         guint i, j, len;
123
124         g_return_val_if_fail (self && self->keyfile, NULL);
125
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);
129
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];
134                 if(provider_id) {
135                         gchar* name = g_key_file_get_string(self->keyfile, provider_id, 
136                                 MODEST_PRESETS_KEY_NAME, NULL);
137
138                         /* Be forgiving of missing names.
139                          * If we use NULL then we will null-terminate the array.
140                          */
141                         if(!name)
142                                 name = g_strdup("");
143
144                         all_providers[i] = name;
145                 }
146                 else
147                         all_providers[i] = NULL;
148         };
149
150         /* return *all* providers? */
151         /*
152         if (mcc == 0 && include_globals) {
153                 *provider_ids = all_provider_ids;
154                 return all_providers;
155         }
156         */
157
158         /* nope: filter them */
159
160         filtered = g_new0(gchar*, len + 1); /* Provider names. */
161         filtered_ids = g_new0(gchar*, len + 1); /* Provider IDs */
162
163         for (i=0, j=0; i != len; ++i) {
164
165                 int this_mcc;
166                 this_mcc = g_key_file_get_integer (self->keyfile, all_provider_ids[i],
167                                                    MODEST_PRESETS_KEY_MCC, &err);
168                 if (err) {
169                         g_strfreev (all_providers);
170                         g_strfreev (all_provider_ids);
171                         g_strfreev (filtered);
172                         g_strfreev (filtered_ids);
173
174                         g_printerr ("modest: error parsing keyfile: %s\n", err->message);
175                         g_error_free (err);
176
177                         return NULL;
178                 }
179
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];
185                         ++j;
186                         filtered[j] = NULL; /* the array must be NULL-terminated */
187                         filtered_ids[j] = NULL; /* the array must be NULL-terminated */
188
189                         all_providers[i]  = NULL; /*  g_strfreev: leave it alone */
190                         all_provider_ids[i]  = NULL; /*  g_strfreev: leave it alone */
191                 }
192         }
193         g_strfreev (all_providers);
194         g_strfreev (all_provider_ids);
195         
196         *provider_ids = filtered_ids;
197         return filtered;
198 }
199
200
201 gchar*
202 modest_presets_get_server (ModestPresets *self, const gchar *provider_id,
203                            gboolean incoming_server)
204 {       
205         g_return_val_if_fail (self && self->keyfile, NULL);
206         g_return_val_if_fail (provider_id, NULL);
207
208         return g_key_file_get_string (self->keyfile, provider_id, 
209                                       incoming_server ?
210                                       MODEST_PRESETS_KEY_INCOMING :
211                                       MODEST_PRESETS_KEY_OUTGOING,
212                                       NULL);
213 }
214
215 gchar *
216 modest_presets_get_domain      (ModestPresets *self,
217                                 const gchar *provider_id)
218 {       
219         g_return_val_if_fail (self && self->keyfile, NULL);
220         g_return_val_if_fail (provider_id, NULL);
221
222         return g_key_file_get_string (self->keyfile, provider_id, 
223                                       MODEST_PRESETS_KEY_DOMAIN,
224                                       NULL);
225 }               
226
227
228
229
230 ModestProtocolType
231 modest_presets_get_info_server_type (ModestPresets *self,
232                                      const gchar *provider_id,
233                                      gboolean incoming_server)
234 {
235         ModestProtocolType protocol_type = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
236         ModestProtocolRegistry *protocol_registry;
237         ModestProtocol *protocol;
238         gchar *val = NULL;
239         
240         g_return_val_if_fail (self && self->keyfile, 0);
241         protocol_registry = modest_runtime_get_protocol_registry ();
242
243         if (incoming_server) {
244                 val = g_key_file_get_string (self->keyfile, provider_id,
245                                              MODEST_PRESETS_KEY_INCOMING, NULL);
246                 if (!val)
247                         return protocol_type;
248                 
249                 g_free (val);
250                 val = g_key_file_get_string (self->keyfile, provider_id,
251                                              MODEST_PRESETS_KEY_MAILBOX_TYPE,NULL);
252                 
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);
257         } else {
258                 val = g_key_file_get_string (self->keyfile, provider_id,
259                                              MODEST_PRESETS_KEY_OUTGOING, NULL);
260                 if (!val)
261                         return protocol_type;
262
263                 protocol_type = MODEST_PROTOCOLS_TRANSPORT_SMTP;
264         }
265         g_free (val);
266
267         /* debug: */
268 /*      g_debug ("provider id: %s, server type: %d", provider_id, info); */
269         return protocol_type;
270 }
271
272
273
274 ModestProtocolType
275 modest_presets_get_info_server_security (ModestPresets *self, const gchar *provider_id,
276                                          gboolean incoming_server)
277 {
278         ModestProtocolType protocol_type = MODEST_PROTOCOLS_CONNECTION_NONE;
279         gchar *val = NULL;
280         
281         g_return_val_if_fail (self && self->keyfile, MODEST_PROTOCOLS_CONNECTION_NONE);
282
283         if (incoming_server) {
284                 val = g_key_file_get_string (self->keyfile, provider_id,
285                                              MODEST_PRESETS_KEY_INCOMING, NULL);
286                 if (val) {
287                         g_free (val);
288
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;
299                         }
300                         g_free (val);
301                 }
302         } else /* outgoing: */ {
303                 val = g_key_file_get_string (self->keyfile, provider_id,
304                                              MODEST_PRESETS_KEY_OUTGOING, NULL);
305                 if (val) {
306                         g_free (val);
307
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;
320                         g_free(val);
321                 }
322         }
323
324         return protocol_type;
325 }
326
327 gboolean 
328 modest_presets_get_info_server_use_alternate_port (ModestPresets *self, const gchar *provider_id,
329                                                    gboolean incoming_server)
330 {
331         gboolean result = FALSE;
332         gchar *val = NULL;
333         
334         g_return_val_if_fail (self && self->keyfile, MODEST_PROTOCOLS_CONNECTION_NONE);
335
336         if (incoming_server) {
337                 val = g_key_file_get_string (self->keyfile, provider_id,
338                                              MODEST_PRESETS_KEY_INCOMING, NULL);
339                 if (val) {
340                         g_free (val);   
341
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)) {
345                                 result = TRUE;
346                         }
347                         g_free (val);
348                 }
349         } 
350
351         return result;
352 }
353
354 ModestProtocolType
355 modest_presets_get_info_server_auth (ModestPresets *self, const gchar *provider_id,
356                                          gboolean incoming_server)
357 {
358         ModestProtocolType protocol_type = MODEST_PROTOCOLS_AUTH_NONE;
359         gchar *val = NULL;
360         
361         g_return_val_if_fail (self && self->keyfile, MODEST_PROTOCOLS_AUTH_NONE);
362
363         if (incoming_server) {
364                 val = g_key_file_get_string (self->keyfile, provider_id,
365                                              MODEST_PRESETS_KEY_INCOMING, NULL);
366                 if (val) {
367                         g_free (val);
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;
372                         g_free(val);
373
374                 }
375         } else /* outgoing: */ {
376                 val = g_key_file_get_string (self->keyfile, provider_id,
377                                              MODEST_PRESETS_KEY_OUTGOING, NULL);
378                 if (val) {
379                         g_free (val);
380                         
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;
386                         g_free(val);
387                 }
388         }
389
390         return protocol_type;
391 }
392
393 /*
394  * at the moment, this only for mac.com, which have a special SMTP port
395  */
396 guint
397 modest_presets_get_port (ModestPresets *self, const gchar* provider_id,
398                          gboolean incoming_server)
399 {
400         guint port;
401         
402         g_return_val_if_fail (self && self->keyfile, 0);
403
404         if (incoming_server)
405                 port = 0; /* not used yet */
406         else 
407                 port = (guint)g_key_file_get_integer (self->keyfile, provider_id,
408                                                       MODEST_PRESETS_KEY_SMTP_PORT, NULL);
409
410         return port;
411 }
412
413
414
415
416         
417 void
418 modest_presets_destroy (ModestPresets *self)
419 {
420         if (!self)
421                 return;
422
423         g_key_file_free (self->keyfile);
424         self->keyfile = NULL;
425         
426         g_free (self);
427 }