* make modest_text_utils_get_display_date return a ptr to
[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-simple-list.h>
36 #include <tny-camel-folder.h>
37 #include <tny-merge-folder.h>
38 #include <modest-protocol-info.h>
39 #include <modest-runtime.h>
40 #include <modest-tny-account-store.h>
41
42
43 /* make sure you use the *full* name, because foo/drafts is not the same as drafts */
44 static TnyFolderType
45 modest_tny_folder_guess_folder_type_from_name (const gchar* full_name)
46 {
47         g_return_val_if_fail (full_name, TNY_FOLDER_TYPE_INVALID);
48         
49         if (strcmp (full_name, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
50                 return TNY_FOLDER_TYPE_OUTBOX;
51         else if (strcmp (full_name, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
52                 return TNY_FOLDER_TYPE_DRAFTS;
53         return
54                 TNY_FOLDER_TYPE_UNKNOWN;
55 }
56
57
58
59 TnyFolderType
60 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
61 {
62         TnyFolderType type;
63         
64         g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_INVALID);
65
66         if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
67                 type = modest_tny_folder_get_local_or_mmc_folder_type ((TnyFolder*)folder);
68         else
69                 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
70         
71         if (type == TNY_FOLDER_TYPE_UNKNOWN) {
72                 const gchar *folder_name =
73                         tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
74                 type =  modest_tny_folder_guess_folder_type_from_name (folder_name);
75         }
76
77         if (type == TNY_FOLDER_TYPE_INVALID)
78                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
79
80         return type;
81 }
82
83
84 /* FIXME: encode all folder rules here */
85 ModestTnyFolderRules
86 modest_tny_folder_get_rules   (TnyFolder *folder)
87 {
88         ModestTnyFolderRules rules = 0;
89         TnyFolderType type;
90
91         g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
92         
93         if (modest_tny_folder_is_local_folder (folder) ||
94             modest_tny_folder_is_memory_card_folder (folder)) {
95         
96                 type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
97                 g_return_val_if_fail (type != TNY_FOLDER_TYPE_INVALID, -1);
98                 
99                 switch (type) {
100                 case TNY_FOLDER_TYPE_OUTBOX:
101                 case TNY_FOLDER_TYPE_SENT:
102                 case TNY_FOLDER_TYPE_DRAFTS:
103                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
104                 case TNY_FOLDER_TYPE_INBOX:
105                 case TNY_FOLDER_TYPE_JUNK:
106                 case TNY_FOLDER_TYPE_TRASH:
107                 case TNY_FOLDER_TYPE_ROOT:
108                 case TNY_FOLDER_TYPE_NOTES:
109                 case TNY_FOLDER_TYPE_CONTACTS:
110                 case TNY_FOLDER_TYPE_CALENDAR:
111                 case TNY_FOLDER_TYPE_ARCHIVE:
112                 case TNY_FOLDER_TYPE_MERGE:
113                 case TNY_FOLDER_TYPE_NUM:
114                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
115                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
116                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
117                 default:
118                         break;
119                 }
120         } else {
121                 ModestTransportStoreProtocol proto;
122                 TnyFolderType folder_type;
123                 TnyAccount *account;
124
125                 account = modest_tny_folder_get_account ((TnyFolder*)folder);
126                 if (!account)
127                         return -1; /* no account: nothing is allowed */
128                 
129                 proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
130
131                 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
132                         rules = 0;
133                 } else {
134                         /* pop, nntp, ... */
135                         rules =
136                                 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
137                                 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
138                                 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE  |
139                                 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
140
141                 }
142                 g_object_unref (G_OBJECT(account));
143
144                 /* Neither INBOX nor ROOT, nor ARCHIVE folders should me moveable */
145                 folder_type = modest_tny_folder_guess_folder_type (folder);
146                 g_return_val_if_fail (folder_type != TNY_FOLDER_TYPE_INVALID, -1);
147                 
148                 if ((folder_type ==  TNY_FOLDER_TYPE_INBOX) ||
149                     (folder_type == TNY_FOLDER_TYPE_ROOT) ||
150                     (folder_type == TNY_FOLDER_TYPE_ARCHIVE)) {
151                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
152                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
153                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
154                 }
155         }
156         return rules;
157 }
158
159         
160
161 gboolean
162 modest_tny_folder_is_local_folder   (TnyFolder *folder)
163 {
164         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
165         
166         /* The merge folder is a special case, 
167          * used to merge the per-account local outbox folders. 
168          * and can have no get_account() implementation.
169          * We should do something more sophisticated if we 
170          * ever use TnyMergeFolder for anything else.
171          */
172         if (TNY_IS_MERGE_FOLDER (folder)) {
173                 return TRUE;
174         }
175         
176         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
177         if (!account) {
178                 g_warning ("folder without account");
179                 return FALSE;
180         }
181         
182         /* Outbox is a special case, using a derived TnyAccount: */
183         if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
184                 //g_warning ("BUG: should not be reached");
185                 /* should be handled with the MERGE_FOLDER above*/
186                 g_object_unref (G_OBJECT(account));
187                 return TRUE;
188         }
189
190         const gchar* account_id = tny_account_get_id (account);
191         if (!account_id) {
192                 g_warning ("BUG: account without account id");
193                 g_object_unref (G_OBJECT(account));
194                 return FALSE;
195         }
196         
197         g_object_unref (G_OBJECT(account));
198         return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
199 }
200
201 gboolean
202 modest_tny_folder_is_memory_card_folder   (TnyFolder *folder)
203 {
204         g_return_val_if_fail (folder, FALSE);
205         g_return_val_if_fail (modest_tny_folder_guess_folder_type (folder) !=
206                               TNY_FOLDER_TYPE_INVALID, FALSE);
207         
208         /* The merge folder is a special case, 
209          * used to merge the per-account local outbox folders. 
210          * and can have no get_account() implementation.
211          */
212         if (TNY_IS_MERGE_FOLDER (folder))
213                 return FALSE;
214
215         TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
216         if (!account)
217                 return FALSE;
218
219         const gchar* account_id = tny_account_get_id (account);
220         if (!account_id)
221                 return FALSE;
222
223         g_object_unref (G_OBJECT(account));
224         
225         return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
226 }       
227
228 gboolean
229 modest_tny_folder_is_remote_folder   (TnyFolder *folder)
230 {
231         gboolean is_local = TRUE;
232
233         g_return_val_if_fail (folder, FALSE);
234         
235         is_local = ((modest_tny_folder_is_local_folder(folder)) ||
236                     (modest_tny_folder_is_memory_card_folder(folder)));
237
238
239         return !is_local;
240 }
241
242
243 TnyFolderType
244 modest_tny_folder_get_local_or_mmc_folder_type  (TnyFolder *folder)
245 {
246         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_INVALID);
247         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
248                               modest_tny_folder_is_memory_card_folder(folder),
249                               TNY_FOLDER_TYPE_INVALID);
250         
251         /* The merge folder is a special case, 
252          * used to merge the per-account local outbox folders. 
253          * and can have no get_account() implementation.
254          * We should do something more sophisticated if we 
255          * ever use TnyMergeFolder for anything else.
256          */
257         if (TNY_IS_MERGE_FOLDER (folder))
258                 return TNY_FOLDER_TYPE_OUTBOX;
259                 
260         /* Outbox is a special case, using a derived TnyAccount: */
261         TnyAccount* parent_account = tny_folder_get_account (folder);
262         if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
263                 g_object_unref (parent_account);
264                 return TNY_FOLDER_TYPE_OUTBOX;  
265         }
266
267         if (parent_account) {
268                 g_object_unref (parent_account);
269                 parent_account = NULL;
270         }
271         
272         /* we need to use the camel functions, because we want the
273          * _full name_, that is, the full path name of the folder,
274          * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
275          */
276
277         const gchar *full_name = tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
278         /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
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 }
297
298 gchar* 
299 modest_tny_folder_get_header_unique_id (TnyHeader *header)
300 {
301         TnyFolder *folder;
302         gchar *url, *retval;
303         const gchar *uid;
304
305         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
306
307         folder = tny_header_get_folder (header);
308         if (!folder)
309                 return NULL;
310
311         url = tny_folder_get_url_string (folder);
312         uid = tny_header_get_uid (header);
313
314         retval = g_strjoin ("/", url, uid, NULL);
315
316         g_free (url);
317         g_object_unref (folder);
318
319         return retval;
320 }
321
322 TnyAccount *
323 modest_tny_folder_get_account (TnyFolder *folder)
324 {
325         TnyAccount *account = NULL;
326         
327         if (TNY_IS_MERGE_FOLDER (folder)) {
328                 /* TnyMergeFolder does not support get_account(), 
329                  * because it could be merging folders from multiple accounts.
330                  * So we assume that this is the local folders account: */
331                  
332                 account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
333         } else {
334                 account = tny_folder_get_account (folder);
335         }
336         
337         return account;
338 }
339
340 /*
341  * It's probably better to use a query to get the folders that match
342  * new_name but currently tinymail only provides a match by name using
343  * regular expressions and we want an exact matching. We're not using
344  * a regular expression for the exact name because we'd need first to
345  * escape @new_name and it's not easy sometimes. 
346  *
347  * The code that uses the query is available in revision 3152.
348  */
349 gboolean 
350 modest_tny_folder_has_subfolder_with_name (TnyFolderStore *parent,
351                                            const gchar *new_name)
352 {
353         TnyList *subfolders = NULL;
354         TnyIterator *iter = NULL;
355         TnyFolder *folder = NULL;
356         GError *err = NULL;
357         gboolean same_subfolder = FALSE;
358
359         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), FALSE);
360
361         /* Get direct subfolders */
362         subfolders = tny_simple_list_new ();
363         tny_folder_store_get_folders (parent, subfolders, NULL, &err);
364
365         /* Check names */
366         iter = tny_list_create_iterator (subfolders);
367         while (!tny_iterator_is_done (iter) && !same_subfolder) {
368                 const gchar *name = NULL;
369
370                 folder = TNY_FOLDER(tny_iterator_get_current (iter));
371                 name = tny_folder_get_name (folder);
372                 
373                 same_subfolder = !strcmp(name, new_name);
374
375                 g_object_unref (folder);
376                 tny_iterator_next(iter);
377         }
378         
379         /* free */
380         if (iter != NULL)
381                 g_object_unref (iter);
382         if (subfolders != NULL)
383                 g_object_unref (subfolders);
384                 
385         return same_subfolder;
386 }
387
388 gboolean 
389 modest_tny_folder_is_ancestor (TnyFolder *folder,
390                                TnyFolderStore *ancestor)
391 {
392         TnyFolderStore *tmp = NULL;
393         gboolean found = FALSE;
394
395         tmp = TNY_FOLDER_STORE (folder);
396         while (!found && tmp && !TNY_IS_ACCOUNT (tmp)) {
397                 TnyFolderStore *folder_store;
398
399                 folder_store = tny_folder_get_folder_store (TNY_FOLDER (tmp));
400                 if (ancestor == folder_store)
401                         found = TRUE;
402                 else
403                         tmp = folder_store;
404                 g_object_unref (folder_store);
405         }
406         return found;
407 }