1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #include <glib/gi18n.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>
43 modest_tny_folder_guess_folder_type_from_name (const gchar* name)
48 g_return_val_if_fail (name, TNY_FOLDER_TYPE_UNKNOWN);
50 type = TNY_FOLDER_TYPE_UNKNOWN;
51 folder = g_utf8_strdown (name, strlen(name));
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 if (strcmp (folder, _("mcen_me_folder_outbox")) == 0)
58 type = TNY_FOLDER_TYPE_OUTBOX;
59 // else if (g_str_has_prefix(folder, "junk") ||
60 // g_str_has_prefix(folder, _("junk")))
61 // type = TNY_FOLDER_TYPE_JUNK;
62 // else if (g_str_has_prefix(folder, "trash") ||
63 // g_str_has_prefix(folder, _("trash")))
64 // type = TNY_FOLDER_TYPE_TRASH;
65 else if (strcmp (folder, _("mcen_me_folder_sent")) == 0)
66 type = TNY_FOLDER_TYPE_SENT;
67 else if (strcmp (folder, _("mcen_me_folder_drafts")) == 0)
68 type = TNY_FOLDER_TYPE_DRAFTS;
69 // else if (g_str_has_prefix(folder, "notes") ||
70 // g_str_has_prefix(folder, _("notes")))
71 // type = TNY_FOLDER_TYPE_NOTES;
72 // else if (g_str_has_prefix(folder, "contacts") ||
73 // g_str_has_prefix(folder, _("contacts")))
74 // type = TNY_FOLDER_TYPE_CONTACTS;
75 // else if (g_str_has_prefix(folder, "calendar") ||
76 // g_str_has_prefix(folder, _("calendar")))
77 // type = TNY_FOLDER_TYPE_CALENDAR;
86 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
90 g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_UNKNOWN);
92 if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
93 type = modest_tny_folder_get_local_folder_type ((TnyFolder*)folder);
95 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
97 if (type == TNY_FOLDER_TYPE_UNKNOWN) {
98 const gchar *folder_name;
99 folder_name = tny_folder_get_name (TNY_FOLDER (folder));
100 type = modest_tny_folder_guess_folder_type_from_name (folder_name);
107 /* FIXME: encode all folder rules here */
109 modest_tny_folder_get_rules (TnyFolder *folder)
111 ModestTnyFolderRules rules = 0;
114 g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
116 if (modest_tny_folder_is_local_folder (folder) ||
117 modest_tny_folder_is_memory_card_folder (folder)) {
119 type = modest_tny_folder_get_local_folder_type (folder);
122 case TNY_FOLDER_TYPE_OUTBOX:
123 case TNY_FOLDER_TYPE_SENT:
124 case TNY_FOLDER_TYPE_DRAFTS:
125 rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
126 case TNY_FOLDER_TYPE_INBOX:
127 case TNY_FOLDER_TYPE_JUNK:
128 case TNY_FOLDER_TYPE_TRASH:
129 case TNY_FOLDER_TYPE_ROOT:
130 case TNY_FOLDER_TYPE_NOTES:
131 case TNY_FOLDER_TYPE_CONTACTS:
132 case TNY_FOLDER_TYPE_CALENDAR:
133 case TNY_FOLDER_TYPE_ARCHIVE:
134 case TNY_FOLDER_TYPE_MERGE:
135 case TNY_FOLDER_TYPE_NUM:
136 rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
137 rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
138 rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
143 ModestTransportStoreProtocol proto;
144 TnyFolderType folder_type;
147 account = modest_tny_folder_get_account ((TnyFolder*)folder);
149 return -1; /* no account: nothing is allowed */
151 proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
153 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
158 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
159 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
160 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE |
161 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
164 g_object_unref (G_OBJECT(account));
166 /* Neither INBOX not ROOT folders should me moveable */
167 folder_type = modest_tny_folder_guess_folder_type (folder);
168 if ((folder_type == TNY_FOLDER_TYPE_INBOX) ||
169 (folder_type == TNY_FOLDER_TYPE_ROOT)) {
170 rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
171 rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
172 rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
180 modest_tny_folder_is_local_folder (TnyFolder *folder)
182 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
184 /* The merge folder is a special case,
185 * used to merge the per-account local outbox folders.
186 * and can have no get_account() implementation.
187 * We should do something more sophisticated if we
188 * ever use TnyMergeFolder for anything else.
190 if (TNY_IS_MERGE_FOLDER (folder))
193 TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
197 /* Outbox is a special case, using a derived TnyAccount: */
198 if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
199 g_object_unref (G_OBJECT(account));
203 const gchar* account_id = tny_account_get_id (account);
205 g_object_unref (G_OBJECT(account));
209 g_object_unref (G_OBJECT(account));
210 return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
214 modest_tny_folder_is_memory_card_folder (TnyFolder *folder)
216 g_return_val_if_fail (folder, FALSE);
218 /* The merge folder is a special case,
219 * used to merge the per-account local outbox folders.
220 * and can have no get_account() implementation.
222 if (TNY_IS_MERGE_FOLDER (folder))
225 TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
229 const gchar* account_id = tny_account_get_id (account);
233 g_object_unref (G_OBJECT(account));
235 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
240 modest_tny_folder_get_local_folder_type (TnyFolder *folder)
242 g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
244 /* The merge folder is a special case,
245 * used to merge the per-account local outbox folders.
246 * and can have no get_account() implementation.
247 * We should do something more sophisticated if we
248 * ever use TnyMergeFolder for anything else.
250 if (TNY_IS_MERGE_FOLDER (folder))
251 return TNY_FOLDER_TYPE_OUTBOX;
253 /* Outbox is a special case, using a derived TnyAccount: */
254 TnyAccount* parent_account = tny_folder_get_account (folder);
255 if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
256 g_object_unref (parent_account);
257 return TNY_FOLDER_TYPE_OUTBOX;
260 if (parent_account) {
261 g_object_unref (parent_account);
262 parent_account = NULL;
266 g_return_val_if_fail (modest_tny_folder_is_local_folder(folder),
267 TNY_FOLDER_TYPE_UNKNOWN);
269 /* we need to use the camel functions, because we want the
270 * _full name_, that is, the full path name of the folder,
271 * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
273 CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
275 return TNY_FOLDER_TYPE_UNKNOWN;
277 const gchar *full_name = camel_folder_get_full_name (camel_folder);
278 /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
279 camel_object_unref (CAMEL_OBJECT(camel_folder));
282 return TNY_FOLDER_TYPE_UNKNOWN;
284 return modest_local_folder_info_get_type (full_name);
288 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
290 g_return_val_if_fail(folder, FALSE);
291 g_return_val_if_fail(account, FALSE);
293 if (modest_tny_folder_get_local_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
298 /* we need to use the camel functions, because we want the
299 * _full name_, that is, the full path name of the folder,
300 * to distinguis between 'Outbox' and 'myfunkyfolder/Outbox'
302 CamelFolder *camel_folder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER(folder));
306 const gchar *full_name = camel_folder_get_full_name (camel_folder);
307 camel_object_unref (CAMEL_OBJECT(camel_folder));
310 return TNY_FOLDER_TYPE_UNKNOWN;
312 return modest_local_folder_info_get_type (full_name);
319 modest_tny_folder_get_header_unique_id (TnyHeader *header)
325 g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
327 folder = tny_header_get_folder (header);
331 url = tny_folder_get_url_string (folder);
332 uid = tny_header_get_uid (header);
334 retval = g_strjoin ("/", url, uid, NULL);
337 g_object_unref (folder);
342 TnyAccount *modest_tny_folder_get_account (TnyFolder *folder)
344 TnyAccount *account = NULL;
346 if (TNY_IS_MERGE_FOLDER (folder)) {
347 /* TnyMergeFolder does not support get_account(),
348 * because it could be merging folders from multiple accounts.
349 * So we assume that this is the local folders account: */
351 account = modest_tny_account_store_get_local_folders_account (
352 TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
354 account = tny_folder_get_account (folder);