using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using LaptopCommunications;
using SharpUtils;
namespace Laptop_Backup
{
/// <summary>
/// Summary description for EditForm.
/// </summary>
public class EditForm : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox tryBox;
private System.Windows.Forms.CheckBox checkOnStartup;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NumericUpDown BackupInDays;
private System.Windows.Forms.CheckBox SubDirectoriesBox;
private System.Windows.Forms.Label label1;
private TimeSet.TimeSetControl timeSetControl1;
private System.Windows.Forms.CheckBox CheckedCopyBox;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox fileEditBox;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox remoteEditBox;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox localEditBox;
private System.Windows.Forms.Button localButton;
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.Button doneButton;
/// <summary>
/// Log progess as u go
/// </summary>
private SimpleEventLog eventLog;
/// <summary>
/// control the amount of information written
/// </summary>
private DebugLevel debugLevel;
private string strComputerName;
private string strIPAddress;
private string strKeyName;
private string strRemote;
private string strLocation;
private int nHours;
private int nMinutes;
private bool bCheckedMode;
private bool bDirectoryCopy;
private int nDays;
private bool bOnStart;
private bool bTryTilDone;
private bool bUpdate;
public string GetKeyName
{
get
{
return strKeyName;
}
}
public string GetRemote
{
get
{
return strRemote;
}
}
public string GetLocation
{
get
{
return strLocation;
}
}
public int GetHours
{
get
{
return nHours;
}
}
public int GetMinutes
{
get
{
return nMinutes;
}
}
public bool GetCheckedMode
{
get
{
return bCheckedMode;
}
}
public bool GetDirectoryCopy
{
get
{
return bDirectoryCopy;
}
}
public int GetDays
{
get
{
return nDays;
}
}
public bool GetOnStart
{
get
{
return bOnStart;
}
}
public bool GetTryTilDone
{
get
{
return bTryTilDone;
}
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public EditForm( string strComputerName, string strIPAddress, string strKeyName, string strRemote, string strLocation, string strTime, string strCheckedMode, string strDirectoryCopy, string strEveryDay, string strOnStart, string strTryTilDone )
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
debugLevel = new DebugLevel( DebugLevel.currentLevel );
eventLog = new SimpleEventLog( "Laptop Backup", false );
this.strComputerName = strComputerName;
this.strIPAddress = strIPAddress;
this.strKeyName = strKeyName;
this.strRemote = strRemote;
this.strLocation = strLocation;
StringBuilder buffer = new StringBuilder( strTime );
this.nHours = Int32.Parse( buffer.Replace( ':', '\0' ).ToString() );
int nInc = strTime.IndexOf( ":" );
nInc++;
this.nMinutes = Int32.Parse( strTime.Substring( nInc ) );
/// Note when comparing a bool converted to text
/// even though a bool is true it comes out in text as True
/// and CompareTo is case sensitive
if( strCheckedMode.CompareTo( "True" ) == 0 )
this.bCheckedMode = true;
else
this.bCheckedMode = false;
if( strDirectoryCopy.CompareTo( "True" ) == 0 )
this.bDirectoryCopy = true;
else
this.bDirectoryCopy = false;
this.nDays = Int32.Parse( strEveryDay );
if( strOnStart.CompareTo( "True" ) == 0 )
this.bOnStart = true;
else
this.bOnStart = false;
if( strTryTilDone.CompareTo( "True" ) == 0 )
this.bTryTilDone = true;
else
this.bTryTilDone = false;
bUpdate = true;
/// set the controls
fileEditBox.Text = strKeyName;
remoteEditBox.Text = strRemote;
localEditBox.Text = strLocation;
timeSetControl1.Hours = nHours;
timeSetControl1.Minutes = nMinutes;
CheckedCopyBox.Checked = bCheckedMode;
SubDirectoriesBox.Checked = bDirectoryCopy;
checkOnStartup.Checked = bOnStart;
BackupInDays.Value = nDays;
tryBox.Checked = bTryTilDone;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
public bool Updated
{
get
{
return bUpdate;
}
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tryBox = new System.Windows.Forms.CheckBox();
this.checkOnStartup = new System.Windows.Forms.CheckBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.BackupInDays = new System.Windows.Forms.NumericUpDown();
this.SubDirectoriesBox = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.timeSetControl1 = new TimeSet.TimeSetControl();
this.CheckedCopyBox = new System.Windows.Forms.CheckBox();
this.label4 = new System.Windows.Forms.Label();
this.fileEditBox = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.remoteEditBox = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.localEditBox = new System.Windows.Forms.TextBox();
this.localButton = new System.Windows.Forms.Button();
this.cancelButton = new System.Windows.Forms.Button();
this.doneButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.BackupInDays)).BeginInit();
this.SuspendLayout();
//
// tryBox
//
this.tryBox.Location = new System.Drawing.Point(16, 368);
this.tryBox.Name = "tryBox";
this.tryBox.Size = new System.Drawing.Size(240, 24);
this.tryBox.TabIndex = 18;
this.tryBox.Text = "Keep Trying To Copy Until Done";
//
// checkOnStartup
//
this.checkOnStartup.Location = new System.Drawing.Point(16, 304);
this.checkOnStartup.Name = "checkOnStartup";
this.checkOnStartup.TabIndex = 17;
this.checkOnStartup.Text = "Copy On Start";
//
// label3
//
this.label3.Location = new System.Drawing.Point(160, 336);
this.label3.Name = "label3";
this.label3.TabIndex = 16;
this.label3.Text = "Days";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 336);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 23);
this.label2.TabIndex = 15;
this.label2.Text = "Backup Every";
//
// BackupInDays
//
this.BackupInDays.Location = new System.Drawing.Point(104, 336);
this.BackupInDays.Name = "BackupInDays";
this.BackupInDays.Size = new System.Drawing.Size(56, 20);
this.BackupInDays.TabIndex = 14;
//
// SubDirectoriesBox
//
this.SubDirectoriesBox.Location = new System.Drawing.Point(16, 272);
this.SubDirectoriesBox.Name = "SubDirectoriesBox";
this.SubDirectoriesBox.Size = new System.Drawing.Size(248, 24);
this.SubDirectoriesBox.TabIndex = 13;
this.SubDirectoriesBox.Text = "Copy SubDirectories";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 168);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 23);
this.label1.TabIndex = 12;
this.label1.Text = "Set The Copy Time";
//
// timeSetControl1
//
this.timeSetControl1.Hours = new System.Decimal(new int[] {
0,
0,
0,
0});
this.timeSetControl1.Location = new System.Drawing.Point(8, 192);
this.timeSetControl1.Minutes = new System.Decimal(new int[] {
0,
0,
0,
0});
this.timeSetControl1.Name = "timeSetControl1";
this.timeSetControl1.Seconds = new System.Decimal(new int[] {
0,
0,
0,
0});
this.timeSetControl1.ShowHours = true;
this.timeSetControl1.ShowHoursLetter = true;
this.timeSetControl1.ShowMinutes = true;
this.timeSetControl1.ShowMinutesLetter = true;
this.timeSetControl1.ShowSeconds = false;
this.timeSetControl1.ShowSecondsLetter = false;
this.timeSetControl1.Size = new System.Drawing.Size(192, 40);
this.timeSetControl1.TabIndex = 11;
//
// CheckedCopyBox
//
this.CheckedCopyBox.Location = new System.Drawing.Point(16, 240);
this.CheckedCopyBox.Name = "CheckedCopyBox";
this.CheckedCopyBox.Size = new System.Drawing.Size(248, 24);
this.CheckedCopyBox.TabIndex = 10;
this.CheckedCopyBox.Text = "Checked Copy ( Only Newer Files Copied )";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 8);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(224, 16);
this.label4.TabIndex = 19;
this.label4.Text = "File Or Directory";
//
// fileEditBox
//
this.fileEditBox.Location = new System.Drawing.Point(16, 24);
this.fileEditBox.Name = "fileEditBox";
this.fileEditBox.ReadOnly = true;
this.fileEditBox.Size = new System.Drawing.Size(152, 20);
this.fileEditBox.TabIndex = 20;
this.fileEditBox.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 56);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(224, 23);
this.label5.TabIndex = 21;
this.label5.Text = "Remote Location ( From ) ";
//
// remoteEditBox
//
this.remoteEditBox.Location = new System.Drawing.Point(16, 80);
this.remoteEditBox.Name = "remoteEditBox";
this.remoteEditBox.ReadOnly = true;
this.remoteEditBox.Size = new System.Drawing.Size(208, 20);
this.remoteEditBox.TabIndex = 22;
this.remoteEditBox.Text = "textBox1";
//
// label6
//
this.label6.Location = new System.Drawing.Point(16, 112);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(216, 23);
this.label6.TabIndex = 23;
this.label6.Text = "Local Location ( To )";
//
// localEditBox
//
this.localEditBox.Location = new System.Drawing.Point(16, 136);
this.localEditBox.Name = "localEditBox";
this.localEditBox.ReadOnly = true;
this.localEditBox.Size = new System.Drawing.Size(200, 20);
this.localEditBox.TabIndex = 24;
this.localEditBox.Text = "textBox1";
//
// localButton
//
this.localButton.Location = new System.Drawing.Point(240, 136);
this.localButton.Name = "localButton";
this.localButton.TabIndex = 25;
this.localButton.Text = "Change";
this.localButton.Click += new System.EventHandler(this.OnChangeLocalLocation);
//
// cancelButton
//
this.cancelButton.Location = new System.Drawing.Point(280, 328);
this.cancelButton.Name = "cancelButton";
this.cancelButton.TabIndex = 26;
this.cancelButton.Text = "Cancel";
this.cancelButton.Click += new System.EventHandler(this.OnCancelButton);
//
// doneButton
//
this.doneButton.Location = new System.Drawing.Point(280, 368);
this.doneButton.Name = "doneButton";
this.doneButton.TabIndex = 27;
this.doneButton.Text = "Done";
this.doneButton.Click += new System.EventHandler(this.OnDoneButton);
//
// EditForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(368, 414);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.doneButton,
this.cancelButton,
this.localButton,
this.localEditBox,
this.label6,
this.remoteEditBox,
this.label5,
this.fileEditBox,
this.label4,
this.tryBox,
this.checkOnStartup,
this.label3,
this.label2,
this.BackupInDays,
this.SubDirectoriesBox,
this.label1,
this.timeSetControl1,
this.CheckedCopyBox});
this.Name = "EditForm";
this.Text = "EditForm";
((System.ComponentModel.ISupportInitialize)(this.BackupInDays)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Change the location the file/directory will be saved to on the current computer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnChangeLocalLocation(object sender, System.EventArgs e)
{
AddNew dest = new AddNew( "localhost", "This Computer", true );
/// 27 July 2004 Empty String bug fix
///
if( dest.ShowDialog( this ) == DialogResult.OK )
{
strLocation = dest.GetDestination;
StringBuilder strTemp = new StringBuilder( strLocation );
strTemp.Remove( 0, "LocalHost\\".Length );
localEditBox.Text = strTemp.ToString();
}
/// 27 July 2004 Empty string bug fix
///
}
/// <summary>
/// Cancel the edit
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnCancelButton(object sender, System.EventArgs e)
{
/// 7 September 2004
/// dialog result fix
///
DialogResult = DialogResult.Cancel;
this.Close();
}
/// <summary>
/// Save the edited information
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDoneButton(object sender, System.EventArgs e)
{
/// store the data
strKeyName = fileEditBox.Text;
strRemote = remoteEditBox.Text;
strLocation = localEditBox.Text;
nHours = ( int )timeSetControl1.Hours;
nMinutes = ( int )timeSetControl1.Minutes;
bCheckedMode = CheckedCopyBox.Checked;
bDirectoryCopy = SubDirectoriesBox.Checked;
nDays = ( int )BackupInDays.Value;
bTryTilDone = tryBox.Checked;
bOnStart = checkOnStartup.Checked;
Save();
/// 7 September 2004
/// Dialog result fix
///
DialogResult = DialogResult.OK;
this.Close();
}
/// <summary>
/// save the edited information
/// </summary>
/// <returns></returns>
private bool Save()
{
QuickRegistry reg = new QuickRegistry();
/// open the registry key
if( reg.OpenKey( "HKEY_LOCAL_MACHINE", "Software\\Laptop Backup", true ) == false )
{
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Add Error " + reg.ErrorMessage );
}
MessageBox.Show( this, "Unable To Add " + strKeyName+ "\nDue To Registry Error " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
}
/// If unable to open the key for the computer from here create it
if( reg.OpenKeyFromCurrentKey( strComputerName, true ) == false )
{
reg.RevertToPrevious();
if( reg.CreateKey( strComputerName, true, true ) == false )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Add Error " + reg.ErrorMessage );
}
MessageBox.Show( this, "Unable To Add " + strKeyName+ "\nDue To Registry Error " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
else
{
/// save the ipaddress here as if the user exits without doing a save and exit the
/// code falls over cause there is no ip address to load
reg.SetValue( "IPAddress", strIPAddress );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to add the ipaddress for " + strComputerName + " due to " + reg.ErrorMessage );
}
}
if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
{
eventLog.WriteInformation( "Added the key " + strComputerName );
}
}
}
/// Add the back up info to the registry
if( reg.OpenKeyFromCurrentKey( strKeyName, true ) == false )
{
/// don't message user as should happen frequently
reg.RevertToPrevious();
if( debugLevel.TestDebugLevel( DebugLevelSet.WarningsAndErrors ) == true )
{
eventLog.WriteWarning( "Unable to open " + strKeyName + " attempting to create it" );
}
if( reg.CreateKey( strKeyName, true, true ) == false )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to create registry key " + strKeyName + reg.ErrorMessage );
}
MessageBox.Show( this, "There is an Error opening the registry key " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
}
reg.SetValue( "Name", strKeyName );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to Name for " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName+ "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "FullPath", strRemote );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to FullPath for " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName+ "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "CheckedCopy", bCheckedMode );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to CheckedCopy " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName+ "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "DirectoryCopy", bDirectoryCopy );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to DirectoryCopy " + strKeyName+ " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName+ "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "CopyOnStart", bOnStart );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write date to CopyOnStart " + strKeyName+ " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName + "\n reason " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "Hours", nHours );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to Hours" + strKeyName+ " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName + "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "Minutes", nMinutes );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to Minutes " + strKeyName+ " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName + "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
/* Dont bother with seconds
reg.SetValue( "Seconds", nSeconds );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( "Error writing the values for " + strKeyName + "\n reason = " + reg.ErrorMessage );
return;
}
*/
StringBuilder strFixLocation = new StringBuilder( "LocalHost\\" );
strFixLocation.Append( strLocation );
reg.SetValue( "Location", strFixLocation.ToString() );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to Location " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName + "\n reason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "NumDays", nDays );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write data to NumDays " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( "Error writing the values for " + strKeyName + "\n reason = " + reg.ErrorMessage );
return false;
}
reg.SetValue( "TryTilDone", bTryTilDone );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write date to TryTilDone " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName + "\nreason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
reg.SetValue( "Done", "False" );
if( reg.Error == true )
{
if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
{
eventLog.WriteError( "Unable to write the data to Done " + strKeyName + " reason " + reg.ErrorMessage );
}
MessageBox.Show( this, "Error writing the values for " + strKeyName + "\nreason = " + reg.ErrorMessage, "Laptop Backup" );
return false;
}
return true;
}
}
}