]> err.no Git - sope/blob - sope-core/NGExtensions/NGFileFolderInfoDataSource.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / NGFileFolderInfoDataSource.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <NGExtensions/NGFileFolderInfoDataSource.h>
23 #import <EOControl/EOFetchSpecification.h>
24 #import <EOControl/EOQualifier.h>
25 #import <EOControl/EOSortOrdering.h>
26 #include "common.h"
27
28 NGExtensions_DECLARE NSString *NSFileName      = @"NSFileName";
29 NGExtensions_DECLARE NSString *NSFilePath      = @"NSFilePath";
30 NGExtensions_DECLARE NSString *NSParentPath    = @"NSParentPath";
31 NGExtensions_DECLARE NSString *NSTraverseLinks = @"NSTraverseLinks";
32
33 @implementation NGFileFolderInfoDataSource
34
35 - (id)initWithFolderPath:(NSString *)_path {
36   if ((self = [self init])) {
37     self->folderPath = [_path copy];
38   }
39   return self;
40 }
41
42 - (void)dealloc {
43   [self->fspec      release];
44   [self->folderPath release];
45   [super dealloc];
46 }
47
48 /* accessors */
49
50 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
51   ASSIGN(self->fspec, _fspec);
52 }
53 - (EOFetchSpecification *)fetchSpecification {
54   return self->fspec;
55 }
56
57 /* operations */
58
59 - (NSArray *)_attributesForPaths:(NSEnumerator *)_paths
60   filterUsingQualifier:(EOQualifier *)_q
61   fileManager:(NSFileManager *)_fm
62 {
63   NSMutableArray      *ma;
64   NSMutableDictionary *workArea;
65   NSArray             *result;
66   NSString            *path;
67   BOOL                tlinks;
68   
69   ma       = [NSMutableArray arrayWithCapacity:256];
70   workArea = [NSMutableDictionary dictionaryWithCapacity:32];
71   
72   tlinks = [[[self->fspec hints] objectForKey:@"NSTraverseLinks"] boolValue];
73   
74   while ((path = [_paths nextObject])) {
75     NSDictionary *record;
76     NSString     *fullPath;
77     
78     fullPath = [self->folderPath stringByAppendingPathComponent:path];
79     
80     [workArea setDictionary:
81                 [_fm fileAttributesAtPath:fullPath traverseLink:tlinks]];
82     [workArea setObject:path             forKey:@"NSFileName"];
83     [workArea setObject:fullPath         forKey:@"NSFilePath"];
84     [workArea setObject:self->folderPath forKey:@"NSParentPath"];
85     
86     record = [[workArea copy] autorelease];
87     
88     if (_q) {
89       if (![(id<EOQualifierEvaluation>)_q evaluateWithObject:record])
90         /* filter out */
91         continue;
92     }
93
94     /* add to result set */
95     [ma addObject:record];
96   }
97   
98   result = [[ma copy] autorelease];
99   return result;
100 }
101
102 - (NSArray *)_fetchObjectsFromFileManager:(NSFileManager *)_fm {
103   NSAutoreleasePool *pool;
104   BOOL        isDir;
105   NSArray     *array;
106   NSArray     *sortOrderings;
107
108   if (![_fm fileExistsAtPath:self->folderPath isDirectory:&isDir])
109     /* path does not exist */
110     return nil;
111   if (!isDir)
112     /* path is not a directory */
113     return nil;
114   
115   pool = [[NSAutoreleasePool alloc] init];
116   
117   array = [_fm directoryContentsAtPath:self->folderPath];
118   
119   if ([array count] == 0) {
120     /* no directory contents */
121     array = [array retain];
122     [pool release];
123     return [array autorelease];
124   }
125
126   array = [self _attributesForPaths:[array objectEnumerator]
127                 filterUsingQualifier:[self->fspec qualifier]
128                 fileManager:_fm];
129   
130   if ((sortOrderings = [self->fspec sortOrderings]))
131     /* sort set */
132     array = [array sortedArrayUsingKeyOrderArray:sortOrderings];
133   
134   array = [array retain];
135   [pool release];
136   
137   return [array autorelease];
138 }
139
140 - (NSArray *)fetchObjects {
141   NSFileManager *fm;
142   
143   fm = [NSFileManager defaultManager];
144   return [self _fetchObjectsFromFileManager:fm];
145 }
146
147 @end /* NGFileInfoDataSource */