8350a21bf2211d14cab270a4c76a454ec3a05159
[modest] / src / modest-account-mgr-helpers.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 <modest-account-mgr-helpers.h>
31 #include <modest-account-mgr-priv.h>
32 #include <tny-simple-list.h>
33 #include <modest-runtime.h>
34 #include <modest-defs.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <modest-account-protocol.h>
38 #include <modest-utils.h>
39
40 static const gchar * null_means_empty (const gchar * str);
41
42 static const gchar *
43 null_means_empty (const gchar * str)
44 {
45         return str ? str : "";
46 }
47
48 gboolean
49 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
50                                         gboolean enabled)
51 {
52         return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
53 }
54
55
56 gboolean
57 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
58 {
59         return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
60 }
61
62 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name, 
63         const gchar* signature, gboolean use_signature)
64 {
65         gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, 
66                 use_signature, FALSE);
67         result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE, 
68                                                           null_means_empty (signature), FALSE);
69         return result;
70 }
71
72 gchar* 
73 modest_account_mgr_get_signature (ModestAccountMgr *self, 
74                                   const gchar* name, 
75                                   gboolean* use_signature)
76 {
77         *use_signature = 
78                 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
79         
80         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
81 }
82
83 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
84 {
85        ModestProtocolType result = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
86        gchar *server_account_name = modest_account_mgr_get_string (self, name,
87                                                                    MODEST_ACCOUNT_STORE_ACCOUNT,
88                                                                    FALSE);
89        if (server_account_name) {
90                ModestServerAccountSettings* server_settings =
91                        modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
92
93                /* Easy setup wizard precreates accounts without
94                   settings so this could happen */
95                if (server_settings) {
96                        result = modest_server_account_settings_get_protocol (server_settings);
97                        g_object_unref (server_settings);
98                }
99                g_free (server_account_name);
100        }
101
102        return result;
103 }
104
105
106 gboolean 
107 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self, 
108                                                  const gchar* connection_id, 
109                                                  const gchar* server_account_name)
110 {
111         modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
112         
113         ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
114
115         gboolean result = TRUE;
116         GError *err = NULL;
117         GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
118                                                     MODEST_CONF_VALUE_STRING, &err);
119         if (err) {
120                 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
121                 g_error_free (err);
122                 err = NULL;
123                 result = FALSE;
124         } else {        
125                 /* The server account is in the item after the connection name: */
126                 list = g_slist_append (list, g_strdup (connection_id));
127                 list = g_slist_append (list, g_strdup (server_account_name));
128         
129                 /* Reset the changed list: */
130                 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
131                                                     MODEST_CONF_VALUE_STRING, &err);
132                 if (err) {
133                         g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
134                         g_error_free (err);
135                         result = FALSE;
136                 }
137         }
138                                 
139         /* Free the list */
140         if (list) {
141                 g_slist_foreach (list, (GFunc) g_free, NULL);
142                 g_slist_free (list);
143         }
144         
145         return result;
146 }
147
148 /**
149  * modest_account_mgr_remove_connection_specific_smtp
150  * @self: a ModestAccountMgr instance
151  * @name: the account name
152  * @connection_id: A libconic IAP connection id
153  * 
154  * Disassacoiate a server account to use with the specific connection for this account.
155  *
156  * Returns: TRUE if it worked, FALSE otherwise
157  */                              
158 gboolean 
159 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self, 
160                                                     const gchar* connection_id)
161 {
162         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
163         
164         gboolean result = TRUE;
165         GError *err = NULL;
166         GSList *list = modest_conf_get_list (priv->modest_conf, 
167                                              MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
168                                              MODEST_CONF_VALUE_STRING, &err);
169         if (err) {
170                 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
171                 g_error_free (err);
172                 err = NULL;
173                 result = FALSE;
174         }
175
176         if (!list)
177                 return FALSE;
178                 
179         /* The server account is in the item after the connection name: */
180         GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
181         if (list_connection) {
182                 GSList *account_node = g_slist_next (list_connection);
183                 /* remove both items: */
184                 list = g_slist_delete_link(list, list_connection);
185                 list = g_slist_delete_link(list, account_node);
186         }
187         
188         /* Reset the changed list: */
189         modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
190                                                     MODEST_CONF_VALUE_STRING, &err);
191         if (err) {
192                 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
193                 g_error_free (err);
194                 result = FALSE;
195         }
196                                 
197         /* Free the list */
198         if (list) {
199                 g_slist_foreach (list, (GFunc) g_free, NULL);
200                 g_slist_free (list);
201         }
202         
203         return result;
204 }
205
206
207 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
208 {
209         return modest_account_mgr_get_bool (self, account_name, 
210                 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
211 }
212
213 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name, 
214         gboolean new_value)
215 {
216         return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, 
217                 new_value, FALSE);
218 }
219
220 /**
221  * modest_account_mgr_get_connection_specific_smtp
222  * @self: a ModestAccountMgr instance
223  * @connection_id: A libconic IAP connection id
224  * 
225  * Retrieve a server account to use with this specific connection for this account.
226  *
227  * Returns: a server account name to use for this connection, or NULL if none is specified.
228  */                      
229 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self,  const gchar* connection_id)
230 {
231         gchar *result = NULL;
232         
233         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
234         
235         GError *err = NULL;
236         GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
237                                                     MODEST_CONF_VALUE_STRING, &err);
238         if (err) {
239                 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
240                 g_error_free (err);
241                 err = NULL;
242         }
243
244         if (!list)
245                 return NULL;
246
247         /* The server account is in the item after the connection name: */
248         GSList *iter = list;
249         while (iter) {
250                 const gchar* this_connection_id = (const gchar*)(iter->data);
251                 if (strcmp (this_connection_id, connection_id) == 0) {
252                         iter = g_slist_next (iter);
253                         
254                         if (iter) {
255                                 const gchar* account_name = (const gchar*)(iter->data);
256                                 if (account_name) {
257                                         result = g_strdup (account_name);
258                                         break;
259                                 }
260                         }
261                 }
262                 
263                 /* Skip 2 to go to the next connection in the list: */
264                 iter = g_slist_next (iter);
265                 if (iter)
266                         iter = g_slist_next (iter);
267         }
268                 
269         /* Free the list */
270         if (list) {
271                 g_slist_foreach (list, (GFunc) g_free, NULL);
272                 g_slist_free (list);
273         }
274         
275         return result;
276 }
277                                          
278 gchar*
279 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
280 {
281         return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME, 
282                 TRUE /* server account */);
283 }
284
285 void
286 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name, 
287         const gchar* username)
288 {
289         /* Note that this won't work properly as long as the gconf cache is broken 
290          * in Maemo Bora: */
291         gchar *existing_username = modest_account_mgr_get_server_account_username(self, 
292                 account_name);
293         
294         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME, 
295                 username, TRUE /* server account */);
296                 
297         /* We don't know anything about new usernames: */
298         if (strcmp (existing_username, username) != 0)
299                 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
300                 
301         g_free (existing_username);
302 }
303
304 gboolean
305 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
306 {
307         return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED, 
308                                             TRUE /* server account */);
309 }
310
311 void
312 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self, 
313                                                   const gchar* account_name, 
314                                                   gboolean succeeded)
315 {
316         modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED, 
317                                      succeeded, TRUE /* server account */);
318 }
319
320 gchar*
321 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
322 {
323         return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD, 
324                 TRUE /* server account */);     
325 }
326
327 gboolean
328 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
329 {
330         gboolean result = FALSE;
331         gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD, 
332                 TRUE /* server account */);
333         if (password && strlen (password)) {
334                 result = TRUE;
335         
336                 /* Clean password */
337                 bzero (password, strlen (password));
338         }
339
340         g_free (password);
341         return result;
342 }
343                          
344         
345 gchar*
346 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self, 
347                                                 const gchar* account_name)
348 {
349         return modest_account_mgr_get_string (self, 
350                                               account_name, 
351                                               MODEST_ACCOUNT_HOSTNAME, 
352                                               TRUE /* server account */);
353 }
354  
355 void
356 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self, 
357                                                 const gchar *server_account_name,
358                                                 const gchar *hostname)
359 {
360         modest_account_mgr_set_string (self, 
361                                        server_account_name,
362                                        MODEST_ACCOUNT_HOSTNAME, 
363                                        hostname, 
364                                        TRUE /* server account */);
365 }
366
367
368
369 ModestProtocolType
370 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self, 
371         const gchar* account_name)
372 {
373         ModestProtocolRegistry *protocol_registry;
374         ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
375         gchar* value;
376
377         protocol_registry = modest_runtime_get_protocol_registry ();
378         value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, 
379                                                TRUE /* server account */);
380         if (value) {
381                 ModestProtocol *protocol;
382
383                 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
384                 g_free (value);
385
386                 if (protocol)
387                         result = modest_protocol_get_type_id (protocol);
388                         
389         }
390         
391         return result;
392 }
393
394
395 void
396 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self, 
397         const gchar* account_name, ModestProtocolType secure_auth)
398 {
399         const gchar* str_value;
400         ModestProtocolRegistry *protocol_registry;
401         ModestProtocol *protocol;
402
403         /* Get the conf string for the protocol: */
404         protocol_registry = modest_runtime_get_protocol_registry ();
405         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
406         str_value = modest_protocol_get_name (protocol);
407         
408         /* Set it in the configuration: */
409         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
410 }
411
412 ModestProtocolType
413 modest_account_mgr_get_server_account_security (ModestAccountMgr *self, 
414         const gchar* account_name)
415 {
416         ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
417         gchar* value;
418
419         value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY, 
420                                                TRUE /* server account */);
421         if (value) {
422                 ModestProtocolRegistry *protocol_registry;
423                 ModestProtocol *protocol;
424
425                 protocol_registry = modest_runtime_get_protocol_registry ();
426                 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
427                                                                           MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
428                                                                           value);
429                 g_free (value);
430
431                 if (protocol)
432                         result = modest_protocol_get_type_id (protocol);
433         }
434         
435         return result;
436 }
437
438 void
439 modest_account_mgr_set_server_account_security (ModestAccountMgr *self, 
440                                                 const gchar* account_name, 
441                                                 ModestProtocolType security)
442 {
443         const gchar* str_value;
444         ModestProtocolRegistry *protocol_registry;
445         ModestProtocol *protocol;
446
447         /* Get the conf string for the protocol type: */
448         protocol_registry = modest_runtime_get_protocol_registry ();
449         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
450         str_value = modest_protocol_get_name (protocol);
451         
452         /* Set it in the configuration: */
453         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
454 }
455
456 ModestServerAccountSettings *
457 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
458 {
459         ModestServerAccountSettings *settings = NULL;
460         ModestProtocol *protocol;
461         ModestProtocolRegistry *registry;
462         gchar *hostname, *username, *pwd, *uri, *proto, *auth, *sec;
463
464         if (!modest_account_mgr_account_exists (self, name, TRUE)) {
465                 g_warning ("%s account %s does not exist", __FUNCTION__, name);
466                 return NULL;
467         }
468
469         registry = modest_runtime_get_protocol_registry ();
470         settings = modest_server_account_settings_new ();
471
472         modest_server_account_settings_set_account_name (settings, name);
473
474         proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
475         if (proto) {
476                 gchar *tag = NULL;
477                 if (is_transport_and_not_store) {
478                         tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS;
479                 } else {
480                         tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
481                 }
482                 protocol = modest_protocol_registry_get_protocol_by_name (registry, tag, proto);
483
484                 modest_server_account_settings_set_protocol (settings,
485                                                              modest_protocol_get_type_id (protocol));
486                 g_free (proto);
487         } else {
488                 goto on_error;
489         }
490
491         modest_server_account_settings_set_port (settings,
492                                                  modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
493
494         auth = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
495         if (auth) {
496                 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, auth);
497                 modest_server_account_settings_set_auth_protocol (settings,
498                                                                   modest_protocol_get_type_id (protocol));
499                 g_free (auth);
500         } else {
501                 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
502         }
503
504         sec = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
505         if (sec) {
506                 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, sec);
507                 modest_server_account_settings_set_security_protocol (settings,
508                                                                       modest_protocol_get_type_id (protocol));
509                 g_free (sec);
510         } else {
511                 modest_server_account_settings_set_security_protocol (settings,
512                                                                       MODEST_PROTOCOLS_CONNECTION_NONE);
513         }
514
515         /* Username, password and URI. Note that the URI could include
516            the former two, so in this case there is no need to have
517            them */
518         username = modest_account_mgr_get_string (self, name,
519                                                   MODEST_ACCOUNT_USERNAME,TRUE);
520         if (username)
521                 modest_server_account_settings_set_username (settings, username);
522
523         pwd = modest_account_mgr_get_string (self, name,
524                                              MODEST_ACCOUNT_PASSWORD, TRUE);
525         if (pwd) {
526                 modest_server_account_settings_set_password (settings, pwd);
527                 g_free (pwd);
528         }
529
530         uri = modest_account_mgr_get_string (self, name,
531                                              MODEST_ACCOUNT_URI, TRUE);
532         if (uri)
533                 modest_server_account_settings_set_uri (settings, uri);
534
535         hostname = modest_account_mgr_get_string (self, name,
536                                                   MODEST_ACCOUNT_HOSTNAME,TRUE);
537         if (hostname)
538                 modest_server_account_settings_set_hostname (settings, hostname);
539
540         if (!uri) {
541                 if (!username || !hostname) {
542                         g_free (username);
543                         g_free (hostname);
544                         goto on_error;
545                 }
546         }
547
548         g_free (username);
549         g_free (hostname);
550         g_free (uri);
551
552         return settings;
553
554  on_error:
555         if (settings)
556                 g_object_unref (settings);
557         return NULL;
558 }
559
560 gboolean 
561 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
562                                          ModestServerAccountSettings *settings)
563 {
564         gboolean has_errors = FALSE;
565         const gchar *account_name;
566         const gchar *protocol_name;
567         const gchar *uri;
568         ModestProtocolRegistry *protocol_registry;
569         ModestProtocol *protocol;
570         
571         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
572         protocol_registry = modest_runtime_get_protocol_registry ();
573         account_name = modest_server_account_settings_get_account_name (settings);
574
575         /* if we don't have a valid account name we cannot save */
576         g_return_val_if_fail (account_name, FALSE);
577
578         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
579                                                                   modest_server_account_settings_get_protocol (settings));
580         protocol_name = modest_protocol_get_name (protocol);
581         uri = modest_server_account_settings_get_uri (settings);
582         if (!uri) {
583                 const gchar *hostname;
584                 const gchar *username;
585                 const gchar *password;
586                 gint port;
587                 const gchar *auth_protocol_name;
588                 const gchar *security_name;
589
590                 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
591                 username = null_means_empty (modest_server_account_settings_get_username (settings));
592                 password = null_means_empty (modest_server_account_settings_get_password (settings));
593                 port = modest_server_account_settings_get_port (settings);
594                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
595                                                                           modest_server_account_settings_get_auth_protocol (settings));
596                 auth_protocol_name = modest_protocol_get_name (protocol);
597                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
598                                                                           modest_server_account_settings_get_security_protocol (settings));
599                 security_name = modest_protocol_get_name (protocol);
600
601                 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME, 
602                                                             hostname, TRUE);
603                 if (!has_errors)
604                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
605                                                                            username, TRUE));
606                 if (!has_errors)
607                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
608                                                                            password, TRUE));
609                 if (!has_errors)
610                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
611                                                                            protocol_name, TRUE));
612                 if (!has_errors)
613                         (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
614                                                                         port, TRUE));
615                 if (!has_errors)
616                         (has_errors = !modest_account_mgr_set_string (self, account_name, 
617                                                                            MODEST_ACCOUNT_AUTH_MECH,
618                                                                            auth_protocol_name, TRUE));          
619                 if (!has_errors)
620                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
621                                                                            security_name,
622                                                                            TRUE));
623         } else {
624                 const gchar *uri = modest_server_account_settings_get_uri (settings);
625                 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
626                                                             uri, TRUE);
627                 if (!has_errors)
628                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
629                                                                            protocol_name, TRUE));
630         }
631
632         return !has_errors;
633
634 }
635
636
637 ModestAccountSettings *
638 modest_account_mgr_load_account_settings (ModestAccountMgr *self, 
639                                           const gchar* name)
640 {
641         ModestAccountSettings *settings;
642         gchar *string;
643         gchar *server_account;
644         gchar *default_account;
645         gboolean use_signature = FALSE;
646         
647         g_return_val_if_fail (self, NULL);
648         g_return_val_if_fail (name, NULL);
649         
650         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
651                 /* For instance, maybe you are mistakenly checking for a server account name? */
652                 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
653                 return NULL;
654         }
655         
656         settings = modest_account_settings_new ();
657
658         modest_account_settings_set_account_name (settings, name);
659
660         string = modest_account_mgr_get_string (self, name,
661                                                 MODEST_ACCOUNT_DISPLAY_NAME,
662                                                 FALSE);
663         modest_account_settings_set_display_name (settings, string);
664         g_free (string);
665
666         string = modest_account_mgr_get_string (self, name,
667                                                 MODEST_ACCOUNT_FULLNAME,
668                                                 FALSE);
669         modest_account_settings_set_fullname (settings, string);
670         g_free (string);
671
672         string = modest_account_mgr_get_string (self, name,
673                                                 MODEST_ACCOUNT_EMAIL,
674                                                 FALSE);
675         modest_account_settings_set_email_address (settings, string);
676         g_free (string);
677
678         modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
679         modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
680         modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
681
682         default_account    = modest_account_mgr_get_default_account (self);
683         modest_account_settings_set_is_default (settings,
684                                                 (default_account && strcmp (default_account, name) == 0));
685         g_free (default_account);
686
687         string = modest_account_mgr_get_signature (self, name, &use_signature);
688         modest_account_settings_set_use_signature (settings, use_signature);
689         modest_account_settings_set_signature (settings, string);
690         g_free (string);
691
692         modest_account_settings_set_leave_messages_on_server 
693                 (settings, modest_account_mgr_get_leave_on_server (self, name));
694         modest_account_settings_set_use_connection_specific_smtp 
695                 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
696
697         /* store */
698         server_account     = modest_account_mgr_get_string (self, name,
699                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
700                                                             FALSE);
701         if (server_account) {
702                 ModestServerAccountSettings *store_settings;
703                 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
704                 g_free (server_account);
705
706                 /* It could happen that the account data is corrupted
707                    so it's not loaded properly */
708                 if (store_settings) {
709                         modest_account_settings_set_store_settings (settings,
710                                                                     store_settings);
711                         g_object_unref (store_settings);
712                 } else {
713                         g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
714                         g_object_unref (settings);
715                         return NULL;
716                 }
717         }
718
719         /* transport */
720         server_account = modest_account_mgr_get_string (self, name,
721                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
722                                                         FALSE);
723         if (server_account) {
724                 ModestServerAccountSettings *transport_settings;
725                 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
726                 g_free (server_account);
727
728                 if (transport_settings) {
729                         modest_account_settings_set_transport_settings (settings, transport_settings);
730                         g_object_unref (transport_settings);
731                 } else {
732                         g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
733                         g_object_unref (settings);
734                         return NULL;
735                 }
736         }
737
738         return settings;
739 }
740
741 void
742 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
743                                           ModestAccountSettings *settings)
744 {
745         const gchar *account_name;
746         ModestServerAccountSettings *store_settings;
747         ModestServerAccountSettings *transport_settings;
748
749         g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
750         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
751
752         account_name = modest_account_settings_get_account_name (settings);
753         g_return_if_fail (account_name != NULL);
754
755         modest_account_mgr_set_display_name (mgr, account_name,
756                                              modest_account_settings_get_display_name (settings));
757         modest_account_mgr_set_user_fullname (mgr, account_name,
758                                               modest_account_settings_get_fullname (settings));
759         modest_account_mgr_set_user_email (mgr, account_name,
760                                            modest_account_settings_get_email_address (settings));
761         modest_account_mgr_set_retrieve_type (mgr, account_name,
762                                               modest_account_settings_get_retrieve_type (settings));
763         modest_account_mgr_set_retrieve_limit (mgr, account_name,
764                                                modest_account_settings_get_retrieve_limit (settings));
765         modest_account_mgr_set_leave_on_server (mgr, account_name,
766                                                 modest_account_settings_get_leave_messages_on_server (settings));
767         modest_account_mgr_set_signature (mgr, account_name,
768                                           modest_account_settings_get_signature (settings),
769                                           modest_account_settings_get_use_signature (settings));
770         modest_account_mgr_set_use_connection_specific_smtp 
771                 (mgr, account_name,
772                  modest_account_settings_get_use_connection_specific_smtp (settings));
773
774         store_settings = modest_account_settings_get_store_settings (settings);
775         if (store_settings) {
776                 const gchar *store_account_name;
777                 store_account_name = modest_server_account_settings_get_account_name (store_settings);
778                 if (store_account_name)
779                         modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT, 
780                                                        store_account_name, FALSE);
781                 modest_account_mgr_save_server_settings (mgr, store_settings);
782                 g_object_unref (store_settings);
783         }
784
785         transport_settings = modest_account_settings_get_transport_settings (settings);
786         if (transport_settings) {
787                 const gchar *transport_account_name;
788                 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
789                 if (transport_account_name)
790                         modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, 
791                                                        transport_account_name, FALSE);
792                 modest_account_mgr_save_server_settings (mgr, transport_settings);
793                 g_object_unref (transport_settings);
794         }
795         modest_account_mgr_set_enabled (mgr, account_name, TRUE);
796 }
797
798
799 gint 
800 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
801 {
802         return g_utf8_collate((const gchar*)a, (const gchar*)b);
803 }
804
805 /** Get the first one, alphabetically, by title. */
806 gchar* 
807 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
808 {
809         const gchar* account_name = NULL;
810         GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
811
812         /* Return TRUE if there is no account */
813         if (!account_names)
814                 return NULL;
815
816         /* Get the first one, alphabetically, by title: */
817         /* gchar *old_default = modest_account_mgr_get_default_account (self); */
818         GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
819
820         GSList* iter = list_sorted;
821         gboolean found = FALSE;
822         while (iter && !found) {
823                 account_name = (const gchar*)list_sorted->data;
824
825                 if (account_name)
826                         found = TRUE;
827
828                 if (!found)
829                         iter = g_slist_next (iter);
830         }
831
832         gchar* result = NULL;
833         if (account_name)
834                 result = g_strdup (account_name);
835                 
836         modest_account_mgr_free_account_names (account_names);
837         account_names = NULL;
838
839         return result;
840 }
841
842 gboolean
843 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
844 {
845         gboolean result = FALSE;
846         
847         gchar* account_name = modest_account_mgr_get_first_account_name(self);
848         if (account_name) {
849                 result = modest_account_mgr_set_default_account (self, account_name);
850                 g_free (account_name);
851         }
852         else
853                 result = TRUE; /* If there are no accounts then it's not a failure. */
854
855         return result;
856 }
857
858 gchar*
859 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name, const gchar *mailbox)
860 {
861         gchar *from;
862         gchar *transport_account;
863         gchar *proto;
864         
865         g_return_val_if_fail (self, NULL);
866         g_return_val_if_fail (name, NULL);
867
868         from = NULL;
869
870         transport_account = modest_account_mgr_get_server_account_name (self,
871                                                                         name,
872                                                                         TNY_ACCOUNT_TYPE_TRANSPORT);
873         if (transport_account) {
874                 proto = modest_account_mgr_get_string (self, transport_account, MODEST_ACCOUNT_PROTO, TRUE);
875                 if (proto != NULL) {
876                         ModestProtocol *protocol = 
877                                 modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
878                                                                                MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
879                                                                                proto);
880                         if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
881                                 from = modest_account_protocol_get_from (MODEST_ACCOUNT_PROTOCOL (protocol),
882                                                                          name,
883                                                                          mailbox);
884                         }
885                         g_free (proto);
886                 }
887         }
888
889         if (from == NULL) {
890                 gchar *fullname, *email;
891
892                 fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
893                                                                FALSE);
894                 email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
895                                                                FALSE);
896                 from = g_strdup_printf ("%s <%s>",
897                                         fullname ? fullname : "",
898                                         email    ? email    : "");
899                 g_free (fullname);
900                 g_free (email);
901         }
902
903         return from;
904 }
905
906 /* Add a number to the end of the text, or increment a number that is already there.
907  */
908 static gchar*
909 util_increment_name (const gchar* text)
910 {
911         g_return_val_if_fail (text, NULL);
912
913         /* Get the end character,
914          * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
915          */
916         const gchar* end = NULL;
917         if (!g_utf8_validate (text, -1, &end))
918                 return NULL;
919   
920         if (!end)
921                 return NULL;
922                 
923         --end; /* Go to before the null-termination. */
924                 
925         /* Look at each UTF-8 characer, starting at the end: */
926         const gchar* p = end;
927         const gchar* alpha_end = NULL;
928         while (p)
929         {       
930                 /* Stop when we reach the first character that is not a numeric digit: */
931                 const gunichar ch = g_utf8_get_char (p);
932                 if (!g_unichar_isdigit (ch)) {
933                         alpha_end = p;
934                         break;
935                 }
936                 
937                 p = g_utf8_find_prev_char (text, p);    
938         }
939         
940         if(!alpha_end) {
941                 /* The text must consist completely of numeric digits. */
942                 alpha_end = text;
943         }
944         else
945                 ++alpha_end;
946         
947         /* Intepret and increment the number, if any: */
948         gint num = atol (alpha_end);
949         ++num;
950         
951         /* Get the name part: */
952         gint name_len = alpha_end - text;
953         gchar *name_without_number = g_malloc(name_len + 1);
954         memcpy (name_without_number, text, name_len);
955         name_without_number[name_len] = 0;\
956         
957     /* Concatenate the text part and the new number: */ 
958         gchar *result = g_strdup_printf("%s%d", name_without_number, num);
959         g_free (name_without_number);
960         
961         return result;  
962 }
963
964 gchar*
965 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
966         gboolean server_account)
967 {
968         gchar *account_name = g_strdup (starting_name);
969
970         while (modest_account_mgr_account_exists (self, 
971                 account_name, server_account /*  server_account */)) {
972                         
973                 gchar * account_name2 = util_increment_name (account_name);
974                 g_free (account_name);
975                 account_name = account_name2;
976         }
977         
978         return account_name;
979 }
980
981 gchar*
982 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
983 {
984         gchar *account_name = g_strdup (starting_name);
985
986         while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
987                         
988                 gchar * account_name2 = util_increment_name (account_name);
989                 g_free (account_name);
990                 account_name = account_name2;
991         }
992         
993         return account_name;
994 }
995
996 void 
997 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self, 
998                                         const gchar *account_name, 
999                                         gboolean leave_on_server)
1000 {
1001         modest_account_mgr_set_bool (self, 
1002                                      account_name,
1003                                      MODEST_ACCOUNT_LEAVE_ON_SERVER, 
1004                                      leave_on_server, 
1005                                      FALSE);
1006 }
1007
1008 gboolean 
1009 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self, 
1010                                         const gchar* account_name)
1011 {
1012         return modest_account_mgr_get_bool (self, 
1013                                             account_name,
1014                                             MODEST_ACCOUNT_LEAVE_ON_SERVER, 
1015                                             FALSE);
1016 }
1017
1018 gint 
1019 modest_account_mgr_get_last_updated (ModestAccountMgr *self, 
1020                                      const gchar* account_name)
1021 {
1022         return modest_account_mgr_get_int (modest_runtime_get_account_mgr (), 
1023                                            account_name, 
1024                                            MODEST_ACCOUNT_LAST_UPDATED, 
1025                                            TRUE);
1026 }
1027
1028 void 
1029 modest_account_mgr_set_last_updated (ModestAccountMgr *self, 
1030                                      const gchar* account_name,
1031                                      gint time)
1032 {
1033         modest_account_mgr_set_int (self, 
1034                                     account_name, 
1035                                     MODEST_ACCOUNT_LAST_UPDATED, 
1036                                     time, 
1037                                     TRUE);
1038
1039         /* TODO: notify about changes */
1040 }
1041
1042 gint  
1043 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self, 
1044                                        const gchar* account_name)
1045 {
1046         return modest_account_mgr_get_int (self, 
1047                                            account_name,
1048                                            MODEST_ACCOUNT_LIMIT_RETRIEVE, 
1049                                            FALSE);
1050 }
1051
1052 void  
1053 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self, 
1054                                        const gchar* account_name,
1055                                        gint limit_retrieve)
1056 {
1057         modest_account_mgr_set_int (self, 
1058                                     account_name,
1059                                     MODEST_ACCOUNT_LIMIT_RETRIEVE, 
1060                                     limit_retrieve, 
1061                                     FALSE /* not server account */);
1062 }
1063
1064 gint  
1065 modest_account_mgr_get_server_account_port (ModestAccountMgr *self, 
1066                                             const gchar* account_name)
1067 {
1068         return modest_account_mgr_get_int (self, 
1069                                            account_name,
1070                                            MODEST_ACCOUNT_PORT, 
1071                                            TRUE);
1072 }
1073
1074 void
1075 modest_account_mgr_set_server_account_port (ModestAccountMgr *self, 
1076                                             const gchar *account_name,
1077                                             gint port_num)
1078 {
1079         modest_account_mgr_set_int (self, 
1080                                     account_name,
1081                                     MODEST_ACCOUNT_PORT, 
1082                                     port_num, TRUE /* server account */);
1083 }
1084
1085 gchar* 
1086 modest_account_mgr_get_server_account_name (ModestAccountMgr *self, 
1087                                             const gchar *account_name,
1088                                             TnyAccountType account_type)
1089 {
1090         return modest_account_mgr_get_string (self, 
1091                                               account_name,
1092                                               (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1093                                               MODEST_ACCOUNT_STORE_ACCOUNT :
1094                                               MODEST_ACCOUNT_TRANSPORT_ACCOUNT, 
1095                                               FALSE);
1096 }
1097
1098 static const gchar *
1099 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1100 {
1101         switch(retrieve_type) {
1102         case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1103                 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1104                 break;
1105         case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1106                 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1107                 break;
1108         case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1109                 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1110                 break;
1111         default:
1112                 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1113         };
1114 }
1115
1116 static ModestAccountRetrieveType
1117 get_retrieve_type (const gchar *name)
1118 {
1119         if (!name || name[0] == 0)
1120                 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1121         if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1122                 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1123         } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1124                 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1125         } else {
1126                 /* we fall back to headers only */
1127                 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1128         }
1129 }
1130
1131 ModestAccountRetrieveType
1132 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self, 
1133                                       const gchar *account_name)
1134 {
1135         gchar *string;
1136         ModestAccountRetrieveType result;
1137
1138         string =  modest_account_mgr_get_string (self, 
1139                                                  account_name,
1140                                                  MODEST_ACCOUNT_RETRIEVE, 
1141                                                  FALSE /* not server account */);
1142         result = get_retrieve_type (string);
1143         g_free (string);
1144
1145         return result;
1146 }
1147
1148 void 
1149 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self, 
1150                                       const gchar *account_name,
1151                                       ModestAccountRetrieveType retrieve_type)
1152 {
1153         modest_account_mgr_set_string (self, 
1154                                        account_name,
1155                                        MODEST_ACCOUNT_RETRIEVE, 
1156                                        get_retrieve_type_name (retrieve_type), 
1157                                        FALSE /* not server account */);
1158 }
1159
1160
1161 void
1162 modest_account_mgr_set_user_fullname (ModestAccountMgr *self, 
1163                                       const gchar *account_name,
1164                                       const gchar *fullname)
1165 {
1166         modest_account_mgr_set_string (self, 
1167                                        account_name,
1168                                        MODEST_ACCOUNT_FULLNAME, 
1169                                        fullname, 
1170                                        FALSE /* not server account */);
1171 }
1172
1173 void
1174 modest_account_mgr_set_user_email (ModestAccountMgr *self, 
1175                                    const gchar *account_name,
1176                                    const gchar *email)
1177 {
1178         modest_account_mgr_set_string (self, 
1179                                        account_name,
1180                                        MODEST_ACCOUNT_EMAIL, 
1181                                        email, 
1182                                        FALSE /* not server account */);
1183 }
1184
1185 gboolean 
1186 modest_account_mgr_account_is_multimailbox (ModestAccountMgr *mgr,
1187                                             const gchar *account_name, 
1188                                             ModestProtocol **mmb_protocol)
1189 {
1190         gchar *transport_account;
1191         gboolean result = FALSE;
1192
1193         if (mmb_protocol)
1194                 *mmb_protocol = NULL;
1195
1196         transport_account = modest_account_mgr_get_server_account_name (mgr,
1197                                                                         account_name,
1198                                                                         TNY_ACCOUNT_TYPE_TRANSPORT);
1199         if (transport_account) {
1200                 gchar *proto;
1201                 ModestProtocolRegistry *registry;
1202
1203                 registry = modest_runtime_get_protocol_registry ();
1204
1205                 proto = modest_account_mgr_get_string (mgr, transport_account, 
1206                                                        MODEST_ACCOUNT_PROTO, TRUE);
1207                 if (proto != NULL) {
1208                         ModestProtocol *protocol = 
1209                                 modest_protocol_registry_get_protocol_by_name (registry,
1210                                                                                MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
1211                                                                                proto);
1212                         if (protocol &&
1213                             modest_protocol_registry_protocol_type_has_tag 
1214                             (registry,
1215                              modest_protocol_get_type_id (protocol),
1216                              MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS)) {
1217                                 if (mmb_protocol)
1218                                         *mmb_protocol = protocol;
1219                                 result = TRUE;
1220                         }
1221                         
1222                 }
1223         }
1224
1225         return result;
1226 }
1227
1228 gchar *
1229 modest_account_mgr_get_signature_from_recipient (ModestAccountMgr *mgr, 
1230                                                  const gchar *current_recipient, 
1231                                                  gboolean *has_signature)
1232 {
1233         gchar *result = NULL;
1234         gchar *mailbox = NULL;
1235         gchar *account_name;
1236         ModestProtocol *protocol = NULL;
1237
1238         *has_signature = FALSE;
1239
1240         account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
1241         if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
1242                 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1243                         result = modest_account_protocol_get_signature (MODEST_ACCOUNT_PROTOCOL (protocol),
1244                                                                         account_name, mailbox, 
1245                                                                         has_signature);
1246                 }
1247         }
1248
1249         if (result == NULL) {
1250                 result = modest_account_mgr_get_signature (mgr, 
1251                                                            account_name, has_signature);
1252         }
1253         return result;
1254 }
1255
1256 void
1257 modest_account_mgr_get_branding_from_recipient (ModestAccountMgr *mgr, 
1258                                                 const gchar *current_recipient, 
1259                                                 gchar **service_name,
1260                                                 const GdkPixbuf **service_icon,
1261                                                 guint64 icon_size)
1262 {
1263         gchar *mailbox = NULL;
1264         gchar *account_name;
1265         ModestProtocol *protocol = NULL;
1266
1267         *service_name = NULL;
1268         *service_icon = NULL;
1269
1270         account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
1271         if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
1272                 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1273                         gchar *name;
1274                         const GdkPixbuf *icon;
1275                         name = modest_account_protocol_get_service_name (MODEST_ACCOUNT_PROTOCOL (protocol),
1276                                                                          account_name, mailbox);
1277                         icon = modest_account_protocol_get_service_icon (MODEST_ACCOUNT_PROTOCOL (protocol),
1278                                                                          account_name, mailbox, icon_size);
1279                         *service_name = name;
1280                         *service_icon = icon;
1281                 }
1282         }
1283
1284 }
1285