home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 53 / IOPROG_53.ISO / soft / c++ / xceedbkp.exe / Samples / Delphi4 / SampleForm.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1999-09-02  |  9.0 KB  |  209 lines

  1. unit SampleForm;
  2. {------------------------------------------------------------------------------}
  3. { SampleForm.pas: Modified text saved and backed up every minute. User can     }
  4. {                 restore any backed up version of the text.                   }
  5. {                                                                              }
  6. { Part of the Delphi 4 Sample application for the Xceed Backup Library         }
  7. { Copyright (C) 1999 - Xceed Software Inc.                                     }
  8. {------------------------------------------------------------------------------}
  9.  
  10. interface
  11.  
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   ComCtrls, StdCtrls, XceedBackupLib_TLB, OleCtrls, RollbackForm, ExtCtrls;
  15.  
  16. type
  17.   TfrmSample = class(TForm)
  18.     Label1: TLabel;
  19.     mmoText: TMemo;
  20.     btRollback: TButton;
  21.     barStatus: TStatusBar;
  22.     btQuit: TButton;
  23.     xMain: TXceedBackup;
  24.     xScheduled: TScheduledJob;
  25.     xRestore: TRestoreJob;
  26.     xBackup: TBackupJob;
  27.     xFiles: TFileSelection;
  28.     lblClock: TLabel;
  29.     tmrClock: TTimer;
  30.     procedure xMainStartingBackup(Sender: TObject;
  31.       const xBackupJob: IXceedBackupJob; dtBackupDate: TDateTime;
  32.       var sMediaLabelPattern: WideString; var bPreventLaunch: WordBool);
  33.     procedure xMainProcessCompleted(Sender: TObject; lFilesTotal,
  34.       lFilesProcessed, lFilesSkipped, lBytesTotal, lBytesProcessed,
  35.       lBytesSkipped: Integer; xResult: TOleEnum);
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FormDestroy(Sender: TObject);
  38.     procedure btRollbackClick(Sender: TObject);
  39.     procedure btQuitClick(Sender: TObject);
  40.     procedure mmoTextChange(Sender: TObject);
  41.     procedure tmrClockTimer(Sender: TObject);
  42.   private
  43.     { Private declarations }
  44.     FDirty : boolean; { Determines if text field needs to be saved }
  45.   public
  46.     { Public declarations }
  47.   end;
  48.  
  49. var
  50.   frmSample: TfrmSample;
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55.  
  56. {------------------------------------------------------------------------------}
  57. { StartingBackup event: Triggered everytime a backup job is starting, manually }
  58. { or scheduled.                                                                }
  59. {------------------------------------------------------------------------------}
  60. procedure TfrmSample.xMainStartingBackup(Sender: TObject;
  61.   const xBackupJob: IXceedBackupJob; dtBackupDate: TDateTime;
  62.   var sMediaLabelPattern: WideString; var bPreventLaunch: WordBool);
  63. begin
  64.   { Let's save the file only when about to backup }
  65.   if FDirty then
  66.   begin
  67.     mmoText.Lines.SaveToFile( ExtractFilePath( Application.ExeName ) + 'TextField.txt' );
  68.     FDirty := false;
  69.   end;
  70.  
  71.   btRollback.Enabled := false;
  72.   barStatus.Panels[0].Text := 'Starting backup...';
  73. end;
  74.  
  75. {------------------------------------------------------------------------------}
  76. { ProcessCompleted event: Triggered every time a TXceedBackup method has       }
  77. { completed, and also when a scheduled job has finished.                       }
  78. {------------------------------------------------------------------------------}
  79. procedure TfrmSample.xMainProcessCompleted(Sender: TObject; lFilesTotal,
  80.   lFilesProcessed, lFilesSkipped, lBytesTotal, lBytesProcessed,
  81.   lBytesSkipped: Integer; xResult: TOleEnum);
  82. begin
  83.   btRollback.Enabled := true;
  84.   barStatus.Panels[0].Text := xMain.GetErrorDescription( bvtError, xResult );
  85. end;
  86.  
  87. {------------------------------------------------------------------------------}
  88. { FormCreate: The main form is about to be created. We take the opportunity    }
  89. { to set the Xceed Backup Library objects' properties to what we need. We      }
  90. { could have set these visually with property pages, but not for this sample.  }
  91. {------------------------------------------------------------------------------}
  92. procedure TfrmSample.FormCreate(Sender: TObject);
  93. begin
  94.   { Specify which files to backup. In this sample, the text edit field is
  95.     saved in a file called TextField.txt, placed in the sample's current
  96.     directory. We use the very useful <AppFolder> macro to accomplish this }
  97.   xFiles.FilesToProcess := '<AppFolder>\TextField.txt';
  98.  
  99.   { How and where to backup the file(s) to }
  100.   xBackup.BackupFolder      := '<AppFolder>\Backups'; { Our sample's directory }
  101.   xBackup.BackupSetName     := 'Delphi Sample';
  102.   xBackup.BackupType        := bbtIncremental;  { Backup only if the text changed! }
  103.   xBackup.RetentionPeriod   := 1; { Delete backup files older than one hour }
  104.   xBackup.Selection         := xFiles.ControlInterface; { Assign which TFileSelection to use }
  105.   xBackup.VerifyAfterBackup := true; { Take extra time to make sure the backup data is OK }
  106.  
  107.   { When to backup. This sample uses the library's scheduler. }
  108.   xScheduled.Job            := xBackup.ControlInterface; { The backup job to schedule }
  109.   xScheduled.OccurenceType  := botEveryNMinutes; { Our backup recurrence time is counted in minutes }
  110.   xScheduled.Occurence      := 1; { The 'N' above! ...Backup every 1 minute }
  111.   xScheduled.Required       := true; { Don't skip this backup - see next comment }
  112.   { We leave the default 1900/01/01 StartDate since we want to start the backups
  113.     as soon as the scheduler is started (and every minute thereafter). Since
  114.     we set the 'Required' property to true, a backup will be made immediately
  115.     when the scheduler is started, because the backup due on 1900/01/01 has not
  116.     yet been performed and cannot be skipped }
  117.  
  118.   { Assign the scheduled job to the backup library's list of scheduled jobs
  119.     that it must monitor }
  120.   xMain.ScheduledJobs.Add( xScheduled.ControlInterface );
  121.  
  122.   { For the first run, the text field is always dirty (so the default text
  123.     "This is the original text" is saved to file and therefore backed up }
  124.   FDirty := true;
  125.  
  126.   { Finally, start the library's scheduler. Since we start it when the
  127.     form is created, we'll be stopping the scheduler when the form is
  128.     destroyed... }
  129.   xMain.StartScheduler;
  130. end;
  131.  
  132. {------------------------------------------------------------------------------}
  133. { FormDestroy: The form is about to be destroyed. Before quitting our          }
  134. { application, we must make sure to stop the scheduler.                        }
  135. {------------------------------------------------------------------------------}
  136. procedure TfrmSample.FormDestroy(Sender: TObject);
  137. begin
  138.   { Stop the Xceed Backup Scheduler }
  139.   xMain.StopScheduler( true );  { Give the scheduler the chance to end a
  140.                                   potentially running backup job }
  141. end;
  142.  
  143. {------------------------------------------------------------------------------}
  144. { btRollbackClick: We show the available file dates and restore the selected   }
  145. { file.                                                                        }
  146. {------------------------------------------------------------------------------}
  147. procedure TfrmSample.btRollbackClick(Sender: TObject);
  148. begin
  149.   { The rollback button opens the frmRollback form which
  150.     allows the user to select which backed-up version of
  151.     the text field to restore. }
  152.  
  153.   { While doing a rollback, let's interrupt the scheduling
  154.     so that new backups aren't being performed while we
  155.     are restoring. }
  156.   xMain.StopScheduler( true );
  157.  
  158.   { The TFileSelection control on the form, named 'xFiles',
  159.     is used by the scheduler to know which file(s) to back up,
  160.     by our restore code (below) to specify which file to
  161.     restore, and also by the frmRollback form for browsing
  162.     the catalogs, which is why we pass xFiles by parameter to the form }
  163.   if frmRollback.ShowModal( xMain, xFiles ) = mrOK then
  164.   begin
  165.     { Set the restore options.}
  166.     { We use the same backup set name that we use when we perform our backups }
  167.     xRestore.BackupSetName  := xBackup.BackupSetName;
  168.     { Assign the TFileSelection control to the restore job }
  169.     xRestore.Selection      := xFiles.ControlInterface;
  170.  
  171.     { Run the restore job, and if it worked successfully,
  172.       load the text from the restored file. If an error occurs,
  173.       it will be displayed by ProcessCompleted event's handler }
  174.     if xMain.Restore( xRestore.ControlInterface ) = berSuccess then
  175.       mmoText.Lines.LoadFromFile( ExtractFilePath( Application.ExeName ) + 'TextField.txt' );
  176.   end;
  177.  
  178.   { After restoring, before restarting the scheduler, we must not forget to
  179.     reset the xFiles.MaxDate property which was possibly changed by the
  180.     frmRollback form, because the scheduler is using the same xFiles instance
  181.     for its backups }
  182.   xFiles.MaxDate := EncodeDate( 9999, 12, 31 );
  183.  
  184.   xMain.StartScheduler;
  185. end;
  186.  
  187. procedure TfrmSample.btQuitClick(Sender: TObject);
  188. begin
  189.   Close;
  190. end;
  191.  
  192. procedure TfrmSample.mmoTextChange(Sender: TObject);
  193. begin
  194.   FDirty := true;
  195.   barStatus.Panels[0].Text := '';
  196. end;
  197.  
  198. procedure TfrmSample.tmrClockTimer(Sender: TObject);
  199. begin
  200.   { This code doesn't launch the backup! It only updates
  201.     the displayed time on the form. The Backup Library's
  202.     scheduler handles launching of the backups.}
  203.  
  204.   lblClock.Caption := TimeToStr( Now );
  205. end;
  206.  
  207. end.
  208.  
  209.