Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / exchange / storage / exchange-hierarchy-somedav.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 2002-2004 Novell, Inc.
4  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* ExchangeHierarchySomeDAV: class for a hierarchy consisting of a
21  * specific group of WebDAV folders
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "exchange-hierarchy-somedav.h"
29 #include "exchange-account.h"
30 #include "e-folder-exchange.h"
31 #include "e2k-propnames.h"
32 #include "e2k-uri.h"
33 #include "e2k-utils.h"
34
35 #include <libedataserver/e-xml-hash-utils.h>
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 struct _ExchangeHierarchySomeDAVPrivate {
42         gboolean scanned;
43 };
44
45 enum {
46         HREF_UNREADABLE,
47         LAST_SIGNAL
48 };
49
50 static guint signals [LAST_SIGNAL] = { 0 };
51
52 #define PARENT_TYPE EXCHANGE_TYPE_HIERARCHY_WEBDAV
53 static ExchangeHierarchyWebDAVClass *parent_class = NULL;
54
55 static ExchangeAccountFolderResult scan_subtree (ExchangeHierarchy *hier,
56                                                  EFolder *folder,
57                                                  int mode);
58 static void finalize (GObject *object);
59
60 static void
61 class_init (GObjectClass *object_class)
62 {
63         ExchangeHierarchyClass *exchange_hierarchy_class =
64                 EXCHANGE_HIERARCHY_CLASS (object_class);
65
66         parent_class = g_type_class_ref (PARENT_TYPE);
67
68         /* virtual method override */
69         object_class->finalize = finalize;
70
71         exchange_hierarchy_class->scan_subtree   = scan_subtree;
72
73         /* signals */
74         signals[HREF_UNREADABLE] =
75                 g_signal_new ("href_unreadable",
76                               G_OBJECT_CLASS_TYPE (object_class),
77                               G_SIGNAL_RUN_LAST,
78                               G_STRUCT_OFFSET (ExchangeHierarchySomeDAVClass, href_unreadable),
79                               NULL, NULL,
80                               g_cclosure_marshal_VOID__STRING,
81                               G_TYPE_NONE, 1,
82                               G_TYPE_STRING);
83 }
84
85 static void
86 init (GObject *object)
87 {
88         ExchangeHierarchySomeDAV *hsd = EXCHANGE_HIERARCHY_SOMEDAV (object);
89
90         hsd->priv = g_new0 (ExchangeHierarchySomeDAVPrivate, 1);
91 }
92
93 static void
94 finalize (GObject *object)
95 {
96         ExchangeHierarchySomeDAV *hsd = EXCHANGE_HIERARCHY_SOMEDAV (object);
97
98         g_free (hsd->priv);
99
100         G_OBJECT_CLASS (parent_class)->finalize (object);
101 }
102
103 E2K_MAKE_TYPE (exchange_hierarchy_somedav, ExchangeHierarchySomeDAV, class_init, init, PARENT_TYPE)
104
105
106 static inline gboolean
107 folder_is_unreadable (E2kProperties *props)
108 {
109         char *access;
110
111         access = e2k_properties_get_prop (props, PR_ACCESS);
112         return !access || !atoi (access);
113 }
114
115 static const char *folder_props[] = {
116         E2K_PR_EXCHANGE_FOLDER_CLASS,
117         E2K_PR_HTTPMAIL_UNREAD_COUNT,
118         E2K_PR_DAV_DISPLAY_NAME,
119         PR_ACCESS
120 };
121 static const int n_folder_props = sizeof (folder_props) / sizeof (folder_props[0]);
122
123 static ExchangeAccountFolderResult
124 scan_subtree (ExchangeHierarchy *hier, EFolder *folder, int mode)
125 {
126         ExchangeHierarchySomeDAV *hsd = EXCHANGE_HIERARCHY_SOMEDAV (hier);
127         GPtrArray *hrefs;
128         E2kResultIter *iter;
129         E2kResult *result;
130         int folders_returned=0, folders_added=0, i;
131         E2kHTTPStatus status;
132         ExchangeAccountFolderResult folder_result;
133         EFolder *iter_folder = NULL;
134
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;
142
143         if (mode == OFFLINE_MODE)
144                 return EXCHANGE_ACCOUNT_FOLDER_OK;
145
146         hrefs = exchange_hierarchy_somedav_get_hrefs (hsd);
147         if (!hrefs)
148                 return EXCHANGE_ACCOUNT_FOLDER_OK;
149         if (!hrefs->len) {
150                 g_ptr_array_free (hrefs, TRUE);
151                 return EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
152         }
153
154         iter = e_folder_exchange_bpropfind_start (hier->toplevel, NULL,
155                                                   (const char **)hrefs->pdata,
156                                                   hrefs->len,
157                                                   folder_props,
158                                                   n_folder_props);
159
160         while ((result = e2k_result_iter_next (iter))) {
161                 folders_returned++;
162
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.
167                  */
168                 if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (result->status) ||
169                     folder_is_unreadable (result->props)) {
170                         exchange_hierarchy_somedav_href_unreadable (hsd, result->href);
171                         continue;
172                 }
173
174                 folders_added++;
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);
180         }
181         status = e2k_result_iter_free (iter);
182
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;
187         else
188                 folder_result = exchange_hierarchy_webdav_status_to_folder_result (status);
189
190         for (i = 0; i < hrefs->len; i++)
191                 g_free (hrefs->pdata[i]);
192         g_ptr_array_free (hrefs, TRUE);
193
194         return folder_result;
195 }
196
197
198 GPtrArray *
199 exchange_hierarchy_somedav_get_hrefs (ExchangeHierarchySomeDAV *hsd)
200 {
201         g_return_val_if_fail (EXCHANGE_IS_HIERARCHY_SOMEDAV (hsd), NULL);
202
203         return EXCHANGE_GET_HIERARCHY_SOMEDAV_CLASS (hsd)->get_hrefs (hsd);
204 }
205
206 void
207 exchange_hierarchy_somedav_href_unreadable (ExchangeHierarchySomeDAV *hsd,
208                                             const char *href)
209 {
210         g_return_if_fail (EXCHANGE_IS_HIERARCHY_SOMEDAV (hsd));
211         g_return_if_fail (href != NULL);
212
213         g_signal_emit (hsd, signals[HREF_UNREADABLE], 0, href);
214 }
215
216 ExchangeAccountFolderResult
217 exchange_hierarchy_somedav_add_folder (ExchangeHierarchySomeDAV *hsd,
218                                        const char *uri)
219 {
220         ExchangeHierarchyWebDAV *hwd;
221         ExchangeHierarchy *hier;
222         E2kContext *ctx;
223         E2kHTTPStatus status;
224         E2kResult *results;
225         int nresults = 0;
226         EFolder *folder;
227
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);
232          
233         hwd = EXCHANGE_HIERARCHY_WEBDAV (hsd);
234         hier = EXCHANGE_HIERARCHY (hsd);
235         ctx = exchange_account_get_context (hier->account);
236
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);
242
243         if (nresults == 0)
244                 return EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
245
246         if (folder_is_unreadable (results[0].props)) {
247                 e2k_results_free (results, nresults);
248                 return EXCHANGE_ACCOUNT_FOLDER_PERMISSION_DENIED;
249         }
250
251         folder = exchange_hierarchy_webdav_parse_folder (hwd, hier->toplevel,
252                                                          &results[0]);
253         e2k_results_free (results, nresults);
254
255         if (!folder)
256                 return EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST;
257
258         exchange_hierarchy_new_folder (hier, folder);
259         g_object_unref (folder);
260         return EXCHANGE_ACCOUNT_FOLDER_OK;
261 }