aka RedditBar, Mac OS X menu bar reddit client
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PrefController.m 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, removeVisited, reloadAfterVisit, launchOnLogin;
  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. if (state.removeVisited) {
  74. [removeVisited setState:1];
  75. [reloadAfterVisit setEnabled:TRUE];
  76. } else {
  77. [removeVisited setState:0];
  78. [reloadAfterVisit setEnabled:FALSE];
  79. }
  80. if (state.reloadAfterVisit) {
  81. [reloadAfterVisit setState:1];
  82. } else {
  83. [reloadAfterVisit setState:0];
  84. }
  85. [launchOnLogin setState:[NSNumber numberWithBool:state.startOnLogin].integerValue];
  86. }
  87. -(IBAction)buttonSave:(id)sender {
  88. if ([username.stringValue isEqualToString:@""]) {
  89. NSAlert *alert = [[NSAlert alloc] init];
  90. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  91. [alert setMessageText:NSLocalizedString(@"Authentication Error", @"Pref Error")];
  92. [alert setInformativeText:NSLocalizedString(@"Please enter a username!", @"Pref Error")];
  93. [alert setAlertStyle:NSCriticalAlertStyle];
  94. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  95. return;
  96. }
  97. if ([state.modhash isEqualToString:@""] && [password.stringValue isEqualToString:@""]) {
  98. NSAlert *alert = [[NSAlert alloc] init];
  99. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  100. [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
  101. [alert setInformativeText:NSLocalizedString(@"Please enter a password!", @"Pref Error")];
  102. [alert setAlertStyle:NSCriticalAlertStyle];
  103. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  104. return;
  105. }
  106. NSString *modhash = state.modhash;
  107. if (![password.stringValue isEqualToString:modhashSetLiteral]) {
  108. [progress startAnimation:self];
  109. Reddit *api = [[Reddit alloc] initWithUsername:username.stringValue Password:password.stringValue];
  110. modhash = [api queryModhash];
  111. [progress stopAnimation:self];
  112. if ((modhash == nil) || ([modhash isEqualToString:@""])) {
  113. NSAlert *alert = [[NSAlert alloc] init];
  114. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  115. [alert setMessageText:NSLocalizedString(@"Authentication Error", nil)];
  116. [alert setInformativeText:NSLocalizedString(@"Wrong Username or Password!", @"Pref API Error")];
  117. [alert setAlertStyle:NSCriticalAlertStyle];
  118. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  119. return;
  120. }
  121. }
  122. Boolean subs;
  123. if (subscriptions.state != 0) {
  124. subs = TRUE;
  125. } else {
  126. subs = FALSE;
  127. if (![self isValidList:subreddits.textStorage.string]) {
  128. NSAlert *alert = [[NSAlert alloc] init];
  129. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  130. [alert setMessageText:NSLocalizedString(@"Preferences Error", @"Pref Error")];
  131. [alert setInformativeText:NSLocalizedString(@"Subreddit List Invalid!", @"Pref Error")];
  132. [alert setAlertStyle:NSCriticalAlertStyle];
  133. [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
  134. return;
  135. }
  136. }
  137. Boolean print;
  138. if (showSubreddit.state != 0)
  139. print = TRUE;
  140. else
  141. print = FALSE;
  142. Boolean remove;
  143. if (removeVisited.state != 0)
  144. remove = TRUE;
  145. else
  146. remove = FALSE;
  147. Boolean reload;
  148. if (reloadAfterVisit.state != 0)
  149. reload = TRUE;
  150. else
  151. reload = FALSE;
  152. Boolean start;
  153. if (launchOnLogin.state != 0)
  154. start = TRUE;
  155. else
  156. start = FALSE;
  157. NSArray *subredditsToUse = [subreddits.textStorage.string componentsSeparatedByString: @"\n"];
  158. Boolean changesRequireReload = FALSE;
  159. if (![[username stringValue] isEqualToString:state.username])
  160. changesRequireReload = TRUE;
  161. if (![modhash isEqualToString:state.modhash])
  162. changesRequireReload = TRUE;
  163. if (subs != state.useSubscriptions)
  164. changesRequireReload = TRUE;
  165. if (subs && (![state.subreddits isEqualToArray:subredditsToUse]))
  166. changesRequireReload = TRUE;
  167. if ([lengthField integerValue] != state.length)
  168. changesRequireReload = TRUE;
  169. if (print != state.showSubreddit)
  170. changesRequireReload = TRUE;
  171. if ([titleField integerValue] != state.titleLength)
  172. changesRequireReload = TRUE;
  173. if (![[filterSelection titleOfSelectedItem] isEqualToString:state.filter])
  174. changesRequireReload = TRUE;
  175. state.username = username.stringValue;
  176. state.modhash = modhash;
  177. state.useSubscriptions = subs;
  178. state.subreddits = subredditsToUse;
  179. state.length = [lengthField integerValue];
  180. state.showSubreddit = print;
  181. state.titleLength = [titleField integerValue];
  182. state.refreshInterval = [refreshField integerValue];
  183. state.filter = [filterSelection titleOfSelectedItem];
  184. state.removeVisited = remove;
  185. state.reloadAfterVisit = reload;
  186. state.startOnLogin = start;
  187. [(AppDelegate *)parent prefsDidSaveReload:changesRequireReload];
  188. [win performClose:self];
  189. }
  190. -(IBAction)toggleSubs:(id)sender {
  191. if (subscriptions.state != 0) {
  192. [subreddits setEditable:FALSE];
  193. [subscriptions setTitle:NSLocalizedString(@"Use Subscriptions", @"Pref Checkbox State 1")];
  194. } else {
  195. [subreddits setEditable:TRUE];
  196. [subscriptions setTitle:NSLocalizedString(@"Use Subreddits list", @"Pref Checkbox State 0")];
  197. }
  198. }
  199. -(IBAction)lengthDidChange:(id)sender {
  200. length = [sender integerValue];
  201. [lengthStepper setIntegerValue:length];
  202. [lengthField setIntegerValue:length];
  203. }
  204. -(IBAction)titleDidChange:(id)sender {
  205. titleLength = [sender integerValue];
  206. [titleStepper setIntegerValue:titleLength];
  207. [titleField setIntegerValue:titleLength];
  208. }
  209. -(IBAction)refreshDidChange:(id)sender {
  210. refreshInterval = [sender integerValue];
  211. [refreshStepper setIntegerValue:refreshInterval];
  212. [refreshField setIntegerValue:refreshInterval];
  213. }
  214. - (IBAction)removeVisitedToggled:(id)sender {
  215. if (removeVisited.state != 0) {
  216. [reloadAfterVisit setEnabled:TRUE];
  217. } else {
  218. [reloadAfterVisit setEnabled:FALSE];
  219. }
  220. }
  221. @end