{
********************************************************************************

SPackGui
Copyright (C) 2013 Geoffray Levasseur <geoffray.levasseurbrandin@numericable.fr>.
Copyright (C) <date> <add your name and mail address here>

http://www.geoffray-levasseur.org/
http://0.tuxfamilly.org/

This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software.  You can  use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability.

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and,  more generally, to use and operate it in the
same conditions as regards security.

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.

********************************************************************************

Description:
  configuration dialog for repositories

********************************************************************************
}
unit frmRepoSettings;

{$include ../common/defines.inc}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  ComCtrls, StdCtrls;

type

  { TfRepoSettings }

  TfRepoSettings = class(TForm)
    btnChange: TBitBtn;
    btnDelete: TBitBtn;
    btnAdd: TBitBtn;
    btnCancel: TBitBtn;
    btnOk: TBitBtn;
    cbDefaultRepo: TComboBox;
    lbDefaultRepo: TLabel;
    lbList: TLabel;
    lvRepoList: TListView;
    procedure btnAddClick(Sender: TObject);
    procedure btnChangeClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure cbDefaultRepoChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure lvRepoListClick(Sender: TObject);
  private
    { private declarations }
    FChange: Boolean;
    procedure PopulateDefaultList;
  public
    { public declarations }
    RepoChanged: Boolean;
  end;

var
  fRepoSettings: TfRepoSettings;

implementation

uses
  frmAddChangeRepo, uStrings, uCommon, uIconManager, uDebug;

{$R *.lfm}

{ TfRepoSettings }

procedure TfRepoSettings.PopulateDefaultList;
var
  I, N: Integer;
begin
  cbDefaultRepo.Items.Clear;
  for I := 0 to lvRepoList.Items.Count - 1 do
  begin
    N := cbDefaultRepo.Items.Add(lvRepoList.Items[I].Caption);
    if cbDefaultRepo.Items[N] = sDefaultRepo then
      cbDefaultRepo.Caption := cbDefaultRepo.Items[N];
  end;
end;

procedure TfRepoSettings.btnAddClick(Sender: TObject);
var
  LI: TListItem;
  I: Integer;
begin
  with TfAddChangeRepo.Create(Self) do
  try
    Caption := rsNewRepo;
    edName.Text := '';
    edAddress.Text := '';
    edComment.Text := '';
    if ShowModal = mrOK then
    begin
      for I := 0 to lvRepoList.Items.Count - 1 do
        if lvRepoList.Items[I].Caption = Trim(edName.Text) then
          MessageDlg(rsError, rsErrorDuplicateRepoName, mtError, [mbOK], 0);
      LI := lvRepoList.Items.Add;
      LI.Caption := Trim(edName.Text);
      LI.SubItems.Add(Trim(edAddress.Text));
      LI.SubItems.Add(Trim(edComment.Text));
      FChange := True;
    end;
  finally
    Free;
    PopulateDefaultList;
  end;
end;

procedure TfRepoSettings.btnChangeClick(Sender: TObject);
begin
  if lvRepoList.SelCount <> 1 then
    Exit;
  with TfAddChangeRepo.Create(Self) do
  try
    Caption := rsChangeRepo;
    edName.Text := lvRepoList.Selected.Caption;
    edAddress.Text := lvRepoList.Selected.SubItems[0];
    edComment.Text := iniRepo.ReadString(lvRepoList.Selected.Caption,
      rsConfNameRepoDesc, '');
    if ShowModal = mrOK then
    begin
      lvRepoList.Selected.Caption := edName.Text;
      lvRepoList.Selected.SubItems[0] := edAddress.Text;
      lvRepoList.Selected.SubItems[1] := edComment.Text;
      FChange := True;
    end;
  finally
    Free;
    PopulateDefaultList;
  end;
end;

procedure TfRepoSettings.btnDeleteClick(Sender: TObject);
begin
  if lvRepoList.SelCount = 1 then
    lvRepoList.Selected.Delete;
  lvRepoListClick(Sender);
  FChange := True;
end;

procedure TfRepoSettings.btnOkClick(Sender: TObject);
var
  I: Integer;
begin
  slRepoList.Clear;
  for I := 0 to lvRepoList.Items.Count - 1 do
    slRepoList.Add(lvRepoList.Items[I].Caption + '='  +
      lvRepoList.Items[I].SubItems[0]);
  sDefaultRepo := cbDefaultRepo.Caption;
  SaveRepoSettings;
  for I := 0 to lvRepoList.Items.Count - 1 do
    iniRepo.WriteString(lvRepoList.Items[I].Caption, rsConfNameRepoDesc,
      lvRepoList.Items[I].SubItems[1]);
  RepoChanged := FChange;
  FChange := False;
end;

procedure TfRepoSettings.cbDefaultRepoChange(Sender: TObject);
begin
  FChange := True;
end;

procedure TfRepoSettings.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  try
    SaveWindowGeometry(Self);
  except
    PrintLnDbg(rsErrorCannotSaveConf, vlLow);
  end;
end;

procedure TfRepoSettings.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  CanClose := not FChange;
  if FChange then
    CanClose := MessageDlg(rsWarning, rsLooseRepoChanges, mtWarning,
      [mbYes, mbNo], 0) = mrYes;
end;

procedure TfRepoSettings.FormCreate(Sender: TObject);
var
  I, N: Integer;
  LI: TListItem;
begin
  FChange := False;
  btnAdd.Tag := icAdd;
  btnChange.Tag := icChange;
  btnDelete.Tag := icDelete;
  btnOk.Tag := icOk;
  btnCancel.Tag := icCancel;
  LoadWindowGeometry(Self);
  InitBtnGlyphs(Self);
  LoadRepoSettings;
  for I := 0 to slRepoList.Count - 1 do
  begin
    N := Pos('=', slRepoList[I]);
    LI := lvRepoList.Items.Add;
    LI.Caption := Copy(slRepoList[I], 1, N - 1);
    LI.SubItems.Add(Copy(slRepoList[I], N + 1, Length(slRepoList[I]) - N));
    LI.SubItems.Add(iniRepo.ReadString(Copy(slRepoList[I], 1, N - 1),
      rsConfNameRepoDesc, ''));
  end;
  PopulateDefaultList;
  lvRepoListClick(nil);
end;

procedure TfRepoSettings.FormShow(Sender: TObject);
begin
  lvRepoList.SortColumn := 0;
  lvRepoList.SortDirection := sdAscending;
  lvRepoList.SortType := stText;
end;

procedure TfRepoSettings.lvRepoListClick(Sender: TObject);
begin
  btnChange.Enabled := lvRepoList.SelCount = 1;
  btnDelete.Enabled := lvRepoList.SelCount = 1;
end;

end.

