Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 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 |
| 38 | target: self |
| 39 | selector:@selector(onTick:) |
| 40 | userInfo: nil repeats:YES]; |
| 41 | [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 42 | |
| 43 | [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]]; |
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) { |
| 82 | [m_operationQueue cancelAllOperations]; |
| 83 | [NSApp terminate:self]; |
| 84 | } else if (res == NSAlertSecondButtonReturn) { |
| 85 | [m_operationQueue cancelAllOperations]; |
| 86 | [NSApp terminate:self]; |
| 87 | } |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 90 | -(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item |
| 91 | { |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 92 | if( ([item view]!=nil) && (item == daemonStatus) ) |
| 93 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 94 | NSView *view = [item view]; |
| 95 | |
| 96 | [statusPopover showRelativeToRect:[view bounds] |
| 97 | ofView:view |
| 98 | preferredEdge:NSMinXEdge]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | { |
| 102 | [statusPopover performClose:nil]; |
| 103 | } |
| 104 | } |
| 105 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 106 | -(void)onTick:(NSTimer *)timer |
| 107 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 108 | [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]]; |
| 109 | } |
| 110 | |
| 111 | - (void)statusUpdated:(NSXMLDocument*)document |
| 112 | { |
| 113 | if (!m_daemonStarted) { |
| 114 | m_daemonStarted = true; |
| 115 | [connectionStatusText setStringValue:@"Active"]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 117 | [statusItem setImage:m_connectedIcon]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 118 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 119 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame^] | 120 | NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt |
| 121 | arguments:nil |
| 122 | error:nil]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 123 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame^] | 124 | NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt |
| 125 | arguments:nil |
| 126 | error:nil]; |
| 127 | |
| 128 | m_statusString = [[NSAttributedString alloc]initWithHTML:[statusXml XMLData] documentAttributes:NULL]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 129 | [daemonStatusHtml setAttributedStringValue:m_statusString]; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame^] | 130 | |
| 131 | [preferencesDelegate updateFibStatus:statusFibXml]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 134 | - (void)statusUnavailable:(id)none |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 135 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 136 | // try start ndnd if it is not started yet |
| 137 | if (m_daemonStarted) { |
| 138 | m_daemonStarted = false; |
| 139 | |
| 140 | [connectionStatusText setStringValue:@"Starting..."]; |
| 141 | |
| 142 | [statusItem setImage:m_disconnectedIcon]; |
| 143 | } |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame^] | 144 | |
| 145 | [daemonStatusHtml setStringValue:@""]; |
| 146 | [preferencesDelegate updateFibStatus:nil]; |
| 147 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 148 | [m_operationQueue addOperationWithBlock:^{ |
| 149 | NSTask *task = [[NSTask alloc] init]; |
| 150 | [task setLaunchPath: @NDND_START_COMMAND]; |
| 151 | [task launch]; |
| 152 | }]; |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 155 | @end |