source: trunk/third/gnome-vfs/libgnomevfs/gnome-vfs-directory-filter.c @ 17128

Revision 17128, 6.8 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17127, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2/* gnome-vfs-directory-filter.c - Directory filter for the GNOME
3   Virtual File System.
4
5   Copyright (C) 1999 Free Software Foundation
6
7   The Gnome Library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Library General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   The Gnome Library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   Library General Public License for more details.
16
17   You should have received a copy of the GNU Library General Public
18   License along with the Gnome Library; see the file COPYING.LIB.  If not,
19   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.
21
22   Author: Ettore Perazzoli <ettore@comm2000.it> */
23
24#include <config.h>
25#include "gnome-vfs-directory-filter.h"
26
27#include "gnome-vfs.h"
28#include "gnome-vfs-private.h"
29#include <string.h>
30
31
32struct GnomeVFSDirectoryFilter {
33        /* Filter type.  */
34        GnomeVFSDirectoryFilterType type;
35
36        /* Filter options.  */
37        GnomeVFSDirectoryFilterOptions options;
38
39        /* The pattern.  */
40        gchar *pattern;
41
42        /* Filter function (only with
43           `gnome_vfs_directory_filter_new_custom()'.  If it returns
44           `FALSE', the item must be discarded, otherwise it must be
45           accepted.  */
46        GnomeVFSDirectoryFilterFunc func;
47
48        /* Data to pass to the filter function.  */
49        gpointer data;
50
51        /* Bit array specifying what kind of information the filter
52           needs to select files.  */
53        GnomeVFSDirectoryFilterNeeds needs;
54};
55
56
57static gboolean
58common_filter (const GnomeVFSDirectoryFilter *filter,
59               GnomeVFSFileInfo *info)
60{
61        GnomeVFSDirectoryFilterOptions options;
62
63        options = filter->options;
64
65        if (info->type != GNOME_VFS_FILE_TYPE_DIRECTORY) {
66                if (options & GNOME_VFS_DIRECTORY_FILTER_DIRSONLY)
67                        return FALSE;
68        } else {
69                if (options & GNOME_VFS_DIRECTORY_FILTER_NODIRS)
70                        return FALSE;
71        }
72
73        if (info->name[0] == '.') {
74                if (options & GNOME_VFS_DIRECTORY_FILTER_NODOTFILES) {
75                        return FALSE;
76                }
77
78            if ((options & GNOME_VFS_DIRECTORY_FILTER_NOSELFDIR)
79                && info->name[1] == 0)
80                    return FALSE;
81
82            if ((options & GNOME_VFS_DIRECTORY_FILTER_NOPARENTDIR)
83                && info->name[1] == '.' && info->name[2] == 0)
84                    return FALSE;
85        }
86
87        if (info->name[strlen (info->name) - 1] == '~') {
88                if (options & GNOME_VFS_DIRECTORY_FILTER_NOBACKUPFILES) {
89                        return FALSE;
90                }
91        }
92        return TRUE;
93}
94
95
96/**
97 * gnome_vfs_directory_filter_new:
98 * @type: Filter type
99 * @options: Options for the filter
100 * @pattern: Pattern for name-based selection
101 *
102 * Create a new directory filter.
103 *
104 * Return value: A pointer to the newly created filter.
105 **/
106GnomeVFSDirectoryFilter *
107gnome_vfs_directory_filter_new (GnomeVFSDirectoryFilterType type,
108                                GnomeVFSDirectoryFilterOptions options,
109                                const gchar *pattern)
110{
111        GnomeVFSDirectoryFilter *new;
112
113        if (type == GNOME_VFS_DIRECTORY_FILTER_NONE && options == 0)
114                return NULL;
115
116        new = g_new (GnomeVFSDirectoryFilter, 1);
117
118        new->type = type;
119        new->options = options;
120
121        if (pattern != NULL)
122                new->pattern = g_strdup (pattern);
123        else
124                new->pattern = NULL;
125
126        new->func = NULL;
127
128        switch (type) {
129        case GNOME_VFS_DIRECTORY_FILTER_REGEXP:
130                new->data = gnome_vfs_regexp_filter_new (pattern,
131                                                         options);
132                break;
133        case GNOME_VFS_DIRECTORY_FILTER_SHELLPATTERN:
134                new->data = gnome_vfs_shellpattern_filter_new (pattern,
135                                                               options);
136                break;
137        default:
138                break;
139        }
140
141        new->needs = GNOME_VFS_DIRECTORY_FILTER_NEEDS_NAME;
142        if (new->options & GNOME_VFS_DIRECTORY_FILTER_NODIRS)
143                new->needs |= GNOME_VFS_DIRECTORY_FILTER_NEEDS_TYPE;
144
145        return new;
146}
147
148/**
149 * gnome_vfs_directory_filter_new_custom:
150 * @func: Function to call to evaluate whether a file must pass the filter or
151 * not
152 * @needs: Bitmask representing the information that @func needs to decide
153 * if a file passes through the filter or not.
154 * @func_data: Additional data to be passed to @func
155 *
156 * Create a new custom directory filter.  Whenever the filter is applied, @func
157 * will be called with info about the file to be filtered and @func_data as
158 * parameters.  If @func returns %TRUE, the file passes the filter; if it
159 * returns %FALSE, it does not.
160 *
161 * Return value: A pointer to the newly created filter.
162 **/
163GnomeVFSDirectoryFilter *
164gnome_vfs_directory_filter_new_custom (GnomeVFSDirectoryFilterFunc func,
165                                       GnomeVFSDirectoryFilterNeeds needs,
166                                       gpointer func_data)
167{
168        GnomeVFSDirectoryFilter *new;
169
170        new = g_new (GnomeVFSDirectoryFilter, 1);
171
172        new->type = GNOME_VFS_DIRECTORY_FILTER_NONE;
173        new->options = GNOME_VFS_DIRECTORY_FILTER_DEFAULT;
174        new->pattern = NULL;
175
176        new->func = func;
177        new->data = func_data;
178        new->needs = needs;
179
180        return new;
181}
182
183/**
184 * gnome_vfs_directory_filter_destroy:
185 * @filter: A directory filter
186 *
187 * Destroy @filter.
188 **/
189void
190gnome_vfs_directory_filter_destroy (GnomeVFSDirectoryFilter *filter)
191{
192        if (filter == NULL)
193                return;
194
195        switch (filter->type) {
196        case GNOME_VFS_DIRECTORY_FILTER_REGEXP:
197                gnome_vfs_regexp_filter_destroy
198                        ((GnomeVFSRegexpFilter *) filter->data);
199                break;
200        case GNOME_VFS_DIRECTORY_FILTER_SHELLPATTERN:
201                gnome_vfs_shellpattern_filter_destroy
202                        ((GnomeVFSShellpatternFilter *) filter->data);
203                break;
204        default:
205                break;
206        }
207
208        g_free (filter->pattern);
209        g_free (filter);
210}
211
212/**
213 * gnome_vfs_directory_filter_apply:
214 * @filter: A directory filter
215 * @info: Information about the file to be filtered
216 *
217 * Apply @filter to the file whose info is @info.
218 *
219 * Return value: TRUE if the file passes through the filter, FALSE otherwise.
220 **/
221gboolean
222gnome_vfs_directory_filter_apply (const GnomeVFSDirectoryFilter *filter,
223                                  GnomeVFSFileInfo *info)
224{
225        g_return_val_if_fail (info != NULL, FALSE);
226
227        if (filter == NULL)
228                return TRUE;
229
230        if (filter->func != NULL)
231                return filter->func (info, filter->data);
232
233        if (! common_filter (filter, info))
234                return FALSE;
235
236        switch (filter->type) {
237        case GNOME_VFS_DIRECTORY_FILTER_REGEXP:
238                return gnome_vfs_regexp_filter_apply
239                        ((GnomeVFSRegexpFilter *) filter->data, info);
240        case GNOME_VFS_DIRECTORY_FILTER_SHELLPATTERN:
241                return gnome_vfs_shellpattern_filter_apply
242                        ((GnomeVFSShellpatternFilter *) filter->data, info);
243        default:
244                break;
245        }
246
247        return TRUE;
248}
249
250/**
251 * gnome_vfs_directory_filter_get_needs:
252 * @filter: A directory filter
253 *
254 * Check what kind of information the filter needs to select files.
255 *
256 * Return value: A bitmask representing the information needed by the filter to
257 * perform selection.
258 **/
259GnomeVFSDirectoryFilterNeeds
260gnome_vfs_directory_filter_get_needs (const GnomeVFSDirectoryFilter *filter)
261{
262        g_return_val_if_fail (filter != NULL,
263                              GNOME_VFS_DIRECTORY_FILTER_NEEDS_NOTHING);
264
265        return filter->needs;
266}
Note: See TracBrowser for help on using the repository browser.