unit vcard;
{
    Copyright 2001, Peter Millard

    This file is part of Exodus.

    Exodus is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Exodus is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Exodus; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}

interface

uses
  XMLVCard,
  XMLTag, iq, Dockable,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, buttonFrame, StdCtrls, ExtCtrls, TntStdCtrls, RxGIF;

type
  TfrmVCard = class(TfrmDockable)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label2: TLabel;
    lblEmail: TLabel;
    TabSheet3: TTabSheet;
    TabSheet5: TTabSheet;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    frameButtons1: TframeButtons;
    lblURL: TLabel;
    txtNick: TTntEdit;
    txtPriEmail: TTntEdit;
    txtWeb: TTntEdit;
    txtOrgName: TTntEdit;
    txtOrgUnit: TTntEdit;
    txtOrgTitle: TTntEdit;
    txtWorkVoice: TTntEdit;
    txtWorkFax: TTntEdit;
    Label16: TLabel;
    txtName: TTntEdit;
    Label12: TLabel;
    Label6: TLabel;
    Label28: TLabel;
    Label1: TLabel;
    cboOcc: TTntComboBox;
    txtBDay: TTntEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    txtWorkCountry: TTntComboBox;
    txtWorkState: TTntEdit;
    txtWorkZip: TTntEdit;
    txtWorkCity: TTntEdit;
    txtWorkStreet2: TTntEdit;
    txtWorkStreet1: TTntEdit;
    memDesc: TTntMemo;
    Label13: TLabel;
    Label21: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    txtHomeCountry: TTntComboBox;
    txtHomeState: TTntEdit;
    txtHomeZip: TTntEdit;
    txtHomeCity: TTntEdit;
    txtHomeStreet2: TTntEdit;
    txtHomeStreet1: TTntEdit;
    Label5: TLabel;
    txtJID: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    txtHomeVoice: TTntEdit;
    txtHomeFax: TTntEdit;
    Image1: TImage;
    Bevel1: TBevel;
    Label7: TLabel;
    Label17: TLabel;
    cboGender: TTntComboBox;
    Image2: TImage;
    Label18: TLabel;
    Bevel2: TBevel;
    Image3: TImage;
    Label25: TLabel;
    Bevel3: TBevel;
    Label26: TLabel;
    txtWorkMobile: TTntEdit;
    Label27: TLabel;
    TntEdit2: TTntEdit;
    Label33: TLabel;
    txtHomeMobile: TTntEdit;
    Label34: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure frameButtons1btnCancelClick(Sender: TObject);
    procedure frameButtons1btnOKClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    Vcardiq: TJabberIQ;
    Judiq: TJabberIQ;
    _vcard: TXMLVCard;
    procedure Callback(event: string; tag: TXMLTag);
    procedure RegCallback(event: string; tag: TXMLTag);
    procedure DoUpdateJUD;
    procedure DoUpdateRoster;
  public
    { Public declarations }
  end;

var
  frmVCard: TfrmVCard;

procedure ShowMyProfile;
procedure RegMyProfile;

resourcestring
  sVCardError = 'No vCard response was ever returned.';


{---------------------------------------}
{---------------------------------------}
{---------------------------------------}
implementation

{$R *.dfm}
uses
  GnuGetText, Session, JabberConst, Signals, RiserWindow, Jabber1;

procedure RegMyProfile;
var
  f: TfrmVCard;
begin
  f := TfrmVCard.Create(Application);
  f.Caption := '注册个人信息';
  f.frameButtons1.btnOK.Visible := false;
  f.frameButtons1.btnCancel.ModalResult := mrNone;
  f.frameButtons1.btnCancel.Caption := '确定';
  f.frameButtons1.btnCancel.OnClick := f.frameButtons1btnOKClick;
  f.Show;
end;

{---------------------------------------}

procedure ShowMyProfile;
var
  f: TfrmVCard;
begin
  f := TfrmVCard.Create(Application);
  f.Show;
end;

{---------------------------------------}

procedure TfrmVCard.FormCreate(Sender: TObject);
var
  tmps: Widestring;
begin
  TranslateProperties(Self);

  PageControl1.ActivePageIndex := 0;
  MainSession.Prefs.RestorePosition(Self);

  _vcard := TXMLVCard.Create();

  tmps := MainSession.generateID();
  Vcardiq := TJabberIQ.Create(MainSession, tmps, Callback);
  Vcardiq.qTag.Name := 'VCARD';
  Vcardiq.Namespace := 'vcard-temp';
  Vcardiq.iqType := 'get';
  Vcardiq.toJid := MainSession.BareJid; // Username + '@' + MainSession.Server;
  Vcardiq.Send();
  txtJID.Text := MainSession.BareJid;
end;

{---------------------------------------}

procedure TfrmVCard.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MainSession.Prefs.SavePosition(Self);
  Action := caFree;
end;

{---------------------------------------}

