PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Trunk/Fantacid303.ProcessAffinityControl/Fantacid303.ProcessAffinityControl.Manager/MainWindowViewModels/ConfigurationViewModel.cs

#
C# | 268 lines | 208 code | 27 blank | 33 comment | 80 complexity | 14c6a66e3d63a4f96ff6ef0af4b856d8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.ObjectModel;
  6. using System.Threading;
  7. using System.Windows.Input;
  8. using Fantacid303.ProcessAffinityControl.Manager.Extensions;
  9. using Fantacid303.ProcessAffinityControl.Config;
  10. using Fantacid303.ProcessAffinityControl.Common;
  11. using System.IO;
  12. using System.Windows;
  13. using Fantacid303.ProcessAffinityControl.Manager.Properties;
  14. namespace Fantacid303.ProcessAffinityControl.Manager.MainWindowViewModels
  15. {
  16. /// <summary>
  17. /// Represents the View Model of the whole configuration
  18. /// </summary>
  19. partial class ConfigurationViewModel : ViewModelBase
  20. {
  21. private uint interval;
  22. private object selectedTreeObject;
  23. private ObservableCollection<ConfigurationEntryViewModel> configurationEntries;
  24. private ConfigurationEntryViewModel selectedConfiguration;
  25. private ObservableCollection<ExcludedProcessViewModel> excludedProcesses;
  26. private ObservableCollection<ProcessViewModel> runningProcesses;
  27. private string statusMessage;
  28. private ConfigurationManager configurationManager = new ConfigurationManager();
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="ConfigurationViewModel"/> class.
  31. /// </summary>
  32. public ConfigurationViewModel()
  33. {
  34. this.AddNewEntryCommand = new MyAddNewEntryCommand(this);
  35. this.RefreshProcessListCommand = new MyRefreshProcessListCommand(this);
  36. this.SaveCommand = new MySaveCommand(this);
  37. this.IncreaseIntervalCommand = new MyIncreaseIntervalCommand(this);
  38. this.DecreaseIntervalCommand = new MyDecreaseIntervalCommand(this);
  39. this.StatusMessage = string.Empty;
  40. this.UpdateConfigurationFromManager();
  41. }
  42. /// <summary>
  43. /// Gets the status message
  44. /// </summary>
  45. /// <value>The status message.</value>
  46. public string StatusMessage
  47. {
  48. get { return statusMessage; }
  49. set
  50. {
  51. if (statusMessage == value)
  52. return;
  53. statusMessage = value;
  54. this.OnPropertyChanged("StatusMessage");
  55. }
  56. }
  57. /// <summary>
  58. /// Gets or sets the scan interval in seconds.
  59. /// </summary>
  60. /// <value>The scan interval.</value>
  61. public uint Interval
  62. {
  63. get { return interval; }
  64. set
  65. {
  66. if (interval == value)
  67. return;
  68. interval = value;
  69. this.OnPropertyChanged("Interval");
  70. }
  71. }
  72. /// <summary>
  73. /// Gets or sets the selected tree object.
  74. /// </summary>
  75. /// <value>The selected tree object.</value>
  76. public object SelectedTreeObject
  77. {
  78. get { return selectedTreeObject; }
  79. set
  80. {
  81. if (selectedTreeObject == value)
  82. return;
  83. selectedTreeObject = value;
  84. this.OnPropertyChanged("SelectedTreeObject");
  85. }
  86. }
  87. /// <summary>
  88. /// Updates the configuration from manager configuration manager
  89. /// </summary>
  90. private void UpdateConfigurationFromManager()
  91. {
  92. try
  93. {
  94. this.configurationManager.ReadConfig();
  95. }
  96. catch (FileNotFoundException)
  97. {
  98. if (MessageBoxResult.No == MessageBox.Show(Messages.ConfigFileNotFoundCreateDefaultConfig, "Configuration Not Found", MessageBoxButton.YesNo, MessageBoxImage.Question))
  99. {
  100. Application.Current.Shutdown();
  101. return;
  102. }
  103. else
  104. {
  105. this.configurationManager.CreateDefaultConfiguration();
  106. }
  107. }
  108. var config = this.configurationManager.Configuration;
  109. foreach (var entry in config.ConfigurationEntries)
  110. {
  111. var modelEntry = new ConfigurationEntryViewModel(this) { IsActive = entry.IsActive, Name = entry.Name };
  112. foreach (var rule in entry.Rules)
  113. {
  114. var modelRule = new ConfigurationRuleViewModel(modelEntry) { AffinityType = rule.ProcessorAffinityType, ProcessName = rule.ProcessName };
  115. #region mask mapping
  116. modelRule.CustomMaskProcessor0 = (rule.AffinityMask & ((ulong)1 << 0)) != 0;
  117. modelRule.CustomMaskProcessor1 = (rule.AffinityMask & ((ulong)1 << 1)) != 0;
  118. modelRule.CustomMaskProcessor2 = (rule.AffinityMask & ((ulong)1 << 2)) != 0;
  119. modelRule.CustomMaskProcessor3 = (rule.AffinityMask & ((ulong)1 << 3)) != 0;
  120. modelRule.CustomMaskProcessor4 = (rule.AffinityMask & ((ulong)1 << 4)) != 0;
  121. modelRule.CustomMaskProcessor5 = (rule.AffinityMask & ((ulong)1 << 5)) != 0;
  122. modelRule.CustomMaskProcessor6 = (rule.AffinityMask & ((ulong)1 << 6)) != 0;
  123. modelRule.CustomMaskProcessor7 = (rule.AffinityMask & ((ulong)1 << 7)) != 0;
  124. modelRule.CustomMaskProcessor8 = (rule.AffinityMask & ((ulong)1 << 8)) != 0;
  125. modelRule.CustomMaskProcessor9 = (rule.AffinityMask & ((ulong)1 << 9)) != 0;
  126. modelRule.CustomMaskProcessor10 = (rule.AffinityMask & ((ulong)1 << 10)) != 0;
  127. modelRule.CustomMaskProcessor11 = (rule.AffinityMask & ((ulong)1 << 11)) != 0;
  128. modelRule.CustomMaskProcessor12 = (rule.AffinityMask & ((ulong)1 << 12)) != 0;
  129. modelRule.CustomMaskProcessor13 = (rule.AffinityMask & ((ulong)1 << 13)) != 0;
  130. modelRule.CustomMaskProcessor14 = (rule.AffinityMask & ((ulong)1 << 14)) != 0;
  131. modelRule.CustomMaskProcessor15 = (rule.AffinityMask & ((ulong)1 << 15)) != 0;
  132. modelRule.CustomMaskProcessor16 = (rule.AffinityMask & ((ulong)1 << 16)) != 0;
  133. modelRule.CustomMaskProcessor17 = (rule.AffinityMask & ((ulong)1 << 17)) != 0;
  134. modelRule.CustomMaskProcessor18 = (rule.AffinityMask & ((ulong)1 << 18)) != 0;
  135. modelRule.CustomMaskProcessor19 = (rule.AffinityMask & ((ulong)1 << 19)) != 0;
  136. modelRule.CustomMaskProcessor20 = (rule.AffinityMask & ((ulong)1 << 20)) != 0;
  137. modelRule.CustomMaskProcessor21 = (rule.AffinityMask & ((ulong)1 << 21)) != 0;
  138. modelRule.CustomMaskProcessor22 = (rule.AffinityMask & ((ulong)1 << 22)) != 0;
  139. modelRule.CustomMaskProcessor23 = (rule.AffinityMask & ((ulong)1 << 23)) != 0;
  140. modelRule.CustomMaskProcessor24 = (rule.AffinityMask & ((ulong)1 << 24)) != 0;
  141. modelRule.CustomMaskProcessor25 = (rule.AffinityMask & ((ulong)1 << 25)) != 0;
  142. modelRule.CustomMaskProcessor26 = (rule.AffinityMask & ((ulong)1 << 26)) != 0;
  143. modelRule.CustomMaskProcessor27 = (rule.AffinityMask & ((ulong)1 << 27)) != 0;
  144. modelRule.CustomMaskProcessor28 = (rule.AffinityMask & ((ulong)1 << 28)) != 0;
  145. modelRule.CustomMaskProcessor29 = (rule.AffinityMask & ((ulong)1 << 29)) != 0;
  146. modelRule.CustomMaskProcessor30 = (rule.AffinityMask & ((ulong)1 << 30)) != 0;
  147. modelRule.CustomMaskProcessor31 = (rule.AffinityMask & ((ulong)1 << 31)) != 0;
  148. modelRule.CustomMaskProcessor32 = (rule.AffinityMask & ((ulong)1 << 32)) != 0;
  149. modelRule.CustomMaskProcessor33 = (rule.AffinityMask & ((ulong)1 << 33)) != 0;
  150. modelRule.CustomMaskProcessor34 = (rule.AffinityMask & ((ulong)1 << 34)) != 0;
  151. modelRule.CustomMaskProcessor35 = (rule.AffinityMask & ((ulong)1 << 35)) != 0;
  152. modelRule.CustomMaskProcessor36 = (rule.AffinityMask & ((ulong)1 << 36)) != 0;
  153. modelRule.CustomMaskProcessor37 = (rule.AffinityMask & ((ulong)1 << 37)) != 0;
  154. modelRule.CustomMaskProcessor38 = (rule.AffinityMask & ((ulong)1 << 38)) != 0;
  155. modelRule.CustomMaskProcessor39 = (rule.AffinityMask & ((ulong)1 << 39)) != 0;
  156. modelRule.CustomMaskProcessor40 = (rule.AffinityMask & ((ulong)1 << 40)) != 0;
  157. modelRule.CustomMaskProcessor41 = (rule.AffinityMask & ((ulong)1 << 41)) != 0;
  158. modelRule.CustomMaskProcessor42 = (rule.AffinityMask & ((ulong)1 << 42)) != 0;
  159. modelRule.CustomMaskProcessor43 = (rule.AffinityMask & ((ulong)1 << 43)) != 0;
  160. modelRule.CustomMaskProcessor44 = (rule.AffinityMask & ((ulong)1 << 44)) != 0;
  161. modelRule.CustomMaskProcessor45 = (rule.AffinityMask & ((ulong)1 << 45)) != 0;
  162. modelRule.CustomMaskProcessor46 = (rule.AffinityMask & ((ulong)1 << 46)) != 0;
  163. modelRule.CustomMaskProcessor47 = (rule.AffinityMask & ((ulong)1 << 47)) != 0;
  164. modelRule.CustomMaskProcessor48 = (rule.AffinityMask & ((ulong)1 << 48)) != 0;
  165. modelRule.CustomMaskProcessor49 = (rule.AffinityMask & ((ulong)1 << 49)) != 0;
  166. modelRule.CustomMaskProcessor50 = (rule.AffinityMask & ((ulong)1 << 50)) != 0;
  167. modelRule.CustomMaskProcessor51 = (rule.AffinityMask & ((ulong)1 << 51)) != 0;
  168. modelRule.CustomMaskProcessor52 = (rule.AffinityMask & ((ulong)1 << 52)) != 0;
  169. modelRule.CustomMaskProcessor53 = (rule.AffinityMask & ((ulong)1 << 53)) != 0;
  170. modelRule.CustomMaskProcessor54 = (rule.AffinityMask & ((ulong)1 << 54)) != 0;
  171. modelRule.CustomMaskProcessor55 = (rule.AffinityMask & ((ulong)1 << 55)) != 0;
  172. modelRule.CustomMaskProcessor56 = (rule.AffinityMask & ((ulong)1 << 56)) != 0;
  173. modelRule.CustomMaskProcessor57 = (rule.AffinityMask & ((ulong)1 << 57)) != 0;
  174. modelRule.CustomMaskProcessor58 = (rule.AffinityMask & ((ulong)1 << 58)) != 0;
  175. modelRule.CustomMaskProcessor59 = (rule.AffinityMask & ((ulong)1 << 59)) != 0;
  176. modelRule.CustomMaskProcessor60 = (rule.AffinityMask & ((ulong)1 << 60)) != 0;
  177. modelRule.CustomMaskProcessor61 = (rule.AffinityMask & ((ulong)1 << 61)) != 0;
  178. modelRule.CustomMaskProcessor62 = (rule.AffinityMask & ((ulong)1 << 62)) != 0;
  179. modelRule.CustomMaskProcessor63 = (rule.AffinityMask & ((ulong)1 << 63)) != 0;
  180. #endregion
  181. modelEntry.Rules.Add(modelRule);
  182. }
  183. this.ConfigurationEntries.Add(modelEntry);
  184. }
  185. this.ExcludedProcesses.Clear();
  186. foreach (var excludedProcess in config.ProcessIgnoreList)
  187. this.ExcludedProcesses.Add(new ExcludedProcessViewModel(this) { Name = excludedProcess });
  188. this.Interval = config.Interval;
  189. }
  190. /// <summary>
  191. /// Gets or sets the selected <see cref="ConfigurationEntry"/> from/to the UI
  192. /// </summary>
  193. public ConfigurationEntryViewModel SelectedConfiguration
  194. {
  195. get { return selectedConfiguration; }
  196. set
  197. {
  198. if (selectedConfiguration == value)
  199. return;
  200. selectedConfiguration = value;
  201. this.OnPropertyChanged("SelectedConfiguration");
  202. }
  203. }
  204. /// <summary>
  205. /// Gets the available list of <see cref="ConfigurationEntry"/>.
  206. /// </summary>
  207. public ObservableCollection<ConfigurationEntryViewModel> ConfigurationEntries
  208. {
  209. get
  210. {
  211. if (configurationEntries == null)
  212. configurationEntries = new ObservableCollection<ConfigurationEntryViewModel>();
  213. return configurationEntries;
  214. }
  215. }
  216. /// <summary>
  217. /// Gets the available list of <see cref="ExcludedProcessViewModel"/>.
  218. /// </summary>
  219. public ObservableCollection<ExcludedProcessViewModel> ExcludedProcesses
  220. {
  221. get
  222. {
  223. if (excludedProcesses == null)
  224. excludedProcesses = new ObservableCollection<ExcludedProcessViewModel>();
  225. return excludedProcesses;
  226. }
  227. }
  228. /// <summary>
  229. /// Gets the available list of <see cref="ProcessViewModel"/>.
  230. /// </summary>
  231. public ObservableCollection<ProcessViewModel> RunningProcesses
  232. {
  233. get
  234. {
  235. if (runningProcesses == null)
  236. runningProcesses = new ObservableCollection<ProcessViewModel>();
  237. return runningProcesses;
  238. }
  239. }
  240. }
  241. }