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