procedure TfrmVCard.Callback(event: string; tag: TXMLTag);
begin
  Vcardiq := nil;
    // callback for vcard info
  if (tag = nil) then begin
    ShowRiserWindow(nil, sVCardError, ico_info);
        //MessageBox(Handle, Pchar(sVCardError),
        //    '信息', MB_OK OR MB_ICONINFORMATION);
    exit;
  end;

  _vcard.parse(tag);

  with _vcard do begin
    txtName.Text := FullName;
    txtNick.Text := nick;
    txtPriEmail.Text := email;
    txtWeb.Text := url;
    cboOcc.Text := role;
    cboGender.ItemIndex := cboGender.items.IndexOf(gender);
    txtBday.Text := bday;

    txtWorkVoice.Text := WorkPhone.number;
    txtWorkFax.Text := WorkFax.number;
    txtWorkMobile.Text := WorkCell.number;
    txtHomeVoice.Text := HomePhone.number;
    txtHomeFax.Text := HomeFax.number;
    txtHomeMobile.Text := HomeCell.number;


    with work do begin
      txtWorkStreet1.Text := Street;
      txtWorkStreet2.Text := ExtAdr;
      txtWorkCity.Text := Locality;
      txtWorkState.Text := Region;
      txtWorkZip.Text := PCode;
      txtWorkCountry.Text := Country;
    end;

    with Home do begin
      txtHomeStreet1.Text := Street;
      txtHomeStreet2.Text := ExtAdr;
      txtHomeCity.Text := Locality;
      txtHomeState.Text := Region;
      txtHomeZip.Text := PCode;
      txtHomeCountry.Text := Country;
    end;

    txtOrgName.Text := OrgName;
    txtOrgUnit.Text := OrgUnit;
    txtOrgTitle.Text := OrgTitle;
    memDesc.Lines.Text := Desc;
  end;
end;

{---------------------------------------}

procedure TfrmVCard.frameButtons1btnCancelClick(Sender: TObject);
begin
  Self.Close;
end;

{---------------------------------------}

procedure TfrmVCard.frameButtons1btnOKClick(Sender: TObject);
var
  iq: TXMLTag;
begin
  if (txtName.Text = '') or (txtNick.Text = '') or
    (txtOrgName.Text = '') or (txtPriEmail.Text = '') or
    (txtWorkMobile.Text = '') then
  begin
    ShowRiserWindow(nil, '姓名、昵称、Email和公司及工作手机必须有值，请检查！', ico_info);
      //MessageBox(handle, '姓名、昵称、Email和公司必须有值，请检查！', '错误', MB_OK or MB_ICONERROR);
    exit;
  end;
    // Save the vcard..
  with _vcard do begin
    FullName := txtName.Text;
//        GivenName := txtFirst.Text;
//        FamilyName := txtLast.Text;
    nick := txtNick.Text;
    email := txtPriEmail.Text;
    url := txtWeb.Text;
    role := cboOcc.Text;
    bday := txtBday.Text;
    gender := cboGender.Text;
    Desc := memDesc.Lines.Text;

    WorkPhone.number := txtWorkVoice.Text;
    WorkPhone.work := true; WorkPhone.voice := true;

    WorkFax.number := txtWorkFax.Text;
    WorkFax.work := true; WorkFax.fax := true;

    WorkCell.number := txtWorkMobile.Text;
    WorkCell.work := true; WorkCell.cell := true;

    HomePhone.number := txtHomeVoice.Text;
    HomePhone.home := true; HomePhone.voice := true;

    HomeFax.number := txtHomeFax.Text;
    HomeFax.home := true; HomeFax.fax := true;

    HomeCell.number := txtHomeMobile.Text;
    HomeCell.Home := true; HomeCell.cell := true;

    with work do begin
      work := true;
      home := false;
      Street := txtWorkStreet1.Text;
      ExtAdr := txtWorkStreet2.Text;
      Locality := txtWorkCity.Text;
      Region := txtWorkState.Text;
      PCode := txtWorkZip.Text;
      Country := txtWorkCountry.Text;
    end;

    with Home do begin
      home := true;
      work := false;
      Street := txtHomeStreet1.Text;
      ExtAdr := txtHomeStreet2.Text;
      Locality := txtHomeCity.Text;
      Region := txtHomeState.Text;
      PCode := txtHomeZip.Text;
      Country := txtHomeCountry.Text;
    end;

    OrgName := txtOrgName.Text;
    OrgUnit := txtOrgUnit.Text;
    OrgTitle := txtOrgTitle.Text;
  end;

  iq := TXMLTag.Create('iq');
  iq.setAttribute('id', MainSession.generateID());
  iq.setAttribute('type', 'set');
  _vcard.fillTag(iq);

  MainSession.SendTag(iq);
  DoUpdateJUD;
  Self.Close;
end;

procedure TfrmVCard.FormDestroy(Sender: TObject);
begin
  _vcard.Free();
end;

procedure TfrmVCard.DoUpdateJUD;
var
  nulproc: TPacketEvent;
begin
  nulproc := nil;
  Judiq := TJabberIQ.Create(MainSession, MainSession.generateID(), nulproc);
  Judiq.iqType := 'set';
  Judiq.toJID := 'user.' + MainSession.Server;
  Judiq.Namespace := XMLNS_REGISTER;
  Judiq.qTag.AddBasicTag('anyq', MainSession.BareJid);
  Judiq.qTag.AddBasicTag('nick', txtNick.Text);
  Judiq.qTag.AddBasicTag('name', txtName.Text);
  Judiq.qTag.AddBasicTag('company', txtOrgName.Text);
  Judiq.qTag.AddBasicTag('email', MainSession.BareJid);
  Judiq.qTag.AddBasicTag('domain', MainSession.Server);
  Judiq.Send();
end;

procedure TfrmVCard.RegCallback(event: string; tag: TXMLTag);
begin
  Judiq := nil;
  if ((event = 'xml') and (tag.getAttribute('type') = 'result')) then begin
    // normal result
  end
  else begin
    // some kind of error
  end;
  self.Close;
end;

procedure TfrmVCard.DoUpdateRoster;
begin

end;

end.
