mattercontrol/original/MatterControlLib/DialogPages/UpdateControlView.cs

192 lines
6.2 KiB
C#
Raw Normal View History

/*
Copyright (c) 2017, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
2015-04-08 15:20:10 -07:00
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
2021-05-21 15:23:25 -07:00
using MatterHackers.ImageProcessing;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl
{
2015-04-08 15:20:10 -07:00
public class UpdateControlView : FlowLayoutWidget
{
private GuiWidget downloadButton;
private GuiWidget checkUpdateButton;
private GuiWidget installButton;
2015-04-08 15:20:10 -07:00
private TextWidget updateStatusText;
private EventHandler unregisterEvents;
2015-04-08 15:20:10 -07:00
public UpdateControlView(ThemeConfig theme)
2015-04-08 15:20:10 -07:00
{
this.HAnchor = HAnchor.Stretch;
this.BackgroundColor = theme.MinimalShade;
this.Padding = theme.ToolbarPadding.Clone(left: 8);
2015-04-08 15:20:10 -07:00
this.AddChild(updateStatusText = new TextWidget(string.Format(""), textColor: theme.TextColor)
{
AutoExpandBoundsToText = true,
VAnchor = VAnchor.Center
});
2017-06-20 06:15:19 -07:00
this.AddChild(new HorizontalSpacer());
2017-06-20 06:12:57 -07:00
checkUpdateButton = new ThemedIconButton(StaticData.Instance.LoadIcon("fa-refresh_14.png", 14, 14).GrayToColor(theme.TextColor), theme)
{
ToolTipText = "Check for Update".Localize(),
BackgroundColor = theme.MinimalShade,
Cursor = Cursors.Hand,
Visible = false
};
checkUpdateButton.Click += (s, e) =>
2017-06-20 06:12:57 -07:00
{
2018-01-13 13:27:34 -08:00
UpdateControlData.Instance.CheckForUpdate();
2017-06-20 06:12:57 -07:00
};
this.AddChild(checkUpdateButton);
2017-06-20 06:12:57 -07:00
this.MinimumSize = new Vector2(0, checkUpdateButton.Height);
downloadButton = new ThemedTextButton("Download Update".Localize(), theme)
{
BackgroundColor = theme.MinimalShade,
Visible = false
};
downloadButton.Click += (s, e) =>
2015-04-08 15:20:10 -07:00
{
downloadButton.Visible = false;
updateStatusText.Text = "Retrieving download info...".Localize();
2015-04-08 15:20:10 -07:00
2017-06-20 06:12:57 -07:00
UpdateControlData.Instance.InitiateUpdateDownload();
};
this.AddChild(downloadButton);
2015-04-08 15:20:10 -07:00
installButton = new ThemedTextButton("Install Update".Localize(), theme)
{
BackgroundColor = theme.MinimalShade,
Visible = false
};
installButton.Click += (s, e) =>
2017-06-20 06:12:57 -07:00
{
try
{
2017-06-20 06:12:57 -07:00
if (!UpdateControlData.Instance.InstallUpdate())
{
installButton.Visible = false;
updateStatusText.Text = "Oops! Unable to install update.".Localize();
}
2017-06-20 06:12:57 -07:00
}
catch
{
GuiWidget.BreakInDebugger();
installButton.Visible = false;
updateStatusText.Text = "Oops! Unable to install update.".Localize();
2017-06-20 06:12:57 -07:00
}
};
this.AddChild(installButton);
2015-04-08 15:20:10 -07:00
UpdateControlData.Instance.UpdateStatusChanged.RegisterEvent(UpdateStatusChanged, ref unregisterEvents);
2017-06-20 06:12:57 -07:00
this.UpdateStatusChanged(null, null);
2015-04-08 15:20:10 -07:00
}
public override void OnClosed(EventArgs e)
2015-04-08 15:20:10 -07:00
{
2017-01-23 14:03:04 -08:00
unregisterEvents?.Invoke(this, null);
2015-04-08 15:20:10 -07:00
base.OnClosed(e);
}
string recommendedUpdateAvailable = "There is a recommended update available".Localize();
string requiredUpdateAvailable = "There is a required update available".Localize();
2015-04-08 15:20:10 -07:00
private void UpdateStatusChanged(object sender, EventArgs e)
{
switch (UpdateControlData.Instance.UpdateStatus)
{
case UpdateControlData.UpdateStatusStates.MayBeAvailable:
updateStatusText.Text = "New updates may be available".Localize();
checkUpdateButton.Visible = true;
2015-04-08 15:20:10 -07:00
break;
case UpdateControlData.UpdateStatusStates.CheckingForUpdate:
updateStatusText.Text = "Checking for updates...".Localize();
//checkUpdateLink.Visible = false;
2015-04-08 15:20:10 -07:00
break;
case UpdateControlData.UpdateStatusStates.UnableToConnectToServer:
updateStatusText.Text = "Oops! Unable to connect to server".Localize();
downloadButton.Visible = false;
installButton.Visible = false;
checkUpdateButton.Visible = true;
2015-04-08 15:20:10 -07:00
break;
case UpdateControlData.UpdateStatusStates.UpdateAvailable:
if (UpdateControlData.Instance.UpdateRequired)
{
updateStatusText.Text = requiredUpdateAvailable;
}
else
{
updateStatusText.Text = recommendedUpdateAvailable;
}
downloadButton.Visible = true;
installButton.Visible = false;
checkUpdateButton.Visible = false;
2015-04-08 15:20:10 -07:00
break;
case UpdateControlData.UpdateStatusStates.UpdateDownloading:
updateStatusText.Text = string.Format(
"{0} {1}%",
"Downloading updates...".Localize(),
UpdateControlData.Instance.DownloadPercent);
2015-04-08 15:20:10 -07:00
break;
case UpdateControlData.UpdateStatusStates.ReadyToInstall:
updateStatusText.Text = "New updates are ready to install".Localize();
downloadButton.Visible = false;
installButton.Visible = true;
checkUpdateButton.Visible = false;
2015-04-08 15:20:10 -07:00
break;
case UpdateControlData.UpdateStatusStates.UpToDate:
updateStatusText.Text = "Your application is up-to-date".Localize();
downloadButton.Visible = false;
installButton.Visible = false;
checkUpdateButton.Visible = true;
2015-04-08 15:20:10 -07:00
break;
default:
throw new NotImplementedException();
}
}
}
}