123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- * PrefController.m
- *
- * Copyright (c) 2013, Thomas Buck <xythobuz@xythobuz.de>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #import "PrefController.h"
- #import "AppDelegate.h"
-
- @implementation PrefController
-
- NSString *modhashSetLiteral = @"__MODHASH__IS__SET__";
- NSString *subredditCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_\n";
-
- @synthesize username, password, subscriptions, subreddits, win, parent, state, lengthField, lengthStepper, length, progress, showSubreddit, titleField, titleStepper, titleLength, refreshField, refreshStepper, refreshInterval, filterSelection;
-
- -(Boolean)isValidList:(NSString *)input {
- NSCharacterSet *invalidChars = [[NSCharacterSet characterSetWithCharactersInString:subredditCharacters] invertedSet];
- if ([input rangeOfCharacterFromSet:invalidChars].location != NSNotFound) {
- return FALSE;
- } else {
- return TRUE;
- }
- }
-
- -(void)showWindow:(id)sender {
- [super showWindow:sender];
- [username setStringValue:state.username];
- if (![state.modhash isEqualToString:@""]) {
- [password setStringValue:modhashSetLiteral];
- }
- [subscriptions setState:[NSNumber numberWithBool:state.useSubscriptions].integerValue];
- [self toggleSubs:nil]; // Maybe the subreddits field needs to be editable
- NSMutableString *reddits = [[NSMutableString alloc] init];
- for(int i = 0; i < [state.subreddits count]; i++) {
- if (![[state.subreddits objectAtIndex:i] isEqualToString:@""])
- [reddits appendFormat:@"%@\n", [state.subreddits objectAtIndex:i]];
- }
- [subreddits setString:reddits];
- length = state.length;
- [lengthStepper setIntegerValue:length];
- [lengthField setIntegerValue:length];
- titleLength = state.titleLength;
- [titleStepper setIntegerValue:titleLength];
- [titleField setIntegerValue:titleLength];
- refreshInterval = state.refreshInterval;
- [refreshStepper setIntegerValue:refreshInterval];
- [refreshField setIntegerValue:refreshInterval];
- [progress setUsesThreadedAnimation:YES];
- [showSubreddit setState:[NSNumber numberWithBool:state.showSubreddit].integerValue];
- if ([state.filter isEqualToString:@"hot"]) {
- [filterSelection selectItemAtIndex:0];
- } else {
- [filterSelection selectItemAtIndex:1];
- }
- }
-
- -(IBAction)buttonSave:(id)sender {
- if ([username.stringValue isEqualToString:@""]) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
- [alert setMessageText:NSLocalizedString(@"Authentication Error", @"Pref Error")];
- [alert setInformativeText:NSLocalizedString(@"Please enter a username!", @"Pref Error")];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
- return;
- }
-
- if ([state.modhash isEqualToString:@""] && [password.stringValue isEqualToString:@""]) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
- [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
- [alert setInformativeText:NSLocalizedString(@"Please enter a password!", @"Pref Error")];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
- return;
- }
-
- NSString *modhash = state.modhash;
- if (![password.stringValue isEqualToString:modhashSetLiteral]) {
- [progress startAnimation:self];
- Reddit *api = [[Reddit alloc] initWithUsername:username.stringValue Password:password.stringValue];
- modhash = [api queryModhash];
- [progress stopAnimation:self];
- if ((modhash == nil) || ([modhash isEqualToString:@""])) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
- [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
- [alert setInformativeText:NSLocalizedString(@"Wrong Username or Password!", @"Pref API Error")];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
- return;
- }
- }
-
- Boolean subs;
- if (subscriptions.state != 0) {
- subs = TRUE;
- } else {
- subs = FALSE;
- if (![self isValidList:subreddits.textStorage.string]) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
- [alert setMessageText:NSLocalizedString(@"Preferences Error", @"Pref Error")];
- [alert setInformativeText:NSLocalizedString(@"Subreddit List Invalid!", @"Pref Error")];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
- return;
- }
- }
- Boolean print;
- if (showSubreddit.state != 0)
- print = TRUE;
- else
- print = FALSE;
-
- state.username = username.stringValue;
- state.modhash = modhash;
- state.useSubscriptions = subs;
- state.subreddits = [subreddits.textStorage.string componentsSeparatedByString: @"\n"];
- state.length = [lengthField integerValue];
- state.showSubreddit = print;
- state.titleLength = [titleField integerValue];
- state.refreshInterval = [refreshField integerValue];
- state.filter = [filterSelection titleOfSelectedItem];
- [(AppDelegate *)parent prefsDidSave];
- [win performClose:self];
- }
-
- -(IBAction)toggleSubs:(id)sender {
- if (subscriptions.state != 0) {
- [subreddits setEditable:FALSE];
- [subscriptions setTitle:NSLocalizedString(@"Use Subscriptions", @"Pref Checkbox State 1")];
- } else {
- [subreddits setEditable:TRUE];
- [subscriptions setTitle:NSLocalizedString(@"Use Subreddits list", @"Pref Checkbox State 0")];
- }
- }
-
- -(IBAction)lengthDidChange:(id)sender {
- length = [sender integerValue];
- [lengthStepper setIntegerValue:length];
- [lengthField setIntegerValue:length];
- }
-
- -(IBAction)titleDidChange:(id)sender {
- titleLength = [sender integerValue];
- [titleStepper setIntegerValue:titleLength];
- [titleField setIntegerValue:titleLength];
- }
-
- -(IBAction)refreshDidChange:(id)sender {
- refreshInterval = [sender integerValue];
- [refreshStepper setIntegerValue:refreshInterval];
- [refreshField setIntegerValue:refreshInterval];
- }
-
- @end
|