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]; |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame^] | 29 | |
| 30 | daemonStarted = true; |
| 31 | [connectionStatusText setStringValue:@"Connected"]; |
| 32 | |
| 33 | NSTask *task = [[NSTask alloc] init]; |
| 34 | [task setLaunchPath: NDND_START_COMMAND]; |
| 35 | [task launch]; |
| 36 | |
| 37 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
| 38 | NSString *path = [bundle pathForResource:@"FlatConnected" ofType:@"png"]; |
| 39 | menuIcon = [[NSImage alloc] initWithContentsOfFile:path]; |
| 40 | [statusItem setImage:menuIcon]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 43 | -(void)awakeFromNib |
| 44 | { |
| 45 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 46 | [statusItem setMenu:statusMenu]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 47 | [statusItem setToolTip:@"NDN Control Center"]; |
| 48 | [statusItem setEnabled:YES]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 49 | [statusItem setHighlightMode:YES]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 50 | //[statusItem setTarget:self]; |
| 51 | |
| 52 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 53 | NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 54 | menuIcon = [[NSImage alloc] initWithContentsOfFile:path]; |
| 55 | [statusItem setTitle:@""]; |
| 56 | [statusItem setImage:menuIcon]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 57 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 58 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 59 | [connectionStatus setView: connectionStatusView]; |
| 60 | [connectionStatus setTarget:self]; |
| 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 | { |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame^] | 71 | [exitWindow makeKeyAndOrderFront:sender]; |
| 72 | [exitWindow setLevel: NSStatusWindowLevel]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 75 | -(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item |
| 76 | { |
| 77 | |
| 78 | if( ([item view]!=nil) && (item == daemonStatus) ) |
| 79 | { |
| 80 | [statusPopover showRelativeToRect:[[item view] bounds] |
| 81 | ofView:[item view] |
| 82 | preferredEdge:NSMinXEdge]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 83 | } |
| 84 | else |
| 85 | { |
| 86 | [statusPopover performClose:nil]; |
| 87 | } |
| 88 | } |
| 89 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 90 | -(void)onTick:(NSTimer *)timer |
| 91 | { |
| 92 | if (daemonStarted) |
| 93 | { |
| 94 | NSTask *task = [[NSTask alloc] init]; |
| 95 | [task setLaunchPath: NDND_STATUS_COMMAND]; |
| 96 | |
| 97 | NSPipe * out = [NSPipe pipe]; |
| 98 | [task setStandardOutput:out]; |
| 99 | |
| 100 | [task launch]; |
| 101 | [task waitUntilExit]; |
| 102 | |
| 103 | NSFileHandle * read = [out fileHandleForReading]; |
| 104 | NSData * dataRead = [read readDataToEndOfFile]; |
| 105 | NSString *stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding]; |
| 106 | |
| 107 | [daemonStatusText setStringValue:stringRead]; |
| 108 | } |
| 109 | } |
| 110 | |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame^] | 111 | -(IBAction)confirmExit:(id)sender |
| 112 | { |
| 113 | [NSApp terminate:self]; |
| 114 | } |
| 115 | |
| 116 | -(IBAction)cancelExit:(id)sender |
| 117 | { |
| 118 | if([exitWindow isVisible]) |
| 119 | [exitWindow orderOut:self]; |
| 120 | } |
| 121 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 122 | @end |