Statistiques
| Révision:

root / UFLicense.pas @ 3

Historique | Voir | Annoter | Télécharger (15,905 ko)

1 3 avalancogn
unit UFLicense;
2
3
interface
4
5
uses
6
  Windows, Forms, Classes, Controls, Messages, StdCtrls, Buttons, ExtCtrls,
7
  ComCtrls, PBNumEdit, IdBaseComponent, IdComponent, IdTCPConnection,
8
  IdTCPClient, IdHTTP, Dialogs, WinInet, IdIOHandler, IdIOHandlerSocket,
9
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL;
10
11
type
12
  TFLicense = class(TForm)
13
    LLastName: TLabel;
14
    LCompany: TLabel;
15
    ELastName: TEdit;
16
    ECompany: TEdit;
17
    LFirstName: TLabel;
18
    EFirstName: TEdit;
19
    LMail: TLabel;
20
    EMail: TEdit;
21
    RBEvaluation: TRadioButton;
22
    RBEducation: TRadioButton;
23
    RBComplete: TRadioButton;
24
    GBLicenseType: TGroupBox;
25
    GBAddress: TGroupBox;
26
    EAddress1: TEdit;
27
    EAddress2: TEdit;
28
    EPostalCode: TEdit;
29
    LPostalCode: TLabel;
30
    ECity: TEdit;
31
    LCity: TLabel;
32
    ECountry: TEdit;
33
    LCountry: TLabel;
34
    LFax: TLabel;
35
    EFax: TEdit;
36
    LPhone: TLabel;
37
    EPhone: TEdit;
38
    PLicense: TPanel;
39
    BBOk: TBitBtn;
40
    BBCancel: TBitBtn;
41
    PInfo: TPanel;
42
    LOrganisation: TLabel;
43
    LLicenseNumber: TLabel;
44
    PBLicenseNumber: TPBNumEdit;
45
    PComplete: TPanel;
46
    PEducation: TPanel;
47
    LCourse: TLabel;
48
    ECourse: TEdit;
49
    REHelp: TRichEdit;
50
    BBHelp: TBitBtn;
51
    GBExport: TGroupBox;
52
    GBImport: TGroupBox;
53
    BBAutomatic: TBitBtn;
54
    BBImport: TBitBtn;
55
    SaveDialogLicFile: TSaveDialog;
56
    OpenDialogLicFile: TOpenDialog;
57
    IdHTTPLicense: TIdHTTP;
58
    BBManual: TBitBtn;
59
    IdSSL: TIdSSLIOHandlerSocketOpenSSL;
60
    procedure FormShow(Sender: TObject);
61
    procedure FormCreate(Sender: TObject);
62
    procedure RBLicenseTypeClick(Sender: TObject);
63
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
64
    procedure BBHelpClick(Sender: TObject);
65
    procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND;
66
    procedure BBAutomaticClick(Sender: TObject);
67
    procedure BBImportClick(Sender: TObject);
68
    procedure ECourseChange(Sender: TObject);
69
    procedure PBLicenseNumberChange(Sender: TObject);
70
    procedure EFirstNameChange(Sender: TObject);
71
    procedure ELastNameChange(Sender: TObject);
72
    procedure ECompanyChange(Sender: TObject);
73
    procedure EAddress1Change(Sender: TObject);
74
    procedure EAddress2Change(Sender: TObject);
75
    procedure EPostalCodeChange(Sender: TObject);
76
    procedure ECityChange(Sender: TObject);
77
    procedure ECountryChange(Sender: TObject);
78
    procedure EPhoneChange(Sender: TObject);
79
    procedure EFaxChange(Sender: TObject);
80
    procedure EMailChange(Sender: TObject);
81
    procedure BBManualClick(Sender: TObject);
82
  private
83
    { Private declarations }
84
    procedure DisplayLicense;
85
    function LicenseIsValid: Boolean;
86
  public
87
    { Public declarations }
88
  end;
89
90
var
91
  FLicense: TFLicense;
92
93
implementation
94
95
uses
96
  ShellAPI, SHFolder, ShlObj, SysUtils, gnugettext, UVariables, UInit, UStrings,
97
  UUtil, UFIntro;
98
99
{$R *.dfm}
100
101
procedure TFLicense.FormCreate(Sender: TObject);
102
begin
103
  if Screen.Fonts.IndexOf('Arial Unicode MS') <> -1
104
  then
105
    Font.Name := 'Arial Unicode MS';
106
  TranslateComponent(Self);
107
  Constraints.MinWidth := 816 + (Width - ClientWidth);
