Making use of preferences

Preferences now saved and used in the application logic (autoupdate is
not yet working though)

Change-Id: I8b3540f8a49c6a151d5cc54d9a49005e0a3c98d7
diff --git a/osx/MainMenu.xib b/osx/MainMenu.xib
index 5f23e85..62d73fc 100644
--- a/osx/MainMenu.xib
+++ b/osx/MainMenu.xib
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="4510" systemVersion="12F37" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="4510" systemVersion="12F45" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
     <dependencies>
         <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="4510"/>
     </dependencies>
@@ -219,7 +219,7 @@
                                     <font key="font" metaFont="system"/>
                                 </buttonCell>
                                 <connections>
-                                    <action selector="switchSoftwareUpdates:" target="0yn-TQ-4SC" id="Gh4-nv-8N8"/>
+                                    <binding destination="y0R-NA-1ih" name="value" keyPath="values.allowSoftwareUpdates" id="fG5-UG-ePy"/>
                                 </connections>
                             </button>
                             <button translatesAutoresizingMaskIntoConstraints="NO" id="ghB-he-yYe">
@@ -232,6 +232,9 @@
                                     <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
                                     <font key="font" metaFont="system"/>
                                 </buttonCell>
+                                <connections>
+                                    <binding destination="y0R-NA-1ih" name="value" keyPath="values.shutdownNdndOnExit" id="yBf-x9-4BT"/>
+                                </connections>
                             </button>
                             <button translatesAutoresizingMaskIntoConstraints="NO" id="pK9-GZ-bQL">
                                 <rect key="frame" x="16" y="53" width="307" height="18"/>
@@ -244,7 +247,7 @@
                                     <font key="font" metaFont="system"/>
                                 </buttonCell>
                                 <connections>
-                                    <action selector="switchHubDiscovery:" target="0yn-TQ-4SC" id="by5-Mt-5bD"/>
+                                    <binding destination="y0R-NA-1ih" name="value" keyPath="values.enableHubDiscovery" id="Tep-jT-r7h"/>
                                 </connections>
                             </button>
                         </subviews>
diff --git a/osx/menu-delegate.h b/osx/menu-delegate.h
index 3b2260c..d279fe2 100644
--- a/osx/menu-delegate.h
+++ b/osx/menu-delegate.h
@@ -32,8 +32,6 @@
   IBOutlet NSMenuItem *ndnPreferences;
   
   BOOL m_daemonStarted;
-  BOOL allowSoftwareUpdates;
-  BOOL enableHubDiscovery;
   
   IBOutlet NSView *daemonStatusView;
   IBOutlet NSTextField *daemonStatusHtml;
diff --git a/osx/menu-delegate.mm b/osx/menu-delegate.mm
index e92c82b..34ef2d0 100644
--- a/osx/menu-delegate.mm
+++ b/osx/menu-delegate.mm
@@ -25,9 +25,19 @@
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
 {
+  // Register the preference defaults early.
+  NSDictionary *appDefaults =
+    [NSDictionary dictionaryWithObjectsAndKeys:
+                      [NSNumber numberWithBool:YES], @"allowSoftwareUpdates",
+                      [NSNumber numberWithBool:YES], @"enableHubDiscovery",
+                      [NSNumber numberWithBool:NO],  @"shutdownNdndOnExit",
+                  nil
+     ];
+  [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
+ 
+   // Other initialization...
+
   m_daemonStarted = false; 
-  allowSoftwareUpdates = true;
-  enableHubDiscovery = true;
 
   NSBundle *bundle = [NSBundle bundleForClass:[self class]];
   m_connectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatConnected" ofType:@"png"]];
@@ -75,18 +85,7 @@
 
 -(IBAction)showExitConfirmationWindow:(id)sender
 {
-  NSAlert *alert = [[NSAlert alloc] init];
-  [alert addButtonWithTitle:@"Yes"];
-  [alert addButtonWithTitle:@"No"];
-  [alert addButtonWithTitle:@"Cancel"];
-  [alert setMessageText:@"Shutdown NDN daemon as well?"];
-  [alert setInformativeText:@"All NDN operations will be become unavailable."];
-  [alert setAlertStyle:NSCriticalAlertStyle];
-  [alert setShowsSuppressionButton: YES];
-
-  NSInteger res = [alert runModal];
-  if (res == NSAlertFirstButtonReturn) {
-    // "YES" stop ndnd
+  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shutdownNdndOnExit"]) {
     [m_operationQueue cancelAllOperations];
 
     [m_operationQueue addOperationWithBlock:^{
@@ -98,10 +97,36 @@
 
     [m_operationQueue waitUntilAllOperationsAreFinished];
     [NSApp terminate:self];
-  } else if (res == NSAlertSecondButtonReturn) {
-    // "NO" terminate app but keep ndnd running
-    [m_operationQueue cancelAllOperations];
-    [NSApp terminate:self];
+  }
+  else {
+    NSAlert *alert = [[NSAlert alloc] init];
+    [alert addButtonWithTitle:@"Yes"];
+    [alert addButtonWithTitle:@"No"];
+    [alert addButtonWithTitle:@"Cancel"];
+    [alert setMessageText:@"Shutdown NDN daemon as well?"];
+    [alert setInformativeText:@"All NDN operations will be become unavailable."];
+    [alert setAlertStyle:NSCriticalAlertStyle];
+    // [alert setShowsSuppressionButton: YES];
+
+    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];
+    }
   }
 }
 
@@ -163,11 +188,13 @@
 
   [preferencesDelegate updateFibStatus:statusFibXml];
 
-  NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil];
-  if ([autoconf count] == 0)
-    {
-      [self restartDaemon:nil];
-    }
+  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { 
+    NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil];
+    if ([autoconf count] == 0)
+      {
+        [self restartDaemon:nil];
+      }
+  }
 }
 
 - (void)statusUnavailable:(id)none
