~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to servers/exchange/storage/e-storage.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
 
/* e-storage.c
3
 
 *
4
 
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or
7
 
 * modify it under the terms of version 2 of the GNU Lesser General Public
8
 
 * License as published by the Free Software Foundation.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this program; if not, write to the
17
 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
 
 * Boston, MA 02110-1301, USA.
19
 
 *
20
 
 * Author: Ettore Perazzoli
21
 
 */
22
 
 
23
 
#ifdef HAVE_CONFIG_H
24
 
#include <config.h>
25
 
#endif
26
 
 
27
 
#include "e-storage.h"
28
 
 
29
 
#include "e-folder-tree.h"
30
 
 
31
 
#include <glib/gi18n-lib.h>
32
 
#include <libedataserver/e-data-server-util.h>
33
 
 
34
 
#include <string.h>
35
 
 
36
 
#define PARENT_TYPE G_TYPE_OBJECT
37
 
static GObjectClass *parent_class = NULL;
38
 
 
39
 
G_DEFINE_TYPE (EStorage, e_storage, G_TYPE_OBJECT)
40
 
 
41
 
struct EStoragePrivate {
42
 
        /* The set of folders we have in this storage.  */
43
 
        EFolderTree *folder_tree;
44
 
 
45
 
        /* Internal name of the storage */
46
 
        gchar *name;
47
 
};
48
 
 
49
 
enum {
50
 
        NEW_FOLDER,
51
 
        UPDATED_FOLDER,
52
 
        REMOVED_FOLDER,
53
 
        LAST_SIGNAL
54
 
};
55
 
 
56
 
static guint signals[LAST_SIGNAL] = { 0 };
57
 
 
58
 
/* Destroy notification function for the folders in the tree.  */
59
 
 
60
 
static void
61
 
folder_destroy_notify (EFolderTree *tree,
62
 
                       const gchar *path,
63
 
                       gpointer data,
64
 
                       gpointer closure)
65
 
{
66
 
        EFolder *e_folder;
67
 
 
68
 
        if (data == NULL) {
69
 
                /* The root folder has no EFolder associated to it.  */
70
 
                return;
71
 
        }
72
 
 
73
 
        e_folder = E_FOLDER (data);
74
 
        g_object_unref (e_folder);
75
 
}
76
 
 
77
 
/* Signal callbacks for the EFolders.  */
78
 
 
79
 
static void
80
 
folder_changed_cb (EFolder *folder,
81
 
                   gpointer data)
82
 
{
83
 
        EStorage *storage;
84
 
        EStoragePrivate *priv;
85
 
        const gchar *path, *p;
86
 
        gboolean highlight;
87
 
 
88
 
        g_assert (E_IS_STORAGE (data));
89
 
 
90
 
        storage = E_STORAGE (data);
91
 
        priv = storage->priv;
92
 
 
93
 
        path = e_folder_tree_get_path_for_data (priv->folder_tree, folder);
94
 
        g_assert (path != NULL);
95
 
 
96
 
        g_signal_emit (storage, signals[UPDATED_FOLDER], 0, path);
97
 
 
98
 
        highlight = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (folder), "last_highlight"));
99
 
        if (highlight != e_folder_get_highlighted (folder)) {
100
 
                highlight = !highlight;
101
 
                g_object_set_data (G_OBJECT (folder), "last_highlight", GINT_TO_POINTER (highlight));
102
 
                p = strrchr (path, '/');
103
 
                if (p && p != path) {
104
 
                        gchar *name;
105
 
 
106
 
                        name = g_strndup (path, p - path);
107
 
                        folder = e_folder_tree_get_folder (priv->folder_tree, name);
108
 
                        g_free (name);
109
 
                        if (folder)
110
 
                                e_folder_set_child_highlight (folder, highlight);
111
 
                }
112
 
        }
113
 
}
114
 
 
115
 
/* GObject methods.  */
116
 
 
117
 
static void
118
 
impl_finalize (GObject *object)
119
 
