home *** CD-ROM | disk | FTP | other *** search
- unit SampleForm;
- {------------------------------------------------------------------------------}
- { SampleForm.pas: Modified text saved and backed up every minute. User can }
- { restore any backed up version of the text. }
- { }
- { Part of the Delphi 4 Sample application for the Xceed Backup Library }
- { Copyright (C) 1999 - Xceed Software Inc. }
- {------------------------------------------------------------------------------}
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ComCtrls, StdCtrls, XceedBackupLib_TLB, OleCtrls, RollbackForm, ExtCtrls;
-
- type
- TfrmSample = class(TForm)
- Label1: TLabel;
- mmoText: TMemo;
- btRollback: TButton;
- barStatus: TStatusBar;
- btQuit: TButton;
- xMain: TXceedBackup;
- xScheduled: TScheduledJob;
- xRestore: TRestoreJob;
- xBackup: TBackupJob;
- xFiles: TFileSelection;
- lblClock: TLabel;
- tmrClock: TTimer;
- procedure xMainStartingBackup(Sender: TObject;
- const xBackupJob: IXceedBackupJob; dtBackupDate: TDateTime;
- var sMediaLabelPattern: WideString; var bPreventLaunch: WordBool);
- procedure xMainProcessCompleted(Sender: TObject; lFilesTotal,
- lFilesProcessed, lFilesSkipped, lBytesTotal, lBytesProcessed,
- lBytesSkipped: Integer; xResult: TOleEnum);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure btRollbackClick(Sender: TObject);
- procedure btQuitClick(Sender: TObject);
- procedure mmoTextChange(Sender: TObject);
- procedure tmrClockTimer(Sender: TObject);
- private
- { Private declarations }
- FDirty : boolean; { Determines if text field needs to be saved }
- public
- { Public declarations }
- end;
-
- var
- frmSample: TfrmSample;
-
- implementation
-
- {$R *.DFM}
-
- {------------------------------------------------------------------------------}
- { StartingBackup event: Triggered everytime a backup job is starting, manually }
- { or scheduled. }
- {------------------------------------------------------------------------------}
- procedure TfrmSample.xMainStartingBackup(Sender: TObject;
- const xBackupJob: IXceedBackupJob; dtBackupDate: TDateTime;
- var sMediaLabelPattern: WideString; var bPreventLaunch: WordBool);
- begin
- { Let's save the file only when about to backup }
- if FDirty then
- begin
- mmoText.Lines.SaveToFile( ExtractFilePath( Application.ExeName ) + 'TextField.txt' );
- FDirty := false;
- end;
-
- btRollback.Enabled := false;
- barStatus.Panels[0].Text := 'Starting backup...';
- end;
-
- {------------------------------------------------------------------------------}
- { ProcessCompleted event: Triggered every time a TXceedBackup method has }
- { completed, and also when a scheduled job has finished. }
- {------------------------------------------------------------------------------}
- procedure TfrmSample.xMainProcessCompleted(Sender: TObject; lFilesTotal,
- lFilesProcessed, lFilesSkipped, lBytesTotal, lBytesProcessed,
- lBytesSkipped: Integer; xResult: TOleEnum);
- begin
- btRollback.Enabled := true;
- barStatus.Panels[0].Text := xMain.GetErrorDescription( bvtError, xResult );
- end;
-
- {------------------------------------------------------------------------------}
- { FormCreate: The main form is about to be created. We take the opportunity }
- { to set the Xceed Backup Library objects' properties to what we need. We }
- { could have set these visually with property pages, but not for this sample. }
- {------------------------------------------------------------------------------}
- procedure TfrmSample.FormCreate(Sender: TObject);
- begin
- { Specify which files to backup. In this sample, the text edit field is
- saved in a file called TextField.txt, placed in the sample's current
- directory. We use the very useful <AppFolder> macro to accomplish this }
- xFiles.FilesToProcess := '<AppFolder>\TextField.txt';
-
- { How and where to backup the file(s) to }
- xBackup.BackupFolder := '<AppFolder>\Backups'; { Our sample's directory }
- xBackup.BackupSetName := 'Delphi Sample';
- xBackup.BackupType := bbtIncremental; { Backup only if the text changed! }
- xBackup.RetentionPeriod := 1; { Delete backup files older than one hour }
- xBackup.Selection := xFiles.ControlInterface; { Assign which TFileSelection to use }
- xBackup.VerifyAfterBackup := true; { Take extra time to make sure the backup data is OK }
-
- { When to backup. This sample uses the library's scheduler. }
- xScheduled.Job := xBackup.ControlInterface; { The backup job to schedule }
- xScheduled.OccurenceType := botEveryNMinutes; { Our backup recurrence time is counted in minutes }
- xScheduled.Occurence := 1; { The 'N' above! ...Backup every 1 minute }
- xScheduled.Required := true; { Don't skip this backup - see next comment }
- { We leave the default 1900/01/01 StartDate since we want to start the backups
- as soon as the scheduler is started (and every minute thereafter). Since
- we set the 'Required' property to true, a backup will be made immediately
- when the scheduler is started, because the backup due on 1900/01/01 has not
- yet been performed and cannot be skipped }
-
- { Assign the scheduled job to the backup library's list of scheduled jobs
- that it must monitor }
- xMain.ScheduledJobs.Add( xScheduled.ControlInterface );
-
- { For the first run, the text field is always dirty (so the default text
- "This is the original text" is saved to file and therefore backed up }
- FDirty := true;
-
- { Finally, start the library's scheduler. Since we start it when the
- form is created, we'll be stopping the scheduler when the form is
- destroyed... }
- xMain.StartScheduler;
- end;
-
- {------------------------------------------------------------------------------}
- { FormDestroy: The form is about to be destroyed. Before quitting our }
- { application, we must make sure to stop the scheduler. }
- {------------------------------------------------------------------------------}
- procedure TfrmSample.FormDestroy(Sender: TObject);
- begin
- { Stop the Xceed Backup Scheduler }
- xMain.StopScheduler( true ); { Give the scheduler the chance to end a
- potentially running backup job }
- end;
-
- {------------------------------------------------------------------------------}
- { btRollbackClick: We show the available file dates and restore the selected }
- { file. }
- {------------------------------------------------------------------------------}
- procedure TfrmSample.btRollbackClick(Sender: TObject);
- begin
- { The rollback button opens the frmRollback form which
- allows the user to select which backed-up version of
- the text field to restore. }
-
- { While doing a rollback, let's interrupt the scheduling
- so that new backups aren't being performed while we
- are restoring. }
- xMain.StopScheduler( true );
-
- { The TFileSelection control on the form, named 'xFiles',
- is used by the scheduler to know which file(s) to back up,
- by our restore code (below) to specify which file to
- restore, and also by the frmRollback form for browsing
- the catalogs, which is why we pass xFiles by parameter to the form }
- if frmRollback.ShowModal( xMain, xFiles ) = mrOK then
- begin
- { Set the restore options.}
- { We use the same backup set name that we use when we perform our backups }
- xRestore.BackupSetName := xBackup.BackupSetName;
- { Assign the TFileSelection control to the restore job }
- xRestore.Selection := xFiles.ControlInterface;
-
- { Run the restore job, and if it worked successfully,
- load the text from the restored file. If an error occurs,
- it will be displayed by ProcessCompleted event's handler }
- if xMain.Restore( xRestore.ControlInterface ) = berSuccess then
- mmoText.Lines.LoadFromFile( ExtractFilePath( Application.ExeName ) + 'TextField.txt' );
- end;
-
- { After restoring, before restarting the scheduler, we must not forget to
- reset the xFiles.MaxDate property which was possibly changed by the
- frmRollback form, because the scheduler is using the same xFiles instance
- for its backups }
- xFiles.MaxDate := EncodeDate( 9999, 12, 31 );
-
- xMain.StartScheduler;
- end;
-
- procedure TfrmSample.btQuitClick(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TfrmSample.mmoTextChange(Sender: TObject);
- begin
- FDirty := true;
- barStatus.Panels[0].Text := '';
- end;
-
- procedure TfrmSample.tmrClockTimer(Sender: TObject);
- begin
- { This code doesn't launch the backup! It only updates
- the displayed time on the form. The Backup Library's
- scheduler handles launching of the backups.}
-
- lblClock.Caption := TimeToStr( Now );
- end;
-
- end.
-
-