108
  Width := Constraints.MinWidth;
109
  Constraints.MinHeight := 568 + (Height - ClientHeight);
110
  Height := Constraints.MinHeight;
111
end;
112
113
procedure TFLicense.FormShow(Sender: TObject);
114
begin
115
  {
116
  ResName := UpperCase('License');
117
  Stream := TResourceStream.Create(ResInstance, ResName, 'TEXT');
118
  try
119
    REHelp.Lines.LoadFromStream(Stream);
120
  finally
121
    Stream.Free;
122
  end;
123
  }
124
  REHelp.Lines.Add(MsgLicense);
125
  DisplayLicense;
126
end;
127
128
procedure TFLicense.FormClose(Sender: TObject; var Action: TCloseAction);
129
var
130
  s: String;
131
  Ok: Boolean;
132
begin
133
  if ModalResult = mrCancel
134
  then
135
  begin
136
    LoadLicense;
137
    Exit;
138
  end;
139
  if not LicenseIsValid
140
  then
141
  begin
142
    Action := caNone;
143
    Exit;
144
  end;
145
  case LicenseType of
146
    1: s := 'AcceptEducation';
147
    2: s := 'AcceptComplete';
148
    else s := 'AcceptEvaluation';
149
  end;
150
  if not BdRReadBoolean('\Software\InraPorc', s, False)
151
  then
152
  begin
153
    FIntro := TFIntro.Create(Self);
154
    Ok := FIntro.ShowModal = mrOk;
155
    FIntro.Release;
156
    if Ok
157
    then // Accepter
158
      BdRWriteBoolean('\Software\InraPorc', s, True)
159
    else // Refuser
160
    begin
161
      Action := caNone;
162
      Exit;
163
    end;
164
  end;
165
  SaveLicense;
166
  if (RBComplete.Checked and not IsComplete)
167
  or (RBEducation.Checked and not IsEducation)
168
  then // Licence incorrecte
169
    MessageDlg (MsgInvalidLicense + sLineBreak + MsgReadOnly, mtWarning, [mbOk], 0)
170
  else
171
    if (RBEvaluation.Checked or RBEducation.Checked) and TooMuchRecords
172
    then // Trop d'enregistrements
173
      MessageDlg(MsgTooMuchRecords, mtWarning, [mbOk], 0);
174
end;
175
176
procedure TFLicense.RBLicenseTypeClick(Sender: TObject);
177
begin
178
  if RBComplete.Checked
179
  then // Compl?te
180
    LicenseType := 2
181
  else
182
    if RBEducation.Checked
183
    then // Education
184
      LicenseType := 1
185
    else // Evaluation
186
      LicenseType := 0;
187
//  PEducation.Visible := RBEducation.Checked;
188
  LCourse.Enabled := RBEducation.Checked;
189
  ECourse.Enabled := RBEducation.Checked;
190
//  PComplete.Visible := RBComplete.Checked;
191
  LLicenseNumber.Enabled := RBComplete.Checked;
192
  PBLicenseNumber.Enabled := RBComplete.Checked;
193
  PInfo.Enabled := RBComplete.Checked or RBEducation.Checked;
194
  EFirstName.Enabled := PInfo.Enabled;
195
  ELastName.Enabled := PInfo.Enabled;
196
  LOrganisation.Visible := RBEducation.Checked;
197
  LCompany.Visible := not LOrganisation.Visible;
198
  ECompany.Enabled := PInfo.Enabled;
199
  EAddress1.Enabled := PInfo.Enabled;
200
  EAddress2.Enabled := PInfo.Enabled;
201
  EPostalCode.Enabled := PInfo.Enabled;
202
  ECity.Enabled := PInfo.Enabled;
203
  ECountry.Enabled := PInfo.Enabled;
204
  EPhone.Enabled := PInfo.Enabled;
205
  EFax.Enabled := PInfo.Enabled;
206
  EMail.Enabled := PInfo.Enabled;
207
  GBExport.Enabled := PInfo.Enabled;
208
end;
209
210
procedure TFLicense.BBAutomaticClick(Sender: TObject);
211
{
212
const
213
  bSSL = True;
214
  sServer = 'inraporc.inra.fr';
215
  sScript = '/inraporc/SoftwareEnableKey.php';
216
  sMethod = 'POST';
217
  accept: packed array[0..1] of LPWSTR = (PChar('*/*'), nil);
218
  header: string = 'Content-Type: application/x-www-form-urlencoded';
219
}
220
var
221
  ParamList: TStringList;
