Now using only one operationQueue and requesting status update after Add/Remove

Change-Id: I3980dfb97e61eadc405753724788d6aa888b540a
diff --git a/osx/menu-delegate.h b/osx/menu-delegate.h
index 03b371e..5e11476 100644
--- a/osx/menu-delegate.h
+++ b/osx/menu-delegate.h
@@ -50,7 +50,9 @@
 -(IBAction)openDaemonStatus:(id)sender;
 -(IBAction)showExitConfirmationWindow:(id)sender;
 
-- (void)statusUpdated:(NSXMLDocument*)document;
-- (void)statusUnavailable:(id)none;
+-(void)updateStatus;
+-(void)updateStatusWithDependency:(NSOperation*)dependency;
+-(void)statusUpdated:(NSXMLDocument*)document;
+-(void)statusUnavailable:(id)none;
 
 @end
diff --git a/osx/menu-delegate.mm b/osx/menu-delegate.mm
index 11cbfc9..687f29c 100644
--- a/osx/menu-delegate.mm
+++ b/osx/menu-delegate.mm
@@ -35,12 +35,11 @@
   m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]];
   
   NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
-                      target: self
-                      selector:@selector(onTick:)
-                      userInfo: nil repeats:YES];
-  [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes];
-
-  [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]];
+                target: self
+                selector:@selector(onTick:)
+                userInfo: nil
+                repeats:YES];
+  [self updateStatus];
 }
 
 -(void)awakeFromNib
@@ -79,9 +78,20 @@
 
   NSInteger res = [alert runModal];
   if (res == NSAlertFirstButtonReturn) {
+    // "YES" stop ndnd
     [m_operationQueue cancelAllOperations];
+
+    [m_operationQueue addOperationWithBlock:^{
+        NSTask *task = [[NSTask alloc] init];
+        [task setLaunchPath: @NDND_STOP_COMMAND];
+        [task launch];
+        [task waitUntilExit];
+      }];
+
+    [m_operationQueue waitUntilAllOperationsAreFinished];
     [NSApp terminate:self];
   } else if (res == NSAlertSecondButtonReturn) {
+    // "NO" terminate app but keep ndnd running
     [m_operationQueue cancelAllOperations];
     [NSApp terminate:self];
   }
@@ -105,7 +115,22 @@
 
 -(void)onTick:(NSTimer *)timer
 {
-  [m_operationQueue addOperation:[[NdndStatusOperation alloc] initWithDelegate:self]];
+  [self updateStatus];
+}
+
+-(void)updateStatus
+{
+  NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self];
+  [m_operationQueue addOperation:operation];
+}
+
+-(void)updateStatusWithDependency:(NSOperation*)dependency
+{
+  NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self];
+  [operation addDependency:dependency];
+
+  [m_operationQueue addOperation:dependency];
+  [m_operationQueue addOperation:operation];
 }
 
 - (void)statusUpdated:(NSXMLDocument*)document
diff --git a/osx/preference-delegate.h b/osx/preference-delegate.h
index 03022bd..9e34ac7 100644
--- a/osx/preference-delegate.h
+++ b/osx/preference-delegate.h
@@ -26,7 +26,6 @@
   IBOutlet NSTextField *endpointText;
   
   IBOutlet FibTableController *tableController;
-  NSOperationQueue *m_operationQueue;
 }
 
 @property BOOL allowSoftwareUpdates;
diff --git a/osx/preference-delegate.mm b/osx/preference-delegate.mm
index 113bf0b..f0c83c7 100644
--- a/osx/preference-delegate.mm
+++ b/osx/preference-delegate.mm
@@ -8,6 +8,7 @@
 
 #include "config.h"
 #import "preference-delegate.h"
+#import "menu-delegate.h"
 
 @implementation PreferenceDelegate
 
@@ -17,9 +18,6 @@
   [preferencesPanel makeKeyAndOrderFront:sender];
   [preferencesPanel setLevel: NSStatusWindowLevel];
   
-  if(m_operationQueue == nil)
-    m_operationQueue = [[NSOperationQueue alloc] init];
-  
   tableController.m_tableView = fibTableView;
 }
 
@@ -76,8 +74,8 @@
   NSString *prefixName = [namePrefixText stringValue];
   NSString *tunnelType = [tunnelCombobox itemObjectValueAtIndex:[tunnelCombobox indexOfSelectedItem]];
   NSString *endpoint = [endpointText stringValue];
-  
-  [m_operationQueue addOperationWithBlock:^{
+
+  NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
       NSTask *task = [[NSTask alloc] init];
       [task setLaunchPath: @NDND_FIB_COMMAND];
       [task setArguments: [NSArray arrayWithObjects: @"add", prefixName, tunnelType, endpoint, nil]];
@@ -85,6 +83,7 @@
       [task waitUntilExit];
     }];
 
+  [(MenuDelegate*)[[NSApplication sharedApplication] delegate] updateStatusWithDependency:operation];
 }
 
 -(IBAction)removeFibEntry:(id)sender
@@ -102,13 +101,15 @@
   if (prefix == nil)
     return;
 
-  [m_operationQueue addOperationWithBlock:^{
+  NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
       NSTask *task = [[NSTask alloc] init];
       [task setLaunchPath: @NDND_FIB_COMMAND];
       [task setArguments: [NSArray arrayWithObjects: @"del", prefix, @"face", faceID, nil]];
       [task launch];
       [task waitUntilExit];
     }];
+
+  [(MenuDelegate*)[[NSApplication sharedApplication] delegate] updateStatusWithDependency:operation];
 }
 
 - (IBAction) showFibEntrySheet:(id)sender