420 lines
12 KiB
C#
420 lines
12 KiB
C#
using Microsoft.VisualBasic.CompilerServices;
|
|
using Pilz.Updating.Administration.Discord;
|
|
using Pilz.Updating.Administration.Integrations;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Telerik.WinControls;
|
|
using Telerik.WinControls.UI;
|
|
using Z.Collections.Extensions;
|
|
|
|
namespace Pilz.Updating.Administration.GUI;
|
|
|
|
public partial class UpdateManagerWindow
|
|
{
|
|
// C o n s t a n t s
|
|
|
|
private const string FILTER_UPDATEINFO_CONFIGURATION = "Json file (*.json)|*.json";
|
|
private const string FILTER_UPDATEPROJECT = "Project file (*.udic)|*.udic";
|
|
private const string FILTER_UPDATEPACKAGE = "Zip archiv (*.zip)|*.zip";
|
|
|
|
// F i e l d s
|
|
|
|
private string curProjectFilePath;
|
|
private UpdateServerManager manager = null;
|
|
private DiscordBot discordBot = null;
|
|
private string curPackageTemplatePath = string.Empty;
|
|
|
|
// P r o p e r t i e s
|
|
|
|
public string TempPackageFilePath { get; private set; } = string.Empty;
|
|
|
|
// C o n s t r u c t o r s
|
|
|
|
public UpdateManagerWindow()
|
|
{
|
|
this.Shown += UpdateManagerWindow_Shown;
|
|
this.Load += UpdateManagerWindow_Load;
|
|
this.FormClosing += UpdateManagerWindow_FormClosing;
|
|
InitializeComponent();
|
|
this.AllowAero = false;
|
|
SetEnabledUiControls(false);
|
|
}
|
|
|
|
// F e a t u r e s
|
|
|
|
private void ProgressControls(bool enabled)
|
|
{
|
|
if (enabled)
|
|
radWaitingBar_PackageLoading.StartWaiting();
|
|
else
|
|
radWaitingBar_PackageLoading.StopWaiting();
|
|
}
|
|
|
|
private void SetEnabledUiControls(bool enabled, bool setProjectOptionsAlwayToTrue = false)
|
|
{
|
|
radRibbonBarGroup_Options.Enabled = enabled || setProjectOptionsAlwayToTrue;
|
|
radButtonElement_SaveProject.Enabled = enabled || setProjectOptionsAlwayToTrue;
|
|
radRibbonBarGroup_Configuration.Enabled = enabled;
|
|
}
|
|
|
|
private async Task CreateNewProject(string filePath)
|
|
{
|
|
var oldProject = General.CurProject;
|
|
General.CurProject = new UpdateProject();
|
|
if (new UpdateServerInfoEditor().ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
curProjectFilePath = filePath;
|
|
SaveProject(curProjectFilePath);
|
|
await LoadManager();
|
|
}
|
|
else
|
|
General.CurProject = oldProject;
|
|
}
|
|
|
|
private async Task OpenProject(string filePath)
|
|
{
|
|
curProjectFilePath = filePath;
|
|
General.CurProject = UpdateProject.Load(filePath);
|
|
General.SetProxyConfig();
|
|
await LoadManager();
|
|
}
|
|
|
|
private void SaveProject(string filePath)
|
|
{
|
|
General.CurProject.Save(filePath);
|
|
}
|
|
|
|
private async Task LoadManager()
|
|
{
|
|
bool hasError;
|
|
ProgressControls(true);
|
|
|
|
try
|
|
{
|
|
manager = new UpdateServerManager(General.CurProject.UpdateServerConfig);
|
|
|
|
if (manager.Config.GitLabSnippetConfig.Enabled && await manager.ReadInfoFromGitLabSnippet())
|
|
{
|
|
LoadPackageList();
|
|
LoadUpdateInstallerInfos();
|
|
hasError = false;
|
|
}
|
|
else
|
|
hasError = true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
hasError = true;
|
|
}
|
|
|
|
if (hasError)
|
|
{
|
|
RadMessageBox.Show(this, "Ein Fehler ist aufgetreten beim laden des Servers.", string.Empty, MessageBoxButtons.OK, RadMessageIcon.Error);
|
|
SetEnabledUiControls(false, true);
|
|
}
|
|
else
|
|
SetEnabledUiControls(true);
|
|
|
|
ProgressControls(false);
|
|
}
|
|
|
|
private void LoadPackageList()
|
|
{
|
|
ProgressControls(true);
|
|
radListView_Packages.BeginUpdate();
|
|
radListView_Packages.Items.Clear();
|
|
|
|
foreach (var pkg in manager.UpdateInfo.Packages)
|
|
{
|
|
var name = pkg.Name;
|
|
var cells = new List<string>
|
|
{
|
|
string.IsNullOrEmpty(name) ? "<No title>" : name,
|
|
pkg.Version.Version.ToString(),
|
|
pkg.Version.Channel.ToString(),
|
|
pkg.Version.Build.ToString(),
|
|
"Yes"
|
|
};
|
|
|
|
var item = new ListViewDataItem(string.Empty, [.. cells])
|
|
{
|
|
Tag = pkg
|
|
};
|
|
|
|
radListView_Packages.Items.Add(item);
|
|
}
|
|
|
|
radListView_Packages.EndUpdate();
|
|
ProgressControls(false);
|
|
|
|
if (radListView_Packages.HasChildren)
|
|
radListView_Packages.SelectedItem = radListView_Packages.Items[0];
|
|
}
|
|
|
|
private void LoadUpdateInstallerInfos()
|
|
{
|
|
radTextBoxControl_DownloadURL.Text = manager.UpdateInfo.UpdateInstallerLink;
|
|
}
|
|
|
|
private UpdatePackageInfo GetSelectedPackage()
|
|
{
|
|
return radListView_Packages.SelectedItem?.Tag as UpdatePackageInfo;
|
|
}
|
|
|
|
private bool CreatePackage(string filePath)
|
|
{
|
|
var pkg = new UpdatePackageInfo();
|
|
var canceld = EditPackageMeta(pkg);
|
|
|
|
if (!canceld)
|
|
manager.UpdateInfo.Packages.Add(pkg);
|
|
|
|
return !canceld;
|
|
}
|
|
|
|
private bool EditPackageMeta(UpdatePackageInfo pkg)
|
|
{
|
|
var frm = new PackageMetaEditor
|
|
{
|
|
Version = pkg.Version.Version,
|
|
Channel = pkg.Version.Channel,
|
|
Build = pkg.Version.Build,
|
|
Packagelink = pkg.Packagelink
|
|
};
|
|
|
|
if (frm.ShowDialog(this) != DialogResult.OK)
|
|
return false;
|
|
|
|
pkg.Version.Version = frm.Version;
|
|
pkg.Version.Build = frm.Build;
|
|
pkg.Version.Channel = frm.Channel;
|
|
pkg.Packagelink = frm.Packagelink;
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool DeletePackage(UpdatePackageInfo pkg)
|
|
{
|
|
ProgressControls(true);
|
|
var success = manager.UpdateInfo.Packages.Remove(pkg);
|
|
ProgressControls(false);
|
|
return success;
|
|
}
|
|
|
|
private async Task<bool> SaveInfoToServer()
|
|
{
|
|
if (!manager.Config.GitLabSnippetConfig.Enabled)
|
|
return false;
|
|
ProgressControls(true);
|
|
var success = await manager.SaveInfoToGitLabSnippet();
|
|
ProgressControls(false);
|
|
return success;
|
|
}
|
|
|
|
// G u i
|
|
|
|
private void UpdateManagerWindow_Shown(object sender, EventArgs e)
|
|
{
|
|
}
|
|
|
|
private async void UpdateManagerWindow_Load(object sender, EventArgs e)
|
|
{
|
|
string[] args = Environment.GetCommandLineArgs();
|
|
|
|
if (args.Length > 1)
|
|
{
|
|
try
|
|
{
|
|
await OpenProject(args[1]);
|
|
}
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
|
|
private void UpdateManagerWindow_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
discordBot?.Stop();
|
|
}
|
|
|
|
private async void ButtonItem_NewProject_Click(object sender, EventArgs e)
|
|
{
|
|
var sfd_updateadministration_upa = new RadSaveFileDialog()
|
|
{
|
|
Filter = "Update Project Files (*.upa)|*.upa"
|
|
};
|
|
if (sfd_updateadministration_upa.ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
await CreateNewProject(sfd_updateadministration_upa.FileName);
|
|
}
|
|
}
|
|
|
|
private async void ButtonItem_OpenProject_Click(object sender, EventArgs e)
|
|
{
|
|
var ofd_updateadministration_upa = new RadOpenFileDialog()
|
|
{
|
|
Filter = "Update Project Files (*.upa)|*.upa"
|
|
};
|
|
if (ofd_updateadministration_upa.ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
await OpenProject(ofd_updateadministration_upa.FileName);
|
|
}
|
|
}
|
|
|
|
private void ButtonItem_SaveProject_Click(object sender, EventArgs e)
|
|
{
|
|
SaveProject(curProjectFilePath);
|
|
}
|
|
|
|
private async void ButtonItem_ProjectOptions_Click(object sender, EventArgs e)
|
|
{
|
|
new UpdateServerInfoEditor().ShowDialog(this);
|
|
await LoadManager();
|
|
}
|
|
|
|
private async void ButtonItem_UploadUpdateConfiguration_Click(object sender, EventArgs e)
|
|
{
|
|
await SaveInfoToServer();
|
|
}
|
|
|
|
private void ButtonItem_ExportUpdateConfiguration_Click(object sender, EventArgs e)
|
|
{
|
|
var sfd_UpdateAdministration_UpdateConfiguration = new RadSaveFileDialog()
|
|
{
|
|
Filter = FILTER_UPDATEINFO_CONFIGURATION
|
|
};
|
|
if (sfd_UpdateAdministration_UpdateConfiguration.ShowDialog(this) == DialogResult.OK)
|
|
manager.SaveInfoToFile(sfd_UpdateAdministration_UpdateConfiguration.FileName);
|
|
}
|
|
|
|
private void ButtonItem_CreateAndUploadPackage_Click(object sender, EventArgs e)
|
|
{
|
|
if (CreatePackage(TempPackageFilePath))
|
|
{
|
|
LoadPackageList();
|
|
return;
|
|
}
|
|
|
|
RadMessageBox.Show(this, My.Resources.UpdatingAdministrationLangRes.MsgBox_PkgExportSuccess, My.Resources.UpdatingAdministrationLangRes.MsgBox_PkgExportSuccess_Titel, MessageBoxButtons.OK, RadMessageIcon.Info);
|
|
}
|
|
|
|
private void ButtonItem_RemovePackage_Click(object sender, EventArgs e)
|
|
{
|
|
if (DeletePackage(GetSelectedPackage()))
|
|
LoadPackageList();
|
|
}
|
|
|
|
private void TextBoxX_UpdateInstallerDownloadUrl_TextChanged(object sender, EventArgs e)
|
|
{
|
|
manager.UpdateInfo.UpdateInstallerLink = radTextBoxControl_DownloadURL.Text.Trim();
|
|
}
|
|
|
|
private void ButtonItem_PostMsgInDiscord_Click(object sender, EventArgs e)
|
|
{
|
|
if (discordBot == null)
|
|
LoadDiscordBot();
|
|
|
|
if (discordBot is not null)
|
|
{
|
|
var pkg = GetSelectedPackage();
|
|
var frm = new DiscordPostDialog(discordBot, pkg);
|
|
frm.ShowDialog(this);
|
|
}
|
|
else
|
|
RadMessageBox.Show(this, "Offenbar ist ein Fehler ist aufgetreten beim Laden des Discord-Bots.", string.Empty, MessageBoxButtons.OK, RadMessageIcon.Error);
|
|
}
|
|
|
|
private void LoadDiscordBot()
|
|
{
|
|
if (discordBot is not null)
|
|
discordBot.Stop();
|
|
|
|
discordBot = new DiscordBot(General.CurProject.DiscordBotConfig);
|
|
|
|
bool hasLoaded = false;
|
|
bool hasError = false;
|
|
|
|
discordBot.GotReady += (sender, e) => hasLoaded = true;
|
|
discordBot.LoggedMsg += (sender, msg, isError) =>
|
|
{
|
|
if (isError)
|
|
hasError = true;
|
|
};
|
|
|
|
discordBot.Start();
|
|
|
|
ProgressControls(true);
|
|
while (!hasLoaded && !hasError)
|
|
Application.DoEvents();
|
|
ProgressControls(false);
|
|
|
|
if (hasError)
|
|
discordBot = null;
|
|
}
|
|
|
|
private void ButtonItem_ChangeVersion_Click(object sender, EventArgs e)
|
|
{
|
|
if (EditPackageMeta(GetSelectedPackage()))
|
|
LoadPackageList();
|
|
}
|
|
|
|
private async void ButtonItem_EditDescription_Click(object sender, EventArgs e)
|
|
{
|
|
var pkg = GetSelectedPackage();
|
|
|
|
var frm = new PackageDescriptionEditor
|
|
{
|
|
Titel = pkg.Name,
|
|
Description = pkg.Notes.Content,
|
|
DescriptionType = pkg.Notes.ContentType
|
|
};
|
|
|
|
if (frm.ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
pkg.Name = frm.Titel;
|
|
pkg.Notes.Content = frm.Description;
|
|
pkg.Notes.ContentType = frm.DescriptionType;
|
|
await SaveInfoToServer();
|
|
}
|
|
}
|
|
|
|
private void ButtonItem_BotSettings_Click(object sender, EventArgs e)
|
|
{
|
|
var frm = new DiscordBotSettingsWindow(General.CurProject.DiscordBotConfig);
|
|
if (frm.ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
if (discordBot is not null)
|
|
LoadDiscordBot();
|
|
}
|
|
}
|
|
|
|
private void ButtonItem_ProxyConfig_Click(object sender, EventArgs e)
|
|
{
|
|
var frm = new ProxyConfigEditor(General.CurProject.ProxyConfig);
|
|
if (frm.ShowDialog(this) == DialogResult.OK)
|
|
General.SetProxyConfig();
|
|
}
|
|
|
|
private void RadListView_Packages_SelectedItemChanged(object sender, EventArgs e)
|
|
{
|
|
var anySelected = radListView_Packages.SelectedItem is not null;
|
|
radRibbonBarGroup_Discord.Enabled = anySelected;
|
|
radRibbonBarGroup_PackageManagement.Enabled = anySelected;
|
|
}
|
|
|
|
private void RadListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
|
|
{
|
|
e.CellElement.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
|
|
}
|
|
|
|
private void RadListView1_CellFormatting_1(object sender, ListViewCellFormattingEventArgs e)
|
|
{
|
|
e.CellElement.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
|
|
}
|
|
|
|
private void RadPageView1_SelectedPageChanged(object sender, EventArgs e)
|
|
{
|
|
}
|
|
} |