aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-folder-tree.c
blob: 9962fa508f0ff9cb2c53f294aa01ed43839fe8d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-folder-set.c
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <string.h>

#include "e-folder-tree.h"


struct _Folder {
    struct _Folder *parent;
    char *path;
    void *data;
    GList *subfolders;
};
typedef struct _Folder Folder;

struct _EFolderTree {
    GHashTable *path_to_folder;

    EFolderDestroyNotify folder_destroy_notify;
    void *folder_destroy_notify_closure;
};


/* Utility functions.  */

static char *
get_parent_path (const char *path)
{
    const char *last_separator;

    g_assert (g_path_is_absolute (path));

    last_separator = strrchr (path, G_DIR_SEPARATOR);

    if (last_separator == path)
        return g_strdup (G_DIR_SEPARATOR_S);

    return g_strndup (path, last_separator - path);
}

static void
traverse_subtree (EFolderTree *tree,
          Folder *root_folder,
          EFolderTreeForeachFunc foreach_func,
          void *data)
{
    GList *p;

    g_assert (foreach_func != NULL);

    (* foreach_func) (tree, root_folder->path, root_folder->data, data);

    for (p = root_folder->subfolders; p != NULL; p = p->next) {
        Folder *folder;

        folder = (Folder *) p->data;
        traverse_subtree (tree, folder, foreach_func, data);
    }
}


/* Folder handling.  */

static Folder *
folder_new (const char *path,
        void *data)
{
    Folder *folder;

    folder = g_new (Folder, 1);
    folder->parent     = NULL;
    folder->path       = g_strdup (path);
    folder->data       = data;
    folder->subfolders = NULL;

    return folder;
}

static void
folder_remove_subfolder (Folder *folder,
             Folder *subfolder)
{
    g_list_remove (folder->subfolders, folder);
}

static void
folder_add_subfolder (Folder *folder,
              Folder *subfolder)
{
    folder->subfolders = g_list_prepend (folder->subfolders, subfolder);
    subfolder->parent = folder;
}

static void
folder_destroy (Folder *folder)
{
    g_assert (folder->subfolders == NULL);

    if (folder->parent != NULL)
        folder_remove_subfolder (folder->parent, folder);

    g_free (folder->path);

    g_free (folder);
}

static void
remove_folder (EFolderTree *folder_tree,
           Folder *folder)
{
    if (folder->subfolders != NULL) {
        GList *p;

        for (p = folder->subfolders; p != NULL; p = p->next) {
            Folder *subfolder;

            subfolder = (Folder *) p->data;
            remove_folder (folder_tree, subfolder);
        }

        g_list_free (folder->subfolders);
        folder->subfolders = NULL;
    }

    g_hash_table_remove (folder_tree->path_to_folder, folder->path);

    if (folder_tree->folder_destroy_notify != NULL)
        (* folder_tree->folder_destroy_notify) (folder_tree,
                            folder->path,
                            folder->data,
                            folder_tree->folder_destroy_notify_closure);

    folder_destroy (folder);
}


/**
 * e_folder_tree_new:
 * @folder_destroy_notify: Function to be called when a folder gets removed from the tree
 * @closure: Additional data to pass to @folder_destroy_notify
 * 
 * Create a new EFolderTree.
 * 
 * Return value: A pointer to the newly created EFolderTree.
 **/
EFolderTree *
e_folder_tree_new (EFolderDestroyNotify folder_destroy_notify,
           void *closure)
{
    EFolderTree *new;
    Folder *root_folder;

    new = g_new (EFolderTree, 1);

    new->folder_destroy_notify         = folder_destroy_notify;
    new->folder_destroy_notify_closure = closure;

    new->path_to_folder = g_hash_table_new (g_str_hash, g_str_equal);

    root_folder = folder_new (G_DIR_SEPARATOR_S, NULL);
    g_hash_table_insert (new->path_to_folder, root_folder->path, root_folder);

    return new;
}

/**
 * e_folder_tree_destroy:
 * @folder_tree: A pointer to an EFolderTree
 * 
 * Destroy @folder_tree.
 **/
void
e_folder_tree_destroy (EFolderTree *folder_tree)
{
    Folder *root_folder;

    g_return_if_fail (folder_tree != NULL);

    root_folder = g_hash_table_lookup (folder_tree->path_to_folder, G_DIR_SEPARATOR_S);
    remove_folder (folder_tree, root_folder);

    g_hash_table_destroy (folder_tree->path_to_folder);

    g_free (folder_tree);
}

/**
 * e_folder_tree_add:
 * @folder_tree: A pointer to an EFolderTree
 * @path: Path at which the new folder must be added
 * @data: Data associated with the new folder
 * 
 * Insert a new folder at @path, with the specified @data.
 *
 * Return value: %TRUE if successful, %FALSE if failed.
 **/
gboolean
e_folder_tree_add (EFolderTree *folder_tree,
           const char *path,
           void *data)
{
    Folder *parent_folder;
    Folder *folder;
    char *parent_path;

    g_return_val_if_fail (folder_tree != NULL, FALSE);
    g_return_val_if_fail (path != NULL, FALSE);
    g_return_val_if_fail (g_path_is_absolute (path), FALSE);

    parent_path = get_parent_path (path);

    parent_folder = g_hash_table_lookup (folder_tree->path_to_folder, parent_path);
    if (parent_folder == NULL) {
        g_warning ("%s: Trying to add a subfolder to a path that does not exist yet -- %s",
               __FUNCTION__, parent_path);
        return FALSE;
    }

    folder = g_hash_table_lookup (folder_tree->path_to_folder, path);
    if (folder != NULL) {
        g_warning ("%s: Trying to add a subfolder for a path that already exists -- %s",
               __FUNCTION__, path);
        return FALSE;
    }

    folder = folder_new (path, data);
    folder_add_subfolder (parent_folder, folder);

    g_hash_table_insert (folder_tree->path_to_folder, folder->path, folder);

    g_free (parent_path);

    return TRUE;
}

