aka RedditBar, Mac OS X menu bar reddit client
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AppDelegate.m 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. [self clearMenuItems];
  56. [firstMenuItem setHidden:NO];
  57. [self showPreferences:nil];
  58. return;
  59. }
  60. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
  61. NSString *tmp = @"";
  62. if (![api isAuthenticatedNewModhash:&tmp]) {
  63. [firstMenuItem setTitle:@"Login Error!"];
  64. [self clearMenuItems];
  65. [firstMenuItem setHidden:NO];
  66. return;
  67. }
  68. // Reddit gives out new modhashes all the time??
  69. //if (![tmp isEqualToString:@""]) {
  70. // NSLog(@"Modhash has changed!\n");
  71. // currentState.modhash = tmp; // We got a new modhash from reddit
  72. // [self savePreferences];
  73. //}
  74. if (currentState.useSubscriptions) {
  75. NSArray *items = [api readFrontpageLength:currentState.length];
  76. if (items == nil) {
  77. [firstMenuItem setTitle:@"Error reading Frontpage!"];
  78. [self clearMenuItems];
  79. [firstMenuItem setHidden:NO];
  80. return;
  81. }
  82. redditItems = items;
  83. } else {
  84. NSArray *items = [api readSubreddits:currentState.subreddits Length:currentState.length];
  85. if (items == nil) {
  86. [firstMenuItem setTitle:@"Error reading Subreddits!"];
  87. [self clearMenuItems];
  88. [firstMenuItem setHidden:NO];
  89. return;
  90. }
  91. redditItems = items;
  92. }
  93. [self clearMenuItems];
  94. [firstMenuItem setHidden:YES];
  95. [self putItemArrayInMenu:redditItems];
  96. }
  97. -(IBAction)linkToOpen:(id)sender {
  98. NSString *title = [(NSMenuItem *)sender title];
  99. if ([title isEqualToString:@"Link..."]) {
  100. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  101. NSMenuItem *item = [menuItems objectAtIndex:i];
  102. NSMenu *submenu = item.submenu;
  103. if (submenu != nil) {
  104. if (sender == [submenu itemAtIndex:0]) {
  105. RedditItem *rItem = [redditItems objectAtIndex:i];
  106. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  107. return;
  108. }
  109. }
  110. }
  111. } else if ([title isEqualToString:@"Comments..."]) {
  112. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  113. NSMenuItem *item = [menuItems objectAtIndex:i];
  114. NSMenu *submenu = item.submenu;
  115. if (submenu != nil) {
  116. if (sender == [submenu itemAtIndex:1]) {
  117. RedditItem *rItem = [redditItems objectAtIndex:i];
  118. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem comments]]];
  119. return;
  120. }
  121. }
  122. }
  123. } else {
  124. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  125. NSMenuItem *item = [menuItems objectAtIndex:i];
  126. if (sender == item) {
  127. RedditItem *rItem = [redditItems objectAtIndex:i];
  128. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  129. return;
  130. }
  131. }
  132. }
  133. }
  134. -(void)clearMenuItems {
  135. if (menuItems != nil) {
  136. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  137. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  138. }
  139. menuItems = nil;
  140. }
  141. }
  142. -(void)putItemArrayInMenu:(NSArray *)array {
  143. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  144. for (NSUInteger i = 0; i < [array count]; i++) {
  145. RedditItem *reddit = [array objectAtIndex:i];
  146. NSMenuItem *item = [[NSMenuItem alloc] init];
  147. [item setTitle:reddit.name];
  148. if (reddit.isSelf) {
  149. [item setAction:@selector(linkToOpen:)];
  150. [item setKeyEquivalent:@""];
  151. } else {
  152. NSMenu *submenu = [[NSMenu alloc] init];
  153. [submenu addItemWithTitle:@"Link..." action:@selector(linkToOpen:) keyEquivalent:@""];
  154. [submenu addItemWithTitle:@"Comments..." action:@selector(linkToOpen:) keyEquivalent:@""];
  155. [item setSubmenu:submenu];
  156. }
  157. [items addObject:item];
  158. [statusMenu insertItem:item atIndex:i];
  159. }
  160. menuItems = items;
  161. }
  162. -(IBAction)showPreferences:(id)sender {
  163. [NSApp activateIgnoringOtherApps:YES];
  164. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  165. [prefWindow setParent:self];
  166. [prefWindow setState:currentState];
  167. [prefWindow showWindow:self];
  168. }
  169. -(IBAction)showAbout:(id)sender {
  170. [NSApp activateIgnoringOtherApps:YES];
  171. [application orderFrontStandardAboutPanel:self];
  172. }
  173. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
  174. currentState.username = name;
  175. currentState.modhash = modhash;
  176. currentState.useSubscriptions = subscriptions;
  177. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  178. currentState.length = length;
  179. [self savePreferences];
  180. [self reloadListWithOptions];
  181. }
  182. @end