home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d56 / MPEGPLUS.ZIP / ID3v1 / Main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-08-02  |  4.4 KB  |  163 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, FileCtrl, ExtCtrls, ID3v1;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     DriveList: TDriveComboBox;
  12.     FolderList: TDirectoryListBox;
  13.     FileList: TFileListBox;
  14.     CloseButton: TButton;
  15.     RemoveButton: TButton;
  16.     SaveButton: TButton;
  17.     InfoBevel: TBevel;
  18.     IconImage: TImage;
  19.     TagExistsLabel: TLabel;
  20.     TagVersionLabel: TLabel;
  21.     TitleLabel: TLabel;
  22.     ArtistLabel: TLabel;
  23.     AlbumLabel: TLabel;
  24.     YearLabel: TLabel;
  25.     CommentLabel: TLabel;
  26.     TrackLabel: TLabel;
  27.     GenreLabel: TLabel;
  28.     TitleEdit: TEdit;
  29.     ArtistEdit: TEdit;
  30.     AlbumEdit: TEdit;
  31.     TrackEdit: TEdit;
  32.     YearEdit: TEdit;
  33.     CommentEdit: TEdit;
  34.     GenreComboBox: TComboBox;
  35.     TagExistsValue: TEdit;
  36.     TagVersionValue: TEdit;
  37.     procedure CloseButtonClick(Sender: TObject);
  38.     procedure FormCreate(Sender: TObject);
  39.     procedure FileListChange(Sender: TObject);
  40.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  41.     procedure SaveButtonClick(Sender: TObject);
  42.     procedure RemoveButtonClick(Sender: TObject);
  43.   private
  44.     { Private declarations }
  45.     FileTag: TID3v1;
  46.     procedure ClearAll;
  47.   end;
  48.  
  49. var
  50.   MainForm: TMainForm;
  51.  
  52. implementation
  53.  
  54. {$R *.dfm}
  55.  
  56. procedure TMainForm.ClearAll;
  57. begin
  58.   { Clear all captions }
  59.   TagExistsValue.Text := '';
  60.   TagVersionValue.Text := '';
  61.   TitleEdit.Text := '';
  62.   ArtistEdit.Text := '';
  63.   AlbumEdit.Text := '';
  64.   TrackEdit.Text := '';
  65.   YearEdit.Text := '';
  66.   GenreComboBox.ItemIndex := 0;
  67.   CommentEdit.Text := '';
  68. end;
  69.  
  70. procedure TMainForm.CloseButtonClick(Sender: TObject);
  71. begin
  72.   { Exit }
  73.   Close;
  74. end;
  75.  
  76. procedure TMainForm.FormCreate(Sender: TObject);
  77. var
  78.   Iterator: Integer;
  79. begin
  80.   { Create object }
  81.   FileTag := TID3v1.Create;
  82.   { Fill and initialize genres }
  83.   GenreComboBox.Items.Add('');
  84.   for Iterator := 0 to MAX_MUSIC_GENRES - 1 do
  85.     GenreComboBox.Items.Add(MusicGenre[Iterator]);
  86.   { Reset }
  87.   ClearAll;
  88. end;
  89.  
  90. procedure TMainForm.FileListChange(Sender: TObject);
  91. begin
  92.   { Clear captions }
  93.   ClearAll;
  94.   if FileList.FileName = '' then exit;
  95.   if FileExists(FileList.FileName) then
  96.     { Load tag data }
  97.     if FileTag.ReadFromFile(FileList.FileName) then
  98.       if FileTag.Exists then
  99.       begin
  100.         { Fill captions }
  101.         TagExistsValue.Text := 'Yes';
  102.         if FileTag.VersionID = TAG_VERSION_1_0 then
  103.           TagVersionValue.Text := '1.0'
  104.         else
  105.           TagVersionValue.Text := '1.1';
  106.         TitleEdit.Text := FileTag.Title;
  107.         ArtistEdit.Text := FileTag.Artist;
  108.         AlbumEdit.Text := FileTag.Album;
  109.         TrackEdit.Text := IntToStr(FileTag.Track);
  110.         YearEdit.Text := FileTag.Year;
  111.         if FileTag.GenreID < MAX_MUSIC_GENRES then
  112.           GenreComboBox.ItemIndex := FileTag.GenreID + 1;
  113.         CommentEdit.Text := FileTag.Comment;
  114.       end
  115.       else
  116.         { Tag not found }
  117.         TagExistsValue.Text := 'No'
  118.     else
  119.       { Read error }
  120.       ShowMessage('Can not read tag from the file: ' + FileList.FileName)
  121.   else
  122.     { File does not exist }
  123.     ShowMessage('The file does not exist: ' + FileList.FileName);
  124. end;
  125.  
  126. procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
  127. begin
  128.   { Free memory }
  129.   FileTag.Free;
  130. end;
  131.  
  132. procedure TMainForm.SaveButtonClick(Sender: TObject);
  133. var
  134.   Value, Code: Integer;
  135. begin
  136.   { Prepare tag data }
  137.   FileTag.Title := TitleEdit.Text;
  138.   FileTag.Artist := ArtistEdit.Text;
  139.   FileTag.Album := AlbumEdit.Text;
  140.   FileTag.Year := YearEdit.Text;
  141.   Val(TrackEdit.Text, Value, Code);
  142.   if (Code = 0) and (Value > 0) then FileTag.Track := Value
  143.   else FileTag.Track := 0;
  144.   if GenreComboBox.ItemIndex = 0 then FileTag.GenreID := DEFAULT_GENRE
  145.   else FileTag.GenreID := GenreComboBox.ItemIndex - 1;
  146.   FileTag.Comment := CommentEdit.Text;
  147.   { Save tag data }
  148.   if (not FileExists(FileList.FileName)) or
  149.     (not FileTag.SaveToFile(FileList.FileName)) then
  150.     ShowMessage('Can not save tag to the file: ' + FileList.FileName);
  151.   FileListChange(Self);
  152. end;
  153.  
  154. procedure TMainForm.RemoveButtonClick(Sender: TObject);
  155. begin
  156.   { Delete tag data }
  157.   if (FileExists(FileList.FileName)) and
  158.     (FileTag.RemoveFromFile(FileList.FileName)) then ClearAll
  159.   else ShowMessage('Can not remove tag from the file: ' + FileList.FileName);
  160. end;
  161.  
  162. end.
  163.