Take the 2008 Git User's Survey and help out! [ hide ]

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:
dustin (author)
Wed Nov 30 21:51:37 -0800 2005
commit  2dceca4018192b712c3dcbd872354326347bca00
tree    ad7e3af5f358026eee3aca0e8acc7a23e1024380
parent  08dbe9d7485be04072af84b3db49e976a0564577
photosync / PhotoClient.m
100644 220 lines (198 sloc) 5.987 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
//
// PhotoClient.m
// PhotoSync
//
// Created by Dustin Sallings on 2/2/2005.
// Copyright 2005 Dustin Sallings <dustin@spy.net>. All rights reserved.
//
 
#import "PhotoClient.h"
#import "PhotoSync.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];
  [photos release];
  [super dealloc];
}
 
-(BOOL)tryRequest:(NSURLRequest *)theRequest
{
  BOOL rv=NO;
  NSLog(@"Trying %@", [theRequest URL]);
  NSHTTPURLResponse *resp=nil;
  NSError *err=nil;
  NSData *body=[NSURLConnection sendSynchronousRequest:theRequest
    returningResponse:&resp error:&err];
  int rc=500;
  if(resp != nil) {
    rc=[resp statusCode];
  }
  if(rc == 200 || (rc >= 300 && rc < 400)) {
    rv=YES;
  } else {
    NSLog(@"%@ failed (rc=%d).", [theRequest URL], rc);
    NSString *str=[[NSString alloc] initWithData: body
      encoding:NSUTF8StringEncoding];
    NSLog(@"Response data: %@", str);
    [str release];
  }
  return(rv);
}
 
-(BOOL)authenticateTo:(NSString *)base
  user:(NSString *)u passwd:(NSString *)p forUser:(NSString *)altUser
{
  BOOL rv=NO;
  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 alloc]
      initWithURL: 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 == YES && 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];
    [theRequest autorelease];
  } else {
    NSLog(@"Not authenticating (but logging out) %@ - no username", base);
    NSURL *url=[[NSURL alloc] initWithString:
      [base stringByAppendingString: @"/logout.do"]];
    NSURLRequest *req=[NSURLRequest requestWithURL:url
      cachePolicy:NSURLRequestReloadIgnoringCacheData
      timeoutInterval:60.0];
    rv=[self tryRequest: req];
    [url release];
  }
  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];
}
 
-(NSSet *)photos
{
  return(photos);
}
 
//
// This class also acts as a delegate for parsing the XML
//
 
#define PS_SEC_NONE 0
#define PS_SEC_ALBUM 2
#define PS_SEC_ANNOTATIONS 3
 
-(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([@"keywords" isEqualToString: elementName]) {
    NSMutableSet *ms=[[NSMutableSet alloc] initWithCapacity:5];
    [current setObject: ms forKey:@"keywords"];
    [ms release];
  } else if([@"keyword" isEqualToString: elementName]) {
    [[current objectForKey: @"keywords"] addObject: [attributeDict objectForKey: @"word"]];
  } else if([@"annotations" isEqualToString: elementName]) {
    section=PS_SEC_ANNOTATIONS;
  } 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];
    // NSLog(@"Finished %@", photo);
    [photos addObject:photo];
    [current release];
    current=nil;
    [el release];
    el=nil;
    [photo release];
  } else if([@"annotations" isEqualToString: elementName]) {
    section=PS_SEC_ALBUM;
  }
}
 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
  if(section != PS_SEC_ANNOTATIONS && 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