This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/UpTool2/MainForm.cs

347 lines
13 KiB
C#
Raw Permalink Normal View History

2019-09-08 21:39:04 +02:00
using System;
2020-05-16 18:19:43 +02:00
using System.Collections.Generic;
2019-09-08 21:39:04 +02:00
using System.Drawing;
using System.IO;
2020-03-24 22:43:48 +01:00
using System.Linq;
2019-11-14 18:53:00 +01:00
using System.Reflection;
using System.Threading;
2020-02-28 14:26:16 +01:00
using System.Windows.Forms;
2020-03-24 20:53:23 +01:00
using UpToolLib;
using UpToolLib.DataStructures;
2020-03-24 20:54:52 +01:00
using System.Diagnostics;
using UpToolLib.v1.Tool;
using UpToolLib.v2;
using UpToolLib.v2.TaskQueue;
2019-09-08 21:39:04 +02:00
namespace UpTool2
{
2020-03-16 20:13:41 +01:00
public sealed partial class MainForm : Form
2019-09-08 21:39:04 +02:00
{
2020-03-24 20:53:23 +01:00
private readonly HelpEventHandler _help;
private List<AppTask> _tasks;
2020-03-24 22:43:48 +01:00
2020-02-28 14:26:16 +01:00
public MainForm()
{
InitializeComponent();
_tasks = new List<AppTask>();
2020-03-24 20:53:23 +01:00
_help = MainForm_HelpRequested;
HelpRequested += _help;
2020-02-28 14:26:16 +01:00
filterBox.DataSource = Enum.GetValues(typeof(Status));
if (Program.Online)
{
Program.SetSplash(8, "Fetching repositories");
Program.Lib.V2.RepoManagement.FetchRepos();
}
2020-02-28 14:26:16 +01:00
else
{
MessageBox.Show("Starting in offline mode!");
controls_reload.Enabled = false;
filterBox.Enabled = false;
filterBox.SelectedIndex = 2;
}
Program.SetSplash(9, "Reloading data");
2020-02-28 14:26:16 +01:00
ReloadElements();
if (!Directory.Exists(Program.Lib.V1.PathTool.AppsPath))
Directory.CreateDirectory(Program.Lib.V1.PathTool.AppsPath);
2020-02-28 14:26:16 +01:00
}
2019-09-08 21:39:04 +02:00
private void Action_install_Click(object sender, EventArgs e)
{
2020-05-16 18:19:43 +02:00
App tmp = (App) action_install.Tag;
2020-04-08 20:57:50 +02:00
if (_tasks.Any(s => s is InstallTask t && t.App == tmp))
2019-09-08 21:39:04 +02:00
{
2020-04-08 20:57:50 +02:00
_tasks = _tasks.Where(s => !(s is InstallTask t) || t.App != tmp).ToList();
action_install.ResetBackColor();
2020-03-24 14:01:52 +01:00
}
2020-04-08 20:57:50 +02:00
else
{
_tasks.Add(Program.Lib.V2.TaskFactory.CreateInstall(tmp, ReloadElements));
2020-04-08 20:57:50 +02:00
action_install.BackColor = Color.Green;
}
UpdateChangesLabel();
2019-09-29 16:19:57 +02:00
}
2020-02-28 12:59:59 +01:00
2019-10-20 15:18:21 +02:00
private void Action_remove_Click(object sender, EventArgs e)
{
2020-05-16 18:19:43 +02:00
App tmp = (App) action_install.Tag;
2020-04-08 20:57:50 +02:00
if (_tasks.Any(s => s is RemoveTask t && t.App == tmp))
2019-10-20 15:18:21 +02:00
{
2020-04-08 20:57:50 +02:00
_tasks = _tasks.Where(s => !(s is RemoveTask t) || t.App != tmp).ToList();
action_remove.ResetBackColor();
2019-10-20 15:18:21 +02:00
}
2020-04-08 20:57:50 +02:00
else
2019-10-20 15:18:21 +02:00
{
_tasks.Add(Program.Lib.V2.TaskFactory.CreateRemove(tmp, ReloadElements));
2020-04-08 20:57:50 +02:00
action_remove.BackColor = Color.Green;
2019-10-20 15:18:21 +02:00
}
2020-04-08 20:57:50 +02:00
UpdateChangesLabel();
2019-10-20 15:18:21 +02:00
}
2020-02-28 12:59:59 +01:00
2019-09-29 16:19:57 +02:00
private void controls_upload_Click(object sender, EventArgs e)
{
2020-04-08 20:57:50 +02:00
if (searchPackageDialog.ShowDialog() != DialogResult.OK)
return;
if (!_tasks.Any(s => s is UploadTask t && t.ZipFile == searchPackageDialog.FileName))
_tasks.Add(Program.Lib.V2.TaskFactory.CreateUpload(searchPackageDialog.FileName, AppNameDialog.Show(), ReloadElements));
2020-04-08 20:57:50 +02:00
UpdateChangesLabel();
}
private void Action_update_Click(object sender, EventArgs e)
{
2020-05-16 18:19:43 +02:00
App tmp = (App) action_install.Tag;
2020-04-08 20:57:50 +02:00
if (_tasks.Any(s => s is UpdateTask t && t.App == tmp))
2019-09-29 16:19:57 +02:00
{
2020-04-08 20:57:50 +02:00
_tasks = _tasks.Where(s => !(s is UpdateTask t) || t.App != tmp).ToList();
action_update.ResetBackColor();
2019-09-29 16:19:57 +02:00
}
2020-04-08 20:57:50 +02:00
else
2019-09-29 16:19:57 +02:00
{
_tasks.Add(Program.Lib.V2.TaskFactory.CreateUpdate(tmp, ReloadElements));
2020-04-08 20:57:50 +02:00
action_update.BackColor = Color.Green;
2019-09-29 16:19:57 +02:00
}
2020-04-08 20:57:50 +02:00
UpdateChangesLabel();
2019-09-08 21:39:04 +02:00
}
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
private void ReloadElements()
2019-10-20 15:18:21 +02:00
{
//remove
toolTip.RemoveAll();
2020-02-28 14:26:16 +01:00
ClearSelection();
2019-10-20 15:18:21 +02:00
infoPanel_Title.Invalidate();
infoPanel_Description.Invalidate();
int f = sidebarPanel.Controls.Count;
for (int i = 0; i < f; i++) sidebarPanel.Controls[0].Dispose();
Program.Lib.V1.Apps.Clear();
2019-10-20 15:18:21 +02:00
//add
toolTip.SetToolTip(controls_settings, "Settings");
toolTip.SetToolTip(controls_reload, "Refresh repositories");
toolTip.SetToolTip(controls_upload, "Install package from disk");
toolTip.SetToolTip(filterBox, "Filter");
toolTip.SetToolTip(action_install, "Install");
toolTip.SetToolTip(action_remove, "Remove");
toolTip.SetToolTip(action_update, "Update");
toolTip.SetToolTip(action_run, "Run");
Program.Lib.V2.RepoManagement.GetReposFromDisk();
2019-11-11 15:23:14 +01:00
int availableUpdates = 0;
foreach (App app in Program.Lib.V1.Apps.Values)
2019-10-20 15:18:21 +02:00
{
2020-03-24 20:53:23 +01:00
Panel sidebarIcon = new Panel
{
Tag = app,
BackColor = app.Color,
Size = new Size(70, 70),
BackgroundImage = (Bitmap) app.Icon,
BackgroundImageLayout = ImageLayout.Stretch
};
2020-06-21 16:46:34 +02:00
sidebarIcon.Paint += (sender, args) =>
{
args.Graphics.Clear(sidebarIcon.BackColor);
args.Graphics.DrawImage(sidebarIcon.BackgroundImage, args.ClipRectangle,
new Rectangle(new Point(0, 0), sidebarIcon.BackgroundImage.Size), GraphicsUnit.Pixel);
};
bool updateable = !app.Local && app.Status.Contains(Status.Updatable);
2020-02-28 14:26:16 +01:00
sidebarIcon.Click += (sender, e) =>
2019-10-20 18:25:00 +02:00
{
2020-02-28 14:26:16 +01:00
infoPanel_Title.Text = app.Name;
infoPanel_Title.ForeColor = app.Local ? Color.Red : Color.Black;
infoPanel_Description.Text = app.Description;
2019-10-20 15:18:21 +02:00
action_install.Tag = app;
action_install.Enabled = !(app.Local || Directory.Exists(app.AppPath));
2020-04-08 20:57:50 +02:00
if (_tasks.Any(s => s is InstallTask t && t.App == app))
action_install.BackColor = Color.Green;
else
action_install.ResetBackColor();
2019-10-20 15:18:21 +02:00
action_remove.Tag = app;
action_remove.Enabled = Directory.Exists(app.AppPath);
2020-04-08 20:57:50 +02:00
if (_tasks.Any(s => s is RemoveTask t && t.App == app))
action_remove.BackColor = Color.Green;
else
action_remove.ResetBackColor();
2019-10-20 15:18:21 +02:00
action_update.Tag = app;
2020-03-24 20:53:23 +01:00
action_update.Enabled = updateable;
2020-04-08 20:57:50 +02:00
if (_tasks.Any(s => s is UpdateTask t && t.App == app))
action_update.BackColor = Color.Green;
else
action_update.ResetBackColor();
2019-10-20 15:18:21 +02:00
action_run.Tag = app;
action_run.Enabled = app.Status.Contains(Status.Installed) && !app.Local &&
app.Runnable && Directory.Exists(app.AppPath);
2019-10-20 15:18:21 +02:00
};
2020-03-24 20:53:23 +01:00
if (updateable)
2019-11-11 15:23:14 +01:00
availableUpdates++;
2020-02-28 14:26:16 +01:00
toolTip.SetToolTip(sidebarIcon, app.Name);
2019-10-20 15:18:21 +02:00
sidebarPanel.Controls.Add(sidebarIcon);
}
2020-02-28 14:26:16 +01:00
UpdateSidebarV(null, null);
Text =
2020-03-24 20:53:23 +01:00
$"UpTool2 {(availableUpdates == 0 ? "(All up-to-date)" : $"({availableUpdates} Updates)")}";
2019-10-20 15:18:21 +02:00
}
2020-02-28 12:59:59 +01:00
2019-11-09 20:51:53 +01:00
private void Action_run_Click(object sender, EventArgs e)
{
2020-02-28 14:26:16 +01:00
try
{
Program.Lib.V1.AppExtras.RunApp((App) action_run.Tag);
2020-02-28 14:26:16 +01:00
}
catch (Exception e1)
{
2020-03-24 20:53:23 +01:00
MessageBox.Show($"{e1}Failed to start!");
2020-02-28 14:26:16 +01:00
}
2019-11-09 20:51:53 +01:00
}
2020-02-28 12:59:59 +01:00
2019-10-20 18:25:00 +02:00
private void Controls_reload_Click(object sender, EventArgs e)
{
Enabled = false;
if (MessageBox.Show("This may take a few minutes. Are you sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
new Thread(() =>
{
Program.Lib.V2.RepoManagement.FetchRepos();
Invoke((Action) (() =>
{
ReloadElements();
Enabled = true;
}));
}).Start();
}
2019-10-20 18:25:00 +02:00
}
2020-02-28 12:59:59 +01:00
2019-10-21 15:33:09 +02:00
private void Controls_settings_Click(object sender, EventArgs e) => new SettingsForms().ShowDialog();
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
private void ClearSelection()
2019-09-10 10:02:24 +02:00
{
action_install.Enabled = false;
2020-06-21 16:46:34 +02:00
action_install.ResetBackColor();
2019-09-10 10:02:24 +02:00
action_remove.Enabled = false;
2020-06-21 16:46:34 +02:00
action_remove.ResetBackColor();
2019-09-10 10:02:24 +02:00
action_update.Enabled = false;
2020-06-21 16:46:34 +02:00
action_update.ResetBackColor();
2019-09-10 10:02:24 +02:00
action_run.Enabled = false;
2020-06-21 16:46:34 +02:00
action_run.ResetBackColor();
2019-09-10 10:02:24 +02:00
infoPanel_Title.Text = "";
infoPanel_Description.Text = "";
}
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
private void UpdateSidebarV(object sender, EventArgs e)
2019-09-09 17:50:33 +02:00
{
2019-11-09 20:51:53 +01:00
if (searchBox.Text == "!DEBUG:PRINT!")
2019-09-09 17:50:33 +02:00
{
2019-11-09 20:51:53 +01:00
searchBox.Text = "!DEBUG:PRINT";
2020-02-28 14:26:16 +01:00
string tmpFile = Path.GetTempFileName();
File.WriteAllText(tmpFile,
string.Join("\r\n\r\n",
Program.Lib.V1.Apps.Values.Select(app => app.ToString()).Concat(new[]
2020-05-16 17:59:44 +02:00
{
$"Assembly version: {Assembly.GetExecutingAssembly().GetName().Version}"
}).ToArray()));
2020-02-28 12:59:59 +01:00
new Thread(() =>
{
2020-02-28 14:26:16 +01:00
Process.Start("notepad", tmpFile).WaitForExit();
File.Delete(tmpFile);
2019-11-09 20:51:53 +01:00
}).Start();
2019-09-10 10:02:24 +02:00
}
2019-11-09 20:51:53 +01:00
else
{
App[] apps = Program.Lib.V1.AppExtras.FindApps(searchBox.Text);
Enum.TryParse(filterBox.SelectedValue.ToString(), out Status status);
for (int i = 0; i < sidebarPanel.Controls.Count; i++)
{
Panel sidebarIcon = (Panel) sidebarPanel.Controls[i];
App app = (App) sidebarIcon.Tag;
sidebarIcon.Visible = apps.Contains(app) && app.Status.Contains(Program.Online ? status : Status.Installed);
}
ClearSelection();
2019-11-09 20:51:53 +01:00
}
2019-09-10 10:02:24 +02:00
}
2020-02-28 12:59:59 +01:00
2019-10-20 15:18:21 +02:00
private void MainForm_Load(object sender, EventArgs e)
{
2020-03-20 12:35:46 +01:00
if (Program.Splash.IsDisposed)
Close();
else
{
2020-03-24 22:43:48 +01:00
Program.Splash.Invoke((Action) Program.Splash.Hide);
2020-03-20 12:35:46 +01:00
BringToFront();
2020-04-08 20:57:50 +02:00
UpdateChangesLabel(false);
2020-03-20 12:35:46 +01:00
}
2019-10-20 15:18:21 +02:00
}
2019-11-14 18:53:00 +01:00
2020-02-28 12:59:59 +01:00
private static DateTime GetBuildDateTime(Assembly assembly)
2019-11-14 18:53:00 +01:00
{
2020-03-16 20:13:41 +01:00
string location = assembly.Location;
const int headerOffset = 60;
const int linkerTimestampOffset = 8;
byte[] buffer = new byte[2048];
Stream? stream = null;
2019-11-14 18:53:00 +01:00
2020-03-16 20:13:41 +01:00
try
{
stream = new FileStream(location, FileMode.Open, FileAccess.Read);
stream.Read(buffer, 0, 2048);
}
finally
{
2020-04-08 20:57:50 +02:00
stream?.Close();
2019-11-14 18:53:00 +01:00
}
2020-03-16 20:13:41 +01:00
int i = BitConverter.ToInt32(buffer, headerOffset);
int secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset);
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
dt = dt.AddSeconds(secondsSince1970);
dt = TimeZoneInfo.ConvertTimeToUtc(dt);
return dt;
2019-11-14 18:53:00 +01:00
}
2020-04-08 20:57:50 +02:00
private void MainForm_HelpRequested(object sender, HelpEventArgs hlpEvent)
2019-11-14 18:53:00 +01:00
{
2020-03-24 20:53:23 +01:00
HelpRequested -= _help;
2020-03-16 20:13:41 +01:00
try
{
DateTime buildTime = GetBuildDateTime(Assembly.GetExecutingAssembly());
2020-03-20 12:35:46 +01:00
MessageBox.Show($@"UpTool2 by JFronny
2020-02-28 14:26:16 +01:00
Version: {Assembly.GetExecutingAssembly().GetName().Version}
Build Date: {buildTime:dd.MM.yyyy}", "UpTool2");
2020-03-16 20:13:41 +01:00
}
finally
{
2020-03-24 20:53:23 +01:00
HelpRequested += _help;
2020-04-08 20:57:50 +02:00
hlpEvent.Handled = true;
2020-03-16 20:13:41 +01:00
}
2020-02-28 14:26:16 +01:00
}
2020-04-08 20:57:50 +02:00
private void changesButton_Click(object sender, EventArgs e)
{
2020-06-21 16:46:34 +02:00
if (TaskPreview.Show(ref _tasks, true))
2020-04-08 20:57:50 +02:00
{
2020-06-21 16:46:34 +02:00
progressBar1.Maximum = _tasks.Count;
progressBar1.Value = 0;
foreach (AppTask task in _tasks)
2020-06-21 16:46:34 +02:00
{
task.Run();
progressBar1.PerformStep();
}
_tasks.Clear();
UpdateChangesLabel();
2020-04-08 20:57:50 +02:00
}
}
private void changesLabel_Click(object sender, EventArgs e)
{
2020-06-21 16:46:34 +02:00
TaskPreview.Show(ref _tasks, false);
2020-04-08 20:57:50 +02:00
UpdateChangesLabel();
}
private void UpdateChangesLabel(bool showPanel = true)
{
changesPanel.Visible = showPanel;
changesButton.Enabled = _tasks.Count > 0;
progressBar1.Maximum = _tasks.Count;
changesLabel.Text = _tasks.Count switch
{
0 => "No Changes Selected",
1 => "1 Change Selected",
_ => $"{_tasks.Count} Changes Selected"
};
}
2019-09-08 21:39:04 +02:00
}
2020-02-28 12:59:59 +01:00
}