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>
43 /* make sure you use the *full* name, because foo/drafts is not the same as drafts */
45 modest_tny_folder_guess_folder_type_from_name (const gchar* full_name)
47 g_return_val_if_fail (full_name, TNY_FOLDER_TYPE_UNKNOWN);
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;
54 TNY_FOLDER_TYPE_UNKNOWN;
60 modest_tny_folder_guess_folder_type (const TnyFolder *folder)
64 g_return_val_if_fail (TNY_IS_FOLDER(folder), TNY_FOLDER_TYPE_UNKNOWN);
66 if (modest_tny_folder_is_local_folder ((TnyFolder*)folder))
67 type = modest_tny_folder_get_local_or_mmc_folder_type ((TnyFolder*)folder);
69 type = tny_folder_get_folder_type (TNY_FOLDER (folder));
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);
81 /* FIXME: encode all folder rules here */
83 modest_tny_folder_get_rules (TnyFolder *folder)
85 ModestTnyFolderRules rules = 0;
88 g_return_val_if_fail (TNY_IS_FOLDER(folder), -1);
90 if (modest_tny_folder_is_local_folder (folder) ||
91 modest_tny_folder_is_memory_card_folder (folder)) {
93 type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
96 case TNY_FOLDER_TYPE_OUTBOX:
97 case TNY_FOLDER_TYPE_SENT:
98 case TNY_FOLDER_TYPE_DRAFTS:
99 rules |= MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
100 case TNY_FOLDER_TYPE_INBOX:
101 case TNY_FOLDER_TYPE_JUNK:
102 case TNY_FOLDER_TYPE_TRASH:
103 case TNY_FOLDER_TYPE_ROOT:
104 case TNY_FOLDER_TYPE_NOTES:
105 case TNY_FOLDER_TYPE_CONTACTS:
106 case TNY_FOLDER_TYPE_CALENDAR:
107 case TNY_FOLDER_TYPE_ARCHIVE:
108 case TNY_FOLDER_TYPE_MERGE:
109 case TNY_FOLDER_TYPE_NUM:
110 rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
111 rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
112 rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
117 ModestTransportStoreProtocol proto;
118 TnyFolderType folder_type;
121 account = modest_tny_folder_get_account ((TnyFolder*)folder);
123 return -1; /* no account: nothing is allowed */
125 proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
127 if (proto == MODEST_PROTOCOL_STORE_IMAP) {
132 MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE |
133 MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE |
134 MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE |
135 MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
138 g_object_unref (G_OBJECT(account));
140 /* Neither INBOX nor ROOT, nor ARCHIVE folders should me moveable */
141 folder_type = modest_tny_folder_guess_folder_type (folder);
142 if ((folder_type == TNY_FOLDER_TYPE_INBOX) ||
143 (folder_type == TNY_FOLDER_TYPE_ROOT) ||
144 (folder_type == TNY_FOLDER_TYPE_ARCHIVE)) {
145 rules |= MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
146 rules |= MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
147 rules |= MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
156 modest_tny_folder_is_local_folder (TnyFolder *folder)
158 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
160 /* The merge folder is a special case,
161 * used to merge the per-account local outbox folders.
162 * and can have no get_account() implementation.
163 * We should do something more sophisticated if we
164 * ever use TnyMergeFolder for anything else.
166 if (TNY_IS_MERGE_FOLDER (folder)) {
170 TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
172 g_warning ("folder without account");
176 /* Outbox is a special case, using a derived TnyAccount: */
177 if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
178 //g_warning ("BUG: should not be reached");
179 /* should be handled with the MERGE_FOLDER above*/
180 g_object_unref (G_OBJECT(account));
184 const gchar* account_id = tny_account_get_id (account);
186 g_warning ("BUG: account without account id");
187 g_object_unref (G_OBJECT(account));
191 g_object_unref (G_OBJECT(account));
192 return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
196 modest_tny_folder_is_memory_card_folder (TnyFolder *folder)
198 g_return_val_if_fail (folder, FALSE);
200 /* The merge folder is a special case,
201 * used to merge the per-account local outbox folders.
202 * and can have no get_account() implementation.
204 if (TNY_IS_MERGE_FOLDER (folder))
207 TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
211 const gchar* account_id = tny_account_get_id (account);
215 g_object_unref (G_OBJECT(account));
217 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
221 modest_tny_folder_is_remote_folder (TnyFolder *folder)
223 gboolean is_local = TRUE;
225 g_return_val_if_fail (folder, FALSE);
227 is_local = ((modest_tny_folder_is_local_folder(folder)) ||
228 (modest_tny_folder_is_memory_card_folder(folder)));
235 modest_tny_folder_get_local_or_mmc_folder_type (TnyFolder *folder)
237 g_return_val_if_fail (folder, TNY_FOLDER_TYPE_UNKNOWN);
238 g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
239 modest_tny_folder_is_memory_card_folder(folder),
240 TNY_FOLDER_TYPE_UNKNOWN);
242 /* The merge folder is a special case,
243 * used to merge the per-account local outbox folders.
244 * and can have no get_account() implementation.
245 * We should do something more sophisticated if we
246 * ever use TnyMergeFolder for anything else.
248 if (TNY_IS_MERGE_FOLDER (folder))
249 return TNY_FOLDER_TYPE_OUTBOX;
251 /* Outbox is a special case, using a derived TnyAccount: */
252 TnyAccount* parent_account = tny_folder_get_account (folder);
253 if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
254 g_object_unref (parent_account);
255 return TNY_FOLDER_TYPE_OUTBOX;
258 if (parent_account) {
259 g_object_unref (parent_account);
260 parent_account = NULL;
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'
268 const gchar *full_name = tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
269 /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
272 return TNY_FOLDER_TYPE_UNKNOWN;
274 return modest_local_folder_info_get_type (full_name);
278 modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
280 g_return_val_if_fail(folder, FALSE);
281 g_return_val_if_fail(account, FALSE);
283 if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
290 modest_tny_folder_get_header_unique_id (TnyHeader *header)
296 g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
298 folder = tny_header_get_folder (header);
302 url = tny_folder_get_url_string (folder);
303 uid = tny_header_get_uid (header);
305 retval = g_strjoin ("/", url, uid, NULL);
308 g_object_unref (folder);
314 modest_tny_folder_get_account (TnyFolder *folder)
316 TnyAccount *account = NULL;
318 if (TNY_IS_MERGE_FOLDER (folder)) {
319 /* TnyMergeFolder does not support get_account(),
320 * because it could be merging folders from multiple accounts.
321 * So we assume that this is the local folders account: */
323 account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
325 account = tny_folder_get_account (folder);
332 * It's probably better to use a query to get the folders that match
333 * new_name but currently tinymail only provides a match by name using
334 * regular expressions and we want an exact matching. We're not using
335 * a regular expression for the exact name because we'd need first to
336 * escape @new_name and it's not easy sometimes.
338 * The code that uses the query is available in revision 3152.
341 modest_tny_folder_has_subfolder_with_name (TnyFolderStore *parent,
342 const gchar *new_name)
344 TnyList *subfolders = NULL;
345 TnyIterator *iter = NULL;
346 TnyFolder *folder = NULL;
348 gboolean same_subfolder = FALSE;
350 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), FALSE);
352 /* Get direct subfolders */
353 subfolders = tny_simple_list_new ();
354 tny_folder_store_get_folders (parent, subfolders, NULL, &err);
357 iter = tny_list_create_iterator (subfolders);
358 while (!tny_iterator_is_done (iter) && !same_subfolder) {
359 const gchar *name = NULL;
361 folder = TNY_FOLDER(tny_iterator_get_current (iter));
362 name = tny_folder_get_name (folder);
364 same_subfolder = !strcmp(name, new_name);
366 g_object_unref (folder);
367 tny_iterator_next(iter);
372 g_object_unref (iter);
373 if (subfolders != NULL)
374 g_object_unref (subfolders);
376 return same_subfolder;
380 modest_tny_folder_is_ancestor (TnyFolder *folder,
381 TnyFolderStore *ancestor)
383 TnyFolderStore *tmp = NULL;
384 gboolean found = FALSE;
386 tmp = TNY_FOLDER_STORE (folder);
387 while (!found && tmp && !TNY_IS_ACCOUNT (tmp)) {
388 TnyFolderStore *folder_store;
390 folder_store = tny_folder_get_folder_store (TNY_FOLDER (tmp));
391 if (ancestor == folder_store)
395 g_object_unref (folder_store);