* add a missing 'break' in the switch statement in
[modest] / src / modest-tny-account.c
index 641f076..cb435a4 100644 (file)
@@ -37,6 +37,7 @@
 #include <tny-camel-transport-account.h>
 #include <tny-camel-imap-store-account.h>
 #include <tny-camel-pop-store-account.h>
+#include <tny-folder-stats.h>
 
 /* for now, ignore the account ===> the special folders are the same,
  * local folders for all accounts
@@ -81,10 +82,43 @@ modest_tny_account_get_special_folder (TnyAccount *account,
        }
        g_object_unref (G_OBJECT (folders));
        g_object_unref (G_OBJECT (iter));
+       g_object_unref (G_OBJECT (local_account));
 
        return special_folder;
 }
 
+/* Camel options: */
+
+/* These seem to be listed in 
+ * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c 
+ */
+#define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
+#define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
+#define MODEST_ACCOUNT_OPTION_SSL_ALWAYS "always"
+#define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
+
+/* These seem to be listed in 
+ * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c 
+ */
+#define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
+#define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
+
+
+/* Posssible values for tny_account_set_secure_auth_mech().
+ * These might be camel-specific.
+ * Really, tinymail should use an enum.
+ * camel_sasl_authtype() seems to list some possible values.
+ */
+/* Note that evolution does not offer these for IMAP: */
+#define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
+#define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
+
+/* Caeml's IMAP uses NULL instead for "Password".
+ * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
+#define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN" 
+#define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
+
 
 /**
  * modest_tny_account_new_from_server_account:
@@ -100,19 +134,19 @@ modest_tny_account_get_special_folder (TnyAccount *account,
 static TnyAccount*
 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
                                            ModestServerAccountData *account_data)
-{
-       TnyAccount *tny_account;
-               
+{      
        g_return_val_if_fail (account_mgr, NULL);
        g_return_val_if_fail (account_data, NULL);
 
        /* sanity checks */
-       if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
+       if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
                g_printerr ("modest: '%s' does not provide a protocol\n",
                            account_data->account_name);
                return NULL;
        }
 
+       TnyAccount *tny_account = NULL;
+       
        switch (account_data->proto) {
        case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
        case MODEST_PROTOCOL_TRANSPORT_SMTP:
@@ -135,24 +169,98 @@ modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
        tny_account_set_id (tny_account, account_data->account_name);
 
        /* Proto */
-       tny_account_set_proto (tny_account,
-                              modest_protocol_info_get_protocol_name(account_data->proto));
+       const gchar* proto_name =
+               modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
+       tny_account_set_proto (tny_account, proto_name);
+
+              
+       /* mbox and maildir accounts use a URI instead of the rest: */
        if (account_data->uri) 
                tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
        else {
-               if (account_data->options) {
-                       GSList *options = account_data->options;
-                       while (options) {
-                               tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
-                                                             options->data);
-                               options = g_slist_next (options);
-                       }
+               /* Set camel-specific options: */
+               
+               /* Enable secure connection settings: */
+               const gchar* option_security = NULL;
+               switch (account_data->security) {
+               case MODEST_PROTOCOL_CONNECTION_NORMAL:
+                       option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER;
+                       break;
+               case MODEST_PROTOCOL_CONNECTION_SSL:
+               case MODEST_PROTOCOL_CONNECTION_TLS:
+                       option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
+                       break;
+               case MODEST_PROTOCOL_CONNECTION_TLS_OP:
+                       option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
+                       break;
+               default:
+                       break;
                }
+               
+               if(option_security) {
+                       tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
+                                                     option_security);
+               }
+               
+               const gchar* auth_mech_name = NULL;
+               switch (account_data->secure_auth) {
+               case MODEST_PROTOCOL_AUTH_NONE:
+                       /* IMAP needs at least a password,
+                        * which camel uses if we specify NULL.
+                        * This setting should never happen anyway. */
+                       if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
+                               auth_mech_name = NULL;
+                       else if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP)
+                               auth_mech_name = MODEST_ACCOUNT_AUTH_ANONYMOUS;
+                       else
+                               auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
+                       break;
+                       
+               case MODEST_PROTOCOL_AUTH_PASSWORD:
+                       /* Camel use a password for IMAP if we specify NULL,
+                        * but will report an error if we use "Password", "Login" or "Plain". */
+                       if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
+                               auth_mech_name = NULL;
+                       else
+                               auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
+                       break;
+                       
+               case MODEST_PROTOCOL_AUTH_CRAMMD5:
+                       auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
+                       break;
+                       
+               default:
+                       g_warning ("%s: Unhandled secure authentication setting %d for "
+                               "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
+                                  account_data->account_name, account_data->hostname);
+                       break;
+               }
+               
+               if(auth_mech_name) {
+                       tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
+               }
+               
+               if (modest_protocol_info_protocol_is_store(account_data->proto)) {
+                       /* Other connection options. Some options are only valid for IMAP
+                          accounts but it's OK for just now since POP is still not
+                          supported */
+                       tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
+                                                                     MODEST_ACCOUNT_OPTION_USE_LSUB);
+                       tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
+                                                                     MODEST_ACCOUNT_OPTION_CHECK_ALL);
+               }
+               
                if (account_data->username) 
                        tny_account_set_user (tny_account, account_data->username);
                if (account_data->hostname)
                        tny_account_set_hostname (tny_account, account_data->hostname);
