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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, lengthField, lengthStepper, length, progress, showSubreddit, titleField, titleStepper, titleLength, refreshField, refreshStepper, refreshInterval, filterSelection;
  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. titleLength = state.titleLength;
  61. [titleStepper setIntegerValue:titleLength];
  62. [titleField setIntegerValue:titleLength];
  63. refreshInterval = state.refreshInterval;
  64. [refreshStepper setIntegerValue:refreshInterval];
  65. [refreshField setIntegerValue:refreshInterval];
  66. [progress setUsesThreadedAnimation:YES];
  67. [showSubreddit setState:[NSNumber numberWithBool:state.showSubreddit].integerValue];
  68. if ([state.filter isEqualToString:@"hot"]) {
  69. [filterSelection selectItemAtIndex:0];
  70. } else {
  71. [filterSelection selectItemAtIndex:1];
  72. }
  73. }
  74. -(IBAction)buttonSave:(id)sender {
  75. if ([username.stringValue isEqualToString:@""]) {
  76. NSAlert *alert = [[NSAlert alloc] init];
  77. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  78. [alert setMessageText:NSLocalizedString(@"Authentication Error", @"Pref Error")];
  79. [alert setInformativeText:NSLocalizedString(@"Please enter a username!", @"Pref Error")];
  80. [alert setAlertStyle:NSCriticalAlertStyle];
  81. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  82. return;
  83. }
  84. if ([state.modhash isEqualToString:@""] && [password.stringValue isEqualToString:@""]) {
  85. NSAlert *alert = [[NSAlert alloc] init];
  86. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  87. [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
  88. [alert setInformativeText:NSLocalizedString(@"Please enter a password!", @"Pref Error")];
  89. [alert setAlertStyle:NSCriticalAlertStyle];
  90. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  91. return;
  92. }
  93. NSString *modhash = state.modhash;
  94. if (![password.stringValue isEqualToString:modhashSetLiteral]) {
  95. [progress startAnimation:self];
  96. Reddit *api = [[Reddit alloc] initWithUsername:username.stringValue Password:password.stringValue];
  97. modhash = [api queryModhash];
  98. [progress stopAnimation:self];
  99. if ((modhash == nil) || ([modhash isEqualToString:@""])) {
  100. NSAlert *alert = [[NSAlert alloc] init];
  101. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  102. [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
  103. [alert setInformativeText:NSLocalizedString(@"Wrong Username or Password!", @"Pref API Error")];
  104. [alert setAlertStyle:NSCriticalAlertStyle];
  105. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  106. return;
  107. }
  108. }
  109. Boolean subs;
  110. if (subscriptions.state != 0) {
  111. subs = TRUE;
  112. } else {
  113. subs = FALSE;
  114. if (![self isValidList:subreddits.textStorage.string]) {
  115. NSAlert *alert = [[NSAlert alloc] init];
  116. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  117. [alert setMessageText:NSLocalizedString(@"Preferences Error", @"Pref Error")];
  118. [alert setInformativeText:NSLocalizedString(@"Subreddit List Invalid!", @"Pref Error")];
  119. [alert setAlertStyle:NSCriticalAlertStyle];
  120. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  121. return;
  122. }
  123. }
  124. Boolean print;
  125. if (showSubreddit.state != 0)
  126. print = TRUE;
  127. else
  128. print = FALSE;
  129. state.username = username.stringValue;
  130. state.modhash = modhash;
  131. state.useSubscriptions = subs;
  132. state.subreddits = [subreddits.textStorage.string componentsSeparatedByString: @"\n"];
  133. state.length = [lengthField integerValue];
  134. state.showSubreddit = print;
  135. state.titleLength = [titleField integerValue];
  136. state.refreshInterval = [refreshField integerValue];
  137. state.filter = [filterSelection titleOfSelectedItem];
  138. [(AppDelegate *)parent prefsDidSave];
  139. [win performClose:self];
  140. }
  141. -(IBAction)toggleSubs:(id)sender {
  142. if (subscriptions.state != 0) {
  143. [subreddits setEditable:FALSE];
  144. [subscriptions setTitle:NSLocalizedString(@"Use Subscriptions", @"Pref Checkbox State 1")];
  145. } else {
  146. [subreddits setEditable:TRUE];
  147. [subscriptions setTitle:NSLocalizedString(@"Use Subreddits list", @"Pref Checkbox State 0")];
  148. }
  149. }
  150. -(IBAction)lengthDidChange:(id)sender {
  151. length = [sender integerValue];
  152. [lengthStepper setIntegerValue:length];
  153. [lengthField setIntegerValue:length];
  154. }
  155. -(IBAction)titleDidChange:(id)sender {
  156. titleLength = [sender integerValue];
  157. [titleStepper setIntegerValue:titleLength];
  158. [titleField setIntegerValue:titleLength];
  159. }
  160. -(IBAction)refreshDidChange:(id)sender {
  161. refreshInterval = [sender integerValue];
  162. [refreshStepper setIntegerValue:refreshInterval];
  163. [refreshField setIntegerValue:refreshInterval];
  164. }
  165. @end