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