aka RedditBar, Mac OS X menu bar reddit client
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PrefController.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * PrefController.m
  3. *
  4. * Copyright (c) 2013, Thomas Buck <xythobuz@xythobuz.de>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * - Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #import "PrefController.h"
  30. #import "AppDelegate.h"
  31. @implementation PrefController
  32. NSString *modhashSetLiteral = @"__MODHASH__IS__SET__";
  33. NSString *subredditCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_\n";
  34. @synthesize username, password, subscriptions, subreddits, win, parent, state, lengthFormat, lengthField, lengthStepper, length, progress, showSubreddit;
  35. -(Boolean)isValidList:(NSString *)input {
  36. NSCharacterSet *invalidChars = [[NSCharacterSet characterSetWithCharactersInString:subredditCharacters] invertedSet];
  37. if ([input rangeOfCharacterFromSet:invalidChars].location != NSNotFound) {
  38. return FALSE;
  39. } else {
  40. return TRUE;
  41. }
  42. }
  43. -(void)showWindow:(id)sender {
  44. [super showWindow:sender];
  45. [username setStringValue:state.username];
  46. if (![state.modhash isEqualToString:@""]) {
  47. [password setStringValue:modhashSetLiteral];
  48. }
  49. [subscriptions setState:[NSNumber numberWithBool:state.useSubscriptions].integerValue];
  50. [self toggleSubs:nil]; // Maybe the subreddits field needs to be editable
  51. NSMutableString *reddits = [[NSMutableString alloc] init];
  52. for(int i = 0; i < [state.subreddits count]; i++) {
  53. if (![[state.subreddits objectAtIndex:i] isEqualToString:@""])
  54. [reddits appendFormat:@"%@\n", [state.subreddits objectAtIndex:i]];
  55. }
  56. [subreddits setString:reddits];
  57. length = state.length;
  58. [lengthStepper setIntegerValue:length];
  59. [lengthField setIntegerValue:length];
  60. [progress setUsesThreadedAnimation:YES];
  61. [showSubreddit setState:[NSNumber numberWithBool:state.showSubreddit].integerValue];
  62. }
  63. -(IBAction)buttonSave:(id)sender {
  64. if ([username.stringValue isEqualToString:@""]) {
  65. NSAlert *alert = [[NSAlert alloc] init];
  66. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  67. [alert setMessageText:NSLocalizedString(@"Authentication Error", @"Pref Error")];
  68. [alert setInformativeText:NSLocalizedString(@"Please enter a username!", @"Pref Error")];
  69. [alert setAlertStyle:NSCriticalAlertStyle];
  70. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  71. return;
  72. }
  73. if ([state.modhash isEqualToString:@""] && [password.stringValue isEqualToString:@""]) {
  74. NSAlert *alert = [[NSAlert alloc] init];
  75. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  76. [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
  77. [alert setInformativeText:NSLocalizedString(@"Please enter a password!", @"Pref Error")];
  78. [alert setAlertStyle:NSCriticalAlertStyle];
  79. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  80. return;
  81. }
  82. NSString *modhash = state.modhash;
  83. if (![password.stringValue isEqualToString:modhashSetLiteral]) {
  84. [progress startAnimation:self];
  85. Reddit *api = [[Reddit alloc] initWithUsername:username.stringValue Password:password.stringValue];
  86. modhash = [api queryModhash];
  87. [progress stopAnimation:self];
  88. if ((modhash == nil) || ([modhash isEqualToString:@""])) {
  89. NSAlert *alert = [[NSAlert alloc] init];
  90. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  91. [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
  92. [alert setInformativeText:NSLocalizedString(@"Wrong Username or Password!", @"Pref API Error")];
  93. [alert setAlertStyle:NSCriticalAlertStyle];
  94. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  95. return;
  96. }
  97. }
  98. Boolean subs;
  99. if (subscriptions.state != 0) {
  100. subs = TRUE;
  101. } else {
  102. subs = FALSE;
  103. if (![self isValidList:subreddits.textStorage.string]) {
  104. NSAlert *alert = [[NSAlert alloc] init];
  105. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  106. [alert setMessageText:NSLocalizedString(@"Preferences Error", @"Pref Error")];
  107. [alert setInformativeText:NSLocalizedString(@"Subreddit List Invalid!", @"Pref Error")];
  108. [alert setAlertStyle:NSCriticalAlertStyle];
  109. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  110. return;
  111. }
  112. }
  113. Boolean print;
  114. if (showSubreddit.state != 0)
  115. print = TRUE;
  116. else
  117. print = FALSE;
  118. AppDelegate *app = (AppDelegate *)parent;
  119. [app prefReturnName:username.stringValue Modhash:modhash subscriptions:subs subreddits:subreddits.textStorage.string length:length printSubs:print];
  120. [win performClose:self];
  121. }
  122. -(IBAction)toggleSubs:(id)sender {
  123. if (subscriptions.state != 0) {
  124. [subreddits setEditable:FALSE];
  125. [subscriptions setTitle:NSLocalizedString(@"Use Subscriptions", @"Pref Checkbox State 1")];
  126. } else {
  127. [subreddits setEditable:TRUE];
  128. [subscriptions setTitle:NSLocalizedString(@"Use Subreddits list", @"Pref Checkbox State 0")];
  129. }
  130. }
  131. -(IBAction)lengthDidChange:(id)sender {
  132. length = [sender integerValue];
  133. [lengthStepper setIntegerValue:length];
  134. [lengthField setIntegerValue:length];
  135. }
  136. @end