blob: cdb02e7d73cccee2e67f91b46bbacf362f647b21 [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
9#import "menu-delegate.h"
10
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070011#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 Afanasyev88f0b3a2013-09-24 23:52:08 -070015@implementation MenuDelegate
16
17- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
18{
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -070019 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 Afanasyev88f0b3a2013-09-24 23:52:08 -070028}
29
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070030-(void)awakeFromNib
31{
32 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070033 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070034 [statusItem setToolTip:@"NDN Control Center"];
35 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070036 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070037 //[statusItem setTarget:self];
38
39 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070040 NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070041 menuIcon = [[NSImage alloc] initWithContentsOfFile:path];
42 [statusItem setTitle:@""];
43 [statusItem setImage:menuIcon];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070044
Ilya Moiseenko01264322013-09-26 15:34:21 -070045
Ilya Moiseenko01264322013-09-26 15:34:21 -070046 [connectionStatus setView: connectionStatusView];
47 [connectionStatus setTarget:self];
48 [daemonStatus setView: daemonStatusView];
49 [daemonStatus setTarget:self];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070050}
51
52-(IBAction)switchDaemon:(id)sender
53{
54 if (daemonStarted)
55 {
56 daemonStarted = false;
57 [sender setTitle:@"Start"];
Ilya Moiseenko01264322013-09-26 15:34:21 -070058 [connectionStatusText setStringValue:@"Disconnected"];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070059
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070060 NSTask *task = [[NSTask alloc] init];
61 [task setLaunchPath: NDND_STOP_COMMAND];
62 [task launch];
63
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070064 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 Moiseenko01264322013-09-26 15:34:21 -070073 [connectionStatusText setStringValue:@"Connected"];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070074
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070075 NSTask *task = [[NSTask alloc] init];
76 [task setLaunchPath: NDND_START_COMMAND];
77 [task launch];
78
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070079 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 Moiseenko350b9b52013-09-25 16:38:41 -070084}
85
86-(IBAction)openDaemonStatus:(id)sender
87{
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070088}
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 Afanasyev88f0b3a2013-09-24 23:52:08 -0700103}
104
Ilya Moiseenko01264322013-09-26 15:34:21 -0700105-(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 Moiseenko01264322013-09-26 15:34:21 -0700113 }
114 else
115 {
116 [statusPopover performClose:nil];
117 }
118}
119
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -0700120-(IBAction)openNDNDPreferences:(id)sender
121{
Ilya Moiseenko90f40c92013-09-27 15:52:01 -0700122 [preferencesPanel setContentView:generalSettingsView];
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -0700123 [preferencesPanel makeKeyAndOrderFront:sender];
124 [preferencesPanel setLevel: NSStatusWindowLevel];
125}
Ilya Moiseenko90f40c92013-09-27 15:52:01 -0700126
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 Moiseenko6d4086c2013-09-27 16:56:02 -0700166-(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 Afanasyev88f0b3a2013-09-24 23:52:08 -0700187@end