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