forked from jonmarimba/CocoaREST
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppDelegate.m
123 lines (92 loc) · 3.63 KB
/
AppDelegate.m
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
//
// AppDelegate.m
// SDNet
//
// Created by Steven Degutis on 5/27/09.
// Copyright 2009 Thoughtful Tree Software. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize isWaiting;
@synthesize results;
- (void) applicationDidFinishLaunching:(NSNotification*)notification {
manager = [[SDTwitterTaskManager manager] retain];
manager.delegate = self;
manager.successSelector = @selector(twitterManager:resultsReadyForTask:);
manager.failSelector = @selector(twitterManager:failedForTask:);
manager.maxConcurrentTasks = 3;
}
- (IBAction) runTask:(id)sender {
[self runUserTask];
}
- (void) runUserTask {
manager.username = [userField stringValue];
manager.password = [passField stringValue];
SDTwitterTask *basicTask = [SDTwitterTask taskWithManager:manager];
basicTask.type = [[taskTypeButton selectedItem] tag];
[basicTask run];
self.isWaiting = YES;
}
- (IBAction) findImage:(id)sender {
NSOpenPanel *imagePanel = [NSOpenPanel openPanel];
NSArray *imageTypes = [NSArray arrayWithObjects:@"gif", @"jpg", @"png", nil];
// Only allow a single file to be chosen
[imagePanel setCanChooseFiles:YES];
[imagePanel setCanChooseDirectories:NO];
[imagePanel setAllowsMultipleSelection:NO];
// Display the sheet and only allow valid Twitter profile image types (gif, png, jpg)
if ([imagePanel runModalForTypes:imageTypes] == NSOKButton) {
[self changeTwitterProfileImage:[[imagePanel filenames] objectAtIndex:0]];
}
}
- (void) changeTwitterProfileImage:(NSString *)imagePath {
NSNumber *imageSize = [self getImageSize:imagePath];
// Verify the image size is under the 700K limit
if ([imageSize isGreaterThan:[NSNumber numberWithInteger:700]]) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
NSString *errorMessage = [[[NSString alloc] initWithFormat:@"The maximum size for the image is 700K. Yours is %@K.", imageSize] autorelease];
[alert setMessageText:@"Error"];
[alert setInformativeText:errorMessage];
[alert runModal];
} else {
manager.successSelector = @selector(twitterManager:resultsReadyForChangeProfileImageTask:);
SDTwitterTask *changeImageTask = [SDTwitterTask taskWithManager:manager];
changeImageTask.type = SDTwitterTaskUpdateProfileImage;
NSURL *newImage = [[NSURL alloc] initFileURLWithPath:imagePath];
changeImageTask.imageToUpload = newImage;
[changeImageTask run];
self.isWaiting = YES;
}
}
- (NSNumber *) getImageSize:(NSString *)imagePath {
unsigned long long size = 0;
NSFileManager * fm = [NSFileManager defaultManager];
NSDictionary *fattrs = [fm fileAttributesAtPath:imagePath traverseLink:NO];
size = [[fattrs objectForKey:NSFileSize] unsignedLongLongValue];
return [NSNumber numberWithUnsignedLongLong:size / 1024];
}
- (void) tableViewSelectionDidChange:(NSNotification *)notification {
// Only enable the "Change..." button when the user selected is the
// currently logged in user.
if ([[manager username] isEqualToString:[userLabel stringValue]]) {
[changeButton setEnabled:YES];
} else {
[changeButton setEnabled:NO];
}
}
- (void) twitterManager:(SDTwitterTaskManager*)manager resultsReadyForTask:(SDTwitterTask*)task {
self.isWaiting = NO;
self.results = task.results;
}
- (void) twitterManager:(SDTwitterTaskManager*)manager failedForTask:(SDTwitterTask*)task {
self.isWaiting = NO;
self.results = nil;
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:@"Error"];
[alert setInformativeText:[task.error localizedDescription]];
[alert runModal];
}
- (void) twitterManager:(SDTwitterTaskManager*)manager resultsReadyForChangeProfileImageTask:(SDTwitterTask*)task {
self.isWaiting = NO;
}
@end