unit fdbtPluginMain;

interface

uses Windows, SysUtils, Classes, TagEditorLibConsts, TagEditorLibAddinConsts,
  fdbtPluginUtils, fdbtPlugin, CDATrack;



const
  FDBTP_INFO: array[0..FDBT_AUTHOR] of string = (
        '1.0',
        'CDATrack',
        '.cda',
        'のらだいこ'
        );



  FDBT_FIELD_LENGTH      = 'length';
  FDBT_FIELD_POSITION    = 'position';


  FDBTP_FIELDCOUNT = 7;
  FDBTP_FIELDS: array[0..FDBTP_FIELDCOUNT-1] of TTagField = (
        (
        Name:        FDBT_FIELD_TITLE;
        DisplayName: 'タイトル';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_ARTIST;
        DisplayName: 'アーティスト';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_ALBUM;
        DisplayName: 'アルバム';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_TRACK;
        DisplayName: 'トラック';
        ValueType:   FDBT_VALUE_TYPE_INTEGER;
        Editable:    False;      Visible: True;
        Data:        nil;
        ),

        (
        Name:        FDBT_FIELD_DURATION;
        DisplayName: '演奏時間';
        ValueType:   FDBT_VALUE_TYPE_DOUBLE;
        Editable:    False;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_LENGTH;
        DisplayName: '時間';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;     Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_POSITION;
        DisplayName: '位置';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;      Visible: True;
        Data:        nil;
        )
        );


type

TFdbtPluginMain = class(TFdbtPlugin)
private
  FAddinMenu: TAddinMenu;
  FGetTrackDataInfo: TGetTrackDataInfo;
public
  TrackTools: TTrackDataInfo;
  function Initialize(info: PFdbtPluginInitInfo): integer; override;
  function GetLibInfo(InfoNo: Integer; Buffer: PChar; Size: Integer): Integer; override;
  function ShowConfig: integer; override;
  function GetTagInfo(FileName: PWideChar; Tag: PTagInfo; Size: integer): integer; override;
  function SetTagInfo(FileName: PWideChar; Tag: PTagInfo): integer; override;
  function GetTagFieldInfo(fl: PTagFieldList; size: integer): integer; override;

  function AddinMenu(place: integer; Identify: PChar; DisplayName: PChar; ProcExe: TAddinProc; ImageType: integer; Image: cardinal): integer;
  function GetTrackDataInfo: TTrackDataInfo;
end;


implementation



function TFdbtPluginMain.Initialize(info: PFdbtPluginInitInfo): integer;
begin
  inherited Initialize(info);
  @FAddinMenu:=InitInfo.GetService('AddinMenu');
  @FGetTrackDataInfo:=InitInfo.GetService('GetTrackDataInfo');

  TrackTools:=GetTrackDataInfo();

  Result:=FDBT_SUCCESS;
end;


function TFdbtPluginMain.GetLibInfo(InfoNo: Integer; Buffer: PChar; Size: Integer): Integer;
var
  PlugInfo : string;
begin
  PlugInfo:=FDBTP_INFO[InfoNo];
  lstrcpyn(Buffer, PChar(PlugInfo), Size);
  Result:=Length(Buffer);
end;

function TFdbtPluginMain.ShowConfig: integer;
begin
  Result:=FDBT_NOTIMPLEMENTED;
end;

function TFdbtPluginMain.GetTagInfo(FileName: PWideChar; Tag: PTagInfo; Size: integer): integer;
var
  tg: TCDAtrack;
begin
  tg:=TCDAtrack.Create;

  if tg.ReadFromFile(FileName) then
  begin
      SetTagValue(Tag^[0], FDBT_FIELD_DURATION,     tg.Duration);

      SetTagValue(Tag^[1],  FDBT_FIELD_ARTIST,      tg.Artist);
      SetTagValue(Tag^[2],  FDBT_FIELD_TITLE,       tg.Title);
      SetTagValue(Tag^[3],  FDBT_FIELD_ALBUM,       tg.Album);
      SetTagValue(Tag^[4],  FDBT_FIELD_LENGTH,      DurationToStr(Round(tg.Duration), False));
      SetTagValue(Tag^[5], FDBT_FIELD_TRACK,        tg.Track);
      SetTagValue(Tag^[6], FDBT_FIELD_POSITION,     DurationToStr(Round(tg.Position), False));

    Result:=7;
  end else
    Result:=0;

  tg.Free;
end;

function TFdbtPluginMain.SetTagInfo(FileName: PWideChar; Tag: PTagInfo): integer;
var
  tg: TCDAtrack;
  i,c: integer;
  fld, v: string;
begin

  tg:=TCDAtrack.Create;
  c:=Length(Tag^);

  for i:=0 to c-1 do
  begin
    fld := Tag^[i].Field;
    v   := UTF8Encode(Value2String(Tag^[i].Value));
  end;

  Result:=FDBT_SUCCESS;
  tg.Free;
end;

function TFdbtPluginMain.GetTagFieldInfo(fl: PTagFieldList; size: integer): integer;
var
  i: integer;
begin
  for i:=0 to FDBTP_FIELDCOUNT-1 do
    fl^[i]:=FDBTP_FIELDS[i];

  Result:=FDBTP_FIELDCOUNT;
end;

function TFdbtPluginMain.AddinMenu(place: integer; Identify: PChar; DisplayName: PChar; ProcExe: TAddinProc; ImageType: integer; Image: cardinal): integer;
begin
  if Assigned(FAddinMenu) then
    Result:=FAddinMenu(place, Identify, DisplayName, ProcExe, ImageType, Image)
  else
    Result:=FDBT_NOTIMPLEMENTED;
end;

function TFdbtPluginMain.GetTrackDataInfo: TTrackDataInfo;
begin
  if Assigned(FGetTrackDataInfo) then Result:=FGetTrackDataInfo();
end;



initialization


end.
