aka RedditBar, Mac OS X menu bar reddit client
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AppDelegate.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. NSInteger itemsBeforeLinkList = 2;
  32. NSInteger numberOfStaticMenuItems = 10;
  33. #define MULTIPLIER_PM_INTERVALL_TO_SEC 60
  34. #define RECHECK_PM_AFTER_OPEN 7
  35. #define SUBMENU_INDEX_LINK 0
  36. #define SUBMENU_INDEX_COMMENTS 1
  37. #define SUBMENU_INDEX_BOTH 2
  38. @synthesize statusMenu, statusItem, statusImage, statusHighlightImage, orangeredImage, orangeredHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems, lastFullName, refreshTimer, PMItem, PMSeparator;
  39. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  40. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  41. NSBundle *bundle = [NSBundle mainBundle];
  42. statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
  43. statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];
  44. orangeredImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"orangered" ofType:@"png"]];
  45. orangeredHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"orangered-alt" ofType:@"png"]];
  46. [statusItem setImage:statusImage];
  47. [statusItem setAlternateImage:statusHighlightImage];
  48. [statusItem setMenu:statusMenu];
  49. [statusItem setToolTip:NSLocalizedString(@"RedditBar", @"Main Menuitem Tooltip")];
  50. [statusItem setHighlightMode:YES];
  51. currentState = [[StateModel alloc] init];
  52. [currentState registerDefaultPreferences];
  53. [currentState loadPreferences];
  54. lastFullName = nil;
  55. [self reloadListWithOptions];
  56. [self recreateRefreshTimer];
  57. [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
  58. }
  59. -(BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
  60. return YES;
  61. }
  62. -(void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
  63. [self openUnread:nil];
  64. }
  65. -(void)recreateRefreshTimer {
  66. if (refreshTimer != nil)
  67. [refreshTimer invalidate];
  68. refreshTimer = [NSTimer scheduledTimerWithTimeInterval:(currentState.refreshInterval * MULTIPLIER_PM_INTERVALL_TO_SEC) target:self selector:@selector(refreshTick:) userInfo:nil repeats:YES];
  69. [refreshTimer fire];
  70. }
  71. -(void)refreshTick:(NSTimer *)timer {
  72. [NSThread detachNewThreadSelector:@selector(readPMs:) toTarget:api withObject:self];
  73. }
  74. -(void)readPMsCallback:(NSArray *)items {
  75. if ((items == nil) || ([items count] < 1) || (((NSNumber *)[items objectAtIndex:0]).integerValue == 0)) {
  76. [statusItem setImage:statusImage];
  77. [statusItem setAlternateImage:statusHighlightImage];
  78. [PMItem setHidden:TRUE];
  79. [PMSeparator setHidden:TRUE];
  80. } else {
  81. [statusItem setImage:orangeredImage];
  82. [statusItem setAlternateImage:orangeredHighlightImage];
  83. [PMItem setTitle:[NSString stringWithFormat:NSLocalizedString(@"You've got %ld unread PMs.", @"PM message"), (long)((NSNumber *)[items objectAtIndex:0]).integerValue]];
  84. [PMItem setHidden:FALSE];
  85. [PMSeparator setHidden:FALSE];
  86. if ([items count] >= 2) {
  87. if (![currentState.lastNotifiedPM isEqualToString:[items objectAtIndex:1]]) {
  88. currentState.lastNotifiedPM = [items objectAtIndex:1];
  89. [currentState savePreferences];
  90. NSUserNotification *notification = [[NSUserNotification alloc] init];
  91. notification.title = NSLocalizedString(@"New Reddit PM!", @"Notification Title");
  92. notification.informativeText = [NSString stringWithFormat:NSLocalizedString(@"You've got %ld unread PMs.", nil), (long)((NSNumber *)[items objectAtIndex:0]).integerValue];
  93. [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
  94. }
  95. }
  96. }
  97. }
  98. -(void)reloadListNotAuthenticatedCallback {
  99. [firstMenuItem setTitle:NSLocalizedString(@"Login Error!", @"Statusitem when API is not authenticated")];
  100. [self clearMenuItems];
  101. [firstMenuItem setHidden:NO];
  102. }
  103. -(void)reloadListHasFrontpageCallback:(NSArray *)items {
  104. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
  105. }
  106. -(void)reloadListHasSubredditsCallback:(NSArray *)items {
  107. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
  108. }
  109. -(void)reloadListHasXCallback:(NSArray *)items ErrorMessage:(NSString*)error {
  110. if (items == nil) {
  111. [firstMenuItem setTitle:error];
  112. [self clearMenuItems];
  113. [firstMenuItem setHidden:NO];
  114. return;
  115. }
  116. lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
  117. items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
  118. redditItems = items;
  119. [self clearMenuItems];
  120. [firstMenuItem setHidden:YES];
  121. [self putItemArrayInMenu:redditItems];
  122. }
  123. -(void)reloadListIsAuthenticatedCallback {
  124. if (currentState.useSubscriptions) {
  125. [NSThread detachNewThreadSelector:@selector(readFrontpage:) toTarget:api withObject:self];
  126. } else {
  127. [api setSubreddits:currentState.subreddits];
  128. [NSThread detachNewThreadSelector:@selector(readSubreddits:) toTarget:api withObject:self];
  129. }
  130. }
  131. -(void)singleItemReloadedCallback:(NSArray *)items {
  132. if (items != nil) {
  133. lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
  134. items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
  135. NSMutableArray *newMenuItems = [NSMutableArray arrayWithArray:menuItems];
  136. NSMenuItem *item = [self prepareItemForMenu:[items objectAtIndex:0]];
  137. [newMenuItems addObject:item];
  138. [statusMenu insertItem:item atIndex:([statusMenu numberOfItems] - numberOfStaticMenuItems + itemsBeforeLinkList)];
  139. menuItems = newMenuItems;
  140. redditItems = [redditItems arrayByAddingObjectsFromArray:items];
  141. }
  142. }
  143. -(void)reloadListWithOptions {
  144. if ([currentState.modhash isEqualToString:@""]) {
  145. [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
  146. [self clearMenuItems];
  147. [firstMenuItem setHidden:NO];
  148. [self showPreferences:nil];
  149. return;
  150. }
  151. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash Length:currentState.length TitleLength:currentState.titleLength];
  152. [NSThread detachNewThreadSelector:@selector(isAuthenticatedNewModhash:) toTarget:api withObject:self];
  153. }
  154. - (IBAction)reloadCompleteList:(id)sender {
  155. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", @"Statusitem when user clicks reload")];
  156. [self clearMenuItems];
  157. [firstMenuItem setHidden:NO];
  158. lastFullName = nil; // reload from start
  159. [self reloadListWithOptions];
  160. }
  161. - (IBAction)reloadNextList:(id)sender {
  162. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", nil)];
  163. [self clearMenuItems];
  164. [firstMenuItem setHidden:NO];
  165. [self reloadListWithOptions];
  166. }
  167. -(void)openAndRemoveAndReloadWithIndex:(NSInteger)index Comments:(Boolean)comments Both:(Boolean)both {
  168. RedditItem *rItem = [redditItems objectAtIndex:index];
  169. NSString *url;
  170. if (comments) {
  171. url = [rItem comments];
  172. [rItem setVisitedComments:TRUE];
  173. } else {
  174. url = [rItem link];
  175. [rItem setVisitedLink:TRUE];
  176. }
  177. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  178. if (both) {
  179. if (!comments) {
  180. url = [rItem comments];
  181. [rItem setVisitedComments:TRUE];
  182. } else {
  183. url = [rItem link];
  184. [rItem setVisitedLink:TRUE];
  185. }
  186. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  187. }
  188. if (currentState.removeVisited) {
  189. Boolean removed = FALSE;
  190. if ((rItem.isSelf && (rItem.visitedLink || rItem.visitedComments)) || ((!rItem.isSelf) && rItem.visitedLink && rItem.visitedComments)) {
  191. [statusMenu removeItem:[menuItems objectAtIndex:index]];
  192. removed = TRUE;
  193. }
  194. if (removed && ([statusMenu numberOfItems] <= numberOfStaticMenuItems)) {
  195. [self reloadNextList:nil];
  196. } else {
  197. if (removed && currentState.reloadAfterVisit) {
  198. [NSThread detachNewThreadSelector:@selector(readSingleItem:) toTarget:api withObject:self];
  199. }
  200. }
  201. }
  202. }
  203. -(IBAction)linkToOpen:(id)sender {
  204. NSString *title = [(NSMenuItem *)sender title];
  205. if ([title isEqualToString:NSLocalizedString(@"Link...", nil)] || [title isEqualToString:NSLocalizedString(@"Comments...", nil)] || [title isEqualToString:NSLocalizedString(@"Both", nil)]) {
  206. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  207. NSMenuItem *item = [menuItems objectAtIndex:i];
  208. NSMenu *submenu = item.submenu;
  209. Boolean isComments = [title isEqualToString:NSLocalizedString(@"Comments...", nil)];
  210. Boolean isBoth = [title isEqualToString:NSLocalizedString(@"Both", nil)];
  211. if (isBoth) {
  212. isComments = !isComments; // Open comments first, then link
  213. }
  214. NSInteger index;
  215. if ([title isEqualToString:NSLocalizedString(@"Link...", nil)])
  216. index = SUBMENU_INDEX_LINK;
  217. else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)])
  218. index = SUBMENU_INDEX_COMMENTS;
  219. else
  220. index = SUBMENU_INDEX_BOTH;
  221. if ((submenu != nil) && (sender == [submenu itemAtIndex:index])) {
  222. [self openAndRemoveAndReloadWithIndex:i Comments:isComments Both:isBoth];
  223. break;
  224. }
  225. }
  226. } else {
  227. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  228. NSMenuItem *item = [menuItems objectAtIndex:i];
  229. if (sender == item) {
  230. [self openAndRemoveAndReloadWithIndex:i Comments:FALSE Both:FALSE];
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. -(IBAction)openUnread:(id)sender {
  237. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.reddit.com/message/unread"]];
  238. [NSTimer scheduledTimerWithTimeInterval:RECHECK_PM_AFTER_OPEN target:self selector:@selector(refreshTick:) userInfo:nil repeats:NO];
  239. }
  240. -(void)clearMenuItems {
  241. if (menuItems != nil) {
  242. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  243. NSMenuItem *item = [menuItems objectAtIndex:i];
  244. if ([statusMenu indexOfItem:item] != -1)
  245. [statusMenu removeItem:item];
  246. }
  247. menuItems = nil;
  248. }
  249. }
  250. -(void)putItemArrayInMenu:(NSArray *)array {
  251. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  252. for (NSUInteger i = 0; i < [array count]; i++) {
  253. NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
  254. [items addObject:item];
  255. [statusMenu insertItem:item atIndex:(i + itemsBeforeLinkList)];
  256. }
  257. menuItems = items;
  258. }
  259. -(NSMenuItem *)prepareItemForMenu:(RedditItem *)reddit {
  260. NSMenuItem *item = [[NSMenuItem alloc] init];
  261. [item setTitle:reddit.name];
  262. if (![reddit.name isEqualToString:reddit.fullName])
  263. [item setToolTip:reddit.fullName];
  264. if (reddit.isSelf) {
  265. [item setAction:@selector(linkToOpen:)];
  266. [item setKeyEquivalent:@""];
  267. } else {
  268. NSMenu *submenu = [[NSMenu alloc] init];
  269. [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
  270. [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
  271. [submenu addItemWithTitle:NSLocalizedString(@"Both", @"Link & Comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
  272. [item setSubmenu:submenu];
  273. }
  274. return item;
  275. }
  276. -(IBAction)showPreferences:(id)sender {
  277. [NSApp activateIgnoringOtherApps:YES];
  278. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  279. [prefWindow setParent:self];
  280. [prefWindow setState:currentState];
  281. [prefWindow showWindow:self];
  282. [[prefWindow window] makeKeyAndOrderFront:self];
  283. }
  284. -(IBAction)showAbout:(id)sender {
  285. [NSApp activateIgnoringOtherApps:YES];
  286. [application orderFrontStandardAboutPanel:self];
  287. }
  288. -(void)prefsDidSave {
  289. [currentState savePreferences];
  290. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", nil)];
  291. [self clearMenuItems];
  292. [firstMenuItem setHidden:NO];
  293. lastFullName = nil; // reload from start
  294. [self reloadListWithOptions];
  295. [self recreateRefreshTimer];
  296. }
  297. @end