//==============================================================================
// hxGrid framework
// Copyright (C) 2007 by Roman Lut
// hax@deep-shadows.com
// http://www.deep-shadows.com/hax/
//==============================================================================

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvComponentBase, JvTrayIcon, T_Agent, StdCtrls, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, JvgXMLSerializer, SyncObjs,
  ExtCtrls, Menus, hxgridcommon, safefpu;

type
  TForm1 = class(TForm)
    JvTrayIcon1: TJvTrayIcon;
    Timer1: TTimer;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    PopupMenu1: TPopupMenu;
    Exit1: TMenuItem;
    N1: TMenuItem;
    Status1: TMenuItem;
    Label6: TLabel;
    Label7: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Status1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
   shouldExit:boolean;

   procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;

  public
    { Public declarations }
   agent: TAgent;
  end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

//==============================================================================
//==============================================================================
procedure TForm1.FormCreate(Sender: TObject);
begin
 agent:=TAgent.Create(self);
 shouldExit:=false;
 caption:= 'hxgrid agent v.'+inttohex(CURRENT_AGENT_VERSION,3);
end;

//==============================================================================
//==============================================================================
procedure TForm1.FormDestroy(Sender: TObject);
begin
 agent.Destroy();
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 Label4.Caption:=inttostr(agent.statusThread.CPUAvail)+'%';
 Label5.Caption:=inttostr(agent.statusThread.CPUUsageByAgent)+'%';
 Label3.caption:='Status: '+agent.GetStatus();
 Label7.caption:=inttostr(agent.settings.poolSize);

 jvtrayicon1.Hint:='Status: '+agent.GetStatus()+#13+'CPU Used By Agent: '+inttostr(agent.statusThread.CPUUsageByAgent)+'%';
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
 ShouldExit:=true;
 Close;
end;

procedure TForm1.Status1Click(Sender: TObject);
begin
 JvTrayIcon1.ShowApplication();
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if shouldExit=false then
  begin
   JvTrayIcon1.HideApplication();
   CanClose:=false;
  end
   else
  begin
   CanClose:=true;
  end;
end;


procedure TForm1.WMQueryEndSession(var Message: TWMQueryEndSession);
begin
 shouldExit:=true;
 inherited;
end;


begin
 decimalSeparator:='.';
end.

(*
todo:
убивать workerthread если windows shutdown, когда задача больше 3 сек
получение данных по запросу гет дата, калбек гет дата
*)

