]> err.no Git - sope/blob - sope-core/NGExtensions/EOExt.subproj/EOFilterDataSource.m
fixed another logging problem
[sope] / sope-core / NGExtensions / EOExt.subproj / EOFilterDataSource.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 "EOFilterDataSource.h"
23 #include "EODataSource+NGExtensions.h"
24 #include "EOGrouping.h"
25 #include <EOControl/EOControl.h>
26 #include "common.h"
27
28 @interface NSDictionary(EOFilterDataSource)
29
30 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
31   andKeys:(NSArray *)_keys;
32
33 @end
34
35 @implementation EOFilterDataSource
36
37 - (id)initWithDataSource:(EODataSource *)_ds {
38   if ((self = [super init])) {
39     [self setSource:_ds];
40   }
41   return self;
42 }
43 - (id)init {
44   return [self initWithDataSource:nil];
45 }
46
47 - (void)dealloc {
48   [[NSNotificationCenter defaultCenter] removeObserver:self];
49   [self->sortOrderings      release];
50   [self->groupings          release];
51   [self->auxiliaryQualifier release];
52   [self->source             release];
53   [super dealloc];
54 }
55
56 /* accessors */
57
58 - (void)setSource:(EODataSource *)_source {
59   NSNotificationCenter *nc;
60   
61   if (self->source == _source)
62     return;
63     
64   nc = [NSNotificationCenter defaultCenter];
65
66   if (self->source) {
67       [nc removeObserver:self
68           name:EODataSourceDidChangeNotification object:self->source];
69   }
70     
71   ASSIGN(self->source, _source);
72     
73   if (self->source) {
74       [nc addObserver:self selector:@selector(_sourceDidChange:)
75           name:EODataSourceDidChangeNotification object:self->source];
76   }
77     
78   [self postDataSourceChangedNotification];
79 }
80 - (EODataSource *)source {
81   return self->source;
82 }
83
84 - (void)setAuxiliaryQualifier:(EOQualifier *)_q {
85   if ([_q isEqual:self->auxiliaryQualifier])
86     return;
87
88   ASSIGN(self->auxiliaryQualifier, _q);
89   [self postDataSourceChangedNotification];
90 }
91 - (EOQualifier *)auxiliaryQualifier {
92   return self->auxiliaryQualifier;
93 }
94
95 - (void)setSortOrderings:(NSArray *)_so {
96   if (self->sortOrderings == _so)
97     return;
98   
99   _so = [_so shallowCopy];
100   [self->sortOrderings release];
101   self->sortOrderings = _so;
102   [self postDataSourceChangedNotification];
103 }
104 - (NSArray *)sortOrderings {
105   return self->sortOrderings;
106 }
107
108 - (void)setGroupings:(NSArray *)_groupings {
109   if (self->groupings == _groupings)
110     return;
111   
112   _groupings = [_groupings shallowCopy];
113   [self->groupings release];
114   self->groupings = _groupings;
115   [self postDataSourceChangedNotification];  
116 }
117 - (NSArray *)groupings {
118   return self->groupings;
119 }
120
121 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
122   [[self source] setFetchSpecification:_fspec];
123 }
124 - (EOFetchSpecification *)fetchSpecification {
125   return [[self source] fetchSpecification];
126 }
127
128 /* notifications */
129
130 - (void)_sourceDidChange:(NSNotification *)_notification {
131   [self postDataSourceChangedNotification];
132 }
133
134 /* operations */
135
136 - (NSArray *)fetchObjects {
137   NSAutoreleasePool *pool;
138   NSArray *objs;
139   NSArray *groups;
140
141   pool = [[NSAutoreleasePool alloc] init];
142   
143   objs = [[self source] fetchObjects];
144   
145   if ([self auxiliaryQualifier])
146     objs = [objs filteredArrayUsingQualifier:[self auxiliaryQualifier]];
147
148   if ((groups = [self groupings]) != nil) {
149     unsigned int cnt;
150     EOGrouping   *grouping;
151     NSArray      *allKeys;
152     NSArray      *sos;
153     NSDictionary *groupDict;
154
155     cnt = [objs count];
156
157     grouping = [groups lastObject];
158     
159     if ((sos = [self sortOrderings]) != nil) {
160       [grouping setSortOrderings:sos];
161     }
162     groupDict = [objs arrayGroupedBy:grouping];
163
164     allKeys = [groupDict allKeys];
165     allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
166     objs    = [groupDict flattenedArrayWithHint:cnt andKeys:allKeys];
167   }
168   else if ([self sortOrderings])
169     objs = [objs sortedArrayUsingKeyOrderArray:[self sortOrderings]];
170   
171   objs = [objs copy];
172   [pool release];
173   
174   return [objs autorelease];
175 }
176
177 - (void)insertObject:(id)_obj {
178   [[self source] insertObject:_obj];
179 }
180
181 - (void)deleteObject:(id)_obj {
182   [[self source] deleteObject:_obj];
183 }
184
185 - (void)updateObject:(id)_obj {
186   [self->source updateObject:_obj];
187 }
188
189 - (id)createObject {
190   return [[self source] createObject];
191 }
192
193 - (EOClassDescription *)classDescriptionForObjects {
194   return [[self source] classDescriptionForObjects];
195 }
196
197 /* description */
198
199 - (NSString *)description {
200   NSMutableString *ms;
201
202   ms = [NSMutableString stringWithCapacity:64];
203   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
204
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];
210   if (self->groupings)
211     [ms appendFormat:@" groupings=%@", self->sortOrderings];
212   
213   [ms appendString:@">"];
214   return ms;
215 }
216
217 @end /* EOFilterDataSource */
218
219 @implementation NSDictionary(EOFilterDataSource)
220
221 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
222   andKeys:(NSArray *)_keys
223 {
224   NSMutableArray *result = nil;
225   unsigned int   i, cnt;
226   
227   result =
228     [[NSMutableArray alloc] initWithCapacity:_hint]; // should be improved
229   
230   for (i = 0, cnt = [_keys count]; i < cnt; i++) {
231     NSString *key;
232     NSArray  *tmp;
233     
234     key = [_keys objectAtIndex:i];
235     tmp = [self objectForKey:key];
236     [result addObjectsFromArray:tmp];
237   }
238   
239   return [result autorelease];
240 }
241
242 @end /* NSDictionary(EOFilterDataSource) */