private void PreferencesSelectedSubnetChanged(object sender, EventArgs e)
        {
            // The app can auto-select the current subnet - this is to notify the preference pane
            NSUserDefaults.StandardUserDefaults.SetInt((int)iModel.Preferences.SelectedSubnetAddress, kPreferenceSelectedSubnetAddress);
            NSUserDefaults.StandardUserDefaults.Synchronize();

            // notify the preference pane
            NSDistributedNotificationCenter centre = NSDistributedNotificationCenter.DefaultCenter as NSDistributedNotificationCenter;

            centre.PostNotificationName("PreferenceSelectedSubnetChanged", iAppId, null, true);
        }
        private void PreferencesSelectedReceiverChanged(object sender, EventArgs e)
        {
            // The app can auto-select the current receiver - this is to notify the preference pane
            NSUserDefaults.StandardUserDefaults.SetString(iModel.Preferences.SelectedReceiverUdn, kPreferenceSelectedReceiverUdn);
            NSUserDefaults.StandardUserDefaults.Synchronize();

            // notify the preference pane
            NSDistributedNotificationCenter centre = NSDistributedNotificationCenter.DefaultCenter as NSDistributedNotificationCenter;

            centre.PostNotificationName("PreferenceSelectedReceiverChanged", iAppId, null, true);
        }
        private void ModelSubnetListChanged(object sender, EventArgs e)
        {
            // This is not a true preference but the current value needs to be communicated to the preference pane
            NSUserDefaults.StandardUserDefaults[kPreferenceSubnetList] = BuildSubnetList();
            NSUserDefaults.StandardUserDefaults.Synchronize();

            // notify the preference pane
            NSDistributedNotificationCenter centre = NSDistributedNotificationCenter.DefaultCenter as NSDistributedNotificationCenter;

            centre.PostNotificationName("PreferenceSubnetListChanged", iAppId, null, true);
        }
        private void OptionPageUpdatesChanged(object sender, EventArgs e)
        {
            // set the new values of the preferences
            NSUserDefaults.StandardUserDefaults.SetBool(iHelperAutoUpdate.OptionPageUpdates.AutoUpdate, kPreferenceAutoUpdatesEnabled);
            NSUserDefaults.StandardUserDefaults.Synchronize();

            // notify the preference pane
            NSDistributedNotificationCenter centre = NSDistributedNotificationCenter.DefaultCenter as NSDistributedNotificationCenter;

            centre.PostNotificationName("PreferenceAutoUpdatesEnabledChanged", iAppId, null, true);
        }
        private void ModelReceiverListChanged(object sender, EventArgs e)
        {
            // This contains more info than the receiver list that is stored in the preferences - it also contains the
            // current status of each receiver
            NSUserDefaults.StandardUserDefaults[kPreferenceReceiverList] = BuildReceiverList();
            NSUserDefaults.StandardUserDefaults.Synchronize();

            // notify the preference pane
            NSDistributedNotificationCenter centre = NSDistributedNotificationCenter.DefaultCenter as NSDistributedNotificationCenter;

            centre.PostNotificationName("PreferenceReceiverListChanged", iAppId, null, true);
        }
        public ViewPreferences(IInvoker aInvoker, Model aModel, HelperAutoUpdate aHelperAutoUpdate)
        {
            iInvoker          = aInvoker;
            iAppId            = "uk.co.linn.songcast.app";
            iModel            = aModel;
            iHelperAutoUpdate = aHelperAutoUpdate;

            // register the preference keys and default values
            object[] keys = new object[] { kPreferenceEnabled,
                                           kPreferenceReceiverList,
                                           kPreferenceSubnetList,
                                           kPreferenceSelectedReceiverUdn,
                                           kPreferenceSelectedSubnetAddress,
                                           kPreferenceMulticastEnabled,
                                           kPreferenceMulticastChannel,
                                           kPreferenceMusicLatencyMs,
                                           kPreferenceVideoLatencyMs,
                                           kPreferenceRotaryVolumeControl,
                                           kPreferenceAutoUpdatesEnabled,
                                           kPreferenceBetaUpdatesEnabled,
                                           kPreferenceUsageDataEnabled };
            object[] vals = new object[] { false,
                                           new NSArray(),
                                           new NSArray(),
                                           string.Empty,
                                           0,
                                           false,
                                           0,
                                           iModel.Preferences.DefaultMusicLatencyMs,
                                           iModel.Preferences.DefaultVideoLatencyMs,
                                           true,
                                           true,
                                           false,
                                           true };

            NSUserDefaults.StandardUserDefaults.RegisterDefaults(NSDictionary.FromObjectsAndKeys(vals, keys));

            // check the multicast channel - if it is currently the default (0), set it to a random value - this ensures that this
            // preference is always set to a valid value
            if (GetIntegerValue(kPreferenceMulticastChannel) == 0)
            {
                Random r       = new Random();
                int    byte1   = r.Next(254) + 1; // in range [1,254]
                int    byte2   = r.Next(254) + 1; // in range [1,254]
                int    channel = byte1 << 8 | byte2;
                NSUserDefaults.StandardUserDefaults.SetInt(channel, kPreferenceMulticastChannel);
            }

            // add observers for the distributed notifications from the preferences pane app
            NSDistributedNotificationCenter centre = NSDistributedNotificationCenter.DefaultCenter as NSDistributedNotificationCenter;
            NSString prefAppId = new NSString("uk.co.linn.songcast.prefs");

            centre.AddObserver(this, new Selector("selectedReceiverChanged:"), new NSString("PreferenceSelectedReceiverChanged"), prefAppId);
            centre.AddObserver(this, new Selector("selectedSubnetChanged:"), new NSString("PreferenceSelectedSubnetChanged"), prefAppId);
            centre.AddObserver(this, new Selector("multicastEnabledChanged:"), new NSString("PreferenceMulticastEnabledChanged"), prefAppId);
            centre.AddObserver(this, new Selector("multicastChannelChanged:"), new NSString("PreferenceMulticastChannelChanged"), prefAppId);
            centre.AddObserver(this, new Selector("musicLatencyMsChanged:"), new NSString("PreferenceMusicLatencyMsChanged"), prefAppId);
            centre.AddObserver(this, new Selector("videoLatencyMsChanged:"), new NSString("PreferenceVideoLatencyMsChanged"), prefAppId);
            centre.AddObserver(this, new Selector("volumeControlClicked:"), new NSString("PreferenceRotaryVolumeControlChanged"), prefAppId);
            centre.AddObserver(this, new Selector("autoUpdatesEnabledChanged:"), new NSString("PreferenceAutoUpdatesEnabledChanged"), prefAppId);
            centre.AddObserver(this, new Selector("betaUpdatesEnabledChanged:"), new NSString("PreferenceBetaUpdatesEnabledChanged"), prefAppId);
            centre.AddObserver(this, new Selector("usageDataEnabledChanged:"), new NSString("PreferenceUsageDataEnabledChanged"), prefAppId);
            centre.AddObserver(this, new Selector("refreshReceiverListClicked:"), new NSString("RefreshReceiverList"), prefAppId);
            centre.AddObserver(this, new Selector("checkForUpdatesClicked:"), new NSString("CheckForUpdates"), prefAppId);

            // hook up event handlers from model events - these changes in the model are reflected in the preference pane
            iHelperAutoUpdate.OptionPageUpdates.EventChanged += OptionPageUpdatesChanged;
            iModel.EventEnabledChanged      += ModelEnabledChanged;
            iModel.EventReceiverListChanged += ModelReceiverListChanged;
            iModel.EventSubnetListChanged   += ModelSubnetListChanged;

            // hook up event handlers from preferences events - the model can change these preferences - all other preferences
            // are only changed by the preference pane
            iModel.Preferences.EventSelectedReceiverChanged += PreferencesSelectedReceiverChanged;
            iModel.Preferences.EventSelectedSubnetChanged   += PreferencesSelectedSubnetChanged;

            // initialise the view with current values of defaults
            NSUserDefaults.StandardUserDefaults.SetBool(iModel.Enabled, kPreferenceEnabled);
            NSUserDefaults.StandardUserDefaults[kPreferenceReceiverList] = BuildReceiverList();
            NSUserDefaults.StandardUserDefaults[kPreferenceSubnetList]   = BuildSubnetList();
            NSUserDefaults.StandardUserDefaults.SetString(iModel.Preferences.SelectedReceiverUdn, kPreferenceSelectedReceiverUdn);
            NSUserDefaults.StandardUserDefaults.SetInt((int)iModel.Preferences.SelectedSubnetAddress, kPreferenceSelectedSubnetAddress);
            NSUserDefaults.StandardUserDefaults.SetBool(iModel.Preferences.MulticastEnabled, kPreferenceMulticastEnabled);
            NSUserDefaults.StandardUserDefaults.SetInt((int)iModel.Preferences.MulticastChannel, kPreferenceMulticastChannel);

            NSUserDefaults.StandardUserDefaults.SetInt((int)iModel.Preferences.MusicLatencyMs, kPreferenceMusicLatencyMs);
            NSUserDefaults.StandardUserDefaults.SetInt((int)iModel.Preferences.VideoLatencyMs, kPreferenceVideoLatencyMs);
            NSUserDefaults.StandardUserDefaults.SetBool(iModel.Preferences.RotaryVolumeControl, kPreferenceRotaryVolumeControl);
            NSUserDefaults.StandardUserDefaults.SetBool(iHelperAutoUpdate.OptionPageUpdates.AutoUpdate, kPreferenceAutoUpdatesEnabled);
            NSUserDefaults.StandardUserDefaults.SetBool(iHelperAutoUpdate.OptionPageUpdates.BetaVersions, kPreferenceBetaUpdatesEnabled);
            NSUserDefaults.StandardUserDefaults.SetBool(iModel.Preferences.UsageData, kPreferenceUsageDataEnabled);
            NSUserDefaults.StandardUserDefaults.Synchronize();
            centre.PostNotificationName("PreferenceAllChanged", iAppId, null, true);
        }