2 // SharingDictionaryTransformer.m
5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
15 // 2. Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials
18 // provided with the distribution.
20 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
33 // The views and conclusions contained in the software and
34 // documentation are those of the authors and should not be
35 // interpreted as representing official policies, either expressed
36 // or implied, of GRNET S.A.
38 #import "SharingDictionaryTransformer.h"
40 @implementation SharingDictionaryTransformer
42 + (Class)transformedValueClass {
43 return [NSMutableDictionary class];
46 + (BOOL)allowsReverseTransformation {
50 - (id)transformedValue:(id)value {
51 NSMutableDictionary *sharingDictionary = [NSMutableDictionary dictionary];
54 return sharingDictionary;
56 NSRange readRange = [(NSString *)value rangeOfString:@"read=" options:NSCaseInsensitiveSearch];
57 NSUInteger readStart = readRange.location + readRange.length;
58 NSRange writeRange = [(NSString *)value rangeOfString:@"write=" options:NSCaseInsensitiveSearch];
59 NSUInteger writeStart = writeRange.location + writeRange.length;
60 if (writeRange.length == 0) {
61 if (readRange.length == 0) {
62 return sharingDictionary;
64 for (NSString *readUser in [[(NSString *)value substringFromIndex:readStart] componentsSeparatedByString:@","]) {
65 [sharingDictionary setValue:@"read only" forKey:readUser];
67 } else if (readRange.length == 0) {
68 for (NSString *writeUser in [[(NSString *)value substringFromIndex:writeStart] componentsSeparatedByString:@","]) {
69 [sharingDictionary setValue:@"read/write" forKey:writeUser];
71 } else if (readRange.location < writeRange.location) {
72 NSRange semicolonRange = [(NSString *)value rangeOfString:@";"];
73 for (NSString *readUser in [[(NSString *)value substringWithRange:NSMakeRange(readStart, semicolonRange.location - readStart)] componentsSeparatedByString:@","]) {
74 [sharingDictionary setValue:@"read only" forKey:readUser];
76 for (NSString *writeUser in [[(NSString *)value substringFromIndex:writeStart] componentsSeparatedByString:@","]) {
77 [sharingDictionary setValue:@"read/write" forKey:writeUser];
80 NSRange semicolonRange = [(NSString *)value rangeOfString:@";"];
81 for (NSString *readUser in [[(NSString *)value substringFromIndex:readStart] componentsSeparatedByString:@","]) {
82 [sharingDictionary setValue:@"read only" forKey:readUser];
84 for (NSString *writeUser in [[(NSString *)value substringWithRange:NSMakeRange(writeStart, semicolonRange.location - writeStart)] componentsSeparatedByString:@","]) {
85 [sharingDictionary setValue:@"read/write" forKey:writeUser];
89 return sharingDictionary;
92 - (id)reverseTransformedValue:(id)value {
93 if ((value == nil) || ([(NSMutableDictionary *)value count] == 0))
96 NSString *readSharingString = nil;
97 NSArray *readUsers = [(NSMutableDictionary *)value allKeysForObject:@"read only"];
98 if ([readUsers count])
99 readSharingString = [NSString stringWithFormat:@"read=%@", [readUsers componentsJoinedByString:@","]];
100 NSString *writeSharingString = nil;
102 NSArray *writeUsers = [(NSMutableDictionary *)value allKeysForObject:@"read/write"];
103 if ([writeUsers count])
104 writeSharingString = [NSString stringWithFormat:@"write=%@", [writeUsers componentsJoinedByString:@","]];
107 if (readSharingString) {
108 if (writeSharingString) {
109 return [NSString stringWithFormat:@"%@;%@", readSharingString, writeSharingString];
111 return readSharingString;
113 return writeSharingString;
118 [[NSValueTransformer class] setValueTransformer:[self new] forName:@"SharingDictionaryTransformer"];