d4e4a19aed6e601b13c59ea4384c354995b66fc3
[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         else if (strcmp (folder, "outbox") == 0 ||
58                  strcmp (folder, _("outbox")) == 0 ||
59                  strcmp (folder, _("mcen_me_folder_outbox")) == 0)
60                 type = TNY_FOLDER_TYPE_OUTBOX;
61         else if (g_str_has_prefix(folder, "junk") ||
62                  g_str_has_prefix(folder, _("junk")))
63                 type = TNY_FOLDER_TYPE_JUNK;
64         else if (g_str_has_prefix(folder, "trash") ||
65                  g_str_has_prefix(folder, _("trash")))
66                 type = TNY_FOLDER_TYPE_TRASH;
67         else if (g_str_has_prefix(folder, "sent") ||
68                  g_str_has_prefix(folder, _("sent")) ||
69                  strcmp (folder, _("mcen_me_folder_sent")) == 0)
70                 type = TNY_FOLDER_TYPE_SENT;
71         else if (g_str_has_prefix(folder, "draft") ||
72                  g_str_has_prefix(folder, _("draft")) ||
73                  strcmp (folder, _("mcen_me_folder_drafts")) == 0)
74                 type = TNY_FOLDER_TYPE_DRAFTS;
75         else if (g_str_has_prefix(folder, "notes") ||
76                  g_str_has_prefix(folder, _("notes")))
77                 type = TNY_FOLDER_TYPE_NOTES;
78         else if (g_str_has_prefix(folder, "contacts") ||
79                  g_str_has_prefix(folder, _("contacts")))
80                 type = TNY_FOLDER_TYPE_CONTACTS;
81         else if (g_str_has_prefix(folder, "calendar") ||
82                  g_str_has_prefix(folder, _("calendar")))
83                 type = TNY_FOLDER_TYPE_CALENDAR;
84         
85         g_free (folder);
86         return type;
87 }
88
89
90
91 TnyFolderType
92 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
93 {
94         TnyFolderType type;
95         
96         g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_UNKNOWN);
97
98         if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
99                 type = modest_tny_folder_get_local_folder_type ((TnyFolder*)folder);
100         else
101                 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
102         
103         if (type == TNY_FOLDER_TYPE_UNKNOWN) {
104                 const gchar *folder_name;
105                 folder_name = tny_folder_get_name (TNY_FOLDER (folder));
106                 type =  modest_tny_folder_guess_folder_type_from_name (folder_name);
107         }
108
109         return type;
110 }
111
112
113 /* FIXME: encode all folder rules here */
114 ModestTnyFolderRules
115 modest_tny_folder_get_rules   (TnyFolder *folder)
116 {
117         ModestTnyFolderRules rules = 0;
118         TnyFolderType type;
119
120         g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
121
122         if (modest_tny_folder_is_local_folder (folder) ||
123             modest_tny_folder_is_memory_card_folder (folder)) {
124         
125                 type = modest_tny_folder_get_local_folder_type (folder);
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 not ROOT folders should me moveable */
173                 folder_type = modest_tny_folder_guess_folder_type (folder);
174                 if ((folder_type ==  TNY_FOLDER_TYPE_INBOX) ||
175                     (folder_type == TNY_FOLDER_TYPE_ROOT)) {
176                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
177                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
178                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
179                 }
180         }
181         return rules;
182 }
183
184
185 gboolean
186 modest_tny_folder_is_local_folder   (TnyFolder *folder)
187 {
188         g_return_val_if_fail (folder, FALSE);
189         
190         /* The merge folder is a special case, 
191          * used to merge the per-account local outbox folders. 
192          * and can have no get_account() implementation.
193          * We should do something more sophisticated if we 
194          * ever use TnyMergeFolder for anything else.
195          */
196         if (TNY_IS_MERGE_FOLDER (folder))
197                 return TRUE;
198
199         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
200         if (!account)
201                 return FALSE;
202
203         /* Outbox is a special case, using a derived TnyAccount: */
204         if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
205                 g_object_unref (G_OBJECT(account));
206                 return TRUE;
207         }
208
209         const gchar* account_id = tny_account_get_id (account);
210         if (!account_id) {
211                 g_object_unref (G_OBJECT(account));
212                 return FALSE;
213         }
214         
215         g_object_unref (G_OBJECT(account));
216         return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
217 }
218
219 gboolean
220 modest_tny_folder_is_memory_card_folder   (TnyFolder *folder)
221 {
222         g_return_val_if_fail (folder, FALSE);
223         
224         /* The merge folder is a special case, 
225          * used to merge the per-account local outbox folders. 
226          * and can have no get_account() implementation.
227          */
228         if (TNY_IS_MERGE_FOLDER (folder))
229                 return FALSE;
230
231         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
232         if (!account)
233                 return FALSE;
234
235         const gchar* account_id = tny_account_get_id (account);
236         if (!account_id)
237                 return FALSE;
238
239         g_object_unref (G_OBJECT(account));
240         
241         return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
242 }       
243
244
245 TnyFolderType
246 modest_tny_folder_get_local_folder_type  (TnyFolder *folder)
247 {
248         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
249
250         /* The merge folder is a special case, 
251          * used to merge the per-account local outbox folders. 
252          * and can have no get_account() implementation.
253          * We should do something more sophisticated if we 
254          * ever use TnyMergeFolder for anything else.
255          */
256         if (TNY_IS_MERGE_FOLDER (folder))
257                 return TNY_FOLDER_TYPE_OUTBOX;
258                 
259         /* Outbox is a special case, using a derived TnyAccount: */
260         TnyAccount* parent_account = tny_folder_get_account (folder);
261         if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
262                 g_object_unref (parent_account);
263                 return TNY_FOLDER_TYPE_OUTBOX;  
264         }
265
266         if (parent_account) {
267                 g_object_unref (parent_account);
268                 parent_account = NULL;
269         }
270
271
272         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder),
273                               TNY_FOLDER_TYPE_UNKNOWN);
274
275         /* we need to use the camel functions, because we want the
276          * _full name_, that is, the full path name of the folder,
277          * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
278          */
279         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
280         if (!camel_folder)
281                 return TNY_FOLDER_TYPE_UNKNOWN;
282
283         const gchar *full_name = camel_folder_get_full_name (camel_folder);
284         /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
285         camel_object_unref (CAMEL_OBJECT(camel_folder));
286         
287         if (!full_name) 
288                 return TNY_FOLDER_TYPE_UNKNOWN;
289         else 
290                 return modest_local_folder_info_get_type (full_name);
291 }
292
293 gboolean
294 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
295 {
296         g_return_val_if_fail(folder, FALSE);
297         g_return_val_if_fail(account, FALSE);
298         
299         if (modest_tny_folder_get_local_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
300                 return FALSE;
301                 
302         return TRUE;
303 #if 0   
304         /* we need to use the camel functions, because we want the
305          * _full name_, that is, the full path name of the folder,
306          * to distinguis between 'Outbox' and 'myfunkyfolder/Outbox'
307          */
308         CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
309         if (!camel_folder)
310                 return FALSE;
311
312         const gchar *full_name = camel_folder_get_full_name (camel_folder);
313         camel_object_unref (CAMEL_OBJECT(camel_folder));
314         
315         if (!full_name) 
316                 return TNY_FOLDER_TYPE_UNKNOWN;
317         else 
318                 return modest_local_folder_info_get_type (full_name);
319                 
320         return FALSE;
321 #endif
322 }
323
324 gchar* 
325 modest_tny_folder_get_header_unique_id (TnyHeader *header)
326 {
327         TnyFolder *folder;
328         gchar *url, *retval;
329         const gchar *uid;
330
331         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
332
333         folder = tny_header_get_folder (header);
334         if (!folder)
335                 return NULL;
336
337         url = tny_folder_get_url_string (folder);
338         uid = tny_header_get_uid (header);
339
340         retval = g_strjoin ("/", url, uid, NULL);
341
342         g_free (url);
343         g_object_unref (folder);
344
345         return retval;
346 }
347
348 TnyAccount *modest_tny_folder_get_account (TnyFolder *folder)
349 {
350         TnyAccount *account = NULL;
351         
352         if (TNY_IS_MERGE_FOLDER (folder)) {
353                 /* TnyMergeFolder does not support get_account(), 
354                  * because it could be merging folders from multiple accounts.
355                  * So we assume that this is the local folders account: */
356                  
357                 account = modest_tny_account_store_get_local_folders_account (
358                         TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
359         } else {
360                 account = tny_folder_get_account (folder);
361         }
362         
363         return account;
364 }
365