* make the memory card Archive folder a localized one,
[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 not ROOT 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                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
171                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
172                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
173                 }
174         }
175         return rules;
176 }
177
178
179 gboolean
180 modest_tny_folder_is_local_folder   (TnyFolder *folder)
181 {
182         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
183         
184         /* The merge folder is a special case, 
185          * used to merge the per-account local outbox folders. 
186          * and can have no get_account() implementation.
187          * We should do something more sophisticated if we 
188          * ever use TnyMergeFolder for anything else.
189          */
190         if (TNY_IS_MERGE_FOLDER (folder))
191                 return TRUE;
192
193         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
194         if (!account)
195                 return FALSE;
196         
197         /* Outbox is a special case, using a derived TnyAccount: */
198         if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
199                 g_object_unref (G_OBJECT(account));
200                 return TRUE;
201         }
202
203         const gchar* account_id = tny_account_get_id (account);
204         if (!account_id) {
205                 g_object_unref (G_OBJECT(account));
206                 return FALSE;
207         }
208         
209         g_object_unref (G_OBJECT(account));
210         return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
211 }
212
213 gboolean
214 modest_tny_folder_is_memory_card_folder   (TnyFolder *folder)
215 {
216         g_return_val_if_fail (folder, FALSE);
217         
218         /* The merge folder is a special case, 
219          * used to merge the per-account local outbox folders. 
220          * and can have no get_account() implementation.
221          */
222         if (TNY_IS_MERGE_FOLDER (folder))
223                 return FALSE;
224
225         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
226         if (!account)
227                 return FALSE;
228
229         const gchar* account_id = tny_account_get_id (account);
230         if (!account_id)
231                 return FALSE;
232
233         g_object_unref (G_OBJECT(account));
234         
235         return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
236 }       
237
238
239 TnyFolderType
240 modest_tny_folder_get_local_or_mmc_folder_type  (TnyFolder *folder)
241 {
242         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
243         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
244                               modest_tny_folder_is_memory_card_folder(folder),
245                               TNY_FOLDER_TYPE_UNKNOWN);
246         
247         /* The merge folder is a special case, 
248          * used to merge the per-account local outbox folders. 
249          * and can have no get_account() implementation.
250          * We should do something more sophisticated if we 
251          * ever use TnyMergeFolder for anything else.
252          */
253         if (TNY_IS_MERGE_FOLDER (folder))
254                 return TNY_FOLDER_TYPE_OUTBOX;
255                 
256         /* Outbox is a special case, using a derived TnyAccount: */
257         TnyAccount* parent_account = tny_folder_get_account (folder);
258         if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
259                 g_object_unref (parent_account);
260                 return TNY_FOLDER_TYPE_OUTBOX;  
261         }
262
263         if (parent_account) {
264                 g_object_unref (parent_account);
265                 parent_account = NULL;
266         }
267         
268         /* we need to use the camel functions, because we want the
269          * _full name_, that is, the full path name of the folder,
270          * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
271          */
272         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
273         if (!camel_folder)
274                 return TNY_FOLDER_TYPE_UNKNOWN;
275
276         const gchar *full_name = camel_folder_get_full_name (camel_folder);
277         /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
278         camel_object_unref (CAMEL_OBJECT(camel_folder));
279         
280         if (!full_name) 
281                 return TNY_FOLDER_TYPE_UNKNOWN;
282         else 
283                 return modest_local_folder_info_get_type (full_name);
284 }
285
286 gboolean
287 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
288 {
289         g_return_val_if_fail(folder, FALSE);
290         g_return_val_if_fail(account, FALSE);
291         
292         if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
293                 return FALSE;
294                 
295         return TRUE;
296 #if 0   
297         /* we need to use the camel functions, because we want the
298          * _full name_, that is, the full path name of the folder,
299          * to distinguis between 'Outbox' and 'myfunkyfolder/Outbox'
300          */
301         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
302         if (!camel_folder)
303                 return FALSE;
304
305         const gchar *full_name = camel_folder_get_full_name (camel_folder);
306         camel_object_unref (CAMEL_OBJECT(camel_folder));
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         return FALSE;
314 #endif
315 }
316
317 gchar* 
318 modest_tny_folder_get_header_unique_id (TnyHeader *header)
319 {
320         TnyFolder *folder;
321         gchar *url, *retval;
322         const gchar *uid;
323
324         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
325
326         folder = tny_header_get_folder (header);
327         if (!folder)
328                 return NULL;
329
330         url = tny_folder_get_url_string (folder);
331         uid = tny_header_get_uid (header);
332
333         retval = g_strjoin ("/", url, uid, NULL);
334
335         g_free (url);
336         g_object_unref (folder);
337
338         return retval;
339 }
340
341 TnyAccount *modest_tny_folder_get_account (TnyFolder *folder)
342 {
343         TnyAccount *account = NULL;
344         
345         if (TNY_IS_MERGE_FOLDER (folder)) {
346                 /* TnyMergeFolder does not support get_account(), 
347                  * because it could be merging folders from multiple accounts.
348                  * So we assume that this is the local folders account: */
349                  
350                 account = modest_tny_account_store_get_local_folders_account (
351                         TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
352         } else {
353                 account = tny_folder_get_account (folder);
354         }
355         
356         return account;
357 }
358