blob: 11cbfc93ed8a79478a13c92ee2d836279efb9402 [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
38 target: self
39 selector:@selector(onTick:)
40 userInfo: nil repeats:YES];
41 [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070042
43 [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070044}
45
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070046-(void)awakeFromNib
47{
48 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070049 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070050 [statusItem setToolTip:@"NDN Control Center"];
51 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070052 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070053 //[statusItem setTarget:self];
54
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070055 [statusItem setTitle:@""];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070056 [statusItem setImage:m_disconnectedIcon];
Ilya Moiseenko01264322013-09-26 15:34:21 -070057
Ilya Moiseenko01264322013-09-26 15:34:21 -070058 [connectionStatus setView: connectionStatusView];
59 [connectionStatus setTarget:self];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070060
Ilya Moiseenko01264322013-09-26 15:34:21 -070061 [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{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070071 NSAlert *alert = [[NSAlert alloc] init];
72 [alert addButtonWithTitle:@"Yes"];
73 [alert addButtonWithTitle:@"No"];
74 [alert addButtonWithTitle:@"Cancel"];
75 [alert setMessageText:@"Shutdown NDN daemon as well?"];
76 [alert setInformativeText:@"All NDN operations will be become unavailable."];
77 [alert setAlertStyle:NSCriticalAlertStyle];
78 [alert setShowsSuppressionButton: YES];
79
80 NSInteger res = [alert runModal];
81 if (res == NSAlertFirstButtonReturn) {
82 [m_operationQueue cancelAllOperations];
83 [NSApp terminate:self];
84 } else if (res == NSAlertSecondButtonReturn) {
85 [m_operationQueue cancelAllOperations];
86 [NSApp terminate:self];
87 }
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070088}
89
Ilya Moiseenko01264322013-09-26 15:34:21 -070090-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
91{
Ilya Moiseenko01264322013-09-26 15:34:21 -070092 if( ([item view]!=nil) && (item == daemonStatus) )
93 {
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070094 NSView *view = [item view];
95
96 [statusPopover showRelativeToRect:[view bounds]
97 ofView:view
98 preferredEdge:NSMinXEdge];
Ilya Moiseenko01264322013-09-26 15:34:21 -070099 }
100 else
101 {
102 [statusPopover performClose:nil];
103 }
104}
105
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700106-(void)onTick:(NSTimer *)timer
107{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700108 [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]];
109}
110
111- (void)statusUpdated:(NSXMLDocument*)document
112{
113 if (!m_daemonStarted) {
114 m_daemonStarted = true;
115 [connectionStatusText setStringValue:@"Active"];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700116
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700117 [statusItem setImage:m_connectedIcon];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700118 }
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700119
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700120 NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt
121 arguments:nil
122 error:nil];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700123
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700124 NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt
125 arguments:nil
126 error:nil];
127
128 m_statusString = [[NSAttributedString alloc]initWithHTML:[statusXml XMLData] documentAttributes:NULL];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700129 [daemonStatusHtml setAttributedStringValue:m_statusString];
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700130
131 [preferencesDelegate updateFibStatus:statusFibXml];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700132}
133
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700134- (void)statusUnavailable:(id)none
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700135{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700136 // try start ndnd if it is not started yet
137 if (m_daemonStarted) {
138 m_daemonStarted = false;
139
140 [connectionStatusText setStringValue:@"Starting..."];
141
142 [statusItem setImage:m_disconnectedIcon];
143 }
Ilya Moiseenkob4aca052013-10-06 15:10:19 -0700144
145 [daemonStatusHtml setStringValue:@""];
146 [preferencesDelegate updateFibStatus:nil];
147
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700148 [m_operationQueue addOperationWithBlock:^{
149 NSTask *task = [[NSTask alloc] init];
150 [task setLaunchPath: @NDND_START_COMMAND];
151 [task launch];
152 }];
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700153}
154
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700155@end