This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
DCFileUploadManager.j
96 lines (77 loc) · 2.25 KB
/
DCFileUploadManager.j
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
@import <AppKit/CPPanel.j>
@import "DCFileUpload.j"
/*
DCFileUploadManagerDelegate protocol
- (void)fileUploadManagerDidChange:(DCFileUploadManager)theManager;
*/
SharedFileUploadManager = nil;
@implementation DCFileUploadManager : CPObject {
CPArray fileUploads @accessors;
id delegate @accessors;
BOOL concurrent @accessors; // YES to make files upload at the same time. NO (default) to upload one at a time.
}
+ (DCFileUploadManager)sharedManager {
if (!SharedFileUploadManager) {
SharedFileUploadManager = [[DCFileUploadManager alloc] init];
}
return SharedFileUploadManager;
}
- (id)init {
self = [super init];
fileUploads = [[CPArray alloc] init];
return self;
}
- (DCFileUpload)fileUploadWithFile:(id)theFile uploadURL:(CPURL)theURL {
var fileUpload = [[DCFileUpload alloc] initWithFile:theFile];
[fileUpload setDelegate:self];
[fileUpload setName:theFile.fileName];
[fileUpload setUploadURL:theURL];
[fileUploads addObject:fileUpload];
[self didChange];
if (concurrent || ![self isUploading]) {
[fileUpload begin];
}
return fileUpload;
}
- (BOOL)isUploading {
for (var i = 0; i < [fileUploads count]; i++) {
var fileUpload = [fileUploads objectAtIndex:i];
if ([fileUpload isUploading]) {
return YES;
}
}
return NO;
}
- (void)removeFileUpload:(DCFileUpload)theFileUpload {
[fileUploads removeObject:theFileUpload];
[self didChange];
}
- (void)fileUploadDidBegin:(DCFileUpload)theFileUpload {
[self didChange];
}
- (void)fileUploadProgressDidChange:(DCFileUpload)theFileUpload {
[self didChange];
}
- (void)fileUploadDidEnd:(DCFileUpload)theFileUpload {
if (!concurrent) {
// start the next one
var i = [fileUploads indexOfObject:theFileUpload] + 1;
if (i < [fileUploads count]) {
[[fileUploads objectAtIndex:i] begin];
}
}
[self didChange];
if ([delegate respondsToSelector:@selector(fileUploadDidEnd:)])
[delegate fileUploadDidEnd:theFileUpload];
}
- (void)fileUpload:(DCFileUpload)anUpload didReceiveResponse:(CPString)aString
{
if ([delegate respondsToSelector:@selector(fileUpload:didReceiveResponse:)])
[delegate fileUpload:self didReceiveResponse:aString];
}
- (void)didChange {
if ([delegate respondsToSelector:@selector(fileUploadManagerDidChange:)]) {
[delegate fileUploadManagerDidChange:self];
}
}
@end