2014-11-20 14:27:37 +00:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
using System.Windows.Forms ;
2014-11-22 22:46:09 +00:00
using System.IO ;
2014-11-20 14:27:37 +00:00
namespace Saviour_Backup_System
{
public partial class currentTransfers : Form
{
2014-11-21 22:30:32 +00:00
public List < CopyFiles . CopyFiles > copyFilesList = new List < CopyFiles . CopyFiles > ( ) ;
public List < transferWindow > transfersList = new List < transferWindow > ( ) ;
public List < DevComponents . DotNetBar . Controls . ProgressBarX > progressBars = new List < DevComponents . DotNetBar . Controls . ProgressBarX > ( ) ;
public int backups = - 1 ; //used for index in arrays
2014-12-29 18:56:18 +00:00
public currentTransfers ( ) {
2014-11-20 14:27:37 +00:00
InitializeComponent ( ) ;
}
2015-01-06 12:44:08 +00:00
2015-02-02 00:40:30 +00:00
/// <summary>
/// Starts a backup for a drive
/// </summary>
/// <param name="drive">Drive object of backup drive</param>
/// <param name="endDirectory">Directory to store files</param>
/// <param name="visible">Will the progress window be displayed?</param>
2014-12-04 23:18:35 +00:00
public void startCopy ( DriveInfo drive , string endDirectory , bool visible ) { //used for validation to make sure the copy wont fail.
2015-02-06 13:16:22 +00:00
if ( ! Directory . Exists ( drive . Name ) ) { MessageBox . Show ( "The drive directory does not exist." ) ; } //Check if the drive is still attached
2014-12-04 23:18:35 +00:00
2015-02-06 13:16:22 +00:00
else if ( ! Directory . Exists ( endDirectory ) ) { MessageBox . Show ( "The end directory does not exist." ) ; } //Check if the directory exists
2014-12-04 23:18:35 +00:00
2014-12-15 17:01:36 +00:00
else { //error checking is done (just in case something slips through.
2015-02-06 13:16:22 +00:00
string hash = tools . hashDirectory ( drive . Name ) ; //Generate a hash of the drive in current state
2014-12-12 18:03:02 +00:00
string DBHash = databaseTools . getHashofRecentBackup ( USBTools . calculateDriveID ( drive ) ) ; //get the hash from the database
2015-01-25 14:56:44 +00:00
if ( DBHash ! = "NONE" ) { //if the hash is found in the database
2014-12-15 17:01:36 +00:00
if ( DBHash = = hash ) { //if the hash in the database matches the drive, don't backup because nothing will have changed.
2014-12-12 18:03:02 +00:00
MessageBox . Show ( "No changes have been made to files on drive " + drive . VolumeLabel + ", Will not backup." , "No Changes" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
return ;
2014-12-29 18:56:18 +00:00
} else { //The actual backup case
2015-01-25 14:56:44 +00:00
string addition ; //stores any extra directory needed, mainly for adding temp.
2015-02-06 13:16:22 +00:00
if ( databaseTools . isCompression ( USBTools . calculateDriveID ( drive ) ) ) //is the drive using compression
2015-01-06 12:44:08 +00:00
{
2015-02-06 13:16:22 +00:00
addition = "\\Temp" ; //Append temp to directory for backup
2015-01-06 12:44:08 +00:00
}
2015-02-06 13:16:22 +00:00
else { addition = "\\" + drive . VolumeLabel + "-" + DateTime . Now . ToString ( ) ; } //Generate directory with date / Time
copyFiles ( drive . Name . Substring ( 0 , 1 ) , endDirectory + addition , visible , drive , hash ) ; //Initiate the copy
2014-12-12 18:03:02 +00:00
}
2014-12-29 18:56:18 +00:00
} else {
2015-02-06 13:16:22 +00:00
MessageBox . Show ( "An error occured when getting the drive hash. Please try again." , "Hash Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ; //Hashes dont match
2014-12-15 17:01:36 +00:00
return ;
2014-12-12 18:03:02 +00:00
}
2014-12-08 18:04:33 +00:00
}
}
2015-02-02 00:40:30 +00:00
/// <summary>
/// Run a backup of a drive
/// </summary>
/// <param name="driveLetter">Windows drive letter for drive</param>
/// <param name="endDirectory">Directory to store backed up files</param>
/// <param name="display">Will the progress window be displayed</param>
/// <param name="drive">Drive object for backup drive</param>
/// <param name="hash">Current hash of drive</param>
private void copyFiles ( string driveLetter , string endDirectory , bool display , DriveInfo drive , string hash )
2014-11-20 15:58:30 +00:00
{
2014-11-21 22:30:32 +00:00
backups + + ; //appends to the number of backups running
2015-02-06 13:16:22 +00:00
progressBars . Add ( new copyProgressBar ( ) ) ; //Create the display progressbar and store it for reference
layoutPanel . Controls . Add ( new copyProgressLabel ( ) ) ; //Create the display label and add it to display
layoutPanel . Controls . Add ( progressBars [ backups ] ) ; //Display the progress bar and add it to display
2014-11-22 22:46:09 +00:00
2015-02-06 13:16:22 +00:00
copyFilesList . Add ( new CopyFiles . CopyFiles ( driveLetter + ":\\" , endDirectory ) ) ; //Generate copying oject, and pass it details of copy
2014-11-20 15:58:30 +00:00
2015-02-06 13:16:22 +00:00
transfersList . Add ( new transferWindow ( backups , drive , hash , endDirectory ) ) ; //Generate progress window, and pass it details
2014-11-21 22:30:32 +00:00
transfersList [ backups ] . SynchronizationObject = this ;
2015-02-06 13:16:22 +00:00
copyFilesList [ backups ] . CopyAsync ( transfersList [ backups ] ) ; //Initiate the backup (in a new thread) from reference to copying object
2014-11-22 22:46:09 +00:00
2014-12-30 17:30:34 +00:00
if ( ! display ) { transfersList [ backups ] . Hide ( ) ; } //if it is a startup backup process, quickly hide the dialog.
2014-11-20 15:58:30 +00:00
}
2014-11-21 22:30:32 +00:00
2015-02-02 00:40:30 +00:00
/// <summary>
/// Label for displaying backup information
/// </summary>
2014-11-24 17:12:01 +00:00
private class copyProgressLabel : Label {
2014-11-22 22:46:09 +00:00
public copyProgressLabel ( )
2014-11-21 22:30:32 +00:00
{
2014-11-22 22:46:09 +00:00
base . InitLayout ( ) ;
this . Font = new Font ( "Lucida Sans Unicode" , 9F , System . Drawing . FontStyle . Regular , System . Drawing . GraphicsUnit . Point , ( ( byte ) ( 0 ) ) ) ;
this . Text = "Content" ;
this . Margin = new Padding ( 20 , 3 , 0 , 0 ) ;
2014-11-21 22:30:32 +00:00
}
}
2015-02-02 00:40:30 +00:00
/// <summary>
/// Progressbar for displaying backup progress
/// </summary>
2014-11-22 22:46:09 +00:00
private class copyProgressBar : DevComponents . DotNetBar . Controls . ProgressBarX
{
public copyProgressBar ( )
{
base . InitLayout ( ) ;
this . Style = DevComponents . DotNetBar . eDotNetBarStyle . StyleManagerControlled ;
this . Text = "Backup in Progress" ;
this . TextVisible = true ;
this . Size = new Size ( 567 , 27 ) ;
this . Margin = new Padding ( 12 , 0 , 0 , 17 ) ;
this . ColorTable = DevComponents . DotNetBar . eProgressBarItemColor . Paused ;
}
}
private void button1_Click ( object sender , EventArgs e )
{
}
private void currentTransfers_FormClosing ( object sender , FormClosingEventArgs e )
{
e . Cancel = true ;
this . Hide ( ) ;
}
2014-12-30 17:30:34 +00:00
2015-02-02 00:40:30 +00:00
/// <summary>
/// Clears the window of all controls
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2014-12-30 17:30:34 +00:00
private void ClearButton_Click ( object sender , EventArgs e )
{
DialogResult result = MessageBox . Show ( "Are you sure you want to clear?\nThis will cancel all active transfers!" , "Clear Window" , MessageBoxButtons . YesNo , MessageBoxIcon . Exclamation ) ;
if ( result = = System . Windows . Forms . DialogResult . Yes ) {
foreach ( transferWindow TW in transfersList ) { TW . cancelCopy ( ) ; } //cancels all the copies as quickly as possible
copyFilesList . Clear ( ) ;
layoutPanel . Controls . Clear ( ) ;
progressBars . Clear ( ) ;
MessageBox . Show ( "All items have been cleared!" ) ;
}
else { return ; }
}
2014-11-20 14:27:37 +00:00
}
}