2014-11-14 18:34:03 +00:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
2014-11-24 17:11:36 +00:00
using System.Threading ;
2014-11-14 18:34:03 +00:00
using System.Windows.Forms ;
2014-11-15 13:44:37 +00:00
using System.IO ;
2014-11-25 22:20:37 +00:00
using System.Data.SqlServerCe ;
2014-11-14 18:34:03 +00:00
namespace Saviour_Backup_System
{
public partial class addBackupWizard : Form
{
2014-11-16 17:20:18 +00:00
private string defaultText ;
2014-11-14 18:34:03 +00:00
public addBackupWizard ( )
{
InitializeComponent ( ) ;
2014-11-16 17:20:18 +00:00
populateDropdown ( ) ;
2014-12-08 11:53:43 +00:00
assignToolTips ( ) ;
2014-11-24 17:11:36 +00:00
this . Size = new Size ( 583 , 299 ) ;
2014-11-15 13:44:37 +00:00
}
2014-12-08 11:53:43 +00:00
private void assignToolTips ( ) {
ToolTip tempTip = new ToolTip ( ) ;
tempTip . AutoPopDelay = 5000 ;
tempTip . InitialDelay = 1000 ;
tempTip . ReshowDelay = 500 ;
tempTip . ShowAlways = true ;
//huge list of tooltips to use!
tempTip . SetToolTip ( this . backupNameInput , "Name the backup\nAn easy name for the backup, or even a description." ) ;
tempTip . SetToolTip ( this . drivesDropdown , "Select the drive\nWhich drive would you like to backup?" ) ;
tempTip . SetToolTip ( this . compressionTypeDropdown , "Compression?\nWould you like to compress the backup to save space on your computer?" ) ;
tempTip . SetToolTip ( this . previousBackupInput , "Previous backups\nHow many past backups would you like to store, enter -1 for all" ) ;
tempTip . SetToolTip ( this . insertionSwitch , "Automated\nWould you like to backup the drive as soon as it is inserted to the computer?" ) ;
tempTip . SetToolTip ( this . unifiedFileSwitch , "Single File\nWould you like to store the backup in a single file?" ) ;
tempTip . SetToolTip ( this . folderPath , "Location\nWhere would you like to store the backup?" ) ;
tempTip . SetToolTip ( this . createButton , "Let's Go!\nClick to create the backup record, this can take a few seconds to run." ) ;
tempTip . SetToolTip ( this . directoryBrowseButton , "Where?\nClick here to browse your computer to find where to store the backup." ) ;
}
2014-11-15 13:44:37 +00:00
private void directoryBrowseButton_Click ( object sender , EventArgs e )
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog ( ) ;
if ( folderBrowserDialog . ShowDialog ( ) = = DialogResult . OK )
{
2014-11-16 17:20:18 +00:00
folderPath . Text = folderBrowserDialog . SelectedPath ;
2014-11-15 13:44:37 +00:00
}
folderBrowserDialog . Dispose ( ) ; //memory management
}
private void populateDropdown ( )
{
DriveInfo [ ] drives = USBTools . getConnectedDrives ( ) ;
foreach ( DriveInfo drive in drives )
{
2014-11-17 16:11:08 +00:00
drivesDropdown . Items . Add ( drive . Name + " " + drive . VolumeLabel ) ;
2014-11-15 13:44:37 +00:00
}
2014-11-14 18:34:03 +00:00
}
2014-12-04 23:15:29 +00:00
private void clearControls ( )
{
backupNameInput . Text = "" ;
drivesDropdown . Text = "" ;
compressionTypeDropdown . Text = "" ;
insertionSwitch . Value = false ;
unifiedFileSwitch . Value = false ;
previousBackupInput . Value = 0 ;
folderPath . Text = "" ;
}
2014-11-28 17:06:48 +00:00
private void lockControls ( bool state )
{
backupNameInput . ReadOnly = state ;
drivesDropdown . Enabled = ! state ;
compressionTypeDropdown . Enabled = ! state ;
insertionSwitch . IsReadOnly = state ;
unifiedFileSwitch . IsReadOnly = state ;
previousBackupInput . Enabled = ! state ;
folderPath . ReadOnly = state ;
}
2014-11-25 22:20:37 +00:00
private void createButton_Click ( object sender , EventArgs e ) {
2014-12-08 15:19:24 +00:00
DriveInfo drive = USBTools . getDriveObject ( drivesDropdown . Text . Substring ( 0 , 1 ) ) ;
2014-11-28 17:06:48 +00:00
lockControls ( true ) ;
if ( ( folderPath . Text = = "" ) | | ( previousBackupInput . Text = = "" ) | | ( compressionTypeDropdown . Text = = "" ) | | ( drivesDropdown . Text = = "" ) | | ( backupNameInput . Text = = "" ) ) {
2014-11-25 22:20:37 +00:00
MessageBox . Show ( "You have not filled in every element, Please try again!" , "Not everything is complete" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2014-11-28 17:06:48 +00:00
lockControls ( false ) ;
2014-11-25 22:20:37 +00:00
return ;
}
2014-11-24 17:11:36 +00:00
statusProgress . Text = "Initialising..." ;
int initHeight = 299 ;
while ( this . Size . Height ! = 330 ) {
initHeight + + ;
this . Size = new Size ( 583 , initHeight ) ;
Thread . Sleep ( 10 ) ;
}
2014-11-25 22:20:37 +00:00
statusProgress . Text = "Checking form..." ;
if ( ! Directory . Exists ( folderPath . Text ) ) {
DialogResult result = MessageBox . Show ( "The folder path you have entered doesnt exist, would you like to create it?" , "Create Folder" , MessageBoxButtons . YesNo ) ;
if ( result = = DialogResult . Yes ) {
try { Directory . CreateDirectory ( folderPath . Text ) ;
} catch {
MessageBox . Show ( "Error Creating Folder! Please check the path and try agian." , "Error creating folder" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2014-11-24 17:11:36 +00:00
}
}
}
2014-11-25 22:20:37 +00:00
else if ( compressionTypeDropdown . Text = = "None" & & unifiedFileSwitch . Value = = true ) {
MessageBox . Show ( "You cannot have a unified file without some form of compression, please select again." , "Compression Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2014-12-08 15:19:24 +00:00
} else if ( drive . VolumeLabel = = "" ) {
MessageBox . Show ( "You cannot backup a drive with no label, please rename it and try again" , "Can't use default name" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2014-12-04 23:15:29 +00:00
} else {
statusProgress . Text = "Initialising database connection..." ;
2014-11-25 22:20:37 +00:00
createRecord ( ) ;
2014-12-04 23:15:29 +00:00
statusProgress . Text = "Complete!" ;
statusProgress . ProgressType = DevComponents . DotNetBar . eProgressItemType . Standard ;
statusProgress . Value = statusProgress . Maximum ;
MessageBox . Show ( "Record created successfully!\nYou may now backup your drive." , "Success" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
clearControls ( ) ;
this . Close ( ) ;
2014-11-25 22:20:37 +00:00
}
while ( this . Size . Height ! = 299 ) {
initHeight - - ;
this . Size = new Size ( 583 , initHeight ) ;
Thread . Sleep ( 10 ) ;
}
}
2014-12-02 13:38:30 +00:00
private void createRecord ( ) {
2014-11-25 22:20:37 +00:00
SqlCeConnection conn = databaseTools . conn ;
SqlCeCommand cmd = conn . CreateCommand ( ) ;
2014-12-02 13:38:30 +00:00
DriveInfo drive = USBTools . getDriveObject ( drivesDropdown . Text . Substring ( 0 , 1 ) ) ;
2014-11-25 22:20:37 +00:00
conn . Open ( ) ;
2014-12-04 23:15:29 +00:00
statusProgress . Text = "Connection established..." ;
2014-12-02 13:38:30 +00:00
2014-11-28 17:06:48 +00:00
cmd . CommandText = "INSERT INTO Drive (ID, Name, Capacity, File_System, Type) VALUES (?,?,?,?,?)" ;
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Drive Name" , SqlDbType . NText ) ) ;
2014-12-08 15:19:24 +00:00
cmd . Parameters . Add ( new SqlCeParameter ( "Capacity" , SqlDbType . BigInt ) ) ;
2014-12-02 13:38:30 +00:00
cmd . Parameters . Add ( new SqlCeParameter ( "File System" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Type" , SqlDbType . NText ) ) ;
2014-11-28 17:06:48 +00:00
cmd . Prepare ( ) ;
2014-12-04 12:38:23 +00:00
cmd . Parameters [ "Drive ID" ] . Value = USBTools . calculateDriveID ( drive ) ;
2014-12-02 13:38:30 +00:00
cmd . Parameters [ "Drive Name" ] . Value = drive . VolumeLabel ;
2014-12-08 15:19:24 +00:00
cmd . Parameters [ "Capacity" ] . Value = Convert . ToInt64 ( drive . TotalSize ) ;
2014-12-02 13:38:30 +00:00
cmd . Parameters [ "File System" ] . Value = drive . DriveFormat ;
cmd . Parameters [ "Type" ] . Value = USBTools . getDriveType ( drive ) ;
cmd . ExecuteNonQuery ( ) ;
cmd . Parameters . Clear ( ) ;
2014-12-04 23:15:29 +00:00
statusProgress . Text = "Drive Record Created..." ;
2014-11-28 17:06:48 +00:00
2014-12-04 23:15:29 +00:00
cmd . CommandText = "INSERT INTO Recordset VALUES (?, ?, ?, ?, ?, ?, ?)" ;
cmd . Parameters . Add ( new SqlCeParameter ( "Name" , SqlDbType . NText ) ) ;
2014-12-02 13:38:30 +00:00
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Creation Date" , SqlDbType . BigInt ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Backup Location" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Automatic" , SqlDbType . Bit ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Compression" , SqlDbType . Bit ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Previous Backups" , SqlDbType . Int ) ) ;
cmd . Prepare ( ) ;
cmd . Parameters [ "Name" ] . Value = backupNameInput . Text ;
2014-12-04 12:38:23 +00:00
cmd . Parameters [ "Drive ID" ] . Value = USBTools . calculateDriveID ( drive ) ;
2014-12-03 12:18:32 +00:00
cmd . Parameters [ "Creation Date" ] . Value = tools . getUnixTimeStamp ( ) ;
cmd . Parameters [ "Backup Location" ] . Value = folderPath . Text ;
2014-12-04 23:15:29 +00:00
cmd . Parameters [ "Automatic" ] . Value = insertionSwitch . Value ;
cmd . Parameters [ "Compression" ] . Value = unifiedFileSwitch . Value ;
2014-12-03 12:18:32 +00:00
cmd . Parameters [ "Previous Backups" ] . Value = previousBackupInput . Value ;
2014-11-28 17:06:48 +00:00
cmd . ExecuteNonQuery ( ) ;
cmd . Parameters . Clear ( ) ;
2014-12-04 23:15:29 +00:00
statusProgress . Text = "Recordset created..." ;
2014-11-25 22:20:37 +00:00
conn . Close ( ) ;
2014-11-24 17:11:36 +00:00
}
2014-12-04 23:15:29 +00:00
private void insertionSwitch_Click ( object sender , EventArgs e ) { insertionSwitch . Value = ! insertionSwitch . Value ; }
private void unifiedFileSwitch_Click ( object sender , EventArgs e ) { unifiedFileSwitch . Value = ! unifiedFileSwitch . Value ; }
2014-11-14 18:34:03 +00:00
}
}