aka RedditBar, Mac OS X menu bar reddit client
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AppDelegate.m 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * AppDelegate.m
  3. *
  4. * Copyright (c) 2013, Thomas Buck <xythobuz@xythobuz.de>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * - Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #import "AppDelegate.h"
  30. @implementation AppDelegate
  31. @synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems, lastFullName;
  32. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  33. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  34. NSBundle *bundle = [NSBundle mainBundle];
  35. statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
  36. statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];
  37. [statusItem setImage:statusImage];
  38. [statusItem setAlternateImage:statusHighlightImage];
  39. [statusItem setMenu:statusMenu];
  40. [statusItem setToolTip:NSLocalizedString(@"RedditBar", @"Main Menuitem Tooltip")];
  41. [statusItem setHighlightMode:YES];
  42. currentState = [[StateModel alloc] init];
  43. [currentState registerDefaultPreferences];
  44. [currentState loadPreferences];
  45. lastFullName = nil;
  46. [self reloadListWithOptions];
  47. }
  48. -(void)reloadListNotAuthenticatedCallback {
  49. [firstMenuItem setTitle:NSLocalizedString(@"Login Error!", @"Statusitem when API is not authenticated")];
  50. [self clearMenuItems];
  51. [firstMenuItem setHidden:NO];
  52. }
  53. -(void)reloadListHasFrontpageCallback:(NSArray *)items {
  54. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
  55. }
  56. -(void)reloadListHasSubredditsCallback:(NSArray *)items {
  57. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
  58. }
  59. -(void)reloadListHasXCallback:(NSArray *)items ErrorMessage:(NSString*)error {
  60. if (items == nil) {
  61. [firstMenuItem setTitle:error];
  62. [self clearMenuItems];
  63. [firstMenuItem setHidden:NO];
  64. return;
  65. }
  66. lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
  67. items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
  68. redditItems = items;
  69. [self clearMenuItems];
  70. [firstMenuItem setHidden:YES];
  71. [self putItemArrayInMenu:redditItems];
  72. }
  73. -(void)reloadListIsAuthenticatedCallback {
  74. if (currentState.useSubscriptions) {
  75. [NSThread detachNewThreadSelector:@selector(readFrontpage:) toTarget:api withObject:self];
  76. } else {
  77. [api setSubreddits:currentState.subreddits];
  78. [NSThread detachNewThreadSelector:@selector(readSubreddits:) toTarget:api withObject:self];
  79. }
  80. }
  81. -(void)reloadListWithOptions {
  82. if ([currentState.modhash isEqualToString:@""]) {
  83. [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
  84. [self clearMenuItems];
  85. [firstMenuItem setHidden:NO];
  86. [self showPreferences:nil];
  87. return;
  88. }
  89. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash Length:currentState.length];
  90. [NSThread detachNewThreadSelector:@selector(isAuthenticatedNewModhash:) toTarget:api withObject:self];
  91. }
  92. - (IBAction)reloadCompleteList:(id)sender {
  93. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", @"Statusitem when user clicks reload")];
  94. [self clearMenuItems];
  95. [firstMenuItem setHidden:NO];
  96. lastFullName = nil; // reload from start
  97. [self reloadListWithOptions];
  98. }
  99. - (IBAction)reloadNextList:(id)sender {
  100. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", nil)];
  101. [self clearMenuItems];
  102. [firstMenuItem setHidden:NO];
  103. [self reloadListWithOptions];
  104. }
  105. -(IBAction)linkToOpen:(id)sender {
  106. NSString *title = [(NSMenuItem *)sender title];
  107. if ([title isEqualToString:NSLocalizedString(@"Link...", nil)]) {
  108. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  109. NSMenuItem *item = [menuItems objectAtIndex:i];
  110. NSMenu *submenu = item.submenu;
  111. if ((submenu != nil) && (sender == [submenu itemAtIndex:0])) {
  112. RedditItem *rItem = [redditItems objectAtIndex:i];
  113. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  114. break;
  115. }
  116. }
  117. } else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)]) {
  118. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  119. NSMenuItem *item = [menuItems objectAtIndex:i];
  120. NSMenu *submenu = item.submenu;
  121. if ((submenu != nil) && (sender == [submenu itemAtIndex:1])) {
  122. RedditItem *rItem = [redditItems objectAtIndex:i];
  123. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem comments]]];
  124. break;
  125. }
  126. }
  127. } else {
  128. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  129. NSMenuItem *item = [menuItems objectAtIndex:i];
  130. if (sender == item) {
  131. RedditItem *rItem = [redditItems objectAtIndex:i];
  132. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  133. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. -(void)clearMenuItems {
  140. if (menuItems != nil) {
  141. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  142. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  143. }
  144. menuItems = nil;
  145. }
  146. }
  147. -(void)putItemArrayInMenu:(NSArray *)array {
  148. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  149. for (NSUInteger i = 0; i < [array count]; i++) {
  150. NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
  151. [items addObject:item];
  152. [statusMenu insertItem:item atIndex:i];
  153. }
  154. menuItems = items;
  155. }
  156. -(NSMenuItem *)prepareItemForMenu:(RedditItem *)reddit {
  157. NSMenuItem *item = [[NSMenuItem alloc] init];
  158. [item setTitle:reddit.name];
  159. if (![reddit.name isEqualToString:reddit.fullName])
  160. [item setToolTip:reddit.fullName];
  161. if (reddit.isSelf) {
  162. [item setAction:@selector(linkToOpen:)];
  163. [item setKeyEquivalent:@""];
  164. } else {
  165. NSMenu *submenu = [[NSMenu alloc] init];
  166. [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
  167. [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
  168. [item setSubmenu:submenu];
  169. }
  170. return item;
  171. }
  172. -(IBAction)showPreferences:(id)sender {
  173. [NSApp activateIgnoringOtherApps:YES];
  174. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  175. [prefWindow setParent:self];
  176. [prefWindow setState:currentState];
  177. [prefWindow showWindow:self];
  178. [[prefWindow window] makeKeyAndOrderFront:self];
  179. }
  180. -(IBAction)showAbout:(id)sender {
  181. [NSApp activateIgnoringOtherApps:YES];
  182. [application orderFrontStandardAboutPanel:self];
  183. }
  184. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length printSubs:(Boolean)showSubreddits {
  185. currentState.username = name;
  186. currentState.modhash = modhash;
  187. currentState.useSubscriptions = subscriptions;
  188. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  189. currentState.length = length;
  190. currentState.showSubreddit = showSubreddits;
  191. [currentState savePreferences];
  192. lastFullName = nil; // reload from start
  193. [self reloadListWithOptions];
  194. }
  195. @end