aka RedditBar, Mac OS X menu bar reddit client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppDelegate.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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, orangeredImage, orangeredHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems, lastFullName, refreshTimer, PMItem, PMSeparator;
  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. orangeredImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"orangered" ofType:@"png"]];
  38. orangeredHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"orangered-alt" ofType:@"png"]];
  39. [statusItem setImage:statusImage];
  40. [statusItem setAlternateImage:statusHighlightImage];
  41. [statusItem setMenu:statusMenu];
  42. [statusItem setToolTip:NSLocalizedString(@"RedditBar", @"Main Menuitem Tooltip")];
  43. [statusItem setHighlightMode:YES];
  44. currentState = [[StateModel alloc] init];
  45. [currentState registerDefaultPreferences];
  46. [currentState loadPreferences];
  47. lastFullName = nil;
  48. [self reloadListWithOptions];
  49. [self recreateRefreshTimer];
  50. }
  51. -(void)recreateRefreshTimer {
  52. if (refreshTimer != nil)
  53. [refreshTimer invalidate];
  54. refreshTimer = [NSTimer scheduledTimerWithTimeInterval:(currentState.refreshInterval * 60) target:self selector:@selector(refreshTick:) userInfo:nil repeats:YES];
  55. [refreshTimer fire];
  56. }
  57. -(void)refreshTick:(NSTimer *)timer {
  58. [NSThread detachNewThreadSelector:@selector(readPMs:) toTarget:api withObject:self];
  59. }
  60. -(void)readPMsCallback:(NSNumber *)items {
  61. if ((items == nil) || ([items integerValue] == 0)) {
  62. [statusItem setImage:statusImage];
  63. [statusItem setAlternateImage:statusHighlightImage];
  64. [PMItem setHidden:TRUE];
  65. [PMSeparator setHidden:TRUE];
  66. } else {
  67. [statusItem setImage:orangeredImage];
  68. [statusItem setAlternateImage:orangeredHighlightImage];
  69. [PMItem setTitle:[NSString stringWithFormat:NSLocalizedString(@"You've got %ld unread PMs!", @"PM message"), (long)items.integerValue]];
  70. [PMItem setHidden:FALSE];
  71. [PMSeparator setHidden:FALSE];
  72. }
  73. }
  74. -(void)reloadListNotAuthenticatedCallback {
  75. [firstMenuItem setTitle:NSLocalizedString(@"Login Error!", @"Statusitem when API is not authenticated")];
  76. [self clearMenuItems];
  77. [firstMenuItem setHidden:NO];
  78. }
  79. -(void)reloadListHasFrontpageCallback:(NSArray *)items {
  80. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
  81. }
  82. -(void)reloadListHasSubredditsCallback:(NSArray *)items {
  83. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
  84. }
  85. -(void)reloadListHasXCallback:(NSArray *)items ErrorMessage:(NSString*)error {
  86. if (items == nil) {
  87. [firstMenuItem setTitle:error];
  88. [self clearMenuItems];
  89. [firstMenuItem setHidden:NO];
  90. return;
  91. }
  92. lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
  93. items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
  94. redditItems = items;
  95. [self clearMenuItems];
  96. [firstMenuItem setHidden:YES];
  97. [self putItemArrayInMenu:redditItems];
  98. }
  99. -(void)reloadListIsAuthenticatedCallback {
  100. if (currentState.useSubscriptions) {
  101. [NSThread detachNewThreadSelector:@selector(readFrontpage:) toTarget:api withObject:self];
  102. } else {
  103. [api setSubreddits:currentState.subreddits];
  104. [NSThread detachNewThreadSelector:@selector(readSubreddits:) toTarget:api withObject:self];
  105. }
  106. }
  107. -(void)reloadListWithOptions {
  108. if ([currentState.modhash isEqualToString:@""]) {
  109. [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
  110. [self clearMenuItems];
  111. [firstMenuItem setHidden:NO];
  112. [self showPreferences:nil];
  113. return;
  114. }
  115. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash Length:currentState.length TitleLength:currentState.titleLength];
  116. [NSThread detachNewThreadSelector:@selector(isAuthenticatedNewModhash:) toTarget:api withObject:self];
  117. }
  118. - (IBAction)reloadCompleteList:(id)sender {
  119. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", @"Statusitem when user clicks reload")];
  120. [self clearMenuItems];
  121. [firstMenuItem setHidden:NO];
  122. lastFullName = nil; // reload from start
  123. [self reloadListWithOptions];
  124. }
  125. - (IBAction)reloadNextList:(id)sender {
  126. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", nil)];
  127. [self clearMenuItems];
  128. [firstMenuItem setHidden:NO];
  129. [self reloadListWithOptions];
  130. }
  131. -(IBAction)linkToOpen:(id)sender {
  132. NSString *title = [(NSMenuItem *)sender title];
  133. if ([title isEqualToString:NSLocalizedString(@"Link...", nil)]) {
  134. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  135. NSMenuItem *item = [menuItems objectAtIndex:i];
  136. NSMenu *submenu = item.submenu;
  137. if ((submenu != nil) && (sender == [submenu itemAtIndex:0])) {
  138. RedditItem *rItem = [redditItems objectAtIndex:i];
  139. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  140. break;
  141. }
  142. }
  143. } else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)]) {
  144. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  145. NSMenuItem *item = [menuItems objectAtIndex:i];
  146. NSMenu *submenu = item.submenu;
  147. if ((submenu != nil) && (sender == [submenu itemAtIndex:1])) {
  148. RedditItem *rItem = [redditItems objectAtIndex:i];
  149. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem comments]]];
  150. break;
  151. }
  152. }
  153. } else {
  154. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  155. NSMenuItem *item = [menuItems objectAtIndex:i];
  156. if (sender == item) {
  157. RedditItem *rItem = [redditItems objectAtIndex:i];
  158. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  159. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. -(IBAction)openUnread:(id)sender {
  166. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.reddit.com/message/unread"]];
  167. [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(refreshTick:) userInfo:nil repeats:NO];
  168. }
  169. -(void)clearMenuItems {
  170. if (menuItems != nil) {
  171. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  172. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  173. }
  174. menuItems = nil;
  175. }
  176. }
  177. -(void)putItemArrayInMenu:(NSArray *)array {
  178. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  179. for (NSUInteger i = 0; i < [array count]; i++) {
  180. NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
  181. [items addObject:item];
  182. [statusMenu insertItem:item atIndex:(i + 2)];
  183. }
  184. menuItems = items;
  185. }
  186. -(NSMenuItem *)prepareItemForMenu:(RedditItem *)reddit {
  187. NSMenuItem *item = [[NSMenuItem alloc] init];
  188. [item setTitle:reddit.name];
  189. if (![reddit.name isEqualToString:reddit.fullName])
  190. [item setToolTip:reddit.fullName];
  191. if (reddit.isSelf) {
  192. [item setAction:@selector(linkToOpen:)];
  193. [item setKeyEquivalent:@""];
  194. } else {
  195. NSMenu *submenu = [[NSMenu alloc] init];
  196. [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
  197. [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
  198. [item setSubmenu:submenu];
  199. }
  200. return item;
  201. }
  202. -(IBAction)showPreferences:(id)sender {
  203. [NSApp activateIgnoringOtherApps:YES];
  204. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  205. [prefWindow setParent:self];
  206. [prefWindow setState:currentState];
  207. [prefWindow showWindow:self];
  208. [[prefWindow window] makeKeyAndOrderFront:self];
  209. }
  210. -(IBAction)showAbout:(id)sender {
  211. [NSApp activateIgnoringOtherApps:YES];
  212. [application orderFrontStandardAboutPanel:self];
  213. }
  214. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length printSubs:(Boolean)showSubreddits titleLength:(NSInteger)titleLength refresh:(NSInteger)refreshInterval {
  215. currentState.username = name;
  216. currentState.modhash = modhash;
  217. currentState.useSubscriptions = subscriptions;
  218. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  219. currentState.length = length;
  220. currentState.showSubreddit = showSubreddits;
  221. currentState.titleLength = titleLength;
  222. currentState.refreshInterval = refreshInterval;
  223. [currentState savePreferences];
  224. lastFullName = nil; // reload from start
  225. [self reloadListWithOptions];
  226. [self recreateRefreshTimer];
  227. }
  228. @end