unewdevicewizard.pas

来自「FMA is a free1 powerful phone editing to」· PAS 代码 · 共 939 行 · 第 1/2 页

PAS
939
字号
unit uNewDeviceWizard;

{
*******************************************************************************
* Descriptions: Getting Started... for FMA
* $Source: /cvsroot/fma/fma/uNewDeviceWizard.pas,v $
* $Locker:  $
*
* TODO:
*
* Change Log:
* $Log: uNewDeviceWizard.pas,v $
*
}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, TntControls, Forms, TntForms,
  Dialogs, TntDialogs, StdCtrls, TntStdCtrls, CheckLst, TntCheckLst, ExtCtrls, TntExtCtrls,
  jpeg, ComCtrls, TntComCtrls, WIrCOMMSocket, WSocket, WBluetoothSocket,
  CPort, ImgList, CPortCtl, Menus, TntMenus, USBMonitor, Registry;

const
  WM_NEXTWPAGE = WM_USER + 101;
  WM_PREVWPAGE = WM_USER + 102;
  WM_HANDLEMESSAGE = WM_USER + 103;

  ndcBluetooth   = 0;
  ndcInfrared    = 1;
  ndcSerial      = 2;

type
  TSearchHandleMessage = record
    Msg: Cardinal;
    Message: PChar;
    Length: Longint;
    Result: Longint;
  end;

  TSelectedDeviceInfo = record
    ConnectionType: byte; // 0 - bluetooth, 1 - infrared, 2 - serial
    DeviceName,FriendlyName: WideString;
    MaxSpeed: TBaudRate; // Serial only
    Address: string; // Bluetooth address, or COM port name
    DisableObex,DisableIrmcSync,DisableSyncML: boolean;
  end;

  TfrmNewDeviceWizard = class(TTntForm)
    NextButton: TTntButton;
    CancelButton: TTntButton;
    Bevel1: TTntBevel;
    PreviousButton: TTntButton;
    nbWizard: TNotebook;
    FinishedPanel: TTntPanel;
    imgWizard: TTntImage;
    lbDescription: TTntLabel;
    lbProductName: TTntLabel;
    WelcomePanel: TTntPanel;
    imgFinished: TTntImage;
    lblFinished: TTntLabel;
    Label3: TTntLabel;
    lblWelcomeNext: TTntLabel;
    TopPanel1: TTntPanel;
    TopDetailsLabel5: TTntLabel;
    TopBevel5: TTntBevel;
    TopCaptionLabel5: TTntLabel;
    imgWizardSmall1: TTntImage;
    TntImage1: TTntImage;
    TntLabel1: TTntLabel;
    TntLabel2: TTntLabel;
    TntImage2: TTntImage;
    TntLabel3: TTntLabel;
    TntImage3: TTntImage;
    TntLabel4: TTntLabel;
    TopPanel3: TTntPanel;
    TntLabel5: TTntLabel;
    TntBevel1: TTntBevel;
    TntLabel6: TTntLabel;
    imgWizardSmall3: TTntImage;
    TopPanel2: TTntPanel;
    TntLabel7: TTntLabel;
    TntBevel2: TTntBevel;
    TntLabel8: TTntLabel;
    imgWizardSmall2: TTntImage;
    lvDevices: TTntListView;
    lblSearchInfo: TTntLabel;
    TntLabel10: TTntLabel;
    TntLabel11: TTntLabel;
    edFriendlyName: TTntEdit;
    Animate1: TAnimate;
    LocalComPort: TComPort;
    LocalWBtSocket: TWBluetoothSocket;
    LocalWIrSocket: TWIrCOMMSocket;
    ImageList1: TImageList;
    TntPopupMenu1: TTntPopupMenu;
    Refresh1: TTntMenuItem;
    TntLabel9: TTntLabel;
    ImageList2: TImageList;
    View1: TTntMenuItem;
    N1: TTntMenuItem;
    AsIcons1: TTntMenuItem;
    AsList1: TTntMenuItem;
    TntLabel12: TTntLabel;
    cbCalibrate: TTntCheckBox;
    cbDeviceReady: TTntCheckBox;
    Image1: TImage;
    TntLabel13: TTntLabel;
    RefreshButton: TTntButton;
    mmoSummary: TTntMemo;
    DeviceMonitorUSB1: TDeviceMonitorUSB;
    Timer1: TTimer;
    cbDontShow: TTntCheckBox;
    cbCheckObex: TTntCheckBox;
    NoItemsPanel: TTntPanel;
    TntLabel14: TTntLabel;
    cbSearchBT: TTntCheckBox;
    cbSearchIR: TTntCheckBox;
    cbSearchCOM: TTntCheckBox;
    cbSearchAll: TTntCheckBox;
    procedure NextButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure PreviousButtonClick(Sender: TObject);
    procedure nbWizardPageChanged(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure TntFormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure LocalComPortRxChar(Sender: TObject; Count: Integer);
    procedure OnSocketDataAvailable(Sender: TObject; Error: Word);
    procedure TntPopupMenu1Popup(Sender: TObject);
    procedure Refresh1Click(Sender: TObject);
    procedure AsIcons1Click(Sender: TObject);
    procedure AsList1Click(Sender: TObject);
    procedure TntFormDestroy(Sender: TObject);
    procedure cbDeviceReadyClick(Sender: TObject);
    procedure lvDevicesSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure OnUSBDeviceChange(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure cbDontShowClick(Sender: TObject);
    procedure lvDevicesInsert(Sender: TObject; Item: TListItem);
    procedure cbSearchAllClick(Sender: TObject);
  private
    { Private declarations }
    FConnectionType: byte;
    FCanceled,FExiting,FReceived,FSearchCompleted: boolean;
    FRxBuffer: TStringList;
    FMessageBuf: string;
    FSelected: TSelectedDeviceInfo;
    procedure EnumLikelyComPorts(var AList: TStringList);
    procedure WizardPageNext(var Msg: TMessage); message WM_NEXTWPAGE;
    procedure WizardPagePrevious(var Msg: TMessage); message WM_PREVWPAGE;
    procedure DoSearch(Reason: WideString = '');
    procedure DoCalibrateConnection;
    procedure DoCheckObex;
    procedure TxAndWait(Data: string);
    procedure AddDevice(Text: WideString; Address: string = '';
      FriendlyName: WideString = ''; Manufacturer: WideString = '');
    function Get_DontShowVis: boolean;
    procedure Set_DontShowVis(const Value: boolean);
    function Get_DontShow: boolean;
  protected
    procedure HandleMessage(var Msg: TSearchHandleMessage); message WM_HANDLEMESSAGE;
  public
    { Public declarations }
    constructor CreateImg(AOwner: TComponent; AImage,AImageSmall: TPicture; AStatus: string = '');
  published
    property IsCanceled: boolean read FCanceled;
    property IsDontShow: boolean read Get_DontShow;
    property IsDontShowVisible: boolean read Get_DontShowVis write Set_DontShowVis;
    property SelectedDevice: TSelectedDeviceInfo read FSelected write FSelected;
  end;

var
  frmNewDeviceWizard: TfrmNewDeviceWizard;

implementation

{$R *.dfm}

uses
  gnugettext, WinSock, TntClasses, Unit1, uGlobal,
  uolSelectPatchPath, uDialogs, uStatusDlg, uThreadSafe, uSMS;

const
  { Wizard pages indexes }
  piWelcome       = 0;
  piPrepare       = 1;
  piSearch        = 2;
  piName          = 3;
  piFinished      = 4;

constructor TfrmNewDeviceWizard.CreateImg(AOwner: TComponent; AImage,AImageSmall: TPicture; AStatus: string);
begin
  Create(AOwner);
  if Assigned(AImage) and Assigned(AImage.Graphic) and not AImage.Graphic.Empty then
    imgWizard.Picture.Assign(AImage);
  if Assigned(AImageSmall) and Assigned(AImageSmall.Graphic) and not AImageSmall.Graphic.Empty then
    imgWizardSmall1.Picture.Assign(AImageSmall);
  { Populate dublicate images }
  if not imgWizard.Picture.Graphic.Empty then imgFinished.Picture.Assign(imgWizard.Picture);
  if not imgWizardSmall1.Picture.Graphic.Empty then begin
    imgWizardSmall2.Picture.Assign(imgWizardSmall1.Picture);
    imgWizardSmall3.Picture.Assign(imgWizardSmall1.Picture);
  end;
  nbWizard.PageIndex := piWelcome;
  if AStatus <> '' then
    lbDescription.Caption := lbDescription.Caption + sLineBreak+sLineBreak + AStatus;
end;

procedure TfrmNewDeviceWizard.FormCreate(Sender: TObject);
begin
  TranslateComponent(Self);
  FRxBuffer := TStringList.Create;
  FCanceled := False;
{$IFDEF VER150}
  WelcomePanel.ParentBackground := False;
  TopPanel1.ParentBackground := False;
  TopPanel2.ParentBackground := False;
  TopPanel3.ParentBackground := False;
  FinishedPanel.ParentBackground := False;
  NoItemsPanel.ParentBackground := False;
{$ENDIF}
end;

procedure TfrmNewDeviceWizard.WizardPageNext(var Msg: TMessage);
begin
  if nbWizard.PageIndex < nbWizard.Pages.Count-1 then
    nbWizard.PageIndex := nbWizard.PageIndex + 1;
end;

procedure TfrmNewDeviceWizard.WizardPagePrevious(var Msg: TMessage);
begin
  if nbWizard.PageIndex > 0 then
    nbWizard.PageIndex := nbWizard.PageIndex - 1;
end;

procedure TfrmNewDeviceWizard.NextButtonClick(Sender: TObject);
var
  e: WideString;
begin
  case nbWizard.PageIndex of
    piName: begin
      e := Form1.IsNewPhoneNameOK(edFriendlyName.Text);
      if e <> '' then
        MessageDlgW(e,mtError,MB_OK)
      else
        SendMessage(Handle,WM_NEXTWPAGE,0,0);
    end;
    piFinished: begin { finished }
      { Get selected COM port }
      LocalComPort.Port := lvDevices.Selected.SubItems[0];
      if cbCalibrate.Checked then DoCalibrateConnection;
      if cbCheckObex.Checked then DoCheckObex;
      Update;
      { Fill selected device data }
      FSelected.ConnectionType := Integer(lvDevices.Selected.Data);
      FSelected.DeviceName := lvDevices.Selected.Caption;
      FSelected.FriendlyName := edFriendlyName.Text;
      FSelected.MaxSpeed := LocalComPort.BaudRate;
      FSelected.Address := lvDevices.Selected.SubItems[0];
      { Exit Wizard }
      FExiting := True;
      ModalResult := mrOk;
    end
    else
      SendMessage(Handle,WM_NEXTWPAGE,0,0);
  end;
end;

procedure TfrmNewDeviceWizard.PreviousButtonClick(Sender: TObject);
begin
  case nbWizard.PageIndex of
    piWelcome: { welcome }
      { do nothing } ;
    else
      SendMessage(Handle,WM_PREVWPAGE,0,0);
  end;
end;

procedure TfrmNewDeviceWizard.nbWizardPageChanged(Sender: TObject);
const
  sTab = '    ';
var
  s: WideString;
  b: Boolean;
begin
  case nbWizard.PageIndex of
    piWelcome: begin { welcome }
      PreviousButton.Enabled := False;
      NextButton.Caption := _('&Next >');
      NextButton.Enabled := True;
      NextButton.Default := False;
      cbDontShowClick(nil); // update Cancel button
    end;
    piPrepare: begin { install }
      PreviousButton.Enabled := True;
      NextButton.Caption := _('&Search');
      CancelButton.Caption := _('&Cancel');
      b := FSearchCompleted;
      cbDeviceReadyClick(cbDeviceReady);
      FSearchCompleted := b;
    end;
    piSearch: begin
      if NextButton.Enabled then NextButton.SetFocus;
      NextButton.Caption := _('&Next >');
      lvDevicesSelectItem(lvDevices,nil,False);
      if not FSearchCompleted then DoSearch;
    end;
    piName: begin
      NextButton.Caption := _('&Next >');
      NextButton.Default := False;
      //CancelButton.Enabled := True;
      s := lvDevices.Selected.SubItems[1];
      if s = '' then s := WideFormat(_('My %s phone'),[lvDevices.Selected.Caption]);
      if edFriendlyName.Text = '' then edFriendlyName.Text := s;
    end;
    piFinished: begin { finished }
      NextButton.Caption := _('&Finish');
      //CancelButton.Enabled := False;
      { setup tasks on finish }
      cbCalibrate.Checked := False;
      cbCalibrate.Visible := Integer(lvDevices.Selected.Data) = ndcSerial; // only for Serial
      cbCheckObex.Checked := True;
      cbCheckObex.Visible := True; // Form1.IsT610orBetter(lvDevices.Selected.Caption); // T610+ only

      FSelected.DisableObex := False;
      FSelected.DisableIrmcSync := False;
      FSelected.DisableSyncML := True;
      with mmoSummary.Lines do begin
        Clear;
        Add(_('Phone Device:'));
        if lvDevices.Selected.SubItems[1] <> '' then
          Add(sTab + _(lvDevices.Selected.SubItems[1]));
        Add(sTab + _(lvDevices.Selected.SubItems[2]));
        Add(sTab + _(lvDevices.Selected.Caption));
        if (edFriendlyName.Text <> '') and (WideCompareStr(edFriendlyName.Text,lvDevices.Selected.SubItems[1]) <> 0) then
          Add(sTab + _(edFriendlyName.Text));
        Add('');
        Add(_('Connection Type:'));
        case Integer(lvDevices.Selected.Data) of
          ndcBluetooth:  s := 'Bluetooth';
          ndcInfrared:   s := 'Infrared';
          ndcSerial:     s := 'Data Cable or Virtual Bluetooth Port';
        end;
        Add(sTab + _(s));
        Add(sTab + _(lvDevices.Selected.SubItems[0]));
        Add('');
        Add(_('Summary:'));
        if WideCompareText('SONY ERICSSON',lvDevices.Selected.SubItems[2]) <> 0 then begin
          Add(sTab + _('Only Sony Ericsson phones are officialy supported.'));
          Add(sTab + _('Not all of the features might work as expected.'));
        end
        else
        if Integer(lvDevices.Selected.Data) = ndcInfrared then begin
          { Infrared, so no OBEX }
          Add(sTab + _('File Browseing is not supported over Infrared.'));
          Add(sTab + _('Phonebook Sync is not supported (requires above).'));
          Add(sTab + _('Switch to Bluetooth or Data Cable in order to use them.'));
          FSelected.DisableObex := True;
          FSelected.DisableIrmcSync := True;
        end
        else
        if not Form1.IsT610orBetter(lvDevices.Selected.Caption) then begin // FSelected.DeviceName is empty here!
          { Older phones (T230, T68, ...) so no OBEX }
          Add(sTab + _('Phonebook Sync is not supported by your phone.'));
          FSelected.DisableIrmcSync := True;
        end
        else
          if Form1.IsK610orBetter(lvDevices.Selected.Caption) then // FSelected.DeviceName is empty here!
            Add(sTab + _('Your phone device is detected and supported partially.')) // K610+
          else
            Add(sTab + _('Your phone device is detected and supported.')); // T610+ are OK
      end;
      cbCheckObex.Enabled := cbCheckObex.Visible and not FSelected.DisableObex;
      cbCheckObex.Checked := cbCheckObex.Enabled;
    end;
  end;
  if Visible and NextButton.Visible and NextButton.Enabled then NextButton.SetFocus;
end;

procedure TfrmNewDeviceWizard.CancelButtonClick(Sender: TObject);
begin
  if (nbWizard.PageIndex = piSearch) and not RefreshButton.Enabled then
    { Just cancel the search process }
    FCanceled := True
  else begin
    MessageBeep(MB_ICONQUESTION);
    if ((nbWizard.PageIndex = piWelcome) and cbDontShow.Checked) or
      (MessageDlgW(_('The Wizard is not complete. Do you really want to exit?'+
      sLinebreak+sLinebreak+'You can run this wizard at a later time to complete it.'+
      sLinebreak+sLinebreak+'To exit Wizard right now click Yes. To continue click No.'),
      mtConfirmation, MB_YESNO) = ID_YES) then begin
      FExiting := True;
      ModalResult := mrCancel;
    end;
  end;
end;

procedure TfrmNewDeviceWizard.TntFormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if CancelButton.Enabled and not FExiting then begin
    CancelButton.Click;
    if not FExiting then begin
      ModalResult := mrNone;
      CanClose := False;
    end;
  end;
  FCanceled := ModalResult = mrCancel;
end;

procedure TfrmNewDeviceWizard.DoSearch(Reason: WideString);
var
  i: integer;
  ComPortList: TStringList;
  ComDevice,ComManufacturer: WideString;
  BtDevices: TBtDevicesInfo;
  IrDevices: TIrdaDevicesInfo;
  DevAddr,DevName: string;
  procedure ProbeDevice;
  begin
    ComDevice := '';
    ComManufacturer := '';
    try
      TxAndWait('ATE0'); // do not localize
      if FReceived then begin
        TxAndWait('ATI'); // do not localize
        if FReceived then begin
          ComDevice := FRxBuffer[0];
          try
            { Check if we have GSM device }
            TxAndWait('AT+CGMM'); // do not localize
            if FReceived then ComDevice := WideFormat('%s (%s)',[ComDevice,FRxBuffer[0]]);
            TxAndWait('AT+CGMI'); // do not localize
            if FReceived then ComManufacturer := FRxBuffer[0];
          except
          end;
          AddDevice(ComDevice,DevAddr,DevName,ComManufacturer);
        end;
      end;
    except
    end;
  end;
begin
  if Reason <> '' then Reason := WideFormat(' (%s)',[Reason]);
  Animate1.Visible := True;
  Animate1.Active := True;
  Animate1.Color := clWindow;
  PreviousButton.Visible := False;
  NextButton.Visible := False;
  RefreshButton.Enabled := False;
  IrDevices := nil;
  BtDevices := nil;
  try
    lvDevices.Items.Clear;
    lvDevices.SetFocus;
    NoItemsPanel.Visible := True;
    FCanceled := False;

    lblSearchInfo.Caption := _('Preparing Search...');
    Application.ProcessMessages;
    if FCanceled then exit;

    // bluetooth
    if cbSearchBT.Checked then begin
      try
        BtDevices := LocalWBtSocket.GetConnectedDevices;
      except
        AddDevice(_('Native Bluetooth is not supported!'));
      end;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?