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.
34 #include <glib/gi18n.h>
35 #include <tny-error.h>
36 #include <modest-tny-local-folders-account.h>
37 #include <modest-tny-outbox-account.h>
38 #include <modest-tny-folder.h>
39 #include <tny-camel-folder.h>
40 #include <tny-merge-folder.h>
41 #include <tny-simple-list.h>
42 #include <tny-gtk-lockable.h>
44 G_DEFINE_TYPE (ModestTnyLocalFoldersAccount,
45 modest_tny_local_folders_account,
46 TNY_TYPE_CAMEL_STORE_ACCOUNT);
48 #define TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE(o) \
49 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_TNY_LOCAL_FOLDERS_ACCOUNT, ModestTnyLocalFoldersAccountPrivate))
51 typedef struct _ModestTnyLocalFoldersAccountPrivate ModestTnyLocalFoldersAccountPrivate;
53 struct _ModestTnyLocalFoldersAccountPrivate
55 TnyMergeFolder *outbox_folder;
58 static void get_folders (TnyFolderStore *self,
60 TnyFolderStoreQuery *query,
63 static TnyFolder* create_folder (TnyFolderStore *self,
68 modest_tny_local_folders_account_finalize (GObject *object)
70 G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->finalize (object);
74 modest_tny_local_folders_account_class_init (ModestTnyLocalFoldersAccountClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 g_type_class_add_private (klass, sizeof (ModestTnyLocalFoldersAccountPrivate));
80 object_class->finalize = modest_tny_local_folders_account_finalize;
82 /* Override virtual functions from the parent class: */
83 TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->get_folders_func = get_folders;
84 TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->create_folder_func = create_folder;
88 modest_tny_local_folders_account_init (ModestTnyLocalFoldersAccount *self)
93 ModestTnyLocalFoldersAccount*
94 modest_tny_local_folders_account_new (void)
96 return g_object_new (MODEST_TYPE_TNY_LOCAL_FOLDERS_ACCOUNT, NULL);
99 /**********************************************************/
100 /* TnyCamelStoreAccount functions redefinitions */
101 /**********************************************************/
103 modest_tny_local_folders_account_query_passes (TnyFolderStoreQuery *query, TnyFolder *folder)
105 gboolean retval = FALSE;
107 if (query && (tny_list_get_length (tny_folder_store_query_get_items (query)) > 0)) {
108 TnyList *items = tny_folder_store_query_get_items (query);
109 TnyIterator *iterator;
110 iterator = tny_list_create_iterator (items);
112 while (!tny_iterator_is_done (iterator))
114 TnyFolderStoreQueryItem *item = (TnyFolderStoreQueryItem*) tny_iterator_get_current (iterator);
116 TnyFolderStoreQueryOption options = tny_folder_store_query_item_get_options (item);
117 const regex_t *regex = tny_folder_store_query_item_get_regex (item);
119 if ((options & TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED) &&
120 tny_folder_is_subscribed (folder))
123 if ((options & TNY_FOLDER_STORE_QUERY_OPTION_UNSUBSCRIBED) &&
124 !(tny_folder_is_subscribed (folder)))
127 if (regex && options & TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_NAME)
128 if (regexec (regex, tny_folder_get_name (folder), 0, NULL, 0) == 0)
131 if (regex && options & TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_ID)
132 if (regexec (regex, tny_folder_get_id (folder), 0, NULL, 0) == 0)
135 g_object_unref (G_OBJECT (item));
138 tny_iterator_next (iterator);
141 g_object_unref (G_OBJECT (iterator));
142 g_object_unref (G_OBJECT (items));
150 get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GError **err)
152 TnyCamelStoreAccountClass *parent_class;
153 ModestTnyLocalFoldersAccountPrivate *priv;
155 /* Call the base class implementation: */
156 parent_class = g_type_class_peek_parent (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
157 parent_class->get_folders_func (self, list, query, err);
159 /* Add our extra folder only if it passes the query */
160 priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
162 if (priv->outbox_folder &&
163 modest_tny_local_folders_account_query_passes (query, TNY_FOLDER (priv->outbox_folder)))
164 tny_list_prepend (list, G_OBJECT (priv->outbox_folder));
168 create_folder (TnyFolderStore *self,
172 TnyCamelStoreAccountClass *parent_class;
174 parent_class = g_type_class_peek_parent (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
176 /* If the folder name is been used by our extra folders */
177 if (modest_tny_local_folders_account_folder_name_in_use (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (self), name)) {
178 g_set_error (err, TNY_FOLDER_STORE_ERROR,
179 TNY_FOLDER_STORE_ERROR_CREATE_FOLDER,
180 "Folder name already in use");
184 /* Call the base class implementation: */
185 return parent_class->create_folder_func (self, name, err);
188 /*****************************/
190 /*****************************/
192 modest_tny_local_folders_account_folder_name_in_use (ModestTnyLocalFoldersAccount *self,
195 ModestTnyLocalFoldersAccountPrivate *priv;
197 const gchar *type_name;
200 /* Check that we're not trying to create/rename any folder
201 with the same name that our OUTBOX, DRAFT, SENT */
202 priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
203 down_name = g_utf8_strdown (name, strlen (name));
205 type_name = modest_local_folder_info_get_type_name (TNY_FOLDER_TYPE_OUTBOX);
206 if (!strcmp (type_name, down_name)) {
209 type_name = modest_local_folder_info_get_type_name (TNY_FOLDER_TYPE_DRAFTS);
210 if (!strcmp (type_name, down_name)) {
213 type_name = modest_local_folder_info_get_type_name (TNY_FOLDER_TYPE_SENT);
214 if (!strcmp (type_name, down_name)) {
227 modest_tny_local_folders_account_add_folder_to_outbox (ModestTnyLocalFoldersAccount *self,
228 TnyFolder *per_account_outbox)
230 ModestTnyLocalFoldersAccountPrivate *priv;
232 g_return_if_fail (MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self));
233 g_return_if_fail (TNY_IS_FOLDER (per_account_outbox));
235 /* We can not test it yet, because there is no API to set the
237 /* g_return_if_fail (tny_folder_get_folder_type (per_account_outbox) == TNY_FOLDER_TYPE_OUTBOX); */
239 priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
241 /* Create on-demand */
242 if (!priv->outbox_folder) {
243 priv->outbox_folder = TNY_MERGE_FOLDER (tny_merge_folder_new_with_ui_locker (_("mcen_me_folder_outbox"), tny_gtk_lockable_new ()));
245 /* Set type to outbox */
246 tny_merge_folder_set_folder_type (priv->outbox_folder, TNY_FOLDER_TYPE_OUTBOX);
249 /* Add outbox to the global OUTBOX folder */
250 tny_merge_folder_add_folder (priv->outbox_folder, per_account_outbox);
254 modest_tny_local_folders_account_remove_folder_from_outbox (ModestTnyLocalFoldersAccount *self,
255 TnyFolder *per_account_outbox)
257 ModestTnyLocalFoldersAccountPrivate *priv;
258 TnyList *merged_folders = NULL;
260 g_return_if_fail (MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self));
261 g_return_if_fail (TNY_IS_FOLDER (per_account_outbox));
263 priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
265 /* Remove outbox from the global OUTBOX folder */
266 tny_merge_folder_remove_folder (priv->outbox_folder, per_account_outbox);
268 /* If there is no folder in the outbox the delete it */
269 merged_folders = tny_simple_list_new ();
270 tny_merge_folder_get_folders (priv->outbox_folder, merged_folders);
271 if (tny_list_get_length (merged_folders) == 0) {
272 g_object_unref (priv->outbox_folder);
273 priv->outbox_folder = NULL;
275 g_object_unref (merged_folders);