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