"Fossies" - the Fresh Open Source Software Archive 
Member "evolution-mapi-3.46.1/src/libexchangemapi/e-mapi-folder.c" (2 Dec 2022, 10686 Bytes) of package /linux/misc/evolution-mapi-3.46.1.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "e-mapi-folder.c" see the
Fossies "Dox" file reference documentation.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with the program; if not, see <http://www.gnu.org/licenses/>
15 *
16 *
17 * Authors:
18 * Srinivasa Ragavan <sragavan@novell.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #include "evolution-mapi-config.h"
25
26 #include <glib/gi18n-lib.h>
27
28 #include <libedataserver/libedataserver.h>
29
30 #include "e-mapi-connection.h"
31 #include "e-mapi-folder.h"
32 #include "e-mapi-utils.h"
33 #include "e-source-mapi-folder.h"
34
35 #define d(x)
36
37 static struct _folder_type_map {
38 const gchar *container_class;
39 EMapiFolderType folder_type;
40 } folder_type_map[] = {
41 { IPF_APPOINTMENT, E_MAPI_FOLDER_TYPE_APPOINTMENT },
42 { IPF_CONTACT, E_MAPI_FOLDER_TYPE_CONTACT },
43 { IPF_STICKYNOTE, E_MAPI_FOLDER_TYPE_MEMO },
44 { IPF_TASK, E_MAPI_FOLDER_TYPE_TASK },
45 { IPF_NOTE, E_MAPI_FOLDER_TYPE_MAIL },
46 { "IPF.Note.HomePage", E_MAPI_FOLDER_TYPE_NOTE_HOMEPAGE },
47 { IPF_JOURNAL, E_MAPI_FOLDER_TYPE_JOURNAL}
48 };
49
50 EMapiFolderType
51 e_mapi_folder_type_from_string (const gchar *container_class)
52 {
53 gint ii;
54
55 if (!container_class)
56 return E_MAPI_FOLDER_TYPE_UNKNOWN;
57
58 for (ii = 0; ii < G_N_ELEMENTS (folder_type_map); ii++) {
59 if (g_str_equal (folder_type_map[ii].container_class, container_class))
60 return folder_type_map[ii].folder_type;
61 }
62
63 return E_MAPI_FOLDER_TYPE_UNKNOWN;
64 }
65
66 const gchar *
67 e_mapi_folder_type_to_string (EMapiFolderType folder_type)
68 {
69 gint ii;
70
71 for (ii = 0; ii < G_N_ELEMENTS (folder_type_map); ii++) {
72 if (folder_type_map[ii].folder_type == folder_type)
73 return folder_type_map[ii].container_class;
74 }
75
76 return NULL;
77 }
78
79 EMapiFolder *
80 e_mapi_folder_new (const gchar *folder_name,
81 const gchar *container_class,
82 EMapiFolderCategory category,
83 mapi_id_t folder_id,
84 mapi_id_t parent_folder_id,
85 uint32_t child_count,
86 uint32_t unread_count,
87 uint32_t total)
88 {
89 EMapiFolder *folder;
90
91 folder = g_new0 (EMapiFolder, 1);
92 folder->is_default = FALSE;
93 folder->folder_name = g_strdup (folder_name);
94 folder->container_class = e_mapi_folder_type_from_string (container_class);
95 folder->folder_id = folder_id;
96 folder->parent_folder_id = parent_folder_id;
97 folder->child_count = child_count;
98 folder->unread_count = unread_count;
99 folder->total = total;
100 folder->category = category;
101
102 return folder;
103 }
104
105 EMapiFolder *
106 e_mapi_folder_copy (EMapiFolder *src)
107 {
108 EMapiFolder *res;
109
110 g_return_val_if_fail (src != NULL, NULL);
111
112 res = g_new0 (EMapiFolder, 1);
113 *res = *src;
114
115 res->owner_name = g_strdup (src->owner_name);
116 res->owner_email = g_strdup (src->owner_email);
117 res->user_name = g_strdup (src->user_name);
118 res->user_email = g_strdup (src->user_email);
119 res->folder_name = g_strdup (src->folder_name);
120
121 return res;
122 }
123
124 void
125 e_mapi_folder_free (EMapiFolder *folder)
126 {
127 if (folder) {
128 g_free (folder->owner_name);
129 g_free (folder->owner_email);
130 g_free (folder->user_name);
131 g_free (folder->user_email);
132 g_free (folder->folder_name);
133 g_free (folder);
134 }
135 }
136
137 const gchar *
138 e_mapi_folder_get_name (EMapiFolder *folder)
139 {
140 return folder->folder_name;
141 }
142
143 guint64
144 e_mapi_folder_get_id (EMapiFolder *folder)
145 {
146 return folder->folder_id;
147 }
148
149 guint64
150 e_mapi_folder_get_parent_id (EMapiFolder *folder)
151 {
152 return folder->parent_folder_id;
153 }
154
155 gboolean
156 e_mapi_folder_is_root (EMapiFolder *folder)
157 {
158 return (folder->parent_folder_id == 0);
159 }
160
161 EMapiFolderType
162 e_mapi_folder_get_type (EMapiFolder *folder)
163 {
164 return folder->container_class;
165 }
166
167 EMapiFolderCategory
168 e_mapi_folder_get_category (EMapiFolder *folder)
169 {
170 return folder->category;
171 }
172
173 guint32
174 e_mapi_folder_get_unread_count (EMapiFolder *folder)
175 {
176 return folder->unread_count;
177 }
178
179 guint32
180 e_mapi_folder_get_total_count (EMapiFolder *folder)
181 {
182 return folder->total;
183 }
184
185 GSList *
186 e_mapi_folder_copy_list (GSList *folder_list)
187 {
188 GSList *res, *ii;
189
190 res = g_slist_copy (folder_list);
191 for (ii = res; ii; ii = ii->next) {
192 ii->data = e_mapi_folder_copy (ii->data);
193 }
194
195 return res;
196 }
197
198 void
199 e_mapi_folder_free_list (GSList *folder_list)
200 {
201 g_slist_foreach (folder_list, (GFunc) e_mapi_folder_free, NULL);
202 g_slist_free (folder_list);
203 }
204
205 gchar *
206 e_mapi_folder_pick_color_spec (gint move_by,
207 gboolean around_middle)
208 {
209 static gint color_mover = 0;
210 static gint color_indexer = -1;
211 const guint32 colors[] = {
212 0x1464ae, /* dark blue */
213 0x14ae64, /* dark green */
214 0xae1464, /* dark red */
215 0
216 };
217 guint32 color;
218
219 if (move_by <= 0)
220 move_by = 1;
221
222 while (move_by > 0) {
223 move_by--;
224
225 color_indexer++;
226 if (colors[color_indexer] == 0) {
227 color_mover += 1;
228 color_indexer = 0;
229 }
230 }
231
232 color = colors[color_indexer];
233 color = (color & ~(0xFF << (color_indexer * 8))) |
234 (((((color >> (color_indexer * 8)) & 0xFF) + (0x33 * color_mover)) % 0xFF) << (color_indexer * 8));
235
236 if (around_middle) {
237 gint rr, gg, bb, diff;
238
239 rr = (0xFF0000 & color) >> 16;
240 gg = (0x00FF00 & color) >> 8;
241 bb = (0x0000FF & color);
242
243 diff = 0x80 - rr;
244 if (diff < 0x80 - gg)
245 diff = 0x80 - gg;
246 if (diff < 0x80 - bb)
247 diff = 0x80 - bb;
248
249 rr = rr + diff < 0 ? 0 : rr + diff > 0xCC ? 0xCC : rr + diff;
250 gg = gg + diff < 0 ? 0 : gg + diff > 0xCC ? 0xCC : gg + diff;
251 bb = bb + diff < 0 ? 0 : bb + diff > 0xCC ? 0xCC : bb + diff;
252
253 color = (rr << 16) + (gg << 8) + bb;
254 }
255
256 return g_strdup_printf ("#%06x", color);
257 }
258
259 gboolean
260 e_mapi_folder_populate_esource (ESource *source,
261 const GList *sources,
262 EMapiFolderType folder_type,
263 const gchar *profile,
264 gboolean offline_sync,
265 EMapiFolderCategory folder_category,
266 const gchar *foreign_username, /* NULL for public folder */
267 const gchar *folder_name,
268 mapi_id_t folder_id,
269 gint color_seed,
270 GCancellable *cancellable,
271 GError **perror)
272 {
273 ESource *master_source;
274 gboolean res = FALSE;
275
276 master_source = e_mapi_utils_get_master_source (sources, profile);
277
278 if (master_source) {
279 ESourceBackend *backend_ext;
280
281 e_source_set_parent (source, e_source_get_uid (master_source));
282 e_source_set_display_name (source, folder_name);
283
284 switch (folder_type) {
285 case E_MAPI_FOLDER_TYPE_APPOINTMENT:
286 backend_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
287 break;
288 case E_MAPI_FOLDER_TYPE_JOURNAL:
289 case E_MAPI_FOLDER_TYPE_MEMO:
290 backend_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_MEMO_LIST);
291 break;
292 case E_MAPI_FOLDER_TYPE_TASK:
293 backend_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_TASK_LIST);
294 break;
295 case E_MAPI_FOLDER_TYPE_CONTACT:
296 backend_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_ADDRESS_BOOK);
297 break;
298 default:
299 backend_ext = NULL;
300 break;
301 }
302
303 if (backend_ext) {
304 ESourceMapiFolder *folder_ext;
305 ESourceOffline *offline_ext;
306
307 e_source_backend_set_backend_name (backend_ext , "mapi");
308
309 folder_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_MAPI_FOLDER);
310 e_source_mapi_folder_set_id (folder_ext, folder_id);
311 if (folder_category == E_MAPI_FOLDER_CATEGORY_PUBLIC)
312 e_source_mapi_folder_set_is_public (folder_ext, TRUE);
313 else
314 e_source_mapi_folder_set_foreign_username (folder_ext, foreign_username);
315
316 offline_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_OFFLINE);
317 e_source_offline_set_stay_synchronized (offline_ext, offline_sync);
318
319 /* set also color for calendar-like sources */
320 if (folder_type != E_MAPI_FOLDER_TYPE_CONTACT) {
321 gchar *color_str;
322
323 color_str = e_mapi_folder_pick_color_spec (1 + g_list_length ((GList *) sources), folder_type != E_MAPI_FOLDER_TYPE_APPOINTMENT);
324 e_source_selectable_set_color (E_SOURCE_SELECTABLE (backend_ext), color_str);
325 g_free (color_str);
326 }
327
328 res = TRUE;
329 } else {
330 g_propagate_error (perror, g_error_new_literal (E_MAPI_ERROR, MAPI_E_INVALID_PARAMETER, _("Cannot add folder, unsupported folder type")));
331 }
332 } else {
333 g_propagate_error (perror, g_error_new_literal (E_MAPI_ERROR, MAPI_E_INVALID_PARAMETER, _("Cannot add folder, master source not found")));
334 }
335
336 return res;
337 }
338
339 gboolean
340 e_mapi_folder_add_as_esource (ESourceRegistry *pregistry,
341 EMapiFolderType folder_type,
342 const gchar *profile,
343 gboolean offline_sync,
344 EMapiFolderCategory folder_category,
345 const gchar *foreign_username, /* NULL for public folder */
346 const gchar *folder_name,
347 mapi_id_t folder_id,
348 gint color_seed,
349 GCancellable *cancellable,
350 GError **perror)
351 {
352 ESourceRegistry *registry;
353 GList *sources;
354 ESource *source;
355 gboolean res = FALSE;
356
357 registry = pregistry;
358 if (!registry) {
359 registry = e_source_registry_new_sync (cancellable, perror);
360 if (!registry)
361 return FALSE;
362 }
363
364 sources = e_source_registry_list_sources (registry, NULL);
365 source = e_source_new (NULL, NULL, NULL);
366
367 if (e_mapi_folder_populate_esource (
368 source,
369 sources,
370 folder_type,
371 profile,
372 offline_sync,
373 folder_category,
374 foreign_username,
375 folder_name,
376 folder_id,
377 color_seed,
378 cancellable,
379 perror)) {
380 res = e_source_registry_commit_source_sync (registry, source, cancellable, perror);
381 }
382 g_object_unref (source);
383
384 g_list_free_full (sources, g_object_unref);
385 if (!pregistry)
386 g_object_unref (registry);
387
388 return res;
389 }
390
391 gboolean
392 e_mapi_folder_remove_as_esource (ESourceRegistry *pregistry,
393 const gchar *profile,
394 mapi_id_t folder_id,
395 GCancellable *cancellable,
396 GError **perror)
397 {
398 ESourceRegistry *registry;
399 ESource *source;
400 GList *sources;
401 gboolean res = TRUE;
402
403 registry = pregistry;
404 if (!registry) {
405 registry = e_source_registry_new_sync (cancellable, perror);
406 if (!registry)
407 return FALSE;
408 }
409
410 sources = e_source_registry_list_sources (registry, NULL);
411 source = e_mapi_utils_get_source_for_folder (sources, profile, folder_id);
412
413 if (source)
414 res = e_source_remove_sync (source, cancellable, perror);
415
416 g_list_free_full (sources, g_object_unref);
417 if (!pregistry)
418 g_object_unref (registry);
419
420 return res;
421 }
422
423 gboolean
424 e_mapi_folder_is_subscribed_as_esource (const GList *esources,
425 const gchar *profile,
426 mapi_id_t fid)
427 {
428 return e_mapi_utils_get_source_for_folder (esources, profile, fid) != NULL;
429 }