mirror of
https://github.com/chylex/TweetDuck.git
synced 2024-11-13 05:42:48 +01:00
134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using CefSharp;
|
|
using System;
|
|
using System.Windows.Forms;
|
|
using TweetDuck.Core;
|
|
using TweetDuck.Core.Controls;
|
|
using TweetDuck.Core.Utils;
|
|
using TweetDuck.Resources;
|
|
|
|
namespace TweetDuck.Updates{
|
|
sealed class UpdateHandler{
|
|
private readonly ITweetDeckBrowser browser;
|
|
private readonly UpdaterSettings settings;
|
|
|
|
public event EventHandler<UpdateEventArgs> UpdateAccepted;
|
|
public event EventHandler<UpdateEventArgs> UpdateDismissed;
|
|
public event EventHandler<UpdateEventArgs> CheckFinished;
|
|
|
|
private int lastEventId;
|
|
private UpdateInfo lastUpdateInfo;
|
|
|
|
public UpdateHandler(ITweetDeckBrowser browser, UpdaterSettings settings){
|
|
this.browser = browser;
|
|
this.settings = settings;
|
|
|
|
browser.OnFrameLoaded(OnFrameLoaded);
|
|
browser.RegisterBridge("$TDU", new Bridge(this));
|
|
}
|
|
|
|
private void OnFrameLoaded(IFrame frame){
|
|
ScriptLoader.ExecuteFile(frame, "update.js");
|
|
Check(false);
|
|
}
|
|
|
|
public int Check(bool force){
|
|
if (Program.UserConfig.EnableUpdateCheck || force){
|
|
if (force){
|
|
settings.DismissedUpdate = null;
|
|
}
|
|
|
|
browser.ExecuteFunction("TDUF_runUpdateCheck", ++lastEventId, Program.VersionTag, settings.DismissedUpdate ?? string.Empty, settings.AllowPreReleases);
|
|
return lastEventId;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public void BeginUpdateDownload(Form ownerForm, UpdateInfo updateInfo, Action<UpdateInfo> onSuccess){
|
|
if (updateInfo.DownloadStatus == UpdateDownloadStatus.Done){
|
|
onSuccess(updateInfo);
|
|
}
|
|
else{
|
|
FormUpdateDownload downloadForm = new FormUpdateDownload(updateInfo);
|
|
|
|
downloadForm.VisibleChanged += (sender, args) => {
|
|
downloadForm.MoveToCenter(ownerForm);
|
|
ownerForm.Hide();
|
|
};
|
|
|
|
downloadForm.FormClosed += (sender, args) => {
|
|
downloadForm.Dispose();
|
|
|
|
if (downloadForm.DialogResult == DialogResult.OK){ // success or manual download
|
|
onSuccess(updateInfo);
|
|
}
|
|
else{
|
|
ownerForm.Show();
|
|
}
|
|
};
|
|
|
|
downloadForm.Show();
|
|
}
|
|
}
|
|
|
|
public void CleanupDownload(){
|
|
if (lastUpdateInfo != null){
|
|
lastUpdateInfo.DeleteInstaller();
|
|
lastUpdateInfo = null;
|
|
}
|
|
}
|
|
|
|
private void TriggerUpdateAcceptedEvent(UpdateEventArgs args){
|
|
UpdateAccepted?.Invoke(this, args);
|
|
}
|
|
|
|
private void TriggerUpdateDismissedEvent(UpdateEventArgs args){
|
|
settings.DismissedUpdate = args.UpdateInfo.VersionTag;
|
|
UpdateDismissed?.Invoke(this, args);
|
|
}
|
|
|
|
private void TriggerCheckFinishedEvent(UpdateEventArgs args){
|
|
CheckFinished?.Invoke(this, args);
|
|
}
|
|
|
|
public sealed class Bridge{
|
|
private readonly UpdateHandler owner;
|
|
|
|
public Bridge(UpdateHandler owner){
|
|
this.owner = owner;
|
|
}
|
|
|
|
public void TriggerUpdateCheck(){
|
|
owner.Check(false);
|
|
}
|
|
|
|
public void OnUpdateCheckFinished(int eventId, string versionTag, string downloadUrl){
|
|
if (versionTag != null && (owner.lastUpdateInfo == null || owner.lastUpdateInfo.VersionTag != versionTag)){
|
|
owner.CleanupDownload();
|
|
owner.lastUpdateInfo = new UpdateInfo(owner.settings, eventId, versionTag, downloadUrl);
|
|
owner.lastUpdateInfo.BeginSilentDownload();
|
|
}
|
|
|
|
owner.TriggerCheckFinishedEvent(new UpdateEventArgs(eventId, owner.lastUpdateInfo));
|
|
}
|
|
|
|
public void OnUpdateAccepted(){
|
|
if (owner.lastUpdateInfo != null){
|
|
owner.TriggerUpdateAcceptedEvent(new UpdateEventArgs(owner.lastUpdateInfo));
|
|
}
|
|
}
|
|
|
|
public void OnUpdateDismissed(){
|
|
if (owner.lastUpdateInfo != null){
|
|
owner.TriggerUpdateDismissedEvent(new UpdateEventArgs(owner.lastUpdateInfo));
|
|
owner.CleanupDownload();
|
|
}
|
|
}
|
|
|
|
public void OpenBrowser(string url){
|
|
BrowserUtils.OpenExternalBrowser(url);
|
|
}
|
|
}
|
|
}
|
|
}
|