Add initial support for user catalog
[pithos-macos] / pithos-macos / PithosNode.m
1 //
2 //  PithosNode.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 "PithosNode.h"
39 #import "PithosNodeInfoController.h"
40 #import "ASIPithosRequest.h"
41
42 @implementation PithosNode
43 @synthesize forcedRefresh, parent, shared, sharingAccount, childrenUpdatedNotificationName, 
44             inheritChildrenUpdatedNotificationName, displayName, isLeafItem, icon;
45 @dynamic url, children, pithos, pithosAccount, pithosContainer, pithosObject;
46
47 #pragma mark -
48 #pragma mark Object Lifecycle
49
50 - (id)init {
51     if ((self = [super init])) {
52         freshness = PithosNodeStateRefreshNeeded;
53         forcedRefresh = NO;
54         shared = NO;
55         isLeafItem = NO;
56         self.childrenUpdatedNotificationName = @"PithosNodeChildrenUpdated";
57         inheritChildrenUpdatedNotificationName = NO;
58     }
59     return self;
60 }
61
62
63 - (BOOL)isEqual:(id)anObject {
64     return ([anObject isKindOfClass:[self class]] && [((PithosNode *)anObject).url isEqual:self.url]);
65 }
66
67 - (NSUInteger)hash {
68     return self.url.hash;
69 }
70
71 #pragma mark -
72 #pragma mark Properties
73
74 - (void)setShared:(BOOL)aShared {
75     if (shared != aShared) {
76         shared = aShared;
77         url = nil;
78     }
79 }
80
81 - (void)setSharingAccount:(NSString *)aSharingAccount {
82     if (![sharingAccount isEqualToString:aSharingAccount]) {
83         sharingAccount = aSharingAccount;
84         url = nil;
85     }
86 }
87
88 #pragma mark -
89 #pragma mark Actions
90
91 - (void)invalidateChildren {
92     if (freshness == PithosNodeStateFresh)
93         freshness = PithosNodeStateRefreshNeeded;
94 }
95
96 - (void)invalidateChildrenRecursive {    
97     if (freshness == PithosNodeStateFresh) {
98         for (PithosNode *child in children) {
99             [child invalidateChildrenRecursive];
100         }
101         freshness = PithosNodeStateRefreshNeeded;
102     }
103 }
104
105 - (void)refresh {
106     [self invalidateChildren];
107     self.children;
108 }
109
110 - (void)forceRefresh {
111     forcedRefresh = YES;
112     [self refresh];
113 }
114
115 - (void)postChildrenUpdatedNotificationName {
116     // Notify observers that children are updated
117     if (inheritChildrenUpdatedNotificationName) {
118         PithosNode *ancestor = self;
119         while (ancestor && ancestor.parent) {
120             ancestor = ancestor.parent;
121         }
122         if (ancestor && ancestor.childrenUpdatedNotificationName) {
123             [[NSNotificationCenter defaultCenter] postNotificationName:ancestor.childrenUpdatedNotificationName object:self];
124         }
125     } else if (childrenUpdatedNotificationName) {
126         [[NSNotificationCenter defaultCenter] postNotificationName:childrenUpdatedNotificationName object:self];
127     }
128 }
129
130 - (void)showPithosNodeInfo:(id)sender {
131     // Abstract method
132 }
133
134 - (void)pithosNodeInfoWillClose:(id)sender {
135     if (pithosNodeInfoController) {
136         pithosNodeInfoController = nil;
137     }
138 }
139
140 - (void)pithosNodeWillBeRemoved {
141     if (pithosNodeInfoController) {
142         pithosNodeInfoController.node = nil;
143         [[pithosNodeInfoController window] close];
144     }
145     for (PithosNode *child in children) {
146         [child pithosNodeWillBeRemoved];
147     }
148 }
149
150 #pragma mark -
151 #pragma mark ASIHTTPRequestDelegate
152
153 - (void)performRequestFinishedDelegateInBackground:(ASIPithosRequest *)request {
154     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
155         [self performSelector:NSSelectorFromString([request.userInfo objectForKey:@"didFinishSelector"]) withObject:request];
156     });
157 }
158
159 - (void)performRequestFailedDelegateInBackground:(ASIPithosRequest *)request {
160     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
161         [self performSelector:NSSelectorFromString([request.userInfo objectForKey:@"didFailSelector"]) withObject:request];
162     });
163 }
164
165 @end