]> err.no Git - sope/blob - sope-core/NGExtensions/NGFileFolderInfoDataSource.m
fixed to URL processing
[sope] / sope-core / NGExtensions / NGFileFolderInfoDataSource.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include <NGExtensions/NGFileFolderInfoDataSource.h>
24 #import <EOControl/EOFetchSpecification.h>
25 #import <EOControl/EOQualifier.h>
26 #import <EOControl/EOSortOrdering.h>
27 #include "common.h"
28
29 NGExtensions_DECLARE NSString *NSFileName      = @"NSFileName";
30 NGExtensions_DECLARE NSString *NSFilePath      = @"NSFilePath";
31 NGExtensions_DECLARE NSString *NSParentPath    = @"NSParentPath";
32 NGExtensions_DECLARE NSString *NSTraverseLinks = @"NSTraverseLinks";
33
34 @implementation NGFileFolderInfoDataSource
35
36 - (id)initWithFolderPath:(NSString *)_path {
37   if ((self = [self init])) {
38     self->folderPath = [_path copy];
39   }
40   return self;
41 }
42
43 - (void)dealloc {
44   [self->fspec      release];
45   [self->folderPath release];
46   [super dealloc];
47 }
48
49 /* accessors */
50
51 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
52   ASSIGN(self->fspec, _fspec);
53 }
54 - (EOFetchSpecification *)fetchSpecification {
55   return self->fspec;
56 }
57
58 /* operations */
59
60 - (NSArray *)_attributesForPaths:(NSEnumerator *)_paths
61   filterUsingQualifier:(EOQualifier *)_q
62   fileManager:(NSFileManager *)_fm
63 {
64   NSMutableArray      *ma;
65   NSMutableDictionary *workArea;
66   NSArray             *result;
67   NSString            *path;
68   BOOL                tlinks;
69   
70   ma       = [NSMutableArray arrayWithCapacity:256];
71   workArea = [NSMutableDictionary dictionaryWithCapacity:32];
72   
73   tlinks = [[[self->fspec hints] objectForKey:@"NSTraverseLinks"] boolValue];
74   
75   while ((path = [_paths nextObject])) {
76     NSDictionary *record;
77     NSString     *fullPath;
78     
79     fullPath = [self->folderPath stringByAppendingPathComponent:path];
80     
81     [workArea setDictionary:
82                 [_fm fileAttributesAtPath:fullPath traverseLink:tlinks]];
83     [workArea setObject:path             forKey:@"NSFileName"];
84     [workArea setObject:fullPath         forKey:@"NSFilePath"];
85     [workArea setObject:self->folderPath forKey:@"NSParentPath"];
86     
87     record = [[workArea copy] autorelease];
88     
89     if (_q) {
90       if (![(id<EOQualifierEvaluation>)_q evaluateWithObject:record])
91         /* filter out */
92         continue;
93     }
94
95     /* add to result set */
96     [ma addObject:record];
97   }
98   
99   result = [[ma copy] autorelease];
100   return result;
101 }
102
103 - (NSArray *)_fetchObjectsFromFileManager:(NSFileManager *)_fm {
104   NSAutoreleasePool *pool;
105   BOOL        isDir;
106   NSArray     *array;
107   NSArray     *sortOrderings;
108
109   if (![_fm fileExistsAtPath:self->folderPath isDirectory:&isDir])
110     /* path does not exist */
111     return nil;
112   if (!isDir)
113     /* path is not a directory */
114     return nil;
115   
116   pool = [[NSAutoreleasePool alloc] init];
117   
118   array = [_fm directoryContentsAtPath:self->folderPath];
119   
120   if ([array count] == 0) {
121     /* no directory contents */
122     array = [array retain];
123     [pool release];
124     return [array autorelease];
125   }
126
127   array = [self _attributesForPaths:[array objectEnumerator]
128                 filterUsingQualifier:[self->fspec qualifier]
129                 fileManager:_fm];
130   
131   if ((sortOrderings = [self->fspec sortOrderings]))
132     /* sort set */
133     array = [array sortedArrayUsingKeyOrderArray:sortOrderings];
134   
135   array = [array retain];
136   [pool release];
137   
138   return [array autorelease];
139 }
140
141 - (NSArray *)fetchObjects {
142   NSFileManager *fm;
143   
144   fm = [NSFileManager defaultManager];
145   return [self _fetchObjectsFromFileManager:fm];
146 }
147
148 @end /* NGFileInfoDataSource */