aka RedditBar, Mac OS X menu bar reddit client
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AppDelegate.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. [self defaultPreferences];
  44. [self loadPreferences];
  45. lastFullName = nil;
  46. [self reloadListWithOptions];
  47. }
  48. -(void)defaultPreferences {
  49. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  50. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:@"username"];
  51. [appDefaults setValue:@"" forKey:@"modhash"];
  52. [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"subscriptions"];
  53. [appDefaults setValue:[NSNumber numberWithInt:10] forKey:@"length"];
  54. [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"showSubs"];
  55. [store registerDefaults:appDefaults];
  56. }
  57. -(void)savePreferences {
  58. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  59. [store setObject:currentState.username forKey:@"username"];
  60. [store setObject:currentState.modhash forKey:@"modhash"];
  61. [store setBool:currentState.useSubscriptions forKey:@"subscriptions"];
  62. [store setObject:currentState.subreddits forKey:@"subreddits"];
  63. [store setInteger:currentState.length forKey:@"length"];
  64. [store setBool:currentState.showSubreddit forKey:@"showSubs"];
  65. [store synchronize];
  66. }
  67. -(void)loadPreferences {
  68. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  69. [store synchronize];
  70. [currentState setUsername:[store stringForKey:@"username"]];
  71. [currentState setModhash:[store stringForKey:@"modhash"]];
  72. [currentState setUseSubscriptions:[store boolForKey:@"subscriptions"]];
  73. [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
  74. [currentState setLength:[store integerForKey:@"length"]];
  75. [currentState setShowSubreddit:[store boolForKey:@"showSubs"]];
  76. }
  77. -(void)reloadListNotAuthenticatedCallback {
  78. [firstMenuItem setTitle:NSLocalizedString(@"Login Error!", @"Statusitem when API is not authenticated")];
  79. [self clearMenuItems];
  80. [firstMenuItem setHidden:NO];
  81. }
  82. -(void)reloadListHasFrontpageCallback:(NSArray *)items {
  83. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
  84. }
  85. -(void)reloadListHasSubredditsCallback:(NSArray *)items {
  86. [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
  87. }
  88. -(void)reloadListHasXCallback:(NSArray *)items ErrorMessage:(NSString*)error {
  89. if (items == nil) {
  90. [firstMenuItem setTitle:error];
  91. [self clearMenuItems];
  92. [firstMenuItem setHidden:NO];
  93. return;
  94. }
  95. lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
  96. items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
  97. redditItems = items;
  98. [self clearMenuItems];
  99. [firstMenuItem setHidden:YES];
  100. [self putItemArrayInMenu:redditItems];
  101. }
  102. -(void)reloadListIsAuthenticatedCallback {
  103. if (currentState.useSubscriptions) {
  104. [NSThread detachNewThreadSelector:@selector(readFrontpage:) toTarget:api withObject:self];
  105. } else {
  106. [api setSubreddits:currentState.subreddits];
  107. [NSThread detachNewThreadSelector:@selector(readSubreddits:) toTarget:api withObject:self];
  108. }
  109. }
  110. -(void)reloadListWithOptions {
  111. if ([currentState.modhash isEqualToString:@""]) {
  112. [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
  113. [self clearMenuItems];
  114. [firstMenuItem setHidden:NO];
  115. [self showPreferences:nil];
  116. return;
  117. }
  118. api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash Length:currentState.length];
  119. [NSThread detachNewThreadSelector:@selector(isAuthenticatedNewModhash:) toTarget:api withObject:self];
  120. }
  121. - (IBAction)reloadCompleteList:(id)sender {
  122. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", @"Statusitem when user clicks reload")];
  123. [self clearMenuItems];
  124. [firstMenuItem setHidden:NO];
  125. lastFullName = nil; // reload from start
  126. [self reloadListWithOptions];
  127. }
  128. - (IBAction)reloadNextList:(id)sender {
  129. [firstMenuItem setTitle:NSLocalizedString(@"Loading...", nil)];
  130. [self clearMenuItems];
  131. [firstMenuItem setHidden:NO];
  132. [self reloadListWithOptions];
  133. }
  134. -(IBAction)linkToOpen:(id)sender {
  135. NSString *title = [(NSMenuItem *)sender title];
  136. if ([title isEqualToString:NSLocalizedString(@"Link...", nil)]) {
  137. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  138. NSMenuItem *item = [menuItems objectAtIndex:i];
  139. NSMenu *submenu = item.submenu;
  140. if ((submenu != nil) && (sender == [submenu itemAtIndex:0])) {
  141. RedditItem *rItem = [redditItems objectAtIndex:i];
  142. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  143. break;
  144. }
  145. }
  146. } else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)]) {
  147. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  148. NSMenuItem *item = [menuItems objectAtIndex:i];
  149. NSMenu *submenu = item.submenu;
  150. if ((submenu != nil) && (sender == [submenu itemAtIndex:1])) {
  151. RedditItem *rItem = [redditItems objectAtIndex:i];
  152. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem comments]]];
  153. break;
  154. }
  155. }
  156. } else {
  157. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  158. NSMenuItem *item = [menuItems objectAtIndex:i];
  159. if (sender == item) {
  160. RedditItem *rItem = [redditItems objectAtIndex:i];
  161. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
  162. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. -(void)clearMenuItems {
  169. if (menuItems != nil) {
  170. for (NSUInteger i = 0; i < [menuItems count]; i++) {
  171. [statusMenu removeItem:[menuItems objectAtIndex:i]];
  172. }
  173. menuItems = nil;
  174. }
  175. }
  176. -(void)putItemArrayInMenu:(NSArray *)array {
  177. NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
  178. for (NSUInteger i = 0; i < [array count]; i++) {
  179. NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
  180. [items addObject:item];
  181. [statusMenu insertItem:item atIndex:i];
  182. }
  183. menuItems = items;
  184. }
  185. -(NSMenuItem *)prepareItemForMenu:(RedditItem *)reddit {
  186. NSMenuItem *item = [[NSMenuItem alloc] init];
  187. [item setTitle:reddit.name];
  188. if (![reddit.name isEqualToString:reddit.fullName])
  189. [item setToolTip:reddit.fullName];
  190. if (reddit.isSelf) {
  191. [item setAction:@selector(linkToOpen:)];
  192. [item setKeyEquivalent:@""];
  193. } else {
  194. NSMenu *submenu = [[NSMenu alloc] init];
  195. [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
  196. [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
  197. [item setSubmenu:submenu];
  198. }
  199. return item;
  200. }
  201. -(IBAction)showPreferences:(id)sender {
  202. [NSApp activateIgnoringOtherApps:YES];
  203. prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
  204. [prefWindow setParent:self];
  205. [prefWindow setState:currentState];
  206. [prefWindow showWindow:self];
  207. }
  208. -(IBAction)showAbout:(id)sender {
  209. [NSApp activateIgnoringOtherApps:YES];
  210. [application orderFrontStandardAboutPanel:self];
  211. }
  212. -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length printSubs:(Boolean)showSubreddits {
  213. currentState.username = name;
  214. currentState.modhash = modhash;
  215. currentState.useSubscriptions = subscriptions;
  216. currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
  217. currentState.length = length;
  218. currentState.showSubreddit = showSubreddits;
  219. [self savePreferences];
  220. lastFullName = nil; // reload from start
  221. [self reloadListWithOptions];
  222. }
  223. @end