const
  //DefURL='http://8-jie.cn/backend.asp?Action=Widget';
  DefURL='http://bbs.8-jie.cn/rss.php';
  mrOK=1;
  mrNone=0;
  mrCancel=2;

  MB_OK=0;
  MB_YESNO=$4;
  MB_ICONWARNING=$30;
  MB_DEFBUTTON2=$100;
  IDOK=1;
  IDYES=6;
  
var
  RSSListFileName:string;

  frm:TForm;
  cbb:TComboBox;
  edt,edt2:TEdit;
  
function GetUrl(aSec,aIdent:string):string;
var
  inifn:TIniFile;
begin
  inifn:=TIniFile.Create(RSSListFileName);
  try
    result:=inifn.ReadString(aSec,aIdent,'');
  finally
    inifn.free;
  end;
end;

procedure OnItemsSelected(sender);
begin
  edtUrl.text:=GetUrl(cbbTypes.text,cbbItems.text);
end;

procedure OnTypesSelected(sender);
var
  inifn:TIniFile;
begin
  if FileExists(RSSListFileName) then
  begin
  	inifn:=TIniFile.Create(RSSListFileName);
	  try
      inifn.ReadSection(cbbTypes.text,cbbItems.items);
      cbbItems.ItemIndex:=0;
      if cbbItems.items.count>0 then
        cbbItems.text:=cbbItems.items.strings[0];
      OnItemsSelected(sender);  
    finally
  	  inifn.free;
  	end;
  end;
end;

//读取配置
procedure InitSetting;
var
  inifn:TIniFile;
  s:string;
begin
  inifn:=TIniFile.Create(RSSListFileName);
  try
    inifn.ReadSections(cbbTypes.Items);
    cbbTypes.ItemIndex:=GetSettingInt('TypeIndex',0);
    s:=GetSetting('Type','');
    if (s='') or (cbbTypes.Items.IndexOf(s)<0) then
      s:='八戒软件';
    inifn.ReadSection(s,cbbItems.items);
    cbbItems.ItemIndex:=GetSettingInt('ItemIndex',1);
  finally
    inifn.free;
  end;
  edtUrl.text:=GetSetting('Url',DefURL);
  edtInterval.text:=GetSetting('RefreshInterval','10');
  edtEveryPageCount.text:=GetSetting('EveryPageCount','5');
  chkShowTitleHint.checked:=GetSettingInt('ShowTitleHint',0)=1;
end;

//保存配置
procedure SaveSetting;
begin
  SetSetting('TypeIndex',cbbTypes.ItemIndex);
  SetSetting('Type',cbbTypes.text);
  SetSetting('ItemIndex',cbbItems.ItemIndex);
  SetSetting('Item',cbbItems.text);
  SetSetting('Url',edtUrl.text);
  SetSetting('RefreshInterval',edtInterval.text);
  SetSetting('EveryPageCount',edtEveryPageCount.text);
  SetSettingInt('ShowTitleHint',chkShowTitleHint.checked);
end;

//--- Widget设置窗口初始化事件，当打开Widget设置窗口时会触发此事件 ---
procedure OnSettingInit;
begin
  RSSListFileName:=AppPath+'Data\rsslist.dat';
  if not FileExists(RSSListFileName) then
    CopyFileEx(AppPath+'res\rsslist.dat',RSSListFileName);
  //RSSListFileName:=AppPath+'res\rsslist.dat';
  Beginupdate;
  InitSetting;
  Endupdate;
  cbbTypes.OnSelect:='OnTypesSelected';
  cbbItems.OnSelect:='OnItemsSelected';
  btnAddTypes.OnClick:='btnAddTypesClick';
  btnDelTypes.OnClick:='btnDelTypesClick';
  btnAdd.OnClick:='btnAddClick';
  btnDel.OnClick:='btnDelClick';
end;

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

