Alexander Afanasyev | 14b0948 | 2013-10-11 18:24:45 +0200 | [diff] [blame] | 1 | /* -*- Mode: obj; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 2 | /* |
| 3 | * @copyright See LICENCE for copyright and license information. |
| 4 | * |
| 5 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * @author Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
Alexander Afanasyev | 2beff7f | 2013-09-27 17:50:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 10 | #import "menu-delegate.h" |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 11 | #import "ndnd-status-operation.h" |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 13 | @implementation MenuDelegate |
| 14 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 15 | -(id)init |
| 16 | { |
| 17 | if (![super init]) { |
| 18 | return nil; |
| 19 | } |
| 20 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 21 | m_autoconfInProgress = false; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 22 | m_operationQueue = [[NSOperationQueue alloc] init]; |
| 23 | return self; |
| 24 | } |
| 25 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 26 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| 27 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 28 | // Register the preference defaults early. |
| 29 | NSDictionary *appDefaults = |
| 30 | [NSDictionary dictionaryWithObjectsAndKeys: |
| 31 | [NSNumber numberWithBool:YES], @"allowSoftwareUpdates", |
| 32 | [NSNumber numberWithBool:YES], @"enableHubDiscovery", |
| 33 | [NSNumber numberWithBool:NO], @"shutdownNdndOnExit", |
| 34 | nil |
| 35 | ]; |
| 36 | [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; |
| 37 | |
| 38 | // Other initialization... |
| 39 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 40 | m_daemonStarted = false; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 41 | |
| 42 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
| 43 | m_connectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatConnected" ofType:@"png"]]; |
| 44 | m_disconnectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatDisconnected" ofType:@"png"]]; |
| 45 | m_statusXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status" ofType:@"xslt"]]; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 46 | m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 48 | [NSTimer scheduledTimerWithTimeInterval: 1.0 |
| 49 | target: self |
| 50 | selector:@selector(onTick:) |
| 51 | userInfo: nil |
| 52 | repeats:YES]; |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 53 | [self updateStatus]; |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 54 | |
| 55 | m_systemEvents = [[SystemEvents alloc] init]; |
| 56 | } |
| 57 | |
| 58 | - (void)applicationWillTerminate:(NSNotification *)aNotification |
| 59 | { |
| 60 | [m_systemEvents disable]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 63 | -(void)awakeFromNib |
| 64 | { |
| 65 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 66 | [statusItem setMenu:statusMenu]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 67 | [statusItem setToolTip:@"NDN Control Center"]; |
| 68 | [statusItem setEnabled:YES]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 69 | [statusItem setHighlightMode:YES]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 70 | //[statusItem setTarget:self]; |
| 71 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 72 | [statusItem setTitle:@""]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 73 | [statusItem setImage:m_disconnectedIcon]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 74 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 75 | [connectionStatus setView: connectionStatusView]; |
| 76 | [connectionStatus setTarget:self]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 77 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 78 | [daemonStatus setView: daemonStatusView]; |
| 79 | [daemonStatus setTarget:self]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 82 | -(IBAction)openDaemonStatus:(id)sender |
| 83 | { |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 86 | -(IBAction)showExitConfirmationWindow:(id)sender |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 87 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 88 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shutdownNdndOnExit"]) { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 89 | [m_operationQueue cancelAllOperations]; |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 90 | |
| 91 | [m_operationQueue addOperationWithBlock:^{ |
| 92 | NSTask *task = [[NSTask alloc] init]; |
| 93 | [task setLaunchPath: @NDND_STOP_COMMAND]; |
| 94 | [task launch]; |
| 95 | [task waitUntilExit]; |
| 96 | }]; |
| 97 | |
| 98 | [m_operationQueue waitUntilAllOperationsAreFinished]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 99 | [NSApp terminate:self]; |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 100 | } |
| 101 | else { |
| 102 | NSAlert *alert = [[NSAlert alloc] init]; |
| 103 | [alert addButtonWithTitle:@"Yes"]; |
| 104 | [alert addButtonWithTitle:@"No"]; |
| 105 | [alert addButtonWithTitle:@"Cancel"]; |
| 106 | [alert setMessageText:@"Shutdown NDN daemon as well?"]; |
| 107 | [alert setInformativeText:@"All NDN operations will be become unavailable."]; |
| 108 | [alert setAlertStyle:NSCriticalAlertStyle]; |
| 109 | // [alert setShowsSuppressionButton: YES]; |
| 110 | |
| 111 | NSInteger res = [alert runModal]; |
| 112 | if (res == NSAlertFirstButtonReturn) { |
| 113 | // "YES" stop ndnd |
| 114 | [m_operationQueue cancelAllOperations]; |
| 115 | |
| 116 | [m_operationQueue addOperationWithBlock:^{ |
| 117 | NSTask *task = [[NSTask alloc] init]; |
| 118 | [task setLaunchPath: @NDND_STOP_COMMAND]; |
| 119 | [task launch]; |
| 120 | [task waitUntilExit]; |
| 121 | }]; |
| 122 | |
| 123 | [m_operationQueue waitUntilAllOperationsAreFinished]; |
| 124 | [NSApp terminate:self]; |
| 125 | } else if (res == NSAlertSecondButtonReturn) { |
| 126 | // "NO" terminate app but keep ndnd running |
| 127 | [m_operationQueue cancelAllOperations]; |
| 128 | [NSApp terminate:self]; |
| 129 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 130 | } |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 133 | -(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item |
| 134 | { |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 135 | if( ([item view]!=nil) && (item == daemonStatus) ) |
| 136 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 137 | NSView *view = [item view]; |
| 138 | |
| 139 | [statusPopover showRelativeToRect:[view bounds] |
| 140 | ofView:view |
| 141 | preferredEdge:NSMinXEdge]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 142 | } |
| 143 | else |
| 144 | { |
| 145 | [statusPopover performClose:nil]; |
| 146 | } |
| 147 | } |
| 148 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 149 | -(void)onTick:(NSTimer *)timer |
| 150 | { |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 151 | [self updateStatus]; |
| 152 | } |
| 153 | |
| 154 | -(void)updateStatus |
| 155 | { |
| 156 | NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self]; |
| 157 | [m_operationQueue addOperation:operation]; |
| 158 | } |
| 159 | |
| 160 | -(void)updateStatusWithDependency:(NSOperation*)dependency |
| 161 | { |
| 162 | NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self]; |
| 163 | [operation addDependency:dependency]; |
| 164 | |
| 165 | [m_operationQueue addOperation:dependency]; |
| 166 | [m_operationQueue addOperation:operation]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | - (void)statusUpdated:(NSXMLDocument*)document |
| 170 | { |
| 171 | if (!m_daemonStarted) { |
| 172 | m_daemonStarted = true; |
| 173 | [connectionStatusText setStringValue:@"Active"]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 175 | [statusItem setImage:m_connectedIcon]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 176 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 177 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 178 | NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt |
| 179 | arguments:nil |
| 180 | error:nil]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 181 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 182 | NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt |
| 183 | arguments:nil |
| 184 | error:nil]; |
| 185 | |
| 186 | m_statusString = [[NSAttributedString alloc]initWithHTML:[statusXml XMLData] documentAttributes:NULL]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 187 | [daemonStatusHtml setAttributedStringValue:m_statusString]; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 188 | |
| 189 | [preferencesDelegate updateFibStatus:statusFibXml]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 190 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 191 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { |
| 192 | NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil]; |
| 193 | if ([autoconf count] == 0) |
| 194 | { |
| 195 | [self restartDaemon:nil]; |
| 196 | } |
| 197 | } |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 200 | - (void)statusUnavailable:(id)none |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 201 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 202 | // try start ndnd if it is not started yet |
| 203 | if (m_daemonStarted) { |
| 204 | m_daemonStarted = false; |
| 205 | |
| 206 | [connectionStatusText setStringValue:@"Starting..."]; |
| 207 | |
| 208 | [statusItem setImage:m_disconnectedIcon]; |
| 209 | } |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 210 | |
| 211 | [daemonStatusHtml setStringValue:@""]; |
| 212 | [preferencesDelegate updateFibStatus:nil]; |
| 213 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 214 | m_autoconfInProgress = true; |
| 215 | |
| 216 | NSOperation *startOp = [NSBlockOperation blockOperationWithBlock:^{ |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 217 | NSTask *task = [[NSTask alloc] init]; |
| 218 | [task setLaunchPath: @NDND_START_COMMAND]; |
| 219 | [task launch]; |
| 220 | }]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 221 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 222 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { |
| 223 | NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{ |
| 224 | NSTask *task = [[NSTask alloc] init]; |
| 225 | [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND]; |
| 226 | [task launch]; |
| 227 | [task waitUntilExit]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 228 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 229 | m_autoconfInProgress = false; |
| 230 | }]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 231 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 232 | [autoconfOp addDependency:startOp]; |
| 233 | [m_operationQueue addOperation:autoconfOp]; |
| 234 | } |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 235 | |
| 236 | [m_operationQueue addOperation:startOp]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | -(void)restartDaemon:(id)none |
| 240 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame^] | 241 | if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) |
| 242 | return; |
| 243 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 244 | if (m_autoconfInProgress) |
| 245 | return; |
| 246 | |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 247 | NSLog (@"No automatically detected route configured, trying to get one"); |
| 248 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 249 | m_autoconfInProgress = true; |
| 250 | [m_operationQueue addOperationWithBlock:^{ |
| 251 | NSTask *task = [[NSTask alloc] init]; |
| 252 | [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND]; |
| 253 | [task launch]; |
| 254 | [task waitUntilExit]; |
| 255 | |
| 256 | m_autoconfInProgress = false; |
| 257 | }]; |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 260 | @end |