blob: c35967149df6824aa87967cbe6ab005c6f240ad7 [file] [log] [blame]
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -07001/* -*- 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 Afanasyev2beff7f2013-09-27 17:50:36 -07009#include "config.h"
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070010#import "menu-delegate.h"
11
Alexander Afanasyev2beff7f2013-09-27 17:50:36 -070012#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 Moiseenko2dffcf52013-09-27 15:08:04 -070015
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070016@implementation MenuDelegate
17
18- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
19{
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -070020 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 Moiseenkoe7058e72013-10-02 15:56:45 -070029
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 Afanasyev88f0b3a2013-09-24 23:52:08 -070041}
42
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070043-(void)awakeFromNib
44{
45 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070046 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070047 [statusItem setToolTip:@"NDN Control Center"];
48 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070049 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070050 //[statusItem setTarget:self];
51
52 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070053 NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070054 menuIcon = [[NSImage alloc] initWithContentsOfFile:path];
55 [statusItem setTitle:@""];
56 [statusItem setImage:menuIcon];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070057
Ilya Moiseenko01264322013-09-26 15:34:21 -070058
Ilya Moiseenko01264322013-09-26 15:34:21 -070059 [connectionStatus setView: connectionStatusView];
60 [connectionStatus setTarget:self];
61 [daemonStatus setView: daemonStatusView];
62 [daemonStatus setTarget:self];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070063}
64
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070065-(IBAction)openDaemonStatus:(id)sender
66{
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070067}
68
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -070069-(IBAction)showExitConfirmationWindow:(id)sender
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070070{
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -070071 [exitWindow makeKeyAndOrderFront:sender];
72 [exitWindow setLevel: NSStatusWindowLevel];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070073}
74
Ilya Moiseenko01264322013-09-26 15:34:21 -070075-(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 Moiseenko01264322013-09-26 15:34:21 -070083 }
84 else
85 {
86 [statusPopover performClose:nil];
87 }
88}
89
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -070090-(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 Moiseenkoe7058e72013-10-02 15:56:45 -0700111-(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 Afanasyev88f0b3a2013-09-24 23:52:08 -0700122@end