| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.ComponentModel; |
| 6 | using System.Diagnostics; |
| 7 | using System.Text.RegularExpressions; |
| 8 | using WixToolset.BootstrapperApplicationApi; |
| 9 | |
| 10 | namespace WixToolset.WixBA |
| 11 | { |
| 12 | public class ProgressViewModel : PropertyNotifyBase |
| 13 | { |
| 14 | private static readonly Regex TrimActionTimeFromMessage = new Regex(@"^\w+\s+\d+:\d+:\d+:\s+", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.Singleline); |
| 15 | |
| 16 | private RootViewModel root; |
| 17 | private Dictionary<string, int> executingPackageOrderIndex; |
| 18 | |
| 19 | private int progressPhases; |
| 20 | private int progress; |
| 21 | private int cacheProgress; |
| 22 | private int executeProgress; |
| 23 | private string package; |
| 24 | private string message; |
| 25 | |
| 26 | public ProgressViewModel(RootViewModel root) |
| 27 | { |
| 28 | this.root = root; |
| 29 | this.executingPackageOrderIndex = new Dictionary<string, int>(); |
| 30 | |
| 31 | this.root.PropertyChanged += this.RootPropertyChanged; |
| 32 | |
| 33 | WixBA.Model.Bootstrapper.ExecutePackageBegin += this.ExecutePackageBegin; |
| 34 | WixBA.Model.Bootstrapper.ExecutePackageComplete += this.ExecutePackageComplete; |
| 35 | WixBA.Model.Bootstrapper.ExecuteProgress += this.ApplyExecuteProgress; |
| 36 | WixBA.Model.Bootstrapper.PauseAutomaticUpdatesBegin += this.PauseAutomaticUpdatesBegin; |
| 37 | WixBA.Model.Bootstrapper.SystemRestorePointBegin += this.SystemRestorePointBegin; |
| 38 | WixBA.Model.Bootstrapper.PlanBegin += this.PlanBegin; |
| 39 | WixBA.Model.Bootstrapper.PlannedPackage += this.PlannedPackage; |
| 40 | WixBA.Model.Bootstrapper.ApplyBegin += this.ApplyBegin; |
| 41 | WixBA.Model.Bootstrapper.Progress += this.ApplyProgress; |
| 42 | WixBA.Model.Bootstrapper.CacheAcquireProgress += this.CacheAcquireProgress; |
| 43 | WixBA.Model.Bootstrapper.CacheContainerOrPayloadVerifyProgress += CacheContainerOrPayloadVerifyProgress; |
| 44 | WixBA.Model.Bootstrapper.CachePayloadExtractProgress += CachePayloadExtractProgress; |
| 45 | WixBA.Model.Bootstrapper.CacheVerifyProgress += CacheVerifyProgress; |
| 46 | WixBA.Model.Bootstrapper.CacheComplete += this.CacheComplete; |
| 47 | } |
| 48 | |
| 49 | public bool ProgressEnabled |
| 50 | { |
| 51 | get { return this.root.InstallState == InstallationState.Applying; } |
| 52 | } |
| 53 | |
| 54 | public int Progress |
| 55 | { |
| 56 | get |
| 57 | { |
| 58 | return this.progress; |
| 59 | } |
| 60 | |
| 61 | set |
| 62 | { |
| 63 | if (this.progress != value) |
| 64 | { |
| 65 | this.progress = value; |
| 66 | base.OnPropertyChanged("Progress"); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public string Package |
| 72 | { |
| 73 | get |
| 74 | { |
| 75 | return this.package; |
| 76 | } |
| 77 | |
| 78 | set |
| 79 | { |
| 80 | if (this.package != value) |
| 81 | { |
| 82 | this.package = value; |
| 83 | base.OnPropertyChanged("Package"); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public string Message |
| 89 | { |
| 90 | get |
| 91 | { |
| 92 | return this.message; |
| 93 | } |
| 94 | |
| 95 | set |
| 96 | { |
| 97 | if (this.message != value) |
| 98 | { |
| 99 | this.message = value; |
| 100 | base.OnPropertyChanged("Message"); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void RootPropertyChanged(object sender, PropertyChangedEventArgs e) |
| 106 | { |
| 107 | if ("InstallState" == e.PropertyName) |
| 108 | { |
| 109 | base.OnPropertyChanged("ProgressEnabled"); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private void PlanBegin(object sender, PlanBeginEventArgs e) |
| 114 | { |
| 115 | lock (this) |
| 116 | { |
| 117 | this.executingPackageOrderIndex.Clear(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private void PlannedPackage(object sender, PlannedPackageEventArgs e) |
| 122 | { |
| 123 | if (ActionState.None != e.Execute) |
| 124 | { |
| 125 | lock (this) |
| 126 | { |
| 127 | Debug.Assert(!this.executingPackageOrderIndex.ContainsKey(e.PackageId)); |
| 128 | this.executingPackageOrderIndex.Add(e.PackageId, this.executingPackageOrderIndex.Count); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void ExecutePackageBegin(object sender, ExecutePackageBeginEventArgs e) |
| 134 | { |
| 135 | lock (this) |
| 136 | { |
| 137 | this.Package = WixBA.Model.GetPackageName(e.PackageId); |
| 138 | this.Message = String.Format("Processing: {0}", this.Package); |
| 139 | e.Cancel = this.root.Canceled; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private void ExecutePackageComplete(object sender, ExecutePackageCompleteEventArgs e) |
| 144 | { |
| 145 | lock (this) |
| 146 | { // avoid a stale display |
| 147 | this.Message = String.Empty; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private void PauseAutomaticUpdatesBegin(object sender, PauseAutomaticUpdatesBeginEventArgs e) |
| 152 | { |
| 153 | lock (this) |
| 154 | { |
| 155 | this.Message = "Pausing Windows automatic updates"; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private void SystemRestorePointBegin(object sender, SystemRestorePointBeginEventArgs e) |
| 160 | { |
| 161 | lock (this) |
| 162 | { |
| 163 | this.Message = "Creating system restore point"; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private void ApplyBegin(object sender, ApplyBeginEventArgs e) |
| 168 | { |
| 169 | this.progressPhases = e.PhaseCount; |
| 170 | } |
| 171 | |
| 172 | private void ApplyProgress(object sender, ProgressEventArgs e) |
| 173 | { |
| 174 | lock (this) |
| 175 | { |
| 176 | e.Cancel = this.root.Canceled; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private void CacheAcquireProgress(object sender, CacheAcquireProgressEventArgs e) |
| 181 | { |
| 182 | lock (this) |
| 183 | { |
| 184 | this.cacheProgress = e.OverallPercentage; |
| 185 | this.Progress = (this.cacheProgress + this.executeProgress) / this.progressPhases; |
| 186 | e.Cancel = this.root.Canceled; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private void CacheContainerOrPayloadVerifyProgress(object sender, CacheContainerOrPayloadVerifyProgressEventArgs e) |
| 191 | { |
| 192 | lock (this) |
| 193 | { |
| 194 | this.cacheProgress = e.OverallPercentage; |
| 195 | this.Progress = (this.cacheProgress + this.executeProgress) / this.progressPhases; |
| 196 | e.Cancel = this.root.Canceled; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private void CachePayloadExtractProgress(object sender, CachePayloadExtractProgressEventArgs e) |
| 201 | { |
| 202 | lock (this) |
| 203 | { |
| 204 | this.cacheProgress = e.OverallPercentage; |
| 205 | this.Progress = (this.cacheProgress + this.executeProgress) / this.progressPhases; |
| 206 | e.Cancel = this.root.Canceled; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private void CacheVerifyProgress(object sender, CacheVerifyProgressEventArgs e) |
| 211 | { |
| 212 | lock (this) |
| 213 | { |
| 214 | this.cacheProgress = e.OverallPercentage; |
| 215 | this.Progress = (this.cacheProgress + this.executeProgress) / this.progressPhases; |
| 216 | e.Cancel = this.root.Canceled; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | private void CacheComplete(object sender, CacheCompleteEventArgs e) |
| 221 | { |
| 222 | lock (this) |
| 223 | { |
| 224 | this.cacheProgress = 100; |
| 225 | this.Progress = (this.cacheProgress + this.executeProgress) / this.progressPhases; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private void ApplyExecuteProgress(object sender, ExecuteProgressEventArgs e) |
| 230 | { |
| 231 | lock (this) |
| 232 | { |
| 233 | this.executeProgress = e.OverallPercentage; |
| 234 | this.Progress = (this.cacheProgress + this.executeProgress) / this.progressPhases; |
| 235 | |
| 236 | if (WixBA.Model.Command.Display == Display.Embedded) |
| 237 | { |
| 238 | WixBA.Model.Engine.SendEmbeddedProgress(e.ProgressPercentage, this.Progress); |
| 239 | } |
| 240 | |
| 241 | e.Cancel = this.root.Canceled; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |