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
photosync / PhotoClient.m
100644 222 lines (198 sloc) 5.756 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// PhotoClient.m
// PhotoSync
//
// Created by Dustin Sallings on 2/2/2005.
// Copyright 2005 Dustin Sallings <dustin@spy.net>. All rights reserved.
//
 
#import "PhotoClient.h"
 
@implementation PhotoClient
 
-initWithIndexPath:(NSString *)path
{
  id rv=[super init];
  indexPath=[path retain];
  return rv;
}
 
-(void)dealloc
{
  NSLog(@"Deallocing %@", self);
  [indexPath release];
  [el release];
  [current release];
  [keywords release];
  [photos release];
  [super dealloc];
}
 
-(BOOL)tryRequest:(NSURLRequest *)theRequest
{
  BOOL rv=FALSE;
  NSLog(@"Trying %@", [theRequest URL]);
  NSHTTPURLResponse *resp=nil;
  NSError *err=nil;
  [NSURLConnection sendSynchronousRequest:theRequest
    returningResponse:&resp error:&err];
  int rc=500;
  if(resp != nil) {
    rc=[resp statusCode];
  }
  if(rc == 200){
    rv=TRUE;
  } else {
    NSLog(@"%@ failed (rc=%d).", [theRequest URL], rc);
  }
  return(rv);
}
 
-(BOOL)authenticateTo:(NSString *)base
  user:(NSString *)u passwd:(NSString *)p forUser:(NSString *)altUser
{
  BOOL rv=FALSE;
  if(u != nil && [u length] > 0) {
    NSLog(@"Authenticating to %@ as %@", base, u);
    NSURL *url=[[NSURL alloc] initWithString:
      [base stringByAppendingString: @"/login.do"]];
  
    // We should post the credentials so they don't show up in the logs
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url
      cachePolicy:NSURLRequestReloadIgnoringCacheData
      timeoutInterval:60.0];
    [theRequest setHTTPMethod: @"POST"];
    NSString *bodyString=[[NSString alloc]
      initWithFormat:@"username=%@&password=%@", u, p];
    [theRequest setHTTPBody:
      [bodyString dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyString release];
 
    rv=[self tryRequest:theRequest];
 
    if(rv == TRUE && altUser != nil && [altUser length] > 0) {
      NSLog(@"Attempting to setuid");
      [url release];
      url=[[NSURL alloc] initWithString:
        [NSString stringWithFormat: @"%@/setuid.do?user=%@",
        base, altUser]];
      NSURLRequest *suReq=[NSURLRequest requestWithURL:url
        cachePolicy:NSURLRequestReloadIgnoringCacheData
        timeoutInterval:60.0];
      rv=[self tryRequest: suReq];
    }
    
    [url release];
  } else {
    NSLog(@"Not authenticating to %@ - no username", base);
    rv=TRUE;
  }
  return(rv);
}
 
-(void)fetchIndexFrom:(NSString *)base downloadDelegate:(id)del
{
  NSLog(@"Fetching index from %@ to %@", base, indexPath);
  NSURL *u=[[NSURL alloc] initWithString:
    [base stringByAppendingString: @"/export"]];
  NSURLRequest *theRequest=[NSURLRequest requestWithURL:u
    cachePolicy:NSURLRequestReloadIgnoringCacheData
    timeoutInterval:60.0];
  NSURLDownload *dl=[[NSURLDownload alloc]
    initWithRequest:theRequest delegate: del];
  if(dl != nil) {
    [dl setDestination:indexPath allowOverwrite:YES];
  } else {
    NSLog(@"Can't instantiate download from %@.", u);
  }
  [u release];
}
 
-(void)parseIndex
{
  NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
  NSURL *u=[[NSURL alloc] initFileURLWithPath:indexPath];
  NSXMLParser *xmlp=[[NSXMLParser alloc] initWithContentsOfURL: u];
 
  [xmlp setDelegate: self];
  if([xmlp parse]) {
    NSLog(@"Parse was successful.");
  } else {
    NSLog(@"Parse was NOT successful.");
  }
 
  [xmlp release];
  [u release];
 
  NSLog(@"Parsed %d photos", [photos count]);
  [pool release];
}
 
-(NSDictionary *)keywords
{
  return(keywords);
}
 
-(NSSet *)photos
{
  return(photos);
}
 
//
// This class also acts as a delegate for parsing the XML
//
 
#define PS_SEC_NONE 0
#define PS_SEC_KEYWORDS 1
#define PS_SEC_ALBUM 2
 
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
  attributes:(NSDictionary *)attributeDict
{
  if([@"photoexport" isEqualToString: elementName]) {
    section=PS_SEC_NONE;
  } else if([@"photo" isEqualToString: elementName]) {
    [current release];
    current = [[NSMutableDictionary alloc] initWithCapacity:5];
  } else if([@"album" isEqualToString: elementName]) {
    section=PS_SEC_ALBUM;
    [photos release];
    photos = [[NSMutableSet alloc] initWithCapacity:5];
  } else if([@"keywordmap" isEqualToString: elementName]) {
    section=PS_SEC_KEYWORDS;
    [keywords release];
    keywords = [[NSMutableDictionary alloc] initWithCapacity:5];
  } else if([@"keywords" isEqualToString: elementName]) {
    NSMutableSet *ms=[[NSMutableSet alloc] initWithCapacity:5];
    [current setObject: ms forKey:@"keywords"];
    [ms release];
  } else if([@"keyword" isEqualToString: elementName]) {
    NSString *kwid=[attributeDict objectForKey: @"id"];
    NSNumber *n=[[NSNumber alloc] initWithInt: [kwid intValue]];
    if(section == PS_SEC_KEYWORDS) {
      [keywords setObject: [attributeDict objectForKey: @"word"]
        forKey: n];
    } else {
      [[current objectForKey: @"keywords"] addObject: n];
    }
    [n release];
  } else {
    [el release];
    el = [elementName retain];
  }
}
 
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
  if([@"photo" isEqualToString: elementName]) {
    Photo *photo=[[Photo alloc] initWithDict:current keywordMap:keywords];
    // NSLog(@"Finished %@", photo);
    [photos addObject:photo];
    [current release];
    current=nil;
    [el release];
    el=nil;
    [photo release];
  }
}
 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
  if(el != nil && current != nil) {
    NSString *trimmed=[string
      stringByTrimmingCharactersInSet:
        [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *curVal=[current objectForKey:el];
    if(curVal == nil) {
      [current setObject:trimmed forKey:el];
    } else {
      if([trimmed length] > 0) {
        NSString *pad=@"";
        [current setObject:[NSString stringWithFormat:@"%@%@%@",
            curVal, pad, trimmed]
          forKey:el];
      }
    }
  }
}
 
@end