1 /* Copyright (c) 2007, 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.
32 #include <glib/gi18n.h>
34 #include <modest-tny-local-folders-account.h>
35 #include <modest-tny-outbox-account.h>
36 #include <modest-tny-folder.h>
37 #include <tny-merge-folder.h>
38 #include <tny-simple-list.h>
43 G_DEFINE_TYPE (ModestTnyLocalFoldersAccount,
44 modest_tny_local_folders_account,
45 TNY_TYPE_CAMEL_STORE_ACCOUNT);
47 #define TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_TNY_LOCAL_FOLDERS_ACCOUNT, ModestTnyLocalFoldersAccountPrivate))
50 typedef struct _ModestTnyLocalFoldersAccountPrivate ModestTnyLocalFoldersAccountPrivate;
52 struct _ModestTnyLocalFoldersAccountPrivate
54 GSList *list_extra_folders;
58 modest_tny_local_folders_account_dispose (GObject *object)
60 if (G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->dispose)
61 G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->dispose (object);
66 modest_tny_local_folders_account_remove_all_extra_folders (ModestTnyLocalFoldersAccount *store)
68 ModestTnyLocalFoldersAccountPrivate *priv =
69 TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (store);
71 GSList *iter = priv->list_extra_folders;
74 TnyFolder *folder = (TnyFolder*)iter->data;
76 g_object_unref (folder);
80 iter = g_slist_next (iter);
83 g_slist_free (priv->list_extra_folders);
84 priv->list_extra_folders = NULL;
88 modest_tny_local_folders_account_finalize (GObject *object)
90 G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->finalize (object);
92 ModestTnyLocalFoldersAccount *self =
93 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (object);
95 modest_tny_local_folders_account_remove_all_extra_folders (self);
99 get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GError **err);
102 get_folders_async (TnyFolderStore *self, TnyList *list, TnyGetFoldersCallback callback, TnyFolderStoreQuery *query, TnyStatusCallback status_callback, gpointer user_data);
105 modest_tny_local_folders_account_class_init (ModestTnyLocalFoldersAccountClass *klass)
107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
109 g_type_class_add_private (klass, sizeof (ModestTnyLocalFoldersAccountPrivate));
111 object_class->dispose = modest_tny_local_folders_account_dispose;
112 object_class->finalize = modest_tny_local_folders_account_finalize;
114 /* Override virtual functions from the parent class: */
115 TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->get_folders_func = get_folders;
116 TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->get_folders_async_func = get_folders_async;
120 modest_tny_local_folders_account_init (ModestTnyLocalFoldersAccount *self)
124 ModestTnyLocalFoldersAccount*
125 modest_tny_local_folders_account_new (void)
127 return g_object_new (MODEST_TYPE_TNY_LOCAL_FOLDERS_ACCOUNT, NULL);
131 modest_tny_local_folders_account_add_extra_folder (ModestTnyLocalFoldersAccount *store,
134 ModestTnyLocalFoldersAccountPrivate *priv =
135 TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (store);
137 /* Check that it isn't already in the list: */
138 GSList *exists = g_slist_find (priv->list_extra_folders, folder);
143 /* The reference is released in finalize: */
144 priv->list_extra_folders = g_slist_append (priv->list_extra_folders, folder);
145 g_object_ref (folder);
149 get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GError **err)
151 ModestTnyLocalFoldersAccountPrivate *priv =
152 TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
154 /* Call the base class implementation: */
155 TnyCamelStoreAccountClass *parent_class = g_type_class_peek_parent (
156 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
157 parent_class->get_folders_func (self, list, query, err);
159 /* Add our extra folders: */
160 GSList *iter = priv->list_extra_folders;
163 TnyFolder *folder = TNY_FOLDER (iter->data);
165 tny_list_append (list, G_OBJECT (folder));
167 iter = g_slist_next (iter);
172 get_folders_async (TnyFolderStore *self, TnyList *list, TnyGetFoldersCallback callback, TnyFolderStoreQuery *query, TnyStatusCallback status_callback, gpointer user_data)
174 /* Call the base class implementation: */
175 TnyCamelStoreAccountClass *parent_class = g_type_class_peek_parent (
176 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
177 parent_class->get_folders_async_func (self, list, callback, query, status_callback, user_data);
181 add_account_folders_to_merged_folder (TnyAccount *account, TnyMergeFolder* merge_folder)
183 const gchar* account_id = tny_account_get_id (account);
184 const gboolean is_actual_local_folders_account = account_id &&
185 (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
187 TnyList *list_outbox_folders = tny_simple_list_new ();
188 tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
189 list_outbox_folders, NULL, NULL);
191 TnyIterator* iter = tny_list_create_iterator (list_outbox_folders);
192 while (!tny_iterator_is_done (iter))
194 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
198 /* TODO: Do not add outboxes that are inside local-folders/,
199 * because these are just left-over from earlier Modest versions
200 * that put the outbox there: */
201 if (is_actual_local_folders_account) {
202 const TnyFolderType type = modest_tny_folder_get_local_folder_type (folder);
203 if (type == TNY_FOLDER_TYPE_OUTBOX) {
209 tny_merge_folder_add_folder (merge_folder, folder);
211 g_object_unref (folder);
214 tny_iterator_next (iter);
217 g_object_unref (list_outbox_folders);
220 void modest_tny_local_folders_account_add_merged_outbox_folders (ModestTnyLocalFoldersAccount *self,
223 modest_tny_local_folders_account_remove_all_extra_folders (self);
225 /* All per-account outbox folders are merged into one folders
226 * so that they appear as one outbox to the user: */
227 TnyMergeFolder *merged_outbox = TNY_MERGE_FOLDER (tny_merge_folder_new());
229 GSList *iter = accounts;
232 TnyAccount *account = TNY_ACCOUNT (iter->data);
234 /* Add both outbox account and local-folders account folders
235 * to our one combined account:
237 if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
238 /* Add the folder to the merged folder.
239 * We will add it later to the virtual local-folders store: */
240 add_account_folders_to_merged_folder (account, merged_outbox);
244 iter = g_slist_next (iter);
247 /* Add the merged outbox folder to the virtual local-folders store: */
248 modest_tny_local_folders_account_add_extra_folder (self, TNY_FOLDER(merged_outbox));
249 g_object_unref (merged_outbox);
250 merged_outbox = NULL;