]> err.no Git - sope/blob - sope-core/NGExtensions/EOExt.subproj/EOSortOrdering+plist.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / EOExt.subproj / EOSortOrdering+plist.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 <NGExtensions/EOSortOrdering+plist.h>
23 #include "common.h"
24
25 @implementation EOSortOrdering(plist)
26
27 /*"
28   Initialize a sort-ordering with information contained in the dictionary.
29   The following keys are recognized: "key" is required and specifies the
30   key to be sorted on, "selector" is optional and specifies the sort
31   selector as a string. The default for "selector" is EOCompareAscending
32   and the following "special" values are recognized: "compareAscending",
33   "compareDescending", "compareCaseInsensitiveAscending", 
34   "compareCaseInsensitiveDescending".
35 "*/
36 - (id)initWithDictionary:(NSDictionary *)_dict {
37   NSString *k  = nil;
38   SEL      sel = EOCompareAscending;
39   NSString *tmp;
40
41   if (_dict == nil) {
42     [self release];
43     return nil;
44   }
45   
46   k = [_dict objectForKey:@"key"];
47   if ([k length] == 0) {
48     NSLog(@"%s: invalid key %@ (dict=%@)", __PRETTY_FUNCTION__, k, _dict);
49     [self release];
50     return nil;
51   }
52   
53   if ((tmp = [[_dict objectForKey:@"selector"] stringValue])) {
54     if ([tmp isEqualToString:@"compareAscending"])
55       sel = EOCompareAscending;
56     else if ([tmp isEqualToString:@"compareDescending"])
57       sel = EOCompareDescending;
58     else if ([tmp isEqualToString:@"compareCaseInsensitiveAscending"])
59       sel = EOCompareCaseInsensitiveAscending;
60     else if ([tmp isEqualToString:@"compareCaseInsensitiveDescending"])
61       sel = EOCompareCaseInsensitiveDescending;
62     else
63       sel = NSSelectorFromString(tmp);
64   }
65   return [self initWithKey:k selector:sel];
66 }
67
68 /*"
69   Initialize/parse a sort-ordering from a string. Usually the string is
70   taken as the key of the ordering and the sorting EOCompareAscending. This
71   can be modified by adding ".reverse" to the key, eg "name.reverse" sorts
72   on the "name" key using EOCompareDescending.
73 "*/
74 - (id)initWithString:(NSString *)_string {
75   SEL      sel;
76   NSString *k;
77   NSRange  r;
78   
79   if ([_string length] == 0) {
80     [self release];
81     return nil;
82   }
83   
84   r = [_string rangeOfString:@".reverse"];
85   if (r.length == 0) {
86     k    = _string;
87     sel = EOCompareAscending;
88   }
89   else {
90     k   = [_string substringToIndex:r.location];
91     sel = EOCompareDescending;
92   }
93   
94   return [self initWithKey:k selector:sel];
95 }
96
97 - (id)initWithPropertyList:(id)_plist owner:(id)_owner {
98   if (_plist == nil) {
99     [self release];
100     return nil;
101   }
102   
103   if ([_plist isKindOfClass:[NSDictionary class]])
104     return [self initWithDictionary:_plist];
105   if ([_plist isKindOfClass:[NSString class]])
106     return [self initWithString:_plist];
107   
108   if ([_plist isKindOfClass:[self class]]) {
109     [self release];
110     return [_plist copy];
111   }
112   
113   [self release];
114   return nil;
115 }
116 - (id)initWithPropertyList:(id)_plist {
117   return [self initWithPropertyList:_plist owner:nil];
118 }
119
120 @end /* EOSortOrdering(plist) */