* speed up modest_account_mgr_has_accounts by caching the status
[modest] / src / modest-tny-folder.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 <glib.h>
31 #include <glib/gi18n.h>
32 #include <string.h>
33 #include <modest-tny-folder.h>
34 #include <modest-tny-outbox-account.h>
35 #include <tny-simple-list.h>
36 #include <tny-camel-folder.h>
37 #include <tny-merge-folder.h>
38 #include <modest-protocol-info.h>
39 #include <modest-runtime.h>
40 #include <modest-tny-account-store.h>
41 #include <modest-text-utils.h>
42
43
44 /* make sure you use the *full* name, because foo/drafts is not the same as drafts */
45 static TnyFolderType
46 modest_tny_folder_guess_folder_type_from_name (const gchar* full_name)
47 {
48         g_return_val_if_fail (full_name, TNY_FOLDER_TYPE_INVALID);
49         
50         if (strcmp (full_name, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
51                 return TNY_FOLDER_TYPE_OUTBOX;
52         else if (strcmp (full_name, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
53                 return TNY_FOLDER_TYPE_DRAFTS;
54         return
55                 TNY_FOLDER_TYPE_NORMAL;
56 }
57
58
59
60 TnyFolderType
61 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
62 {
63         TnyFolderType type;
64         
65         g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_INVALID);
66
67         if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
68                 type = modest_tny_folder_get_local_or_mmc_folder_type ((TnyFolder*)folder);
69         else
70                 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
71         
72         if (type == TNY_FOLDER_TYPE_UNKNOWN) {
73                 const gchar *folder_name =
74                         tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
75                 type =  modest_tny_folder_guess_folder_type_from_name (folder_name);
76         }
77
78         if (type == TNY_FOLDER_TYPE_INVALID)
79                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
80
81         return type;
82 }
83
84
85 const gchar*
86 modest_tny_folder_get_help_id (const TnyFolder *folder)
87 {
88         TnyFolderType type;
89         const gchar* help_id = NULL;
90         
91         g_return_val_if_fail (folder, NULL);
92         g_return_val_if_fail (TNY_IS_FOLDER(folder), NULL);
93         
94         type = modest_tny_folder_guess_folder_type (TNY_FOLDER (folder));
95         
96         switch (type) {
97         case TNY_FOLDER_TYPE_NORMAL:  help_id = "applications_email_managefolders"; break;
98         case TNY_FOLDER_TYPE_INBOX:   help_id = "applications_email_inbox";break;
99         case TNY_FOLDER_TYPE_OUTBOX:  help_id = "applications_email_outbox";break;
100         case TNY_FOLDER_TYPE_SENT:    help_id = "applications_email_sent"; break;
101         case TNY_FOLDER_TYPE_DRAFTS:  help_id = "applications_email_drafts";break;
102         case TNY_FOLDER_TYPE_ARCHIVE: help_id = "applications_email_managefolders";break;
103
104         case TNY_FOLDER_TYPE_INVALID: g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);break;
105         default:                      g_warning ("%s: BUG: unexpected folder type (%d)", __FUNCTION__, type);
106         }
107         
108         return help_id;
109 }
110
111
112
113 /* FIXME: encode all folder rules here */
114 ModestTnyFolderRules
115 modest_tny_folder_get_rules   (TnyFolder *folder)
116 {
117         ModestTnyFolderRules rules = 0;
118         TnyFolderType type;
119
120         g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
121         
122         if (modest_tny_folder_is_local_folder (folder) ||
123             modest_tny_folder_is_memory_card_folder (folder)) {
124         
125                 type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
126                 g_return_val_if_fail (type != TNY_FOLDER_TYPE_INVALID, -1);
127                 
128                 switch (type) {
129                 case TNY_FOLDER_TYPE_OUTBOX:
130                 case TNY_FOLDER_TYPE_SENT:
131                 case TNY_FOLDER_TYPE_DRAFTS:
132                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
133                 case TNY_FOLDER_TYPE_INBOX:
134                 case TNY_FOLDER_TYPE_JUNK:
135                 case TNY_FOLDER_TYPE_TRASH:
136                 case TNY_FOLDER_TYPE_ROOT:
137                 case TNY_FOLDER_TYPE_NOTES:
138                 case TNY_FOLDER_TYPE_CONTACTS:
139                 case TNY_FOLDER_TYPE_CALENDAR:
140                 case TNY_FOLDER_TYPE_ARCHIVE:
141                 case TNY_FOLDER_TYPE_MERGE:
142                 case TNY_FOLDER_TYPE_NUM:
143                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
144                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
145                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
146                 default:
147                         break;
148                 }
149         } else {
150                 ModestTransportStoreProtocol proto;
151                 TnyFolderType folder_type;
152                 TnyAccount *account;
153
154                 account = modest_tny_folder_get_account ((TnyFolder*)folder);
155                 if (!account)
156                         return -1; /* no account: nothing is allowed */
157                 
158                 proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
159
160                 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
161                         rules = 0;
162                 } else {
163                         /* pop, nntp, ... */
164                         rules =
165                                 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
166                                 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
167                                 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE  |
168                                 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
169
170                 }
171                 g_object_unref (G_OBJECT(account));
172
173                 /* Neither INBOX nor ROOT, nor ARCHIVE folders should me moveable */
174                 folder_type = modest_tny_folder_guess_folder_type (folder);
175                 g_return_val_if_fail (folder_type != TNY_FOLDER_TYPE_INVALID, -1);
176                 
177                 if ((folder_type ==  TNY_FOLDER_TYPE_INBOX) ||
178                     (folder_type == TNY_FOLDER_TYPE_ROOT) ||
179                     (folder_type == TNY_FOLDER_TYPE_ARCHIVE)) {
180                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
181                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
182                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
183                 }
184         }
185         return rules;
186 }
187
188         
189
190 gboolean
191 modest_tny_folder_is_local_folder   (TnyFolder *folder)
192 {
193         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
194         
195         /* The merge folder is a special case, 
196          * used to merge the per-account local outbox folders. 
197          * and can have no get_account() implementation.
198          * We should do something more sophisticated if we 
199          * ever use TnyMergeFolder for anything else.
200          */
201         if (TNY_IS_MERGE_FOLDER (folder)) {
202                 return TRUE;
203         }
204         
205         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
206         if (!account) {
207                 g_warning ("folder without account");
208                 return FALSE;
209         }
210         
211         /* Outbox is a special case, using a derived TnyAccount: */
212         if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
213                 //g_warning ("BUG: should not be reached");
214                 /* should be handled with the MERGE_FOLDER above*/
215                 g_object_unref (G_OBJECT(account));
216                 return TRUE;
217         }
218
219         const gchar* account_id = tny_account_get_id (account);
220         if (!account_id) {
221                 g_warning ("BUG: account without account id");
222                 g_object_unref (G_OBJECT(account));
223                 return FALSE;
224         }
225         
226         g_object_unref (G_OBJECT(account));
227         return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
228 }
229
230 gboolean
231 modest_tny_folder_is_memory_card_folder   (TnyFolder *folder)
232 {
233         g_return_val_if_fail (folder, FALSE);
234         g_return_val_if_fail (modest_tny_folder_guess_folder_type (folder) !=
235                               TNY_FOLDER_TYPE_INVALID, FALSE);
236         
237         /* The merge folder is a special case, 
238          * used to merge the per-account local outbox folders. 
239          * and can have no get_account() implementation.
240          */
241         if (TNY_IS_MERGE_FOLDER (folder))
242                 return FALSE;
243
244         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
245         if (!account)
246                 return FALSE;
247
248         const gchar* account_id = tny_account_get_id (account);
249         if (!account_id)
250                 return FALSE;
251
252         g_object_unref (G_OBJECT(account));
253         
254         return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
255 }       
256
257 gboolean
258 modest_tny_folder_is_remote_folder   (TnyFolder *folder)
259 {
260         gboolean is_local = TRUE;
261
262         g_return_val_if_fail (folder, FALSE);
263         
264         is_local = ((modest_tny_folder_is_local_folder(folder)) ||
265                     (modest_tny_folder_is_memory_card_folder(folder)));
266
267
268         return !is_local;
269 }
270
271
272 TnyFolderType
273 modest_tny_folder_get_local_or_mmc_folder_type  (TnyFolder *folder)
274 {
275         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_INVALID);
276         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
277                               modest_tny_folder_is_memory_card_folder(folder),
278                               TNY_FOLDER_TYPE_INVALID);
279         
280         /* The merge folder is a special case, 
281          * used to merge the per-account local outbox folders. 
282          * and can have no get_account() implementation.
283          * We should do something more sophisticated if we 
284          * ever use TnyMergeFolder for anything else.
285          */
286         if (TNY_IS_MERGE_FOLDER (folder))
287                 return TNY_FOLDER_TYPE_OUTBOX;
288                 
289         /* Outbox is a special case, using a derived TnyAccount: */
290         TnyAccount* parent_account = tny_folder_get_account (folder);
291         if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
292                 g_object_unref (parent_account);
293                 return TNY_FOLDER_TYPE_OUTBOX;  
294         }
295
296         if (parent_account) {
297                 g_object_unref (parent_account);
298                 parent_account = NULL;
299         }
300         
301         /* we need to use the camel functions, because we want the
302          * _full name_, that is, the full path name of the folder,
303          * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
304          */
305
306         const gchar *full_name = tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
307         /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
308         
309         if (!full_name) 
310                 return TNY_FOLDER_TYPE_UNKNOWN;
311         else 
312                 return modest_local_folder_info_get_type (full_name);
313 }
314
315 gboolean
316 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
317 {
318         g_return_val_if_fail(folder, FALSE);
319         g_return_val_if_fail(account, FALSE);
320         
321         if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
322                 return FALSE;
323                 
324         return TRUE;
325 }
326
327 gchar* 
328 modest_tny_folder_get_header_unique_id (TnyHeader *header)
329 {
330         TnyFolder *folder;
331         gchar *url, *retval;
332         const gchar *uid;
333
334         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
335
336         folder = tny_header_get_folder (header);
337         if (!folder)
338                 return NULL;
339
340         url = tny_folder_get_url_string (folder);
341         uid = tny_header_get_uid (header);
342
343         retval = g_strjoin ("/", url, uid, NULL);
344
345         g_free (url);
346         g_object_unref (folder);
347
348         return retval;
349 }
350
351 TnyAccount *
352 modest_tny_folder_get_account (TnyFolder *folder)
353 {
354         TnyAccount *account = NULL;
355         
356         if (TNY_IS_MERGE_FOLDER (folder)) {
357                 /* TnyMergeFolder does not support get_account(), 
358                  * because it could be merging folders from multiple accounts.
359                  * So we assume that this is the local folders account: */
360                  
361                 account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
362         } else {
363                 account = tny_folder_get_account (folder);
364         }
365         
366         return account;
367 }
368
369 /*
370  * It's probably better to use a query to get the folders that match
371  * new_name but currently tinymail only provides a match by name using
372  * regular expressions and we want an exact matching. We're not using
373  * a regular expression for the exact name because we'd need first to
374  * escape @new_name and it's not easy sometimes. 
375  *
376  * The code that uses the query is available in revision 3152.
377  */
378 gboolean 
379 modest_tny_folder_has_subfolder_with_name (TnyFolderStore *parent,
380                                            const gchar *new_name,
381                                            gboolean non_strict)
382 {
383         TnyList *subfolders = NULL;
384         TnyIterator *iter = NULL;
385         TnyFolder *folder = NULL;
386         GError *err = NULL;
387         gboolean has_name = FALSE;
388
389         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), FALSE);
390         g_return_val_if_fail (new_name, FALSE);
391         
392         /* Get direct subfolders */
393         subfolders = tny_simple_list_new ();
394         tny_folder_store_get_folders (parent, subfolders, NULL, &err);
395
396         /* Check names */
397         iter = tny_list_create_iterator (subfolders);
398         while (!tny_iterator_is_done (iter) && !has_name) {
399                 
400                 const gchar *name;
401                 
402                 folder = (TnyFolder*)tny_iterator_get_current (iter);
403                 if (!folder || ! TNY_IS_FOLDER(folder)) {
404                         g_warning ("%s: invalid folder", __FUNCTION__);
405                         continue;
406                 }
407                 
408                 name = tny_folder_get_name (folder);
409                 if (!name) {
410                         g_warning ("%s: folder name == NULL", __FUNCTION__);
411                         g_object_unref (folder);
412                         continue;
413                 }
414
415                 /* is it simply the same folder name? */
416                 if (strcmp (name, new_name) == 0)
417                         has_name = TRUE;
418                 /* or is it the same when ignoring case (non-strict mode)? */
419                 else if (non_strict && modest_text_utils_utf8_strcmp (name, new_name, TRUE) == 0)
420                         has_name = TRUE;
421                 /* or is the name equal to the display name of some folder, in the current locale? */
422                 else if (non_strict) {
423                         TnyFolderType type = modest_tny_folder_guess_folder_type (folder);
424                         if (type != TNY_FOLDER_TYPE_INVALID && type != TNY_FOLDER_TYPE_NORMAL) 
425                                 has_name = !(modest_text_utils_utf8_strcmp (modest_local_folder_info_get_type_display_name (type),
426                                                                             new_name,
427                                                                             TRUE));
428                 } else {
429                         has_name = FALSE;
430                 }
431                 
432                 g_object_unref (folder);
433                 tny_iterator_next(iter);
434         }
435         
436         /* free */
437         if (iter != NULL)
438                 g_object_unref (iter);
439         if (subfolders != NULL)
440                 g_object_unref (subfolders);
441                 
442         return has_name;
443 }
444
445 gboolean 
446 modest_tny_folder_is_ancestor (TnyFolder *folder,
447                                TnyFolderStore *ancestor)
448 {
449         TnyFolderStore *tmp = NULL;
450         gboolean found = FALSE;
451
452         tmp = TNY_FOLDER_STORE (folder);
453         while (!found && tmp && !TNY_IS_ACCOUNT (tmp)) {
454                 TnyFolderStore *folder_store;
455
456                 folder_store = tny_folder_get_folder_store (TNY_FOLDER (tmp));
457                 if (ancestor == folder_store)
458                         found = TRUE;
459                 else
460                         tmp = folder_store;
461                 g_object_unref (folder_store);
462         }
463         return found;
464 }