Add preference to skip hidden files during sync.
[pithos-macos] / pithos-macos / SharingDictionaryTransformer.m
1 //
2 //  SharingDictionaryTransformer.m
3 //  pithos-macos
4 //
5 // Copyright 2011 GRNET S.A. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
9 // conditions are met:
10 // 
11 //   1. Redistributions of source code must retain the above
12 //      copyright notice, this list of conditions and the following
13 //      disclaimer.
14 // 
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.
19 // 
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.
32 // 
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.
37
38 #import "SharingDictionaryTransformer.h"
39
40 @implementation SharingDictionaryTransformer
41
42 + (Class)transformedValueClass {
43         return [NSMutableDictionary class];
44 }
45
46 + (BOOL)allowsReverseTransformation {
47         return YES;
48 }
49
50 - (id)transformedValue:(id)value {
51     NSMutableDictionary *sharingDictionary = [NSMutableDictionary dictionary];
52     
53     if (value == nil)
54         return sharingDictionary;
55     
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;
63         }
64         for (NSString *readUser in [[(NSString *)value substringFromIndex:readStart] componentsSeparatedByString:@","]) {
65             [sharingDictionary setValue:@"read only" forKey:readUser];
66         }
67     } else if (readRange.length == 0) {
68         for (NSString *writeUser in [[(NSString *)value substringFromIndex:writeStart] componentsSeparatedByString:@","]) {
69             [sharingDictionary setValue:@"read/write" forKey:writeUser];
70         }
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];
75         }
76         for (NSString *writeUser in [[(NSString *)value substringFromIndex:writeStart] componentsSeparatedByString:@","]) {
77             [sharingDictionary setValue:@"read/write" forKey:writeUser];
78         }        
79     } else {
80         NSRange semicolonRange = [(NSString *)value rangeOfString:@";"];
81         for (NSString *readUser in [[(NSString *)value substringFromIndex:readStart] componentsSeparatedByString:@","]) {
82             [sharingDictionary setValue:@"read only" forKey:readUser];
83         }                
84         for (NSString *writeUser in [[(NSString *)value substringWithRange:NSMakeRange(writeStart, semicolonRange.location - writeStart)] componentsSeparatedByString:@","]) {
85             [sharingDictionary setValue:@"read/write" forKey:writeUser];
86         }
87     }
88     
89     return sharingDictionary;
90 }
91
92 - (id)reverseTransformedValue:(id)value {
93     if ((value == nil) || ([(NSMutableDictionary *)value count] == 0))
94         return nil;
95     
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;
101     
102     NSArray *writeUsers = [(NSMutableDictionary *)value allKeysForObject:@"read/write"];
103     if ([writeUsers count])
104         writeSharingString = [NSString stringWithFormat:@"write=%@", [writeUsers componentsJoinedByString:@","]];
105         
106
107     if (readSharingString) {
108         if (writeSharingString) {
109             return [NSString stringWithFormat:@"%@;%@", readSharingString, writeSharingString];
110         }
111         return readSharingString;
112     } else {
113         return writeSharingString;
114     }
115 }
116
117 + (void)initialize {
118     [[NSValueTransformer class] setValueTransformer:[self new] forName:@"SharingDictionaryTransformer"];
119 }
120
121 @end