aka RedditBar, Mac OS X menu bar reddit client
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PrefController.m 4.9KB

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