unit Profile;
{
    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
  XMLTag, IQ,
  ShellAPI, Dockable, Unicode,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  buttonFrame, StdCtrls, CheckLst, ExtCtrls, ComCtrls, TntStdCtrls, RxGIF;

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

var
  frmProfile: TfrmProfile;

function ShowProfile(jid: string): TfrmProfile;

implementation

{$R *.DFM}
uses
  JabberConst, XMLVCard, ExUtils, GnuGetText,
  Presence, Roster, JabberID, Session, Jabber1, RosterWindow, RiserWindow;

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

function ShowProfile(jid: string): TfrmProfile;
var
  tmp_jid: TJabberID;
  ritem: TJabberRosterItem;
  f: TfrmProfile;
  p: TJabberPres;
  tmps: string;
  i, gi: integer;
begin
  tmp_jid := TJabberID.Create(jid);
  f := TfrmProfile.Create(Application);

  with f do begin
    GrpListBox.Items.Assign(MainSession.roster.GrpList);
    removeSpecialGroups(GrpListBox.Items);
    txtJID.Text := tmp_jid.jid;
    ritem := MainSession.Roster.Find(tmp_jid.jid);
    if (ritem <> nil) then begin
      txtNick.Text := ritem.RawNickname;

      for i := 0 to ritem.Groups.Count - 1 do begin
        gi := GrpListBox.Items.IndexOf(ritem.Groups[i]);
        if gi >= 0 then
          GrpListBox.Checked[gi] := true;
      end;
    end;

        // Show all the resources
    p := MainSession.ppdb.FindPres(tmp_jid.jid, '');
    while p <> nil do begin
      p := MainSession.ppdb.NextPres(p)
    end;

    tmps := MainSession.generateID();
    iq := TJabberIQ.Create(MainSession, tmps, vcard);
    iq.Namespace := 'vcard-temp';
    iq.qTag.Name := 'vCard';
    iq.iqType := 'get';
    iq.toJid := tmp_jid.jid;
    iq.Send;

    PageControl1.ActivePageIndex := 0;
  end;

  tmp_jid.Free();

  f.Show;
//    f.aniProfile.Visible := true;
//    f.aniProfile.Active := true;
  Result := f;
end;

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

procedure TfrmProfile.vcard(event: string; tag: TXMLTag);
var
  vcard: TXMLVCard;
begin
    // callback for vcard info
  iq := nil;
//    aniProfile.Visible := false;
//    aniProfile.Active := false;

  if (event <> 'xml') then exit;
  vcard := TXMLVCard.Create;
  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;

  vcard.Free();
end;

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

procedure TfrmProfile.FormCreate(Sender: TObject);
begin
  TranslateProperties(Self);
  iq := nil;
  MainSession.Prefs.RestorePosition(Self);
end;

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

procedure TfrmProfile.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MainSession.Prefs.SavePosition(Self);
  if (iq <> nil) then
    iq.Free;
  Action := caFree;
end;

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

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

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

procedure TfrmProfile.frameButtons1btnOKClick(Sender: TObject);
var
  ritem: TJabberRosterItem;
  changed: boolean;
  i: integer;
  tmp_grplist: TStringList;
begin
    // if the nick has changed then change the roster item
  ritem := MainSession.roster.Find(txtJID.Text);
  changed := false;
  if ritem <> nil then begin
    if ritem.RawNickname <> txtNick.Text then begin
      ritem.RawNickname := txtNick.Text;
      changed := true;
    end;

    if ritem.mobile <> txtWorkMobile.Text then begin
      ritem.mobile := txtWorkMobile.Text;
      changed := true;
    end;

    tmp_grplist := TStringlist.Create;
    for i := 0 to GrpListBox.Items.Count - 1 do begin
      if GrpListBox.Checked[i] then
        tmp_grplist.Add(grpListBox.Items[i]);
    end;

    for i := 0 to tmp_grplist.Count - 1 do begin
      if ritem.Groups.indexOf(tmp_grplist[i]) < 0 then begin
        ritem.Groups.Add(tmp_grplist[i]);
        changed := true;
      end;
    end;

    for i := ritem.Groups.Count - 1 downto 0 do begin
      if tmp_grpList.indexOf(ritem.Groups[i]) < 0 then begin
        ritem.Groups.Delete(i);
        changed := true;
      end;
    end;
    tmp_grplist.Free();

    if changed then ritem.update();
    MainSession.FireEvent('/roster/item', nil, ritem);
  end;
  Self.Close;
end;

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

procedure TfrmProfile.lblEmailClick(Sender: TObject);
var
  url: string;
begin
    // launch a mailto link..
  if (Sender = lblURL) then
    url := txtWeb.Text
  else
    url := 'mailto:' + txtPriEmail.Text;
  ShellExecute(0, 'open', PChar(url), '', '', SW_SHOW);
end;

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

procedure TfrmProfile.btnAddGroupClick(Sender: TObject);
var
  tmps: Widestring;
  gl: TWideStringList;
  x: TXMLTag;  {}
begin
    // add this group to the listbox.
  tmps := txtNewGrp.Text;
  if (tmps <> '') then begin
      // add the new grp.
    gl := MainSession.Roster.GrpList;
    if (gl.IndexOf(tmps) >= 0) then begin
          // this grp already exists
      ShowRiserWindow(nil, sNewGroupExists, ico_info);
          //MessageBox(Handle,Pchar(sNewGroupExists), '´يخَ', MB_OK OR MB_ICONERROR);
    end
    else begin
          // add the new grp.
      GrpListBox.Items.Add(tmps);
      gl.Add(tmps);
      with frmRosterWindow do begin
        with Lvusers.Items.Add do
        begin
          SubItems.Add(tmps);
          ImageIndex := ico_right;
          caption := '';
          Data := nil;
        end;
        Lvusers.AlphaSort;
      end;
      x := TXMLTag.Create('group');
      x.setAttribute('name', tmps);
      MainSession.FireEvent('/roster/newgroup', x);
      x.Free();
    end;
  end;  {}
end;

procedure TfrmProfile.GrpListBoxClickCheck(Sender: TObject);
var
  i: integer;
begin
  inherited;
  for i:= GrpListBox.Items.Count-1 downto 0 do
    if GrpListBox.ItemIndex<>i then
      GrpListBox.Checked[i] := false;
end;

end.
