]> err.no Git - sope/blob - sope-core/EOCoreData/EOSortOrdering+CoreData.m
Drop apache 1 build-dependency
[sope] / sope-core / EOCoreData / EOSortOrdering+CoreData.m
1 /*
2   Copyright (C) 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 "EOSortOrdering+CoreData.h"
23 #include "common.h"
24
25 @implementation EOSortOrdering(CoreData)
26
27 - (id)initWithSortDescriptor:(NSSortDescriptor *)_descriptor {
28   SEL sel;
29   
30   if (_descriptor == nil) {
31     [self release];
32     return nil;
33   }
34   
35   sel = [_descriptor selector];
36   if (SEL_EQ(sel, @selector(compare:))) {
37     sel = [_descriptor ascending] 
38       ? EOCompareAscending
39       : EOCompareDescending;
40   }
41   else if (SEL_EQ(sel, @selector(caseInsensitiveCompare:))) {
42     sel = [_descriptor ascending] 
43       ? EOCompareCaseInsensitiveAscending
44       : EOCompareCaseInsensitiveDescending;
45   }
46   else {
47     if (![_descriptor ascending]) {
48       NSLog(@"WARNING(%s): cannot representing descending selector in "
49             @"NSSortDescriptor: %@", __PRETTY_FUNCTION__, _descriptor);
50     }
51   }
52   
53   return [self initWithKey:[_descriptor key] selector:sel];
54 }
55
56 - (BOOL)isAscendingEOSortSelector:(SEL)_sel {
57   if (SEL_EQ(_sel, EOCompareDescending)) return NO;
58   if (SEL_EQ(_sel, EOCompareCaseInsensitiveAscending)) return NO;
59   return YES;
60 }
61
62 - (SEL)cdSortSelectorFromEOSortSelector:(SEL)_sel {
63   if (SEL_EQ(_sel, EOCompareAscending))  return @selector(compare:);
64   if (SEL_EQ(_sel, EOCompareDescending)) return @selector(compare:);
65   
66   if (SEL_EQ(_sel, EOCompareCaseInsensitiveAscending))
67     return @selector(caseInsensitiveCompare:);
68   if (SEL_EQ(_sel, EOCompareCaseInsensitiveDescending))
69     return @selector(caseInsensitiveCompare:);
70   
71   return _sel;
72 }
73
74 - (NSSortDescriptor *)asSortDescriptor {
75   SEL sel;
76
77   sel = [self selector];
78   
79   return [[[NSSortDescriptor alloc] 
80             initWithKey:[self key]
81             ascending:[self isAscendingEOSortSelector:sel]
82             selector:[self cdSortSelectorFromEOSortSelector:sel]] autorelease];
83 }
84
85 @end /* EOSortOrdering(CoreData) */
86
87
88 @implementation NSSortDescriptor(EOCoreData)
89
90 - (NSSortDescriptor *)asSortDescriptor {
91   return self;
92 }
93
94 @end /* NSSortDescriptor(EOCoreData) */