{
120
 
        EStorage *storage;
121
 
        EStoragePrivate *priv;
122
 
 
123
 
        storage = E_STORAGE (object);
124
 
        priv = storage->priv;
125
 
 
126
 
        if (priv->folder_tree != NULL)
127
 
                e_folder_tree_destroy (priv->folder_tree);
128
 
 
129
 
        g_free (priv->name);
130
 
 
131
 
        g_free (priv);
132
 
 
133
 
        (* G_OBJECT_CLASS (parent_class)->finalize) (object);
134
 
}
135
 
 
136
 
/* EStorage methods.  */
137
 
 
138
 
static GList *
139
 
impl_get_subfolder_paths (EStorage *storage,
140
 
                          const gchar *path)
141
 
{
142
 
        EStoragePrivate *priv;
143
 
 
144
 
        priv = storage->priv;
145
 
 
146
 
        return e_folder_tree_get_subfolders (priv->folder_tree, path);
147
 
}
148
 
 
149
 
static EFolder *
150
 
impl_get_folder (EStorage *storage,
151
 
                 const gchar *path)
152
 
{
153
 
        EStoragePrivate *priv;
154
 
        EFolder *e_folder;
155
 
 
156
 
        priv = storage->priv;
157
 
 
158
 
        e_folder = (EFolder *) e_folder_tree_get_folder (priv->folder_tree, path);
159
 
 
160
 
        return e_folder;
161
 
}
162
 
 
163
 
static const gchar *
164
 
impl_get_name (EStorage *storage)
165
 
{
166
 
        return storage->priv->name;
167
 
}
168
 
 
169
 
static void
170
 
impl_async_create_folder (EStorage *storage,
171
 
                          const gchar *path,
172
 
                          const gchar *type,
173
 
                          EStorageResultCallback callback,
174
 
                          gpointer data)
175
 
{
176
 
        (* callback) (storage, E_STORAGE_NOTIMPLEMENTED, data);
177
 
}
178
 
 
179
 
static void
180
 
impl_async_remove_folder (EStorage *storage,
181
 
                          const gchar *path,
182
 
                          EStorageResultCallback callback,
183
 
                          gpointer data)
184
 
{
185
 
        (* callback) (storage, E_STORAGE_NOTIMPLEMENTED, data);
186
 
}
187
 
 
188
 
static void
189
 
impl_async_xfer_folder (EStorage *storage,
190
 
                        const gchar *source_path,
191
 
                        const gchar *destination_path,
192
 
                        gboolean remove_source,
193
 
                        EStorageResultCallback callback,
194
 
                        gpointer data)
195
 
{
196
 
        (* callback) (storage, E_STORAGE_NOTIMPLEMENTED, data);
197
 
}
198
 
 
199
 
static void
200
 
impl_async_open_folder (EStorage *storage,
201
 
                        const gchar *path,
202
 
                        EStorageDiscoveryCallback callback,
203
 
                        gpointer data)
204
 
{
205
 
        (* callback) (storage, E_STORAGE_NOTIMPLEMENTED, NULL, data);
206
 
}
207
 
 
208
 
static gboolean
209
 
impl_will_accept_folder (EStorage *storage,
210
 
                         EFolder *new_parent,
211
 
                         EFolder *source)
212
 
{
213
 
        EStoragePrivate *priv = storage->priv;
214
 
        const gchar *parent_path, *source_path;
215
 
        gint source_len;
216
 
 
217
 
        /* By default, we only disallow dragging a folder into
218
 
         * a subfolder of itself.
219
 
         */
220
 
 
221
 
        if (new_parent == source)
222
 
                return FALSE;
223
 
 
224
 
        parent_path = e_folder_tree_get_path_for_data (priv->folder_tree,
225
 
                                                       new_parent);
226
 
        source_path = e_folder_tree_get_path_for_data (priv->folder_tree,
227
 
                                                       source);
228
 
        if (!parent_path || !source_path)
229
 
                return FALSE;
230
 
 
231
 
        source_len = strlen (source_path);
232
 
        if (!strncmp (parent_path, source_path, source_len) &&
233
 
            parent_path[source_len] == '/')
234
 
                return FALSE;
235
 
 
236
 
        return TRUE;
237
 
}
238
 
 
239
 
static void
240
 
impl_async_discover_shared_folder (EStorage *storage,
241
 
                                   const gchar *owner,
242
 
                                   const gchar *folder_name,
243
 
                                   EStorageDiscoveryCallback callback,
244
 
                                   gpointer data)
245
 
