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