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.

AppDelegate.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. [self reloadListWithOptions];
  25. }
  26. -(void)defaultPreferences {
  27. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  28. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:@"username"];
  29. [appDefaults setValue:@"" forKey:@"modhash"];
  30. [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"subscriptions"];
  31. [appDefaults setValue:[NSNumber numberWithInt:10] forKey:@"length"];
  32. [store registerDefaults:appDefaults];
  33. }
  34. -(void)savePreferences {
  35. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  36. [store setObject:currentState.username forKey:@"username"];
  37. [store setObject:currentState.modhash forKey:@"modhash"];
  38. [store setBool:currentState.useSubscriptions forKey:@"subscriptions"];
  39. [store setObject:currentState.subreddits forKey:@"subreddits"];
  40. [store setInteger:currentState.length forKey:@"length"];
  41. [store synchronize];
  42. }
  43. -(void)loadPreferences {
  44. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  45. [store synchronize];
  46. [currentState setUsername:[store stringForKey:@"username"]];
  47. [currentState setModhash:[store stringForKey:@"modhash"]];
  48. [currentState setUseSubscriptions:[store boolForKey:@"subscriptions"]];
  49. [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
  50. [currentState setLength:[store integerForKey:@"length"]];
  51. }
  52. -(void)reloadListWithOptions {
  53. if ([currentState.modhash isEqualToString:@""]) {
  54. [firstMenuItem setTitle:@"Not logged in!"];
  55. return;
  56. }
  57. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
  58. NSString *tmp = @"";
  59. if (![api isAuthenticatedNewModhash:&tmp]) {
  60. [firstMenuItem setTitle:@"Not logged in!"];
  61. return;
  62. }
  63. // Reddit gives out new modhashes all the time??
  64. //if (![tmp isEqualToString:@""]) {
  65. // NSLog(@"Modhash has changed!\n");
  66. // currentState.modhash = tmp; // We got a new modhash from reddit
  67. // [self savePreferences];
  68. //}
  69. if (currentState.useSubscriptions) {
  70. NSArray *items = [api readFrontpageLength:currentState.length];
  71. if (items == nil) {
  72. [firstMenuItem setTitle:@"Error reading Frontpage!"];
  73. return;
  74. }
  75. [self putItemArrayInMenu:items];
  76. } else {
  77. NSArray *items = [api readSubreddits:currentState.subreddits Length:currentState.length];
  78. if (items == nil) {
  79. [firstMenuItem setTitle:@"Error reading Subreddits!"];
  80. return;
  81. }
  82. [self putItemArrayInMenu:items];
  83. }
  84. }
  85. -(void)putItemArrayInMenu:(NSArray *)array {
  86. // TODO populate menu
  87. }
  88. -(IBAction)showPreferences:(id)sender {
  89. [NSApp activateIgnoringOtherApps:YES];
  90. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  91. [prefWindow setParent:self];
  92. [prefWindow setState:currentState];
  93. [prefWindow showWindow:self];
  94. }
  95. -(IBAction)showAbout:(id)sender {
  96. [NSApp activateIgnoringOtherApps:YES];
  97. [application orderFrontStandardAboutPanel:self];
  98. }
  99. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
  100. currentState.username = name;
  101. currentState.modhash = modhash;
  102. currentState.useSubscriptions = subscriptions;
  103. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  104. currentState.length = length;
  105. [self savePreferences];
  106. [self reloadListWithOptions];
  107. }
  108. @end