0
#import "SyncSubTask.h"
0
@interface SyncTaskDelegate
0
-(void)completedTask:(SyncTask *)task;
0
[photoClient fetchIndexFrom: [location url] downloadDelegate:self];
0
+-(void)ensureDir:(NSString *)path
0
+ NSFileManager *fm=[NSFileManager defaultManager];
0
+ if([fm fileExistsAtPath:path isDirectory:&isDir] && isDir) {
0
+ NSLog(@"%@ exists", path);
0
+ NSLog(@"Creating pages dir");
0
+ if(![fm createDirectoryAtPath:path attributes:nil]) {
0
+ [NSException raise:@"CheckPath" format:@"Couldn't create dir %@",
0
+// Format for the index on the index page
0
+#define INDEX_INDEX_FMT \
0
+ @"<li><a href=\"pages/%@.html\">%@ (%d %@)</a></li>\n"
0
+// Format for the month list on a year page
0
+#define YEAR_MONTH_INDEX_FMT \
0
+ @"<li><a href=\"%@/%02d.html\">%02d (%d %@)</a></li>"
0
+// Format string for lines in the month index
0
+#define MONTH_IMG_INDEX_FMT \
0
+ @"<a href=\"%02d/%d.html\"><img alt=\"%d\" src=\"%02d/%d_tn.%@\"/></a>\n"
0
+-(void)writePages:(NSCountedSet *)yearSet months:(NSDictionary *)months
0
+ PageWriter *pw=[PageWriter sharedInstance];
0
+ // Make array versions of the years so we can sort them
0
+ NSMutableArray *years=[[NSMutableArray alloc]
0
+ initWithCapacity:[yearSet count]];
0
+ NSEnumerator *e=[yearSet objectEnumerator];
0
+ while(year = [e nextObject]) {
0
+ [years addObject: year];
0
+ [years sortUsingSelector:@selector(compare:)];
0
+ NSMutableString *indexIdx=[[NSMutableString alloc] initWithCapacity:256];
0
+ NSMutableString *yearIdx=[[NSMutableString alloc] initWithCapacity:256];
0
+ NSMutableString *monthIdx=[[NSMutableString alloc] initWithCapacity:256];
0
+ [indexIdx setString:@""];
0
+ e=[years objectEnumerator];
0
+ while(year = [e nextObject]) {
0
+ [yearIdx setString:@""];
0
+ NSString *yearDir=[[NSString alloc] initWithFormat:@"%@/pages/%@",
0
+ [location destDir], year];
0
+ NSString *yearFile=[[NSString alloc] initWithFormat:@"%@/pages/%@.html",
0
+ [location destDir], year];
0
+ [self ensureDir:yearDir];
0
+ // Set up the index line
0
+ int yearCount=[yearSet countForObject: year];
0
+ [indexIdx appendFormat:INDEX_INDEX_FMT,
0
+ year, year, yearCount, (yearCount == 1 ? @"image":@"images")];
0
+ NSLog(@"OK, doing the months");
0
+ for(i=1; i<=12; i++) {
0
+ NSString *monthStr=[[NSString alloc] initWithFormat:@"%@/%02d",
0
+ id monthList=[months objectForKey:monthStr];
0
+ if(monthList != nil) {
0
+ [monthIdx setString:@""];
0
+ [yearIdx appendFormat:YEAR_MONTH_INDEX_FMT,
0
+ year, i, i, [monthList count],
0
+ ([monthList count] == 1 ? @"image":@"images")];
0
+ NSString *monthDir=[[NSString alloc]
0
+ initWithFormat:@"%@/pages/%@/%02d",
0
+ [location destDir], year, i];
0
+ [self ensureDir:monthDir];
0
+ NSString *monthFile=[[NSString alloc]
0
+ initWithFormat:@"%@/pages/%@/%02d.html",
0
+ [location destDir], year, i];
0
+ // Images within a month
0
+ NSEnumerator *ye=[monthList objectEnumerator];
0
+ while(photo = [ye nextObject]) {
0
+ [monthIdx appendFormat:MONTH_IMG_INDEX_FMT,
0
+ i, [photo imgId], [photo imgId], i, [photo imgId],
0
+ NSDictionary *toks=[[NSDictionary alloc] initWithObjectsAndKeys:
0
+ [NSString stringWithFormat:@"%02d"], @"MONTH",
0
+ [pw writePage:@"month" dest:monthFile tokens:toks];
0
+ NSLog(@"No month %@", monthStr);
0
+ } // All possible months
0
+ NSDictionary *toks=[[NSDictionary alloc] initWithObjectsAndKeys:
0
+ [pw writePage:@"year" dest:yearFile tokens:toks];
0
+ // Now write the final index file
0
+ NSString *idxFile=[[NSString alloc] initWithFormat:@"%@/index.html",
0
+ NSDictionary *toks=[[NSDictionary alloc] initWithObjectsAndKeys:
0
+ indexIdx, @"YEARS", nil];
0
+ [pw writePage:@"index" dest:idxFile tokens:toks];
0
+ [pw copyMiscFiles:[location destDir]];
0
+ NSLog(@"Setting up the main pages.");
0
+ NSString *pagesDir=[[NSString alloc] initWithFormat:@"%@/pages",
0
+ [self ensureDir:pagesDir];
0
+ NSCountedSet *yearSet=[[NSCountedSet alloc] initWithCapacity:50];
0
+ NSMutableDictionary *imgLists=[[NSMutableDictionary alloc]
0
+ initWithCapacity:500];
0
+ NSEnumerator *e=[[photoClient photos] objectEnumerator];
0
+ while(photo = [e nextObject]) {
0
+ NSString *year=[[NSString alloc] initWithFormat:@"%04d", [photo year]];
0
+ NSString *month=[[NSString alloc] initWithFormat:@"%04d/%02d",
0
+ [photo year], [photo month]];
0
+ [yearSet addObject: year];
0
+ NSMutableArray *imgList=[imgLists objectForKey:month];
0
+ imgList=[[NSMutableArray alloc] initWithCapacity:128];
0
+ [imgLists setObject:imgList forKey:month];
0
+ [imgList addObject: photo];
0
+ // Now we're going to go through the values and sort them.
0
+ e=[imgLists objectEnumerator];
0
+ while(imgList = [e nextObject]) {
0
+ [imgList sortUsingSelector:@selector(compare:)];
0
+ // Now that we have our structures, let's write out some pages
0
+ [self writePages:yearSet months:imgLists];
0
- (void)downloadDidFinish:(NSURLDownload *)download
0
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
0
NSLog(@"Created %d tasks", [subTasks count]);
0
- NSString *pagesDir=[[NSString alloc] initWithFormat:@"%@/pages",
0
- NSFileManager *fm=[NSFileManager defaultManager];
0
- if([fm fileExistsAtPath:pagesDir isDirectory:&isDir] && isDir) {
0
- NSLog(@"%@ exists", pagesDir);
0
- NSLog(@"Creating pages dir");
0
- if(![fm createDirectoryAtPath:pagesDir attributes:nil]) {
0
- [NSException raise:@"CheckPath" format:@"Couldn't create dir %@",
0
[self doNextTask:self];
Comments
No one has commented yet.