aka RedditBar, Mac OS X menu bar reddit client
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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:NSLocalizedString(@"RedditBar", @"Main Menuitem Tooltip")];
  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:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
  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:NSLocalizedString(@"Login Error!", @"Statusitem when API is not authenticated")];
  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:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
  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:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
  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:NSLocalizedString(@"Link...", nil)]) {
  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:NSLocalizedString(@"Comments...", nil)]) {
  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. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  130. return;
  131. }
  132. }
  133. }
  134. }
  135. -(void)clearMenuItems {
  136. if (menuItems != nil) {
  137. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  138. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  139. }
  140. menuItems = nil;
  141. }
  142. }
  143. -(void)putItemArrayInMenu:(NSArray *)array {
  144. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  145. for (NSUInteger i = 0; i < [array count]; i++) {
  146. RedditItem *reddit = [array objectAtIndex:i];
  147. NSMenuItem *item = [[NSMenuItem alloc] init];
  148. [item setTitle:reddit.name];
  149. if (reddit.isSelf) {
  150. [item setAction:@selector(linkToOpen:)];
  151. [item setKeyEquivalent:@""];
  152. } else {
  153. NSMenu *submenu = [[NSMenu alloc] init];
  154. [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
  155. [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
  156. [item setSubmenu:submenu];
  157. }
  158. [items addObject:item];
  159. [statusMenu insertItem:item atIndex:i];
  160. }
  161. menuItems = items;
  162. }
  163. -(IBAction)showPreferences:(id)sender {
  164. [NSApp activateIgnoringOtherApps:YES];
  165. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  166. [prefWindow setParent:self];
  167. [prefWindow setState:currentState];
  168. [prefWindow showWindow:self];
  169. }
  170. -(IBAction)showAbout:(id)sender {
  171. [NSApp activateIgnoringOtherApps:YES];
  172. [application orderFrontStandardAboutPanel:self];
  173. }
  174. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
  175. currentState.username = name;
  176. currentState.modhash = modhash;
  177. currentState.useSubscriptions = subscriptions;
  178. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  179. currentState.length = length;
  180. [self savePreferences];
  181. [self reloadListWithOptions];
  182. }
  183. @end