g_utf8_pointer_to_offset returns offsets in char units and not in byte units.
[modest] / src / modest-protocol-registry.c
1 /* Copyright (c) 2007, 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>
31 #include <modest-account-protocol.h>
32 #include <modest-defs.h>
33 #include <modest-protocol-registry.h>
34 #include <modest-transport-account-decorator.h>
35 #include <tny-camel-pop-store-account.h>
36 #include <tny-camel-imap-store-account.h>
37 #include <tny-camel-store-account.h>
38 #include <tny-camel-transport-account.h>
39 #include <tny-simple-list.h>
40
41 #define TAG_ALL_PROTOCOLS "__MODEST_PROTOCOL_REGISTRY_ALL_PROTOCOLS"
42
43 /* These seem to be listed in 
44  * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c 
45  */
46 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
47 /* This is a tinymail camel-lite specific option, 
48  * roughly equivalent to "always" in regular camel,
49  * which is appropriate for a generic "SSL" connection option: */
50 #define MODEST_ACCOUNT_OPTION_SSL_WRAPPED "wrapped"
51 /* Not used in our UI so far: */
52 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
53 /* This is a tinymailcamel-lite specific option that is not in regular camel. */
54 #define MODEST_ACCOUNT_OPTION_SSL_TLS "tls"
55
56 /* Posssible values for tny_account_set_secure_auth_mech().
57  * These might be camel-specific.
58  * Really, tinymail should use an enum.
59  * camel_sasl_authtype() seems to list some possible values.
60  */
61  
62 /* Note that evolution does not offer these for IMAP: */
63 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
64 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
65
66 /* Caeml's IMAP uses NULL instead for "Password".
67  * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
68 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN" 
69 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
70
71 /* These seem to be listed in 
72  * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c 
73  */
74 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
75 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
76
77 /* 'private'/'protected' functions */
78 static void   modest_protocol_registry_class_init (ModestProtocolRegistryClass *klass);
79 static void   modest_protocol_registry_finalize   (GObject *obj);
80 static void   modest_protocol_registry_instance_init (ModestProtocolRegistry *obj);
81 static GHashTable *   modest_protocol_registry_create_tag (ModestProtocolRegistry *obj, const gchar *tag);
82
83 /* translation handlers */
84 static gchar * translation_is_userdata (gpointer userdata, va_list args);
85
86 typedef struct _ModestProtocolRegistryPrivate ModestProtocolRegistryPrivate;
87 struct _ModestProtocolRegistryPrivate {
88         GHashTable *tags_table;
89         GHashTable *priorities;
90 };
91
92 #define MODEST_PROTOCOL_REGISTRY_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
93                                                                          MODEST_TYPE_PROTOCOL_REGISTRY, \
94                                                                          ModestProtocolRegistryPrivate))
95
96 /* globals */
97 static GObjectClass *parent_class = NULL;
98
99 static ModestProtocolType pop_protocol_type_id = -1;
100 static ModestProtocolType imap_protocol_type_id = -1;
101 static ModestProtocolType maildir_protocol_type_id = -1;
102 static ModestProtocolType mbox_protocol_type_id = -1;
103 static ModestProtocolType smtp_protocol_type_id = -1;
104 static ModestProtocolType sendmail_protocol_type_id = -1;
105 static ModestProtocolType none_connection_protocol_type_id = -1;
106 static ModestProtocolType ssl_connection_protocol_type_id = -1;
107 static ModestProtocolType tls_connection_protocol_type_id = -1;
108 static ModestProtocolType tlsop_connection_protocol_type_id = -1;
109 static ModestProtocolType none_auth_protocol_type_id = -1;
110 static ModestProtocolType password_auth_protocol_type_id = -1;
111 static ModestProtocolType crammd5_auth_protocol_type_id = -1;
112
113 GType
114 modest_protocol_registry_get_type (void)
115 {
116         static GType my_type = 0;
117
118         if (!my_type) {
119                 static const GTypeInfo my_info = {
120                         sizeof(ModestProtocolRegistryClass),
121                         NULL,   /* base init */
122                         NULL,   /* base finalize */
123                         (GClassInitFunc) modest_protocol_registry_class_init,
124                         NULL,   /* class finalize */
125                         NULL,   /* class data */
126                         sizeof(ModestProtocolRegistry),
127                         0,      /* n_preallocs */
128                         (GInstanceInitFunc) modest_protocol_registry_instance_init,
129                         NULL
130                 };
131
132                 my_type = g_type_register_static (G_TYPE_OBJECT,
133                                                   "ModestProtocolRegistry",
134                                                   &my_info, 0);
135         }
136         return my_type;
137 }
138
139 static void
140 modest_protocol_registry_class_init (ModestProtocolRegistryClass *klass)
141 {
142         GObjectClass *object_class;
143         object_class = (GObjectClass *) klass;
144
145         parent_class = g_type_class_peek_parent (klass);
146         object_class->finalize = modest_protocol_registry_finalize;
147         g_type_class_add_private (object_class,
148                                   sizeof(ModestProtocolRegistryPrivate));
149 }
150
151 static void
152 modest_protocol_registry_instance_init (ModestProtocolRegistry *obj)
153 {
154         ModestProtocolRegistryPrivate *priv;
155
156         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (obj);
157
158         priv->tags_table = g_hash_table_new_full (g_str_hash, g_str_equal, 
159                                                   g_free, (GDestroyNotify) g_hash_table_unref);
160         priv->priorities = g_hash_table_new (g_direct_hash, g_direct_equal);
161         
162         modest_protocol_registry_create_tag (obj, TAG_ALL_PROTOCOLS);
163 }
164
165 static void   
166 modest_protocol_registry_finalize   (GObject *obj)
167 {
168         ModestProtocolRegistryPrivate *priv;
169
170         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (obj);
171
172         /* Free hash tables */
173         if (priv->tags_table)
174                 g_hash_table_unref (priv->tags_table);
175         if (priv->priorities)
176                 g_hash_table_unref (priv->priorities);
177
178         G_OBJECT_CLASS (parent_class)->finalize (obj);
179 }
180
181
182 ModestProtocolRegistry*
183 modest_protocol_registry_new ()
184 {
185         return g_object_new (MODEST_TYPE_PROTOCOL_REGISTRY, NULL);
186 }
187
188 void
189 modest_protocol_registry_add (ModestProtocolRegistry *self, ModestProtocol *protocol, gint priority, const gchar *first_tag, ...)
190 {
191         va_list list;
192         GSList *tags_list = NULL, *node = NULL;
193         const gchar *va_string;
194         GHashTable *tag_table;
195         ModestProtocolRegistryPrivate *priv;
196
197         g_return_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self));
198         g_return_if_fail (MODEST_IS_PROTOCOL (protocol));
199         g_return_if_fail (first_tag != NULL);
200         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
201
202         tag_table = g_hash_table_lookup (priv->tags_table, TAG_ALL_PROTOCOLS);
203         g_hash_table_insert (tag_table, GINT_TO_POINTER (modest_protocol_get_type_id (protocol)), g_object_ref (protocol));
204
205         g_hash_table_insert (priv->priorities, GINT_TO_POINTER (modest_protocol_get_type_id (protocol)), GINT_TO_POINTER (priority));
206
207         tags_list = g_slist_prepend (tags_list, (gpointer) first_tag);
208         va_start (list, first_tag);
209         while ((va_string = va_arg (list, const gchar *)) != NULL) {
210                 tags_list = g_slist_prepend (tags_list, (gpointer) va_string);
211         }
212         va_end (list);
213
214         for (node = tags_list; node != NULL; node = g_slist_next (node)) {
215
216                 tag_table = g_hash_table_lookup (priv->tags_table, node->data);
217                 if (tag_table == NULL)
218                         tag_table = modest_protocol_registry_create_tag (self, node->data);
219                 g_hash_table_insert (tag_table, GINT_TO_POINTER (modest_protocol_get_type_id (protocol)), g_object_ref (protocol));
220         }
221         g_slist_free (tags_list);
222 }
223
224 GSList *
225 modest_protocol_registry_get_all (ModestProtocolRegistry *self)
226 {
227         ModestProtocolRegistryPrivate *priv;
228
229         g_return_val_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self), NULL);
230         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
231
232         return modest_protocol_registry_get_by_tag (self, TAG_ALL_PROTOCOLS);
233         
234 }
235
236 static void
237 add_protocol_to_list (ModestProtocolType key, ModestProtocol *protocol, GSList **list)
238 {
239         *list = g_slist_prepend (*list, protocol);
240 }
241
242 static gint
243 compare_protocols (ModestProtocol *a, ModestProtocol *b, GHashTable *priorities)
244 {
245         ModestProtocolType a_type, b_type;
246         gint result;
247
248         a_type = modest_protocol_get_type_id (a);
249         b_type = modest_protocol_get_type_id (b);
250
251         result = g_hash_table_lookup (priorities, GINT_TO_POINTER (a_type)) - g_hash_table_lookup (priorities, GINT_TO_POINTER (b_type));
252         if (result == 0) {
253                 const gchar *a_display_name;
254                 const gchar *b_display_name;
255
256                 a_display_name = modest_protocol_get_display_name (a);
257                 b_display_name = modest_protocol_get_display_name (b);
258                 result = g_utf8_collate (a_display_name, b_display_name);
259         }
260         return result;
261 }
262
263 GSList *
264 modest_protocol_registry_get_by_tag (ModestProtocolRegistry *self, const gchar *tag)
265 {
266         ModestProtocolRegistryPrivate *priv;
267         GHashTable *tag_table;
268         GSList *result;
269
270         g_return_val_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self), NULL);
271         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
272
273         tag_table = g_hash_table_lookup (priv->tags_table, tag);
274         if (tag_table == NULL) {
275                 return NULL;
276         }
277
278         result = NULL;
279         g_hash_table_foreach (tag_table, (GHFunc) add_protocol_to_list, &result);
280
281         result = g_slist_sort_with_data (result, (GCompareDataFunc) compare_protocols, priv->priorities);
282
283         return result;
284
285 }
286
287 static void
288 add_protocol_to_pair_list (ModestProtocolType type_id, ModestProtocol *protocol, GSList **list)
289 {
290         *list = g_slist_append (*list,
291                                 (gpointer) modest_pair_new (
292                                         (gpointer) modest_protocol_get_name (protocol),
293                                         (gpointer) modest_protocol_get_display_name (protocol),
294                                         FALSE));
295 }
296
297 ModestPairList *
298 modest_protocol_registry_get_pair_list_by_tag (ModestProtocolRegistry *self, const gchar *tag)
299 {
300         ModestProtocolRegistryPrivate *priv;
301         GHashTable *tag_table;
302         GSList *result = NULL;
303
304         g_return_val_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self), NULL);
305         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
306
307         tag_table = g_hash_table_lookup (priv->tags_table, tag);
308         if (tag_table == NULL) {
309                 return NULL;
310         }
311
312         g_hash_table_foreach (tag_table, (GHFunc) add_protocol_to_pair_list, &result);
313
314         return result;
315 }
316
317 static gboolean
318 find_protocol_by_name (ModestProtocolType type_id,
319                        ModestProtocol *protocol,
320                        const gchar *name)
321 {
322         return (strcmp (name, modest_protocol_get_name (protocol)) == 0);
323 }
324
325 ModestProtocol *
326 modest_protocol_registry_get_protocol_by_name (ModestProtocolRegistry *self, 
327                                                const gchar *tag, 
328                                                const gchar *name)
329 {
330         ModestProtocolRegistryPrivate *priv;
331         GHashTable *tag_table;
332
333         g_return_val_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self), NULL);
334         g_return_val_if_fail (tag, NULL);
335         g_return_val_if_fail (name, NULL);
336
337         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
338
339         tag_table = g_hash_table_lookup (priv->tags_table, tag);
340         if (tag_table == NULL) {
341                 return NULL;
342         }
343         
344         return g_hash_table_find (tag_table, (GHRFunc) find_protocol_by_name, (gpointer) name);
345 }
346
347 ModestProtocol *
348 modest_protocol_registry_get_protocol_by_type (ModestProtocolRegistry *self, ModestProtocolType type_id)
349 {
350         ModestProtocolRegistryPrivate *priv;
351         GHashTable *tag_table;
352
353         g_return_val_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self), NULL);
354         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
355
356         tag_table = g_hash_table_lookup (priv->tags_table, TAG_ALL_PROTOCOLS);
357         if (tag_table == NULL) {
358                 return NULL;
359         }
360         
361         return g_hash_table_lookup (tag_table, GINT_TO_POINTER (type_id));
362 }
363
364 gboolean 
365 modest_protocol_registry_protocol_type_has_tag (ModestProtocolRegistry *self, ModestProtocolType type_id, const gchar *tag)
366 {
367         ModestProtocolRegistryPrivate *priv;
368         GHashTable *tag_table;
369
370         g_return_val_if_fail (MODEST_IS_PROTOCOL_REGISTRY (self), FALSE);
371         g_return_val_if_fail (tag != NULL, FALSE);
372         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
373
374         tag_table = g_hash_table_lookup (priv->tags_table, tag);
375         if (tag_table == NULL) {
376                 return FALSE;
377         }
378         
379         return (g_hash_table_lookup (tag_table, GINT_TO_POINTER (type_id))!= NULL);
380         
381 }
382
383 static GHashTable *
384 modest_protocol_registry_create_tag (ModestProtocolRegistry *self, const gchar *tag)
385 {
386         ModestProtocolRegistryPrivate *priv;
387         GHashTable *tag_table;
388
389         g_assert (tag != NULL);
390         priv = MODEST_PROTOCOL_REGISTRY_GET_PRIVATE (self);
391         tag_table = g_hash_table_new_full (g_direct_hash, g_direct_equal, 
392                                            NULL, g_object_unref);
393         g_hash_table_insert (priv->tags_table, g_strdup (tag), tag_table);
394
395         return tag_table;
396 }
397
398 static gchar * 
399 translation_is_userdata (gpointer userdata, va_list args)
400 {
401         va_list dest;
402         gchar *result;
403
404         G_VA_COPY (dest, args);
405         result = g_strdup_vprintf (_(userdata), dest);
406         va_end (dest);
407
408         return result;
409 }
410
411 static gchar * 
412 translation_is_userdata_no_param (gpointer userdata, va_list args)
413 {
414         gchar *result;
415
416         result = g_strdup (_(userdata));
417
418         return result;
419 }
420
421
422 void 
423 modest_protocol_registry_set_to_default (ModestProtocolRegistry *self)
424 {
425         ModestProtocol *protocol;
426         TnyList *account_options;
427         TnyPair *pair;
428
429         protocol = modest_account_protocol_new ("sendmail", N_("Sendmail"),
430                                                 0, 0,
431                                                 TNY_TYPE_CAMEL_TRANSPORT_ACCOUNT);
432         sendmail_protocol_type_id = modest_protocol_get_type_id (protocol);
433         modest_protocol_registry_add (self, protocol, 1,
434                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
435                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
436                                       NULL);
437         g_object_unref (protocol);
438
439         protocol = modest_account_protocol_new ("smtp", N_("SMTP Server"),
440                                                 25, 465,
441                                                 MODEST_TYPE_TRANSPORT_ACCOUNT_DECORATOR);
442         smtp_protocol_type_id = modest_protocol_get_type_id (protocol);
443         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_CONNECT_ERROR, translation_is_userdata, "emev_ib_ui_smtp_server_invalid", NULL);
444         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_ACCOUNT_CONNECTION_ERROR, translation_is_userdata, "emev_ib_ui_smtp_server_invalid", NULL);
445         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_AUTH_ERROR, translation_is_userdata, "emev_ni_ui_smtp_authentication_fail_error", NULL);
446         modest_protocol_registry_add (self, protocol, 2,
447                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
448                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
449                                       NULL);
450         g_object_unref (protocol);
451
452         protocol = modest_account_protocol_new ("pop", _("mail_fi_emailtype_pop3"),
453                                                 110, 995,
454                                                 TNY_TYPE_CAMEL_POP_STORE_ACCOUNT);
455         pop_protocol_type_id = modest_protocol_get_type_id (protocol);
456         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_CONNECT_ERROR, translation_is_userdata, "emev_ni_ui_pop3_msg_connect_error", NULL);
457         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_AUTH_ERROR, translation_is_userdata, "emev_ni_ui_pop3_msg_connect_error", NULL);
458         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_ACCOUNT_CONNECTION_ERROR, translation_is_userdata, "emev_ni_ui_pop3_msg_connect_error", NULL);
459         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE, translation_is_userdata_no_param, "emev_ni_ui_pop3_msg_recv_error", NULL);
460         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE_LOST_HEADER, translation_is_userdata, "emev_ni_ui_pop3_msg_recv_error", NULL);
461         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_SSL_PROTO_NAME, translation_is_userdata_no_param, "mcen_fi_advsetup_other_security_securepop3s", NULL);
462         modest_protocol_registry_add (self, protocol, 3,
463                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
464                                       MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,
465                                       MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS,
466                                       MODEST_PROTOCOL_REGISTRY_HAS_LEAVE_ON_SERVER_PROTOCOLS,
467                                       MODEST_PROTOCOL_REGISTRY_STORE_FORBID_INCOMING_XFERS,
468                                       MODEST_PROTOCOL_REGISTRY_STORE_LIMIT_HEADER_WINDOW,
469                                       NULL);
470         g_object_unref (protocol);
471
472         protocol = modest_account_protocol_new ("imap", _("mail_fi_emailtype_imap"),
473                                                 143, 993,
474                                                 TNY_TYPE_CAMEL_IMAP_STORE_ACCOUNT);
475         imap_protocol_type_id = modest_protocol_get_type_id (protocol);
476         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_DELETE_MAILBOX, translation_is_userdata, "emev_nc_delete_mailboximap", NULL);
477         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_CONNECT_ERROR, translation_is_userdata, "emev_ni_ui_imap_connect_server_error", NULL);
478         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_AUTH_ERROR, translation_is_userdata, "emev_ni_ui_imap_connect_server_error", NULL);
479         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_ACCOUNT_CONNECTION_ERROR, translation_is_userdata, "emev_ni_ui_imap_connect_server_error", NULL);
480         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE, translation_is_userdata, "emev_ni_ui_imap_message_not_available_in_server", NULL);
481         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_MSG_NOT_AVAILABLE_LOST_HEADER, translation_is_userdata, "emev_ni_ui_pop3_msg_recv_error", NULL);
482         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_SSL_PROTO_NAME, translation_is_userdata_no_param, "mcen_fi_advsetup_other_security_secureimap4s", NULL);
483         account_options = tny_simple_list_new ();
484         pair = tny_pair_new (MODEST_ACCOUNT_OPTION_USE_LSUB, "");
485         tny_list_append (account_options, G_OBJECT (pair));
486         g_object_unref (pair);
487         pair = tny_pair_new (MODEST_ACCOUNT_OPTION_CHECK_ALL, "");
488         tny_list_append (account_options, G_OBJECT (pair));
489         g_object_unref (pair);
490         modest_account_protocol_set_account_options (MODEST_ACCOUNT_PROTOCOL (protocol), account_options);
491         g_object_unref (account_options);
492         modest_protocol_registry_add (self, protocol, 4,
493                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
494                                       MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,
495                                       MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS,
496                                       MODEST_PROTOCOL_REGISTRY_STORE_HAS_FOLDERS,
497                                       MODEST_PROTOCOL_REGISTRY_STORE_LIMIT_HEADER_WINDOW,
498                                       NULL);
499         g_object_unref (protocol);
500
501         protocol = modest_account_protocol_new ("maildir", N_("Maildir"),
502                                                 0, 0,
503                                                 TNY_TYPE_CAMEL_STORE_ACCOUNT);
504         maildir_protocol_type_id = modest_protocol_get_type_id (protocol);
505         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_ACCOUNT_CONNECTION_ERROR, 
506                                          translation_is_userdata_no_param, "emev_nc_mailbox_notavailable", NULL);
507         modest_protocol_registry_add (self, protocol, 5, 
508                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
509                                       MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,
510                                       MODEST_PROTOCOL_REGISTRY_LOCAL_STORE_PROTOCOLS,
511                                       MODEST_PROTOCOL_REGISTRY_STORE_HAS_FOLDERS,
512                                       NULL);
513         g_object_unref (protocol);
514
515         protocol = modest_account_protocol_new ("mbox", N_("MBox"),
516                                                 0, 0,
517                                                 TNY_TYPE_CAMEL_STORE_ACCOUNT);
518         mbox_protocol_type_id = modest_protocol_get_type_id (protocol);
519         modest_protocol_set_translation (protocol, MODEST_PROTOCOL_TRANSLATION_ACCOUNT_CONNECTION_ERROR, 
520                                          translation_is_userdata_no_param, "emev_nc_mailbox_notavailable", NULL);
521         modest_protocol_registry_add (self, protocol, 6,
522                                       MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
523                                       MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,
524                                       MODEST_PROTOCOL_REGISTRY_LOCAL_STORE_PROTOCOLS,
525                                       MODEST_PROTOCOL_REGISTRY_STORE_HAS_FOLDERS,
526                                       NULL);
527         g_object_unref (protocol);
528
529         protocol = modest_protocol_new ("none", N_("None"));
530         modest_protocol_set (protocol, MODEST_PROTOCOL_SECURITY_ACCOUNT_OPTION, MODEST_ACCOUNT_OPTION_SSL_NEVER);
531         none_connection_protocol_type_id = modest_protocol_get_type_id (protocol);
532         modest_protocol_registry_add (self, protocol, 7,
533                                       MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
534                                       NULL);
535         g_object_unref (protocol);
536
537         protocol = modest_protocol_new ("ssl", N_("SSL"));
538         modest_protocol_set (protocol, MODEST_PROTOCOL_SECURITY_ACCOUNT_OPTION, MODEST_ACCOUNT_OPTION_SSL_WRAPPED);
539         ssl_connection_protocol_type_id = modest_protocol_get_type_id (protocol);
540         modest_protocol_registry_add (self, protocol, 8,
541                                       MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
542                                       MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS,
543                                       MODEST_PROTOCOL_REGISTRY_USE_ALTERNATE_PORT,
544                                       NULL);
545         g_object_unref (protocol);
546
547         protocol = modest_protocol_new ("tls", N_("TLS"));
548         modest_protocol_set (protocol, MODEST_PROTOCOL_SECURITY_ACCOUNT_OPTION, MODEST_ACCOUNT_OPTION_SSL_TLS);
549         tls_connection_protocol_type_id = modest_protocol_get_type_id (protocol);
550         modest_protocol_registry_add (self, protocol, 9, 
551                                       MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
552                                       MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS,
553                                       NULL);
554         g_object_unref (protocol);
555
556         protocol = modest_protocol_new ("tls-op", N_("TLS when possible"));
557         modest_protocol_set (protocol, MODEST_PROTOCOL_SECURITY_ACCOUNT_OPTION, MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE);
558         tlsop_connection_protocol_type_id = modest_protocol_get_type_id (protocol);
559         modest_protocol_registry_add (self, protocol, 10,
560                                       MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
561                                       MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS,
562                                       NULL);
563         g_object_unref (protocol);
564
565         protocol = modest_protocol_new (MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE, _("mcen_fi_advsetup_smtp_none"));
566         none_auth_protocol_type_id = modest_protocol_get_type_id (protocol);
567         modest_protocol_set (protocol, MODEST_PROTOCOL_AUTH_ACCOUNT_OPTION, MODEST_ACCOUNT_AUTH_PLAIN);
568         modest_protocol_registry_add (self, protocol, 11,
569                                       MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS,
570                                       NULL);
571         g_object_unref (protocol);
572
573         protocol = modest_protocol_new (MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD, _("mcen_fi_advsetup_smtp_login"));
574         password_auth_protocol_type_id = modest_protocol_get_type_id (protocol);
575         modest_protocol_set (protocol, MODEST_PROTOCOL_AUTH_ACCOUNT_OPTION, MODEST_ACCOUNT_AUTH_PASSWORD);
576         modest_protocol_registry_add (self, protocol, 12,
577                                       MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS,
578                                       MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS,
579                                       NULL);
580         g_object_unref (protocol);
581
582         protocol = modest_protocol_new (MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5, _("mcen_fi_advsetup_smtp_cram_md5"));
583         crammd5_auth_protocol_type_id = modest_protocol_get_type_id (protocol);
584         modest_protocol_set (protocol, MODEST_PROTOCOL_AUTH_ACCOUNT_OPTION, MODEST_ACCOUNT_AUTH_CRAMMD5);
585         modest_protocol_registry_add (self, protocol, 13,
586                                       MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS,
587                                       MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS,
588                                       NULL);
589         g_object_unref (protocol);
590
591         /* set the custom auth mechs. We do this after creating all the protocols, because if we don't, then we
592          * won't register the auth protocol type id's properly */
593
594         /* IMAP and POP need at least a password,
595          * which camel uses if we specify NULL.
596          * Camel use a password for IMAP or POP if we specify NULL,
597          * For IMAP, at least it will report an error if we use "Password", "Login" or "Plain".
598          * (POP is know to report an error for Login too. Probably Password and Plain too.) */
599         protocol = modest_protocol_registry_get_protocol_by_type (self, MODEST_PROTOCOLS_STORE_IMAP);
600         modest_account_protocol_set_custom_secure_auth_mech (MODEST_ACCOUNT_PROTOCOL (protocol), MODEST_PROTOCOLS_AUTH_NONE, NULL);
601         modest_account_protocol_set_custom_secure_auth_mech (MODEST_ACCOUNT_PROTOCOL (protocol), MODEST_PROTOCOLS_AUTH_PASSWORD, NULL);
602         protocol = modest_protocol_registry_get_protocol_by_type (self, MODEST_PROTOCOLS_STORE_POP);
603         modest_account_protocol_set_custom_secure_auth_mech (MODEST_ACCOUNT_PROTOCOL (protocol), MODEST_PROTOCOLS_AUTH_NONE, NULL);
604         modest_account_protocol_set_custom_secure_auth_mech (MODEST_ACCOUNT_PROTOCOL (protocol), MODEST_PROTOCOLS_AUTH_PASSWORD, NULL);
605 }
606
607 ModestProtocolType
608 modest_protocol_registry_get_imap_type_id (void)
609 {
610         return imap_protocol_type_id;
611 }
612
613 ModestProtocolType 
614 modest_protocol_registry_get_pop_type_id (void)
615 {
616         return pop_protocol_type_id;
617 }
618
619 ModestProtocolType
620 modest_protocol_registry_get_maildir_type_id (void)
621 {
622         return maildir_protocol_type_id;
623 }
624
625 ModestProtocolType
626 modest_protocol_registry_get_mbox_type_id (void)
627 {
628         return mbox_protocol_type_id;
629 }
630
631 ModestProtocolType 
632 modest_protocol_registry_get_smtp_type_id (void)
633 {
634         return smtp_protocol_type_id;
635 }
636
637 ModestProtocolType 
638 modest_protocol_registry_get_sendmail_type_id (void)
639 {
640         return sendmail_protocol_type_id;
641 }
642
643 ModestProtocolType 
644 modest_protocol_registry_get_none_connection_type_id (void)
645 {
646         return none_connection_protocol_type_id;
647 }
648
649 ModestProtocolType 
650 modest_protocol_registry_get_ssl_connection_type_id (void)
651 {
652         return ssl_connection_protocol_type_id;
653 }
654
655 ModestProtocolType 
656 modest_protocol_registry_get_tls_connection_type_id (void)
657 {
658         return tls_connection_protocol_type_id;
659 }
660
661 ModestProtocolType 
662 modest_protocol_registry_get_tlsop_connection_type_id (void)
663 {
664         return tlsop_connection_protocol_type_id;
665 }
666
667 ModestProtocolType 
668 modest_protocol_registry_get_none_auth_type_id (void)
669 {
670         return none_auth_protocol_type_id;
671 }
672
673 ModestProtocolType 
674 modest_protocol_registry_get_password_auth_type_id (void)
675 {
676         return password_auth_protocol_type_id;
677 }
678
679 ModestProtocolType 
680 modest_protocol_registry_get_crammd5_auth_type_id (void)
681 {
682         return crammd5_auth_protocol_type_id;
683 }
684