const
  bgCount=10;
var
  bgIndex:integer;
  IniFile: TIniFile;
  
procedure PlaySoundFile;
begin
  s:='';
  case cbbSoundList.ItemIndex of 
    1 : s:=WidgetPath+'Sound\default.wav';
    2 : s:=WidgetPath+'Sound\qqmsg.wav';
    3 : s:=WidgetPath+'Sound\youhaveemail.wav';
    4 : s:=WidgetPath+'Sound\zhangshaohan.wav';  
  end;
  if FileExists(s) then PlaySound(s);
end;

procedure CbbLoadItems;   //加载服务提供商名单
var
  i:Integer;
  PopServerlst:TStrings;
begin
  PopServerlst:= TStringList.create;
  Try
    PopServerlst.loadFromFile(WidgetPath+'PopServer.dat');
    for i:=0 to PopServerlst.count-1 do
      CbbPopServer.Items.add(PopServerlst.names[i]);
  finally
    PopServerlst.free;
  end;
end;

function GetPopAdd(ServerIndex:Integer):String;
var
  PopServerlst:TStrings;
begin
  result:='Default';
  PopServerlst:= TStringList.create;
  Try
    PopServerlst.loadFromFile(WidgetPath+'PopServer.dat');
    result:=PopServerlst.ValueFromIndex[ServerIndex];
  finally
    PopServerlst.free;
  end;
end;

procedure CbbSelected;
begin
  edtCustomPopSrv.text:= GetPopAdd(CbbPopServer.ItemIndex);
end;

procedure RefreshPreview;
begin
  xspfrm.beginupdate;
  bgIndex:=((bgIndex-1) mod bgCount)+1;
  imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
  lblIndex.caption:=inttostr(bgIndex)+'/'+inttostr(bgCount);
  xspfrm.endupdate;
end;

procedure InitSoundList;
begin
cbbSoundList.items.Add('无');
cbbSoundList.items.Add('默认');
cbbSoundList.items.Add('QQ信息提示音');
cbbSoundList.items.Add('女声英文语音提示');
cbbSoundList.items.Add('张韶涵邮件语音提示');
end;

//读取配置
procedure InitIni;
begin
  CbbLoadItems;    //初始化Combobox 
  CbbPopServer.ItemIndex:=GetSettingInt('CbbItemIndex',0);
  IniFile := TIniFile.Create(WidgetPath+'Settings.ini');
  Try
    cbbSoundList.ItemIndex:= GetSettingInt('SoundIndex',0);
    bgIndex:= GetSettingInt('bgIndex',1); 
    lblIndex.caption:=format('%d/%d',[bgIndex,bgCount]);
    imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
    edtCustomPopSrv.text:= GetSetting('PopServer','pop.163.com');
    edtInterval.text:= GetSetting('RefreshInterval', '30');
    edtUserName.Text:= GetSetting('UserName', '');
    edtPassword.Text:= GetSetting('Password', '');
  finally
    IniFile.Free;
  end;
end;

//保存配置
procedure SaveIni;
begin
  IniFile := TIniFile.Create(WidgetPath+'Settings.ini');
  Try
    SetSettingInt('SoundIndex',cbbSoundList.ItemIndex);
    SetSettingInt('bgIndex',bgIndex);
    SetSetting('SoundType', cbbSoundList.text);
    SetSetting('RefreshInterval', edtInterval.text);
    SetSetting('PopServer', edtCustomPopSrv.text);
    SetSetting('UserName', edtUserName.Text);
    SetSetting('Password', edtPassword.Text);
    SetSettingInt('CbbItemIndex',CbbPopServer.ItemIndex);
  finally
    IniFile.Free; 
  end; 
end;

//--- Widget设置窗口初始化事件，当打开Widget设置窗口时会触发此事件 ---
procedure OnSettingInit;
begin
  Beginupdate;
  CbbPopServer.OnSelect:='CbbSelected';
  InitSoundList;
  InitIni;
  Endupdate;
end;

//--- 按下设置窗口的确定按钮时，触发此事件 ---
//通过设置返回值来改变是否允许确认,当result为False时不关闭设置窗口
function OnSettingClose(iResult);
var
  i:integer;
begin
  result:=true;
  try
    i:=StrToInt(edtInterval.text);
    if i<=0 then raise('');
  except
    showmsg('无效的刷新间隔时间！');
    //edtInterval.SetFocus;
    result:=false;
  end;
  if Result then
    SaveIni;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  //
end;