{
246
 
        (* callback) (storage, E_STORAGE_NOTIMPLEMENTED, NULL, data);
247
 
}
248
 
 
249
 
static void
250
 
impl_async_remove_shared_folder (EStorage *storage,
251
 
                                 const gchar *path,
252
 
                                 EStorageResultCallback callback,
253
 
                                 gpointer data)
254
 
{
255
 
        (* callback) (storage, E_STORAGE_NOTIMPLEMENTED, data);
256
 
}
257
 
 
258
 
/* Initialization.  */
259
 
 
260
 
static void
261
 
e_storage_class_init (EStorageClass *class)
262
 
{
263
 
        GObjectClass *object_class;
264
 
 
265
 
        object_class = G_OBJECT_CLASS (class);
266
 
        parent_class = g_type_class_ref (PARENT_TYPE);
267
 
 
268
 
        object_class->finalize = impl_finalize;
269
 
 
270
 
        class->get_subfolder_paths = impl_get_subfolder_paths;
271
 
        class->get_folder          = impl_get_folder;
272
 
        class->get_name            = impl_get_name;
273
 
        class->async_create_folder = impl_async_create_folder;
274
 
        class->async_remove_folder = impl_async_remove_folder;
275
 
        class->async_xfer_folder   = impl_async_xfer_folder;
276
 
        class->async_open_folder   = impl_async_open_folder;
277
 
        class->will_accept_folder  = impl_will_accept_folder;
278
 
 
279
 
        class->async_discover_shared_folder  = impl_async_discover_shared_folder;
280
 
        class->async_remove_shared_folder    = impl_async_remove_shared_folder;
281
 
        signals[NEW_FOLDER] =
282
 
                g_signal_new ("new_folder",
283
 
                              G_OBJECT_CLASS_TYPE (object_class),
284
 
                              G_SIGNAL_RUN_FIRST,
285
 
                              G_STRUCT_OFFSET (EStorageClass, new_folder),
286
 
                              NULL, NULL,
287
 
                              g_cclosure_marshal_VOID__STRING,
288
 
                              G_TYPE_NONE, 1,
289
 
                              G_TYPE_STRING);
290
 
        signals[UPDATED_FOLDER] =
291
 
                g_signal_new ("updated_folder",
292
 
                              G_OBJECT_CLASS_TYPE (object_class),
293
 
                              G_SIGNAL_RUN_FIRST,
294
 
                              G_STRUCT_OFFSET (EStorageClass, updated_folder),
295
 
                              NULL, NULL,
296
 
                              g_cclosure_marshal_VOID__STRING,
297
 
                              G_TYPE_NONE, 1,
298
 
                              G_TYPE_STRING);
299
 
        signals[REMOVED_FOLDER] =
300
 
                g_signal_new ("removed_folder",
301
 
                              G_OBJECT_CLASS_TYPE (object_class),
302
 
                              G_SIGNAL_RUN_FIRST,
303
 
                              G_STRUCT_OFFSET (EStorageClass, removed_folder),
304
 
                              NULL, NULL,
305
 
                              g_cclosure_marshal_VOID__STRING,
306
 
                              G_TYPE_NONE, 1,
307
 
                              G_TYPE_STRING);
308
 
}
309
 
 
310
 
static void
311
 
e_storage_init (EStorage *storage)
312
 
{
313
 
        EStoragePrivate *priv;
314
 
 
315
 
        priv = g_new0 (EStoragePrivate, 1);
316
 
 
317
 
        priv->folder_tree   = e_folder_tree_new (folder_destroy_notify, NULL);
318
 
 
319
 
        storage->priv = priv;
320
 
}
321
 
 
322
 
/* Creation.  */
323
 
 
324
 
void
325
 
e_storage_construct (EStorage *storage,
326
 
                     const gchar *name,
327
 
                     EFolder *root_folder)
328
 
{
329
 
        EStoragePrivate *priv;
330
 
 
331
 
        g_return_if_fail (E_IS_STORAGE (storage));
332
 
 
333
 
        priv = storage->priv;
334
 
 
335
 
        priv->name = g_strdup (name);
336
 
 
337
 
        e_storage_new_folder (storage, "/", root_folder);
338
 
}
339
 
 
340
 
EStorage *
341
 
