1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
saviour-backup-system/Saviour Backup System/mainWindow.cs

176 lines
7 KiB
C#

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace Saviour_Backup_System
{
public partial class mainWindow : Form
{
private DriveInfo selectedDrive;
public mainWindow() {
InitializeComponent();
try
{
if (setup.runtimeArguements[0] == "STARTUP") { this.removeDisplay(); }
else { this.showDisplay(); }
} catch { }
}
public void removeDisplay() {
formatDriveCapacityTimer.Stop();
driveRefreshTimer.Stop();
connectedDrivesList.Update();
clearDriveDetails();
this.Hide();
setup.icon.notifyIcon.Visible = true;
}
public void showDisplay() {
refreshDriveList();
connectedDrivesList.Update();
connectedDrivesList.Items[0].Selected = true;
displayDriveDetails(connectedDrivesList.SelectedItems[0].Text.Substring(0, 3));
formatDriveCapacityTimer.Start();
driveRefreshTimer.Start();
setup.icon.notifyIcon.Visible = false;
this.Show();
}
public void refreshDriveList() {
DriveInfo[] drives = USBTools.getConnectedDrives();
if (connectedDrivesList.Items.Count == USBTools.countDrives()) { return; } //if there is no change in the numer
connectedDrivesList.Items.Clear();
bool deviceStillConnected = false;
foreach (DriveInfo drive in drives){
try {
ListViewItem driveItem = new ListViewItem(drive.Name + " " + tools.Trim(drive.VolumeLabel, 24));
driveItem.SubItems.Add("X");
connectedDrivesList.Items.Add(driveItem);
if (drive.VolumeLabel == driveNameDisplay.Text)
{
deviceStillConnected = true;
driveItem.Selected = true;
}
}
catch { continue; }
}
if (!deviceStillConnected) { clearDriveDetails(); }
connectedDrivesList.Sort();
}
private void connectedDrivesListRefresh_Click(object sender, EventArgs e)
{
foreach (ListViewItem i in connectedDrivesList.SelectedItems){ i.Selected = false; } //Deselected all elements in the list first
clearDriveDetails();
refreshDriveList();
}
private void connectedDrivesList_Selection(object sender, ListViewItemSelectionChangedEventArgs e) {
if (!e.IsSelected) {
deviceTab.Visible = false;
backupRestoreTab.Select();
return;
}
formatDriveCapacityTimer.Start();
ribbonControl.RecalcLayout();
populateDeviceTab();
string selectedDevice = connectedDrivesList.SelectedItems[0].Text;
displayDriveDetails(selectedDevice.Substring(0,3));
}
private void displayDriveDetails(string driveName)
{
foreach (DriveInfo drive in USBTools.getConnectedDrives()) {
if (drive.Name == driveName) { selectedDrive = drive; break; }
}
driveNameDisplay.Text = tools.Trim(selectedDrive.VolumeLabel, 16);
driveLetterDisplay.Text = selectedDrive.Name;
driveSystemDisplay.Text = selectedDrive.DriveFormat;
driveTypeDisplay.Text = USBTools.getDriveType(selectedDrive);
switch (USBTools.getDriveType(selectedDrive))
{
case "Optical Disk Drive":
driveIconBox.Image = Properties.Resources.cdIcon;
break;
case "Removable Drive":
driveIconBox.Image = Properties.Resources.usbIcon;
break;
case "Fixed Disk Drive":
driveIconBox.Image = Properties.Resources.hddIcon;
break;
}
}
private void formatDriveCapacity()
{
DriveInfo drive = selectedDrive;
int driveCapacityPerc = USBTools.spacePercentage(drive);
driveCapacityDisplay.Text = "Drive Capacity: " + (driveCapacityDisplay.Value / 100).ToString() + "%";
while (driveCapacityPerc != driveCapacityDisplay.Value) {
if (driveCapacityPerc < driveCapacityDisplay.Value) { driveCapacityDisplay.Value -= 1; }
if (driveCapacityPerc > driveCapacityDisplay.Value) { driveCapacityDisplay.Value += 1; }
driveCapacityDisplay.Text = "Drive Capacity: " + (driveCapacityDisplay.Value / 100).ToString() + "%";
//adjust the colour of the progressbar depending on capacity of the drive
if (driveCapacityDisplay.Value <= 6800) { driveCapacityDisplay.ColorTable = DevComponents.DotNetBar.eProgressBarItemColor.Normal; }
else if (driveCapacityDisplay.Value > 6800 && driveCapacityDisplay.Value < 9000) { driveCapacityDisplay.ColorTable = DevComponents.DotNetBar.eProgressBarItemColor.Paused; }
else if (driveCapacityDisplay.Value >= 9000) { driveCapacityDisplay.ColorTable = DevComponents.DotNetBar.eProgressBarItemColor.Error; }
}
formatDriveCapacityTimer.Stop();
}
private void clearDriveDetails() {
string blankText = "";
selectedDrive = null;
driveNameDisplay.Text = blankText;
driveLetterDisplay.Text = blankText;
driveSystemDisplay.Text = blankText;
driveTypeDisplay.Text = blankText;
driveCapacityDisplay.Value = 0;
driveCapacityDisplay.Text = blankText;
}
private void driveRefreshTimer_Tick(object sender, EventArgs e) { refreshDriveList(); }
private void formatDriveCapacityTimer_Tick(object sender, EventArgs e) { try { formatDriveCapacity(); } catch { } } //Because background workers cant interact with the GUI (very quickly), so a timer has to
private void populateDeviceTab() {
// put stuff in here!!
deviceTab.Visible = true;
deviceTab.Select();
}
private void mainWindow_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = true;
switch (e.CloseReason) {
case(CloseReason.ApplicationExitCall):
case(CloseReason.WindowsShutDown):
formatDriveCapacityTimer.Stop();
driveRefreshTimer.Stop();
this.Close();
break;
case(CloseReason.UserClosing):
this.removeDisplay();
setup.icon.displayStillRunning();
break;
default:
this.Close();
break;
}
}
}
}