unit Broadcastsms;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, RichEdit2, ExRichEdit, fRosterTree, ExtCtrls,
  TntStdCtrls, Dockable, ToolWin, ImgList;

type
  TfrmBroadcastSMS = class(TfrmDockable)
    GroupBox1: TGroupBox;
    Panel1: TPanel;
    frameTreeRoster1: TframeTreeRoster;
    Panel2: TPanel;
    Panel3: TPanel;
    txtMsg: TExRichEdit;
    Label1: TLabel;
    ToolbarImages: TImageList;
    Panel5: TPanel;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  private
    { Private declarations }
    _thread: string; // thread for conversation
    _Updateing: Boolean;
    procedure SendMsg(L: TList);
    function GetThread(jid: widestring): string;
  public
    { Public declarations }
    ComController: TObject;
  end;

var
  frmBroadcastSMS: TfrmBroadcastSMS;

implementation

{$R *.dfm}
uses GnuGetText, Emoticons, math, MsgDisplay, Jabber1, JabberMsg, XMLTag,
  ExUtils, ExSession, Session, Roster, JabberConst, XMLUtils, COMChatController,
  RiserWindow;

procedure TfrmBroadcastSMS.FormCreate(Sender: TObject);
var
  i: integer;
  echat: TAnyQChat;
begin
  TranslateProperties(Self);

  _thread := '';

  frameTreeRoster1.Initialize();
  frameTreeRoster1.DrawRoster(False);

    // COM interface for this MsgWindow
  echat := TAnyQChat.Create();
//    echat.setIM(Self);
  echat.ObjAddRef();
  ComController := echat;
end;

function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
  FontType: Integer; Data: Pointer): Integer; stdcall;
begin
  TStrings(Data).Add(LogFont.lfFaceName);
  Result := 1;
end;

procedure TfrmBroadcastSMS.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmBroadcastSMS.Button1Click(Sender: TObject);
var
  l: Tlist;
  txt: widestring;
begin
  txt := txtMsg.WideLines.Text;
  if txt = '' then
  begin
    ShowRiserWindow(nil, '请输入广播内容', ico_info);
    abort;
    //raise Exception.Create('请输入广播内容');
  end;

  if (ComController <> nil) then
    TAnyQChat(ComController).fireBeforeMsg(txt);

  if txt = '' then exit;

  l := frameTreeRoster1.GetCheckedList;
  if l.Count = 0 then
  begin
    L.free;
    ShowRiserWindow(nil, '请选择广播对象', ico_info);
    abort;
    //Raise Exception.Create('请选择广播对象');
  end;

  SendMsg(l);
  L.free;
  Close;
end;

procedure TfrmBroadcastSMS.SendMsg(L: TList);
var
  txt: WideString;
  i: Integer;
begin
  txt := getInputText(txtMsg);
  if (txt = '') then exit;

  for i := 0 to L.Count - 1 do
  begin
    frmAnyQ.sendsms(TJabberRosterItem(L.Items[i]).jid.jid, txt);
  end;
end;

function TfrmBroadcastSMS.GetThread(jid: widestring): string;
var
  seed: string;
begin
  if _thread <> '' then exit;

  seed := FormatDateTime('MMDDYYYYHHMM', now);
  seed := seed + jid + MainSession.Username + MainSession.Server;

    // hash the seed to get the thread
  _thread := Sha1Hash(seed);
  Result := _thread;
end;

procedure TfrmBroadcastSMS.Button2Click(Sender: TObject);
begin
  Close;
end;

end.

