using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; using System.IO; namespace IndianHealthService.BMXNet.WinForm.Configuration { /// /// Container for all persistent and available connection spec /// public class RpmsConnectionSettings { private Settings _userSettings = new Settings(); public Settings UserSettings { get { return _userSettings; } set { _userSettings = value; } } private RpmsConnectionSpec _commandLineConnectionSpec = null; public RpmsConnectionSpec CommandLineConnectionSpec { get { return _commandLineConnectionSpec; } set { _commandLineConnectionSpec = value; } } private List _managedConnectionSpecs = new List(); /// /// Answer all connection specs in user defined order /// public List ManagedConnectionSpecs { get { return _managedConnectionSpecs; } set { _managedConnectionSpecs = value; } } /// /// Answer all available connection specs, including command-line spec /// public List ConnectionSpecs { get { if (this.CommandLineConnectionSpec==null) { return this.ManagedConnectionSpecs; } else { List allSpecs=new List(this.ManagedConnectionSpecs); allSpecs.Insert(0,this.CommandLineConnectionSpec); return allSpecs; } } } private RpmsConnectionSpec _defaultConnectionSpec=null; /// /// Answer the default connection spec. If there is not /// user defined default, answer the first connection spec /// public RpmsConnectionSpec DefaultConnectionSpec { get { if (_defaultConnectionSpec== null) { if (this.CommandLineConnectionSpec == null) { foreach (RpmsConnectionSpec each in this.ManagedConnectionSpecs) { if (each.IsDefault) { return each; } } } return this.ConnectionSpecs.Count == 0 ? null : this.ConnectionSpecs[0]; } else { return _defaultConnectionSpec; } } set { this._defaultConnectionSpec = value; } } /// /// Add aSpec to the receiver. Not persisted /// /// public void AddConnectionSpec(RpmsConnectionSpec aSpec) { this.ManagedConnectionSpecs.Add(aSpec); if (aSpec.IsDefault) { this.SetConnectionSpecAsDefault(aSpec); } } /// /// Remove aSpec from the receiver. Not persisted. /// /// public void RemoveConnectionSpec(RpmsConnectionSpec aSpec) { this.ManagedConnectionSpecs.Remove(aSpec); } /// /// /// /// /// True if there was any movement public bool MoveConnectionSpecUp(RpmsConnectionSpec aSpec) { int currentIndex = this.ManagedConnectionSpecs.IndexOf(aSpec); if (currentIndex == 0) { return false; } else { this.RemoveConnectionSpec(aSpec); this.ManagedConnectionSpecs.Insert(currentIndex - 1, aSpec); return true; } } /// /// /// /// /// True if there was any movement public bool MoveConnectionSpecDown(RpmsConnectionSpec aSpec) { int currentIndex = this.ManagedConnectionSpecs.IndexOf(aSpec); if (currentIndex == this.ManagedConnectionSpecs.Count - 1) { return false; } else { this.RemoveConnectionSpec(aSpec); this.ManagedConnectionSpecs.Insert(currentIndex + 1, aSpec); return true; } } public void SetConnectionSpecAsDefault(RpmsConnectionSpec aSpec) { foreach (RpmsConnectionSpec each in this.ManagedConnectionSpecs) { each.IsDefault = false; } aSpec.IsDefault = true; } /// /// Refresh the receiver from the current state of the persistent store /// public void Revert() { this.Load(); } public void Load() { this.ManagedConnectionSpecs = this.Store.Read>(this.StorageKeys); if (this.ManagedConnectionSpecs == null) this.ManagedConnectionSpecs = new List(); } public void Save() { this.Store.Write>(this.ManagedConnectionSpecs, this.StorageKeys); } private string[] _storageKeys = null; public string[] StorageKeys { get { return _storageKeys; } set { _storageKeys = value; } } private PersistentStore _store = null; public PersistentStore Store { get { return _store; } set { _store = value; } } } }