222
  ResultString: string;
223
  {
224
  rbsParams: RawByteString;
225
  hInet, hConnect, hRequest: HINTERNET;
226
  port: Word;
227
  flag: DWORD;
228
  i: Integer;
229
  }
230
begin
231
  if not LicenseIsValid
232
  then
233
    Exit;
234
  BBAutomatic.Enabled := False;
235
  Screen.Cursor := crHourGlass;
236
  ParamList := TStringList.Create;
237
  try
238
    ParamList.Add(Format('Language=%s', [UpperCase(Copy(LanguageCode, 1, 2))]));
239
    ParamList.Add(Format('LicenseType=%d', [LicenseType]));
240
    ParamList.Add(Format('LicenseNumber=%d', [LicenseNumber]));
241
    ParamList.Add(Format('Version=%s', [VersionString]));
242
    ParamList.Add(Format('FirstName=%s', [FirstName]));
243
    ParamList.Add(Format('LastName=%s', [LastName]));
244
    ParamList.Add(Format('Company=%s', [Company]));
245
    ParamList.Add(Format('Address1=%s', [Address1]));
246
    ParamList.Add(Format('Address2=%s', [Address2]));
247
    ParamList.Add(Format('PostalCode=%s', [PostalCode]));
248
    ParamList.Add(Format('City=%s', [City]));
249
    ParamList.Add(Format('Country=%s', [Country]));
250
    ParamList.Add(Format('Phone=%s', [Phone]));
251
    ParamList.Add(Format('Fax=%s', [Fax]));
252
    ParamList.Add(Format('Mail=%s', [Mail]));
253
    ParamList.Add(Format('Course=%s', [Course]));
254
    ParamList.Add(Format('VolumeSerialNumber=%s', [Volume]));
255
    ParamList.Add(Format('FinalDate=%s', [FinalDate]));
256
    ParamList.Add(Format('SoftwareEnableKey=%s', [SoftwareEnableKey]));
257
    {
258
    // M?thode plus propre, mais qui n'enregistre pas les param?tre sans valeur
259
    ParamList.Values['Language'] := UpperCase(Copy(LanguageCode, 1, 2));
260
    ParamList.Values['LicenseType'] := IntToStr(LicenseType);
261
    ParamList.Values['LicenseNumber'] := IntToStr(LicenseNumber);
262
    ParamList.Values['Version'] := VersionString;
263
    ParamList.Values['FirstName'] := FirstName;
264
    ParamList.Values['LastName'] := LastName;
265
    ParamList.Values['Company'] := Company;
266
    ParamList.Values['Address1'] := Address1;
267
    ParamList.Values['Address2'] := Address2;
268
    ParamList.Values['PostalCode'] := PostalCode;
269
    ParamList.Values['City'] := City;
270
    ParamList.Values['Country'] := Country;
271
    ParamList.Values['Phone'] := Phone;
272
    ParamList.Values['Fax'] := Fax;
273
    ParamList.Values['Mail'] := Mail;
274
    ParamList.Values['Course'] := Course;
275
    ParamList.Values['VolumeSerialNumber'] := Volume;
276
    ParamList.Values['FinalDate'] := FinalDate;
277
    ParamList.Values['SoftwareEnableKey'] := SoftwareEnableKey;
278
    }
279
    try
280
      IdHTTPLicense.ConnectTimeout := 10000;
281
      ResultString := IdHTTPLicense.Post(URL_HTTPS + 'SoftwareEnableKey.php', ParamList);
282
      if ResultString <> ''
283
      then // Erreur PHP
284
        MessageDlg(ResultString, mtError, [mbOk], 0)
285
      else // Enregistrement effectu?
286
        MessageDlg(MsgLicenseSuccess, mtConfirmation, [mbOk], 0);
287
    except
288
      // Exception Delphi (Indy HTTP)
289
      MessageDlg(MsgServerUnreachable, mtError, [mbOk], 0);
290
    end;
291
    {
292
    for i := 0 to ParamList.Count - 1 do
293
      if i = 0 then
294
        rbsParams := utf8encode(ParamList[i])
295
      else
296
        rbsParams := rbsParams + '&' + utf8encode(ParamList[i]);
297
    hInet := InternetOpen('InraPorc', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
298
    if not Assigned(hInet) then
299
      MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOk], 0)
300
    else try
301
      if bSSL then
302
        port := INTERNET_DEFAULT_HTTPS_PORT
303
      else
304
        port := INTERNET_DEFAULT_HTTP_PORT;
