1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Copyright (C) 2002-2004 Novell, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 /* ExchangeHierarchySomeDAV: class for a hierarchy consisting of a
21 * specific group of WebDAV folders
28 #include "exchange-hierarchy-somedav.h"
29 #include "exchange-account.h"
30 #include "e-folder-exchange.h"
31 #include "e2k-propnames.h"
33 #include "e2k-utils.h"
35 #include <libedataserver/e-xml-hash-utils.h>
41 struct _ExchangeHierarchySomeDAVPrivate {
50 static guint signals [LAST_SIGNAL] = { 0 };
52 #define PARENT_TYPE EXCHANGE_TYPE_HIERARCHY_WEBDAV
53 static ExchangeHierarchyWebDAVClass *parent_class = NULL;
55 static ExchangeAccountFolderResult scan_subtree (ExchangeHierarchy *hier,
58 static void finalize (GObject *object);
61 class_init (GObjectClass *object_class)
63 ExchangeHierarchyClass *exchange_hierarchy_class =
64 EXCHANGE_HIERARCHY_CLASS (object_class);
66 parent_class = g_type_class_ref (PARENT_TYPE);
68 /* virtual method override */
69 object_class->finalize = finalize;
71 exchange_hierarchy_class->scan_subtree = scan_subtree;
74 signals[HREF_UNREADABLE] =
75 g_signal_new ("href_unreadable",
76 G_OBJECT_CLASS_TYPE (object_class),
78 G_STRUCT_OFFSET (ExchangeHierarchySomeDAVClass, href_unreadable),
80 g_cclosure_marshal_VOID__STRING,
86 init (GObject *object)
88 ExchangeHierarchySomeDAV *hsd = EXCHANGE_HIERARCHY_SOMEDAV (object);
90 hsd->priv = g_new0 (ExchangeHierarchySomeDAVPrivate, 1);
94 finalize (GObject *object)
96 ExchangeHierarchySomeDAV *hsd = EXCHANGE_HIERARCHY_SOMEDAV (object);
100 G_OBJECT_CLASS (parent_class)->finalize (object);
103 E2K_MAKE_TYPE (exchange_hierarchy_somedav, ExchangeHierarchySomeDAV, class_init, init, PARENT_TYPE)
106 static inline gboolean
107 folder_is_unreadable (E2kProperties *props)
111 access = e2k_properties_get_prop (props, PR_ACCESS);
112 return !access || !atoi (access);
115 static const char *folder_props[] = {
116 E2K_PR_EXCHANGE_FOLDER_CLASS,
117 E2K_PR_HTTPMAIL_UNREAD_COUNT,
118 E2K_PR_DAV_DISPLAY_NAME,
121 static const int n_folder_props = sizeof (folder_props) / sizeof (folder_props[0]);
123 static ExchangeAccountFolderResult
124 scan_subtree (ExchangeHierarchy *hier, EFolder *folder, int mode)
126 ExchangeHierarchySomeDAV *hsd = EXCHANGE_HIERARCHY_SOMEDAV (hier);
130 int folders_returned=0, folders_added=0, i;
131 E2kHTTPStatus status;
132 ExchangeAccountFolderResult folder_result;
133 EFolder *iter_folder = NULL;
135 /* FIXME : Temporarily allow a rescan of the hierarchy. The proper fix
136 is to handle the folder list either in ExchangeAccount or in the
137 plugins/exchange backend separately by listening to signals.
138 if (hsd->priv->scanned || folder != hier->toplevel) */
139 if (folder != hier->toplevel)
140 return EXCHANGE_ACCOUNT_FOLDER_OK;
141 hsd->priv->scanned = TRUE;
143 if (mode == OFFLINE_MODE)
144 return EXCHANGE_ACCOUNT_FOLDER_OK;
146 hrefs = exchange_hierarchy_somedav_get_hrefs (hsd);
148 return EXCHANGE_ACCOUNT_FOLDER_OK;
150 g_ptr_array_free (hrefs, TRUE);
151 return EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
154 iter = e_folder_exchange_bpropfind_start (hier->toplevel, NULL,
155 (const char **)hrefs->pdata,
160 while ((result = e2k_result_iter_next (iter))) {
163 /* If you have "folder visible" permission but nothing
164 * else, you'll be able to fetch properties, but not
165 * see anything in the folder. In that case, PR_ACCESS
166 * will be 0, and we ignore the folder.
168 if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (result->status) ||
169 folder_is_unreadable (result->props)) {
170 exchange_hierarchy_somedav_href_unreadable (hsd, result->href);
175 iter_folder = exchange_hierarchy_webdav_parse_folder (
176 EXCHANGE_HIERARCHY_WEBDAV (hier),
177 hier->toplevel, result);
178 exchange_hierarchy_new_folder (hier, iter_folder);
179 g_object_unref (iter_folder);
181 status = e2k_result_iter_free (iter);
183 if (folders_returned == 0)
184 folder_result = EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
185 else if (folders_added == 0)
186 folder_result = EXCHANGE_ACCOUNT_FOLDER_PERMISSION_DENIED;
188 folder_result = exchange_hierarchy_webdav_status_to_folder_result (status);
190 for (i = 0; i < hrefs->len; i++)
191 g_free (hrefs->pdata[i]);
192 g_ptr_array_free (hrefs, TRUE);
194 return folder_result;
199 exchange_hierarchy_somedav_get_hrefs (ExchangeHierarchySomeDAV *hsd)
201 g_return_val_if_fail (EXCHANGE_IS_HIERARCHY_SOMEDAV (hsd), NULL);
203 return EXCHANGE_GET_HIERARCHY_SOMEDAV_CLASS (hsd)->get_hrefs (hsd);
207 exchange_hierarchy_somedav_href_unreadable (ExchangeHierarchySomeDAV *hsd,
210 g_return_if_fail (EXCHANGE_IS_HIERARCHY_SOMEDAV (hsd));
211 g_return_if_fail (href != NULL);
213 g_signal_emit (hsd, signals[HREF_UNREADABLE], 0, href);
216 ExchangeAccountFolderResult
217 exchange_hierarchy_somedav_add_folder (ExchangeHierarchySomeDAV *hsd,
220 ExchangeHierarchyWebDAV *hwd;
221 ExchangeHierarchy *hier;
223 E2kHTTPStatus status;
228 g_return_val_if_fail (EXCHANGE_IS_HIERARCHY_SOMEDAV (hsd),
229 EXCHANGE_ACCOUNT_FOLDER_GENERIC_ERROR);
230 g_return_val_if_fail (uri != NULL,
231 EXCHANGE_ACCOUNT_FOLDER_GENERIC_ERROR);
233 hwd = EXCHANGE_HIERARCHY_WEBDAV (hsd);
234 hier = EXCHANGE_HIERARCHY (hsd);
235 ctx = exchange_account_get_context (hier->account);
237 status = e2k_context_propfind (ctx, NULL, uri,
238 folder_props, n_folder_props,
239 &results, &nresults);
240 if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (status))
241 return exchange_hierarchy_webdav_status_to_folder_result (status);
244 return EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
246 if (folder_is_unreadable (results[0].props)) {
247 e2k_results_free (results, nresults);
248 return EXCHANGE_ACCOUNT_FOLDER_PERMISSION_DENIED;
251 folder = exchange_hierarchy_webdav_parse_folder (hwd, hier->toplevel,
253 e2k_results_free (results, nresults);
256 return EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
258 exchange_hierarchy_new_folder (hier, folder);
259 g_object_unref (folder);
260 return EXCHANGE_ACCOUNT_FOLDER_OK;