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