305
      flag := INTERNET_SERVICE_HTTP;
306
      hConnect := InternetConnect(hInet, PWideChar(sServer), port, nil, nil, flag, 0, 0);
307
      if not Assigned(hConnect) then
308
        MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOk], 0)
309
      else try
310
        if bSSL then
311
          flag := INTERNET_FLAG_SECURE  or INTERNET_FLAG_KEEP_CONNECTION;
312
        hRequest := HttpOpenRequest(hConnect, PWideChar(sMethod), PWideChar(sScript), nil, nil, @accept, flag, 0);
313
        if not Assigned(hRequest) then
314
          MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOk], 0)
315
        else try
316
          flag := HTTP_ADDREQ_FLAG_ADD;
317
          HttpAddRequestHeaders(hRequest, Pointer(header), ByteLength(header), flag);
318
          if not HttpSendRequest(hRequest, nil, 0, Pointer(rbsParams), Length(rbsParams)) then
319
            MessageDlg(SysErrorMessage(GetLastError), mtError, [mbOk], 0)
320
          else
321
            MessageDlg(MsgLicenseSuccess, mtConfirmation, [mbOk], 0);
322
        finally
323
          InternetCloseHandle(hRequest);
324
        end;
325
      finally
326
        InternetCloseHandle(hConnect);
327
      end;
328
    finally
329
      InternetCloseHandle(hInet);
330
    end;
331
    }
332
  finally
333
    ParamList.Free;
334
    Screen.Cursor:= crDefault;
335
    BBAutomatic.Enabled := True;
336
  end;
337
end;
338
339
procedure TFLicense.BBHelpClick(Sender: TObject);
340
begin
341
  Application.HelpCommand(HELP_CONTEXT, HelpContext);
342
end;
343
344
procedure TFLicense.BBImportClick(Sender: TObject);
345
var
346
  DesktopPath: array[0..MAX_PATH] of Char;
347
begin
348
  OpenDialogLicFile.FileName := 'InraPorc.lic';
349
  SHGetFolderPath(0, CSIDL_DESKTOP, 0, SHGFP_TYPE_CURRENT, DesktopPath);
350
  OpenDialogLicFile.InitialDir := DesktopPath;
351
  if OpenDialogLicFile.Execute
352
  then // Importer les informations
353
  begin
354
    LoadLicFile(OpenDialogLicFile.FileName);
355
    DisplayLicense;
356
  end;
357
end;
358
359
procedure TFLicense.BBManualClick(Sender: TObject);
360
var
361
  DesktopPath: array[0..MAX_PATH] of Char;
362
begin
363
  if not LicenseIsValid
364
  then
365
    Exit;
366
  SaveDialogLicFile.FileName := 'InraPorc.lic';
367
  SHGetFolderPath(0, CSIDL_DESKTOP, 0, SHGFP_TYPE_CURRENT, DesktopPath);
368
  SaveDialogLicFile.InitialDir := DesktopPath;
369
  if SaveDialogLicFile.Execute
370
  then // Exporter les informations
371
  begin
372
    SaveLicFile(SaveDialogLicFile.FileName);
373
    if Copy(LanguageCode, 1, 2) = 'fr'
374
    then
375
      ShellExecute(Handle, 'open', PChar(URL_HTTPS + 'key_fr.html'), nil, nil, SW_SHOWNORMAL)
376
    else
377
      ShellExecute(Handle, 'open', PChar(URL_HTTPS + 'key_en.html'), nil, nil, SW_SHOWNORMAL);
378
  end;
379
end;
380
381
procedure TFLicense.PBLicenseNumberChange(Sender: TObject);
382
begin
383
  LicenseNumber := PBLicenseNumber.AsInteger;
384
end;
385
386
procedure TFLicense.DisplayLicense;
387
begin
388
  case LicenseType of
389
    2: // Compl?te
390
      RBComplete.Checked := True;
391
    1: // Education
392
      RBEducation.Checked := True;
393
    else // Evaluation
394
      RBEvaluation.Checked := True;
395
  end;
396
  PBLicenseNumber.AsInteger := LicenseNumber;
397
  ECourse.Text := Course;
398
  EFirstName.Text := FirstName;
399
  ELastName.Text := LastName;
400
  ECompany.Text := Company;
401
  EAddress1.Text := Address1;
402
  EAddress2.Text := Address2;
403
  EPostalCode.Text := PostalCode;
404
  ECity.Text := City;
405
  ECountry.Text := Country;
406
  EPhone.Text := Phone;
407
  EFax.Text := Fax;
