unit AuthFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, jpeg;

type
  TAuthorizeForm = class(TForm)
    nbMain: TNotebook;
    btnPrev: TButton;
    btnNext: TButton;
    btnCancel: TButton;
    Label1: TLabel;
    Panel1: TPanel;
    Memo1: TMemo;
    Label4: TLabel;
    gbManual: TGroupBox;
    rbManual: TRadioButton;
    gbAuthFile: TGroupBox;
    rbAuthFile: TRadioButton;
    btnBrowseAuthFile: TButton;
    OpenDialog1: TOpenDialog;
    leAuthorize: TLabeledEdit;
    leAuthFile: TLabeledEdit;
    Image1: TImage;
    mmFinish: TMemo;
    Image2: TImage;
    Label3: TLabel;
    Label7: TLabel;
    Label2: TLabel;
    leHaspID: TLabeledEdit;
    lblPhone: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel3: TPanel;
    Image3: TImage;
    Label10: TLabel;
    lblHint: TLabel;
    Panel2: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure nbMainPageChanged(Sender: TObject);
    procedure btnPrevClick(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure rbAuthFileClick(Sender: TObject);
    procedure rbManualClick(Sender: TObject);
    procedure btnBrowseAuthFileClick(Sender: TObject);
    procedure Label9MouseEnter(Sender: TObject);
    procedure Label9MouseLeave(Sender: TObject);
    procedure Label9Click(Sender: TObject);
  private
    { Private declarations }
    procedure InternalAuthorize(AFile: string);
    procedure InternalAuthorizeByText(AKey: string);
    // 0 正确；-1 错误； -2 损坏
    function CheckAuthorizeFile(AFile: string): Integer;
  public
    { Public declarations }
  end;

function Authorize: Boolean;

implementation

uses
  ScUtils, mEncryptUnit, CryptUtils, ScHaspPwd, ShellAPI;

{$R *.dfm}

function Authorize: Boolean;
var
  Form: TAuthorizeForm;
begin
  {$IFDEF _ScNet}
  {$ELSE}
  if bAuthorized then
  begin
    MessageHint(0, '本产品已注册。'#13#10'产品序列号为：' + strHaspID);
    Exit;
  end;
  Form := TAuthorizeForm.Create(nil);
  try
    Result := Form.ShowModal = mrOK;
  finally
    Form.Free;
  end;
  {$ENDIF}
end;

const
  strWelcome = '尊敬的用户：'#13#10#13#10 +

               '    感谢您购买《纵横SmartCost工程造价管理系统》！'#13#10#13#10 +

               '    您现在可以开始使用本系统，为了保障您的权益，客服中心将有专人与您联系，以完成产品的注册。'#13#10#13#10 +

               '    在完成注册前，本产品可以正常使用50次或200小时，在这段时间内，若未完成注册，请查询“帮助->产品注册”菜单，以完成注册，或咨询客服中心。'#13#10#13#10#13#10#13#10 +

               '    点击“下一步”开始注册。';
  strSuccess = '恭喜您！您已经注册成功！'#13#10#13#10'请重新运行程序即可正常使用软件。';
  strFailed = '注册失败，请确认您的软件合法性，以及产品编号的正确性。'#13#10#13#10'详情请咨询客服中心。';

procedure TAuthorizeForm.FormCreate(Sender: TObject);
begin
  nbMain.PageIndex := 0;
  Memo1.Lines.Clear;
  Memo1.Text := Format(strWelcome, [LoadAuthorizePhone]);
  lblPhone.Caption := LoadAuthorizePhone;
  leHaspID.Text := strHaspID;
end;

procedure TAuthorizeForm.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
  Close;
end;

procedure TAuthorizeForm.nbMainPageChanged(Sender: TObject);
begin
  if nbMain.PageIndex = 0 then
    btnPrev.Enabled := False
  else
    btnPrev.Enabled := True;
  if nbMain.PageIndex = nbMain.Pages.Count - 1 then
  begin
    btnPrev.Enabled := False;
    btnNext.Enabled := True;
    btnNext.Caption := '完成(&O)';
    btnCancel.Enabled := False;
  end;
  case nbMain.PageIndex of
    0:
      lblHint.Caption := '产品注册概述';
    1:
      lblHint.Caption := '注册方式提示';
    2:
      lblHint.Caption := '进行注册';
    3:
      lblHint.Caption := '完成注册';
  end;
end;

procedure TAuthorizeForm.btnPrevClick(Sender: TObject);
begin
  nbMain.PageIndex := nbMain.PageIndex - 1;
end;

procedure TAuthorizeForm.btnNextClick(Sender: TObject);
begin
  case nbMain.PageIndex of
    2:
    begin
      if rbAuthFile.Checked then
      begin
        if not FileExists(leAuthFile.Text) then
        begin
          MessageHint(0, '指定的注册文件不存在，请重新选择。');
          Exit;
        end
        else if CheckAuthorizeFile(leAuthFile.Text) < 0 then
        begin
          MessageHint(0, '指定的注册文件不是有效的SmartCost注册文件，请重新选择。');
          Exit;
        end;
        InternalAuthorize(leAuthFile.Text);
      end
      else if rbManual.Checked then
      begin
        if leAuthorize.Text = '' then
        begin
          MessageHint(0, '请输入注册码。');
          Exit;
        end;
        InternalAuthorizeByText(leAuthorize.Text);
      end;
      if bAuthorized then
      begin
        mmFinish.Text := strSuccess;
        Image1.Visible := True;
        Image2.Visible := False;
      end
      else
      begin
        mmFinish.Text := strFailed;
        Image1.Visible := False;
        Image2.Visible := True;
      end;
    end;
    3:
    begin
      ModalResult := mrOK;
      Close;
    end;
  end;
  nbMain.PageIndex := nbMain.PageIndex + 1;
end;

procedure TAuthorizeForm.rbAuthFileClick(Sender: TObject);
begin
  rbManual.Checked := False;
  leAuthFile.Enabled := True;
  btnBrowseAuthFile.Enabled := True;
  leAuthorize.Enabled := False;
  leAuthFile.SetFocus;
end;

procedure TAuthorizeForm.rbManualClick(Sender: TObject);
begin
  rbAuthFile.Checked := False;
  leAuthFile.Enabled := False;
  btnBrowseAuthFile.Enabled := False;
  leAuthorize.Enabled := True;
  leAuthorize.SetFocus;
end;

procedure TAuthorizeForm.InternalAuthorize(AFile: string);
var
  Key: array [0..1023] of Byte;
  fsFile: TFileStream;
begin
  fsFile := TFileStream.Create(AFile, fmOpenRead);
  try
    fsFile.Read(Key, fsFile.Size);
  finally
    fsFile.Free;
  end;

  UserAuthorize(Key);
end;

procedure TAuthorizeForm.btnBrowseAuthFileClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    leAuthFile.Text := OpenDialog1.FileName;
end;

function TAuthorizeForm.CheckAuthorizeFile(AFile: string): Integer;
var
  fsFile: TFileStream;
  rFile: PAuthorizeFile;
begin
{  fsFile := TFileStream.Create(AFile, fmOpenRead);
  New(rFile);
  ZeroMemory(rFile, SizeOf(TAuthorizeFile));
  try
    fsFile.Read(rFile^, SizeOf(TAuthorizeFile));
    if SameText(rFile.Head, HASP_AuthorizeFileHead) then
      Result := 1
    else
      Result := -1;
  finally
    Dispose(rFile);
    fsFile.Free;
  end;
//}  Result := 1;
end;

procedure TAuthorizeForm.InternalAuthorizeByText(AKey: string);
var
  Key: array [0..1023] of Byte;
  fsFile: TFileStream;
  I: Integer;
begin
//  if Length(AKey) <> Length_AuthorizeFile * 2 then
//    Exit;
  for I := 0 to (Length(AKey) div 2 - 1) do
  begin
    Key[I] := StrToInt('$' + AKey[2 * I + 1] + AKey[2 * I + 2]);
  end;
  UserAuthorize(Key);
end;

procedure TAuthorizeForm.Label9MouseEnter(Sender: TObject);
begin
  TLabel(Sender).Font.Color := clHighLight;
  Screen.Cursor := crHandPoint;
end;

procedure TAuthorizeForm.Label9MouseLeave(Sender: TObject);
begin
  TLabel(Sender).Font.Color := clHotLight;
  Screen.Cursor := crDefault;
end;

procedure TAuthorizeForm.Label9Click(Sender: TObject);
begin
  Shellexecute(Handle, 'open', PChar(Format('mailto:"纵横产品注册中心"<support@smartcost.com.cn>?subject=SmartCost注册，序列号[%s]', [strHaspID])), 'SmartCost软件锁注册', '', SW_SHOW);
end;

end.
