-
Notifications
You must be signed in to change notification settings - Fork 72
/
CKInternalTransferRecord.m
197 lines (163 loc) · 5.33 KB
/
CKInternalTransferRecord.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (c) 2007, Greg Hulands <ghulands@mac.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
Neither the name of Greg Hulands nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "CKInternalTransferRecord.h"
#import "RunLoopForwarder.h"
#import "NSObject+Connection.h"
@implementation CKInternalTransferRecord
+ (id)recordWithLocal:(NSString *)localPath
data:(NSData *)data
offset:(unsigned long long)offset
remote:(NSString *)remote
delegate:(id)delegate
userInfo:(id)ui
{
return [[[CKInternalTransferRecord alloc] initWithLocal:localPath
data:data
offset:offset
remote:remote
delegate:delegate
userInfo:ui] autorelease];
}
- (id)initWithLocal:(NSString *)localPath
data:(NSData *)data
offset:(unsigned long long)offset
remote:(NSString *)remote
delegate:(id)delegate
userInfo:(id)ui
{
if ((self = [super init]))
{
myLocalPath = [localPath copy];
myRemotePath = [remote copy];
myData = [data retain];
myOffset = offset;
myDelegate = [delegate retain];
/* Why retain this delegate? If an app only uses the original upload/download methods that don't take a delegate, then internally the delegate will be created which is a CKTR. If the internal transfer record doesn't retain it then you can see that when the transfer starts up, the delegate will be a dangling pointer and will crash. Because the internal transfer record is a private class, convention doesn't have to apply.
*/
myForwarder = [[RunLoopForwarder alloc] init];
[myForwarder setUseMainThread:YES];
[myForwarder setDelegate:myDelegate];
myUserInfo = [ui retain];
myFlags.didBegin = [myDelegate respondsToSelector:@selector(transferDidBegin:)];
myFlags.didFinish = [myDelegate respondsToSelector:@selector(transferDidFinish:error:)];
myFlags.error = [myDelegate respondsToSelector:@selector(transfer:didReceiveError:)];
myFlags.percent = [myDelegate respondsToSelector:@selector(transfer:progressedTo:)];
myFlags.progressed = [myDelegate respondsToSelector:@selector(transfer:transferredDataOfLength:)];
myProperties = [[NSMutableDictionary alloc] initWithCapacity:8];
}
return self;
}
- (void)dealloc
{
[myDelegate release];
[myLocalPath release];
[myRemotePath release];
[myData release];
[myUserInfo release];
[myForwarder release];
[myProperties release];
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
CKInternalTransferRecord *copy = [[[self class] alloc] initWithLocal:myLocalPath
data:myData
offset:myOffset
remote:myRemotePath
delegate:myDelegate
userInfo:myUserInfo];
return copy;
}
- (NSString *)localPath
{
return myLocalPath;
}
- (NSData *)data
{
return myData;
}
- (unsigned long long)offset
{
return myOffset;
}
- (void)setRemotePath:(NSString *)path
{
[myRemotePath autorelease];
myRemotePath = [path copy];
}
- (NSString *)remotePath
{
return myRemotePath;
}
- (id)delegate
{
return myForwarder;
}
- (void)setUserInfo:(id)ui
{
[myUserInfo autorelease];
myUserInfo = [ui retain];
}
- (id)userInfo
{
return myUserInfo;
}
- (BOOL)delegateRespondsToTransferDidBegin
{
return myFlags.didBegin;
}
- (BOOL)delegateRespondsToTransferProgressedTo
{
return myFlags.percent;
}
- (BOOL)delegateRespondsToTransferTransferredData
{
return myFlags.progressed;
}
- (BOOL)delegateRespondsToTransferDidFinish
{
return myFlags.didFinish;
}
- (BOOL)delegateRespondsToError
{
return myFlags.error;
}
- (NSString *)description
{
NSMutableString *str = [NSMutableString stringWithFormat:@"%@ <0x%06x>\n", [self className], self];
[str appendFormat:@"Local: %@\n", myLocalPath];
[str appendFormat:@"Remote: %@\n", myRemotePath];
[str appendFormat:@"Data length: %u\n", [myData length]];
[str appendFormat:@"Offset: %lld", myOffset];
return str;
}
- (void)setObject:(id)object forKey:(id)key
{
[myProperties setObject:object forKey:key];
}
- (id)objectForKey:(id)key
{
return [myProperties objectForKey:key];
}
@end