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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. NSString *modhashSetLiteral = @"__MODHASH__IS__SET__";
  12. @synthesize username, password, subscriptions, subreddits, win, parent, state, lengthFormat, lengthField, lengthStepper, length;
  13. -(Boolean)isValidList:(NSString *)input {
  14. NSCharacterSet *invalidChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_\n"] invertedSet];
  15. if ([input rangeOfCharacterFromSet:invalidChars].location != NSNotFound) {
  16. return FALSE;
  17. } else {
  18. return TRUE;
  19. }
  20. }
  21. -(void)showWindow:(id)sender {
  22. [super showWindow:sender];
  23. [username setStringValue:state.username];
  24. if (![state.modhash isEqualToString:@""]) {
  25. [password setStringValue:modhashSetLiteral];
  26. }
  27. [subscriptions setState:[NSNumber numberWithBool:state.useSubsciptions].integerValue];
  28. [self toggleSubs:nil]; // Maybe the subreddits field needs to be editable
  29. NSMutableString *reddits = [[NSMutableString alloc] init];
  30. for(int i = 0; i < [state.subreddits count]; i++) {
  31. if (![[state.subreddits objectAtIndex:i] isEqualToString:@""])
  32. [reddits appendFormat:@"%@\n", [state.subreddits objectAtIndex:i]];
  33. }
  34. [subreddits setString:reddits];
  35. length = state.length;
  36. [lengthStepper setIntegerValue:length];
  37. [lengthField setIntegerValue:length];
  38. }
  39. -(IBAction)buttonSave:(id)sender {
  40. if ([username.stringValue isEqualToString:@""]) {
  41. NSAlert *alert = [[NSAlert alloc] init];
  42. [alert addButtonWithTitle:@"OK"];
  43. [alert setMessageText:@"Authentication Error"];
  44. [alert setInformativeText:@"Please enter a username!"];
  45. [alert setAlertStyle:NSCriticalAlertStyle];
  46. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  47. return;
  48. }
  49. if ([state.modhash isEqualToString:@""] && [password.stringValue isEqualToString:@""]) {
  50. NSAlert *alert = [[NSAlert alloc] init];
  51. [alert addButtonWithTitle:@"OK"];
  52. [alert setMessageText:@"Authentication Error"];
  53. [alert setInformativeText:@"Please enter a password!"];
  54. [alert setAlertStyle:NSCriticalAlertStyle];
  55. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  56. return;
  57. }
  58. NSString *modhash = state.modhash;
  59. if (![password.stringValue isEqualToString:modhashSetLiteral]) {
  60. // TODO reauthenticate & get modhash from reddit
  61. }
  62. Boolean subs;
  63. if (subscriptions.state != 0) {
  64. subs = TRUE;
  65. } else {
  66. subs = FALSE;
  67. if (![self isValidList:subreddits.textStorage.string]) {
  68. NSAlert *alert = [[NSAlert alloc] init];
  69. [alert addButtonWithTitle:@"OK"];
  70. [alert setMessageText:@"Preferences Error"];
  71. [alert setInformativeText:@"Subreddit List Invalid!"];
  72. [alert setAlertStyle:NSCriticalAlertStyle];
  73. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  74. return;
  75. }
  76. }
  77. AppDelegate *app = (AppDelegate *)parent;
  78. [app prefReturnName:username.stringValue Modhash:modhash subscriptions:subs subreddits:subreddits.textStorage.string length:length];
  79. [win performClose:self];
  80. }
  81. -(IBAction)toggleSubs:(id)sender {
  82. if (subscriptions.state != 0) {
  83. [subreddits setEditable:FALSE];
  84. } else {
  85. [subreddits setEditable:TRUE];
  86. }
  87. }
  88. -(IBAction)lengthDidChange:(id)sender {
  89. length = [sender integerValue];
  90. [lengthStepper setIntegerValue:length];
  91. [lengthField setIntegerValue:length];
  92. }
  93. @end