@@ -192,23 +219,28 @@
       [task launch];
     }];
 
-  NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{
-      NSTask *task = [[NSTask alloc] init];
-      [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND];
-      [task launch];
-      [task waitUntilExit];
+  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) {
+    NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{
+        NSTask *task = [[NSTask alloc] init];
+        [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND];
+        [task launch];
+        [task waitUntilExit];
 
-      m_autoconfInProgress = false;
-    }];
+        m_autoconfInProgress = false;
+      }];
 
-  [autoconfOp addDependency:startOp];
+    [autoconfOp addDependency:startOp];
+    [m_operationQueue addOperation:autoconfOp];
+  }
 
   [m_operationQueue addOperation:startOp];
-  [m_operationQueue addOperation:autoconfOp];
 }
 
 -(void)restartDaemon:(id)none
 {
+  if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"])
+    return;
+    
   if (m_autoconfInProgress)
     return;
 
diff --git a/osx/preference-delegate.h b/osx/preference-delegate.h
index 9e34ac7..b711155 100644
--- a/osx/preference-delegate.h
+++ b/osx/preference-delegate.h
@@ -12,8 +12,12 @@
 @interface PreferenceDelegate : NSObject
 {
   IBOutlet NSWindow *preferencesPanel;
-  IBOutlet NSView *generalSettingsView;
   
+  IBOutlet NSView *generalSettingsView;
+  IBOutlet NSButton *checkAllowSoftwareUpdates;
+  IBOutlet NSButton *checkEnableHubDiscovery;
+  IBOutlet NSButton *checkShutdownNdndOnExit;
+
   IBOutlet NSView *forwardingSettingsView;
   IBOutlet NSTableView *fibTableView;
   
@@ -28,17 +32,11 @@
   IBOutlet FibTableController *tableController;
 }
 
-@property BOOL allowSoftwareUpdates;
-@property BOOL enableHubDiscovery;
-
 -(IBAction)showPreferencesPanel:(id)sender;
 -(IBAction)openGeneralSettings:(id)sender;
 -(IBAction)openForwardingSettings:(id)sender;
 -(IBAction)openSecuritySettings:(id)sender;
 
--(IBAction)switchSoftwareUpdates:(id)sender;
--(IBAction)switchHubDiscovery:(id)sender;
-
 -(IBAction)addFibEntry:(id)sender;
 -(IBAction)removeFibEntry:(id)sender;
 -(IBAction)showFibEntrySheet:(id)sender;
diff --git a/osx/preference-delegate.mm b/osx/preference-delegate.mm
index f0c83c7..b6a6686 100644
--- a/osx/preference-delegate.mm
+++ b/osx/preference-delegate.mm
@@ -36,30 +36,6 @@
   [preferencesPanel setContentView:securitySettingsView];
 }
 
--(IBAction)switchSoftwareUpdates:(id)sender
-{
-  if ([(NSButton*)sender state] == NSOnState)
-  {
-    _allowSoftwareUpdates = true;
-  }
-  else
-  {
-    _allowSoftwareUpdates = false;
-  }
-}
-
--(IBAction)switchHubDiscovery:(id)sender
-{
-  if ([(NSButton*)sender state] == NSOnState)
-  {
-    _enableHubDiscovery = true;
-  }
-  else
-  {
-    _enableHubDiscovery = false;
-  }
-}
-
 -(void)updateFibStatus:(NSXMLDocument*)status;
 {
   [tableController loadStatus:status];