1//
2//  App.m
3//  Keybase
4//
5//  Created by Gabriel on 11/23/15.
6//  Copyright © 2015 Keybase. All rights reserved.
7//
8
9#import "App.h"
10
11@interface App ()
12@property NSStatusItem *statusItem;
13@property KBApp *app;
14@end
15
16@implementation App
17
18- (void)applicationDidFinishLaunching:(NSNotification *)notification {
19  NSUserDefaults *userDefaults = [KBWorkspace userDefaults];
20  [userDefaults registerDefaults:
21   @{
22     @"Preferences.Log.Level": @(DDLogLevelError),
23     }];
24  [KBWorkspace setupLogging];
25
26  _statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
27  //_statusItem.title = @"Keybase";
28
29#if DEBUG
30  NSString *statusBarName = @"StatusIconDev";
31#else
32  NSString *statusBarName = @"StatusIcon";
33#endif
34
35  _statusItem.image = [NSImage imageNamed:statusBarName];
36  //_statusItem.alternateImage = [NSImage imageNamed:@""]; // Highlighted
37  _statusItem.highlightMode = YES; // Blue background when selected
38
39  _app = [[KBApp alloc] init];
40
41  [self updateMenu];
42
43  [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusChanged:) name:KBStatusDidChangeNotification object:nil];
44
45  [_app open];
46}
47
48+ (instancetype)sharedDelegate {
49  return (App *)[NSApp delegate];
50}
51
52- (BOOL)setError:(NSError *)error sender:(NSView *)sender completion:(void (^)(NSModalResponse))completion {
53  return [_app setError:error sender:sender completion:completion];
54}
55
56- (void)updateMenu {
57  _statusItem.menu = [self loadMenu];
58}
59
60- (void)statusChanged:(NSNotification *)notification {
61  [self updateMenu];
62}
63
64- (NSMenu *)loadMenu {
65  NSMenu *menu = [[NSMenu alloc] init];
66
67  /*
68   [menu addItemWithTitle:@"Preferences" action:@selector(preferences:) keyEquivalent:@""];
69
70   KBRGetCurrentStatusRes *userStatus = self.app.appView.userStatus;
71   if (userStatus) {
72   if (userStatus.loggedIn && userStatus.user) {
73   [menu addItemWithTitle:NSStringWithFormat(@"Log Out (%@)", userStatus.user.username) action:@selector(logout:) keyEquivalent:@""];
74   [menu addItem:[NSMenuItem separatorItem]];
75   } else {
76   [menu addItemWithTitle:@"Log In" action:@selector(login:) keyEquivalent:@""];
77   [menu addItem:[NSMenuItem separatorItem]];
78   }
79   }
80   */
81
82  NSString *runMode = NSBundle.mainBundle.infoDictionary[@"KBRunMode"];
83  NSAssert(runMode, @"No run mode");
84  [menu addItemWithTitle:NSStringWithFormat(@"Status (%@)", runMode) action:@selector(showInstallStatus:) keyEquivalent:@""];
85  [menu addItem:[NSMenuItem separatorItem]];
86  [menu addItemWithTitle:@"Quit" action:@selector(quit:) keyEquivalent:@""];
87  return menu;
88}
89
90- (IBAction)preferences:(id)sender {
91  [self.app.preferences openWithUserDefaults:[KBWorkspace userDefaults] sender:self.app.appView];
92}
93
94- (IBAction)showInstallStatus:(id)sender {
95  [self.app.appView showInstallStatusView:^(NSError *error) {}];
96  [self.app.appView openWindow];
97}
98
99- (IBAction)login:(id)sender {
100  [self.app.appView showLogin];
101}
102
103- (IBAction)logout:(id)sender {
104  [self.app.appView logout:YES];
105}
106
107- (IBAction)quit:(id)sender {
108  [self.app quitWithPrompt:NO sender:sender];
109}
110
111#pragma mark Preferences
112
113- (id)preferencesValueForIdentifier:(NSString *)identifier {
114  /*
115   if ([identifier isEqualTo:@"Preferences.Sparkle.AutoUpdate"]) {
116   return @(SUUpdater.sharedUpdater.automaticallyChecksForUpdates);
117   }
118
119   if ([identifier isEqualTo:@"Preferences.Sparkle.CheckInterval"]) {
120   return @(SUUpdater.sharedUpdater.updateCheckInterval);
121   }
122
123   if ([identifier isEqualTo:@"Preferences.Sparkle.AutoDownload"]) {
124   return @(SUUpdater.sharedUpdater.automaticallyDownloadsUpdates);
125   }
126
127   if ([identifier isEqualTo:@"Preferences.Sparkle.SendsProfile"]) {
128   return @(SUUpdater.sharedUpdater.sendsSystemProfile);
129   }
130   */
131
132  if ([identifier isEqualTo:@"Preferences.LaunchAtLogin"]) {
133    return @([KBLoginItem isLoginEnabledForURL:[NSURL fileURLWithPath:NSBundle.mainBundle.executablePath]]);
134  }
135
136  return nil;
137}
138
139- (BOOL)setPrefencesValue:(id)value forIdentifier:(NSString *)identifier synchronize:(BOOL)synchronize {
140  /*
141   if ([identifier isEqualTo:@"Preferences.Sparkle.AutoUpdate"]) {
142   SUUpdater.sharedUpdater.automaticallyChecksForUpdates = [value boolValue];
143   } else if ([identifier isEqualTo:@"Preferences.Sparkle.CheckInterval"]) {
144   SUUpdater.sharedUpdater.updateCheckInterval = [value doubleValue];
145   } else if ([identifier isEqualTo:@"Preferences.Sparkle.AutoDownload"]) {
146   SUUpdater.sharedUpdater.automaticallyDownloadsUpdates = [value boolValue];
147   } else if ([identifier isEqualTo:@"Preferences.Sparkle.SendsProfile"]) {
148   SUUpdater.sharedUpdater.sendsSystemProfile = [value boolValue];
149   */
150  if ([identifier isEqualTo:@"Preferences.LaunchAtLogin"]) {
151    NSError *error = nil;
152    [KBLoginItem setLoginEnabled:[value boolValue] URL:NSBundle.mainBundle.bundleURL error:&error];
153    if (error) DDLogError(@"Error configuring login item: %@", error);
154  } else {
155    // Not found
156    return NO;
157  }
158
159  if (synchronize) {
160    // TODO
161  }
162
163  return YES;
164}
165
166@end
167