1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <modest-tny-platform-factory.h>
31 #include <modest-tny-account.h>
32 #include <modest-tny-account-store.h>
33 #include <modest-runtime.h>
34 #include <tny-simple-list.h>
35 #include <modest-tny-folder.h>
36 #include <modest-account-mgr-helpers.h>
37 #include <tny-camel-transport-account.h>
38 #include <tny-camel-imap-store-account.h>
39 #include <tny-camel-pop-store-account.h>
40 #include <tny-folder-stats.h>
42 /* for now, ignore the account ===> the special folders are the same,
43 * local folders for all accounts
44 * this might change, ie, IMAP might have server-side sent-items
47 modest_tny_account_get_special_folder (TnyAccount *account,
48 TnyFolderType special_type)
52 TnyFolder *special_folder = NULL;
53 TnyAccount *local_account;
55 g_return_val_if_fail (account, NULL);
56 g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
59 local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
60 MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
62 g_printerr ("modest: cannot get local account\n");
66 folders = TNY_LIST (tny_simple_list_new ());
68 /* no need to do this _async, as these are local folders */
69 tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
71 iter = tny_list_create_iterator (folders);
73 while (!tny_iterator_is_done (iter)) {
75 TNY_FOLDER (tny_iterator_get_current (iter));
76 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
77 special_folder = folder;
80 g_object_unref (G_OBJECT(folder));
81 tny_iterator_next (iter);
83 g_object_unref (G_OBJECT (folders));
84 g_object_unref (G_OBJECT (iter));
85 g_object_unref (G_OBJECT (local_account));
87 return special_folder;
92 /* These seem to be listed in
93 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c
95 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
96 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
97 #define MODEST_ACCOUNT_OPTION_SSL_ALWAYS "always"
98 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
100 /* These seem to be listed in
101 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c
103 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
104 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
107 /* Posssible values for tny_account_set_secure_auth_mech().
108 * These might be camel-specific.
109 * Really, tinymail should use an enum.
110 * camel_sasl_authtype() seems to list some possible values.
113 /* Note that evolution does not offer these for IMAP: */
114 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
115 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
117 /* Caeml's IMAP uses NULL instead for "Password".
118 * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
119 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN"
120 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
124 * modest_tny_account_new_from_server_account:
125 * @account_mgr: a valid account mgr instance
126 * @account_name: the server account name for which to create a corresponding tny account
127 * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
129 * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
130 * NOTE: this function does not set the camel session or the get/forget password functions
132 * Returns: a new TnyAccount or NULL in case of error.
135 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
136 ModestServerAccountData *account_data)
138 g_return_val_if_fail (account_mgr, NULL);
139 g_return_val_if_fail (account_data, NULL);
142 if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
143 g_printerr ("modest: '%s' does not provide a protocol\n",
144 account_data->account_name);
148 TnyAccount *tny_account = NULL;
150 switch (account_data->proto) {
151 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
152 case MODEST_PROTOCOL_TRANSPORT_SMTP:
153 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
154 case MODEST_PROTOCOL_STORE_POP:
155 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
156 case MODEST_PROTOCOL_STORE_IMAP:
157 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
158 case MODEST_PROTOCOL_STORE_MAILDIR:
159 case MODEST_PROTOCOL_STORE_MBOX:
160 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
162 g_return_val_if_reached (NULL);
165 g_printerr ("modest: could not create tny account for '%s'\n",
166 account_data->account_name);
169 tny_account_set_id (tny_account, account_data->account_name);
172 const gchar* proto_name =
173 modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
174 tny_account_set_proto (tny_account, proto_name);
177 /* mbox and maildir accounts use a URI instead of the rest: */
178 if (account_data->uri)
179 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
181 /* Set camel-specific options: */
183 /* Enable secure connection settings: */
184 const gchar* option_security = NULL;
185 switch (account_data->security) {
186 case MODEST_PROTOCOL_CONNECTION_NORMAL:
187 option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER;
189 case MODEST_PROTOCOL_CONNECTION_SSL:
190 case MODEST_PROTOCOL_CONNECTION_TLS:
191 option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
193 case MODEST_PROTOCOL_CONNECTION_TLS_OP:
194 option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
200 if(option_security) {
201 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
205 const gchar* auth_mech_name = NULL;
206 switch (account_data->secure_auth) {
207 case MODEST_PROTOCOL_AUTH_NONE:
208 /* IMAP needs at least a password,
209 * which camel uses if we specify NULL.
210 * This setting should never happen anyway. */
211 if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
212 auth_mech_name = NULL;
213 else if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP)
214 auth_mech_name = MODEST_ACCOUNT_AUTH_ANONYMOUS;
216 auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
219 case MODEST_PROTOCOL_AUTH_PASSWORD:
220 /* Camel use a password for IMAP if we specify NULL,
221 * but will report an error if we use "Password", "Login" or "Plain". */
222 if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
223 auth_mech_name = NULL;
225 auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
228 case MODEST_PROTOCOL_AUTH_CRAMMD5:
229 auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
233 g_warning ("%s: Unhandled secure authentication setting %d for "
234 "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
235 account_data->account_name, account_data->hostname);
240 tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
243 if (modest_protocol_info_protocol_is_store(account_data->proto)) {
244 /* Other connection options. Some options are only valid for IMAP
245 accounts but it's OK for just now since POP is still not
247 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
248 MODEST_ACCOUNT_OPTION_USE_LSUB);
249 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
250 MODEST_ACCOUNT_OPTION_CHECK_ALL);
253 if (account_data->username)
254 tny_account_set_user (tny_account, account_data->username);
255 if (account_data->hostname)
256 tny_account_set_hostname (tny_account, account_data->hostname);
259 if (account_data->port)
260 tny_account_set_port (tny_account, account_data->port);
268 /* we need these dummy functions, or tinymail will complain */
270 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
275 forget_pass_dummy (TnyAccount *account)
277 /* intentionally left blank */
281 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
283 TnySessionCamel *session,
284 TnyGetPassFunc get_pass_func,
285 TnyForgetPassFunc forget_pass_func)
287 TnyAccount *tny_account = NULL;
288 ModestAccountData *account_data = NULL;
289 ModestServerAccountData *server_data = NULL;
291 g_return_val_if_fail (account_mgr, NULL);
292 g_return_val_if_fail (account_name, NULL);
294 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
296 g_printerr ("modest: cannot get account data for account %s\n", account_name);
300 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
301 server_data = account_data->store_account;
302 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
303 server_data = account_data->transport_account;
305 g_printerr ("modest: no %s account defined for '%s'\n",
306 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
307 account_data->display_name);
308 modest_account_mgr_free_account_data (account_mgr, account_data);
312 tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
314 g_printerr ("modest: failed to create tny account for %s (%s)\n",
315 account_data->account_name, server_data->account_name);
316 modest_account_mgr_free_account_data (account_mgr, account_data);
320 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
321 tny_account_set_forget_pass_func (tny_account,
322 forget_pass_func ? forget_pass_func : forget_pass_dummy);
323 tny_account_set_pass_func (tny_account,
324 get_pass_func ? get_pass_func: get_pass_dummy);
326 /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
327 * account, we set its name to the account of which it is part. */
328 if (account_data->display_name)
329 tny_account_set_name (tny_account, account_data->display_name);
331 g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
332 (gpointer*) g_strdup (account_name), g_free);
334 modest_account_mgr_free_account_data (account_mgr, account_data);
340 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
342 TnyStoreAccount *tny_account;
344 gchar *maildir, *url_string;
346 g_return_val_if_fail (account_mgr, NULL);
348 tny_account = tny_camel_store_account_new ();
350 g_printerr ("modest: cannot create account for local folders");
353 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
355 maildir = modest_local_folder_info_get_maildir_path ();
356 url = camel_url_new ("maildir:", NULL);
357 camel_url_set_path (url, maildir);
358 /* Needed by tinymail's DBC assertions */
359 camel_url_set_host (url, "localhost");
360 url_string = camel_url_to_string (url, 0);
362 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
364 tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
365 tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
366 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
367 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
369 g_object_set_data (G_OBJECT(tny_account), "modest_account",
370 (gpointer*)MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
372 camel_url_free (url);
376 return TNY_ACCOUNT(tny_account);
379 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
381 typedef struct _RecurseFoldersHelper {
382 TnyStatsFunc function;
385 } RecurseFoldersHelper;
388 recurse_folders (TnyFolderStore *store,
389 TnyFolderStoreQuery *query,
390 RecurseFoldersHelper *helper)
393 TnyList *folders = tny_simple_list_new ();
395 tny_folder_store_get_folders (store, folders, query, NULL);
396 iter = tny_list_create_iterator (folders);
398 helper->folders += tny_list_get_length (folders);
400 while (!tny_iterator_is_done (iter)) {
401 TnyFolderStats *stats;
404 folder = TNY_FOLDER (tny_iterator_get_current (iter));
405 stats = tny_folder_get_stats (folder);
407 /* initially, we sometimes get -1 from tinymail; ignore that */
408 if (helper->function && helper->function (stats) > 0)
409 helper->sum += helper->function (stats);
411 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
413 g_object_unref (folder);
414 g_object_unref (stats);
415 tny_iterator_next (iter);
417 g_object_unref (G_OBJECT (iter));
418 g_object_unref (G_OBJECT (folders));
422 modest_tny_account_get_folder_count (TnyAccount *self)
424 RecurseFoldersHelper *helper;
427 g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
430 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
431 helper->function = NULL;
435 recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
437 retval = helper->folders;
445 modest_tny_account_get_message_count (TnyAccount *self)
447 RecurseFoldersHelper *helper;
450 g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
453 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
454 helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
457 recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
459 retval = helper->sum;
467 modest_tny_account_get_local_size (TnyAccount *self)
469 RecurseFoldersHelper *helper;
472 g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
475 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
476 helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
479 recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
481 retval = helper->sum;