e_storage_new (const gchar *name,
342
 
               EFolder *root_folder)
343
 
{
344
 
        EStorage *new;
345
 
 
346
 
        new = g_object_new (e_storage_get_type (), NULL);
347
 
 
348
 
        e_storage_construct (new, name, root_folder);
349
 
 
350
 
        return new;
351
 
}
352
 
 
353
 
gboolean
354
 
e_storage_path_is_absolute (const gchar *path)
355
 
{
356
 
        g_return_val_if_fail (path != NULL, FALSE);
357
 
 
358
 
        return *path == '/';
359
 
}
360
 
 
361
 
gboolean
362
 
e_storage_path_is_relative (const gchar *path)
363
 
{
364
 
        g_return_val_if_fail (path != NULL, FALSE);
365
 
 
366
 
        return *path != '/';
367
 
}
368
 
 
369
 
GList *
370
 
e_storage_get_subfolder_paths (EStorage *storage,
371
 
                               const gchar *path)
372
 
{
373
 
        g_return_val_if_fail (E_IS_STORAGE (storage), NULL);
374
 
        g_return_val_if_fail (path != NULL, NULL);
375
 
        g_return_val_if_fail (g_path_is_absolute (path), NULL);
376
 
 
377
 
        return (* E_STORAGE_GET_CLASS (storage)->get_subfolder_paths) (storage, path);
378
 
}
379
 
 
380
 
EFolder *
381
 
e_storage_get_folder (EStorage *storage,
382
 
                      const gchar *path)
383
 
{
384
 
        g_return_val_if_fail (E_IS_STORAGE (storage), NULL);
385
 
        g_return_val_if_fail (path != NULL, NULL);
386
 
        g_return_val_if_fail (e_storage_path_is_absolute (path), NULL);
387
 
 
388
 
        return (* E_STORAGE_GET_CLASS (storage)->get_folder) (storage, path);
389
 
}
390
 
 
391
 
const gchar *
392
 
e_storage_get_name (EStorage *storage)
393
 
{
394
 
        g_return_val_if_fail (E_IS_STORAGE (storage), NULL);
395
 
 
396
 
        return (* E_STORAGE_GET_CLASS (storage)->get_name) (storage);
397
 
}
398
 
 
399
 
/* Folder operations.  */
400
 
 
401
 
void
402
 
e_storage_async_create_folder (EStorage *storage,
403
 
                               const gchar *path,
404
 
                               const gchar *type,
405
 
                               EStorageResultCallback callback,
406
 
                               gpointer data)
407
 
{
408
 
        g_return_if_fail (E_IS_STORAGE (storage));
409
 
        g_return_if_fail (path != NULL);
410
 
        g_return_if_fail (g_path_is_absolute (path));
411
 
        g_return_if_fail (type != NULL);
412
 
        g_return_if_fail (callback != NULL);
413
 
 
414
 
        (* E_STORAGE_GET_CLASS (storage)->async_create_folder) (storage, path, type, callback, data);
415
 
}
416
 
 
417
 
void
418
 
e_storage_async_remove_folder (EStorage              *storage,
419
 
                               const gchar            *path,
420
 
                               EStorageResultCallback callback,
421
 
                               void                  *data)
422
 
{
423
 
        g_return_if_fail (E_IS_STORAGE (storage));
424
 
        g_return_if_fail (path != NULL);
425
 
        g_return_if_fail (g_path_is_absolute (path));
426
 
        g_return_if_fail (callback != NULL);
427
 
 
428
 
        (* E_STORAGE_GET_CLASS (storage)->async_remove_folder) (storage, path, callback, data);
429
 
}
430
 
 
431
 
void
432
 
e_storage_async_xfer_folder (EStorage *storage,
433
 
                             const gchar *source_path,
434
 
                             const gchar *destination_path,
435
 
                             const gboolean remove_source,
436
 
                             EStorageResultCallback callback,
437
 
                             gpointer data)
438
 
