const
  bgCount=80;

var
  tmr,stmr:TTimer;
  aSoundType:string;
  bgIndex, SoundIndex, mCount:integer;
  RefreshInterval, UserName, Password, PopServer:string;
  pop:TxsPOP3Headers;
  iLastMailCount:Integer;
  iLastNewMailCount:Integer;
  xshttp:TxsHTTP;
  RSSFileName:string;
  aurl:string;

procedure PlaySoundFile;
begin
 if (SoundIndex <> 0) and (mCount >= 1) then
  case SoundIndex of 
   0 : PlaySound('');
   1 : PlaySound(WidgetPath+'Sound\default.wav');
   2 : PlaySound(WidgetPath+'Sound\qqmsg.wav');
   3 : PlaySound(WidgetPath+'Sound\youhaveemail.wav');
   4 : PlaySound(WidgetPath+'Sound\zhangshaohan.wav');
  end;
end;
function GetMailCount(aFileName:string):Integer;  //获取邮件数量
var
  iAllMailCount:integer;
begin
  iMailCount:=0;
  xmld := CreateOleObject('microsoft.xmldom');
  if xmld.Load(aFileName) then
  begin
    Try
      nodel := xmld.DocumentElement.ChildNodes;
      nodeMailBoxInfo:=nodel.item(2);
      iAllMailCount:=StrToInt(nodeMailBoxInfo.text);
   { if (iMailCount=0) then
      ShowTrayHint('提示','暂无新邮件');}
    result:= iAllMailCount;
    except
      ShowTrayHint('提示','帐户信息错误或获取错误!');
      result:=0;
    end;
  end;
  xmld := unassigned;  
end;

procedure HttpGetError(url, isString);
begin
  if aurl=url then
  begin
    showtrayhint('提示','读取邮件信息失败！');
  end;
  if xshttp<>nil then
  begin
  	xshttp.free;
  	xshttp:=nil;
  end;
end;

procedure HttpDone(url,result,isString);
var
  lst:TStrings;
  iGMailCount:Integer;
begin
  if aurl=url then
  begin
  	lst:=TStringList.create;
  	try
  		lst.text:=result;
  		lst.savetofile(RSSFileName);   //保存XML文件
      iGMailCount:=GetMailCount(RSSFileName);
      if IGmailCount=0 then 
        lblNewMailCount.Caption:='暂无新邮件' else
      if iLastNewMailCount<>iGmailCount then
      begin
        lblNewMailCount.Caption:='新邮件数：'+IntToStr(iGMailCount)+'封';
        PlaySoundFile;
        iLastNewMailCount:=iGmailCount;
      end;
    except
    end;
  end;  
end;
      
procedure GetGmailInfo(Var name,pass:string);  //获取GMail信息
begin
  aUrl:='http://'+name+':'+pass+'@gmail.google.com/mail/feed/atom';
  xshttp:=TxsHTTP.Create(nil);
  xshttp.OnHTTPGetFailed:='HttpGetError';
  xshttp.OnHttpGetDone:='HttpDone';
  xshttp.GetHTTP(aurl,true,'AllowRedirects=1;');
end;

//刷新邮件信息
procedure RefreshMailInfo;
begin
//=========================================================================
  lblUserName.caption:=UserName;
  if LowerCase(Trim(popServer))='pop.gmail.com' then
    GetGmailInfo(UserName,Password) else
  begin
    if pop<>nil then exit;
    pop:=TxsPOP3Headers.Create(widget);
    pop.OnPOP3HeadersGetDone:='GetDone';
    pop.OnPOP3HeadersGetFailed:='GetError';
    //GetPOP3Headers函数的参数分别为：POP3服务器、用户名、密码、端口
    pop.GetPOP3Headers(PopServer,UserName,Password,110);
  end;
end;

procedure GetDone(iCount: Integer);
var
  iNewMailCount:Integer;
begin
  mCount:=iCount;
  iNewMailCount:=mCount-iLastMailCount;
  POP.free;
  pop:=nil;
  lblAllMailCount.Caption:='总邮件数：'+ IntToStr(mCount) +'封';
  if iNewMailCount>0 then
  begin
    lblNewMailCount.Caption:='新邮件数：'+IntToStr(iNewMailCount)+'封';
    if iLastNewMailCount<>iNewMailCount then
      PlaySoundFile;
  end else
    lblNewMailCount.Caption:= '暂无新邮件';
  iLastNewMailCount:=iNewMailCount;
end;

procedure GetError;
begin
  POP.free;
  pop:=nil;
  showTrayHint('提示','请检查帐户信息是否正确');
  lblAllMailCount.Caption:='读取邮件信息失败';
end;

//读取当前Widget的INI设置
procedure InitIni;
var
  s:string;
  IniFile:TIniFile;
begin
  IniFile := TIniFile.Create(WidgetPath+'Settings.ini');
  try
    aSoundType:= GetSetting('SoundType', '默认');
    SoundIndex:= GetSettingInt('SoundIndex',0);
    UserName:= GetSetting('UserName', '');
    RefreshInterval:= GetSetting('RefreshInterval', '30');
    PopServer:= GetSetting('PopServer', '');
    Password:= GetSetting('Password', '');
    bgIndex:= GetSettingInt('bgIndex',1); 
  finally
    IniFile.Free;
  end;
  if (bgIndex<=0) or (bgIndex>bgCount) then bgIndex:=1;
	imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
  //Showmsg(PopServer);
end;

procedure OnWidgetInit;
begin
  pop:=nil;
  BeginUpdate;
  InitIni;
  mCount:=5; //初始化未读邮件数量
  RSSFileName:=AppPath+'Data\'+WidgetName+'_RSS.xml';
  iLastMailCount:=GetSettingInt('mCount',0);
  iLastNewMailCount:=0;
  if (UserName='') or (PopServer='') then
  begin
    lblNewMailCount.Caption:='请先设置邮箱信息';
    imgReget.visible:=False;
  end else
  RefreshMailInfo;
  EndUpdate;
  tmr:=TTimer.Create(Widget);
  tmr.OnTimer:='OntmrTimer';
  tmr.Interval:=1000*60*StrToInt(RefreshInterval);
  tmr.Enabled:=true;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  tmr.free;
  SetSettingInt('mCount',mCount);
 if pop<>nil then pop.free;
end;

procedure OntmrTimer(sender)
begin
  RefreshMailInfo;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  Widget.Width:=134;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  Widget.Width:=142;
end;


