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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, menuItems, redditItems;
  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. redditItems = 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. redditItems = items;
  83. }
  84. [self putItemArrayInMenu:redditItems];
  85. }
  86. -(IBAction)linkToOpen:(id)sender {
  87. NSString *title = [(NSMenuItem *)sender title];
  88. if ([title isEqualToString:@"Link..."]) {
  89. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  90. NSMenuItem *item = [menuItems objectAtIndex:i];
  91. NSMenu *submenu = item.submenu;
  92. if (submenu != nil) {
  93. if (sender == [submenu itemAtIndex:0]) {
  94. RedditItem *rItem = [redditItems objectAtIndex:i];
  95. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  96. return;
  97. }
  98. }
  99. }
  100. } else if ([title isEqualToString:@"Comments..."]) {
  101. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  102. NSMenuItem *item = [menuItems objectAtIndex:i];
  103. NSMenu *submenu = item.submenu;
  104. if (submenu != nil) {
  105. if (sender == [submenu itemAtIndex:1]) {
  106. RedditItem *rItem = [redditItems objectAtIndex:i];
  107. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem comments]]];
  108. return;
  109. }
  110. }
  111. }
  112. } else {
  113. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  114. NSMenuItem *item = [menuItems objectAtIndex:i];
  115. if (sender == item) {
  116. RedditItem *rItem = [redditItems objectAtIndex:i];
  117. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  118. return;
  119. }
  120. }
  121. }
  122. }
  123. -(void)putItemArrayInMenu:(NSArray *)array {
  124. [firstMenuItem setHidden:YES];
  125. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  126. for (NSUInteger i = 0; i < [array count]; i++) {
  127. RedditItem *reddit = [array objectAtIndex:i];
  128. NSMenuItem *item = [[NSMenuItem alloc] init];
  129. [item setTitle:reddit.name];
  130. if (reddit.isSelf) {
  131. [item setAction:@selector(linkToOpen:)];
  132. [item setKeyEquivalent:@""];
  133. } else {
  134. NSMenu *submenu = [[NSMenu alloc] init];
  135. [submenu addItemWithTitle:@"Link..." action:@selector(linkToOpen:) keyEquivalent:@""];
  136. [submenu addItemWithTitle:@"Comments..." action:@selector(linkToOpen:) keyEquivalent:@""];
  137. [item setSubmenu:submenu];
  138. }
  139. [items addObject:item];
  140. [statusMenu insertItem:item atIndex:i];
  141. }
  142. menuItems = items;
  143. }
  144. -(IBAction)showPreferences:(id)sender {
  145. [NSApp activateIgnoringOtherApps:YES];
  146. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  147. [prefWindow setParent:self];
  148. [prefWindow setState:currentState];
  149. [prefWindow showWindow:self];
  150. }
  151. -(IBAction)showAbout:(id)sender {
  152. [NSApp activateIgnoringOtherApps:YES];
  153. [application orderFrontStandardAboutPanel:self];
  154. }
  155. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
  156. currentState.username = name;
  157. currentState.modhash = modhash;
  158. currentState.useSubscriptions = subscriptions;
  159. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  160. currentState.length = length;
  161. [self savePreferences];
  162. [self reloadListWithOptions];
  163. }
  164. @end