{
439
 
        g_return_if_fail (E_IS_STORAGE (storage));
440
 
        g_return_if_fail (source_path != NULL);
441
 
        g_return_if_fail (g_path_is_absolute (source_path));
442
 
        g_return_if_fail (destination_path != NULL);
443
 
        g_return_if_fail (g_path_is_absolute (destination_path));
444
 
 
445
 
        if (strcmp (source_path, destination_path) == 0) {
446
 
                (* callback) (storage, E_STORAGE_OK, data);
447
 
                return;
448
 
        }
449
 
 
450
 
        if (remove_source) {
451
 
                gint destination_len;
452
 
                gint source_len;
453
 
 
454
 
                source_len = strlen (source_path);
455
 
                destination_len = strlen (destination_path);
456
 
 
457
 
                if (source_len < destination_len
458
 
                    && destination_path[source_len] == '/'
459
 
                    && strncmp (destination_path, source_path, source_len) == 0) {
460
 
                        (* callback) (storage, E_STORAGE_CANTMOVETODESCENDANT, data);
461
 
                        return;
462
 
                }
463
 
        }
464
 
 
465
 
        (* E_STORAGE_GET_CLASS (storage)->async_xfer_folder) (storage, source_path, destination_path, remove_source, callback, data);
466
 
}
467
 
 
468
 
void
469
 
e_storage_async_open_folder (EStorage *storage,
470
 
                             const gchar *path,
471
 
                             EStorageDiscoveryCallback callback,
472
 
                             gpointer data)
473
 
{
474
 
        EStoragePrivate *priv;
475
 
        EFolder *folder;
476
 
 
477
 
        g_return_if_fail (E_IS_STORAGE (storage));
478
 
        g_return_if_fail (path != NULL);
479
 
        g_return_if_fail (g_path_is_absolute (path));
480
 
 
481
 
        priv = storage->priv;
482
 
 
483
 
        folder = e_folder_tree_get_folder (priv->folder_tree, path);
484
 
        if (folder == NULL) {
485
 
                (* callback) (storage, E_STORAGE_NOTFOUND, path, data);
486
 
                return;
487
 
        }
488
 
 
489
 
        if (! e_folder_get_has_subfolders (folder)) {
490
 
                (* callback) (storage, E_STORAGE_OK, path, data);
491
 
                return;
492
 
        }
493
 
 
494
 
        (* E_STORAGE_GET_CLASS (storage)->async_open_folder) (storage, path, callback, data);
495
 
}
496
 
 
497
 
gboolean
498
 
e_storage_will_accept_folder (EStorage *storage,
499
 
                              EFolder *new_parent, EFolder *source)
500
 
{
501
 
        g_return_val_if_fail (E_IS_STORAGE (storage), FALSE);
502
 
        g_return_val_if_fail (E_IS_FOLDER (new_parent), FALSE);
503
 
        g_return_val_if_fail (E_IS_FOLDER (source), FALSE);
504
 
 
505
 
        return (* E_STORAGE_GET_CLASS (storage)->will_accept_folder) (storage, new_parent, source);
506
 
}
507
 
 
508
 
/* Shared folders.  */
509
 
 
510
 
void
511
 
e_storage_async_discover_shared_folder (EStorage *storage,
512
 
                                        const gchar *owner,
513
 
                                        const gchar *folder_name,
514
 
                                        EStorageDiscoveryCallback callback,
515
 
                                        gpointer data)
516
 
{
517
 
        g_return_if_fail (E_IS_STORAGE (storage));
518
 
        g_return_if_fail (owner != NULL);
519
 
        g_return_if_fail (folder_name != NULL);
520
 
 
521
 
        (* E_STORAGE_GET_CLASS (storage)->async_discover_shared_folder) (storage, owner, folder_name, callback, data);
522
 
}
523
 
 
524
 
void
525
 
e_storage_cancel_discover_shared_folder (EStorage *storage,
526
 
                                         const gchar *owner,
527
 
                                         const gchar *folder_name)
528
 
{
529
 
        g_return_if_fail (E_IS_STORAGE (storage));
530
 
        g_return_if_fail (owner != NULL);
531
 
        g_return_if_fail (folder_name != NULL);
532
 
        g_return_if_fail (E_STORAGE_GET_CLASS (storage)->cancel_discover_shared_folder != NULL);
533
 
 
534
 
        (* E_STORAGE_GET_CLASS (storage)->cancel_discover_shared_folder) (storage, owner, folder_name);
535
 
}
536
 
 
537
 
void
538
 