/**
 * e_folder_tree_remove:
 * @folder_tree: A pointer to an EFolderTree
 * @path: Path of the folder to remove
 * 
 * Remove the folder at @path from @folder_tree.
 *
 * Return value: %TRUE if successful, %FALSE if failed.
 **/
gboolean
e_folder_tree_remove (EFolderTree *folder_tree,
              const char *path)
{
    Folder *folder;

    g_return_val_if_fail (folder_tree != NULL, FALSE);
    g_return_val_if_fail (path != NULL, FALSE);
    g_return_val_if_fail (g_path_is_absolute (path), FALSE);

    folder = g_hash_table_lookup (folder_tree->path_to_folder, path);
    if (folder == NULL)
        return FALSE;

    remove_folder (folder_tree, folder);
    return TRUE;
}

/**
 * e_folder_tree_get_folder:
 * @folder_tree: A pointer to an EFolderTree
 * @path: Path of the folder for which we want to get the data
 * 
 * Get the data for the folder at @path.
 * 
 * Return value: The pointer to the data for the folder at @path.
 **/
void *
e_folder_tree_get_folder (EFolderTree *folder_tree,
              const char *path)
{
    Folder *folder;

    g_return_val_if_fail (folder_tree != NULL, NULL);
    g_return_val_if_fail (path != NULL, NULL);
    g_return_val_if_fail (g_path_is_absolute (path), NULL);

    folder = g_hash_table_lookup (folder_tree->path_to_folder, path);
    if (folder == NULL)
        return NULL;

    return folder->data;
}

/**
 * e_folder_tree_get_subfolders:
 * @folder_tree: A pointer to an EFolderTree
 * @path: A path in @folder_tree
 * 
 * Get a list of the paths of the subfolders of @path.
 * 
 * Return value: A list of pointers to the paths of the subfolders.  The list
 * and the strings must be freed by the caller.
 **/
GList *
e_folder_tree_get_subfolders (EFolderTree *folder_tree,
                  const char *path)
{
    Folder *folder;
    GList *list;
    GList *p;

    g_return_val_if_fail (folder_tree != NULL, NULL);
    g_return_val_if_fail (path != NULL, NULL);
    g_return_val_if_fail (g_path_is_absolute (path), NULL);

    folder = g_hash_table_lookup (folder_tree->path_to_folder, path);
    if (folder == NULL)
        return NULL;

    list = NULL;
    for (p = folder->subfolders; p != NULL; p = p->next) {
        const Folder *folder;

        folder = (const Folder *) p->data;
        list = g_list_prepend (list, g_strdup (folder->path));
    }

    return list;
}


/**
 * e_folder_tree_foreach:
 * @folder_tree: 
 * @foreach_func: 
 * @data: 
 * 
 * Call @foreach_func with the specified @data for all the folders
 * in @folder_tree, starting at the root node.
 **/
void
e_folder_tree_foreach (EFolderTree *folder_tree,
               EFolderTreeForeachFunc foreach_func,
               void *data)
{
    Folder *root_node;

    g_return_if_fail (folder_tree != NULL);
    g_return_if_fail (foreach_func != NULL);

    root_node = g_hash_table_lookup (folder_tree->path_to_folder,
                     G_DIR_SEPARATOR_S);
    if (root_node == NULL) {
        g_warning ("e_folder_tree_foreach -- What?!  No root node!?");
        return;
    }

    traverse_subtree (folder_tree, root_node, foreach_func, data);
}
5924b372ec4b429a1b2609d536'>Merge some patches from gchemutils SVN to fix the build with goffice 0.7.5.marcus2009-05-055-3/+274 * Add some more goffice TYPE name conversions.marcus2009-05-046-0/+89 * Fix another TYPE name change with goffice 0.7.5.marcus2009-05-041-0/+20 * Add another goffice-0.7.5 build fix.marcus2009-05-031-0/+11 * Fix the build with goffice 0.7.5.marcus2009-05-031-0/+11 * Chase the goffice update.marcus2009-05-032-4/+4 * - Update to 1.1.0miwi2009-05-014-42/+7 * Update to 2.2.1makc2009-04-266-20/+23 * Fix build with NOPORTDOCS, NOPORTEXAMPLES.maho2009-04-241-2/+2 * Machine Learning PY (mlpy) is a high-performance Python package formiwi2009-04-245-0/+125 * Update to 3.4.0.maho2009-04-217-720/+862 * Fix build on amd64.maho2009-04-132-0/+22 * - Update to 6.02leeym2009-04-122-22/+11 * 2009-03-22 science/oof: Broken with GCC 4.2 and beyondmiwi2009-04-114-90/+0 * - Fix DEPRECATED outputmiwi2009-04-111-0/+1 * - Upgrade to 5.7.3thierry2009-04-106-26/+208 * - Update maintainer mail addressdhn2009-04-064-4/+4 * Use NOPORTEXAMPLES and %%PORTEXAMPLES%% where appropiate to fix packaging withitetcu2009-04-05