1090461d899a83f77bca593aac57085c894f6c6c
[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 <tny-camel-folder.h>
35 #include <camel/camel-folder.h>
36 #include <modest-protocol-info.h>
37
38 TnyFolderType
39 modest_tny_folder_guess_folder_type_from_name (const gchar* name)
40 {
41         gint  type;
42         gchar *folder;
43
44         g_return_val_if_fail (name, TNY_FOLDER_TYPE_UNKNOWN);
45         
46         type = TNY_FOLDER_TYPE_UNKNOWN;
47         folder = g_utf8_strdown (name, strlen(name));
48
49         if (strcmp (folder, "inbox") == 0 ||
50             strcmp (folder, _("inbox")) == 0 ||
51             strcmp (folder, _("mcen_me_folder_inbox")) == 0)
52                 type = TNY_FOLDER_TYPE_INBOX;
53         else if (strcmp (folder, "outbox") == 0 ||
54                  strcmp (folder, _("outbox")) == 0 ||
55                  strcmp (folder, _("mcen_me_folder_outbox")) == 0)
56                 type = TNY_FOLDER_TYPE_OUTBOX;
57         else if (g_str_has_prefix(folder, "junk") ||
58                  g_str_has_prefix(folder, _("junk")))
59                 type = TNY_FOLDER_TYPE_JUNK;
60         else if (g_str_has_prefix(folder, "trash") ||
61                  g_str_has_prefix(folder, _("trash")))
62                 type = TNY_FOLDER_TYPE_TRASH;
63         else if (g_str_has_prefix(folder, "sent") ||
64                  g_str_has_prefix(folder, _("sent")) ||
65                  strcmp (folder, _("mcen_me_folder_sent")) == 0)
66                 type = TNY_FOLDER_TYPE_SENT;
67         else if (g_str_has_prefix(folder, "draft") ||
68                  g_str_has_prefix(folder, _("draft")) ||
69                  strcmp (folder, _("mcen_me_folder_drafts")) == 0)
70                 type = TNY_FOLDER_TYPE_DRAFTS;
71         else if (g_str_has_prefix(folder, "notes") ||
72                  g_str_has_prefix(folder, _("notes")))
73                 type = TNY_FOLDER_TYPE_NOTES;
74         else if (g_str_has_prefix(folder, "contacts") ||
75                  g_str_has_prefix(folder, _("contacts")))
76                 type = TNY_FOLDER_TYPE_CONTACTS;
77         else if (g_str_has_prefix(folder, "calendar") ||
78                  g_str_has_prefix(folder, _("calendar")))
79                 type = TNY_FOLDER_TYPE_CALENDAR;
80         
81         g_free (folder);
82         return type;
83 }
84
85
86
87 TnyFolderType
88 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
89 {
90         TnyFolderType type;
91
92         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
93
94         type = tny_folder_get_folder_type (TNY_FOLDER (folder));
95         
96         if (type == TNY_FOLDER_TYPE_UNKNOWN) {
97                 const gchar *folder_name;
98
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_folder_rules   (const 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         
118                 type = modest_tny_folder_get_local_folder_type (folder);
119                 
120                 switch (type) {
121                 case TNY_FOLDER_TYPE_DRAFTS:
122                 case TNY_FOLDER_TYPE_OUTBOX:
123                 case TNY_FOLDER_TYPE_SENT:
124                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
125                 case TNY_FOLDER_TYPE_INBOX:
126                 case TNY_FOLDER_TYPE_JUNK:
127                 case TNY_FOLDER_TYPE_TRASH:
128                 default:
129                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
130                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
131                         rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
132                 }
133         } else {
134                 ModestProtocol proto;
135                 TnyAccount *account =
136                         tny_folder_get_account ((TnyFolder*)folder);
137                 if (!account)
138                         return -1; /* no account: nothing is allowed */
139                 
140                 proto = modest_protocol_info_get_protocol (
141                         tny_account_get_proto (account));
142
143                 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
144                         rules = 0;
145                 } else {
146                         /* pop, nntp, ... */
147                         rules =
148                                 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
149                                 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
150                                 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE  |
151                                 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
152
153                 }
154                 g_object_unref (G_OBJECT(account));
155         }
156         return rules;
157 }
158
159
160 gboolean
161 modest_tny_folder_is_local_folder   (const TnyFolder *folder)
162 {
163         TnyAccount*  account;
164         const gchar* account_id;
165         
166         g_return_val_if_fail (folder, FALSE);
167         
168         account = tny_folder_get_account ((TnyFolder*)folder);
169         if (!account)
170                 return FALSE;
171
172         account_id = tny_account_get_id (account);
173         if (!account_id)
174                 return FALSE;
175
176         g_object_unref (G_OBJECT(account));
177         
178         return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
179 }       
180
181
182 TnyFolderType
183 modest_tny_folder_get_local_folder_type  (const TnyFolder *folder)
184 {
185         CamelFolder *camel_folder;
186         const gchar *full_name;
187         
188         g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
189         g_return_val_if_fail (modest_tny_folder_is_local_folder(folder),
190                               TNY_FOLDER_TYPE_UNKNOWN);
191
192         /* we need to use the camel functions, because we want the
193          * _full name_, that is, the full path name of the folder,
194          * to distinguis between 'Outbox' and 'myfunkyfolder/Outbox'
195          */
196         camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
197         if (!camel_folder)
198                 return TNY_FOLDER_TYPE_UNKNOWN;
199
200         full_name = camel_folder_get_full_name (camel_folder);
201         camel_object_unref (CAMEL_OBJECT(camel_folder));
202         
203         if (!full_name) 
204                 return TNY_FOLDER_TYPE_UNKNOWN;
205         else 
206                 return modest_local_folder_info_get_type (full_name);
207 }