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-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 #include <modest-text-utils.h>
44 /* make sure you use the *full* name, because foo/drafts is not the same as drafts */
46 modest_tny_folder_guess_folder_type_from_name (const gchar* full_name)
48 g_return_val_if_fail (full_name, TNY_FOLDER_TYPE_INVALID);
50 if (strcmp (full_name, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
51 return TNY_FOLDER_TYPE_OUTBOX;
52 else if (strcmp (full_name, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
53 return TNY_FOLDER_TYPE_DRAFTS;
55 TNY_FOLDER_TYPE_NORMAL;
61 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
65 g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_INVALID);
67 if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
68 type = modest_tny_folder_get_local_or_mmc_folder_type ((TnyFolder*)folder);
70 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
72 if (type == TNY_FOLDER_TYPE_UNKNOWN) {
73 const gchar *folder_name =
74 tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
75 type = modest_tny_folder_guess_folder_type_from_name (folder_name);
78 if (type == TNY_FOLDER_TYPE_INVALID)
79 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
86 modest_tny_folder_get_help_id (const TnyFolder *folder)
89 const gchar* help_id = NULL;
91 g_return_val_if_fail (folder, NULL);
92 g_return_val_if_fail (TNY_IS_FOLDER(folder), NULL);
94 type = modest_tny_folder_guess_folder_type (TNY_FOLDER (folder));
97 case TNY_FOLDER_TYPE_NORMAL: help_id = "applications_email_managefolders"; break;
98 case TNY_FOLDER_TYPE_INBOX: help_id = "applications_email_inbox";break;
99 case TNY_FOLDER_TYPE_OUTBOX: help_id = "applications_email_outbox";break;
100 case TNY_FOLDER_TYPE_SENT: help_id = "applications_email_sent"; break;
101 case TNY_FOLDER_TYPE_DRAFTS: help_id = "applications_email_drafts";break;
102 case TNY_FOLDER_TYPE_ARCHIVE: help_id = "applications_email_managefolders";break;
104 case TNY_FOLDER_TYPE_INVALID: g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);break;
105 default: g_warning ("%s: BUG: unexpected folder type (%d)", __FUNCTION__, type);
113 /* FIXME: encode all folder rules here */
115 modest_tny_folder_get_rules (TnyFolder *folder)
117 ModestTnyFolderRules rules = 0;
120 g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
122 if (modest_tny_folder_is_local_folder (folder) ||
123 modest_tny_folder_is_memory_card_folder (folder)) {
125 type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
126 g_return_val_if_fail (type != TNY_FOLDER_TYPE_INVALID, -1);
129 case TNY_FOLDER_TYPE_OUTBOX:
130 case TNY_FOLDER_TYPE_SENT:
131 case TNY_FOLDER_TYPE_DRAFTS:
132 rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
133 case TNY_FOLDER_TYPE_INBOX:
134 case TNY_FOLDER_TYPE_JUNK:
135 case TNY_FOLDER_TYPE_TRASH:
136 case TNY_FOLDER_TYPE_ROOT:
137 case TNY_FOLDER_TYPE_NOTES:
138 case TNY_FOLDER_TYPE_CONTACTS:
139 case TNY_FOLDER_TYPE_CALENDAR:
140 case TNY_FOLDER_TYPE_ARCHIVE:
141 case TNY_FOLDER_TYPE_MERGE:
142 case TNY_FOLDER_TYPE_NUM:
143 rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
144 rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
145 rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
150 ModestTransportStoreProtocol proto;
151 TnyFolderType folder_type;
154 account = modest_tny_folder_get_account ((TnyFolder*)folder);
156 return -1; /* no account: nothing is allowed */
158 proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
160 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
165 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
166 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
167 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE |
168 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
171 g_object_unref (G_OBJECT(account));
173 /* Neither INBOX nor ROOT, nor ARCHIVE folders should me moveable */
174 folder_type = modest_tny_folder_guess_folder_type (folder);
175 g_return_val_if_fail (folder_type != TNY_FOLDER_TYPE_INVALID, -1);
177 if ((folder_type == TNY_FOLDER_TYPE_INBOX) ||
178 (folder_type == TNY_FOLDER_TYPE_ROOT) ||
179 (folder_type == TNY_FOLDER_TYPE_ARCHIVE)) {
180 rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
181 rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
182 rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
191 modest_tny_folder_is_local_folder (TnyFolder *folder)
193 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
195 /* The merge folder is a special case,
196 * used to merge the per-account local outbox folders.
197 * and can have no get_account() implementation.
198 * We should do something more sophisticated if we
199 * ever use TnyMergeFolder for anything else.
201 if (TNY_IS_MERGE_FOLDER (folder)) {
205 TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
207 g_warning ("folder without account");
211 /* Outbox is a special case, using a derived TnyAccount: */
212 if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
213 //g_warning ("BUG: should not be reached");
214 /* should be handled with the MERGE_FOLDER above*/
215 g_object_unref (G_OBJECT(account));
219 const gchar* account_id = tny_account_get_id (account);
221 g_warning ("BUG: account without account id");
222 g_object_unref (G_OBJECT(account));
226 g_object_unref (G_OBJECT(account));
227 return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
231 modest_tny_folder_is_memory_card_folder (TnyFolder *folder)
233 g_return_val_if_fail (folder, FALSE);
234 g_return_val_if_fail (modest_tny_folder_guess_folder_type (folder) !=
235 TNY_FOLDER_TYPE_INVALID, FALSE);
237 /* The merge folder is a special case,
238 * used to merge the per-account local outbox folders.
239 * and can have no get_account() implementation.
241 if (TNY_IS_MERGE_FOLDER (folder))
244 TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
248 const gchar* account_id = tny_account_get_id (account);
252 g_object_unref (G_OBJECT(account));
254 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
258 modest_tny_folder_is_remote_folder (TnyFolder *folder)
260 gboolean is_local = TRUE;
262 g_return_val_if_fail (folder, FALSE);
264 is_local = ((modest_tny_folder_is_local_folder(folder)) ||
265 (modest_tny_folder_is_memory_card_folder(folder)));
273 modest_tny_folder_get_local_or_mmc_folder_type (TnyFolder *folder)
275 g_return_val_if_fail (folder, TNY_FOLDER_TYPE_INVALID);
276 g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
277 modest_tny_folder_is_memory_card_folder(folder),
278 TNY_FOLDER_TYPE_INVALID);
280 /* The merge folder is a special case,
281 * used to merge the per-account local outbox folders.
282 * and can have no get_account() implementation.
283 * We should do something more sophisticated if we
284 * ever use TnyMergeFolder for anything else.
286 if (TNY_IS_MERGE_FOLDER (folder))
287 return TNY_FOLDER_TYPE_OUTBOX;
289 /* Outbox is a special case, using a derived TnyAccount: */
290 TnyAccount* parent_account = tny_folder_get_account (folder);
291 if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
292 g_object_unref (parent_account);
293 return TNY_FOLDER_TYPE_OUTBOX;
296 if (parent_account) {
297 g_object_unref (parent_account);
298 parent_account = NULL;
301 /* we need to use the camel functions, because we want the
302 * _full name_, that is, the full path name of the folder,
303 * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
306 const gchar *full_name = tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
307 /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
310 return TNY_FOLDER_TYPE_UNKNOWN;
312 return modest_local_folder_info_get_type (full_name);
316 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
318 g_return_val_if_fail(folder, FALSE);
319 g_return_val_if_fail(account, FALSE);
321 if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
328 modest_tny_folder_get_header_unique_id (TnyHeader *header)
334 g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
336 folder = tny_header_get_folder (header);
340 url = tny_folder_get_url_string (folder);
341 uid = tny_header_get_uid (header);
343 retval = g_strjoin ("/", url, uid, NULL);
346 g_object_unref (folder);
352 modest_tny_folder_get_account (TnyFolder *folder)
354 TnyAccount *account = NULL;
356 if (TNY_IS_MERGE_FOLDER (folder)) {
357 /* TnyMergeFolder does not support get_account(),
358 * because it could be merging folders from multiple accounts.
359 * So we assume that this is the local folders account: */
361 account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
363 account = tny_folder_get_account (folder);
370 * It's probably better to use a query to get the folders that match
371 * new_name but currently tinymail only provides a match by name using
372 * regular expressions and we want an exact matching. We're not using
373 * a regular expression for the exact name because we'd need first to
374 * escape @new_name and it's not easy sometimes.
376 * The code that uses the query is available in revision 3152.
379 modest_tny_folder_has_subfolder_with_name (TnyFolderStore *parent,
380 const gchar *new_name,
383 TnyList *subfolders = NULL;
384 TnyIterator *iter = NULL;
385 TnyFolder *folder = NULL;
387 gboolean has_name = FALSE;
389 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), FALSE);
390 g_return_val_if_fail (new_name, FALSE);
392 /* Get direct subfolders */
393 subfolders = tny_simple_list_new ();
394 tny_folder_store_get_folders (parent, subfolders, NULL, &err);
397 iter = tny_list_create_iterator (subfolders);
398 while (!tny_iterator_is_done (iter) && !has_name) {
402 folder = (TnyFolder*)tny_iterator_get_current (iter);
403 if (!folder || ! TNY_IS_FOLDER(folder)) {
404 g_warning ("%s: invalid folder", __FUNCTION__);
408 name = tny_folder_get_name (folder);
410 g_warning ("%s: folder name == NULL", __FUNCTION__);
411 g_object_unref (folder);
415 /* is it simply the same folder name? */
416 if (strcmp (name, new_name) == 0)
418 /* or is it the same when ignoring case (non-strict mode)? */
419 else if (non_strict && modest_text_utils_utf8_strcmp (name, new_name, TRUE) == 0)
421 /* or is the name equal to the display name of some folder, in the current locale? */
422 else if (non_strict) {
423 TnyFolderType type = modest_tny_folder_guess_folder_type (folder);
424 if (type != TNY_FOLDER_TYPE_INVALID && type != TNY_FOLDER_TYPE_NORMAL)
425 has_name = !(modest_text_utils_utf8_strcmp (modest_local_folder_info_get_type_display_name (type),
432 g_object_unref (folder);
433 tny_iterator_next(iter);
438 g_object_unref (iter);
439 if (subfolders != NULL)
440 g_object_unref (subfolders);
446 modest_tny_folder_is_ancestor (TnyFolder *folder,
447 TnyFolderStore *ancestor)
449 TnyFolderStore *tmp = NULL;
450 gboolean found = FALSE;
452 tmp = TNY_FOLDER_STORE (folder);
453 while (!found && tmp && !TNY_IS_ACCOUNT (tmp)) {
454 TnyFolderStore *folder_store;
456 folder_store = tny_folder_get_folder_store (TNY_FOLDER (tmp));
457 if (ancestor == folder_store)
461 g_object_unref (folder_store);