Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 2beff7f | 2013-09-27 17:50:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 10 | #import "menu-delegate.h" |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 11 | #import "ndnd-status-operation.h" |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 13 | @implementation MenuDelegate |
| 14 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 15 | -(id)init |
| 16 | { |
| 17 | if (![super init]) { |
| 18 | return nil; |
| 19 | } |
| 20 | |
| 21 | m_operationQueue = [[NSOperationQueue alloc] init]; |
| 22 | return self; |
| 23 | } |
| 24 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 25 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| 26 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 27 | m_daemonStarted = false; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 28 | allowSoftwareUpdates = true; |
| 29 | enableHubDiscovery = true; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 30 | |
| 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 Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 35 | |
| 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 Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 41 | |
| 42 | [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 45 | -(void)awakeFromNib |
| 46 | { |
| 47 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 48 | [statusItem setMenu:statusMenu]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 49 | [statusItem setToolTip:@"NDN Control Center"]; |
| 50 | [statusItem setEnabled:YES]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 51 | [statusItem setHighlightMode:YES]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 52 | //[statusItem setTarget:self]; |
| 53 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 54 | [statusItem setTitle:@""]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 55 | [statusItem setImage:m_disconnectedIcon]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 56 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 57 | [connectionStatus setView: connectionStatusView]; |
| 58 | [connectionStatus setTarget:self]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 59 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 60 | [daemonStatus setView: daemonStatusView]; |
| 61 | [daemonStatus setTarget:self]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 64 | -(IBAction)openDaemonStatus:(id)sender |
| 65 | { |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 68 | -(IBAction)showExitConfirmationWindow:(id)sender |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 69 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 70 | 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 Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 89 | -(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item |
| 90 | { |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 91 | if( ([item view]!=nil) && (item == daemonStatus) ) |
| 92 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 93 | NSView *view = [item view]; |
| 94 | |
| 95 | [statusPopover showRelativeToRect:[view bounds] |
| 96 | ofView:view |
| 97 | preferredEdge:NSMinXEdge]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
| 101 | [statusPopover performClose:nil]; |
| 102 | } |
| 103 | } |
| 104 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 105 | -(void)onTick:(NSTimer *)timer |
| 106 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 107 | [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 Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 116 | [statusItem setImage:m_connectedIcon]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 117 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 118 | |
| 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 Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 127 | - (void)statusUnavailable:(id)none |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame^] | 129 | // 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 Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 145 | @end |