2010-01-25 08:59:44 -05:00
|
|
|
unit updateform;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2010-03-07 10:57:10 -05:00
|
|
|
Classes, SysUtils, FileUtil, LResources, Forms,MufasaBase, Controls, Graphics, Dialogs,
|
2010-01-25 08:59:44 -05:00
|
|
|
ComCtrls, StdCtrls, updater;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TSimbaUpdateForm }
|
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
{ TDownloadThread }
|
2010-01-25 08:59:44 -05:00
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
TDownloadThread = class(TThread)
|
2010-04-18 11:14:19 -04:00
|
|
|
private
|
2010-01-25 08:59:44 -05:00
|
|
|
InputURL : string;
|
2010-04-18 11:14:19 -04:00
|
|
|
ResultStr : PString;
|
|
|
|
public
|
2010-01-25 08:59:44 -05:00
|
|
|
Done : boolean;
|
2010-04-18 11:14:19 -04:00
|
|
|
constructor Create(const URL : string; const Output : PString);
|
2010-01-25 08:59:44 -05:00
|
|
|
procedure Execute; override;
|
|
|
|
end;
|
|
|
|
TSimbaUpdateForm = class(TForm)
|
|
|
|
DownloadSpeed: TLabel;
|
|
|
|
UpdateLog: TMemo;
|
|
|
|
UpdateButton: TButton;
|
2010-03-06 07:06:19 -05:00
|
|
|
CloseButton: TButton;
|
2010-01-25 08:59:44 -05:00
|
|
|
DownloadProgress: TProgressBar;
|
2010-03-06 07:06:19 -05:00
|
|
|
procedure CloseButtonClick(Sender: TObject);
|
2010-01-25 08:59:44 -05:00
|
|
|
procedure CleanUpdateForm(Sender: TObject);
|
|
|
|
procedure FormCreate(Sender: TObject);
|
|
|
|
procedure UpdateButtonClick(Sender: TObject);
|
|
|
|
private
|
|
|
|
{ private declarations }
|
|
|
|
|
|
|
|
Updater: TMMLFileDownloader;
|
|
|
|
FStartTime : longword;
|
|
|
|
FCancelling: Boolean;
|
|
|
|
FDone: Boolean;
|
|
|
|
FUpdating : boolean;
|
|
|
|
FOldSpeed : integer;
|
|
|
|
FLastUpdateSpeed : longword;
|
|
|
|
FSimbaVersion: Integer;
|
2010-03-29 17:01:54 -04:00
|
|
|
FFontVersion : integer;
|
|
|
|
SimbaVersionThread : TDownloadThread;
|
|
|
|
FontVersionThread : TDownloadThread;
|
2010-01-25 08:59:44 -05:00
|
|
|
private
|
|
|
|
function OnUpdateBeat: Boolean;
|
|
|
|
public
|
2010-03-07 10:57:10 -05:00
|
|
|
function CanUpdate: Boolean;
|
2010-03-29 17:01:54 -04:00
|
|
|
function GetLatestFontVersion : integer;
|
2010-03-07 10:57:10 -05:00
|
|
|
function GetLatestSimbaVersion: Integer;
|
2010-01-25 08:59:44 -05:00
|
|
|
procedure PerformUpdate;
|
|
|
|
protected
|
|
|
|
FCancelled: Boolean;
|
2010-02-21 12:13:30 -05:00
|
|
|
end;
|
2010-01-25 08:59:44 -05:00
|
|
|
|
|
|
|
const
|
|
|
|
DownloadSpeedTextRunning = 'Downloading at %d kB/s';
|
|
|
|
DownloadSpeedTextEnded = 'Downloaded at %d kB/s';
|
2010-02-21 14:36:35 -05:00
|
|
|
SimbaURL = {$IFDEF WINDOWS}
|
|
|
|
{$IFDEF CPUI386}
|
|
|
|
'http://simba.villavu.com/bin/Windows/x86/Stable/'
|
|
|
|
{$ELSE}
|
|
|
|
'http://simba.villavu.com/bin/Windows/x86_64/Stable/'
|
|
|
|
{$ENDIF}
|
|
|
|
{$ELSE}
|
|
|
|
{$IFDEF CPUI386}
|
|
|
|
'http://simba.villavu.com/bin/Linux/x86/Stable/'
|
|
|
|
{$ELSE}
|
|
|
|
'http://simba.villavu.com/bin/Linux/x86_64/Stable/'
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF};
|
2010-03-29 17:01:54 -04:00
|
|
|
FontURL = 'http://simba.villavu.com/bin/Fonts/';
|
2010-02-21 14:36:35 -05:00
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
var
|
|
|
|
SimbaUpdateForm: TSimbaUpdateForm;
|
2010-02-21 14:36:35 -05:00
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
internets, TestUnit, simbasettings,lclintf;
|
2010-02-21 14:36:35 -05:00
|
|
|
|
2010-01-25 08:59:44 -05:00
|
|
|
function TSimbaUpdateForm.CanUpdate: Boolean;
|
|
|
|
begin
|
|
|
|
GetLatestSimbaVersion;
|
2010-03-07 10:57:10 -05:00
|
|
|
mDebugLn(format('Current Simba version: %d',[TestUnit.SimbaVersion]));
|
|
|
|
mDebugLn('Latest Simba Version: ' + IntToStr(FSimbaVersion));
|
2010-01-25 08:59:44 -05:00
|
|
|
Exit(testunit.SimbaVersion < FSimbaVersion);
|
|
|
|
end;
|
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
function TSimbaUpdateForm.GetLatestFontVersion: integer;
|
2010-04-18 11:14:19 -04:00
|
|
|
var
|
|
|
|
Vers : string;
|
2010-03-29 17:01:54 -04:00
|
|
|
begin
|
|
|
|
if FontVersionThread = nil then//Create thread (only if no-other one is already running)
|
|
|
|
begin
|
2010-04-18 11:14:19 -04:00
|
|
|
FontVersionThread := TDownloadThread.Create(SettingsForm.Settings.GetKeyValueDefLoad(
|
|
|
|
'Settings/Fonts/VersionLink',FontURL + 'Version',SimbaSettingsFile),
|
|
|
|
@Vers);
|
2010-03-29 17:01:54 -04:00
|
|
|
FontVersionThread.Resume;
|
|
|
|
while FontVersionThread.Done = false do//Wait till thread is done
|
|
|
|
begin
|
|
|
|
Application.ProcessMessages;
|
|
|
|
Sleep(25);
|
|
|
|
end;
|
2010-04-18 11:14:19 -04:00
|
|
|
FFontVersion := StrToIntDef(Trim(Vers), -1);//Read output
|
|
|
|
FontVersionThread := nil; //It's already freed
|
2010-03-29 17:01:54 -04:00
|
|
|
end else
|
|
|
|
begin
|
|
|
|
//Another thread is already running, lets wait for it! (When it's nil, it means that the result is written!)
|
|
|
|
while FontVersionThread = nil do
|
|
|
|
begin;
|
|
|
|
Application.ProcessMessages;
|
|
|
|
Sleep(50);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Exit(FFontVersion);
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2010-01-25 08:59:44 -05:00
|
|
|
function TSimbaUpdateForm.GetLatestSimbaVersion: Integer;
|
2010-04-18 11:14:19 -04:00
|
|
|
var
|
|
|
|
Vers : string;
|
2010-01-25 08:59:44 -05:00
|
|
|
begin
|
|
|
|
if SimbaVersionThread = nil then//Create thread (only if no-other one is already running)
|
|
|
|
begin
|
2010-04-18 11:14:19 -04:00
|
|
|
SimbaVersionThread := TDownloadThread.Create(SettingsForm.Settings.GetKeyValueDefLoad(
|
|
|
|
'Settings/Updater/RemoteVersionLink',SimbaURL + 'Version'
|
|
|
|
,SimbaSettingsFile),@Vers);
|
2010-01-25 08:59:44 -05:00
|
|
|
SimbaVersionThread.Resume;
|
|
|
|
while SimbaVersionThread.Done = false do//Wait till thread is done
|
|
|
|
begin
|
|
|
|
Application.ProcessMessages;
|
2010-03-29 17:01:54 -04:00
|
|
|
Sleep(25);
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
2010-04-18 11:14:19 -04:00
|
|
|
FSimbaVersion := StrToIntDef(Trim(Vers), -1);//Read output
|
|
|
|
SimbaVersionThread := nil;//It's automatically freed
|
2010-01-25 08:59:44 -05:00
|
|
|
end else
|
|
|
|
begin
|
|
|
|
//Another thread is already running, lets wait for it! (When it's nil, it means that the result is written!)
|
|
|
|
while SimbaVersionThread = nil do
|
|
|
|
begin;
|
|
|
|
Application.ProcessMessages;
|
|
|
|
Sleep(50);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Exit(FSimbaVersion);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSimbaUpdateForm.UpdateButtonClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if FUpdating then
|
2010-03-06 07:06:19 -05:00
|
|
|
FCancelling := True
|
2010-01-25 08:59:44 -05:00
|
|
|
else
|
|
|
|
Self.PerformUpdate;
|
|
|
|
end;
|
|
|
|
|
2010-03-06 07:06:19 -05:00
|
|
|
procedure TSimbaUpdateForm.CloseButtonClick(Sender: TObject);
|
2010-01-25 08:59:44 -05:00
|
|
|
begin
|
|
|
|
if FCancelled or FDone then
|
2010-03-06 07:06:19 -05:00
|
|
|
Self.ModalResult := mrCancel
|
|
|
|
else
|
|
|
|
Self.UpdateLog.Lines.Add('Update in progress!');
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSimbaUpdateForm.CleanUpdateForm(Sender: TObject);
|
|
|
|
begin
|
2010-03-06 07:06:19 -05:00
|
|
|
Self.DownloadProgress.Position := 0;
|
2010-01-25 08:59:44 -05:00
|
|
|
Self.UpdateLog.Clear;
|
|
|
|
Self.UpdateLog.Lines.Add('---------- Update Session ----------');
|
2010-03-06 07:06:19 -05:00
|
|
|
Self.DownloadSpeed.Visible := false;
|
|
|
|
if not CanUpdate then
|
|
|
|
begin
|
|
|
|
ShowMessage('No Updates Available!');
|
|
|
|
Self.UpdateLog.Lines.Add('No Updates Available!');
|
|
|
|
Self.UpdateButton.Enabled := False;
|
|
|
|
end else
|
|
|
|
Self.UpdateButton.Enabled := true;
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSimbaUpdateForm.FormCreate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
FDone := True;
|
2010-03-06 07:06:19 -05:00
|
|
|
FUpdating := false;
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ Return true if we have to cancel }
|
|
|
|
function TSimbaUpdateForm.OnUpdateBeat: Boolean;
|
|
|
|
var
|
|
|
|
Percentage: Integer;
|
|
|
|
NewSpeed : integer;
|
|
|
|
begin
|
|
|
|
Application.ProcessMessages;
|
|
|
|
|
|
|
|
Percentage := Updater.GetPercentage();
|
|
|
|
if Percentage <> -1 then
|
|
|
|
DownloadProgress.Position:=Percentage;
|
|
|
|
// Formula for speed (kB/s) -> (Bytes div 1000) / (MSecSinceStart div 1000) = Bytes/ MSecSinceStart
|
|
|
|
NewSpeed :=(Updater.DownloadedSize) div ((GetTickCount-FStartTime));
|
|
|
|
if abs(NewSpeed - FOldSpeed) > 1 then
|
|
|
|
if (GetTickCount - FLastUpdateSpeed) > 1000 then //Only update the speed text every second
|
|
|
|
begin;
|
|
|
|
FOldSpeed:= NewSpeed;
|
|
|
|
DownloadSpeed.Caption:= Format(DownloadSpeedTextRunning,[NewSpeed]);
|
|
|
|
FLastUpdateSpeed:= GetTickCount;
|
|
|
|
end;
|
|
|
|
Result := FCancelling;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSimbaUpdateForm.PerformUpdate;
|
|
|
|
begin
|
|
|
|
FUpdating:= True;
|
|
|
|
Updater := TMMLFileDownloader.Create;
|
|
|
|
|
|
|
|
FDone := False;
|
|
|
|
FCancelling := False;
|
|
|
|
FCancelled := False;
|
|
|
|
|
2010-03-31 18:22:46 -04:00
|
|
|
Updater.FileURL := SettingsForm.Settings.GetKeyValueDefLoad(
|
2010-01-25 08:59:44 -05:00
|
|
|
'Settings/Updater/RemoteLink',
|
2010-02-21 14:36:35 -05:00
|
|
|
SimbaURL + 'Simba'{$IFDEF WINDOWS} +'.exe'{$ENDIF},
|
2010-01-25 08:59:44 -05:00
|
|
|
SimbaSettingsFile
|
|
|
|
);
|
|
|
|
|
|
|
|
//ApplicationName{$IFDEF WINDOWS} +'.exe'{$ENDIF};
|
|
|
|
|
|
|
|
// Should work on Windows as well
|
|
|
|
Updater.ReplacementFile := ExtractFileName(Application.ExeName);
|
|
|
|
Updater.OnBeat := @Self.OnUpdateBeat;
|
|
|
|
Updater.BasePath := ExtractFilePath(Application.ExeName);
|
|
|
|
|
|
|
|
Self.UpdateLog.Lines.Add('Starting download of ' + Updater.FileURL + ' ...');
|
|
|
|
try
|
2010-03-06 07:06:19 -05:00
|
|
|
Self.UpdateButton.Caption := 'Cancel'; // Update to Cancel
|
|
|
|
Self.CloseButton.Enabled := false;
|
2010-01-25 08:59:44 -05:00
|
|
|
DownloadSpeed.Visible:= true;
|
|
|
|
DownloadSpeed.Caption:= Format(DownloadSpeedTextRunning,[0]);
|
|
|
|
FStartTime:= GetTickCount - 1;//Be sure that we don't get div 0
|
|
|
|
Updater.DownloadAndSave;
|
|
|
|
DownloadSpeed.Caption := Format(DownloadSpeedTextEnded,[Updater.FileSize div (GetTickCount-FStartTime)]);
|
|
|
|
Self.UpdateLog.Lines.Add('Downloaded to ' + Updater.ReplacementFile + '_ ...');
|
|
|
|
Updater.Replace;
|
|
|
|
Self.UpdateLog.Lines.Add('Renaming ' + Updater.ReplacementFile + ' to ' + Updater.ReplacementFile + '_old_');
|
|
|
|
Self.UpdateLog.Lines.Add('Renaming ' + Updater.ReplacementFile + '_ to ' + Updater.ReplacementFile);
|
|
|
|
Self.UpdateLog.Lines.Add('Deleting ' + Updater.ReplacementFile + '_old_');
|
|
|
|
Updater.Free;
|
|
|
|
Self.UpdateLog.Lines.Add('Done ... ');
|
|
|
|
Self.UpdateLog.Lines.Add('Please restart all currently running Simba binaries.');
|
|
|
|
except
|
|
|
|
FCancelling := False;
|
|
|
|
FCancelled := True;
|
2010-03-06 07:06:19 -05:00
|
|
|
DownloadSpeed.Visible := false;
|
2010-01-25 08:59:44 -05:00
|
|
|
Self.UpdateLog.Lines.Add('Download stopped at '+inttostr(DownloadProgress.Position)+'%... Simba did not succesfully update.');
|
|
|
|
// more detailed info
|
2010-03-22 10:58:30 -04:00
|
|
|
mDebugLn('EXCEPTION IN UPDATEFORM: We either hit Cancel, or something went wrong with files');
|
2010-03-06 09:23:24 -05:00
|
|
|
if FileExists(Updater.BasePath + Updater.ReplacementFile + '_') then
|
|
|
|
begin
|
2010-03-22 10:58:30 -04:00
|
|
|
Self.UpdateLog.Lines.Add(Format('Deleting the ghost file (%s)',[Updater.BasePath + Updater.ReplacementFile + '_']));
|
2010-03-06 09:23:24 -05:00
|
|
|
DeleteFile(Updater.BasePath + Updater.ReplacementFile + '_');
|
|
|
|
end;
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
FDone := True;
|
2010-03-06 07:06:19 -05:00
|
|
|
Self.UpdateButton.Caption := 'Update!';
|
|
|
|
Self.CloseButton.Enabled := true;
|
2010-01-25 08:59:44 -05:00
|
|
|
FUpdating:= false;
|
|
|
|
end;
|
|
|
|
|
2010-04-18 11:14:19 -04:00
|
|
|
constructor TDownloadThread.Create(const url : String; const Output : PString);
|
2010-04-16 14:50:10 -04:00
|
|
|
begin
|
|
|
|
inherited Create(true);
|
|
|
|
FreeOnTerminate:= True;
|
2010-04-18 11:14:19 -04:00
|
|
|
InputURL:= url;
|
|
|
|
ResultStr:= Output;
|
2010-04-16 14:50:10 -04:00
|
|
|
end;
|
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
{ TDownloadThread }
|
2010-01-25 08:59:44 -05:00
|
|
|
|
2010-03-29 17:01:54 -04:00
|
|
|
procedure TDownloadThread.Execute;
|
2010-01-25 08:59:44 -05:00
|
|
|
begin
|
2010-04-18 11:14:19 -04:00
|
|
|
ResultStr^:= GetPage(InputURL);
|
2010-01-25 08:59:44 -05:00
|
|
|
done := true;
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
2010-04-07 08:44:55 -04:00
|
|
|
{$R *.lfm}
|
2010-01-25 08:59:44 -05:00
|
|
|
|
|
|
|
end.
|
|
|
|
|