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-presets.h"
33 /* Include config.h so that _() works: */
38 #define MODEST_PRESETS_KEY_NAME "Name"
39 #define MODEST_PRESETS_KEY_DOMAIN "Domain"
40 #define MODEST_PRESETS_KEY_MCC "MCC"
41 #define MODEST_PRESETS_KEY_INCOMING "IncomingMailServer"
42 #define MODEST_PRESETS_KEY_INCOMING_SECURITY "IncomingSecurity"
43 #define MODEST_PRESETS_KEY_OUTGOING "OutgoingMailServer"
44 #define MODEST_PRESETS_KEY_MAILBOX_TYPE "MailboxType"
45 #define MODEST_PRESETS_KEY_APOP "APOPSecureLogin"
46 #define MODEST_PRESETS_KEY_SECURE_SMTP "SecureSMTP"
50 modest_presets_new (const gchar *presetfile)
52 ModestPresets *presets = NULL;
55 g_return_val_if_fail (presetfile, NULL);
57 presets = g_new (ModestPresets, 1);
58 presets->keyfile = g_key_file_new ();
60 if (!presets->keyfile) {
61 g_printerr ("modest: cannot instantiate GKeyFile\n");
66 /* TODO: Unobfuscate an obfuscated file and then load it with g_key_file_load_from_data() instead. */
68 if (!g_key_file_load_from_file (presets->keyfile, presetfile,
69 G_KEY_FILE_NONE, &err)) {
70 g_printerr ("modest: cannot open keyfile from %s:\n %s\n", presetfile,
71 err ? err->message : "unknown reason");
81 modest_presets_get_providers (ModestPresets *self, guint mcc,
82 gboolean include_globals, gchar ***provider_ids)
84 gchar **all_providers = NULL;
85 gchar **all_provider_ids = NULL;
86 gchar **filtered = NULL;
87 gchar **filtered_ids = NULL;
91 g_return_val_if_fail (self && self->keyfile, NULL);
93 /* Get all the provider IDs: */
94 all_provider_ids = g_key_file_get_groups (self->keyfile, NULL);
95 len = g_strv_length(all_provider_ids);
97 /* Get the names for all these providers: */
98 all_providers = g_new0(gchar*, len + 1); /* Provider names. */
99 for (i=0; i != len; ++i) {
100 const gchar * provider_id = all_provider_ids[i];
102 gchar* name = g_key_file_get_string(self->keyfile, provider_id,
103 MODEST_PRESETS_KEY_NAME, NULL);
105 /* Be forgiving of missing names.
106 * If we use NULL then we will null-terminate the array.
111 all_providers[i] = name;
114 all_providers[i] = NULL;
117 /* return *all* providers? */
118 if (mcc == 0 && include_globals) {
119 *provider_ids = all_provider_ids;
120 return all_providers;
123 /* nope: filter them */
125 filtered = g_new0(gchar*, len + 1); /* Provider names. */
126 filtered_ids = g_new0(gchar*, len + 1); /* Provider IDs */
128 for (i=0, j=0; i != len; ++i) {
131 this_mcc = g_key_file_get_integer (self->keyfile, all_provider_ids[i],
132 MODEST_PRESETS_KEY_MCC, &err);
134 g_strfreev (all_providers);
135 g_strfreev (all_provider_ids);
136 g_strfreev (filtered);
137 g_strfreev (filtered_ids);
139 g_printerr ("modest: error parsing keyfile: %s\n", err->message);
145 if (this_mcc == mcc || (this_mcc == 0 && include_globals)) {
146 filtered[j] = all_providers[i];
147 filtered_ids[j] = all_provider_ids[i];
149 filtered[j] = NULL; /* the array must be NULL-terminated */
150 filtered_ids[j] = NULL; /* the array must be NULL-terminated */
152 all_providers[i] = NULL; /* g_strfreev: leave it alone */
153 all_provider_ids[i] = NULL; /* g_strfreev: leave it alone */
157 g_strfreev (all_providers);
158 g_strfreev (all_provider_ids);
160 *provider_ids = filtered_ids;
166 modest_presets_get_server (ModestPresets *self, const gchar *provider_id,
167 gboolean incoming_server)
169 g_return_val_if_fail (self && self->keyfile, NULL);
170 g_return_val_if_fail (provider_id, NULL);
172 return g_key_file_get_string (self->keyfile, provider_id,
174 MODEST_PRESETS_KEY_INCOMING :
175 MODEST_PRESETS_KEY_OUTGOING,
180 modest_presets_get_domain (ModestPresets *self,
181 const gchar *provider_id)
183 g_return_val_if_fail (self && self->keyfile, NULL);
184 g_return_val_if_fail (provider_id, NULL);
186 return g_key_file_get_string (self->keyfile, provider_id,
187 MODEST_PRESETS_KEY_DOMAIN,
194 ModestPresetsServerType
195 modest_presets_get_info_server_type (ModestPresets *self,
196 const gchar *provider_id,
197 gboolean incoming_server)
199 ModestPresetsServerType info = MODEST_PRESETS_SERVER_TYPE_NONE;
202 g_return_val_if_fail (self && self->keyfile, 0);
204 if (incoming_server) {
205 val = g_key_file_get_string (self->keyfile, provider_id,
206 MODEST_PRESETS_KEY_INCOMING, NULL);
211 val = g_key_file_get_string (self->keyfile, provider_id,
212 MODEST_PRESETS_KEY_MAILBOX_TYPE,NULL);
213 if (val && strcmp (val, "pop") == 0)
214 info = MODEST_PRESETS_SERVER_TYPE_POP;
215 if (val && strcmp (val, "imap") == 0)
216 info = MODEST_PRESETS_SERVER_TYPE_IMAP;
218 val = g_key_file_get_string (self->keyfile, provider_id,
219 MODEST_PRESETS_KEY_OUTGOING, NULL);
222 info = MODEST_PRESETS_SERVER_TYPE_SMTP;
227 /* g_message ("provider id: %s, server type: %d", provider_id, info); */
233 ModestPresetsSecurity
234 modest_presets_get_info_server_security (ModestPresets *self, const gchar *provider_id,
235 gboolean incoming_server)
237 ModestPresetsSecurity info = MODEST_PRESETS_SECURITY_NONE;
240 g_return_val_if_fail (self && self->keyfile, MODEST_PRESETS_SECURITY_NONE);
242 if (incoming_server) {
243 val = g_key_file_get_string (self->keyfile, provider_id,
244 MODEST_PRESETS_KEY_INCOMING, NULL);
247 val = g_key_file_get_string (self->keyfile, provider_id,
248 MODEST_PRESETS_KEY_APOP, NULL);
249 if (val && strcmp(val, "true") == 0)
250 info |= MODEST_PRESETS_SECURITY_APOP;
253 val = g_key_file_get_string (self->keyfile, provider_id,
254 MODEST_PRESETS_KEY_INCOMING_SECURITY, NULL);
255 if (val && strcmp (val, "1") == 0)
256 info |= MODEST_PRESETS_SECURITY_SECURE_INCOMING;
257 if (val && strcmp (val, "2") == 0) {
258 info |= MODEST_PRESETS_SECURITY_SECURE_INCOMING;
259 info |= MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT;
263 } else /* outgoing: */ {
264 val = g_key_file_get_string (self->keyfile, provider_id,
265 MODEST_PRESETS_KEY_OUTGOING, NULL);
268 info |= MODEST_PRESETS_SECURITY_SECURE_SMTP;
270 val = g_key_file_get_string (self->keyfile, provider_id,
271 MODEST_PRESETS_KEY_SECURE_SMTP, NULL);
272 if (val && strcmp(val,"true") == 0)
273 info |= MODEST_PRESETS_SECURITY_SECURE_SMTP;
279 /* g_message ("provider id: %s, apop:%s, secure-incoming:%s, altport: %s, secure-smtp: %s", */
281 /* info & MODEST_PRESETS_SECURITY_APOP ? "yes" : "no", */
282 /* info & MODEST_PRESETS_SECURITY_SECURE_INCOMING ? "yes" : "no", */
283 /* info & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT ? "yes" : "no", */
284 /* info & MODEST_PRESETS_SECURITY_SECURE_SMTP ? "yes" : "no"); */
293 modest_presets_destroy (ModestPresets *self)
298 g_key_file_free (self->keyfile);
299 self->keyfile = NULL;