//打开RSS分类的窗口
procedure btnAddTypesClick(sender);
begin
  frm:=TForm.create(WidgetSetting)
  frm.width:=230;
  frm.height:=130;
  frm.Position:=poScreenCenter;
  frm.caption:='添加RSS分类';
  if screen.fonts.indexof('微软雅黑')>=0 then
    frm.font.name:='微软雅黑'
  else
    frm.font.name:='宋体';
  frm.font.size:=9;
  frm.BorderStyle:=bsDialog;
  frm.FormStyle:=fsStayOnTop;
  try
    lbl:=TLabel.create(frm);
    lbl.parent:=frm;
    lbl.Caption:='请输入RSS分类名称：';
    lbl.left:=16; lbl.top:=12;
    
    edt:=TEdit.create(frm);
    edt.parent:=frm;
    edt.left:=16; edt.top:=34;
    edt.width:=180;
    edt.TabOrder:=0;
    
    btnCancel:=TButton.create(frm);
    btnCancel.caption:='取  消';
    btnCancel.left:=130;
    btnCancel.top:=70;
    btnCancel.parent:=frm;
    btnCancel.ModalResult:=2;
    btnCancel.cancel:=true;
    
    btnOK:=TButton.create(frm);
    btnOK.caption:='确  定';
    btnOK.left:=50;
    btnOK.top:=70;
    btnOK.parent:=frm;
    btnOK.ModalResult:=1;
    btnOk.OnClick:='btnAddTypesOkClick';
    
    if frm.ShowModal=mrOK then
    begin
      //showmsg(edt.text);
    end;
  finally
    frm.free;
  end;
end;

//添加RSS分类窗口的确定按钮事件
procedure btnAddTypesOkClick(sender);
begin
  if (frm<>nil) and (edt<>nil) then
  begin
    if trim(edt.text)='' then
    begin
      showtrayhint('','请输入RSS分类名称！');
      frm.ModalResult:=mrNone;
    end
    else if cbbTypes.items.IndexOf(edt.text)>=0 then
    begin
      showtrayhint('','此RSS分类已存在！');
      frm.ModalResult:=mrNone;
    end
    else
    begin
      cbbTypes.items.add(edt.text);
      lst:=TStringList.create;
      try
        lst.loadfromfile(RSSListFileName);
        lst.add(format('[%s]',[edt.text]));
        lst.savetofile(RSSListFileName);
      finally
        lst.free;
      end;
    end;
  end;
end;

//删除RSS分类按钮事件
procedure btnDelTypesClick(sender);
begin
  if cbbItems.items.count>0 then
    if messagebox(WidgetSetting.handle,'此分类下有多个链接，确认要删除这些链接？','警告',MB_YESNO+MB_ICONWARNING+MB_DEFBUTTON2)<>IDYES then exit;
  
  ini:=TIniFile.create(RSSListFileName);
  try
    ini.EraseSection(cbbTypes.text);
    if (cbbTypes.ItemIndex>=0) and (cbbTypes.ItemIndex<cbbTypes.items.count) then
    begin
      cbbTypes.items.delete(cbbTypes.ItemIndex);
      if cbbTypes.items.count>0 then
      begin
        cbbTypes.ItemIndex:=0;
        cbbTypes.text:=cbbTypes.Items.strings[0];
      end
      else
      begin
        cbbTypes.ItemIndex:=-1;
        cbbTypes.text:='';
      end;  
    end;
  finally
    ini.free;
  end;
end;

