* check for a valid foldername
[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
247 TnyFolderType
248 modest_tny_folder_get_local_or_mmc_folder_type  (TnyFolder *folder)
249 {
250         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
251         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
252                               modest_tny_folder_is_memory_card_folder(folder),
253                               TNY_FOLDER_TYPE_UNKNOWN);
254         
255         /* The merge folder is a special case, 
256          * used to merge the per-account local outbox folders. 
257          * and can have no get_account() implementation.
258          * We should do something more sophisticated if we 
259          * ever use TnyMergeFolder for anything else.
260          */
261         if (TNY_IS_MERGE_FOLDER (folder))
262                 return TNY_FOLDER_TYPE_OUTBOX;
263                 
264         /* Outbox is a special case, using a derived TnyAccount: */
265         TnyAccount* parent_account = tny_folder_get_account (folder);
266         if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
267                 g_object_unref (parent_account);
268                 return TNY_FOLDER_TYPE_OUTBOX;  
269         }
270
271         if (parent_account) {
272                 g_object_unref (parent_account);
273                 parent_account = NULL;
274         }
275         
276         /* we need to use the camel functions, because we want the
277          * _full name_, that is, the full path name of the folder,
278          * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
279          */
280         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
281         if (!camel_folder)
282                 return TNY_FOLDER_TYPE_UNKNOWN;
283
284         const gchar *full_name = camel_folder_get_full_name (camel_folder);
285         /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
286         camel_object_unref (CAMEL_OBJECT(camel_folder));
287         
288         if (!full_name) 
289                 return TNY_FOLDER_TYPE_UNKNOWN;
290         else 
291                 return modest_local_folder_info_get_type (full_name);
292 }
293
294 gboolean
295 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
296 {
297         g_return_val_if_fail(folder, FALSE);
298         g_return_val_if_fail(account, FALSE);
299         
300         if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
301                 return FALSE;
302                 
303         return TRUE;
304 #if 0   
305         /* we need to use the camel functions, because we want the
306          * _full name_, that is, the full path name of the folder,
307          * to distinguis between 'Outbox' and 'myfunkyfolder/Outbox'
308          */
309         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
310         if (!camel_folder)
311                 return FALSE;
312
313         const gchar *full_name = camel_folder_get_full_name (camel_folder);
314         camel_object_unref (CAMEL_OBJECT(camel_folder));
315         
316         if (!full_name) 
317                 return TNY_FOLDER_TYPE_UNKNOWN;
318         else 
319                 return modest_local_folder_info_get_type (full_name);
320                 
321         return FALSE;
322 #endif
323 }
324
325 gchar* 
326 modest_tny_folder_get_header_unique_id (TnyHeader *header)
327 {
328         TnyFolder *folder;
329         gchar *url, *retval;
330         const gchar *uid;
331
332         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
333
334         folder = tny_header_get_folder (header);
335         if (!folder)
336                 return NULL;
337
338         url = tny_folder_get_url_string (folder);
339         uid = tny_header_get_uid (header);
340
341         retval = g_strjoin ("/", url, uid, NULL);
342
343         g_free (url);
344         g_object_unref (folder);
345
346         return retval;
347 }
348
349 TnyAccount *
350 modest_tny_folder_get_account (TnyFolder *folder)
351 {
352         TnyAccount *account = NULL;
353         
354         if (TNY_IS_MERGE_FOLDER (folder)) {
355                 /* TnyMergeFolder does not support get_account(), 
356                  * because it could be merging folders from multiple accounts.
357                  * So we assume that this is the local folders account: */
358                  
359                 account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
360         } else {
361                 account = tny_folder_get_account (folder);
362         }
363         
364         return account;
365 }