blob: 687f29c2dbe59e150402409fc34554ce0f0d404b [file] [log] [blame]
Alexander Afanasyev14b09482013-10-11 18:24:45 +02001/* -*- Mode: obj; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -07002/*
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"
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070011#import "ndnd-status-operation.h"
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070012
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070013@implementation MenuDelegate
14
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070015-(id)init
16{
17 if (![super init]) {
18 return nil;
19 }
20
21 m_operationQueue = [[NSOperationQueue alloc] init];
22 return self;
23}
24
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070025- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
26{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070027 m_daemonStarted = false;
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -070028 allowSoftwareUpdates = true;
29 enableHubDiscovery = true;
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070030
31 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
32 m_connectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatConnected" ofType:@"png"]];
33 m_disconnectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatDisconnected" ofType:@"png"]];
34 m_statusXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status" ofType:@"xslt"]];
Ilya Moiseenkob4aca052013-10-06 15:10:19 -070035 m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -070036
37 NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +020038 target: self
39 selector:@selector(onTick:)
40 userInfo: nil
41 repeats:YES];
42 [self updateStatus];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070043}
44
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070045-(void)awakeFromNib
46{
47 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070048 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070049 [statusItem setToolTip:@"NDN Control Center"];
50 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070051 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070052 //[statusItem setTarget:self];
53
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070054 [statusItem setTitle:@""];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070055 [statusItem setImage:m_disconnectedIcon];
Ilya Moiseenko01264322013-09-26 15:34:21 -070056
Ilya Moiseenko01264322013-09-26 15:34:21 -070057 [connectionStatus setView: connectionStatusView];
58 [connectionStatus setTarget:self];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070059
Ilya Moiseenko01264322013-09-26 15:34:21 -070060 [daemonStatus setView: daemonStatusView];
61 [daemonStatus setTarget:self];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070062}
63
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070064-(IBAction)openDaemonStatus:(id)sender
65{
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070066}
67
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -070068-(IBAction)showExitConfirmationWindow:(id)sender
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070069{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070070 NSAlert *alert = [[NSAlert alloc] init];
71 [alert addButtonWithTitle:@"Yes"];
72 [alert addButtonWithTitle:@"No"];
73 [alert addButtonWithTitle:@"Cancel"];
74 [alert setMessageText:@"Shutdown NDN daemon as well?"];
75 [alert setInformativeText:@"All NDN operations will be become unavailable."];
76 [alert setAlertStyle:NSCriticalAlertStyle];
77 [alert setShowsSuppressionButton: YES];
78
79 NSInteger res = [alert runModal];
80 if (res == NSAlertFirstButtonReturn) {
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +020081 // "YES" stop ndnd
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070082 [m_operationQueue cancelAllOperations];
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +020083
84 [m_operationQueue addOperationWithBlock:^{
85 NSTask *task = [[NSTask alloc] init];
86 [task setLaunchPath: @NDND_STOP_COMMAND];
87 [task launch];
88 [task waitUntilExit];
89 }];
90
91 [m_operationQueue waitUntilAllOperationsAreFinished];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070092 [NSApp terminate:self];
93 } else if (res == NSAlertSecondButtonReturn) {
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +020094 // "NO" terminate app but keep ndnd running
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070095 [m_operationQueue cancelAllOperations];
96 [NSApp terminate:self];
97 }
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070098}
99
Ilya Moiseenko01264322013-09-26 15:34:21 -0700100-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
101{
Ilya Moiseenko01264322013-09-26 15:34:21 -0700102 if( ([item view]!=nil) && (item == daemonStatus) )
103 {
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700104 NSView *view = [item view];
105
106 [statusPopover showRelativeToRect:[view bounds]
107 ofView:view
108 preferredEdge:NSMinXEdge];
Ilya Moiseenko01264322013-09-26 15:34:21 -0700109 }
110 else
111 {
112 [statusPopover performClose:nil];
113 }
114}
115
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700116-(void)onTick:(NSTimer *)timer
117{
Alexander Afanasyev4e1c7e92013-10-11 18:25:09 +0200118 [self updateStatus];
119}
120
121-(void)updateStatus
122{
123 NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self];
124 [m_operationQueue addOperation:operation];
125}
126
127-(void)updateStatusWithDependency:(NSOperation*)dependency
128{
129 NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self];
130 [operation addDependency:dependency];
131
132 [m_operationQueue addOperation:dependency];
133 [m_operationQueue addOperation:operation];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700134}
135
136- (void)statusUpdated:(NSXMLDocument*)document
137{
138 if (!m_daemonStarted) {
139 m_daemonStarted = true;
140 [connectionStatusText setStringValue:@"Active"];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700141
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700142 [statusItem setImage:m_connectedIcon];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700143 }
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700144
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700145 NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt
146 arguments:nil
147 error:nil];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700148
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700149 NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt
150 arguments:nil
151 error:nil];
152
153 m_statusString = [[NSAttributedString alloc]initWithHTML:[statusXml XMLData] documentAttributes:NULL];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700154 [daemonStatusHtml setAttributedStringValue:m_statusString];
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700155
156 [preferencesDelegate updateFibStatus:statusFibXml];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700157}
158
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700159- (void)statusUnavailable:(id)none
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700160{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700161 // try start ndnd if it is not started yet
162 if (m_daemonStarted) {
163 m_daemonStarted = false;
164
165 [connectionStatusText setStringValue:@"Starting..."];
166
167 [statusItem setImage:m_disconnectedIcon];
168 }
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700169
170 [daemonStatusHtml setStringValue:@""];
171 [preferencesDelegate updateFibStatus:nil];
172
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700173 [m_operationQueue addOperationWithBlock:^{
174 NSTask *task = [[NSTask alloc] init];
175 [task setLaunchPath: @NDND_START_COMMAND];
176 [task launch];
177 }];
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700178}
179
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700180@end