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 <EOControl/EOArrayDataSource.h>
23#include <EOControl/EOFetchSpecification.h>
24#include <EOControl/EOSortOrdering.h>
25#include <EOControl/EOQualifier.h>
26#include "common.h"
27
28@interface EODataSource(PostChange)
29- (void)postDataSourceChangedNotification;
30@end
31
32@implementation EOArrayDataSource
33
34- (id)init {
35  if ((self = [super init])) {
36    self->objects = [[NSMutableArray alloc] init];
37  }
38  return self;
39}
40- (void)dealloc {
41  [self->fetchSpecification release];
42  [self->objects            release];
43  [super dealloc];
44}
45
46/* accessors */
47
48- (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
49  if ([self->fetchSpecification isEqual:_fspec])
50    return;
51
52  [self->fetchSpecification autorelease];
53  self->fetchSpecification = [_fspec copy];
54
55  [self postDataSourceChangedNotification];
56}
57- (EOFetchSpecification *)fetchSpecification {
58  return self->fetchSpecification;
59}
60
61- (void)setArray:(NSArray *)_array {
62  [self->objects removeAllObjects];
63  [self->objects addObjectsFromArray:_array];
64}
65
66/* fetching */
67
68- (NSArray *)fetchObjects {
69  NSArray *result;
70
71  if (self->fetchSpecification == nil) {
72    result = [[self->objects copy] autorelease];
73  }
74  else {
75    EOQualifier *q;
76    NSArray     *sort;
77
78    q    = [self->fetchSpecification qualifier];
79    sort = [self->fetchSpecification sortOrderings];
80
81    if (q == nil) {
82      if (sort)
83        result = [self->objects sortedArrayUsingKeyOrderArray:sort];
84      else
85        result = [[self->objects copy] autorelease];
86    }
87    else {
88      result = [self->objects filteredArrayUsingQualifier:q];
89      if (sort) result = [result sortedArrayUsingKeyOrderArray:sort];
90    }
91  }
92  return result;
93}
94
95/* operations */
96
97- (void)deleteObject:(id)_object {
98  [self->objects removeObjectIdenticalTo:_object];
99  [self postDataSourceChangedNotification];
100}
101
102- (void)insertObject:(id)_object {
103  [self->objects addObject:_object];
104  [self postDataSourceChangedNotification];
105}
106
107- (id)createObject {
108  return [NSMutableDictionary dictionaryWithCapacity:16];
109}
110
111@end /* EOArrayDataSource */
112