blob: 4eff7f925b6b27fb3499fe73bef260805eba97ec [file] [log] [blame]
Alexander Afanasyevb6392e32014-05-12 23:43:50 -07001/* -*- Mode: objc; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California,
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -07004 *
Alexander Afanasyevb6392e32014-05-12 23:43:50 -07005 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * \author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
20 * \author Ilya Moiseenko <http://ilyamoiseenko.com/>
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070021 */
22
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070023#include "config.hpp"
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070024#import "menu-delegate.h"
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070025#import "ndnd-status-operation.h"
Ilya Moiseenko0c9ab7c2013-10-29 15:08:23 -070026#import "tight-menu-item-view.h"
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070027
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070028@implementation MenuDelegate
29
Alexander Afanasyev506d0ab2013-10-29 19:18:10 -070030@synthesize interestSent;
31@synthesize interestRecv;
32@synthesize dataSent;
33@synthesize dataRecv;
34
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070035-(id)init
36{
37 if (![super init]) {
38 return nil;
39 }
40
Alexander Afanasyev506d0ab2013-10-29 19:18:10 -070041 interestSent = @"N/A";
42 interestRecv = @"N/A";
43 dataSent = @"N/A";
44 dataRecv = @"N/A";
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070045
Alexander Afanasyevb9024d12013-10-14 18:10:18 +030046 m_autoconfInProgress = false;
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070047 // m_operationQueue = [[NSOperationQueue alloc] init];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070048 return self;
49}
50
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070051- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
52{
Alexander Afanasyev1d794ce2013-10-22 12:48:30 -070053 // Register the preference defaults early.
54 NSDictionary *appDefaults =
55 [NSDictionary dictionaryWithObjectsAndKeys:
56 [NSNumber numberWithBool:YES], @"allowSoftwareUpdates",
57 [NSNumber numberWithBool:YES], @"enableHubDiscovery",
58 [NSNumber numberWithBool:NO], @"shutdownNdndOnExit",
59 nil
60 ];
61 [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070062
Alexander Afanasyev1d794ce2013-10-22 12:48:30 -070063 // Other initialization...
64
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070065 m_daemonStarted = false;
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070066
67 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
68 m_connectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatConnected" ofType:@"png"]];
69 m_disconnectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatDisconnected" ofType:@"png"]];
Alexander Afanasyev17922872013-10-15 11:45:58 +030070
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070071 [statusItem setImage:m_disconnectedIcon];
72
73 // m_statusXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status" ofType:@"xslt"]];
74 // m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]];
75
76 // [NSTimer scheduledTimerWithTimeInterval: 1.0
77 // target: self
78 // selector:@selector(onTick:)
79 // userInfo: nil
80 // repeats:YES];
81 // [self updateStatus];
82
83 // m_systemEvents = [[SystemEvents alloc] init];
Alexander Afanasyev17922872013-10-15 11:45:58 +030084}
85
86- (void)applicationWillTerminate:(NSNotification *)aNotification
87{
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070088 // [m_systemEvents disable];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070089}
90
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070091-(void)awakeFromNib
92{
93 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070094 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070095 [statusItem setToolTip:@"NDN Control Center"];
96 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070097 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070098 //[statusItem setTarget:self];
99
Ilya Moiseenko350b9b52013-09-25 16:38:41 -0700100 [statusItem setTitle:@""];
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700101
Ilya Moiseenko0c9ab7c2013-10-29 15:08:23 -0700102 float menuItemHeight = 20;
103
104 NSRect viewRect = NSMakeRect(0, 0, /* width autoresizes */ 1, menuItemHeight);
105 connectionStatusView = [[TightMenuItemView alloc] initWithFrame:viewRect];
106 connectionStatusView.autoresizingMask = NSViewWidthSizable;
107
108 [connectionStatus setView:connectionStatusView];
Ilya Moiseenko01264322013-09-26 15:34:21 -0700109 [connectionStatus setTarget:self];
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700110
Ilya Moiseenko01264322013-09-26 15:34:21 -0700111 [daemonStatus setView: daemonStatusView];
112 [daemonStatus setTarget:self];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -0700113}
114
Ilya Moiseenko350b9b52013-09-25 16:38:41 -0700115-(IBAction)openDaemonStatus:(id)sender
116{
Ilya Moiseenko350b9b52013-09-25 16:38:41 -0700117}
118
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700119-(IBAction)showExitConfirmationWindow:(id)sender
Ilya Moiseenko350b9b52013-09-25 16:38:41 -0700120{
Alexander Afanasyev1d794ce2013-10-22 12:48:30 -0700121 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shutdownNdndOnExit"]) {
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700122 [m_operationQueue cancelAllOperations];
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +0200123
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700124 // [m_operationQueue addOperationWithBlock:^{
125 // NSTask *task = [[NSTask alloc] init];
126 // [task setLaunchPath: @NDND_STOP_COMMAND];
127 // [task launch];
128 // [task waitUntilExit];
129 // }];
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +0200130
131 [m_operationQueue waitUntilAllOperationsAreFinished];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700132 [NSApp terminate:self];
Alexander Afanasyev1d794ce2013-10-22 12:48:30 -0700133 }
134 else {
135 NSAlert *alert = [[NSAlert alloc] init];
136 [alert addButtonWithTitle:@"Yes"];
137 [alert addButtonWithTitle:@"No"];
138 [alert addButtonWithTitle:@"Cancel"];
139 [alert setMessageText:@"Shutdown NDN daemon as well?"];
140 [alert setInformativeText:@"All NDN operations will be become unavailable."];
141 [alert setAlertStyle:NSCriticalAlertStyle];
142 // [alert setShowsSuppressionButton: YES];
143
144 NSInteger res = [alert runModal];
145 if (res == NSAlertFirstButtonReturn) {
146 // "YES" stop ndnd
147 [m_operationQueue cancelAllOperations];
148
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700149 // [m_operationQueue addOperationWithBlock:^{
150 // NSTask *task = [[NSTask alloc] init];
151 // [task setLaunchPath: @NDND_STOP_COMMAND];
152 // [task launch];
153 // [task waitUntilExit];
154 // }];
Alexander Afanasyev1d794ce2013-10-22 12:48:30 -0700155
156 [m_operationQueue waitUntilAllOperationsAreFinished];
157 [NSApp terminate:self];
158 } else if (res == NSAlertSecondButtonReturn) {
159 // "NO" terminate app but keep ndnd running
160 [m_operationQueue cancelAllOperations];
161 [NSApp terminate:self];
162 }
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700163 }
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700164}
165
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700166-(void)onTick:(NSTimer *)timer
167{
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +0200168 [self updateStatus];
169}
170
171-(void)updateStatus
172{
173 NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self];
174 [m_operationQueue addOperation:operation];
175}
176
177-(void)updateStatusWithDependency:(NSOperation*)dependency
178{
179 NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self];
180 [operation addDependency:dependency];
181
182 [m_operationQueue addOperation:dependency];
183 [m_operationQueue addOperation:operation];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700184}
185
186- (void)statusUpdated:(NSXMLDocument*)document
187{
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700188 // if (!m_daemonStarted) {
189 // m_daemonStarted = true;
190 // [connectionStatusView setStatus:@"Active"];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700191
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700192 // [statusItem setImage:m_connectedIcon];
193 // }
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700194
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700195 // NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt
196 // arguments:nil
197 // error:nil];
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700198
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700199 // NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt
200 // arguments:nil
201 // error:nil];
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300202
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700203 // NSXMLNode *element = [[statusXml rootElement] childAtIndex:0]; //data
204 // [self setDataRecv:[[element childAtIndex:0] stringValue]];
205 // [self setDataSent:[[element childAtIndex:1] stringValue]];
206
207 // element = [[statusXml rootElement] childAtIndex:1]; //interests
208 // [self setInterestRecv:[[element childAtIndex:0] stringValue]];
209 // [self setInterestSent:[[element childAtIndex:1] stringValue]];
210
211 // [preferencesDelegate updateFibStatus:statusFibXml];
212
213 // if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) {
214 // NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil];
215 // if ([autoconf count] == 0)
216 // {
217 // [self restartDaemon:nil];
218 // }
219 // }
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700220}
221
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700222- (void)statusUnavailable:(id)none
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700223{
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700224 // // try start ndnd if it is not started yet
225 // if (m_daemonStarted) {
226 // m_daemonStarted = false;
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700227
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700228 // [connectionStatusView setStatus:@"Starting..."];
Alexander Afanasyev506d0ab2013-10-29 19:18:10 -0700229
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700230 // [statusItem setImage:m_disconnectedIcon];
231 // }
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700232
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700233 // [self setInterestSent:@"N/A"];
234 // [self setInterestRecv:@"N/A"];
235 // [self setDataSent:@"N/A"];
236 // [self setDataRecv:@"N/A"];
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300237
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700238 // [preferencesDelegate updateFibStatus:nil];
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300239
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700240 // m_autoconfInProgress = true;
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300241
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700242 // // NSOperation *startOp = [NSBlockOperation blockOperationWithBlock:^{
243 // // NSTask *task = [[NSTask alloc] init];
244 // // [task setLaunchPath: @NDND_START_COMMAND];
245 // // [task launch];
246 // // }];
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300247
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700248 // // if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) {
249 // // NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{
250 // // NSTask *task = [[NSTask alloc] init];
251 // // [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND];
252 // // [task launch];
253 // // [task waitUntilExit];
254
255 // // m_autoconfInProgress = false;
256 // // }];
257
258 // // [autoconfOp addDependency:startOp];
259 // // [m_operationQueue addOperation:autoconfOp];
260 // // }
261
262 // // [m_operationQueue addOperation:startOp];
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300263}
264
265-(void)restartDaemon:(id)none
266{
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700267 // if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"])
268 // return;
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300269
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700270 // if (m_autoconfInProgress)
271 // return;
Alexander Afanasyevb9024d12013-10-14 18:10:18 +0300272
Alexander Afanasyevb6392e32014-05-12 23:43:50 -0700273 // NSLog(@"No automatically detected route configured, trying to get one");
274
275 // // m_autoconfInProgress = true;
276 // // [m_operationQueue addOperationWithBlock:^{
277 // // NSTask *task = [[NSTask alloc] init];
278 // // [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND];
279 // // [task launch];
280 // // [task waitUntilExit];
281
282 // // m_autoconfInProgress = false;
283 // // }];
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700284}
285
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700286@end