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