1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-08 20:34:05 +02:00

Rewrite update handling and add an update check button to Settings

This commit is contained in:
chylex 2016-05-26 14:13:30 +02:00
parent 1162e9b655
commit 2a65e20fb9
14 changed files with 265 additions and 91 deletions

View File

@ -7,9 +7,9 @@
using TweetDck.Core.Handling;
using TweetDck.Core.Other;
using TweetDck.Resources;
using TweetDck.Core.Utils;
using TweetDck.Core.Controls;
using System.Drawing;
using TweetDck.Updates;
namespace TweetDck.Core{
sealed partial class FormBrowser : Form{
@ -24,6 +24,7 @@ private static UserConfig Config{
private readonly ChromiumWebBrowser browser;
private readonly TweetDeckBridge bridge;
private readonly FormNotification notification;
private readonly UpdateHandler updates;
private FormSettings currentFormSettings;
private FormAbout currentFormAbout;
@ -60,6 +61,9 @@ public FormBrowser(){
notification = CreateNotificationForm(true);
notification.CanMoveWindow = () => false;
notification.Show();
updates = new UpdateHandler(browser,this);
updates.UpdateAccepted += updates_UpdateAccepted;
}
private void ShowChildForm(Form form){
@ -110,7 +114,7 @@ private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEvent
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
if (e.Frame.IsMain){
foreach(string js in ScriptLoader.LoadResources("code.js","update.js").Where(js => js != null)){
foreach(string js in ScriptLoader.LoadResources("code.js").Where(js => js != null)){
browser.ExecuteScriptAsync(js);
}
}
@ -179,6 +183,25 @@ private void trayIcon_ClickClose(object sender, EventArgs e){
ForceClose();
}
private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){
Hide();
FormUpdateDownload downloadForm = new FormUpdateDownload(e.UpdateInfo);
downloadForm.MoveToCenter(this);
downloadForm.ShowDialog();
if (downloadForm.UpdateStatus == FormUpdateDownload.Status.Succeeded){
UpdateInstallerPath = downloadForm.InstallerPath;
ForceClose();
}
else if (downloadForm.UpdateStatus == FormUpdateDownload.Status.Manual){
ForceClose();
}
else{
Show();
}
}
protected override void WndProc(ref Message m){
if (isLoaded && m.Msg == 0x210 && (m.WParam.ToInt32() & 0xFFFF) == 0x020B){ // WM_PARENTNOTIFY, WM_XBUTTONDOWN
browser.ExecuteScriptAsync("TDGF_onMouseClickExtra",(m.WParam.ToInt32() >> 16) & 0xFFFF);
@ -197,7 +220,7 @@ public void OpenSettings(){
else{
bool prevEnableUpdateCheck = Config.EnableUpdateCheck;
currentFormSettings = new FormSettings(this);
currentFormSettings = new FormSettings(this,updates);
currentFormSettings.FormClosed += (sender, args) => {
currentFormSettings = null;
@ -205,8 +228,7 @@ public void OpenSettings(){
if (!prevEnableUpdateCheck && Config.EnableUpdateCheck){
Config.DismissedUpdate = string.Empty;
Config.Save();
browser.ExecuteScriptAsync("TDGF_runUpdateCheck",new object[0]);
updates.Check(false);
}
};
@ -256,24 +278,5 @@ public void OnImagePasted(){
public void OnImagePastedFinish(){
browser.ExecuteScriptAsync("TDGF_tryPasteImageFinish",new object[0]);
}
public void BeginUpdateProcess(string versionTag, string downloadUrl){
Hide();
FormUpdateDownload downloadForm = new FormUpdateDownload(new UpdateInfo(versionTag,downloadUrl));
downloadForm.MoveToCenter(this);
downloadForm.ShowDialog();
if (downloadForm.UpdateStatus == FormUpdateDownload.Status.Succeeded){
UpdateInstallerPath = downloadForm.InstallerPath;
ForceClose();
}
else if (downloadForm.UpdateStatus == FormUpdateDownload.Status.Manual){
ForceClose();
}
else{
Show();
}
}
}
}

View File

@ -32,24 +32,12 @@ public bool MuteNotifications{
}
}
public bool UpdateCheckEnabled{
get{
return Program.UserConfig.EnableUpdateCheck;
}
}
public bool ExpandLinksOnHover{
get{
return Program.UserConfig.ExpandLinksOnHover;
}
}
public string DismissedVersionTag{
get{
return Program.UserConfig.DismissedUpdate ?? string.Empty;
}
}
public TweetDeckBridge(FormBrowser form){
this.form = form;
}
@ -88,19 +76,6 @@ public void OnTweetSound(){
form.InvokeSafe(form.OnTweetSound);
}
public void OnUpdateAccepted(string versionTag, string downloadUrl){
form.InvokeSafe(() => {
form.BeginUpdateProcess(versionTag,downloadUrl);
});
}
public void OnUpdateDismissed(string versionTag){
form.InvokeSafe(() => {
Program.UserConfig.DismissedUpdate = versionTag;
Program.UserConfig.Save();
});
}
public void DisplayTooltip(string text, bool showInNotification){
form.InvokeSafe(() => {
form.DisplayTooltip(text,showInNotification);

View File

@ -53,6 +53,8 @@ private void InitializeComponent() {
this.labelMiscellaneous = new System.Windows.Forms.Label();
this.checkHardwareAcceleration = new System.Windows.Forms.CheckBox();
this.tableColumn1Panel = new System.Windows.Forms.Panel();
this.labelUpdateNotifications = new System.Windows.Forms.Label();
this.btnCheckUpdates = new System.Windows.Forms.Button();
this.groupNotificationLocation.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.trackBarEdgeDistance)).BeginInit();
this.tableLayout.SuspendLayout();
@ -80,7 +82,7 @@ private void InitializeComponent() {
this.groupNotificationLocation.Controls.Add(this.radioLocTL);
this.groupNotificationLocation.Location = new System.Drawing.Point(3, 3);
this.groupNotificationLocation.Name = "groupNotificationLocation";
this.groupNotificationLocation.Size = new System.Drawing.Size(183, 326);
this.groupNotificationLocation.Size = new System.Drawing.Size(183, 324);
this.groupNotificationLocation.TabIndex = 0;
this.groupNotificationLocation.TabStop = false;
this.groupNotificationLocation.Text = "Notification Location";
@ -88,7 +90,7 @@ private void InitializeComponent() {
// labelDisplay
//
this.labelDisplay.AutoSize = true;
this.labelDisplay.Location = new System.Drawing.Point(6, 148);
this.labelDisplay.Location = new System.Drawing.Point(3, 148);
this.labelDisplay.Margin = new System.Windows.Forms.Padding(3, 12, 3, 0);
this.labelDisplay.Name = "labelDisplay";
this.labelDisplay.Size = new System.Drawing.Size(41, 13);
@ -101,16 +103,16 @@ private void InitializeComponent() {
| System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxDisplay.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxDisplay.FormattingEnabled = true;
this.comboBoxDisplay.Location = new System.Drawing.Point(9, 164);
this.comboBoxDisplay.Location = new System.Drawing.Point(6, 164);
this.comboBoxDisplay.Name = "comboBoxDisplay";
this.comboBoxDisplay.Size = new System.Drawing.Size(168, 21);
this.comboBoxDisplay.Size = new System.Drawing.Size(171, 21);
this.comboBoxDisplay.TabIndex = 7;
this.comboBoxDisplay.SelectedValueChanged += new System.EventHandler(this.comboBoxDisplay_SelectedValueChanged);
//
// labelEdgeDistance
//
this.labelEdgeDistance.AutoSize = true;
this.labelEdgeDistance.Location = new System.Drawing.Point(6, 197);
this.labelEdgeDistance.Location = new System.Drawing.Point(3, 197);
this.labelEdgeDistance.Margin = new System.Windows.Forms.Padding(3, 9, 3, 0);
this.labelEdgeDistance.Name = "labelEdgeDistance";
this.labelEdgeDistance.Size = new System.Drawing.Size(103, 13);
@ -214,7 +216,7 @@ private void InitializeComponent() {
this.tableLayout.Padding = new System.Windows.Forms.Padding(3);
this.tableLayout.RowCount = 1;
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Size = new System.Drawing.Size(576, 338);
this.tableLayout.Size = new System.Drawing.Size(576, 336);
this.tableLayout.TabIndex = 2;
//
// tableColumn2Panel
@ -225,7 +227,7 @@ private void InitializeComponent() {
this.tableColumn2Panel.Location = new System.Drawing.Point(192, 3);
this.tableColumn2Panel.Margin = new System.Windows.Forms.Padding(0);
this.tableColumn2Panel.Name = "tableColumn2Panel";
this.tableColumn2Panel.Size = new System.Drawing.Size(189, 332);
this.tableColumn2Panel.Size = new System.Drawing.Size(189, 330);
this.tableColumn2Panel.TabIndex = 3;
//
// groupUserInterface
@ -233,6 +235,8 @@ private void InitializeComponent() {
this.groupUserInterface.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupUserInterface.Controls.Add(this.btnCheckUpdates);
this.groupUserInterface.Controls.Add(this.labelUpdateNotifications);
this.groupUserInterface.Controls.Add(this.checkExpandLinks);
this.groupUserInterface.Controls.Add(this.comboBoxTrayType);
this.groupUserInterface.Controls.Add(this.labelTrayType);
@ -240,7 +244,7 @@ private void InitializeComponent() {
this.groupUserInterface.Controls.Add(this.checkNotificationTimer);
this.groupUserInterface.Location = new System.Drawing.Point(3, 128);
this.groupUserInterface.Name = "groupUserInterface";
this.groupUserInterface.Size = new System.Drawing.Size(183, 201);
this.groupUserInterface.Size = new System.Drawing.Size(183, 199);
this.groupUserInterface.TabIndex = 3;
this.groupUserInterface.TabStop = false;
this.groupUserInterface.Text = "User Interface";
@ -263,17 +267,16 @@ private void InitializeComponent() {
| System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxTrayType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxTrayType.FormattingEnabled = true;
this.comboBoxTrayType.Location = new System.Drawing.Point(9, 116);
this.comboBoxTrayType.Margin = new System.Windows.Forms.Padding(3, 3, 3, 12);
this.comboBoxTrayType.Location = new System.Drawing.Point(6, 92);
this.comboBoxTrayType.Name = "comboBoxTrayType";
this.comboBoxTrayType.Size = new System.Drawing.Size(168, 21);
this.comboBoxTrayType.Size = new System.Drawing.Size(171, 21);
this.comboBoxTrayType.TabIndex = 10;
this.comboBoxTrayType.SelectedIndexChanged += new System.EventHandler(this.comboBoxTrayType_SelectedIndexChanged);
//
// labelTrayType
//
this.labelTrayType.AutoSize = true;
this.labelTrayType.Location = new System.Drawing.Point(6, 100);
this.labelTrayType.Location = new System.Drawing.Point(3, 76);
this.labelTrayType.Margin = new System.Windows.Forms.Padding(3, 11, 3, 0);
this.labelTrayType.Name = "labelTrayType";
this.labelTrayType.Size = new System.Drawing.Size(52, 13);
@ -283,12 +286,12 @@ private void InitializeComponent() {
// checkUpdateNotifications
//
this.checkUpdateNotifications.AutoSize = true;
this.checkUpdateNotifications.Location = new System.Drawing.Point(6, 69);
this.checkUpdateNotifications.Margin = new System.Windows.Forms.Padding(3, 4, 3, 3);
this.checkUpdateNotifications.Location = new System.Drawing.Point(6, 145);
this.checkUpdateNotifications.Margin = new System.Windows.Forms.Padding(3, 5, 3, 3);
this.checkUpdateNotifications.Name = "checkUpdateNotifications";
this.checkUpdateNotifications.Size = new System.Drawing.Size(115, 17);
this.checkUpdateNotifications.Size = new System.Drawing.Size(165, 17);
this.checkUpdateNotifications.TabIndex = 5;
this.checkUpdateNotifications.Text = "Check for Updates";
this.checkUpdateNotifications.Text = "Check Updates Automatically";
this.checkUpdateNotifications.UseVisualStyleBackColor = true;
this.checkUpdateNotifications.CheckedChanged += new System.EventHandler(this.checkUpdateNotifications_CheckedChanged);
//
@ -378,7 +381,7 @@ private void InitializeComponent() {
this.tableColumn3Panel.Location = new System.Drawing.Point(381, 3);
this.tableColumn3Panel.Margin = new System.Windows.Forms.Padding(0);
this.tableColumn3Panel.Name = "tableColumn3Panel";
this.tableColumn3Panel.Size = new System.Drawing.Size(192, 332);
this.tableColumn3Panel.Size = new System.Drawing.Size(192, 330);
this.tableColumn3Panel.TabIndex = 4;
//
// groupAdvancedSettings
@ -391,7 +394,7 @@ private void InitializeComponent() {
this.groupAdvancedSettings.Controls.Add(this.checkHardwareAcceleration);
this.groupAdvancedSettings.Location = new System.Drawing.Point(3, 3);
this.groupAdvancedSettings.Name = "groupAdvancedSettings";
this.groupAdvancedSettings.Size = new System.Drawing.Size(183, 326);
this.groupAdvancedSettings.Size = new System.Drawing.Size(183, 324);
this.groupAdvancedSettings.TabIndex = 0;
this.groupAdvancedSettings.TabStop = false;
this.groupAdvancedSettings.Text = "Advanced Settings";
@ -435,14 +438,34 @@ private void InitializeComponent() {
this.tableColumn1Panel.Location = new System.Drawing.Point(3, 3);
this.tableColumn1Panel.Margin = new System.Windows.Forms.Padding(0);
this.tableColumn1Panel.Name = "tableColumn1Panel";
this.tableColumn1Panel.Size = new System.Drawing.Size(189, 332);
this.tableColumn1Panel.Size = new System.Drawing.Size(189, 330);
this.tableColumn1Panel.TabIndex = 5;
//
// labelUpdateNotifications
//
this.labelUpdateNotifications.AutoSize = true;
this.labelUpdateNotifications.Location = new System.Drawing.Point(3, 127);
this.labelUpdateNotifications.Margin = new System.Windows.Forms.Padding(3, 11, 3, 0);
this.labelUpdateNotifications.Name = "labelUpdateNotifications";
this.labelUpdateNotifications.Size = new System.Drawing.Size(103, 13);
this.labelUpdateNotifications.TabIndex = 12;
this.labelUpdateNotifications.Text = "Update Notifications";
//
// btnCheckUpdates
//
this.btnCheckUpdates.Location = new System.Drawing.Point(6, 168);
this.btnCheckUpdates.Name = "btnCheckUpdates";
this.btnCheckUpdates.Size = new System.Drawing.Size(171, 23);
this.btnCheckUpdates.TabIndex = 13;
this.btnCheckUpdates.Text = "Check Updates Now";
this.btnCheckUpdates.UseVisualStyleBackColor = true;
this.btnCheckUpdates.Click += new System.EventHandler(this.btnCheckUpdates_Click);
//
// FormSettings
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(576, 338);
this.ClientSize = new System.Drawing.Size(576, 336);
this.Controls.Add(this.tableLayout);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
@ -499,5 +522,7 @@ private void InitializeComponent() {
private System.Windows.Forms.Label labelMiscellaneous;
private System.Windows.Forms.Button btnClearCache;
private System.Windows.Forms.Panel tableColumn1Panel;
private System.Windows.Forms.Label labelUpdateNotifications;
private System.Windows.Forms.Button btnCheckUpdates;
}
}

View File

@ -5,6 +5,7 @@
using TweetDck.Core.Handling;
using TweetDck.Core.Utils;
using TweetDck.Core.Controls;
using TweetDck.Updates;
namespace TweetDck.Core.Other{
sealed partial class FormSettings : Form{
@ -15,14 +16,22 @@ private static UserConfig Config{
}
private readonly FormNotification notification;
private bool isLoaded;
private readonly UpdateHandler updates;
public FormSettings(FormBrowser browserForm){
private bool isLoaded;
private int updateCheckEventId;
public FormSettings(FormBrowser browserForm, UpdateHandler updates){
InitializeComponent();
Shown += (sender, args) => isLoaded = true;
Text = Program.BrandName+" Settings";
this.updates = updates;
updates.CheckFinished += updates_CheckFinished;
Disposed += (sender, args) => updates.CheckFinished -= updates_CheckFinished;
notification = browserForm.CreateNotificationForm(false);
notification.CanMoveWindow = () => radioLocCustom.Checked;
@ -203,5 +212,28 @@ private void btnClearCache_Click(object sender, EventArgs e){
MessageBox.Show("Cache will be automatically cleared when "+Program.BrandName+" exits.","Clear Cache",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void btnCheckUpdates_Click(object sender, EventArgs e){
if (!isLoaded)return;
Config.DismissedUpdate = string.Empty;
Config.Save();
updateCheckEventId = updates.Check(true);
isLoaded = false;
btnCheckUpdates.Enabled = false;
isLoaded = true; // SAME AS ABOVE
}
private void updates_CheckFinished(object sender, UpdateCheckEventArgs e){
if (e.EventId == updateCheckEventId){
btnCheckUpdates.Enabled = true;
if (!e.UpdateAvailable){
MessageBox.Show("Your version of "+Program.BrandName+" is up to date.","No Updates Available",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
}
}

View File

@ -16,8 +16,8 @@ public static string LoadResource(string name){
}
}
public static IList<string> LoadResources(params string[] names){
return names.Select(LoadResource).ToList();
public static IEnumerable<string> LoadResources(params string[] names){
return names.Select(LoadResource);
}
}
}

View File

@ -1,4 +1,4 @@
(function($,$TD){
(function($,$TDU){
//
// Variable: Current timeout ID for update checking.
//
@ -7,7 +7,12 @@
//
// Constant: Update exe file name.
//
const updateFileName = $TD.brandName+".Update.exe";
const updateFileName = $TDU.brandName+".Update.exe";
//
// Constant: Url that returns JSON data about latest version.
//
const updateCheckUrl = "https://api.github.com/repos/chylex/"+$TDU.brandName+"/releases/latest";
//
// Function: Creates the update notification element. Removes the old one if already exists.
@ -22,7 +27,7 @@
var html = [
"<div id='tweetdck-update'>",
"<p class='tdu-title'>"+$TD.brandName+" Update</p>",
"<p class='tdu-title'>"+$TDU.brandName+" Update</p>",
"<p class='tdu-info'>Version "+version+" is now available.</p>",
"<div class='tdu-buttons'>",
"<button class='btn btn-positive tdu-btn-download'><span class='label'>Download</button>",
@ -85,11 +90,11 @@
buttonDiv.children(".tdu-btn-download").click(function(){
ele.remove();
$TD.onUpdateAccepted(version,download);
$TDU.onUpdateAccepted(version,download);
});
buttonDiv.children(".tdu-btn-dismiss").click(function(){
$TD.onUpdateDismissed(version);
$TDU.onUpdateDismissed(version);
ele.slideUp(function(){ ele.remove(); });
});
@ -103,25 +108,30 @@
//
// Function: Runs an update check and updates all DOM elements appropriately.
//
var runUpdateCheck = function(){
var runUpdateCheck = function(force, eventID){
clearTimeout(updateCheckTimeoutID);
updateCheckTimeoutID = setTimeout(runUpdateCheck,1000*60*60); // 1 hour
if (!$TD.updateCheckEnabled)return;
if (!$TDU.updateCheckEnabled && !force)return;
$.getJSON("https://api.github.com/repos/chylex/"+$TD.brandName+"/releases/latest",function(response){
$.getJSON(updateCheckUrl,function(response){
var tagName = response.tag_name;
var hasUpdate = tagName !== $TDU.versionTag && tagName !== $TDU.dismissedVersionTag && response.assets.length > 0;
if (tagName !== $TD.versionTag && tagName !== $TD.dismissedVersionTag && response.assets.length > 0){
if (hasUpdate){
var obj = response.assets.find(asset => asset.name === updateFileName) || response.assets[0];
createUpdateNotificationElement(tagName,obj.browser_download_url);
}
if (eventID !== 0){
$TDU.onUpdateCheckFinished(eventID,hasUpdate,tagName);
}
});
};
//
// Block: Setup global functions.
//
window.TDGF_runUpdateCheck = runUpdateCheck;
window.TDUF_runUpdateCheck = runUpdateCheck;
runUpdateCheck();
})($,$TD);
})($,$TDU);

View File

@ -126,10 +126,10 @@
<Compile Include="Core\Other\FormSettings.Designer.cs">
<DependentUpon>FormSettings.cs</DependentUpon>
</Compile>
<Compile Include="Core\Other\FormUpdateDownload.cs">
<Compile Include="Updates\FormUpdateDownload.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Core\Other\FormUpdateDownload.Designer.cs">
<Compile Include="Updates\FormUpdateDownload.Designer.cs">
<DependentUpon>FormUpdateDownload.cs</DependentUpon>
</Compile>
<Compile Include="Core\TrayIcon.cs">
@ -142,7 +142,10 @@
<Compile Include="Core\Utils\BrowserUtils.cs" />
<Compile Include="Core\Utils\HardwareAcceleration.cs" />
<Compile Include="Core\Utils\NativeMethods.cs" />
<Compile Include="Core\Utils\UpdateInfo.cs" />
<Compile Include="Updates\UpdateAcceptedEventArgs.cs" />
<Compile Include="Updates\UpdateCheckEventArgs.cs" />
<Compile Include="Updates\UpdateHandler.cs" />
<Compile Include="Updates\UpdateInfo.cs" />
<Compile Include="Migration\FormMigrationQuestion.cs">
<SubType>Form</SubType>
</Compile>
@ -219,7 +222,7 @@
<EmbeddedResource Include="Core\Other\FormSettings.resx">
<DependentUpon>FormSettings.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Core\Other\FormUpdateDownload.resx">
<EmbeddedResource Include="Updates\FormUpdateDownload.resx">
<DependentUpon>FormUpdateDownload.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">

View File

@ -1,4 +1,4 @@
namespace TweetDck.Core.Other {
namespace TweetDck.Updates {
partial class FormUpdateDownload {
/// <summary>
/// Required designer variable.

View File

@ -6,7 +6,7 @@
using TweetDck.Core.Controls;
using TweetDck.Core.Utils;
namespace TweetDck.Core.Other{
namespace TweetDck.Updates{
sealed partial class FormUpdateDownload : Form{
public string InstallerPath{
get{

View File

@ -0,0 +1,11 @@
using System;
namespace TweetDck.Updates{
class UpdateAcceptedEventArgs : EventArgs{
public readonly UpdateInfo UpdateInfo;
public UpdateAcceptedEventArgs(UpdateInfo info){
this.UpdateInfo = info;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
namespace TweetDck.Updates{
class UpdateCheckEventArgs : EventArgs{
public int EventId { get; private set; }
public bool UpdateAvailable { get; private set; }
public string LatestVersion { get; private set; }
public UpdateCheckEventArgs(int eventId, bool updateAvailable, string latestVersion){
EventId = eventId;
UpdateAvailable = updateAvailable;
LatestVersion = latestVersion;
}
}
}

98
Updates/UpdateHandler.cs Normal file
View File

@ -0,0 +1,98 @@
using System;
using System.Linq;
using CefSharp;
using CefSharp.WinForms;
using TweetDck.Core;
using TweetDck.Core.Controls;
using TweetDck.Resources;
namespace TweetDck.Updates{
class UpdateHandler{
private readonly ChromiumWebBrowser browser;
private readonly FormBrowser form;
public event EventHandler<UpdateAcceptedEventArgs> UpdateAccepted;
public event EventHandler<UpdateCheckEventArgs> CheckFinished;
private int lastEventId;
public UpdateHandler(ChromiumWebBrowser browser, FormBrowser form){
this.browser = browser;
this.form = form;
browser.FrameLoadEnd += browser_FrameLoadEnd;
browser.RegisterJsObject("$TDU",new Bridge(this));
}
private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
if (e.Frame.IsMain){
foreach(string js in ScriptLoader.LoadResources("update.js").Where(js => js != null)){
browser.ExecuteScriptAsync(js);
}
}
}
public int Check(bool force){
browser.ExecuteScriptAsync("TDUF_runUpdateCheck",force,++lastEventId);
return lastEventId;
}
private void TriggerUpdateAcceptedEvent(UpdateAcceptedEventArgs args){
if (UpdateAccepted != null){
form.InvokeSafe(() => UpdateAccepted(this,args));
}
}
private void TriggerCheckFinishedEvent(UpdateCheckEventArgs args){
if (CheckFinished != null){
form.InvokeSafe(() => CheckFinished(this,args));
}
}
public class Bridge{
public string BrandName{
get{
return Program.BrandName;
}
}
public string VersionTag{
get{
return Program.VersionTag;
}
}
public bool UpdateCheckEnabled{
get{
return Program.UserConfig.EnableUpdateCheck;
}
}
public string DismissedVersionTag{
get{
return Program.UserConfig.DismissedUpdate ?? string.Empty;
}
}
private readonly UpdateHandler owner;
public Bridge(UpdateHandler owner){
this.owner = owner;
}
public void OnUpdateCheckFinished(int eventId, bool isUpdateAvailable, string latestVersion){
owner.TriggerCheckFinishedEvent(new UpdateCheckEventArgs(eventId,isUpdateAvailable,latestVersion));
}
public void OnUpdateAccepted(string versionTag, string downloadUrl){
owner.TriggerUpdateAcceptedEvent(new UpdateAcceptedEventArgs(new UpdateInfo(versionTag,downloadUrl)));
}
public void OnUpdateDismissed(string versionTag){
owner.form.InvokeSafe(() => {
Program.UserConfig.DismissedUpdate = versionTag;
Program.UserConfig.Save();
});
}
}
}
}

View File

@ -1,4 +1,6 @@
namespace TweetDck.Core.Utils{
using TweetDck.Core.Utils;
namespace TweetDck.Updates{
class UpdateInfo{
public readonly string VersionTag;
public readonly string DownloadUrl;