{**************************
Dashboard Clock OnSetting script Ver1.0 by snowfox
**************************}
var
  i: Integer;
  
  ts1:TTabSheet;
  //Tab1
  lblList,lblTitle,lblUrl,lblRows,lblRefresh:TLabel;
  cbbList:TComboBox;
  edtTitle,edtUrl:TEdit;
  seRows,seRefresh:TSpinEdit;
  lstUrl:TStrings;
  
  
procedure cbbListChange(Sender: TObject);
var
	i:integer;
begin
  i:=cbbList.itemindex;
  if (i>=0) and (i<cbbList.items.count) then
  begin
  	edtTitle.text:=cbbList.text;
  	edtUrl.text:=lstUrl[i];
  end;
end;

procedure edtUrlChange(Sender: TObject);
var
	i:integer;
begin
  edtTitle.text:='';
  if lstUrl.count>0 then
  begin
    for i:=0 to lstUrl.count-1 do
      if lstUrl[i]=edtUrl.text then
      begin
  			edtTitle.text:=cbbList.items[i];
        break;
      end;
  end;
end;

//Init Skin form setting
procedure InitSetting;
var
	inifn:TIniFile;
	lst:TStrings;
	i:integer;
begin
  inifn:=TIniFile.create(SkinFileName);
	lst:=TStringList.create;
  with inifn do
  try
    edtTitle.text:=ReadString('system','RssTitle','卡卡论坛_皮肤资源下载');
    edtUrl.text:=ReadString('system','RssLink','http://bbs.mycaca.com/rss.php?fid=46');
    cbbList.Items.add('<自定义>');
    lstUrl.add('');
    if FileExists(g_ResPath+'RSS_mycaca.ini') then
    begin
    	lst.loadfromfile(g_ResPath+'RSS_mycaca.ini');
    	if lst.count>1 then
    	  for i:=0 to lst.count-1 do
    	  begin
    	    cbbList.Items.add(lst.names[i]);
    	    lstUrl.add(lst.values[lst.Names[i]]);
    	  end;
    	cbbList.ItemIndex:=ReadInteger('Item0','RSSIndex',1);
    end;
    edtUrl.OnChange:=@edtUrlChange;
    cbbList.OnChange:=@cbbListChange;

    seRefresh.value:=ReadInteger('system','RSSRefreshInterval',60);
    seRows.value:=ReadInteger('Item0','MaxRows',15);
  finally
    inifn.free;
    lst.free;
  end;
end;

//save Skin form setting
function SaveSetting:boolean;
var
	s:string;
	inifn:TIniFile;
	i:integer;
	bRefresh:boolean;
begin
  bRefresh:=false;
  result:=true;
  inifn:=TIniFile.create(SkinFileName);
  with inifn do
  try
    WriteInteger('Item0','RSSIndex',cbbList.ItemIndex);
    WriteString('system','RSSTitle',edtTitle.text);
    if edtUrl.text<>ReadString('system','RssLink','') then
    begin
    	WriteString('system','RSSLink',edtUrl.text);
   		bRefresh:=true;
   	end;
    
    WriteInteger('system','RSSRefreshInterval',seRefresh.value);
    WriteInteger('Item0','MaxRows',seRows.value);
  finally
    inifn.free;
  end;
  if bRefresh then RefreshRSS;
end;

procedure OnClose;
begin
 if lstUrl<>nil then lstUrl.free;
end;

//--- Create ---
procedure CreateTab1;
begin
  ts1:=TTabSheet.Create(pgc);
  ts1.PageControl:=pgc;
  ts1.Caption:='RSS设置';
  
  lblList:=TLabel.create(self);
  with lblList do
  begin
    parent:=ts1;
    left:=16; top:=16; width:=78;
    caption:='默认RSS链接：';
  end;
  cbbList:=TComboBox.create(self);
  with cbbList do
  begin
    parent:=ts1;
    left:=16; top:=32; width:=321;
    DropDownCount:=15;
    Style:=csDropDownList;
  end;
  
  lblTitle:=TLabel.create(self);
  with lblTitle do
  begin
    parent:=ts1;
    left:=16; top:=64; width:=54;
    caption:='RSS标题：';
  end;
  edtTitle:=TEdit.create(self);
  with edtTitle do
  begin
    parent:=ts1;
    left:=80; top:=60; width:=257;
  end;
  lblUrl:=TLabel.create(self);
  with lblUrl do
  begin
    parent:=ts1;
    left:=16; top:=88; width:=54;
    caption:='RSS地址：';
  end;
  edtUrl:=TEdit.create(self);
  with edtUrl do
  begin
    parent:=ts1;
    left:=80; top:=84; width:=257;
  end;
  //Refresh interval
  lblRefresh:=TLabel.create(self);
  with lblRefresh do
  begin
    parent:=ts1;
    left:=16; top:=116; width:=150;
    caption:='每           分钟刷新一次';
  end;
  seRefresh:=TSpinEdit.create(self);
  with seRefresh do
  begin
    parent:=ts1;
    left:=40; top:=112; width:=49;
  end;
  //Rows
  lblRows:=TLabel.create(self);
  with lblRows do
  begin
    parent:=ts1;
    left:=16; top:=140; width:=60;
    caption:='显示行数：';
  end;
  seRows:=TSpinEdit.create(self);
  with seRows do
  begin
    parent:=ts1;
    left:=80; top:=136; width:=73;
  end; 
end;


begin
  try
  	CreateTab1;
  	lstUrl:=TStringList.create;
  	InitSetting;
  except
    showmessage('except');
  end;
end.