e_storage_async_remove_shared_folder (EStorage *storage,
539
 
                                      const gchar *path,
540
 
                                      EStorageResultCallback callback,
541
 
                                      gpointer data)
542
 
{
543
 
        g_return_if_fail (E_IS_STORAGE (storage));
544
 
        g_return_if_fail (path != NULL);
545
 
        g_return_if_fail (g_path_is_absolute (path));
546
 
 
547
 
        (* E_STORAGE_GET_CLASS (storage)->async_remove_shared_folder) (storage, path, callback, data);
548
 
}
549
 
 
550
 
const gchar *
551
 
e_storage_result_to_string (EStorageResult result)
552
 
{
553
 
        switch (result) {
554
 
        case E_STORAGE_OK:
555
 
                return _("No error");
556
 
        case E_STORAGE_GENERICERROR:
557
 
                return _("Generic error");
558
 
        case E_STORAGE_EXISTS:
559
 
                return _("A folder with the same name already exists");
560
 
        case E_STORAGE_INVALIDTYPE:
561
 
                return _("The specified folder type is not valid");
562
 
        case E_STORAGE_IOERROR:
563
 
                return _("I/O error");
564
 
        case E_STORAGE_NOSPACE:
565
 
                return _("Not enough space to create the folder");
566
 
        case E_STORAGE_NOTEMPTY:
567
 
                return _("The folder is not empty");
568
 
        case E_STORAGE_NOTFOUND:
569
 
                return _("The specified folder was not found");
570
 
        case E_STORAGE_NOTIMPLEMENTED:
571
 
                return _("Function not implemented in this storage");
572
 
        case E_STORAGE_PERMISSIONDENIED:
573
 
                return _("Permission denied");
574
 
        case E_STORAGE_UNSUPPORTEDOPERATION:
575
 
                return _("Operation not supported");
576
 
        case E_STORAGE_UNSUPPORTEDTYPE:
577
 
                return _("The specified type is not supported in this storage");
578
 
        case E_STORAGE_CANTCHANGESTOCKFOLDER:
579
 
                return _("The specified folder cannot be modified or removed");
580
 
        case E_STORAGE_CANTMOVETODESCENDANT:
581
 
                return _("Cannot make a folder a child of one of its descendants");
582
 
        case E_STORAGE_INVALIDNAME:
583
 
                return _("Cannot create a folder with that name");
584
 
        case E_STORAGE_NOTONLINE:
585
 
                return _("This operation cannot be performed in off-line mode");
586
 
        default:
587
 
                return _("Unknown error");
588
 
        }
589
 
}
590
 
 
591
 
/* Public utility functions.  */
592
 
 
593
 
typedef struct {
594
 
        const gchar *physical_uri;
595
 
        gchar *retval;
596
 
} GetPathForPhysicalUriForeachData;
597
 
 
598
 
static void
599
 
get_path_for_physical_uri_foreach (EFolderTree *folder_tree,
600
 
                                   const gchar *path,
601
 
                                   gpointer path_data,
602
 
                                   gpointer user_data)
603
 
{
604
 
        GetPathForPhysicalUriForeachData *foreach_data;
605
 
        const gchar *physical_uri;
606
 
        EFolder *e_folder;
607
 
 
608
 
        foreach_data = (GetPathForPhysicalUriForeachData *) user_data;
609
 
        if (foreach_data->retval != NULL)
610
 
                return;
611
 
 
612
 
        e_folder = (EFolder *) path_data;
613
 
        if (e_folder == NULL)
614
 
                return;
615
 
 
616
 
        physical_uri = e_folder_get_physical_uri (e_folder);
617
 
        if (physical_uri == NULL)
618
 
                return;
619
 
 
620
 
        if (strcmp (foreach_data->physical_uri, physical_uri) == 0)
621
 
                foreach_data->retval = g_strdup (path);
622
 
}
623
 
 
624
 
/**
625
 
 * e_storage_get_path_for_physical_uri:
626
 
 * @storage: A storage
627
 
 * @physical_uri: A physical URI
628
 
 *
629
 
 * Look for the folder having the specified @physical_uri.
630
 
 *
631
 
 * Return value: The path of the folder having the specified @physical_uri in
632
 
 * @storage.  If such a folder does not exist, just return NULL.  The return
633
 
 * value must be freed by the caller.
634
 
 **/
