aka RedditBar, Mac OS X menu bar reddit client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrefController.m 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // PrefController.m
  3. // RedditBar
  4. //
  5. // Created by Thomas Buck on 30.11.13.
  6. // Copyright (c) 2013 xythobuz. All rights reserved.
  7. //
  8. #import "PrefController.h"
  9. #import "AppDelegate.h"
  10. @implementation PrefController
  11. @synthesize username, password, subscriptions, subreddits, win, parent, state;
  12. -(Boolean)isValidList:(NSString *)input {
  13. NSCharacterSet *invalidChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_\n"] invertedSet];
  14. if ([input rangeOfCharacterFromSet:invalidChars].location != NSNotFound) {
  15. return FALSE;
  16. } else {
  17. return TRUE;
  18. }
  19. }
  20. -(void)showWindow:(id)sender {
  21. [super showWindow:sender];
  22. [username setStringValue:state.username];
  23. // TODO what to do with modhash and password field??
  24. [subscriptions setState:[NSNumber numberWithBool:state.useSubsciptions].integerValue];
  25. [self toggleSubs:nil]; // Maybe the subreddits field needs to be editable
  26. NSMutableString *reddits = [[NSMutableString alloc] init];
  27. for(int i = 0; i < [state.subreddits count]; i++) {
  28. [reddits appendFormat:@"%@\n", [state.subreddits objectAtIndex:i]];
  29. }
  30. [subreddits setString:reddits];
  31. }
  32. -(IBAction)buttonSave:(id)sender {
  33. Boolean subs;
  34. if (subscriptions.state != 0) {
  35. subs = TRUE;
  36. } else {
  37. subs = FALSE;
  38. if (![self isValidList:subreddits.textStorage.string]) {
  39. NSAlert *alert = [[NSAlert alloc] init];
  40. [alert addButtonWithTitle:@"OK"];
  41. [alert setMessageText:@"Preferences Error"];
  42. [alert setInformativeText:@"Subreddit List Invalid!"];
  43. [alert setAlertStyle:NSCriticalAlertStyle];
  44. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  45. return;
  46. }
  47. }
  48. // TODO if username / password changed, get modhash! Else, use the one we got from init
  49. NSString *modhash;
  50. AppDelegate *app = (AppDelegate *)parent;
  51. [app prefReturnName:username.stringValue Modhash:modhash subscriptions:subs subreddits:subreddits.textStorage.string];
  52. [win performClose:self];
  53. }
  54. -(IBAction)toggleSubs:(id)sender {
  55. if (subscriptions.state != 0) {
  56. [subreddits setEditable:FALSE];
  57. } else {
  58. [subreddits setEditable:TRUE];
  59. }
  60. }
  61. @end