unit uRenameTrack;
// (C) by Marco Schulze <marco@nightlabs.com>
// [YMD] 2001-03-07
// published under GPL (GNU Public Licence)

interface

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

type
  TFormRenameTrack = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    EditAlbumAuthor: TEdit;
    EditAlbumName: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    EditTrackAuthor: TEdit;
    EditTrackName: TEdit;
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    Label5: TLabel;
    EditTrackGenre: TEdit;
    CheckBox1: TCheckBox;
    procedure Execute(AlbumID, TrackID:Integer);
    procedure FormShow(Sender: TObject);
  private
	 { Private declarations }
  public
    { Public declarations }
  end;

var
  FormRenameTrack: TFormRenameTrack;

implementation

uses MServClients, MServCDatas;

{$R *.DFM}

procedure TFormRenameTrack.Execute(AlbumID, TrackID:Integer);
var
  AlbumIdx, TrackIdx  : Integer;
  i,TrackID2 				: Integer;
begin
  try
    AlbumIdx:=MServClient.MServCData.GetAlbumIdx(AlbumID);
    if AlbumIdx<0 then raise Exception.Create('no album cached with AlbumID='+IntToStr(AlbumID));

    if High(MServClient.MServCData.Albums[AlbumIdx].Tracks)<0 then begin
      MServClient.CacheTracks(AlbumID);
    end;

    TrackIdx:=MServClient.MServCData.Albums[AlbumIdx].GetTrackIdx(TrackID);
    if TrackIdx<0 then raise Exception.Create('no track cached with TrackID='+IntToStr(TrackID)+' in album #'+IntToStr(AlbumID));


	 EditAlbumAuthor.Text:=MServClient.MServCData.Albums[AlbumIdx].AlbumAuthor;
	 EditAlbumName.Text:=MServClient.MServCData.Albums[AlbumIdx].AlbumName;

	 EditTrackAuthor.Text:=MServClient.MServCData.Albums[AlbumIdx].Tracks[TrackIdx].TrackAuthor;
	 EditTrackName.Text:=MServClient.MServCData.Albums[AlbumIdx].Tracks[TrackIdx].TrackName;
	 EditTrackGenre.Text:=MServClient.MServCData.Albums[AlbumIdx].Tracks[TrackIdx].Genre;
    // default NOT for entire album
	 CheckBox1.Checked:=false;

	 // NOW.. let the user edit it
	 if ShowModal=mrOK then begin
		MServClient.RenameTrack(AlbumID, TrackID, EditTrackAuthor.Text, EditTrackName.Text, EditTrackGenre.Text);
		if (EditAlbumAuthor.Text<>MServClient.MServCData.Albums[AlbumIdx].AlbumAuthor) or
			  (EditAlbumName.Text<>MServClient.MServCData.Albums[AlbumIdx].AlbumName) then
			MServClient.RenameAlbum(AlbumID, EditAlbumAuthor.Text,EditAlbumName.Text);
		if CheckBox1.Checked then begin
			// apply genre to all..
			for i:=0 to High(MServClient.MServCData.Albums[AlbumIdx].Tracks) do begin
				TrackID2:=MServClient.MServCData.Albums[AlbumIdx].Tracks[i].TrackID;
				MServClient.RenameTrack(AlbumID, TrackID2, '', '', EditTrackGenre.Text);
			end; // for i
		end; // checked
	 end; // if ShowModal=mrOK then begin
  except
    on E:Exception do raise Exception.Create('TFormRenameTrack.Execute: '+E.Message);
  end;
end;

procedure TFormRenameTrack.FormShow(Sender: TObject);
begin
  EditTrackAuthor.SetFocus;
  EditTrackAuthor.SelectAll;
end;

end.
