summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/Chess4Net/URLVersionQueryUnit.pas
blob: 24c73a1a94996904db5cceea9e5b0e69d22b09ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.

unit URLVersionQueryUnit;

interface

uses
  Classes;

type
  TApplicationID = (aidAnalyzer = 1, aidSkype = 2);
  TOperatingSystemID = (osidWindows = 1, osidLinux = 2);

  TURLVersionQuery = class;

  TQueryReadyEvent = procedure(Sender: TURLVersionQuery) of object;

  TURLVersionQuery = class(TDataModule)
  private
    m_iLastVersion: integer;
    m_wstrInfo: WideString;
    FQueryReadyEvent: TQueryReadyEvent;
    procedure FDoQueryReady;
    function FQuery(const strURL: string): string;
    function FGetURL(ApplicationID: TApplicationID; iVersion: integer;
      OperatingSystemID: TOperatingSystemID): string;
    procedure FParseResponse(const strResponse: string);
  public
    constructor Create; reintroduce;
    procedure Query(ApplicationID: TApplicationID; iVersion: integer;
      OperatingSystemID: TOperatingSystemID);
    property LastVersion: integer read m_iLastVersion;
    property Info: WideString read m_wstrInfo;
    property OnQueryReady: TQueryReadyEvent read FQueryReadyEvent write FQueryReadyEvent;
  end;

implementation

{$R *.dfm}

uses
  Forms, SysUtils, StrUtils,
  //
  XIE;

type
  TQueryThread = class(TThread)
  private
    m_URLVersionQuery: TURLVersionQuery;
    m_strURL: string;
    m_strResponse: string;
    procedure FNotifyOnResponse;
  protected
    procedure Execute; override;
  public
    constructor Create(AURLVersionQuery: TURLVersionQuery; const strURL: string);
  end;

////////////////////////////////////////////////////////////////////////////////
// TURLVersionQuery

constructor TURLVersionQuery.Create;
begin
  inherited Create(Application);
end;

procedure TURLVersionQuery.FDoQueryReady;
begin
  if (Assigned(FQueryReadyEvent)) then
    FQueryReadyEvent(self);
end;


procedure TURLVersionQuery.Query(ApplicationID: TApplicationID; iVersion: integer;
  OperatingSystemID: TOperatingSystemID);
begin
  TQueryThread.Create(self, FGetURL(ApplicationID, iVersion, OperatingSystemID));
end;


function TURLVersionQuery.FQuery(const strURL: string): string;
begin
  with TIEWrapper.Create do
  try
    Result := OpenRequest(strURL);
  finally
    Free;
  end;
end;


function TURLVersionQuery.FGetURL(ApplicationID: TApplicationID; iVersion: integer;
  OperatingSystemID: TOperatingSystemID): string;
begin
  Result := Format('http://chess4net.ru/stat.php?app=%d&ver=%d&os=%d',
    [Ord(ApplicationID), iVersion, Ord(OperatingSystemID)]);
end;


procedure TURLVersionQuery.FParseResponse(const strResponse: string);

  procedure NSplit(const str: string; out strlList: TStringList);
  var
    iPosPrev, iPosNext: integer;
    strSub: string;
  begin
    strlList := TStringList.Create;

    strSub := '';
    iPosPrev := 1;

    while (iPosPrev <= Length(str)) do
    begin
      iPosNext := iPosPrev;

      iPosNext := PosEx(';', str, iPosNext);

      if (iPosNext = 0) then
        iPosNext := MaxInt - 1;

      strSub := strSub + Copy(str, iPosPrev, iPosNext - iPosPrev);
      if ((iPosNext < Length(str)) and (str[iPosNext + 1] = ';')) then
      begin
        strSub := strSub + ';';
        iPosPrev := iPosNext + 2;
        continue;
      end;

      strlList.Append(strSub);
      strSub := '';

      iPosPrev := iPosNext + 1;
    end;

  end;

var
  strl: TStringList;
begin // .FParseResponse
  NSplit(strResponse, strl);
  try
    m_iLastVersion := StrToIntDef(strl.Values['Last version'], 0);
    m_wstrInfo := strl.Values['Info'];
  finally
    strl.Free;
  end;
end;

////////////////////////////////////////////////////////////////////////////////
// TQueryThread

constructor TQueryThread.Create(AURLVersionQuery: TURLVersionQuery; const strURL: string);
begin
  m_URLVersionQuery := AURLVersionQuery;
  m_strURL := strURL;

  inherited Create(TRUE);
  FreeOnTerminate := TRUE;

  Resume;
end;


procedure TQueryThread.Execute;
begin
{$IFNDEF TESTING}
  m_strResponse := m_URLVersionQuery.FQuery(m_strURL);
{$ELSE}
  m_strResponse := 'Last version=201102;Info=Version 2011.2 is available'#10'TEST> You can download it from http://chess4net.ru <TEST';
{$ENDIF}
  Synchronize(FNotifyOnResponse);
end;


procedure TQueryThread.FNotifyOnResponse;
begin
  m_URLVersionQuery.FParseResponse(m_strResponse);
  m_URLVersionQuery.FDoQueryReady;
end;

end.