Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 1 | /* -*- Mode: objc; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California, |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 5 | * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD |
| 6 | * authors and contributors. |
| 7 | * |
| 8 | * NFD Control Center is free software: you can redistribute it and/or modify it under the |
| 9 | * terms of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 14 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with NFD |
| 17 | * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * \author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 20 | * \author Ilya Moiseenko <http://ilyamoiseenko.com/> |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 23 | #include "config.hpp" |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 24 | #import "menu-delegate.h" |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 25 | #import "ndnd-status-operation.h" |
Ilya Moiseenko | 0c9ab7c | 2013-10-29 15:08:23 -0700 | [diff] [blame] | 26 | #import "tight-menu-item-view.h" |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 28 | @implementation MenuDelegate |
| 29 | |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame] | 30 | @synthesize interestSent; |
| 31 | @synthesize interestRecv; |
| 32 | @synthesize dataSent; |
| 33 | @synthesize dataRecv; |
| 34 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 35 | -(id)init |
| 36 | { |
| 37 | if (![super init]) { |
| 38 | return nil; |
| 39 | } |
| 40 | |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame] | 41 | interestSent = @"N/A"; |
| 42 | interestRecv = @"N/A"; |
| 43 | dataSent = @"N/A"; |
| 44 | dataRecv = @"N/A"; |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 45 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 46 | m_autoconfInProgress = false; |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 47 | // m_operationQueue = [[NSOperationQueue alloc] init]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 48 | return self; |
| 49 | } |
| 50 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 51 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| 52 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 53 | // Register the preference defaults early. |
| 54 | NSDictionary *appDefaults = |
| 55 | [NSDictionary dictionaryWithObjectsAndKeys: |
| 56 | [NSNumber numberWithBool:YES], @"allowSoftwareUpdates", |
| 57 | [NSNumber numberWithBool:YES], @"enableHubDiscovery", |
| 58 | [NSNumber numberWithBool:NO], @"shutdownNdndOnExit", |
| 59 | nil |
| 60 | ]; |
| 61 | [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 62 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 63 | // Other initialization... |
| 64 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 65 | m_daemonStarted = false; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 66 | |
| 67 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
| 68 | m_connectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatConnected" ofType:@"png"]]; |
| 69 | m_disconnectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatDisconnected" ofType:@"png"]]; |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 70 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 71 | [statusItem setImage:m_disconnectedIcon]; |
| 72 | |
| 73 | // m_statusXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status" ofType:@"xslt"]]; |
| 74 | // m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]]; |
| 75 | |
| 76 | // [NSTimer scheduledTimerWithTimeInterval: 1.0 |
| 77 | // target: self |
| 78 | // selector:@selector(onTick:) |
| 79 | // userInfo: nil |
| 80 | // repeats:YES]; |
| 81 | // [self updateStatus]; |
| 82 | |
| 83 | // m_systemEvents = [[SystemEvents alloc] init]; |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | - (void)applicationWillTerminate:(NSNotification *)aNotification |
| 87 | { |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 88 | // [m_systemEvents disable]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 91 | -(void)awakeFromNib |
| 92 | { |
| 93 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 94 | [statusItem setMenu:statusMenu]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 95 | [statusItem setToolTip:@"NDN Control Center"]; |
| 96 | [statusItem setEnabled:YES]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 97 | [statusItem setHighlightMode:YES]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 98 | //[statusItem setTarget:self]; |
| 99 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 100 | [statusItem setTitle:@""]; |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 101 | |
Ilya Moiseenko | 0c9ab7c | 2013-10-29 15:08:23 -0700 | [diff] [blame] | 102 | float menuItemHeight = 20; |
| 103 | |
| 104 | NSRect viewRect = NSMakeRect(0, 0, /* width autoresizes */ 1, menuItemHeight); |
| 105 | connectionStatusView = [[TightMenuItemView alloc] initWithFrame:viewRect]; |
| 106 | connectionStatusView.autoresizingMask = NSViewWidthSizable; |
| 107 | |
| 108 | [connectionStatus setView:connectionStatusView]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 109 | [connectionStatus setTarget:self]; |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 110 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 111 | [daemonStatus setView: daemonStatusView]; |
| 112 | [daemonStatus setTarget:self]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 115 | -(IBAction)openDaemonStatus:(id)sender |
| 116 | { |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 119 | -(IBAction)showExitConfirmationWindow:(id)sender |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 120 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 121 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shutdownNdndOnExit"]) { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 122 | [m_operationQueue cancelAllOperations]; |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 123 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 124 | // [m_operationQueue addOperationWithBlock:^{ |
| 125 | // NSTask *task = [[NSTask alloc] init]; |
| 126 | // [task setLaunchPath: @NDND_STOP_COMMAND]; |
| 127 | // [task launch]; |
| 128 | // [task waitUntilExit]; |
| 129 | // }]; |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 130 | |
| 131 | [m_operationQueue waitUntilAllOperationsAreFinished]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 132 | [NSApp terminate:self]; |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 133 | } |
| 134 | else { |
| 135 | NSAlert *alert = [[NSAlert alloc] init]; |
| 136 | [alert addButtonWithTitle:@"Yes"]; |
| 137 | [alert addButtonWithTitle:@"No"]; |
| 138 | [alert addButtonWithTitle:@"Cancel"]; |
| 139 | [alert setMessageText:@"Shutdown NDN daemon as well?"]; |
| 140 | [alert setInformativeText:@"All NDN operations will be become unavailable."]; |
| 141 | [alert setAlertStyle:NSCriticalAlertStyle]; |
| 142 | // [alert setShowsSuppressionButton: YES]; |
| 143 | |
| 144 | NSInteger res = [alert runModal]; |
| 145 | if (res == NSAlertFirstButtonReturn) { |
| 146 | // "YES" stop ndnd |
| 147 | [m_operationQueue cancelAllOperations]; |
| 148 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 149 | // [m_operationQueue addOperationWithBlock:^{ |
| 150 | // NSTask *task = [[NSTask alloc] init]; |
| 151 | // [task setLaunchPath: @NDND_STOP_COMMAND]; |
| 152 | // [task launch]; |
| 153 | // [task waitUntilExit]; |
| 154 | // }]; |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 155 | |
| 156 | [m_operationQueue waitUntilAllOperationsAreFinished]; |
| 157 | [NSApp terminate:self]; |
| 158 | } else if (res == NSAlertSecondButtonReturn) { |
| 159 | // "NO" terminate app but keep ndnd running |
| 160 | [m_operationQueue cancelAllOperations]; |
| 161 | [NSApp terminate:self]; |
| 162 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 163 | } |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 166 | -(void)onTick:(NSTimer *)timer |
| 167 | { |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 168 | [self updateStatus]; |
| 169 | } |
| 170 | |
| 171 | -(void)updateStatus |
| 172 | { |
| 173 | NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self]; |
| 174 | [m_operationQueue addOperation:operation]; |
| 175 | } |
| 176 | |
| 177 | -(void)updateStatusWithDependency:(NSOperation*)dependency |
| 178 | { |
| 179 | NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self]; |
| 180 | [operation addDependency:dependency]; |
| 181 | |
| 182 | [m_operationQueue addOperation:dependency]; |
| 183 | [m_operationQueue addOperation:operation]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | - (void)statusUpdated:(NSXMLDocument*)document |
| 187 | { |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 188 | // if (!m_daemonStarted) { |
| 189 | // m_daemonStarted = true; |
| 190 | // [connectionStatusView setStatus:@"Active"]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 192 | // [statusItem setImage:m_connectedIcon]; |
| 193 | // } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 194 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 195 | // NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt |
| 196 | // arguments:nil |
| 197 | // error:nil]; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 199 | // NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt |
| 200 | // arguments:nil |
| 201 | // error:nil]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 202 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 203 | // NSXMLNode *element = [[statusXml rootElement] childAtIndex:0]; //data |
| 204 | // [self setDataRecv:[[element childAtIndex:0] stringValue]]; |
| 205 | // [self setDataSent:[[element childAtIndex:1] stringValue]]; |
| 206 | |
| 207 | // element = [[statusXml rootElement] childAtIndex:1]; //interests |
| 208 | // [self setInterestRecv:[[element childAtIndex:0] stringValue]]; |
| 209 | // [self setInterestSent:[[element childAtIndex:1] stringValue]]; |
| 210 | |
| 211 | // [preferencesDelegate updateFibStatus:statusFibXml]; |
| 212 | |
| 213 | // if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { |
| 214 | // NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil]; |
| 215 | // if ([autoconf count] == 0) |
| 216 | // { |
| 217 | // [self restartDaemon:nil]; |
| 218 | // } |
| 219 | // } |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 222 | - (void)statusUnavailable:(id)none |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 223 | { |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 224 | // // try start ndnd if it is not started yet |
| 225 | // if (m_daemonStarted) { |
| 226 | // m_daemonStarted = false; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 227 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 228 | // [connectionStatusView setStatus:@"Starting..."]; |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 230 | // [statusItem setImage:m_disconnectedIcon]; |
| 231 | // } |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 232 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 233 | // [self setInterestSent:@"N/A"]; |
| 234 | // [self setInterestRecv:@"N/A"]; |
| 235 | // [self setDataSent:@"N/A"]; |
| 236 | // [self setDataRecv:@"N/A"]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 237 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 238 | // [preferencesDelegate updateFibStatus:nil]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 239 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 240 | // m_autoconfInProgress = true; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 241 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 242 | // // NSOperation *startOp = [NSBlockOperation blockOperationWithBlock:^{ |
| 243 | // // NSTask *task = [[NSTask alloc] init]; |
| 244 | // // [task setLaunchPath: @NDND_START_COMMAND]; |
| 245 | // // [task launch]; |
| 246 | // // }]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 247 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 248 | // // if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { |
| 249 | // // NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{ |
| 250 | // // NSTask *task = [[NSTask alloc] init]; |
| 251 | // // [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND]; |
| 252 | // // [task launch]; |
| 253 | // // [task waitUntilExit]; |
| 254 | |
| 255 | // // m_autoconfInProgress = false; |
| 256 | // // }]; |
| 257 | |
| 258 | // // [autoconfOp addDependency:startOp]; |
| 259 | // // [m_operationQueue addOperation:autoconfOp]; |
| 260 | // // } |
| 261 | |
| 262 | // // [m_operationQueue addOperation:startOp]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | -(void)restartDaemon:(id)none |
| 266 | { |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 267 | // if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) |
| 268 | // return; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 269 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 270 | // if (m_autoconfInProgress) |
| 271 | // return; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 272 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame^] | 273 | // NSLog(@"No automatically detected route configured, trying to get one"); |
| 274 | |
| 275 | // // m_autoconfInProgress = true; |
| 276 | // // [m_operationQueue addOperationWithBlock:^{ |
| 277 | // // NSTask *task = [[NSTask alloc] init]; |
| 278 | // // [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND]; |
| 279 | // // [task launch]; |
| 280 | // // [task waitUntilExit]; |
| 281 | |
| 282 | // // m_autoconfInProgress = false; |
| 283 | // // }]; |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 286 | @end |