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

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