{ Seksi Commander ---------------------------- Implementing of progress dialog for file operation Licence : GNU GPL v 2.0 Author : radek.cervinka@centrum.cz contributors: Copyright (C) 2008-2010 Koblov Alexander (Alexx2000@mail.ru) } unit fFileOpDlg; {$mode objfpc}{$H+} interface uses LResources, SysUtils, Classes, Controls, Forms, StdCtrls, ComCtrls, Buttons, ExtCtrls, uOperationsManager, uFileSourceOperation, uFileSourceOperationUI, fViewOperations; type TFileOpDlgLook = set of (fodl_from_lbl, fodl_to_lbl, fodl_first_pb, fodl_second_pb); { TfrmFileOp } TfrmFileOp = class(TForm) btnPauseStart: TBitBtn; btnWorkInBackground: TButton; lblFrom: TLabel; lblTo: TLabel; lblFileNameTo: TLabel; pbSecond: TProgressBar; pbFirst: TProgressBar; lblFileNameFrom: TLabel; lblEstimated: TLabel; btnCancel: TBitBtn; btnToQueue: TSpeedButton; btnOpHome: TSpeedButton; bthOpEnd: TSpeedButton; procedure bthOpEndClick(Sender: TObject); procedure btnCancelClick(Sender: TObject); procedure btnOpHomeClick(Sender: TObject); procedure btnPauseStartClick(Sender: TObject); procedure btnToQueueClick(Sender: TObject); procedure btnWorkInBackgroundClick(Sender: TObject); procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); procedure FormCreate(Sender: TObject); private { Private declarations } FOperationHandle: TOperationHandle; FUpdateTimer: TTimer; // fsosStopped) then begin Result := False; Operation.Stop; end end; end; procedure TfrmFileOp.OnUpdateTimer(Sender: TObject); var Operation: TFileSourceOperation; NewCaption: String; begin Operation := OperationsManager.GetOperationByHandle(FOperationHandle); if Assigned(Operation) and (Operation.State <> fsosStopped) then begin case Operation.ID of fsoCopy, fsoCopyIn, fsoCopyOut: UpdateCopyOperation(Operation); fsoMove: UpdateMoveOperation(Operation); fsoDelete: UpdateDeleteOperation(Operation); fsoWipe: UpdateWipeOperation(Operation); fsoCalcChecksum: UpdateCalcChecksumOperation(Operation); fsoTestArchive: UpdateTestArchiveOperation(Operation); else begin // Operation not currently supported for display. // Only show general progress. pbFirst.Position := Operation.Progress; end; end; UpdatePauseStartButton(Operation.State); NewCaption := IntToStr(Operation.Progress) + '% ' + Hint; if Operation.State <> fsosRunning then NewCaption := NewCaption + ' [' + FileSourceOperationStateText[Operation.State] + ']'; Caption := NewCaption; end else begin // Operation has finished. // if CloseOnFinish then Close; // if BeepOnFinish then Beep; end; end; procedure TfrmFileOp.InitializeControls(FileOpDlgLook: TFileOpDlgLook); begin lblFrom.Visible := fodl_from_lbl in FileOpDlgLook; lblFileNameFrom.Visible := fodl_from_lbl in FileOpDlgLook; lblTo.Visible := fodl_to_lbl in FileOpDlgLook; lblFileNameTo.Visible := fodl_to_lbl in FileOpDlgLook; pbFirst.Visible := fodl_first_pb in FileOpDlgLook; pbSecond.Visible := fodl_second_pb in FileOpDlgLook; lblFileNameFrom.Caption := ''; lblFileNameTo.Caption := ''; lblEstimated.Caption := ''; Hint := Caption; end; procedure TfrmFileOp.SetPauseGlyph; begin dmComData.ImageList.GetBitmap(1, btnPauseStart.Glyph); end; procedure TfrmFileOp.SetPlayGlyph; begin dmComData.ImageList.GetBitmap(0, btnPauseStart.Glyph); end; procedure TfrmFileOp.UpdatePauseStartButton(OperationState: TFileSourceOperationState); begin case OperationState of fsosNotStarted, fsosStopped, fsosPaused: begin btnPauseStart.Enabled := True; SetPlayGlyph; end; fsosStarting, fsosStopping, fsosPausing, fsosWaitingForFeedback: begin btnPauseStart.Enabled := False; end; fsosRunning, fsosWaitingForConnection: begin btnPauseStart.Enabled := True; SetPauseGlyph; end; else btnPauseStart.Enabled := False; end; end; procedure TfrmFileOp.SetProgress(ProgressBar: TProgressBar; CurrentValue: Int64; MaxValue: Int64; BarText: String); {$IFDEF LCLGTK2} var wText: String; {$ENDIF} {$IFDEF LCLQT} var wText: WideString; {$ENDIF} begin if MaxValue <> 0 then ProgressBar.Position := (CurrentValue * 100) div MaxValue else ProgressBar.Position := 0; {$IFDEF LCLGTK2} { %v - the current progress value. %l - the lower bound for the progress value. %u - the upper bound for the progress value. %p - the current progress percentage. } if BarText <> '' then wText := BarText + ' (%p%%)' else wText := '%p%%'; gtk_progress_set_format_string(PGtkProgress(ProgressBar.Handle), PChar(wText)); // Have to reset 'show_text' every time because LCLGTK2 will set it according to BarShowText. gtk_progress_set_show_text(PGtkProgress(ProgressBar.Handle), True); {$ENDIF} {$IFDEF LCLQT} { %p - is replaced by the percentage completed. %v - is replaced by the current value. %m - is replaced by the total number of steps. } if BarText <> '' then wText := WideString(BarText) + ' (%p%)' else wText := '%p%'; QProgressBar_setFormat(QProgressBarH(TQtProgressBar(ProgressBar.Handle).Widget), @wText); //QProgressBar_setTextVisible(QProgressBarH(TQtProgressBar(ProgressBar.Handle).Widget), True); {$ENDIF} end; procedure TfrmFileOp.SetProgressBytes(ProgressBar: TProgressBar; CurrentBytes: Int64; TotalBytes: Int64); begin SetProgress(ProgressBar, CurrentBytes, TotalBytes, cnvFormatFileSize(CurrentBytes, True) + 'B/' + cnvFormatFileSize(TotalBytes, True) + 'B'); end; procedure TfrmFileOp.SetSpeedAndTime(Operation: TFileSourceOperation; RemainingTime: TDateTime; Speed: String); var sEstimated: String; begin if Operation.State <> fsosRunning then sEstimated := '' else begin sEstimated := FormatDateTime('HH:MM:SS', RemainingTime); sEstimated := Format(rsDlgSpeedTime, [Speed, sEstimated]); end; lblEstimated.Caption := sEstimated; end; procedure TfrmFileOp.InitializeCopyOperation(Operation: TFileSourceOperation); begin Caption := rsDlgCp; InitializeControls([fodl_from_lbl, fodl_to_lbl, fodl_first_pb, fodl_second_pb]); end; procedure TfrmFileOp.InitializeMoveOperation(Operation: TFileSourceOperation); begin Caption := rsDlgMv; InitializeControls([fodl_from_lbl, fodl_to_lbl, fodl_first_pb, fodl_second_pb]); end; procedure TfrmFileOp.InitializeDeleteOperation(Operation: TFileSourceOperation); begin Caption := rsDlgDel; InitializeControls([fodl_from_lbl, fodl_first_pb]); lblFrom.Caption := rsDlgDeleting; end; procedure TfrmFileOp.InitializeWipeOperation(Operation: TFileSourceOperation); begin Caption := rsDlgWipe; InitializeControls([fodl_from_lbl, fodl_first_pb, fodl_second_pb]); lblFrom.Caption := rsDlgDeleting; end; procedure TfrmFileOp.InitializeCalcChecksumOperation(Operation: TFileSourceOperation); begin Caption := rsDlgCheckSumCalc; InitializeControls([fodl_from_lbl, fodl_first_pb, fodl_second_pb]); lblFrom.Visible := False; end; procedure TfrmFileOp.InitializeTestArchiveOperation(Operation: TFileSourceOperation); begin Caption := rsDlgTest; InitializeControls([fodl_from_lbl, fodl_to_lbl, fodl_first_pb, fodl_second_pb]); end; procedure TfrmFileOp.UpdateCopyOperation(Operation: TFileSourceOperation); var CopyOperation: TFileSourceCopyOperation; CopyStatistics: TFileSourceCopyOperationStatistics; begin CopyOperation := Operation as TFileSourceCopyOperation; CopyStatistics := CopyOperation.RetrieveStatistics; with CopyStatistics do begin lblFileNameFrom.Caption := CurrentFileFrom; lblFileNameTo.Caption := CurrentFileTo; SetProgressBytes(pbFirst, CurrentFileDoneBytes, CurrentFileTotalBytes); SetProgressBytes(pbSecond, DoneBytes, TotalBytes); SetSpeedAndTime(Operation, RemainingTime, cnvFormatFileSize(BytesPerSecond, True) + 'B'); end; end; procedure TfrmFileOp.UpdateMoveOperation(Operation: TFileSourceOperation); var MoveOperation: TFileSourceMoveOperation; MoveStatistics: TFileSourceMoveOperationStatistics; begin MoveOperation := Operation as TFileSourceMoveOperation; MoveStatistics := MoveOperation.RetrieveStatistics; with MoveStatistics do begin lblFileNameFrom.Caption := CurrentFileFrom; lblFileNameTo.Caption := CurrentFileTo; SetProgressBytes(pbFirst, CurrentFileDoneBytes, CurrentFileTotalBytes); SetProgressBytes(pbSecond, DoneBytes, TotalBytes); SetSpeedAndTime(Operation, RemainingTime, cnvFormatFileSize(BytesPerSecond, True) + 'B'); end; end; procedure TfrmFileOp.UpdateDeleteOperation(Operation: TFileSourceOperation); var DeleteOperation: TFileSourceDeleteOperation; DeleteStatistics: TFileSourceDeleteOperationStatistics; begin DeleteOperation := Operation as TFileSourceDeleteOperation; DeleteStatistics := DeleteOperation.RetrieveStatistics; with DeleteStatistics do begin lblFileNameFrom.Caption := CurrentFile; SetProgress(pbFirst, DoneFiles, TotalFiles, cnvFormatFileSize(DoneFiles, True) + '/' + cnvFormatFileSize(TotalFiles, True)); SetSpeedAndTime(Operation, RemainingTime, cnvFormatFileSize(FilesPerSecond, True)); end; end; procedure TfrmFileOp.UpdateWipeOperation(Operation: TFileSourceOperation); var WipeOperation: TFileSourceWipeOperation; WipeStatistics: TFileSourceWipeOperationStatistics; begin WipeOperation := Operation as TFileSourceWipeOperation; WipeStatistics := WipeOperation.RetrieveStatistics; with WipeStatistics do begin lblFileNameFrom.Caption := CurrentFile; SetProgressBytes(pbFirst, CurrentFileDoneBytes, CurrentFileTotalBytes); SetProgressBytes(pbSecond, DoneBytes, TotalBytes); SetSpeedAndTime(Operation, RemainingTime, cnvFormatFileSize(BytesPerSecond, True) + 'B'); end; end; procedure TfrmFileOp.UpdateCalcChecksumOperation(Operation: TFileSourceOperation); var CalcChecksumOperation: TFileSourceCalcChecksumOperation; CalcChecksumStatistics: TFileSourceCalcChecksumOperationStatistics; begin CalcChecksumOperation := Operation as TFileSourceCalcChecksumOperation; CalcChecksumStatistics := CalcChecksumOperation.RetrieveStatistics; with CalcChecksumStatistics do begin lblFileNameFrom.Caption := CurrentFile; SetProgressBytes(pbFirst, CurrentFileDoneBytes, CurrentFileTotalBytes); SetProgressBytes(pbSecond, DoneBytes, TotalBytes); SetSpeedAndTime(Operation, RemainingTime, cnvFormatFileSize(BytesPerSecond, True) + 'B'); end; end; procedure TfrmFileOp.UpdateTestArchiveOperation(Operation: TFileSourceOperation); var TestArchiveOperation: TFileSourceTestArchiveOperation; TestArchiveStatistics: TFileSourceTestArchiveOperationStatistics; begin TestArchiveOperation := Operation as TFileSourceTestArchiveOperation; TestArchiveStatistics := TestArchiveOperation.RetrieveStatistics; with TestArchiveStatistics do begin lblFileNameFrom.Caption := ArchiveFile; lblFileNameTo.Caption := CurrentFile; SetProgressBytes(pbFirst, CurrentFileDoneBytes, CurrentFileTotalBytes); SetProgressBytes(pbSecond, DoneBytes, TotalBytes); SetSpeedAndTime(Operation, RemainingTime, cnvFormatFileSize(BytesPerSecond, True) + 'B'); end; end; procedure TfrmFileOp.ToggleProgressBarStyle; begin if (pbFirst.Style = pbstMarquee) and (pbSecond.Style = pbstMarquee) then begin pbFirst.Style:= pbstNormal; pbSecond.Style:= pbstNormal; end else begin pbFirst.Style:= pbstMarquee; pbSecond.Style:= pbstMarquee; end; end; initialization {$I fFileOpDlg.lrs} end.