635
 
gchar *
636
 
e_storage_get_path_for_physical_uri (EStorage *storage,
637
 
                                     const gchar *physical_uri)
638
 
{
639
 
        GetPathForPhysicalUriForeachData foreach_data;
640
 
        EStoragePrivate *priv;
641
 
 
642
 
        g_return_val_if_fail (E_IS_STORAGE (storage), NULL);
643
 
        g_return_val_if_fail (physical_uri != NULL, NULL);
644
 
 
645
 
        priv = storage->priv;
646
 
 
647
 
        foreach_data.physical_uri = physical_uri;
648
 
        foreach_data.retval       = NULL;
649
 
 
650
 
        e_folder_tree_foreach (priv->folder_tree, get_path_for_physical_uri_foreach, &foreach_data);
651
 
 
652
 
        return foreach_data.retval;
653
 
}
654
 
 
655
 
/* Protected functions.  */
656
 
 
657
 
/* These functions are used by subclasses to add and remove folders from the
658
 
   state stored in the storage object.  */
659
 
 
660
 
static void
661
 
remove_subfolders_except (EStorage *storage, const gchar *path, const gchar *except)
662
 
{
663
 
        EStoragePrivate *priv;
664
 
        GList *subfolders, *f;
665
 
        const gchar *folder_path;
666
 
 
667
 
        priv = storage->priv;
668
 
 
669
 
        subfolders = e_folder_tree_get_subfolders (priv->folder_tree, path);
670
 
        for (f = subfolders; f; f = f->next) {
671
 
                folder_path = f->data;
672
 
                if (!except || strcmp (folder_path, except) != 0)
673
 
                        e_storage_removed_folder (storage, folder_path);
674
 
        }
675
 
        for (f = subfolders; f != NULL; f = f->next)
676
 
                g_free (f->data);
677
 
 
678
 
        g_list_free (subfolders);
679
 
}
680
 
 
681
 
gboolean
682
 
e_storage_new_folder (EStorage *storage,
683
 
                      const gchar *path,
684
 
                      EFolder *e_folder)
685
 
{
686
 
        EStoragePrivate *priv;
687
 
        gchar *parent_path, *p;
688
 
        EFolder *parent;
689
 
 
690
 
        g_return_val_if_fail (E_IS_STORAGE (storage), FALSE);
691
 
        g_return_val_if_fail (path != NULL, FALSE);
692
 
        g_return_val_if_fail (g_path_is_absolute (path), FALSE);
693
 
        g_return_val_if_fail (E_IS_FOLDER (e_folder), FALSE);
694
 
 
695
 
        priv = storage->priv;
696
 
 
697
 
        if (! e_folder_tree_add (priv->folder_tree, path, e_folder))
698
 
                return FALSE;
699
 
 
700
 
        /* If this is the child of a folder that has a pseudo child,
701
 
         * remove the pseudo child now.
702
 
         */
703
 
        p = strrchr (path, '/');
704
 
        if (p && p != path)
705
 
                parent_path = g_strndup (path, p - path);
706
 
        else
707
 
                parent_path = g_strdup ("/");
708
 
        parent = e_folder_tree_get_folder (priv->folder_tree, parent_path);
709
 
        if (parent && e_folder_get_has_subfolders (parent)) {
710
 
                remove_subfolders_except (storage, parent_path, path);
711
 
                e_folder_set_has_subfolders (parent, FALSE);
712
 
        }
713
 
        g_free (parent_path);
714
 
 
715
 
        g_signal_connect_object (e_folder, "changed", G_CALLBACK (folder_changed_cb), storage, 0);
716
 
 
717
 
        g_signal_emit (storage, signals[NEW_FOLDER], 0, path);
718
 
 
719
 
        folder_changed_cb (e_folder, storage);
720
 
 
721
 
        return TRUE;
722
 
}
723
 
 
724
 
/* This really should be called e_storage_set_has_subfolders, but then
725
 
 * it would look like it was an EStorageSet function. (Fact o' the
726
 
 * day: The word "set" has more distinct meanings than any other word
727
 
 * in the English language.) Anyway, we now return you to your
728
 
 * regularly scheduled source code, already in progress.
729
 
 */
