|
| 1 | +' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server. |
| 2 | +' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas) |
| 3 | +' |
| 4 | +' This program is free software: you can redistribute it and/or modify it under the terms of the |
| 5 | +' GNU General Public License as published by the Free Software Foundation, either version 3 of the |
| 6 | +' License, or any later version. |
| 7 | +' |
| 8 | +' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY |
| 9 | + |
| 10 | +Imports System.ComponentModel |
| 11 | +Imports System.IO |
| 12 | +Imports System.Runtime.CompilerServices |
| 13 | +Imports System.Threading |
| 14 | +Imports WinNUT_Client_Common |
| 15 | + |
| 16 | +Namespace Models |
| 17 | + Friend Class UpgradePrefsDialogModel |
| 18 | + Implements INotifyPropertyChanged |
| 19 | + |
| 20 | + Private ReadOnly upgradeWorker As New BackgroundWorker With { |
| 21 | + .WorkerReportsProgress = True |
| 22 | + } |
| 23 | + Private oldPrefs As OldParams.UpgradableParams |
| 24 | + |
| 25 | +#Region "Properties and backing fields" |
| 26 | + |
| 27 | + Private _ImportPreviousSettings As Boolean |
| 28 | + Private _BackupPreviousSettings As Boolean |
| 29 | + Private _DeletePreviousSettings As Boolean |
| 30 | + Private _OKButtonEnabled As Boolean |
| 31 | + Private _FormEnabled As Boolean |
| 32 | + Private _FormUseWaitCursor As Boolean |
| 33 | + Private _ProgressPercent = 0 |
| 34 | + Private _Icon As Bitmap = My.Resources.regedit_exe_14_100_0 |
| 35 | + |
| 36 | + Public Property ImportPreviousSettigns As Boolean |
| 37 | + Get |
| 38 | + Return _ImportPreviousSettings |
| 39 | + End Get |
| 40 | + Set(value As Boolean) |
| 41 | + _ImportPreviousSettings = value |
| 42 | + NotifyPropertyChanged() |
| 43 | + ' CalculateOKButtonState() |
| 44 | + End Set |
| 45 | + End Property |
| 46 | + |
| 47 | + Public Property BackupPreviousSettings As Boolean |
| 48 | + Get |
| 49 | + Return _BackupPreviousSettings |
| 50 | + End Get |
| 51 | + Set(value As Boolean) |
| 52 | + If _BackupPreviousSettings <> value Then |
| 53 | + _BackupPreviousSettings = value |
| 54 | + NotifyPropertyChanged() |
| 55 | + ' CalculateOKButtonState() |
| 56 | + End If |
| 57 | + End Set |
| 58 | + End Property |
| 59 | + |
| 60 | + Public Property DeletePreviousSettings As Boolean |
| 61 | + Get |
| 62 | + Return _DeletePreviousSettings |
| 63 | + End Get |
| 64 | + Set(value As Boolean) |
| 65 | + _DeletePreviousSettings = value |
| 66 | + NotifyPropertyChanged() |
| 67 | + ' CalculateOKButtonState() |
| 68 | + End Set |
| 69 | + End Property |
| 70 | + |
| 71 | + Public Property OKButtonEnabled As Boolean |
| 72 | + Get |
| 73 | + Return _OKButtonEnabled |
| 74 | + End Get |
| 75 | + Set(value As Boolean) |
| 76 | + If _OKButtonEnabled <> value Then |
| 77 | + _OKButtonEnabled = value |
| 78 | + NotifyPropertyChanged() |
| 79 | + End If |
| 80 | + End Set |
| 81 | + End Property |
| 82 | + |
| 83 | + ''' <summary> |
| 84 | + ''' Enable/disable the main form and cascade changes down as necessary. |
| 85 | + ''' </summary> |
| 86 | + ''' <returns></returns> |
| 87 | + Public Property FormEnabled As Boolean |
| 88 | + Get |
| 89 | + Return _FormEnabled |
| 90 | + End Get |
| 91 | + Set(value As Boolean) |
| 92 | + If _FormEnabled <> value Then |
| 93 | + _FormEnabled = value |
| 94 | + NotifyPropertyChanged() |
| 95 | + End If |
| 96 | + End Set |
| 97 | + End Property |
| 98 | + |
| 99 | + Public Property FormUseWaitCursor As Boolean |
| 100 | + Get |
| 101 | + Return _FormUseWaitCursor |
| 102 | + End Get |
| 103 | + Set(value As Boolean) |
| 104 | + If _FormUseWaitCursor <> value Then |
| 105 | + _FormUseWaitCursor = value |
| 106 | + NotifyPropertyChanged() |
| 107 | + End If |
| 108 | + End Set |
| 109 | + End Property |
| 110 | + |
| 111 | + Public Property ProgressPercent As Integer |
| 112 | + Get |
| 113 | + Return _ProgressPercent |
| 114 | + End Get |
| 115 | + Set(value As Integer) |
| 116 | + If value < 0 OrElse value > 100 Then |
| 117 | + Throw New ArgumentOutOfRangeException( |
| 118 | + "Progress must be reported as a percentage inbetween 0 and 100.") |
| 119 | + Else |
| 120 | + _ProgressPercent = value |
| 121 | + NotifyPropertyChanged() |
| 122 | + End If |
| 123 | + End Set |
| 124 | + End Property |
| 125 | + |
| 126 | + Public ReadOnly Property UpgradeInProgress As Boolean |
| 127 | + Get |
| 128 | + Return upgradeWorker IsNot Nothing AndAlso upgradeWorker.IsBusy |
| 129 | + End Get |
| 130 | + End Property |
| 131 | + |
| 132 | + Public Property Icon As Bitmap |
| 133 | + Get |
| 134 | + Return _Icon |
| 135 | + End Get |
| 136 | + Private Set(value As Bitmap) |
| 137 | + If Not _Icon.Equals(value) Then |
| 138 | + _Icon = value |
| 139 | + NotifyPropertyChanged() |
| 140 | + End If |
| 141 | + End Set |
| 142 | + End Property |
| 143 | + |
| 144 | +#End Region |
| 145 | + |
| 146 | + Public Event WorkComplete As RunWorkerCompletedEventHandler |
| 147 | + |
| 148 | +#Region "INotifyPropertyChanged implementation" |
| 149 | + |
| 150 | + ' This method is called by the Set accessor of each property. |
| 151 | + ' The CallerMemberName attribute that is applied to the optional propertyName |
| 152 | + ' parameter causes the property name of the caller to be substituted as an argument. |
| 153 | + Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing) |
| 154 | + RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) |
| 155 | + End Sub |
| 156 | + |
| 157 | + Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged |
| 158 | + |
| 159 | +#End Region |
| 160 | + |
| 161 | + Friend Sub New() |
| 162 | + FormEnabled = True |
| 163 | + FormUseWaitCursor = False |
| 164 | + ImportPreviousSettigns = True |
| 165 | + BackupPreviousSettings = True |
| 166 | + |
| 167 | + AddHandler upgradeWorker.DoWork, AddressOf ProcessUpgradeWork |
| 168 | + AddHandler upgradeWorker.ProgressChanged, AddressOf UpgradeProgressChanged |
| 169 | + AddHandler upgradeWorker.RunWorkerCompleted, AddressOf UpgradeWorkComplete |
| 170 | + End Sub |
| 171 | + |
| 172 | + Friend Sub BeginUpgradeWorkAsync() |
| 173 | + FormEnabled = False |
| 174 | + FormUseWaitCursor = True |
| 175 | + upgradeWorker.RunWorkerAsync() |
| 176 | + End Sub |
| 177 | + |
| 178 | + Friend Sub CancelUpgradeWork() |
| 179 | + upgradeWorker.CancelAsync() |
| 180 | + End Sub |
| 181 | + |
| 182 | + Private Sub CalculateOKButtonState(Optional sender As Object = Nothing, Optional args As PropertyChangedEventArgs = Nothing) _ |
| 183 | + Handles Me.PropertyChanged |
| 184 | + |
| 185 | + If FormEnabled AndAlso ImportPreviousSettigns OrElse BackupPreviousSettings OrElse DeletePreviousSettings Then |
| 186 | + OKButtonEnabled = True |
| 187 | + Else |
| 188 | + OKButtonEnabled = False |
| 189 | + End If |
| 190 | + End Sub |
| 191 | + |
| 192 | +#Region "BackgroundWorker" |
| 193 | + ''' <summary> |
| 194 | + ''' Perform the work of <see cref="upgradeWorker"/> |
| 195 | + ''' </summary> |
| 196 | + ''' <param name="sender"></param> |
| 197 | + ''' <param name="e"></param> |
| 198 | + Private Sub ProcessUpgradeWork(sender As Object, e As DoWorkEventArgs) |
| 199 | + If Not e.Cancel AndAlso ImportPreviousSettigns Then |
| 200 | + DoImportWork() |
| 201 | + Thread.Sleep(2000) |
| 202 | + End If |
| 203 | + |
| 204 | + If Not e.Cancel AndAlso BackupPreviousSettings Then |
| 205 | + DoBackupWork() |
| 206 | + Thread.Sleep(2000) |
| 207 | + End If |
| 208 | + |
| 209 | + If Not e.Cancel AndAlso DeletePreviousSettings Then |
| 210 | + DoDeleteWork() |
| 211 | + Thread.Sleep(2000) |
| 212 | + End If |
| 213 | + End Sub |
| 214 | + |
| 215 | + Private Sub UpgradeWorkComplete(sender As Object, e As RunWorkerCompletedEventArgs) |
| 216 | + oldPrefs = Nothing |
| 217 | + |
| 218 | + If e.Cancelled Then |
| 219 | + LogFile.LogTracing("Upgrade work was cancelled.", LogLvl.LOG_WARNING, Me, My.Resources.UpgradePrefsDialog_Cancelled) |
| 220 | + End If |
| 221 | + |
| 222 | + If e.Error IsNot Nothing Then |
| 223 | + LogFile.LogTracing("Error encountered while doing upgrade work: " & e.Error.ToString(), LogLvl.LOG_ERROR, Me, |
| 224 | + String.Format(My.Resources.UpgradePrefsDialog_ErrorEncountered, e.Error.Message)) |
| 225 | + End If |
| 226 | + |
| 227 | + ProgressPercent = 100 |
| 228 | + |
| 229 | + RaiseEvent WorkComplete(sender, e) |
| 230 | + End Sub |
| 231 | + |
| 232 | + Private Sub ReportProgress(percentComplete As Integer, logMsg As String, logLevel As LogLvl, sender As Object, |
| 233 | + Optional logRes As String = Nothing) |
| 234 | + upgradeWorker.ReportProgress(percentComplete, |
| 235 | + New UpgradeWorkerProgressReport(logMsg, logLevel, sender, logRes)) |
| 236 | + End Sub |
| 237 | + |
| 238 | + Private Sub UpgradeProgressChanged(sender As Object, e As ProgressChangedEventArgs) |
| 239 | + Dim progReport As UpgradeWorkerProgressReport = CType(e.UserState, UpgradeWorkerProgressReport) |
| 240 | + LogFile.LogTracing(progReport.LogOutput, progReport.LogLevel, progReport.Sender, progReport.LogResourceString) |
| 241 | + ProgressPercent = e.ProgressPercentage |
| 242 | + End Sub |
| 243 | + |
| 244 | + Private Sub DoImportWork() |
| 245 | + ReportProgress(0, "Import operation beginning.", LogLvl.LOG_NOTICE, Me) |
| 246 | + oldPrefs = New OldParams.UpgradableParams() |
| 247 | + ReportProgress(100, "Old parameters loaded.", LogLvl.LOG_NOTICE, Me) |
| 248 | + |
| 249 | + Dim progress = 0 |
| 250 | + For Each section As KeyValuePair(Of String, Dictionary(Of String, Object)) In oldPrefs.Parameters |
| 251 | + For Each oldPref As KeyValuePair(Of String, Object) In section.Value |
| 252 | + Dim percentComplete = (progress / oldPrefs.TotalPrefs) * 100 |
| 253 | + Try |
| 254 | + Dim pairLookupRes = oldPrefs.PrefSettingsLookup.First(Function(p As OldParams.UpgradableParams.PrefSettingPair) |
| 255 | + Return p.OldPreferenceName.Equals(oldPref.Key) |
| 256 | + End Function) |
| 257 | + My.Settings.Item(pairLookupRes.NewSettingsName) = oldPref.Value |
| 258 | + ReportProgress(percentComplete, "Imported " & oldPref.Key & " into " & pairLookupRes.NewSettingsName, |
| 259 | + LogLvl.LOG_NOTICE, Me) |
| 260 | + |
| 261 | + ' Remove found entry since we no longer need it. |
| 262 | + oldPrefs.PrefSettingsLookup.Remove(pairLookupRes) |
| 263 | + progress += 1 |
| 264 | + Catch ex As Exception |
| 265 | + ReportProgress(percentComplete, String.Format("Error importing {0}:{2}{1}", |
| 266 | + oldPref.Key, vbNewLine, ex.ToString()), LogLvl.LOG_ERROR, Me) |
| 267 | + End Try |
| 268 | + Next |
| 269 | + Next |
| 270 | + |
| 271 | + |
| 272 | + Dim failedSettingsPairs = oldPrefs.PrefSettingsLookup |
| 273 | + |
| 274 | + If failedSettingsPairs.Count > 0 Then |
| 275 | + Dim unmatchedPairsList = String.Empty |
| 276 | + For Each failedPair In failedSettingsPairs |
| 277 | + unmatchedPairsList &= String.Format("[{0}, {1}]", failedPair.OldPreferenceName, failedPair.NewSettingsName) |
| 278 | + Next |
| 279 | + |
| 280 | + ReportProgress(95, String.Format("{0} unmatched settings pairs: {1}", failedSettingsPairs.Count, |
| 281 | + unmatchedPairsList), LogLvl.LOG_ERROR, Me, |
| 282 | + String.Format(My.Resources.UpgradePrefsDialog_UnmatchedPairs, failedSettingsPairs.Count)) |
| 283 | + End If |
| 284 | + |
| 285 | + ReportProgress(100, "Import procedure complete.", LogLvl.LOG_NOTICE, Me, |
| 286 | + My.Resources.UpgradePrefsDialog_ImportProcedureCompleted) |
| 287 | + End Sub |
| 288 | + |
| 289 | + ''' <summary> |
| 290 | + ''' Performs synchronous work to backup Registry data. Throws exceptions if any error occurs. |
| 291 | + ''' </summary> |
| 292 | + Private Sub DoBackupWork() |
| 293 | + ReportProgress(0, "Beginning backup procedure.", LogLvl.LOG_NOTICE, Me) |
| 294 | + |
| 295 | + Dim saveFileDialog = New SaveFileDialog() With { |
| 296 | + .DefaultExt = "reg", |
| 297 | + .FileName = "WinNUT-Prefs-Export", |
| 298 | + .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop), |
| 299 | + .Title = My.Resources.UpgradePrefsDialog_BackupLocationTitle |
| 300 | + } |
| 301 | + Dim dialogRes = saveFileDialog.ShowDialog() |
| 302 | + |
| 303 | + If dialogRes = DialogResult.Cancel Then |
| 304 | + Throw New InvalidOperationException("User opted to cancel the backup file dialog.") |
| 305 | + End If |
| 306 | + |
| 307 | + Dim fileLoc = Path.GetDirectoryName(saveFileDialog.FileName) & "\\" & saveFileDialog.FileName |
| 308 | + ReportProgress(50, "Beginning reg export process to " & fileLoc, LogLvl.LOG_NOTICE, Me) |
| 309 | + OldParams.WinNUT_Params.ExportParams(fileLoc) |
| 310 | + ReportProgress(100, "reg export process exited. Backup complete.", LogLvl.LOG_NOTICE, Me, |
| 311 | + String.Format(My.Resources.UpgradePrefsDialog_BackupProcedureCompleted, fileLoc)) |
| 312 | + End Sub |
| 313 | + |
| 314 | + Private Sub DoDeleteWork() |
| 315 | + ReportProgress(0, "Starting delete procedure.", LogLvl.LOG_NOTICE, Me) |
| 316 | + OldParams.WinNUT_Params.DeleteParams() |
| 317 | + ReportProgress(100, "Delete procedure completed successfully.", LogLvl.LOG_NOTICE, Me, |
| 318 | + My.Resources.UpgradePrefsDialog_DeleteProcedureComplete) |
| 319 | + End Sub |
| 320 | + |
| 321 | +#End Region |
| 322 | + |
| 323 | + Protected Class UpgradeWorkerProgressReport |
| 324 | + Public Property LogOutput As String |
| 325 | + Public Property LogLevel As LogLvl |
| 326 | + Public Property Sender As Object |
| 327 | + Public Property LogResourceString As String |
| 328 | + |
| 329 | + Public Sub New(logOutput As String, logLevel As LogLvl, sender As Object, Optional logRes As String = Nothing) |
| 330 | + Me.LogOutput = logOutput |
| 331 | + Me.LogLevel = logLevel |
| 332 | + Me.Sender = sender |
| 333 | + Me.LogResourceString = logRes |
| 334 | + End Sub |
| 335 | + End Class |
| 336 | + |
| 337 | + End Class |
| 338 | + |
| 339 | +End Namespace |
0 commit comments