public
Description: A Mac OS X photo uploader program for my photo album.
Homepage: http://bleu.west.spy.net/~dustin/projects/photoupload/
Clone URL: git://github.com/dustin/photoupload.git
dustin (author)
Mon Oct 29 22:12:08 -0700 2007
commit  3645951ef6d97a1f99754d83550e44b768a9e680
tree    d779d45f73bc18c3acbd7b17eb5640c8655a38b1
parent  63f39e4d4d6371aeb1b7afb5ef375f57cea456f5
photoupload / UploadController.m
100644 382 lines (321 sloc) 10.732 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// arch-tag: 4FAC61DA-9196-11D8-88DB-000A957659CC
 
#import <CoreServices/CoreServices.h>
 
#import "UploadController.h"
#import "UploadThread.h"
#import "UploadParams.h"
#import "SizeScaler.h"
#import "PUImageList.h"
 
@interface UploadController (Private)
 
- (void)setButtonAction: (int)to;
 
@end
 
@implementation UploadController
 
- (void)alert:(id)title message:(id)msg
{
    NSRunAlertPanel(title, msg, @"OK", nil, nil);
}
 
- (IBAction)authenticate:(id)sender
{
    /* Set the defaults */
    [defaults setObject: [username stringValue] forKey:@"username"];
    [defaults setObject: [url stringValue] forKey:@"url"];
 
    NSURL *u=[[NSURL alloc] initWithString:[url stringValue]];
 
  NSDictionary *argDict=[NSDictionary dictionaryWithObjectsAndKeys:
    [username stringValue], @"username",
    [password stringValue], @"password", nil];
  
  NSArray *argOrder=[NSArray arrayWithObjects: @"username", @"password", nil];
  WSMethodInvocationRef rpcCall=WSMethodInvocationCreate(
      (CFURLRef)u, (CFStringRef)@"getCategories.getAddable",
      kWSXMLRPCProtocol);
  
  WSMethodInvocationSetParameters(rpcCall,
                  (CFDictionaryRef)argDict,
                  (CFArrayRef)argOrder);
 
  NSDictionary *result =
    (NSDictionary *)WSMethodInvocationInvoke(rpcCall);
 
  NSLog(@"Result: %@", result);
 
  if(WSMethodResultIsFault((CFDictionaryRef)result)) {
        [self alert:_str(@"Auth Exception")
      message:[result objectForKey: (NSString *) kWSFaultString]];
  } else {
    id rv=[result objectForKey: (NSString *)kWSMethodInvocationResult];
        /* Populate the categories */
        [categories removeAllItems];
        [categories addItemsWithTitles:rv];
 
        /* Out with the auth */
        [authWindow orderOut: self];
        /* In with the uploader */
    [self openUploadWindow: self];
  }
 
    // [defaults setObject: catList forKey:@"categories"];
 
    [u release];
  CFRelease(rpcCall);
  [result release];
}
 
- (IBAction)dateToToday:(id)sender
{
    id kw=[NSApp keyWindow];
    // Check to see if the key window is the window
    // for the other controller, and do the right thing.
    if(_batchController != nil && (kw == [_batchController window])) {
        [_batchController dateToToday:self];
    } else {
        NSDate *today = [NSDate date];
        NSString *datestr=
            [today descriptionWithCalendarFormat:
                            @"%Y/%m/%d" timeZone: nil
                                          locale: nil];
        [dateTaken setStringValue:datestr];
    }
}
 
- (IBAction)newBatch:(id)sender
{
  // Create a new batch controller if we don't have one.
  if(_batchController == nil) {
      NSLog(@"initializing controller.\n");
      _batchController = [[BatchController alloc] initWithWindowNibName:
          @"PhotoUploadBatch"];
      NSLog(@"Opening window.\n");
  }
    [authWindow orderOut: self];
    [_batchController showWindow:self];
}
 
- (IBAction)openAuthWindow:(id)sender
{
    [authWindow makeKeyAndOrderFront: self];
}
 
- (IBAction)openBatch:(id)sender
{
    id filePanel=[NSOpenPanel openPanel];
    [filePanel setAllowsMultipleSelection: FALSE];
    [filePanel setCanChooseDirectories: FALSE];
 
    id types=[NSArray arrayWithObjects:@"pbatch", nil];
    int rv = [filePanel runModalForTypes:types];
 
    if (rv == NSOKButton) {
        id batch = [NSKeyedUnarchiver unarchiveObjectWithFile:
            [[filePanel filenames] objectAtIndex: 0]];
        [categories selectItemWithTitle: [batch category]];
        [dateTaken setStringValue:
            [[batch taken] descriptionWithCalendarFormat: @"%Y/%m/%d"
                                                timeZone: nil
                                                  locale: nil ]];
        [description setStringValue: [batch description]];
        [keywords setStringValue: [batch keywords]];
 
        [imgStorage removeAllObjects];
        NSEnumerator *e=[[batch files] objectEnumerator];
        id object;
        while(object = [e nextObject]) {
            [imgStorage addPath: object];
        }
    }
}
 
- (IBAction)openUploadWindow:(id)sender
{
    [uploadWindow makeKeyAndOrderFront: self];
}
 
- (IBAction)removeAllFiles:(id)sender
{
    id kw=[NSApp keyWindow];
    if(_batchController != nil && (kw == [_batchController window])) {
        [[_batchController imgMatrix] clear];
    } else {
        [imgStorage removeAllObjects];
    }
}
 
- (IBAction)removeSelected:(id)sender
{
    id kw=[NSApp keyWindow];
    if(_batchController != nil && (kw == [_batchController window])) {
        [[_batchController imgMatrix] removeSelected];
    } else {
        [imgStorage removeSelected];
    }
}
 
- (IBAction)saveBatch:(id)sender
{
 
}
 
- (IBAction)selectFiles:(id)sender
{
    id filePanel=[NSOpenPanel openPanel];
    [filePanel setAllowsMultipleSelection: TRUE];
    [filePanel setCanChooseDirectories: FALSE];
 
    id types=[NSArray arrayWithObjects:@"jpg", @"jpeg", @"JPG",
        @"GIF", @"gif", @"png", @"PNG", nil];
    int rv = [filePanel runModalForTypes:types];
 
    if (rv == NSOKButton) {
        NSArray *files=[filePanel filenames];
        // This is what's displayed in the image box.
        int i=0;
        for(i=0; i<[files count]; i++) {
      [imgStorage addPath: [files objectAtIndex: i]];
        }
    }
}
 
- (IBAction)showFiles:(id)sender
{
    NSLog(@"Files: %@", imgStorage);
}
 
- (IBAction)showSelectedImages:(id)sender
{
  /*
NSArray *a=[imgMatrix selectedCells];
int i=0;
for(i=0; i<[a count]; i++) {
NSLog(@"Selected image: %@\n", [[a objectAtIndex: i] image]);
}
  */
}
 
- (IBAction)stopUpload:(id)sender
{
    [params setFinished: TRUE];
    [uploadButton setEnabled: FALSE];
}
 
- (IBAction)upload:(id)sender
{
    NSDate *date=[NSCalendarDate dateWithString: [dateTaken stringValue]
                                 calendarFormat: @"%Y/%m/%d"];
    NSString *k=[keywords stringValue];
    if([k length] == 0) {
        [self alert:_str(@"A.T.NoKeywords")
            message:_str(@"A.B.NoKeywords")];
        return;
    }
    NSString *d=[description stringValue];
    if([d length] == 0) {
        [self alert:_str(@"A.T.NoDescription")
            message:_str(@"A.B.NoDescription")];
        return;
    }
    NSString *cat=[categories titleOfSelectedItem];
    NSString *u=[username stringValue];
    NSString *p=[password stringValue];
    
  Batch *batch=[[Batch alloc] init];
 
    NSArray *files=[imgStorage images];
    [batch setUrl: [url stringValue]];
    [batch setUsername: u];
    [batch setPassword: p];
    [batch setKeywords: k];
    [batch setDescription: d];
    [batch setCategory: cat];
    [batch setTaken: date];
    [batch setFiles: files];
 
    if(params != nil) {
        [params release];
    }
    params=[[UploadParams alloc] init];
 
    [params setController: self];
    [params setUploadErrorMethod: @selector(uploadError:)];
    [params setUploadedFileMethod: @selector(uploadedFile)];
    [params setUploadCompleteMethod: @selector(uploadComplete)];
 
    // UI updates
    // Fix up the progress bar
    [progressBar setMinValue: 0];
    [progressBar setMaxValue: [files count]];
    [progressBar setDoubleValue: 0];
    [progressBar setHidden: FALSE];
    currentFile=1;
    // And the uploading text
    [self updateProgressText];
    [uploadingText setHidden: FALSE];
 
    UploadThread *ut=[[UploadThread alloc] init];
  [ut setBatch: batch];
    [NSThread detachNewThreadSelector: @selector(run:)
                                         toTarget:ut withObject: params];
 
    [self setButtonAction: BUTTON_STOP];
    // [addFilesButton setEnabled: FALSE];
    [ut release];
}
 
