blob: 24102fd9c35995e0ed4800834f5c8d65d19997c1 [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"
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 Moiseenko6d4086c2013-09-27 16:56:02 -070035
36 NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
37 target: self
38 selector:@selector(onTick:)
39 userInfo: nil repeats:YES];
40 [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes];
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070041
42 [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]];
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) {
81 [m_operationQueue cancelAllOperations];
82 [NSApp terminate:self];
83 } else if (res == NSAlertSecondButtonReturn) {
84 [m_operationQueue cancelAllOperations];
85 [NSApp terminate:self];
86 }
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070087}
88
Ilya Moiseenko01264322013-09-26 15:34:21 -070089-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
90{
Ilya Moiseenko01264322013-09-26 15:34:21 -070091 if( ([item view]!=nil) && (item == daemonStatus) )
92 {
Alexander Afanasyev6cacb972013-10-03 23:39:06 -070093 NSView *view = [item view];
94
95 [statusPopover showRelativeToRect:[view bounds]
96 ofView:view
97 preferredEdge:NSMinXEdge];
Ilya Moiseenko01264322013-09-26 15:34:21 -070098 }
99 else
100 {
101 [statusPopover performClose:nil];
102 }
103}
104
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700105-(void)onTick:(NSTimer *)timer
106{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700107 [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]];
108}
109
110- (void)statusUpdated:(NSXMLDocument*)document
111{
112 if (!m_daemonStarted) {
113 m_daemonStarted = true;
114 [connectionStatusText setStringValue:@"Active"];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700115
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700116 [statusItem setImage:m_connectedIcon];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700117 }
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700118
119 NSXMLDocument *result = [document objectByApplyingXSLT:m_statusXslt
120 arguments:nil
121 error:nil];
122
123 m_statusString = [[NSAttributedString alloc]initWithHTML:[result XMLData] documentAttributes:NULL];
124 [daemonStatusHtml setAttributedStringValue:m_statusString];
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700125}
126
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700127- (void)statusUnavailable:(id)none
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700128{
Alexander Afanasyev6cacb972013-10-03 23:39:06 -0700129 // try start ndnd if it is not started yet
130 if (m_daemonStarted) {
131 m_daemonStarted = false;
132
133 [connectionStatusText setStringValue:@"Starting..."];
134
135 [statusItem setImage:m_disconnectedIcon];
136 }
137
138 [m_operationQueue addOperationWithBlock:^{
139 NSTask *task = [[NSTask alloc] init];
140 [task setLaunchPath: @NDND_START_COMMAND];
141 [task launch];
142 }];
Ilya Moiseenkoe7058e72013-10-02 15:56:45 -0700143}
144
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700145@end