dustin / photosync

An OS X application to build a standalone web site as an export from my photo album.

photosync / SyncTask.m
100644 198 lines (171 sloc) 4.815 kb
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
198
//
// SyncTask.m
// PhotoSync
//
// Created by Dustin Sallings on 2005/2/3.
// Copyright 2005 Dustin Sallings. All rights reserved.
//
 
#import "SyncTask.h"
#import "SyncSubTask.h"
#import "PhotoSync.h"
#import "PageWriter.h"
 
@interface SyncTaskDelegate
-(void)completedTask:(SyncTask *)task;
-(void)updateStatus:(NSString *)msg with:(int)done of:(int)total;
@end
 
@implementation SyncTask
 
-initWithLocation:(Location *)loc delegate:(id)del
{
id rv=[super init];
subTasks=[[NSMutableArray alloc] init];
location=[loc retain];
delegate=[del retain];
NSString *idxpath=[[location destDir]
stringByAppendingPathComponent: @"index.xml"];
photoClient=[[PhotoClient alloc] initWithIndexPath:idxpath];
 
BOOL authed=[photoClient authenticateTo:[location url]
user:[location username] passwd:[location password]
forUser:[location forUser]];
if(authed) {
NSLog(@"Authenticated");
} else {
NSLog(@"Authentication failed");
[rv release];
rv=nil;
}
 
// OK, this is a bit weird, but it happens outside of the
// run loop, so it's going to retain and release itself
[rv retain];
return rv;
}
 
-(void)stopSubTasks:(id)sender
{
NSLog(@"Subtask stopping.");
// First cancel the objects so we can release
NSEnumerator *e=[subTasks objectEnumerator];
id object=nil;
while(object = [e nextObject]) {
[object cancel];
}
[subTasks removeAllObjects];
}
 
-(NSString *)description
{
return([NSString stringWithFormat:@"<SyncTask loc=%@>", location]);
}
 
-(void)updateStatus:(NSString *)msg with:(int)done of:(int)total
{
if([delegate respondsToSelector:@selector(updateStatus:with:of:)]) {
[delegate updateStatus:msg with:done of:total];
}
}
 
-(void)doNextTask:(id)sender
{
// NSLog(@"doNextTask: called.");
if([subTasks count] == 0) {
NSLog(@"All subtasks complete.");
// At this point, we have nothing left to offer. Inform the delegate
// and release self
if([delegate respondsToSelector:@selector(completedTask:)]) {
[delegate completedTask:self];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self release];
} else {
// NSLog(@"Starting a subtask");
SyncSubTask *task=[subTasks lastObject];
[task retain];
[subTasks removeLastObject];
 
int todo=[[photoClient photos] count];
int done=todo - [subTasks count];
 
[self updateStatus:[NSString
stringWithFormat:@"Processing %@", [task name]] with:done of:todo];
[task run];
 
[task release];
}
}
 
-(void)completedTask:(SyncTask *)task
{
// NSLog(@"Completed subtask: %@", task);
// Figure out where where are
[self performSelector:@selector(doNextTask:) withObject:self afterDelay:0];
}
 
-(void)cancel
{
[self release];
}
 
-(void)run
{
NSLog(@"Running %@", self);
 
BOOL authed=[photoClient authenticateTo:[location url]
user:[location username] passwd:[location password]
forUser:[location forUser]];
if(authed) {
NSLog(@"Authenticated");
// Register myself for stopping
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(stopSubTasks:)
name: PS_STOP
object: nil];
 
[self updateStatus: [@"Fetching index from "
stringByAppendingString: [location url]] with:0 of:0];
[photoClient fetchIndexFrom: [location url] downloadDelegate:self];
} else {
NSLog(@"Authentication failed");
}
}
 
- (void)download:(NSURLDownload *)download
didReceiveResponse:(NSURLResponse *)response
{
/*
NSLog(@"Received response from %@, expected length is %d",
[location url], [response expectedContentLength]);
*/
}
 
- (void)download:(NSURLDownload *)download
didReceiveDataOfLength:(unsigned)l
{
// NSLog(@"Received %u bytes of data from %@", l, [location url]);
}
 
- (void)downloadDidFinish:(NSURLDownload *)download
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSLog(@"Finished downloading index from %@, beginning parse",
[location url]);
    [photoClient parseIndex];
[subTasks removeAllObjects];
NSEnumerator *e=[[photoClient photos] objectEnumerator];
id object=nil;
NSLog(@"Creating tasks...");
while(object = [e nextObject]) {
NSString *n=[[NSString alloc]
initWithFormat:@"Photo %d", [object imgId]];
SyncSubTask *sst=[[SyncSubTask alloc]
initWithName:n location:location photo:object delegate:self];
[n release];
[subTasks addObject:sst];
[sst release];
}
NSLog(@"Created %d tasks", [subTasks count]);
 
PageWriter *pw=[PageWriter sharedInstance];
[pw setupPages:photoClient location:location];
 
[pool release];
[download release];
[self doNextTask:self];
}
 
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
NSLog(@"%@ failed", [[download request] URL]);
[download release];
}
 
-(void)dealloc
{
// NSLog(@"Deallocing %@", self);
[subTasks release];
[location release];
[photoClient release];
[delegate release];
[super dealloc];
}
 
@end