2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include "EOFilterDataSource.h"
23 #include "EODataSource+NGExtensions.h"
24 #include "EOGrouping.h"
25 #include <EOControl/EOControl.h>
28 @interface NSDictionary(EOFilterDataSource)
30 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
31 andKeys:(NSArray *)_keys;
35 @implementation EOFilterDataSource
37 - (id)initWithDataSource:(EODataSource *)_ds {
38 if ((self = [super init])) {
44 return [self initWithDataSource:nil];
48 [[NSNotificationCenter defaultCenter] removeObserver:self];
49 [self->sortOrderings release];
50 [self->groupings release];
51 [self->auxiliaryQualifier release];
52 [self->source release];
58 - (void)setSource:(EODataSource *)_source {
59 NSNotificationCenter *nc;
61 if (self->source == _source)
64 nc = [NSNotificationCenter defaultCenter];
67 [nc removeObserver:self
68 name:EODataSourceDidChangeNotification object:self->source];
71 ASSIGN(self->source, _source);
74 [nc addObserver:self selector:@selector(_sourceDidChange:)
75 name:EODataSourceDidChangeNotification object:self->source];
78 [self postDataSourceChangedNotification];
80 - (EODataSource *)source {
84 - (void)setAuxiliaryQualifier:(EOQualifier *)_q {
85 if ([_q isEqual:self->auxiliaryQualifier])
88 ASSIGN(self->auxiliaryQualifier, _q);
89 [self postDataSourceChangedNotification];
91 - (EOQualifier *)auxiliaryQualifier {
92 return self->auxiliaryQualifier;
95 - (void)setSortOrderings:(NSArray *)_so {
96 if (self->sortOrderings == _so)
99 _so = [_so shallowCopy];
100 [self->sortOrderings release];
101 self->sortOrderings = _so;
102 [self postDataSourceChangedNotification];
104 - (NSArray *)sortOrderings {
105 return self->sortOrderings;
108 - (void)setGroupings:(NSArray *)_groupings {
109 if (self->groupings == _groupings)
112 _groupings = [_groupings shallowCopy];
113 [self->groupings release];
114 self->groupings = _groupings;
115 [self postDataSourceChangedNotification];
117 - (NSArray *)groupings {
118 return self->groupings;
121 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
122 [[self source] setFetchSpecification:_fspec];
124 - (EOFetchSpecification *)fetchSpecification {
125 return [[self source] fetchSpecification];
130 - (void)_sourceDidChange:(NSNotification *)_notification {
131 [self postDataSourceChangedNotification];
136 - (NSArray *)fetchObjects {
137 NSAutoreleasePool *pool;
141 pool = [[NSAutoreleasePool alloc] init];
143 objs = [[self source] fetchObjects];
145 if ([self auxiliaryQualifier])
146 objs = [objs filteredArrayUsingQualifier:[self auxiliaryQualifier]];
148 if ((groups = [self groupings]) != nil) {
150 EOGrouping *grouping;
153 NSDictionary *groupDict;
157 grouping = [groups lastObject];
159 if ((sos = [self sortOrderings]) != nil) {
160 [grouping setSortOrderings:sos];
162 groupDict = [objs arrayGroupedBy:grouping];
164 allKeys = [groupDict allKeys];
165 allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
166 objs = [groupDict flattenedArrayWithHint:cnt andKeys:allKeys];
168 else if ([self sortOrderings])
169 objs = [objs sortedArrayUsingKeyOrderArray:[self sortOrderings]];
174 return [objs autorelease];
177 - (void)insertObject:(id)_obj {
178 [[self source] insertObject:_obj];
181 - (void)deleteObject:(id)_obj {
182 [[self source] deleteObject:_obj];
185 - (void)updateObject:(id)_obj {
186 [self->source updateObject:_obj];
190 return [[self source] createObject];
193 - (EOClassDescription *)classDescriptionForObjects {
194 return [[self source] classDescriptionForObjects];
199 - (NSString *)description {
202 ms = [NSMutableString stringWithCapacity:64];
203 [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
205 if (self->source) [ms appendFormat:@" source=%@", self->source];
206 if (self->auxiliaryQualifier)
207 [ms appendFormat:@" qualifier=%@", self->auxiliaryQualifier];
208 if (self->sortOrderings)
209 [ms appendFormat:@" orderings=%@", self->sortOrderings];
211 [ms appendFormat:@" groupings=%@", self->sortOrderings];
213 [ms appendString:@">"];
217 @end /* EOFilterDataSource */
219 @implementation NSDictionary(EOFilterDataSource)
221 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
222 andKeys:(NSArray *)_keys
224 NSMutableArray *result = nil;
228 [[NSMutableArray alloc] initWithCapacity:_hint]; // should be improved
230 for (i = 0, cnt = [_keys count]; i < cnt; i++) {
234 key = [_keys objectAtIndex:i];
235 tmp = [self objectForKey:key];
236 [result addObjectsFromArray:tmp];
239 return [result autorelease];
242 @end /* NSDictionary(EOFilterDataSource) */