//打开添加RSS窗口
procedure btnAddClick(sender);
begin
  frm:=TForm.create(WidgetSetting)
  frm.width:=270;
  frm.height:=220;
  frm.Position:=poScreenCenter;
  frm.caption:='添加RSS';
  if screen.fonts.indexof('微软雅黑')>=0 then
    frm.font.name:='微软雅黑'
  else
    frm.font.name:='宋体';
  frm.font.size:=9;
  frm.BorderStyle:=bsDialog;
  frm.FormStyle:=fsStayOnTop;
  try
    lbl:=TLabel.create(frm);
    lbl.parent:=frm;
    lbl.Caption:='RSS分类：';
    lbl.left:=16; lbl.top:=14;
    
    cbb:=TComboBox.create(frm);
    cbb.parent:=frm;
    cbb.Items.text:=cbbTypes.items.text;
    cbb.left:=16; cbb.top:=32; cbb.width:=230;
    //if cbb.items.count>0 then cbb.itemindex:=0;
    cbb.itemindex:=cbbTypes.itemindex;
    cbb.TabOrder:=2;
    cbb.style:=csDropDownList;
    
    lbl1:=TLabel.create(frm);
    lbl1.parent:=frm;
    lbl1.Caption:='请输入RSS名称：';
    lbl1.left:=16; lbl1.top:=62;
    
    edt:=TEdit.create(frm);
    edt.parent:=frm;
    edt.left:=16; edt.top:=80; edt.width:=230;
    edt.TabOrder:=0;
    
    lbl2:=TLabel.create(frm);
    lbl2.parent:=frm;
    lbl2.Caption:='请输入RSS地址：';
    lbl2.left:=16; lbl2.top:=110;
    
    edt2:=TEdit.create(frm);
    edt2.parent:=frm;
    edt2.left:=16; edt2.top:=128; edt2.width:=230;
    edt2.TabOrder:=1;
    
    btnOK:=TButton.create(frm);
    btnOK.caption:='确  定';
    btnOK.left:=90;
    btnOK.top:=162;
    btnOK.parent:=frm;
    btnOk.OnClick:='btnAddOkClick';
    
    btnCancel:=TButton.create(frm);
    btnCancel.caption:='取  消';
    btnCancel.left:=170;
    btnCancel.top:=162;
    btnCancel.parent:=frm;
    btnCancel.ModalResult:=mrCancel;
    btnCancel.cancel:=true;
    
    if frm.ShowModal=1 then
    begin
      //showmsg(edt.text);
      if cbb.text=cbbTypes.text then
      begin
        i:=cbbItems.items.add(edt.text);
        cbbItems.text:=edt.text;
        cbbItems.ItemIndex:=i;
        edtUrl.text:=edt2.text;
      end;
    end;
  finally
    frm.free;
  end;
end;

//添加RSS窗口的确认按钮事件
procedure btnAddOkClick(sender);
begin
  if (frm<>nil) and (edt<>nil) then
  begin
    if trim(cbb.text)='' then
    begin
      //showtrayhint('','请选择RSS分类！');
      messagebox(frm.handle,'请选择RSS分类！','',MB_OK);
      cbb.SetFocus;
      frm.ModalResult:=mrNone;
    end
    else if trim(edt.text)='' then
    begin
      //showtrayhint('','请输入RSS名称！');
      messagebox(frm.handle,'请输入RSS名称！','',MB_OK);
      edt.SetFocus;
      frm.ModalResult:=mrNone;
    end
    else if trim(edt2.text)='' then
    begin
      //showtrayhint('','请输入RSS地址！');
      messagebox(frm.handle,'请输入RSS地址！','',MB_OK);
      edt2.SetFocus;
      frm.ModalResult:=mrNone;
    end
    else
    begin
      ini:=TIniFile.create(RSSListFileName);
      try
        if ini.ReadString(cbb.text,edt.text,'')<>'' then
        begin
          //showmsg('重复的RSS名称！');
      		messagebox(frm.handle,'重复的RSS名称！','',MB_OK);
          edt.setfocus;
        end
        else
        begin
          ini.WriteString(cbb.text,edt.text,edt2.text);
          frm.ModalResult:=mrOK;
        end;
      finally
        ini.free;
      end;
    end;
  end;
end;

//删除RSS按钮事件
procedure btnDelClick(sender);
begin
  ini:=TIniFile.create(RSSListFileName);
  try
    ini.DeleteKey(cbbTypes.text,cbbItems.text);
    if (cbbItems.ItemIndex>=0) and (cbbItems.ItemIndex<cbbItems.items.count) then
    begin
      cbbItems.items.delete(cbbItems.ItemIndex);
      if cbbItems.items.count>0 then
      begin
        cbbItems.ItemIndex:=0;
        cbbItems.text:=cbbItems.Items.strings[0];
        OnItemsSelected(cbbItems);
      end
      else
      begin
        cbbItems.ItemIndex:=-1;
        cbbItems.text:='';
      end;  
    end;
  finally
    ini.free;
  end;
end;

