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 ( ) ;
defaultText = introTextBox . Text ; //stores it so we can append to the end at runtime
2014-11-24 17:11:36 +00:00
this . Size = new Size ( 583 , 299 ) ;
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-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-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 ) ;
}
else
{
createRecord ( ) ;
}
while ( this . Size . Height ! = 299 ) {
initHeight - - ;
this . Size = new Size ( 583 , initHeight ) ;
Thread . Sleep ( 10 ) ;
}
}
private void createRecord ( )
{
SqlCeConnection conn = databaseTools . conn ;
SqlCeCommand cmd = conn . CreateCommand ( ) ;
conn . Open ( ) ;
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 ) ) ;
cmd . CommandText = "INSERT INTO Recordset (Name, Drive_ID, Creation_Date, Backup_Location, Automatic, Compression, Previous_Backups) VALUES (?, ?, ?)" ;
cmd . Parameters . Add ( new SqlCeParameter ( "p1" , SqlDbType . Int ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "p2" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "p3" , SqlDbType . Money ) ) ;
cmd . Prepare ( ) ;
cmd . Parameters [ "p1" ] . Value = 1 ;
cmd . Parameters [ "p2" ] . Value = "abc" ;
cmd . Parameters [ "p3" ] . Value = 15.66 ;
cmd . ExecuteNonQuery ( ) ;
2014-11-25 22:20:37 +00:00
2014-11-28 17:06:48 +00:00
cmd . Parameters . Clear ( ) ;
2014-11-25 22:20:37 +00:00
conn . Close ( ) ;
MessageBox . Show ( "Record created successfully!" , "Success" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2014-11-28 17:06:48 +00:00
this . Close ( ) ;
2014-11-25 22:20:37 +00:00
2014-11-24 17:11:36 +00:00
}
2014-11-14 18:34:03 +00:00
}
}