+                
+               /* Set the port: */
+               if (account_data->port)
+                       tny_account_set_port (tny_account, account_data->port);
        }
+       
+       
        return tny_account;
 }
 
@@ -215,13 +323,13 @@ modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar
        tny_account_set_pass_func (tny_account,
                                   get_pass_func ? get_pass_func: get_pass_dummy);
 
-       /* this name is what shows up in the folder view -- so for some POP/IMAP/... server
-        * account, we set its name to the acount of which it is part */
+       /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
+        * account, we set its name to the account of which it is part. */
        if (account_data->display_name)
                tny_account_set_name (tny_account, account_data->display_name); 
 
        g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
-                               (gpointer*)account_name, g_free);
+                               (gpointer*) g_strdup (account_name), g_free);
        
        modest_account_mgr_free_account_data (account_mgr, account_data);
        return tny_account;
@@ -247,6 +355,8 @@ modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySess
        maildir = modest_local_folder_info_get_maildir_path ();
        url = camel_url_new ("maildir:", NULL);
        camel_url_set_path (url, maildir);
+       /* Needed by tinymail's DBC assertions */
+       camel_url_set_host (url, "localhost");
        url_string = camel_url_to_string (url, 0);
        
        tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
@@ -266,4 +376,111 @@ modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySess
        return TNY_ACCOUNT(tny_account);
 }
 
+typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
+
+typedef struct _RecurseFoldersHelper {
+       TnyStatsFunc function;
+       guint sum;
+       guint folders;
+} RecurseFoldersHelper;
+
+static void
+recurse_folders (TnyFolderStore *store, 
+                TnyFolderStoreQuery *query, 
+                RecurseFoldersHelper *helper)
+{
+       TnyIterator *iter;
+       TnyList *folders = tny_simple_list_new ();
+
+       tny_folder_store_get_folders (store, folders, query, NULL);
+       iter = tny_list_create_iterator (folders);
+
+       helper->folders += tny_list_get_length (folders);
+
+       while (!tny_iterator_is_done (iter)) {
+               TnyFolderStats *stats;
+               TnyFolder *folder;
+
+               folder = TNY_FOLDER (tny_iterator_get_current (iter));
+               stats = tny_folder_get_stats (folder);
+
+               /* initially, we sometimes get -1 from tinymail; ignore that */
+               if (helper->function && helper->function (stats) > 0)
+                       helper->sum += helper->function (stats);
+
+               recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
+           
+               g_object_unref (folder);
+               g_object_unref (stats);
+               tny_iterator_next (iter);
+       }
+        g_object_unref (G_OBJECT (iter));
+        g_object_unref (G_OBJECT (folders));
+}
+
+gint 
+modest_tny_account_get_folder_count (TnyAccount *self)
+{
+       RecurseFoldersHelper *helper;
+       gint retval;
+
+       g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
+
+       /* Create helper */
+       helper = g_malloc0 (sizeof (RecurseFoldersHelper));
+       helper->function = NULL;
+       helper->sum = 0;
+       helper->folders = 0;
+
+       recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
+
+       retval = helper->folders;
+
+       g_free (helper);
+
+       return retval;
+}
+
+gint
+modest_tny_account_get_message_count (TnyAccount *self)
+{
+       RecurseFoldersHelper *helper;
+       gint retval;
+
+       g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
+       
+       /* Create helper */
+       helper = g_malloc0 (sizeof (RecurseFoldersHelper));
+       helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
+       helper->sum = 0;
+
+       recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
+
+       retval = helper->sum;
 
+       g_free (helper);
+
+       return retval;
+}
+
+gint 
+modest_tny_account_get_local_size (TnyAccount *self)
+{
+       RecurseFoldersHelper *helper;
+       gint retval;
+
+       g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
+
+       /* Create helper */
+       helper = g_malloc0 (sizeof (RecurseFoldersHelper));
+       helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
+       helper->sum = 0;
+
+       recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
+
+       retval = helper->sum;
+
+       g_free (helper);
+
+       return retval;
+}