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