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
23 #include "EOFilterDataSource.h"
24 #include "EODataSource+NGExtensions.h"
25 #include "EOGrouping.h"
26 #include <EOControl/EOControl.h>
29 @interface NSDictionary(EOFilterDataSource)
31 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
32 andKeys:(NSArray *)_keys;
36 @implementation EOFilterDataSource
38 - (id)initWithDataSource:(EODataSource *)_ds {
39 if ((self = [super init])) {
45 return [self initWithDataSource:nil];
49 [[NSNotificationCenter defaultCenter] removeObserver:self];
50 [self->sortOrderings release];
51 [self->groupings release];
52 [self->auxiliaryQualifier release];
53 [self->source release];
59 - (void)setSource:(EODataSource *)_source {
60 NSNotificationCenter *nc;
62 if (self->source == _source)
65 nc = [NSNotificationCenter defaultCenter];
68 [nc removeObserver:self
69 name:EODataSourceDidChangeNotification object:self->source];
72 ASSIGN(self->source, _source);
75 [nc addObserver:self selector:@selector(_sourceDidChange:)
76 name:EODataSourceDidChangeNotification object:self->source];
79 [self postDataSourceChangedNotification];
81 - (EODataSource *)source {
85 - (void)setAuxiliaryQualifier:(EOQualifier *)_q {
86 if ([_q isEqual:self->auxiliaryQualifier])
89 ASSIGN(self->auxiliaryQualifier, _q);
90 [self postDataSourceChangedNotification];
92 - (EOQualifier *)auxiliaryQualifier {
93 return self->auxiliaryQualifier;
96 - (void)setSortOrderings:(NSArray *)_so {
97 if (self->sortOrderings == _so)
100 _so = [_so shallowCopy];
101 [self->sortOrderings release];
102 self->sortOrderings = _so;
103 [self postDataSourceChangedNotification];
105 - (NSArray *)sortOrderings {
106 return self->sortOrderings;
109 - (void)setGroupings:(NSArray *)_groupings {
110 if (self->groupings == _groupings)
113 _groupings = [_groupings shallowCopy];
114 [self->groupings release];
115 self->groupings = _groupings;
116 [self postDataSourceChangedNotification];
118 - (NSArray *)groupings {
119 return self->groupings;
122 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
123 [[self source] setFetchSpecification:_fspec];
125 - (EOFetchSpecification *)fetchSpecification {
126 return [[self source] fetchSpecification];
131 - (void)_sourceDidChange:(NSNotification *)_notification {
132 [self postDataSourceChangedNotification];
137 - (NSArray *)fetchObjects {
138 NSAutoreleasePool *pool;
142 pool = [[NSAutoreleasePool alloc] init];
144 objs = [[self source] fetchObjects];
146 if ([self auxiliaryQualifier])
147 objs = [objs filteredArrayUsingQualifier:[self auxiliaryQualifier]];
149 if ((groups = [self groupings]) != nil) {
151 EOGrouping *grouping;
154 NSDictionary *groupDict;
158 grouping = [groups lastObject];
160 if ((sos = [self sortOrderings]) != nil) {
161 [grouping setSortOrderings:sos];
163 groupDict = [objs arrayGroupedBy:grouping];
165 allKeys = [groupDict allKeys];
166 allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
167 objs = [groupDict flattenedArrayWithHint:cnt andKeys:allKeys];
169 else if ([self sortOrderings])
170 objs = [objs sortedArrayUsingKeyOrderArray:[self sortOrderings]];
175 return [objs autorelease];
178 - (void)insertObject:(id)_obj {
179 [[self source] insertObject:_obj];
182 - (void)deleteObject:(id)_obj {
183 [[self source] deleteObject:_obj];
186 - (void)updateObject:(id)_obj {
187 [self->source updateObject:_obj];
191 return [[self source] createObject];
194 - (EOClassDescription *)classDescriptionForObjects {
195 return [[self source] classDescriptionForObjects];
200 - (NSString *)description {
203 ms = [NSMutableString stringWithCapacity:64];
204 [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
206 if (self->source) [ms appendFormat:@" source=%@", self->source];
207 if (self->auxiliaryQualifier)
208 [ms appendFormat:@" qualifier=%@", self->auxiliaryQualifier];
209 if (self->sortOrderings)
210 [ms appendFormat:@" orderings=%@", self->sortOrderings];
212 [ms appendFormat:@" groupings=%@", self->sortOrderings];
214 [ms appendString:@">"];
218 @end /* EOFilterDataSource */
220 @implementation NSDictionary(EOFilterDataSource)
222 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
223 andKeys:(NSArray *)_keys
225 NSMutableArray *result = nil;
229 [[NSMutableArray alloc] initWithCapacity:_hint]; // should be improved
231 for (i = 0, cnt = [_keys count]; i < cnt; i++) {
235 key = [_keys objectAtIndex:i];
236 tmp = [self objectForKey:key];
237 [result addObjectsFromArray:tmp];
240 return [result autorelease];
243 @end /* NSDictionary(EOFilterDataSource) */