11using System ;
22using System . Collections . Generic ;
3+ using System . Data . SQLite ;
34using System . Linq ;
45using System . Text ;
56using System . ComponentModel ;
67using System . Xml . Linq ;
78using System . IO ;
89using System . Net ;
910using System . Windows ;
11+ using VidCoder . Model ;
1012using VidCoder . Properties ;
1113using System . Diagnostics ;
1214using Microsoft . Practices . Unity ;
@@ -15,6 +17,11 @@ namespace VidCoder.Services
1517{
1618 public class Updater : IUpdater
1719 {
20+ public const string UpdateInProgress = "UpdateInProgress" ;
21+ public const string UpdateVersion = "UpdateVersion" ;
22+ public const string UpdateInstallerLocation = "UpdateInstallerLocation" ;
23+ public const string UpdateChangelogLocation = "UpdateChangelogLocation" ;
24+
1825 public event EventHandler < EventArgs < double > > UpdateDownloadProgress ;
1926 public event EventHandler < EventArgs > UpdateDownloadCompleted ;
2027
@@ -30,6 +37,7 @@ private static bool DebugMode
3037 }
3138 }
3239
40+ private ILogger logger = Unity . Container . Resolve < ILogger > ( ) ;
3341 private BackgroundWorker updateDownloader ;
3442 private bool processDownloadsUpdates = true ;
3543
@@ -44,9 +52,9 @@ public void PromptToApplyUpdate()
4452 // If updates are enabled, and we are the last process instance, prompt to apply the update.
4553 if ( Settings . Default . UpdatesEnabled && Utilities . CurrentProcessInstances == 1 )
4654 {
47- string installerPath = Settings . Default . UpdateInstallerLocation ;
55+ string installerPath = DatabaseConfig . GetConfigString ( UpdateInstallerLocation , Database . Connection ) ;
4856
49- if ( installerPath != string . Empty )
57+ if ( installerPath != string . Empty && File . Exists ( installerPath ) )
5058 {
5159 // An update is ready, to give a prompt to apply it.
5260 var updateConfirmation = new ApplyUpdateConfirmation ( ) ;
@@ -79,10 +87,9 @@ public void ApplyUpdate()
7987 // Re-check the process count in case another one was opened while the prompt was active.
8088 if ( Utilities . CurrentProcessInstances == 1 )
8189 {
82- string installerPath = Settings . Default . UpdateInstallerLocation ;
90+ string installerPath = DatabaseConfig . GetConfigString ( UpdateInstallerLocation , Database . Connection ) ;
8391
84- Settings . Default . UpdateInProgress = true ;
85- Settings . Default . Save ( ) ;
92+ DatabaseConfig . SetConfigValue ( UpdateInProgress , true , Database . Connection ) ;
8693
8794 var installerProcess = new Process ( ) ;
8895 installerProcess . StartInfo = new ProcessStartInfo { FileName = installerPath , Arguments = "/silent /noicons" } ;
@@ -100,7 +107,7 @@ public void HandleUpdatedSettings(bool updatesEnabled)
100107 if ( updatesEnabled )
101108 {
102109 // If we don't already have an update waiting to install, check for updates.
103- if ( processDownloadsUpdates && Settings . Default . UpdateInstallerLocation == string . Empty )
110+ if ( processDownloadsUpdates && DatabaseConfig . GetConfigString ( UpdateInstallerLocation , Database . Connection ) == string . Empty )
104111 {
105112 this . StartBackgroundUpdate ( ) ;
106113 }
@@ -115,17 +122,29 @@ public void HandleUpdatedSettings(bool updatesEnabled)
115122
116123 public void HandlePendingUpdate ( )
117124 {
118- bool updateInProgress = Settings . Default . UpdateInProgress ;
119- string targetUpdateVersion = Settings . Default . UpdateVersion ;
120-
125+ // This flag signifies VidCoder is being run by the installer after an update.
126+ // In this case we report success, delete the installer, clean up the update flags and exit.
127+ bool updateInProgress = DatabaseConfig . GetConfigBool ( UpdateInProgress , Database . Connection ) ;
121128 if ( updateInProgress )
122129 {
123- Settings . Default . UpdateInProgress = false ;
124- Settings . Default . Save ( ) ;
130+ string targetUpdateVersion = DatabaseConfig . GetConfigString ( UpdateVersion , Database . Connection ) ;
131+ bool updateSucceeded = Utilities . CompareVersions ( targetUpdateVersion , Utilities . CurrentVersion ) == 0 ;
132+
133+ using ( SQLiteTransaction transaction = Database . Connection . BeginTransaction ( ) )
134+ {
135+ DatabaseConfig . SetConfigValue ( UpdateInProgress , false , Database . Connection ) ;
136+
137+ if ( updateSucceeded )
138+ {
139+ DatabaseConfig . SetConfigValue ( UpdateVersion , string . Empty , Database . Connection ) ;
140+ DatabaseConfig . SetConfigValue ( UpdateInstallerLocation , string . Empty , Database . Connection ) ;
141+ DatabaseConfig . SetConfigValue ( UpdateChangelogLocation , string . Empty , Database . Connection ) ;
142+ }
143+
144+ transaction . Commit ( ) ;
145+ }
125146
126- // This flag signifies VidCoder is being run by the installer after an update.
127- // In this case we report success, delete the installer, clean up the update flags and exit.
128- if ( Utilities . CompareVersions ( targetUpdateVersion , Utilities . CurrentVersion ) == 0 )
147+ if ( updateSucceeded )
129148 {
130149 MessageBox . Show ( "VidCoder has been successfully updated." ) ;
131150
@@ -134,18 +153,15 @@ public void HandlePendingUpdate()
134153 Directory . Delete ( Utilities . UpdatesFolder , true ) ;
135154 }
136155
137- Settings . Default . UpdateVersion = string . Empty ;
138- Settings . Default . UpdateInstallerLocation = string . Empty ;
139- Settings . Default . UpdateChangelogLocation = string . Empty ;
140- Settings . Default . Save ( ) ;
141-
142156 Environment . Exit ( 0 ) ;
143157 }
144-
145- // If the target version is different from the currently running version,
146- // this means the attempted upgrade failed. We give an error message but
147- // continue with the program.
148- MessageBox . Show ( "The update was not applied. If you did not cancel it, try installing it manually." ) ;
158+ else
159+ {
160+ // If the target version is different from the currently running version,
161+ // this means the attempted upgrade failed. We give an error message but
162+ // continue with the program.
163+ MessageBox . Show ( "The update was not applied. If you did not cancel it, try installing it manually." ) ;
164+ }
149165 }
150166 }
151167
@@ -164,8 +180,7 @@ public void CheckUpdates()
164180 if ( ! Settings . Default . UpdatesEnabled )
165181 {
166182 // On a program restart, if updates are disabled, clean any pending installers.
167- Settings . Default . UpdateInstallerLocation = string . Empty ;
168- Settings . Default . Save ( ) ;
183+ DatabaseConfig . SetConfigValue ( UpdateInstallerLocation , string . Empty , Database . Connection ) ;
169184
170185 if ( Directory . Exists ( Utilities . UpdatesFolder ) )
171186 {
@@ -200,6 +215,7 @@ private void StartBackgroundUpdate()
200215 private void CheckAndDownloadUpdate ( object sender , DoWorkEventArgs e )
201216 {
202217 var updateDownloader = sender as BackgroundWorker ;
218+ SQLiteConnection connection = Database . CreateConnection ( ) ;
203219
204220 try
205221 {
@@ -235,9 +251,28 @@ private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
235251
236252 if ( Utilities . CompareVersions ( updateVersion , Utilities . CurrentVersion ) > 0 )
237253 {
254+ // If an update is reported to be ready but the installer doesn't exist, clear out all the
255+ // installer info and redownload.
256+ string updateInstallerLocation = DatabaseConfig . GetConfigString ( UpdateInstallerLocation , connection ) ;
257+ if ( updateInstallerLocation != string . Empty && ! File . Exists ( updateInstallerLocation ) )
258+ {
259+ using ( SQLiteTransaction transaction = connection . BeginTransaction ( ) )
260+ {
261+ DatabaseConfig . SetConfigValue ( UpdateVersion , string . Empty , Database . Connection ) ;
262+ DatabaseConfig . SetConfigValue ( UpdateInstallerLocation , string . Empty , Database . Connection ) ;
263+ DatabaseConfig . SetConfigValue ( UpdateChangelogLocation , string . Empty , Database . Connection ) ;
264+
265+ transaction . Commit ( ) ;
266+ }
267+
268+ this . logger . Log ( "Downloaded update (" + updateInstallerLocation + ") could not be found. Re-downloading it." ) ;
269+ }
270+
238271 // If we have not finished the download update yet, start/resume the download.
239- if ( Settings . Default . UpdateInstallerLocation == string . Empty )
272+ if ( DatabaseConfig . GetConfigString ( UpdateInstallerLocation , connection ) == string . Empty )
240273 {
274+ this . logger . Log ( "Started downloading update " + updateVersion ) ;
275+
241276 string downloadLocation = downloadElement . Value ;
242277 string changelogLink = changelogLinkElement . Value ;
243278 string installerFileName = Path . GetFileName ( downloadLocation ) ;
@@ -269,7 +304,7 @@ private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
269304 responseStream = response . GetResponseStream ( ) ;
270305
271306 byte [ ] downloadBuffer = new byte [ 2048 ] ;
272- int bytesRead = 0 ;
307+ int bytesRead ;
273308
274309 while ( ( bytesRead = responseStream . Read ( downloadBuffer , 0 , downloadBuffer . Length ) ) > 0 && ! updateDownloader . CancellationPending )
275310 {
@@ -285,12 +320,18 @@ private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
285320
286321 if ( bytesRead == 0 )
287322 {
288- Settings . Default . UpdateVersion = updateVersion ;
289- Settings . Default . UpdateInstallerLocation = installerFilePath ;
290- Settings . Default . UpdateChangelogLocation = changelogLink ;
291- Settings . Default . Save ( ) ;
323+ using ( SQLiteTransaction transaction = connection . BeginTransaction ( ) )
324+ {
325+ DatabaseConfig . SetConfigValue ( UpdateVersion , updateVersion , connection ) ;
326+ DatabaseConfig . SetConfigValue ( UpdateInstallerLocation , installerFilePath , connection ) ;
327+ DatabaseConfig . SetConfigValue ( UpdateChangelogLocation , changelogLink , connection ) ;
328+
329+ transaction . Commit ( ) ;
330+ }
292331
293332 this . UpdateReady = true ;
333+
334+ this . logger . Log ( "Update " + updateVersion + " has finished downloading and will install on exit." ) ;
294335 }
295336 }
296337 finally
0 commit comments