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

AppDelegate.m 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // AppDelegate.m
  3. // RedditBar
  4. //
  5. // Created by Thomas Buck on 30.11.13.
  6. // Copyright (c) 2013 xythobuz. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. @implementation AppDelegate
  10. @synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow, currentState, application, api, firstMenuItem;
  11. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  12. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  13. NSBundle *bundle = [NSBundle mainBundle];
  14. statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
  15. statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];
  16. [statusItem setImage:statusImage];
  17. [statusItem setAlternateImage:statusHighlightImage];
  18. [statusItem setMenu:statusMenu];
  19. [statusItem setToolTip:@"Reddit Bar"];
  20. [statusItem setHighlightMode:YES];
  21. currentState = [[StateModel alloc] init];
  22. [self defaultPreferences];
  23. [self loadPreferences];
  24. if ([currentState.modhash isEqualToString:@""]) {
  25. [firstMenuItem setTitle:@"Not logged in!"];
  26. } else {
  27. [self reloadListWithOptions];
  28. }
  29. }
  30. -(void)defaultPreferences {
  31. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  32. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:@"username"];
  33. [appDefaults setValue:@"" forKey:@"modhash"];
  34. [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"subscriptions"];
  35. [appDefaults setValue:[NSNumber numberWithInt:10] forKey:@"length"];
  36. [store registerDefaults:appDefaults];
  37. }
  38. -(void)savePreferences {
  39. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  40. [store setObject:currentState.username forKey:@"username"];
  41. [store setObject:currentState.modhash forKey:@"modhash"];
  42. [store setBool:currentState.useSubsciptions forKey:@"subscriptions"];
  43. [store setObject:currentState.subreddits forKey:@"subreddits"];
  44. [store setInteger:currentState.length forKey:@"length"];
  45. [store synchronize];
  46. }
  47. -(void)loadPreferences {
  48. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  49. [store synchronize];
  50. [currentState setUsername:[store stringForKey:@"username"]];
  51. [currentState setModhash:[store stringForKey:@"modhash"]];
  52. [currentState setUseSubsciptions:[store boolForKey:@"subscriptions"]];
  53. [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
  54. [currentState setLength:[store integerForKey:@"length"]];
  55. }
  56. -(void)reloadListWithOptions {
  57. if ([currentState.modhash isEqualToString:@""]) {
  58. [firstMenuItem setTitle:@"Not logged in!"];
  59. return;
  60. }
  61. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
  62. }
  63. -(IBAction)showPreferences:(id)sender {
  64. [NSApp activateIgnoringOtherApps:YES];
  65. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  66. [prefWindow setParent:self];
  67. [prefWindow setState:currentState];
  68. [prefWindow showWindow:self];
  69. }
  70. -(IBAction)showAbout:(id)sender {
  71. [NSApp activateIgnoringOtherApps:YES];
  72. [application orderFrontStandardAboutPanel:self];
  73. }
  74. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
  75. currentState.username = name;
  76. currentState.modhash = modhash;
  77. currentState.useSubsciptions = subscriptions;
  78. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  79. currentState.length = length;
  80. [self savePreferences];
  81. [self reloadListWithOptions];
  82. }
  83. @end