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