Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

Commit

Permalink
Refactor to improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowsink committed May 21, 2020
1 parent 570a86c commit 7d233eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
53 changes: 25 additions & 28 deletions 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;
Expand All @@ -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<string> Urls = new List<string>();
public static bool Busy = false;
private static string _savePath;
private static List<string> _urls = new List<string>();
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
Expand All @@ -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)
Expand All @@ -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!",
Expand All @@ -87,23 +87,20 @@ private async void buttonDownload_Click(object sender, EventArgs e)
return;
}

System.IO.Directory.SetCurrentDirectory(SavePath);
List<string> tempUrls = new List<string>();
Directory.SetCurrentDirectory(_savePath);
var tempUrls = new List<string>();
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
{
Expand All @@ -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;
}
}
}
8 changes: 4 additions & 4 deletions Ytdl-Gui/VistaSecurity.cs
Expand Up @@ -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

Expand All @@ -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)
{
Expand Down

0 comments on commit 7d233eb

Please sign in to comment.