- (IBAction)uploadBatches:(id)sender
{
    id filePanel=[NSOpenPanel openPanel];
    [filePanel setAllowsMultipleSelection: TRUE];
    [filePanel setCanChooseDirectories: FALSE];
 
    id types=[NSArray arrayWithObjects:@"pbatch", nil];
    int rv = [filePanel runModalForTypes:types];
 
    if (rv == NSOKButton) {
 
        // Create a new batch controller if we don't have one.
        if(_batchUploadController == nil) {
            _batchUploadController=[[BatchUploadController alloc] initWithWindowNibName:
                @"BatchUpload"];
        }
        [_batchUploadController setUrl: [url stringValue]];
        [_batchUploadController setUsername: [username stringValue]];
        [_batchUploadController setPassword: [password stringValue]];
 
        [_batchUploadController processFiles: [filePanel filenames]];
    }
}
 
- (void)updateProgressText
{
    if(currentFile <= [imgStorage count])
    {
        NSString *msg=[NSString stringWithFormat:_str(@"UploadingText"),
            currentFile, [imgStorage count]];
        [uploadingText setStringValue: msg];
        [uploadingText displayIfNeeded];
    }
}
 
- (void)setButtonAction: (int)to
{
    switch(to) {
        case BUTTON_UPLOAD:
            [uploadButton setTitle:_str(@"B.Upload")];
            [uploadButton setAction:@selector(upload:)];
            [uploadButton setToolTip: _str(@"B.Upload.ToolTip")];
            break;
        case BUTTON_STOP:
            [uploadButton setTitle:_str(@"B.Stop")];
            [uploadButton setAction:@selector(stopUpload:)];
            [uploadButton setToolTip: _str(@"B.Stop.ToolTip")];
            break;
    }
    [uploadButton setNeedsDisplay: TRUE];
}
 
-(void)uploadError: (id)msg
{
    [self alert:_str(@"Upload Error") message: msg];
}
 
-(void)uploadedFile
{
    // NSLog(@"Uploaded a file.\n");
    currentFile++;
    [self updateProgressText];
    [progressBar incrementBy: 1];
}
 
-(void)uploadComplete
{
    NSLog(@"Upload is complete.\n");
    // [addFilesButton setEnabled: TRUE];
    [uploadingText setHidden: TRUE];
    [progressBar setMinValue: 0];
    [progressBar setMaxValue: 0];
    [progressBar setDoubleValue: 0];
    [progressBar setHidden: TRUE];
    [progressBar displayIfNeeded];
    [self setButtonAction: BUTTON_UPLOAD];
    [uploadButton setEnabled: TRUE];
}
 
- (void)awakeFromNib
{
    // Set up windows
    [uploadWindow orderOut: self];
    [progressBar setDisplayedWhenStopped: FALSE];
    // Initialize the button
    buttonType=BUTTON_UPLOAD;
    [self setButtonAction: buttonType];
 
    defaults=[NSUserDefaults standardUserDefaults];
    id defaultUrl=[defaults objectForKey:@"url"];
    id defaultUsername=[defaults objectForKey:@"username"];
    if(defaultUrl != nil) {
        [url setStringValue:defaultUrl];
    }
    if(defaultUsername != nil) {
        [username setStringValue:defaultUsername];
    }
 
    // Fill in form entries with defaults
    [self dateToToday: self];
 
    [imgStorage removeAllObjects];
  /*
[imgMatrix registerForDraggedTypes:[NSArray arrayWithObjects:
NSFilenamesPboardType, nil]];
  */
 
    [authWindow makeKeyAndOrderFront: self];
}
 
@end