2018-10-29 07:15:48 +01:00
using BandagedBD.Panels ;
using System ;
2020-04-10 17:33:53 +02:00
using System.Net ;
2018-10-29 07:15:48 +01:00
using System.Collections.Generic ;
using System.Windows.Forms ;
2020-04-10 17:33:53 +02:00
using System.Threading.Tasks ;
2018-10-29 07:15:48 +01:00
namespace BandagedBD {
public enum PanelTypes { License , Action , InstallConfig , RepairConfig , UninstallConfig , Install , Repair , Uninstall , NONE } ;
public partial class FormMain : Form {
private PanelTypes CurrentPanel = PanelTypes . NONE ;
public PanelTypes Action = PanelTypes . NONE ;
2020-04-10 17:33:53 +02:00
private string currentTag = "v" + Properties . Resources . Version ;
private string remoteTag = "v" ;
2018-10-29 07:15:48 +01:00
private Dictionary < PanelTypes , IPanel > panelMap = new Dictionary < PanelTypes , IPanel > ( ) ;
public FormMain ( ) {
InitializeComponent ( ) ;
2019-07-06 06:14:31 +02:00
this . Text = "BandagedBD Installer v" + Properties . Resources . Version ;
2018-10-29 07:15:48 +01:00
logo . Click + = Logo_Click ;
panelMap [ PanelTypes . License ] = new LicensePanel ( ) ;
panelMap [ PanelTypes . Action ] = new ActionPanel ( ) ;
panelMap [ PanelTypes . InstallConfig ] = new InstallConfigPanel ( ) ;
panelMap [ PanelTypes . UninstallConfig ] = new UninstallConfigPanel ( ) ;
panelMap [ PanelTypes . RepairConfig ] = new RepairConfigPanel ( ) ;
panelMap [ PanelTypes . Install ] = new InstallPanel ( ) ;
panelMap [ PanelTypes . Uninstall ] = new UninstallPanel ( ) ;
panelMap [ PanelTypes . Repair ] = new RepairPanel ( ) ;
foreach ( KeyValuePair < PanelTypes , IPanel > entry in panelMap ) {
entry . Value . SetWindow ( this ) ;
entry . Value . Control . Dock = DockStyle . Fill ;
}
if ( ! Properties . Settings . Default . AgreedToTerms ) SwitchPanel ( PanelTypes . License ) ;
else SwitchPanel ( PanelTypes . Action ) ;
2020-04-10 17:33:53 +02:00
Task . Run ( checkForUpdate ) . ContinueWith ( result = > {
if ( result . Result ) showUpdateNotice ( ) ;
} ) ;
2018-10-29 07:15:48 +01:00
}
public void SetTitle ( string title ) = > lblTitle . Text = title ;
public IPanel GetPanel ( PanelTypes which ) {
return panelMap [ which ] ;
}
public void SwitchPanel ( PanelTypes which ) {
if ( CurrentPanel ! = PanelTypes . NONE ) panelDock . Controls . Remove ( panelMap [ CurrentPanel ] . Control ) ;
CurrentPanel = which ;
panelDock . Controls . Add ( panelMap [ which ] . Control ) ;
SetTitle ( $"BandagedBD — {panelMap[which].Title}" ) ;
panelMap [ which ] . OnShow ( ) ;
if ( panelMap [ CurrentPanel ] . PreviousPanel = = PanelTypes . NONE ) btnBack . HideDisable ( ) ;
if ( panelMap [ CurrentPanel ] . NextPanel = = PanelTypes . NONE ) {
btnBack . HideDisable ( ) ;
btnNext . HideDisable ( ) ;
btnCancel . ShowEnable ( "Exit" ) ;
}
}
protected override void OnPaint ( PaintEventArgs e ) {
base . OnPaint ( e ) ;
var g = e . Graphics ;
g . FillRectangle ( headerBrush , headerShape ) ;
g . DrawLine ( headerPen , 0 , 85 , Width , 85 ) ;
}
private void btnNext_Click ( object sender , EventArgs e ) {
if ( CurrentPanel = = PanelTypes . License ) {
Properties . Settings . Default . AgreedToTerms = true ;
Properties . Settings . Default . Save ( ) ;
}
SwitchPanel ( panelMap [ CurrentPanel ] . NextPanel ) ;
}
private void btnCancel_Click ( object sender , EventArgs e ) {
Application . Exit ( ) ;
}
private void btnBack_Click ( object sender , EventArgs e ) {
SwitchPanel ( panelMap [ CurrentPanel ] . PreviousPanel ) ;
}
public void Fail ( ) {
btnCancel . ShowEnable ( ) ;
btnNext . ShowDisable ( ) ;
btnBack . ShowEnable ( ) ;
}
private void Logo_Click ( object sender , EventArgs e ) {
Utilities . OpenProcess ( "https://github.com/rauenzi/BetterDiscordApp" ) ;
}
private void linkLabel1_LinkClicked ( object sender , LinkLabelLinkClickedEventArgs e ) {
2019-07-06 06:14:31 +02:00
Utilities . OpenProcess ( "https://paypal.me/ZackRauen" ) ;
2018-10-29 07:15:48 +01:00
}
2020-04-10 17:33:53 +02:00
private async Task < bool > checkForUpdate ( )
{
string endpoint = "https://api.github.com/repos/rauenzi/BBDInstaller/releases/latest" ;
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls12 | SecurityProtocolType . Ssl3 ;
string tagNameKey = "\"tag_name\":" ;
using ( var wc = new TimedWebClient ( ) )
{
try
{
wc . Headers . Set ( "User-Agent" , "BBDInstaller" ) ;
string json = wc . DownloadString ( endpoint ) ;
int tagNameIndex = json . IndexOf ( tagNameKey ) ;
int versionStart = tagNameIndex + tagNameKey . Length + 1 ;
int versionEnd = json . IndexOf ( "\"" , versionStart ) ;
remoteTag = json . Substring ( versionStart , versionEnd - versionStart ) ;
return string . CompareOrdinal ( remoteTag , currentTag ) > 0 ;
}
catch
{
return false ;
}
}
}
private void showUpdateNotice ( )
{
DialogResult confirmResult = MessageBox . Show ( $"There is a newer version of the installer available ({remoteTag}). Would you like to download it now?" , "Update Available!" , MessageBoxButtons . YesNo ) ;
if ( confirmResult = = DialogResult . Yes )
{
Utilities . OpenProcess ( $"https://github.com/rauenzi/BBDInstaller/releases/download/{remoteTag}/BandagedBD.exe" ) ;
Application . Exit ( ) ;
}
else
{
Application . Exit ( ) ;
}
}
private void FormMain_Load ( object sender , EventArgs e )
{
}
2018-10-29 07:15:48 +01:00
}
}