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