ShareX/ShareX.HelpersLib/Forms/HashCheckerForm.cs

225 lines
7 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Drawing;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public partial class HashCheckerForm : Form
2013-11-03 23:53:49 +13:00
{
2020-09-13 12:45:40 +12:00
public bool CompareTwoFiles { get; private set; }
private HashChecker hashChecker;
2013-11-03 23:53:49 +13:00
public HashCheckerForm()
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
2023-06-04 10:07:30 +12:00
ShareXResources.ApplyTheme(this, true);
2013-11-03 23:53:49 +13:00
2020-09-13 12:45:40 +12:00
UpdateCompareControls();
2023-12-24 16:47:16 +13:00
UpdateCheckButton();
2013-11-03 23:53:49 +13:00
cbHashType.Items.AddRange(Helpers.GetEnumDescriptions<HashType>());
cbHashType.SelectedIndex = (int)HashType.SHA256;
2013-11-03 23:53:49 +13:00
hashChecker = new HashChecker();
hashChecker.FileCheckProgressChanged += fileCheck_FileCheckProgressChanged;
2021-12-18 10:47:29 +13:00
txtResult.SupportSelectAll();
txtTarget.SupportSelectAll();
2013-11-03 23:53:49 +13:00
}
2023-12-23 20:36:49 +13:00
private void UpdateCompareControls()
{
lblFilePath2.Enabled = txtFilePath2.Enabled = btnFilePathBrowse2.Enabled = CompareTwoFiles;
if (CompareTwoFiles)
{
lblResult.Text = Resources.ResultOfFirstFile;
lblTarget.Text = Resources.ResultOfSecondFile;
}
else
{
lblResult.Text = Resources.Result;
lblTarget.Text = Resources.Target;
}
txtTarget.ReadOnly = CompareTwoFiles;
}
2023-12-24 16:47:16 +13:00
private void UpdateCheckButton()
{
btnStartHashCheck.Enabled = !string.IsNullOrEmpty(txtFilePath.Text) && (!CompareTwoFiles || !string.IsNullOrEmpty(txtFilePath2.Text));
}
private void UpdateResult()
{
if (!string.IsNullOrEmpty(txtResult.Text) && !string.IsNullOrEmpty(txtTarget.Text))
{
2022-10-15 12:10:59 +13:00
if (txtResult.Text.Equals(txtTarget.Text, StringComparison.OrdinalIgnoreCase))
{
txtTarget.BackColor = Color.FromArgb(200, 255, 200);
}
else
{
txtTarget.BackColor = Color.FromArgb(255, 200, 200);
}
2019-06-24 01:59:48 +12:00
txtTarget.ForeColor = SystemColors.ControlText;
}
else
{
2019-06-24 01:59:48 +12:00
txtTarget.BackColor = txtResult.BackColor;
txtTarget.ForeColor = txtResult.ForeColor;
}
}
2023-12-23 20:36:49 +13:00
private void HashCheckerForm_DragEnter(object sender, DragEventArgs e)
2020-09-13 12:45:40 +12:00
{
2023-12-23 20:36:49 +13:00
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
2020-09-13 12:45:40 +12:00
{
2023-12-23 20:36:49 +13:00
e.Effect = DragDropEffects.Copy;
2020-09-13 12:45:40 +12:00
}
else
{
2023-12-23 20:36:49 +13:00
e.Effect = DragDropEffects.None;
2020-09-13 12:45:40 +12:00
}
}
2023-12-23 20:36:49 +13:00
private void HashCheckerForm_DragDrop(object sender, DragEventArgs e)
2013-11-03 23:53:49 +13:00
{
2023-12-23 20:36:49 +13:00
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) && e.Data.GetData(DataFormats.FileDrop, false) is string[] files && files.Length > 0)
{
txtFilePath.Text = files[0];
}
2013-11-03 23:53:49 +13:00
}
2023-12-24 16:47:16 +13:00
private void txtFilePath_TextChanged(object sender, EventArgs e)
{
UpdateCheckButton();
}
2023-12-23 20:36:49 +13:00
private void btnFilePathBrowse_Click(object sender, EventArgs e)
2020-09-13 12:45:40 +12:00
{
2023-12-23 20:36:49 +13:00
FileHelpers.BrowseFile(txtFilePath);
2020-09-13 12:45:40 +12:00
}
private void cbCompareTwoFiles_CheckedChanged(object sender, EventArgs e)
{
CompareTwoFiles = cbCompareTwoFiles.Checked;
UpdateCompareControls();
2023-12-24 16:47:16 +13:00
UpdateCheckButton();
}
private void txtFilePath2_TextChanged(object sender, EventArgs e)
{
UpdateCheckButton();
2020-09-13 12:45:40 +12:00
}
2023-12-23 20:36:49 +13:00
private void txtFilePath2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void txtFilePath2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) && e.Data.GetData(DataFormats.FileDrop, false) is string[] files && files.Length > 0)
{
txtFilePath2.Text = files[0];
}
}
private void btnFilePathBrowse2_Click(object sender, EventArgs e)
{
FileHelpers.BrowseFile(txtFilePath2);
}
2020-09-13 15:04:03 +12:00
private async void btnStartHashCheck_Click(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
if (hashChecker.IsWorking)
2013-11-03 23:53:49 +13:00
{
hashChecker.Stop();
2013-11-03 23:53:49 +13:00
}
else
{
2020-09-13 15:04:03 +12:00
btnStartHashCheck.Text = Resources.Stop;
pbProgress.Value = 0;
txtResult.Text = "";
2020-09-13 15:17:14 +12:00
if (CompareTwoFiles)
{
txtTarget.Text = "";
}
2013-11-03 23:53:49 +13:00
HashType hashType = (HashType)cbHashType.SelectedIndex;
2020-09-13 15:17:14 +12:00
string filePath = txtFilePath.Text;
string result = await hashChecker.Start(filePath, hashType);
2013-11-03 23:53:49 +13:00
2020-09-13 15:17:14 +12:00
if (!string.IsNullOrEmpty(result))
{
txtResult.Text = result.ToUpperInvariant();
if (CompareTwoFiles)
{
string filePath2 = txtFilePath2.Text;
string result2 = await hashChecker.Start(filePath2, hashType);
2020-09-13 15:17:14 +12:00
if (!string.IsNullOrEmpty(result2))
{
txtTarget.Text = result2.ToUpperInvariant();
}
}
}
2023-12-24 16:47:16 +13:00
btnStartHashCheck.Text = Resources.Check;
2013-11-03 23:53:49 +13:00
}
}
private void fileCheck_FileCheckProgressChanged(float progress)
{
2023-12-23 20:36:49 +13:00
pbProgress.Value = (int)Math.Round(progress);
2013-11-03 23:53:49 +13:00
}
private void txtResult_TextChanged(object sender, EventArgs e)
{
UpdateResult();
}
private void txtTarget_TextChanged(object sender, EventArgs e)
{
UpdateResult();
2013-11-03 23:53:49 +13:00
}
}
}