730
 
gboolean
731
 
e_storage_declare_has_subfolders (EStorage *storage,
732
 
                                  const gchar *path,
733
 
                                  const gchar *message)
734
 
{
735
 
        EStoragePrivate *priv;
736
 
        EFolder *parent, *pseudofolder;
737
 
        gchar *pseudofolder_path;
738
 
        gboolean ok;
739
 
 
740
 
        g_return_val_if_fail (E_IS_STORAGE (storage), FALSE);
741
 
        g_return_val_if_fail (path != NULL, FALSE);
742
 
        g_return_val_if_fail (g_path_is_absolute (path), FALSE);
743
 
        g_return_val_if_fail (message != NULL, FALSE);
744
 
 
745
 
        priv = storage->priv;
746
 
 
747
 
        parent = e_folder_tree_get_folder (priv->folder_tree, path);
748
 
        if (parent == NULL)
749
 
                return FALSE;
750
 
        if (e_folder_get_has_subfolders (parent))
751
 
                return TRUE;
752
 
 
753
 
        remove_subfolders_except (storage, path, NULL);
754
 
 
755
 
        pseudofolder = e_folder_new (message, "working", "");
756
 
        if (strcmp (path, "/") == 0)
757
 
                pseudofolder_path = g_strdup_printf ("/%s", message);
758
 
        else
759
 
                pseudofolder_path = g_strdup_printf ("%s/%s", path, message);
760
 
        e_folder_set_physical_uri (pseudofolder, pseudofolder_path);
761
 
 
762
 
        ok = e_storage_new_folder (storage, pseudofolder_path, pseudofolder);
763
 
        g_free (pseudofolder_path);
764
 
        if (!ok) {
765
 
                g_object_unref (pseudofolder);
766
 
                return FALSE;
767
 
        }
768
 
 
769
 
        e_folder_set_has_subfolders (parent, TRUE);
770
 
        return TRUE;
771
 
}
772
 
 
773
 
gboolean
774
 
e_storage_get_has_subfolders (EStorage *storage,
775
 
                              const gchar *path)
776
 
{
777
 
        EStoragePrivate *priv;
778
 
        EFolder *folder;
779
 
 
780
 
        g_return_val_if_fail (E_IS_STORAGE (storage), FALSE);
781
 
        g_return_val_if_fail (path != NULL, FALSE);
782
 
        g_return_val_if_fail (g_path_is_absolute (path), FALSE);
783
 
 
784
 
        priv = storage->priv;
785
 
 
786
 
        folder = e_folder_tree_get_folder (priv->folder_tree, path);
787
 
 
788
 
        return folder && e_folder_get_has_subfolders (folder);
789
 
}
790
 
 
791
 
gboolean
792
 
e_storage_removed_folder (EStorage *storage,
793
 
                          const gchar *path)
794
 
{
795
 
        EStoragePrivate *priv;
796
 
        EFolder *folder;
797
 
        const gchar *p;
798
 
 
799
 
        g_return_val_if_fail (E_IS_STORAGE (storage), FALSE);
800
 
        g_return_val_if_fail (path != NULL, FALSE);
801
 
        g_return_val_if_fail (g_path_is_absolute (path), FALSE);
802
 
 
803
 
        priv = storage->priv;
804
 
 
805
 
        folder = e_folder_tree_get_folder (priv->folder_tree, path);
806
 
        if (folder == NULL)
807
 
                return FALSE;
808
 
 
809
 
        p = strrchr (path, '/');
810
 
        if (p != NULL && p != path) {
811
 
                EFolder *parent_folder;
812
 
                gchar *parent_path;
813
 
 
814
 
                parent_path = g_strndup (path, p - path);
815
 
                parent_folder = e_folder_tree_get_folder (priv->folder_tree, parent_path);
816
 
 
817
 
                if (e_folder_get_highlighted (folder))
818
 
                        e_folder_set_child_highlight (parent_folder, FALSE);
819
 
 
820
 
                g_free (parent_path);
821
 
        }
822
 
 
823
 
        g_signal_emit (storage, signals[REMOVED_FOLDER], 0, path);
824
 
 
825
 
        e_folder_tree_remove (priv->folder_tree, path);
826
 
 
827
 
        return TRUE;
828
 
}