forked from prisonerjohn/Scrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrapePrefsController.m
306 lines (253 loc) · 12.2 KB
/
ScrapePrefsController.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
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
//
// ScrapePrefsController.m
// Scrape
//
// Created by Elie Zananiri on 10-03-12.
// Copyright 2010 silentlyCrashing::net. All rights reserved.
//
#import "ScrapePrefsController.h"
#import "ScrapeAppDelegate.h"
#import "sCLoginItemsManager.h"
#import <Growl/Growl.h>
//--------------------------------------------------------------
//--------------------------------------------------------------
static BOOL loggedIn = NO;
NSString *KeychainUsername = nil;
NSString *KeychainPassword = nil;
//--------------------------------------------------------------
//--------------------------------------------------------------
@implementation ScrapePrefsController
//--------------------------------------------------------------
- (id)init {
self = [super initWithWindowNibName:@"Preferences"];
if (self) {
[self loadWindow];
return self;
}
return nil;
}
//--------------------------------------------------------------
- (void)awakeFromNib {
NSLog(@"Loading Preferences");
// hide status labels
[successLabel setHidden:YES];
[errorLabel setHidden:YES];
// set login item preference
//if ([sCLoginItemsManager loginItemExistsForAppPath:[[NSBundle mainBundle] bundlePath]]) {
if ([sCLoginItemsManager willStartAtLogin:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]] == YES) {
[startAtLoginSwitch setState:NSOnState];
} else {
[startAtLoginSwitch setState:NSOffState];
}
// set other saved preferences
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[showInDockSwitch setState:[defaults boolForKey:ScrapeEnableDockIconKey]];
if ([showInDockSwitch state] == NSOffState) {
// if the dock icon is hidden, force the menu bar icon
[showInMenuBarSwitch setState:NSOnState];
[showInMenuBarSwitch setEnabled:NO];
} else {
[showInMenuBarSwitch setState:[defaults boolForKey:ScrapeEnableMenuBarIconKey]];
}
[showGrowlNotificationsSwitch setState:[defaults boolForKey:ScrapeShowGrowlNotificationsKey]];
[automaticSwitch setState:[defaults boolForKey:ScrapeAutomaticToggleKey]];
[automaticMinStepper setIntegerValue:[defaults integerForKey:ScrapeAutomaticMinKey]];
[automaticMinStepper setEnabled:[automaticSwitch state]];
[automaticMaxStepper setIntegerValue:[defaults integerForKey:ScrapeAutomaticMaxKey]];
[automaticMaxStepper setEnabled:[automaticSwitch state]];
[automaticMinLabel setIntegerValue:[defaults integerForKey:ScrapeAutomaticMinKey]];
[automaticMinLabel setEnabled:[automaticSwitch state]];
[automaticMaxLabel setIntegerValue:[defaults integerForKey:ScrapeAutomaticMaxKey]];
[automaticMaxLabel setEnabled:[automaticSwitch state]];
[destroyDataSwitch setState:[defaults boolForKey:ScrapeDestroyDataOnReleaseKey]];
// try to load the credentials from the keychain
NSURL *url = [NSURL URLWithString:[SiteRoot stringByAppendingString:@"verify.php"]];
NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForHost:[url host] port:[[url port] intValue] protocol:[url scheme] realm:nil];
if (authenticationCredentials) {
KeychainUsername = [authenticationCredentials user];
KeychainPassword = [authenticationCredentials password];
if (KeychainUsername && KeychainPassword) {
NSLog(@"Successfully retrieved credentials from keychain");
// update input fields
[usernameInput setStringValue:KeychainUsername];
[passwordInput setStringValue:KeychainPassword];
// try logging in
[self loginToScrape:nil];
}
}
}
//--------------------------------------------------------------
- (void)dealloc {
[super dealloc];
}
//--------------------------------------------------------------
- (IBAction)setStartAtLogin:(id)sender {
[sCLoginItemsManager setStartAtLogin:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]
enabled:[sender state]];
// if ([sender state] == NSOnState) {
// [sCLoginItemsManager enableLoginItemForAppPath:[[NSBundle mainBundle] bundlePath]];
// } else {
// [sCLoginItemsManager disableLoginItemForAppPath:[[NSBundle mainBundle] bundlePath]];
// }
}
//--------------------------------------------------------------
- (IBAction)setShowInDock:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:[sender state]
forKey:ScrapeEnableDockIconKey];
[defaults synchronize];
if ([sender state] == NSOffState) {
// if the dock icon is hidden, force the menu bar icon
[showInMenuBarSwitch setState:NSOnState];
[showInMenuBarSwitch setEnabled:NO];
} else {
[showInMenuBarSwitch setEnabled:YES];
}
}
//--------------------------------------------------------------
- (IBAction)setShowInMenuBar:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:[sender state]
forKey:ScrapeEnableMenuBarIconKey];
[defaults synchronize];
}
//--------------------------------------------------------------
- (IBAction)setShowGrowlNotifications:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:[sender state]
forKey:ScrapeShowGrowlNotificationsKey];
[defaults synchronize];
}
//--------------------------------------------------------------
- (IBAction)setAutomaticToggle:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:[sender state]
forKey:ScrapeAutomaticToggleKey];
[defaults synchronize];
[automaticMinLabel setEnabled:[sender state]];
[automaticMaxLabel setEnabled:[sender state]];
[automaticMinStepper setEnabled:[sender state]];
[automaticMaxStepper setEnabled:[sender state]];
[[NSNotificationCenter defaultCenter] postNotificationName:ScrapeAutomaticSettingsChangedKey
object:self];
}
//--------------------------------------------------------------
- (IBAction)setAutomaticMinFromStepper:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[sender intValue]
forKey:ScrapeAutomaticMinKey];
[defaults synchronize];
[automaticMinLabel setIntValue:[sender intValue]];
[automaticMaxStepper setMinValue:([sender intValue] + 1)];
[[automaticMaxLabel formatter] setMinimum:[NSNumber numberWithInt:([sender intValue] + 1)]];
[[NSNotificationCenter defaultCenter] postNotificationName:ScrapeAutomaticSettingsChangedKey
object:self];
}
//--------------------------------------------------------------
- (IBAction)setAutomaticMinFromTextField:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[sender intValue]
forKey:ScrapeAutomaticMinKey];
[defaults synchronize];
[automaticMinStepper setIntValue:[sender intValue]];
[automaticMaxStepper setMinValue:([sender intValue] + 1)];
[[automaticMaxLabel formatter] setMinimum:[NSNumber numberWithInt:([sender intValue] + 1)]];
[[NSNotificationCenter defaultCenter] postNotificationName:ScrapeAutomaticSettingsChangedKey
object:self];
}
//--------------------------------------------------------------
- (IBAction)setAutomaticMaxFromStepper:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[sender intValue]
forKey:ScrapeAutomaticMaxKey];
[defaults synchronize];
[automaticMaxLabel setIntValue:[sender intValue]];
[automaticMinStepper setMaxValue:([sender intValue] - 1)];
[[automaticMinLabel formatter] setMaximum:[NSNumber numberWithInt:([sender intValue] - 1)]];
[[NSNotificationCenter defaultCenter] postNotificationName:ScrapeAutomaticSettingsChangedKey
object:self];
}
//--------------------------------------------------------------
- (IBAction)setAutomaticMaxFromTextField:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[sender intValue]
forKey:ScrapeAutomaticMaxKey];
[defaults synchronize];
[automaticMaxStepper setIntValue:[sender intValue]];
[automaticMinStepper setMaxValue:([sender intValue] - 1)];
[[automaticMinLabel formatter] setMaximum:[NSNumber numberWithInt:([sender intValue] - 1)]];
[[NSNotificationCenter defaultCenter] postNotificationName:ScrapeAutomaticSettingsChangedKey
object:self];
}
//--------------------------------------------------------------
- (IBAction)setDestroyData:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:[sender state]
forKey:ScrapeDestroyDataOnReleaseKey];
[defaults synchronize];
}
//--------------------------------------------------------------
- (IBAction)loginToScrape:(id)sender {
NSURL *url = [NSURL URLWithString:[SiteRoot stringByAppendingString:@"verify.php"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"User-Agent" value:@"Scrape-User-Agent-1.0"];
[request setPostValue:[usernameInput stringValue]
forKey:@"username"];
[request setPostValue:[passwordInput stringValue]
forKey:@"password"];
[request setDelegate:self];
[request startAsynchronous];
}
//--------------------------------------------------------------
- (IBAction)signupForScrape:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[SiteRoot stringByAppendingString:@"register.php"]]];
}
//--------------------------------------------------------------
- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];
if ([responseString compare:@"OK"] == NSOrderedSame) {
NSLog(@"Successfully logged in");
[successLabel setHidden:NO];
[errorLabel setHidden:YES];
// add the saved username and password to the keychain
NSMutableDictionary *credentials = [[[NSMutableDictionary alloc] init] autorelease];
[credentials setObject:[usernameInput stringValue]
forKey:(NSString *)kCFHTTPAuthenticationUsername];
[credentials setObject:[passwordInput stringValue]
forKey:(NSString *)kCFHTTPAuthenticationPassword];
[request saveCredentialsToKeychain:credentials];
// save the keychain values in static variables for easy access
KeychainUsername = [usernameInput stringValue];
KeychainPassword = [passwordInput stringValue];
[ScrapePrefsController setLoggedIn:YES];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:ScrapeShowGrowlNotificationsKey] == YES) {
[GrowlApplicationBridge notifyWithTitle:@"Logged in to Scrape"
description:nil
notificationName:@"Login"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
}
} else {
NSLog(@"Error logging in");
[successLabel setHidden:YES];
[errorLabel setHidden:NO];
[ScrapePrefsController setLoggedIn:NO];
}
}
//--------------------------------------------------------------
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(@"%@", [error localizedDescription]);
}
//--------------------------------------------------------------
+ (void)setLoggedIn:(BOOL)val {
loggedIn = val;
}
//--------------------------------------------------------------
+ (BOOL)isLoggedIn {
return loggedIn;
}
@end