using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Xml.Serialization; using Shared; using UAF; namespace Plugin { public class EngineConnectionManager : INotifyPropertyChanged { #region variables private ObservableCollection _connectionList = null; private EngineConnectionSettings _preferedConnectionSettings = null; private const string _settingsFileName = "connectionsettings.xml"; #endregion #region properties private bool Modified { get; set; } private string IPAdressPattern { get { return @"^(\d{1,4})\.(\d{1,4})\.(\d{1,4})\.(\d{1,4})$"; } } public bool IsAddressValid(string ipAddress) { MatchCollection results = Regex.Matches(ipAddress, IPAdressPattern, RegexOptions.None); return results.Cast().Any(); } public bool IsPortValid(string portName) { ushort parseResult = 0; if (string.IsNullOrEmpty(portName) || ushort.TryParse(portName, out parseResult) == false) { return false; } return true; } private ushort Port(string portName) { if (string.IsNullOrEmpty(portName) == false) { ushort parseResult = 0; if (ushort.TryParse(portName, out parseResult) == true) return parseResult; } return 0; } public ObservableCollection ConnectionList { get { return _connectionList; } set { _connectionList = value; OnPropertyChanged("ConnectionList"); } } public EngineConnectionSettings PreferedConnectionSettings { get { return _preferedConnectionSettings; } set { if (ConnectionList == null) throw new NullReferenceException(); int indexOf = ConnectionList.IndexOf(value); if (indexOf >= 0) { _preferedConnectionSettings = ConnectionList[indexOf]; OnPropertyChanged("PreferedConnectionSettings"); Modified = true; } } } #endregion #region members private string FullConfigPath { get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), _settingsFileName); } } public bool Load() { bool result = true; try { var serializerReader = new XmlSerializer(typeof (List)); ConnectionList = new ObservableCollection(); if (File.Exists(FullConfigPath)) { using (var fs = new FileStream(FullConfigPath, FileMode.Open)) using (TextReader reader = new StreamReader(fs)) { var listSerialize = (List) serializerReader.Deserialize(reader); // dont add last settings (the user selected one) foreach (var engineConnectionSetting in listSerialize) { if (engineConnectionSetting != listSerialize.Last()) ConnectionList.Add(engineConnectionSetting); } // keep the user selction in prefered settings PreferedConnectionSettings = listSerialize.Last(); } } } catch (System.Exception ex) { MessageBox.Show(ex.Message); result = false; } finally { if (ConnectionList.Contains(EngineConnectionSettings.Default) == false) { ConnectionList.Insert(0, EngineConnectionSettings.Default); PreferedConnectionSettings = EngineConnectionSettings.Default; Modified = true; } } return result; } public bool Save() { if (Modified) { if (ConnectionList.Count != 0) { var listSerialize = new List(); listSerialize.AddRange(ConnectionList); listSerialize.Add(PreferedConnectionSettings); try { var serializer = new XmlSerializer(typeof(List)); TextWriter writer = new StreamWriter(FullConfigPath); serializer.Serialize(writer, listSerialize); writer.Close(); Modified = false; return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } else { File.Delete(FullConfigPath); } } return true; } public bool AddConnectionSetting(string ipAddressName, string portName, string targetName, string connectionName) { MatchCollection results = Regex.Matches(ipAddressName, IPAdressPattern, RegexOptions.None); foreach (Match match in results) { if (IsPortValid(portName) == false) { MessageBox.Show("Invalid Port value (should be unsigned short integer 0 < port < 65536)"); return false; } var newConnection = new EngineConnectionSettings { Port = Port(portName), Ip = ipAddressName, Target = EnumUtils.TargetFromName(targetName, default(PlatformId)), ConnectionName = connectionName }; if (ConnectionList.Contains(newConnection) == false) { ConnectionList.Add(newConnection); Modified = true; } else { MessageBox.Show("Adress already registered"); } break; } return true; } public void RemoveConnectionSetting(EngineConnectionSettings setting) { if (ConnectionList.Contains(setting)) { // check that user selected setting is not the removed one if (setting == PreferedConnectionSettings) PreferedConnectionSettings = EngineConnectionSettings.Default; ConnectionList.Remove(setting); OnPropertyChanged("ConnectionList"); } } #endregion public event PropertyChangedEventHandler PropertyChanged; //[NotifyPropertyChangedInvocator] public virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } public void Clear() { if (ConnectionList != null) ConnectionList.Clear(); } } }