unit FeedSetup;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, StrUtils;

type
  TFeeds = Record
    Name,
    Link: String;
    NumberOfItems,
    UpdateInterval: Integer;
  end;
  TfrmFeedSetup = class(TForm)
    Memo1: TMemo;
    txtName: TEdit;
    txtLink: TEdit;
    txtInterval: TEdit;
    txtItems: TEdit;
    btnAdd: TButton;
    btnSave: TButton;
    cmbFeeds: TComboBox;
    btnRemove: TButton;
    lblFeeds: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    GroupBox1: TGroupBox;
    procedure btnSaveClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure txtNameExit(Sender: TObject);
    procedure txtLinkExit(Sender: TObject);
    procedure txtIntervalExit(Sender: TObject);
    procedure txtItemsExit(Sender: TObject);
    procedure txtLinkEnter(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    blnDirty: Boolean;
    arFeeds, arTemp: Array of TFeeds;
    procedure FeedUpdate;
    procedure LoadReg;
    procedure SaveReg;
  public
		function Execute: Boolean;
  end;

const
  RegPath = '\Software\The Coolmon Project\Gen_Feed';

var
  Form1: TfrmFeedSetup;

implementation

{$R *.dfm}

Uses
  Registry;

function TfrmFeedSetup.Execute: Boolean;
begin
	Result := (ShowModal = mrOk);
end;

procedure TfrmFeedSetup.LoadReg;
var
  Reg: TRegistry;
  i, Num: Integer;
begin
  Reg := TRegistry.Create;
  With Reg do
  try
    if OpenKey(RegPath, false) then
    begin
      Num := ReadInteger('NumberOfFeeds');
      CloseKey;
      if (Num > 0) and OpenKey(RegPath + '\Feeds', false) then
      begin
        SetLength(arFeeds, Num);
        for i := 0 to Num -1 do
        begin
          arFeeds[i].Name := ReadString('Name' + IntToStr(i));
          arFeeds[i].Link := ReadString('Link' + IntToStr(i));
          arFeeds[i].NumberOfItems := ReadInteger('NumberOfItems' + IntToStr(i));
          arFeeds[i].UpdateInterval := ReadInteger('UpdateInterval' + IntToStr(i));
        end;
        CloseKey;

        txtName.Text := 'Feed' + IntToStr(Length(arFeeds)+1);
        btnRemove.Enabled := True;
        cmbFeeds.Enabled := True;
        Memo1.Enabled := True;
      end;
    end;
  finally
    Reg.Free;
  end;

  FeedUpdate;
end;

procedure TfrmFeedSetup.SaveReg;
var
  Reg: TRegistry;
  i: Integer;
begin
  Reg := TRegistry.Create;
  With Reg do
  try
    if OpenKey(RegPath, true) then
    begin
      WriteInteger('NumberOfFeeds', Length(arFeeds));
      DeleteKey('Feeds');
      CloseKey;
      if (Length(arFeeds) > 0) and OpenKey(RegPath + '\Feeds', true) then
      begin
        for i := 0 to Length(arFeeds)-1 do
        begin
          WriteString('Name' + IntToStr(i), arFeeds[i].Name);
          WriteString('Link' + IntToStr(i), arFeeds[i].Link);
          WriteInteger('NumberOfItems' + IntToStr(i), arFeeds[i].NumberOfItems);
          WriteInteger('UpdateInterval' + IntToStr(i), arFeeds[i].UpdateInterval);
        end;
        CloseKey;
      end;
    end;
  finally
    Reg.Free;
  end;
end;

procedure TfrmFeedSetup.FeedUpdate;
var
  i: Integer;
begin
  Memo1.Lines.BeginUpdate;
  try
    Memo1.Lines.Clear;
    cmbFeeds.Items.Clear;
    for i := 0 to Length(arFeeds) -1 do
    begin
      Memo1.Lines.Add('== Feed ' + IntToStr(i+1) + ' ==');
      Memo1.Lines.Add(arFeeds[i].Name);
      Memo1.Lines.Add(arFeeds[i].Link);
      Memo1.Lines.Add(IntToStr(arFeeds[i].UpdateInterval));
      Memo1.Lines.Add(IntToStr(arFeeds[i].NumberOfItems));
      cmbFeeds.Items.Add(arFeeds[i].Name);
    end;
    cmbFeeds.ItemIndex := 0;
  finally
    Memo1.Lines.EndUpdate;
  end;
  lblFeeds.Caption := 'Number of Feeds: ' + IntToStr(Length(arFeeds));
end;         

procedure TfrmFeedSetup.btnSaveClick(Sender: TObject);
begin
  if Length(arFeeds) = 0 then
    ShowMessage('You Haven''t Setup Any Feeds');

  if blnDirty then
    SaveReg;

  blnDirty := False;
  self.Close;
end;

procedure TfrmFeedSetup.btnAddClick(Sender: TObject);
var
  i: Integer;
begin
  blnDirty := True; 
  btnSave.Caption := 'Save';

  for i := 0 to Length(arFeeds)-1 do
  begin
    if UpperCase(arFeeds[i].Name) = UpperCase(txtName.Text) then
    begin
      ShowMessage('Name Already Used');
      Exit;
    end;
  end;
  if UpperCase(AnsiLeftStr(txtLink.Text, 7)) <> 'HTTP://' then
  begin
    ShowMessage('Not a Valid Link');
    Exit;
  end;
  if StrToInt(txtInterval.Text) < 20 then
    txtInterval.Text := '20';
  if StrToInt(txtItems.Text) < 1 then
    txtItems.Text := '1'
  else if StrToInt(txtItems.Text) > 99 then
    txtItems.Text := '99';

  SetLength(arFeeds, Length(arFeeds) +1);
  i := High(arFeeds);
  arFeeds[i].Name := txtName.Text;
  arFeeds[i].Link := txtLink.Text;
  arFeeds[i].UpdateInterval := StrToInt(txtInterval.Text);
  arFeeds[i].NumberOfItems := StrToInt(txtItems.Text);

  FeedUpdate;

  txtName.Text := 'Feed' + IntToStr(Length(arFeeds) +1);
  txtLink.Text := 'Link to Feed';
  txtInterval.Text := '30';
  txtItems.Text := '5';
  btnAdd.Enabled := False;

  btnRemove.Enabled := True;
  cmbFeeds.Enabled := True;
  Memo1.Enabled := True;
end;

procedure TfrmFeedSetup.btnRemoveClick(Sender: TObject);
var
  i: Integer;
begin
  blnDirty := True;
  btnSave.Caption := 'Save';

  arTemp := Copy(arFeeds, 0, Length(arFeeds));
  SetLength(arFeeds, 0);
  for i := 0 to High(arTemp) do
  begin
    if i = cmbFeeds.ItemIndex then
      Continue;
    SetLength(arFeeds, Length(arFeeds)+1);
    arFeeds[High(arFeeds)] := arTemp[i];
  end;
  SetLength(arTemp, 0);

  FeedUpdate;

  If Length(arFeeds) = 0 then
  begin
    btnRemove.Enabled := False;
    cmbFeeds.Enabled := False;
    Memo1.Enabled := False;
    cmbFeeds.Text := 'No Feeds';
    Memo1.Lines.Add('No Feeds');
    txtName.Text := 'Feed1';
  end;
end;

procedure TfrmFeedSetup.txtNameExit(Sender: TObject);
begin
  If txtName.Text = '' then
    txtName.Text := 'Feed' + IntToStr(Length(arFeeds) +1);
end;

procedure TfrmFeedSetup.txtLinkExit(Sender: TObject);
begin
  If txtLink.Text = '' then
    txtLink.Text := 'Link to Feed';
end;

procedure TfrmFeedSetup.txtIntervalExit(Sender: TObject);
begin
  If txtInterval.Text = '' then
    txtInterval.Text := '30';
end;

procedure TfrmFeedSetup.txtItemsExit(Sender: TObject);
begin
  If txtItems.Text = '' then
    txtItems.Text := '5';
end;

procedure TfrmFeedSetup.txtLinkEnter(Sender: TObject);
begin
  btnAdd.Enabled := True;

  if txtLink.Text = 'Link to Feed' then
    txtLink.Text := 'http://';
end;

procedure TfrmFeedSetup.FormCreate(Sender: TObject);
begin
  LoadReg;
end;

procedure TfrmFeedSetup.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if blnDirty then
  begin
    Case MessageDlg('Save The Changes?', mtConfirmation, mbYesNoCancel, 0) of
      6: SaveReg;
      2: CanClose := False;
    end;
  end;
end;

end.
