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" |
| 11 | |
Alexander Afanasyev | 2beff7f | 2013-09-27 17:50:36 -0700 | [diff] [blame^] | 12 | #define NDND_START_COMMAND @ NDNX_ROOT "/bin/ndndstart" |
| 13 | #define NDND_STOP_COMMAND @ NDNX_ROOT "/bin/ndndstop" |
| 14 | #define NDND_STATUS_COMMAND @ NDNX_ROOT "/bin/ndndstatus" |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 15 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 16 | @implementation MenuDelegate |
| 17 | |
| 18 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| 19 | { |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 20 | daemonStarted = false; |
| 21 | allowSoftwareUpdates = true; |
| 22 | enableHubDiscovery = true; |
| 23 | |
| 24 | NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0 |
| 25 | target: self |
| 26 | selector:@selector(onTick:) |
| 27 | userInfo: nil repeats:YES]; |
| 28 | [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 31 | -(void)awakeFromNib |
| 32 | { |
| 33 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 34 | [statusItem setMenu:statusMenu]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 35 | [statusItem setToolTip:@"NDN Control Center"]; |
| 36 | [statusItem setEnabled:YES]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 37 | [statusItem setHighlightMode:YES]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 38 | //[statusItem setTarget:self]; |
| 39 | |
| 40 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 41 | NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 42 | menuIcon = [[NSImage alloc] initWithContentsOfFile:path]; |
| 43 | [statusItem setTitle:@""]; |
| 44 | [statusItem setImage:menuIcon]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 45 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 46 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 47 | [connectionStatus setView: connectionStatusView]; |
| 48 | [connectionStatus setTarget:self]; |
| 49 | [daemonStatus setView: daemonStatusView]; |
| 50 | [daemonStatus setTarget:self]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | -(IBAction)switchDaemon:(id)sender |
| 54 | { |
| 55 | if (daemonStarted) |
| 56 | { |
| 57 | daemonStarted = false; |
| 58 | [sender setTitle:@"Start"]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 59 | [connectionStatusText setStringValue:@"Disconnected"]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 60 | |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 61 | NSTask *task = [[NSTask alloc] init]; |
| 62 | [task setLaunchPath: NDND_STOP_COMMAND]; |
| 63 | [task launch]; |
| 64 | |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 65 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
| 66 | NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"]; |
| 67 | menuIcon = [[NSImage alloc] initWithContentsOfFile:path]; |
| 68 | [statusItem setImage:menuIcon]; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | daemonStarted = true; |
| 73 | [sender setTitle:@"Stop"]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 74 | [connectionStatusText setStringValue:@"Connected"]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 75 | |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 76 | NSTask *task = [[NSTask alloc] init]; |
| 77 | [task setLaunchPath: NDND_START_COMMAND]; |
| 78 | [task launch]; |
| 79 | |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 80 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
| 81 | NSString *path = [bundle pathForResource:@"FlatConnected" ofType:@"png"]; |
| 82 | menuIcon = [[NSImage alloc] initWithContentsOfFile:path]; |
| 83 | [statusItem setImage:menuIcon]; |
| 84 | } |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | -(IBAction)openDaemonStatus:(id)sender |
| 88 | { |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | -(IBAction)openRoutingStatusPage:(id)sender |
| 92 | { |
| 93 | NSURL *pageURL = [NSURL URLWithString:@"http://netlab.cs.memphis.edu/script/htm/status.htm"]; |
| 94 | |
| 95 | [[NSWorkspace sharedWorkspace] openURL: pageURL]; |
| 96 | } |
| 97 | |
| 98 | -(IBAction)openTrafficMapPage:(id)sender |
| 99 | { |
| 100 | |
| 101 | NSURL *pageURL = [NSURL URLWithString:@"http://ndnmap.arl.wustl.edu"]; |
| 102 | |
| 103 | [[NSWorkspace sharedWorkspace] openURL: pageURL]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 106 | -(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item |
| 107 | { |
| 108 | |
| 109 | if( ([item view]!=nil) && (item == daemonStatus) ) |
| 110 | { |
| 111 | [statusPopover showRelativeToRect:[[item view] bounds] |
| 112 | ofView:[item view] |
| 113 | preferredEdge:NSMinXEdge]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
| 117 | [statusPopover performClose:nil]; |
| 118 | } |
| 119 | } |
| 120 | |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 121 | -(IBAction)openNDNDPreferences:(id)sender |
| 122 | { |
Ilya Moiseenko | 90f40c9 | 2013-09-27 15:52:01 -0700 | [diff] [blame] | 123 | [preferencesPanel setContentView:generalSettingsView]; |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 124 | [preferencesPanel makeKeyAndOrderFront:sender]; |
| 125 | [preferencesPanel setLevel: NSStatusWindowLevel]; |
| 126 | } |
Ilya Moiseenko | 90f40c9 | 2013-09-27 15:52:01 -0700 | [diff] [blame] | 127 | |
| 128 | -(IBAction)openGeneralSettings:(id)sender |
| 129 | { |
| 130 | [preferencesPanel setContentView:generalSettingsView]; |
| 131 | } |
| 132 | |
| 133 | -(IBAction)openForwardingSettings:(id)sender |
| 134 | { |
| 135 | [preferencesPanel setContentView:forwardingSettingsView]; |
| 136 | } |
| 137 | |
| 138 | -(IBAction)openSecuritySettings:(id)sender |
| 139 | { |
| 140 | [preferencesPanel setContentView:securitySettingsView]; |
| 141 | } |
| 142 | |
| 143 | -(IBAction)switchSoftwareUpdates:(id)sender |
| 144 | { |
| 145 | if ([(NSButton*)sender state] == NSOnState) |
| 146 | { |
| 147 | allowSoftwareUpdates = true; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | allowSoftwareUpdates = false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | -(IBAction)switchHubDiscovery:(id)sender |
| 156 | { |
| 157 | if ([(NSButton*)sender state] == NSOnState) |
| 158 | { |
| 159 | enableHubDiscovery = true; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | enableHubDiscovery = false; |
| 164 | } |
| 165 | } |
| 166 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 167 | -(void)onTick:(NSTimer *)timer |
| 168 | { |
| 169 | if (daemonStarted) |
| 170 | { |
| 171 | NSTask *task = [[NSTask alloc] init]; |
| 172 | [task setLaunchPath: NDND_STATUS_COMMAND]; |
| 173 | |
| 174 | NSPipe * out = [NSPipe pipe]; |
| 175 | [task setStandardOutput:out]; |
| 176 | |
| 177 | [task launch]; |
| 178 | [task waitUntilExit]; |
| 179 | |
| 180 | NSFileHandle * read = [out fileHandleForReading]; |
| 181 | NSData * dataRead = [read readDataToEndOfFile]; |
| 182 | NSString *stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding]; |
| 183 | |
| 184 | [daemonStatusText setStringValue:stringRead]; |
| 185 | } |
| 186 | } |
| 187 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 188 | @end |