blob: 86601088dc07cbcd9afb5bb58a96c8a2fae95ed0 [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];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070029}
30
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070031-(void)awakeFromNib
32{
33 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070034 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070035 [statusItem setToolTip:@"NDN Control Center"];
36 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070037 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070038 //[statusItem setTarget:self];
39
40 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070041 NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070042 menuIcon = [[NSImage alloc] initWithContentsOfFile:path];
43 [statusItem setTitle:@""];
44 [statusItem setImage:menuIcon];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070045
Ilya Moiseenko01264322013-09-26 15:34:21 -070046
Ilya Moiseenko01264322013-09-26 15:34:21 -070047 [connectionStatus setView: connectionStatusView];
48 [connectionStatus setTarget:self];
49 [daemonStatus setView: daemonStatusView];
50 [daemonStatus setTarget:self];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070051}
52
53-(IBAction)switchDaemon:(id)sender
54{
55 if (daemonStarted)
56 {
57 daemonStarted = false;
58 [sender setTitle:@"Start"];
Ilya Moiseenko01264322013-09-26 15:34:21 -070059 [connectionStatusText setStringValue:@"Disconnected"];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070060
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070061 NSTask *task = [[NSTask alloc] init];
62 [task setLaunchPath: NDND_STOP_COMMAND];
63 [task launch];
64
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070065 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 Moiseenko01264322013-09-26 15:34:21 -070074 [connectionStatusText setStringValue:@"Connected"];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070075
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070076 NSTask *task = [[NSTask alloc] init];
77 [task setLaunchPath: NDND_START_COMMAND];
78 [task launch];
79
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070080 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 Moiseenko350b9b52013-09-25 16:38:41 -070085}
86
87-(IBAction)openDaemonStatus:(id)sender
88{
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070089}
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 Afanasyev88f0b3a2013-09-24 23:52:08 -0700104}
105
Ilya Moiseenko01264322013-09-26 15:34:21 -0700106-(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 Moiseenko01264322013-09-26 15:34:21 -0700114 }
115 else
116 {
117 [statusPopover performClose:nil];
118 }
119}
120
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -0700121-(IBAction)openNDNDPreferences:(id)sender
122{
Ilya Moiseenko90f40c92013-09-27 15:52:01 -0700123 [preferencesPanel setContentView:generalSettingsView];
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -0700124 [preferencesPanel makeKeyAndOrderFront:sender];
125 [preferencesPanel setLevel: NSStatusWindowLevel];
126}
Ilya Moiseenko90f40c92013-09-27 15:52:01 -0700127
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 Moiseenko6d4086c2013-09-27 16:56:02 -0700167-(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 Afanasyev88f0b3a2013-09-24 23:52:08 -0700188@end