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

241 lines
10 KiB
C#
Raw Normal View History

2019-09-08 21:39:04 +02:00
using System;
using System.Drawing;
using System.Windows.Forms;
using UpTool2.Properties;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using System.IO.Compression;
using System.Security.Cryptography;
2019-09-29 16:19:57 +02:00
using Microsoft.VisualBasic;
2019-09-08 21:39:04 +02:00
namespace UpTool2
{
public partial class MainForm : Form
{
private void Action_install_Click(object sender, EventArgs e)
{
2019-09-29 16:19:57 +02:00
#if !DEBUG
2019-09-08 21:39:04 +02:00
try
{
2019-09-29 16:19:57 +02:00
#endif
2019-11-11 16:06:15 +01:00
AppInstall.Install((App)action_install.Tag);
2019-09-29 16:19:57 +02:00
#if !DEBUG
2019-09-08 21:39:04 +02:00
}
catch (Exception e1)
{
2019-11-11 15:48:49 +01:00
if (!GlobalVariables.relE)
2019-09-08 21:39:04 +02:00
throw;
MessageBox.Show(e1.ToString(), "Install failed");
}
2019-09-29 16:19:57 +02:00
#endif
}
2019-10-20 15:18:21 +02:00
private void Action_remove_Click(object sender, EventArgs e)
{
try
{
2019-11-11 15:48:49 +01:00
string app = GlobalVariables.dir + @"\Apps\" + ((App)action_remove.Tag).ID.ToString();
string tmp = GlobalVariables.dir + @"\tmp";
2019-10-20 15:18:21 +02:00
if (Directory.Exists(tmp))
Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp);
ZipFile.ExtractToDirectory(app + @"\package.zip", tmp);
Process.Start(new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/C \"" + tmp + "\\Remove.bat\"", WorkingDirectory = app + @"\app" }).WaitForExit();
Directory.Delete(tmp, true);
Directory.Delete(app, true);
2019-11-11 15:48:49 +01:00
if (GlobalVariables.relE)
2019-10-20 15:18:21 +02:00
reloadElements();
}
catch (Exception e1)
{
2019-11-11 15:48:49 +01:00
if (!GlobalVariables.relE)
2019-10-20 15:18:21 +02:00
throw;
MessageBox.Show(e1.ToString(), "Removal failed");
}
}
2019-09-29 16:19:57 +02:00
private void controls_upload_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
#endif
if (searchPackageDialog.ShowDialog() == DialogResult.OK)
{
Guid ID = Guid.NewGuid();
2019-11-11 16:06:15 +01:00
while (GlobalVariables.apps.ContainsKey(ID) || Directory.Exists(GlobalVariables.getAppPath(ID)))
2019-09-29 16:19:57 +02:00
ID = Guid.NewGuid();
App appI = new App(Interaction.InputBox("Name:"), "Locally installed package, removal only", -1, "", true, "", ID, Color.Red, Resources.C_64.ToBitmap(), false, "");
2019-11-11 16:06:15 +01:00
AppInstall.installZip(searchPackageDialog.FileName, appI);
2019-09-29 16:19:57 +02:00
}
#if !DEBUG
}
catch (Exception e1)
{
2019-11-11 15:48:49 +01:00
if (!GlobalVariables.relE)
2019-09-29 16:19:57 +02:00
throw;
MessageBox.Show(e1.ToString(), "Install failed");
}
2019-11-09 23:46:49 +01:00
#endif
2019-09-08 21:39:04 +02:00
}
2019-10-20 15:18:21 +02:00
void reloadElements()
{
//remove
toolTip.RemoveAll();
clearSelection();
infoPanel_Title.Invalidate();
infoPanel_Description.Invalidate();
int F = sidebarPanel.Controls.Count;
for (int i = 0; i < F; i++)
{
sidebarPanel.Controls[0].Dispose();
}
2019-11-11 15:48:49 +01:00
GlobalVariables.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");
2019-11-11 14:41:11 +01:00
toolTip.SetToolTip(controls_local, "Install UpTool2 locally");
2019-11-11 15:48:49 +01:00
controls_local.Visible = Application.ExecutablePath != GlobalVariables.dir + @"\UpTool2.exe";
searchBox.Size = (Application.ExecutablePath != GlobalVariables.dir + @"\UpTool2.exe") ? new Size(233, 20) : new Size(262, 20);
2019-10-20 15:18:21 +02:00
toolTip.SetToolTip(filterBox, "Filter");
toolTip.SetToolTip(action_install, "Install");
toolTip.SetToolTip(action_remove, "Remove");
toolTip.SetToolTip(action_update, "Update");
toolTip.SetToolTip(action_run, "Run");
2019-11-11 15:48:49 +01:00
RepoManagement.getReposFromDisk();
2019-11-11 15:23:14 +01:00
int availableUpdates = 0;
2019-11-11 15:48:49 +01:00
foreach (App app in GlobalVariables.apps.Values)
2019-10-20 15:18:21 +02:00
{
Panel sidebarIcon = new Panel();
sidebarIcon.Tag = app;
sidebarIcon.BackColor = app.color;
sidebarIcon.Size = new Size(70, 70);
sidebarIcon.BackgroundImage = app.icon;
sidebarIcon.BackgroundImageLayout = ImageLayout.Stretch;
2019-10-20 18:25:00 +02:00
sidebarIcon.Click += (object sender, EventArgs e) =>
{
2019-10-20 15:18:21 +02:00
infoPanel_Title.Text = app.name;
infoPanel_Title.ForeColor = app.local ? Color.Red : Color.Black;
infoPanel_Description.Text = app.description;
action_install.Tag = app;
2019-11-11 15:48:49 +01:00
action_install.Enabled = !(app.local || Directory.Exists(GlobalVariables.getAppPath(app)));
2019-10-20 15:18:21 +02:00
action_remove.Tag = app;
2019-11-11 15:48:49 +01:00
action_remove.Enabled = Directory.Exists(GlobalVariables.getAppPath(app));
2019-10-20 15:18:21 +02:00
action_update.Tag = app;
2019-11-11 15:48:49 +01:00
action_update.Enabled = (!app.local) && File.Exists(GlobalVariables.getInfoPath(app)) && int.Parse(XDocument.Load(GlobalVariables.getInfoPath(app)).Element("app").Element("Version").Value) < app.version;
2019-10-20 15:18:21 +02:00
action_run.Tag = app;
2019-11-11 15:48:49 +01:00
action_run.Enabled = (!app.local) && app.runnable && Directory.Exists(GlobalVariables.getAppPath(app));
2019-10-20 15:18:21 +02:00
};
2019-11-11 15:48:49 +01:00
if ((!app.local) && File.Exists(GlobalVariables.getInfoPath(app)) && int.Parse(XDocument.Load(GlobalVariables.getInfoPath(app)).Element("app").Element("Version").Value) < app.version)
2019-11-11 15:23:14 +01:00
availableUpdates++;
2019-10-20 15:18:21 +02:00
toolTip.SetToolTip(sidebarIcon, app.name);
sidebarPanel.Controls.Add(sidebarIcon);
}
updateSidebarV(null, null);
2019-11-11 15:23:14 +01:00
Text = "UpTool2 " + ((availableUpdates == 0) ? "(All up-to-date)" : "(" + availableUpdates.ToString() + " Updates)");
2019-10-20 15:18:21 +02:00
}
2019-11-09 20:51:53 +01:00
private void Action_run_Click(object sender, EventArgs e)
{
Console.WriteLine(new string('-', 10));
2019-11-11 15:48:49 +01:00
Console.WriteLine(GlobalVariables.getDataPath((App)action_run.Tag));
2019-11-09 20:51:53 +01:00
Console.WriteLine("\\");
Console.WriteLine(((App)action_run.Tag).mainFile);
2019-11-11 15:48:49 +01:00
Console.WriteLine(GlobalVariables.getDataPath((App)action_run.Tag));
2019-11-09 20:51:53 +01:00
_ = Process.Start(
2019-11-11 14:41:11 +01:00
new ProcessStartInfo
{
2019-11-11 15:48:49 +01:00
FileName = GlobalVariables.getDataPath((App)action_run.Tag) + "\\" + ((App)action_run.Tag).mainFile,
WorkingDirectory = GlobalVariables.getDataPath((App)action_run.Tag)
2019-11-09 20:51:53 +01:00
});
}
2019-10-20 15:18:21 +02:00
private void Action_update_Click(object sender, EventArgs e)
{
try
{
2019-11-11 15:48:49 +01:00
GlobalVariables.relE = false;
2019-10-20 15:18:21 +02:00
Action_remove_Click(sender, e);
Action_install_Click(sender, e);
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString(), "Install failed");
}
reloadElements();
2019-11-11 15:48:49 +01:00
GlobalVariables.relE = true;
2019-10-20 15:18:21 +02:00
}
2019-10-20 18:25:00 +02:00
private void Controls_reload_Click(object sender, EventArgs e)
{
2019-11-11 15:48:49 +01:00
RepoManagement.fetchRepos();
2019-10-20 18:25:00 +02:00
reloadElements();
}
2019-10-21 15:33:09 +02:00
private void Controls_settings_Click(object sender, EventArgs e) => new SettingsForms().ShowDialog();
2019-09-10 10:02:24 +02:00
void clearSelection()
{
action_install.Enabled = false;
action_remove.Enabled = false;
action_update.Enabled = false;
action_run.Enabled = false;
infoPanel_Title.Text = "";
infoPanel_Description.Text = "";
}
private void updateSidebarV(object sender, EventArgs e)
2019-09-09 17:50:33 +02:00
{
2019-11-09 20:51:53 +01:00
#if DEBUG
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";
string _tmp_file = Path.GetTempFileName();
File.WriteAllText(_tmp_file, string.Join("\r\n\r\n", apps.Select(app => app.Value).Select(app => app.ToString()).ToArray()));
new Thread(() => {
Process.Start("notepad", _tmp_file).WaitForExit();
File.Delete(_tmp_file);
}).Start();
2019-09-10 10:02:24 +02:00
}
2019-11-09 20:51:53 +01:00
else
{
#endif
2019-11-11 14:41:11 +01:00
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 = app.name.Contains(searchBox.Text) && ((int)app.status & (int)(Program.online ? status : Status.Installed)) != 0;
}
clearSelection();
2019-11-09 20:51:53 +01:00
#if DEBUG
}
#endif
2019-09-10 10:02:24 +02:00
}
2019-10-20 15:18:21 +02:00
public MainForm()
{
2019-11-11 16:06:15 +01:00
GlobalVariables.reloadElements = reloadElements;
2019-10-20 15:18:21 +02:00
InitializeComponent();
filterBox.DataSource = Enum.GetValues(typeof(Status));
2019-10-21 16:12:07 +02:00
if (Program.online)
2019-11-11 15:48:49 +01:00
RepoManagement.fetchRepos();
2019-10-21 16:12:07 +02:00
else
{
MessageBox.Show("Starting in offline mode!");
controls_reload.Enabled = false;
filterBox.Enabled = false;
filterBox.SelectedIndex = 2;
}
2019-10-20 15:18:21 +02:00
reloadElements();
2019-11-11 15:48:49 +01:00
if (!Directory.Exists(GlobalVariables.appsPath))
Directory.CreateDirectory(GlobalVariables.appsPath);
2019-10-20 15:18:21 +02:00
}
private void MainForm_Load(object sender, EventArgs e)
{
Program.splash.Hide();
BringToFront();
}
2019-11-11 14:41:11 +01:00
private void controls_local_Click(object sender, EventArgs e)
{
2019-11-11 15:48:49 +01:00
File.Copy(GlobalVariables.dir + @"\update.exe", GlobalVariables.dir + @"\UpTool2.exe", true);
Shortcut.Make(GlobalVariables.dir + @"\UpTool2.exe", Path.GetDirectoryName(Application.ExecutablePath) + "\\UpTool2.lnk");
Shortcut.Make(GlobalVariables.dir + @"\UpTool2.exe", Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "\\UpTool2.lnk");
2019-11-11 14:41:11 +01:00
Close();
}
2019-09-08 21:39:04 +02:00
}
}