public
Description: An OS X application to build a standalone web site as an export from my photo album.
Homepage: http://bleu.west.spy.net/~dustin/projects/photosync/
Clone URL: git://github.com/dustin/photosync.git
Search Repo:
photosync / SyncSubTask.m
100644 176 lines (154 sloc) 3.934 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
//
// SyncSubTask.m
// PhotoSync
//
// Created by Dustin Sallings on 2005/2/4.
// Copyright 2005 Dustin Sallings. All rights reserved.
//
 
#import "PhotoSync.h"
#import "SyncSubTask.h"
#import "SyncTask.h"
#import "Location.h"
#import "PageWriter.h"
 
@interface SyncSubTaskDelegate
-(void)completedTask:(id)sender;
@end
 
@implementation SyncSubTask
 
-initWithName:(NSString *)n location:(Location *)l
  photo:(NSString *)p delegate:(id)del
{
  id rv=[super init];
  location=[l retain];
  name=[n retain];
  photo=[p retain];
  delegate=[del retain];
  imgsToFetch=[[NSMutableArray alloc] init];
  // Like synctask, I'll retain myself
  [self retain];
  return rv;
}
 
-(NSString *)name
{
  return name;
}
 
-(void)fetch:(NSString *)u to:(NSString *)dest
{
  NSURL *url=[[NSURL alloc] initWithString:u];
  NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
    cachePolicy:NSURLRequestUseProtocolCachePolicy
    timeoutInterval:60.0];
  NSURLDownload *dl=[[NSURLDownload alloc]
    initWithRequest:theRequest delegate: self];
  if(dl != nil) {
    [dl setDestination:dest allowOverwrite:YES];
    [imgsToFetch addObject: dl];
  } else {
    [NSException raise:@"FetchImage" format:@"Couldn't fetch image from %@",
      u];
  }
  [url release];
}
 
-(void)fetchTn:(NSString *)path
{
  NSString *u=[[NSString alloc]
    initWithFormat:@"%@/PhotoServlet?id=%d&thumbnail=1",
      [location url], [photo imgId]];
  [self fetch:u to:path];
  [u release];
}
 
-(void)fetchNormal:(NSString *)path
{
  // XXX: hard-coded scale
  NSString *u=[[NSString alloc]
    initWithFormat:@"%@/PhotoServlet?id=%d&scale=800x600",
      [location url], [photo imgId]];
  [self fetch:u to:path];
  [u release];
}
 
-(void)complete
{
  if([delegate respondsToSelector:@selector(completedTask:)]) {
    [delegate completedTask:self];
  }
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [self release];
}
 
- (void)downloadDidFinish:(NSURLDownload *)download
{
  // NSLog(@"%@ completed successfully", [[download request] URL]);
  [imgsToFetch removeObject: download];
  [download release];
  if([imgsToFetch count] == 0) {
    [self complete];
  }
}
 
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
  NSLog(@"%@ failed", [[download request] URL]);
  [imgsToFetch removeObject: download];
  [download release];
  if([imgsToFetch count] == 0) {
    [self complete];
  }
}
 
-(void)stopDownloads:(id)sender
{
  NSLog(@"Subtask needs to stop downloads");
  NSEnumerator *e=[imgsToFetch objectEnumerator];
  id dl=nil;
  while(dl = [e nextObject]) {
    NSLog(@"Cancelling download of %@", [[dl request] URL]);
    [dl cancel];
  }
  [imgsToFetch removeAllObjects];
  [self complete];
}
 
-(void)run
{
  // See if the images are there
  NSFileManager *fm=[NSFileManager defaultManager];
  NSString *normalFn=[[NSString alloc]
    initWithFormat:@"%@/pages/%04d/%02d/%d_normal.jpg",
      [location destDir], [photo year], [photo month], [photo imgId]];
  NSString *tnFn=[[NSString alloc]
    initWithFormat:@"%@/pages/%04d/%02d/%d_tn.jpg",
      [location destDir], [photo year], [photo month], [photo imgId]];
 
  if(![fm fileExistsAtPath:normalFn]) {
    [self fetchNormal:normalFn];
  }
  if(![fm fileExistsAtPath:tnFn]) {
    [self fetchTn:tnFn];
  }
 
  NSString *pageLoc=[[NSString alloc]
    initWithFormat:@"%@/pages/%04d/%02d/%d.html",
      [location destDir], [photo year], [photo month], [photo imgId]];
 
  PageWriter *pw=[PageWriter sharedInstance];
  [pw writePage:@"page" dest:pageLoc tokens:[photo tokens]];
 
  [pageLoc release];
  [normalFn release];
  [tnFn release];
 
  // If we don't have anything to do go ahead and shut down.
  if([imgsToFetch count] == 0) {
    [self complete];
  } else {
    [[NSNotificationCenter defaultCenter]
      addObserver:self
      selector:@selector(stopDownloads:)
      name: PS_STOP
      object: nil];
  }
 
}
 
-(void)cancel
{
  [self release];
}
 
-(void)dealloc
{
  [imgsToFetch release];
  [location release];
  [name release];
  [photo release];
  [delegate release];
  [super dealloc];
}
 
@end