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