From 7d233eb3cfd675ab3e5c397cbcfcc9848f103ccf Mon Sep 17 00:00:00 2001 From: cainy-a Date: Thu, 21 May 2020 18:03:41 +0100 Subject: [PATCH] Refactor to improve code quality --- Ytdl-Gui/Form1.cs | 53 ++++++++++++++++++--------------------- Ytdl-Gui/VistaSecurity.cs | 8 +++--- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/Ytdl-Gui/Form1.cs b/Ytdl-Gui/Form1.cs index 1d9ea97..fe85120 100644 --- a/Ytdl-Gui/Form1.cs +++ b/Ytdl-Gui/Form1.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; +using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using static Ytdl_Gui.Download; @@ -13,21 +15,19 @@ public Form1() { InitializeComponent(); - if (!VistaSecurity.IsAdmin()) - { - VistaSecurity.AddShieldToButton(buttonDownload); // Add a shield to Download Button - buttonDownload.Text = "Get admin (REQUIRED)"; - } + if (VistaSecurity.IsAdmin()) return; + VistaSecurity.AddShieldToButton(buttonDownload); // Add a shield to Download Button + buttonDownload.Text = "Get admin (REQUIRED)"; } - public static string SavePath; - public static List Urls = new List(); - public static bool Busy = false; + private static string _savePath; + private static List _urls = new List(); + private static bool _busy; private void Form1_Load(object sender, EventArgs e) { - SavePath = System.IO.Directory.GetCurrentDirectory(); // Default Save Path to current directory - textBoxPath.Text = SavePath; // Update Display + _savePath = Directory.GetCurrentDirectory(); // Default Save Path to current directory + textBoxPath.Text = _savePath; // Update Display if (!VistaSecurity.IsAdmin()) VistaSecurity.RestartElevated(); // Restart as admin if needed @@ -48,7 +48,7 @@ private void linkLabelCain_LinkClicked(object sender, LinkLabelLinkClickedEventA private void buttonClear_Click(object sender, EventArgs e) { listBoxSelected.Items.Clear(); - Urls.Clear(); + _urls.Clear(); } private void linkLabelSites_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) @@ -59,25 +59,25 @@ private void linkLabelSites_LinkClicked(object sender, LinkLabelLinkClickedEvent private void buttonAdd_Click(object sender, EventArgs e) { - Urls.Add(textBoxAdd.Text); // Add to local list + _urls.Add(textBoxAdd.Text); // Add to local list listBoxSelected.Items.Clear(); // Clear Displayed list listBoxSelected.Items - .AddRange(Urls + .AddRange(_urls .ToArray()); // Add local list into empty displayed list. This approach makes sure that they are always in sync. } private void buttonBrowse_Click(object sender, EventArgs e) { - if (Busy) return; - folderBrowserDialog1.SelectedPath = SavePath; // Make Folder Browser use current path as Root + if (_busy) return; + folderBrowserDialog1.SelectedPath = _savePath; // Make Folder Browser use current path as Root folderBrowserDialog1.ShowDialog(); // Show the Dialog - SavePath = folderBrowserDialog1.SelectedPath; // Save the Folder Path - textBoxPath.Text = SavePath; // Update Display + _savePath = folderBrowserDialog1.SelectedPath; // Save the Folder Path + textBoxPath.Text = _savePath; // Update Display } private async void buttonDownload_Click(object sender, EventArgs e) { - if (Busy) + if (_busy) { buttonDownload.Text = "I'm busy, Please Wait."; MessageBox.Show("I'm busy!", @@ -87,23 +87,20 @@ private async void buttonDownload_Click(object sender, EventArgs e) return; } - System.IO.Directory.SetCurrentDirectory(SavePath); - List tempUrls = new List(); + Directory.SetCurrentDirectory(_savePath); + var tempUrls = new List(); if (VistaSecurity.IsAdmin()) { - Busy = true; + _busy = true; buttonDownload.Text = "Downloading... Please wait"; - foreach (var VARIABLE in listBoxSelected.Items) - { - tempUrls.Add(VARIABLE.ToString()); - } // Hacky code to try and work around the WinForms collection + tempUrls.AddRange(from object variable in listBoxSelected.Items select variable.ToString()); // DownloadUrls(tempUrls); // old way, runs it synchronously await Task.Factory.StartNew(() => DownloadUrls(tempUrls)); // New way, should run async. buttonDownload.Text = "Done!"; - Busy = false; + _busy = false; } else { @@ -115,8 +112,8 @@ private async void buttonDownload_Click(object sender, EventArgs e) private void textBoxPath_TextChanged(object sender, EventArgs e) { - if (Busy) return; - SavePath = textBoxPath.Text; + if (_busy) return; + _savePath = textBoxPath.Text; } } } \ No newline at end of file diff --git a/Ytdl-Gui/VistaSecurity.cs b/Ytdl-Gui/VistaSecurity.cs index 2facf6e..1926a67 100644 --- a/Ytdl-Gui/VistaSecurity.cs +++ b/Ytdl-Gui/VistaSecurity.cs @@ -19,8 +19,8 @@ public class VistaSecurity static internal bool IsAdmin() { - WindowsIdentity id = WindowsIdentity.GetCurrent(); - WindowsPrincipal p = new WindowsPrincipal(id); + var id = WindowsIdentity.GetCurrent(); + var p = new WindowsPrincipal(id); return p.IsInRole(WindowsBuiltInRole.Administrator); } // Checks for Admin @@ -32,14 +32,14 @@ static internal void AddShieldToButton(Button b) internal static void RestartElevated() { - ProcessStartInfo startInfo = new ProcessStartInfo(); + var startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.FileName = Application.ExecutablePath; startInfo.Verb = "runas"; try { - Process p = Process.Start(startInfo); + var p = Process.Start(startInfo); } catch(System.ComponentModel.Win32Exception) {