229 lines
7.6 KiB
C#
229 lines
7.6 KiB
C#
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 JD.Core;
|
|
|
|
namespace JD.Remoting.Gui
|
|
{
|
|
public class EngineConnectionManager : INotifyPropertyChanged
|
|
{
|
|
#region variables
|
|
|
|
private ObservableCollection<EngineConnectionSettings> _connectionList;
|
|
private EngineConnectionSettings _preferedConnectionSettings;
|
|
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<Match>().Any();
|
|
}
|
|
|
|
public bool IsPortValid(string portName)
|
|
{
|
|
if (string.IsNullOrEmpty(portName) || ushort.TryParse(portName, out ushort parseResult) == false)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private ushort Port(string portName)
|
|
{
|
|
if (string.IsNullOrEmpty(portName) == false)
|
|
{
|
|
if (ushort.TryParse(portName, out ushort parseResult) == true)
|
|
return parseResult;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public ObservableCollection<EngineConnectionSettings> 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<EngineConnectionSettings>));
|
|
|
|
ConnectionList = new ObservableCollection<EngineConnectionSettings>();
|
|
if (File.Exists(FullConfigPath))
|
|
{
|
|
using (var fs = new FileStream(FullConfigPath, FileMode.Open))
|
|
using (TextReader reader = new StreamReader(fs))
|
|
{
|
|
var listDeserialized = (List<EngineConnectionSettings>) serializerReader.Deserialize(reader);
|
|
// dont add last settings (the user selected one)
|
|
foreach (var engineConnectionSetting in listDeserialized.GetRange(0, listDeserialized.Count - 1))
|
|
{
|
|
ConnectionList.Add(engineConnectionSetting);
|
|
}
|
|
// keep the user selection in prefered settings
|
|
PreferedConnectionSettings = listDeserialized.Last();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
result = false;
|
|
}
|
|
finally
|
|
{
|
|
if (ConnectionList.All(item => item.Name != EngineConnectionSettings.Default.Name))
|
|
{
|
|
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<EngineConnectionSettings>();
|
|
listSerialize.AddRange(ConnectionList);
|
|
listSerialize.Add(PreferedConnectionSettings);
|
|
try
|
|
{
|
|
var serializer = new XmlSerializer(typeof(List<EngineConnectionSettings>));
|
|
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(portName),
|
|
ipAddressName,
|
|
(PlatformId)Enum.Parse(typeof(PlatformId), targetName, ignoreCase:true),
|
|
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)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
if (ConnectionList != null)
|
|
ConnectionList.Clear();
|
|
}
|
|
}
|
|
}
|