408
  EMail.Text := Mail;
409
end;
410
411
function TFLicense.LicenseIsValid: Boolean;
412
begin
413
  Result := False;
414
  if RBComplete.Checked and (DriveType(Drive) <> DRIVE_FIXED)
415
  then // La version compl?te doit ?tre install?e sur un disque fixe
416
  begin
417
    MessageDlg(MsgFixedDrive, mtWarning, [mbOk], 0);
418
    ActiveControl := RBComplete;
419
    Exit;
420
  end;
421
  if RBComplete.Checked and (LicenseNumber = 0)
422
  then // Num?ro de licence non renseign?
423
  begin
424
    MessageDlg(Format(MsgRequiredField, [LLicenseNumber.Caption]), mtWarning, [mbOk], 0);
425
    ActiveControl := PBLicenseNumber;
426
    Exit;
427
  end;
428
  if (RBEducation.Checked or RBComplete.Checked) and (Length(FirstName) = 0)
429
  then // Pr?nom non renseign?
430
  begin
431
    MessageDlg(Format(MsgRequiredField, [LFirstName.Caption]), mtWarning, [mbOk], 0);
432
    ActiveControl := EFirstName;
433
    Exit;
434
  end;
435
  if (RBEducation.Checked or RBComplete.Checked) and (Length(LastName) = 0)
436
  then // Nom de famille non renseign?
437
  begin
438
    MessageDlg(Format(MsgRequiredField, [LLastName.Caption]), mtWarning, [mbOk], 0);
439
    ActiveControl := ELastName;
440
    Exit;
441
  end;
442
  if RBEducation.Checked and (Length(Company) = 0)
443
  then // Etablissement non renseign?
444
  begin
445
    MessageDlg(Format(MsgRequiredField, [LOrganisation.Caption]), mtWarning, [mbOk], 0);
446
    ActiveControl := ECompany;
447
    Exit;
448
  end;
449
  if (RBEducation.Checked or RBComplete.Checked) and (Length(Mail) = 0)
450
  then // Mail non renseign?
451
  begin
452
    MessageDlg(MsgMissingMail, mtWarning, [mbOk], 0);
453
    ActiveControl := EMail;
454
    Exit;
455
  end;
456
  if RBComplete.Checked and ((Length(Address1) = 0) or (Length(City) = 0) or (Length(Country) = 0))
457
  then // Adresse non renseign?e
458
  begin
459
    MessageDlg(MsgMissingAddress, mtWarning, [mbOk], 0);
460
    ActiveControl := EAddress1;
461
    Exit;
462
  end;
463
  Result := True;
464
end;
465
466
procedure TFLicense.EAddress1Change(Sender: TObject);
467
begin
468
  Address1 := EAddress1.Text;
469
end;
470
471
procedure TFLicense.EAddress2Change(Sender: TObject);
472
begin
473
  Address2 := EAddress2.Text;
474
end;
475
476
procedure TFLicense.ECityChange(Sender: TObject);
477
begin
478
  City := ECity.Text;
479
end;
480
481
procedure TFLicense.ECompanyChange(Sender: TObject);
482
begin
483
  Company := ECompany.Text;
484
end;
485
486
procedure TFLicense.ECountryChange(Sender: TObject);
487
begin
488
  Country := ECountry.Text;
489
end;
490
491
procedure TFLicense.ECourseChange(Sender: TObject);
492
begin
493
  Course :=ECourse.Text;
494
end;
495
496
procedure TFLicense.EFaxChange(Sender: TObject);
497
begin
498
  Fax := EFax.Text;
499
end;
500
501
procedure TFLicense.EFirstNameChange(Sender: TObject);
502
begin
503
  FirstName := EFirstName.Text;
504
end;
505
506
procedure TFLicense.ELastNameChange(Sender: TObject);
507
begin
508
  LastName := ELastName.Text;
509
end;
510
511
procedure TFLicense.EMailChange(Sender: TObject);
512
begin
513
  Mail := EMail.Text;
514
end;
515
516
procedure TFLicense.EPhoneChange(Sender: TObject);
517
begin
518
  Phone := EPhone.Text;
519
end;
520
521
procedure TFLicense.EPostalCodeChange(Sender: TObject);
522
begin
523
  PostalCode := EPostalCode.Text;
524
end;
525
526
procedure TFLicense.WMSysCommand(var Message: TWMSysCommand);
527
begin
528
  if Message.CmdType = SC_MINIMIZE
529
  then
530
    Application.Minimize
531
  else
532
    inherited;
533
end;
534
535
end.