* Review fucntion 'has_retrieved_msgs' implementation
[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-camel-folder.h>
36 #include <tny-merge-folder.h>
37 #include <camel/camel-folder.h>
38 #include <modest-protocol-info.h>
39 #include <modest-runtime.h>
40 #include <modest-tny-account-store.h>
41
42 TnyFolderType
43 modest_tny_folder_guess_folder_type_from_name (const gchar* name)
44 {
45         gint  type;
46         gchar *folder;
47
48         g_return_val_if_fail (name, TNY_FOLDER_TYPE_UNKNOWN);
49         
50         type = TNY_FOLDER_TYPE_UNKNOWN;
51         folder = g_utf8_strdown (name, strlen(name));
52
53 //      if (strcmp (folder, "inbox") == 0 ||
54 //          strcmp (folder, _("inbox")) == 0 ||
55 //          strcmp (folder, _("mcen_me_folder_inbox")) == 0)
56 //              type = TNY_FOLDER_TYPE_INBOX;
57         if (strcmp (folder, _("mcen_me_folder_outbox")) == 0)
58                 type = TNY_FOLDER_TYPE_OUTBOX;
59 //      else if (g_str_has_prefix(folder, "junk") ||
60 //               g_str_has_prefix(folder, _("junk")))
61 //              type = TNY_FOLDER_TYPE_JUNK;
62 //      else if (g_str_has_prefix(folder, "trash") ||
63 //               g_str_has_prefix(folder, _("trash")))
64 //              type = TNY_FOLDER_TYPE_TRASH;
65         else if (strcmp (folder, _("mcen_me_folder_sent")) == 0)
66                 type = TNY_FOLDER_TYPE_SENT;
67         else if (strcmp (folder, _("mcen_me_folder_drafts")) == 0)
68                 type = TNY_FOLDER_TYPE_DRAFTS;
69 //      else if (g_str_has_prefix(folder, "notes") ||
70 //               g_str_has_prefix(folder, _("notes")))
71 //              type = TNY_FOLDER_TYPE_NOTES;
72 //      else if (g_str_has_prefix(folder, "contacts") ||
73 //               g_str_has_prefix(folder, _("contacts")))
74 //              type = TNY_FOLDER_TYPE_CONTACTS;
75 //      else if (g_str_has_prefix(folder, "calendar") ||
76 //               g_str_has_prefix(folder, _("calendar")))
77 //              type = TNY_FOLDER_TYPE_CALENDAR;
78         
79         g_free (folder);
80         return type;
81 }
82
83
84
85 TnyFolderType
86 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
87 {
88         TnyFolderType type;
89         
90         g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_UNKNOWN);
91
92         if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
93                 type = modest_tny_folder_get_local_or_mmc_folder_type ((TnyFolder*)folder);
94         else
95                 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
96         
97         if (type == TNY_FOLDER_TYPE_UNKNOWN) {
98                 const gchar *folder_name;
99                 folder_name = tny_folder_get_name (TNY_FOLDER (folder));
100                 type =  modest_tny_folder_guess_folder_type_from_name (folder_name);
101         }
102
103         return type;
104 }
105
106
107 /* FIXME: encode all folder rules here */
108 ModestTnyFolderRules
109 modest_tny_folder_get_rules   (TnyFolder *folder)
110 {
111         ModestTnyFolderRules rules = 0;
112         TnyFolderType type;
113
114         g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
115
116         if (modest_tny_folder_is_local_folder (folder) ||
117             modest_tny_folder_is_memory_card_folder (folder)) {
118         
119                 type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
120                 
121                 switch (type) {
122                 case TNY_FOLDER_TYPE_OUTBOX:
123                 case TNY_FOLDER_TYPE_SENT:
124                 case TNY_FOLDER_TYPE_DRAFTS:
125                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
126                 case TNY_FOLDER_TYPE_INBOX:
127                 case TNY_FOLDER_TYPE_JUNK:
128                 case TNY_FOLDER_TYPE_TRASH:
129                 case TNY_FOLDER_TYPE_ROOT:
130                 case TNY_FOLDER_TYPE_NOTES:
131                 case TNY_FOLDER_TYPE_CONTACTS:
132                 case TNY_FOLDER_TYPE_CALENDAR:
133                 case TNY_FOLDER_TYPE_ARCHIVE:
134                 case TNY_FOLDER_TYPE_MERGE:
135                 case TNY_FOLDER_TYPE_NUM:
136                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
137                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
138                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
139                 default:
140                         break;
141                 }
142         } else {
143                 ModestTransportStoreProtocol proto;
144                 TnyFolderType folder_type;
145                 TnyAccount *account;
146
147                 account = modest_tny_folder_get_account ((TnyFolder*)folder);
148                 if (!account)
149                         return -1; /* no account: nothing is allowed */
150                 
151                 proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
152
153                 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
154                         rules = 0;
155                 } else {
156                         /* pop, nntp, ... */
157                         rules =
158                                 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
159                                 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
160                                 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE  |
161                                 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
162
163                 }
164                 g_object_unref (G_OBJECT(account));
165
166                 /* Neither INBOX nor ROOT, nor ARCHIVE folders should me moveable */
167                 folder_type = modest_tny_folder_guess_folder_type (folder);
168                 if ((folder_type ==  TNY_FOLDER_TYPE_INBOX) ||
169                     (folder_type == TNY_FOLDER_TYPE_ROOT) ||
170                     (folder_type == TNY_FOLDER_TYPE_ARCHIVE)) {
171                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
172                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
173                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
174                 }
175         }
176         return rules;
177 }
178
179         
180
181 gboolean
182 modest_tny_folder_is_local_folder   (TnyFolder *folder)
183 {
184         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
185         
186         /* The merge folder is a special case, 
187          * used to merge the per-account local outbox folders. 
188          * and can have no get_account() implementation.
189          * We should do something more sophisticated if we 
190          * ever use TnyMergeFolder for anything else.
191          */
192         if (TNY_IS_MERGE_FOLDER (folder)) {
193                 return TRUE;
194         }
195         
196         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
197         if (!account) {
198                 g_warning ("folder without account");
199                 return FALSE;
200         }
201         
202         /* Outbox is a special case, using a derived TnyAccount: */
203         if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
204                 //g_warning ("BUG: should not be reached");
205                 /* should be handled with the MERGE_FOLDER above*/
206                 g_object_unref (G_OBJECT(account));
207                 return TRUE;
208         }
209
210         const gchar* account_id = tny_account_get_id (account);
211         if (!account_id) {
212                 g_warning ("BUG: account without account id");
213                 g_object_unref (G_OBJECT(account));
214                 return FALSE;
215         }
216         
217         g_object_unref (G_OBJECT(account));
218         return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
219 }
220
221 gboolean
222 modest_tny_folder_is_memory_card_folder   (TnyFolder *folder)
223 {
224         g_return_val_if_fail (folder, FALSE);
225         
226         /* The merge folder is a special case, 
227          * used to merge the per-account local outbox folders. 
228          * and can have no get_account() implementation.
229          */
230         if (TNY_IS_MERGE_FOLDER (folder))
231                 return FALSE;
232
233         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
234         if (!account)
235                 return FALSE;
236
237         const gchar* account_id = tny_account_get_id (account);
238         if (!account_id)
239                 return FALSE;
240
241         g_object_unref (G_OBJECT(account));
242         
243         return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
244 }       
245
246 gboolean
247 modest_tny_folder_is_remote_folder   (TnyFolder *folder)
248 {
249         gboolean is_local = TRUE;
250
251         g_return_val_if_fail (folder, FALSE);
252         
253         is_local = ((modest_tny_folder_is_local_folder(folder)) ||
254                     (modest_tny_folder_is_memory_card_folder(folder)));
255
256
257         return !is_local;
258 }
259
260 TnyFolderType
261 modest_tny_folder_get_local_or_mmc_folder_type  (TnyFolder *folder)
262 {
263         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
264         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
265                               modest_tny_folder_is_memory_card_folder(folder),
266                               TNY_FOLDER_TYPE_UNKNOWN);
267         
268         /* The merge folder is a special case, 
269          * used to merge the per-account local outbox folders. 
270          * and can have no get_account() implementation.
271          * We should do something more sophisticated if we 
272          * ever use TnyMergeFolder for anything else.
273          */
274         if (TNY_IS_MERGE_FOLDER (folder))
275                 return TNY_FOLDER_TYPE_OUTBOX;
276                 
277         /* Outbox is a special case, using a derived TnyAccount: */
278         TnyAccount* parent_account = tny_folder_get_account (folder);
279         if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
280                 g_object_unref (parent_account);
281                 return TNY_FOLDER_TYPE_OUTBOX;  
282         }
283
284         if (parent_account) {
285                 g_object_unref (parent_account);
286                 parent_account = NULL;
287         }
288         
289         /* we need to use the camel functions, because we want the
290          * _full name_, that is, the full path name of the folder,
291          * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
292          */
293         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
294         if (!camel_folder)
295                 return TNY_FOLDER_TYPE_UNKNOWN;
296
297         const gchar *full_name = camel_folder_get_full_name (camel_folder);
298         /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
299         camel_object_unref (CAMEL_OBJECT(camel_folder));
300         
301         if (!full_name) 
302                 return TNY_FOLDER_TYPE_UNKNOWN;
303         else 
304                 return modest_local_folder_info_get_type (full_name);
305 }
306
307 gboolean
308 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
309 {
310         g_return_val_if_fail(folder, FALSE);
311         g_return_val_if_fail(account, FALSE);
312         
313         if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
314                 return FALSE;
315                 
316         return TRUE;
317 #if 0   
318         /* we need to use the camel functions, because we want the
319          * _full name_, that is, the full path name of the folder,
320          * to distinguis between 'Outbox' and 'myfunkyfolder/Outbox'
321          */
322         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
323         if (!camel_folder)
324                 return FALSE;
325
326         const gchar *full_name = camel_folder_get_full_name (camel_folder);
327         camel_object_unref (CAMEL_OBJECT(camel_folder));
328         
329         if (!full_name) 
330                 return TNY_FOLDER_TYPE_UNKNOWN;
331         else 
332                 return modest_local_folder_info_get_type (full_name);
333                 
334         return FALSE;
335 #endif
336 }
337
338 gchar* 
339 modest_tny_folder_get_header_unique_id (TnyHeader *header)
340 {
341         TnyFolder *folder;
342         gchar *url, *retval;
343         const gchar *uid;
344
345         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
346
347         folder = tny_header_get_folder (header);
348         if (!folder)
349                 return NULL;
350
351         url = tny_folder_get_url_string (folder);
352         uid = tny_header_get_uid (header);
353
354         retval = g_strjoin ("/", url, uid, NULL);
355
356         g_free (url);
357         g_object_unref (folder);
358
359         return retval;
360 }
361
362 TnyAccount *
363 modest_tny_folder_get_account (TnyFolder *folder)
364 {
365         TnyAccount *account = NULL;
366         
367         if (TNY_IS_MERGE_FOLDER (folder)) {
368                 /* TnyMergeFolder does not support get_account(), 
369                  * because it could be merging folders from multiple accounts.
370                  * So we assume that this is the local folders account: */
371                  
372                 account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
373         } else {
374                 account = tny_folder_get_account (folder);
375         }
376         
377         return account;
378 }