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