2 Copyright (C) 2002-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "OFSFolderDataSource.h"
23 #include <EOControl/EOQualifier.h>
24 #include <EOControl/EOSortOrdering.h>
27 @interface OFSFolderFetchEnum : NSEnumerator
31 EOQualifier *qualifier;
36 - (id)initWithFolder:(id)_folder
37 fetchSpecification:(EOFetchSpecification *)_fs;
41 @implementation OFSFolderDataSource
43 - (id)initWithFolder:(id)_folder {
44 if ((self = [super init])) {
45 self->folder = [_folder retain];
50 return [self initWithFolder:nil];
52 + (id)dataSourceOnFolder:(id)_folder {
53 return [[[self alloc] initWithFolder:_folder] autorelease];
57 [self->fetchSpecification release];
58 [self->folder release];
68 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
69 if ([self->fetchSpecification isEqual:_fspec])
72 [self->fetchSpecification autorelease];
73 self->fetchSpecification = [_fspec copy];
75 [self postDataSourceChangedNotification];
77 - (EOFetchSpecification *)fetchSpecification {
78 return self->fetchSpecification;
83 - (NSEnumerator *)fetchEnumerator {
84 OFSFolderFetchEnum *e;
85 EOFetchSpecification *fs;
86 NSArray *sortOrderings;
87 NSAutoreleasePool *pool;
92 if ((fs = [self fetchSpecification]) == nil) {
93 e = [[OFSFolderFetchEnum alloc] initWithFolder:[self folder]
94 fetchSpecification:nil];
95 return [e autorelease];
98 sortOrderings = [fs sortOrderings];
99 if ([sortOrderings count] == 0) {
100 /* can do incremental fetch ... */
101 e = [[OFSFolderFetchEnum alloc] initWithFolder:[self folder]
102 fetchSpecification:fs];
103 return [e autorelease];
106 /* fetch => filter => limit => sort, then return enum ... */
108 pool = [[NSAutoreleasePool alloc] init];
110 array = [[self folder] allValues];
112 if ((q = [fs qualifier]))
113 array = [array filteredArrayUsingQualifier:q];
115 if ((limit = [fs fetchLimit]) > 0) {
117 if (limit < [array count])
118 array = [array subarrayWithRange:NSMakeRange(0, limit)];
121 array = [array sortedArrayUsingKeyOrderArray:sortOrderings];
123 e = [[array objectEnumerator] retain];
126 return [e autorelease];
129 - (NSArray *)fetchObjects {
132 e = [self fetchEnumerator];
133 return [[[NSArray alloc] initWithObjectsFromEnumerator:e] autorelease];
138 - (id)copyWithZone:(NSZone *)_zone {
139 OFSFolderDataSource *ds;
141 ds = [[[self class] alloc] initWithFolder:[self folder]];
142 [ds setFetchSpecification:[self fetchSpecification]];
146 @end /* OFSFolderDataSource */
148 @implementation OFSFolderFetchEnum
150 - (id)initWithFolder:(id)_folder
151 fetchSpecification:(EOFetchSpecification *)_fs
153 if ((self = [super init])) {
154 self->folder = [_folder retain];
155 self->names = [[[self->folder allKeys] objectEnumerator] retain];
156 self->qualifier = [[_fs qualifier] retain];
157 self->limit = [_fs fetchLimit];
162 return [self initWithFolder:nil fetchSpecification:nil];
166 [self->qualifier release];
167 [self->names release];
168 [self->folder release];
175 [self->qualifier release]; self->qualifier = nil;
176 [self->names release]; self->names = nil;
177 [self->folder release]; self->folder = nil;
182 - (id)_nextObjectToBeFiltered {
186 if ((nextName = [self->names nextObject]) == nil) {
190 if ((object = [self->folder objectForKey:nextName]) == nil) {
200 if(self->qualifier != nil) {
202 if ((obj = [self _nextObjectToBeFiltered]) == nil) {
207 while (![(id<EOQualifierEvaluation>)self->qualifier
208 evaluateWithObject:obj]);
211 if ((obj = [self _nextObjectToBeFiltered]) == nil) {
218 if (self->limit > 0 && self->count > self->limit) {
226 @end /* OFSFolderFetchEnum */