home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / mp3osr05.zip / src / mp3srch.pas < prev    next >
Pascal/Delphi Source File  |  1999-12-30  |  9KB  |  328 lines

  1. (*
  2.  * part of MPEG searcher project
  3.  *  (c) 1999 by Alexander Trunov, 2:5069/10, jnc@mail.ru
  4.  *)
  5.  
  6. {&AlignRec-} // this is important since vp generated bad code with &AlignRec+
  7.  
  8. {$IFNDEF virtualpascal}
  9.   {$IFNDEF fpc}
  10.     {$DEFINE bp}
  11.   {$ENDIF}
  12. {$ENDIF}
  13.  
  14. uses Objects, App, MsgBox, Drivers, Menus, Views, Dialogs, StdDlg, Gadgets,
  15.   DOS, StatusW, Searcher, MP3List, mp3lb, jCtl, Config, Wizard, Idle,
  16.   VPUtils, ID3v1, Codepage
  17.   {$IFNDEF fpc}, EditChD {$ENDIF}
  18.   {$IFDEF fpc}, Commands {$IFDEF win32}, CRT{$ENDIF} {$ENDIF}
  19.   ;
  20.  
  21. const
  22.   cmAbout = 204;
  23.   cmLoadM3U = 210;
  24.  
  25. type
  26.   PMP3Searcher = ^TMP3Searcher;
  27.   TMP3Searcher = object(TApplication)
  28.     StatusWindow: PStatusWindow;
  29.     MP3ListDialog: PMP3List;
  30.     constructor Init;
  31.     procedure Idle; virtual;
  32.     procedure InitMenuBar; virtual;
  33.     procedure HandleEvent(var Event: TEvent); virtual;
  34.     destructor Done; virtual;
  35.   private
  36.     Clock: PClockView;
  37.     Heap: PHeapView;
  38.     mp3s: PCollection;
  39.     function ReadConfig: Boolean;
  40.   end;
  41.  
  42. var
  43.   mp3Searcher: PMP3Searcher;
  44.   mp3Count: Longint;
  45.  
  46. {$I nonfpc.inc}
  47. {$I sort.inc}
  48.  
  49. function TMP3Searcher.ReadConfig: Boolean;
  50. var
  51.   ctl: PCtl;
  52.   tempStr: string;
  53.  
  54.  procedure ExpandColl(tpls: PStringCollection);
  55.  var
  56.    i: Integer;
  57.    st: string;
  58.  begin
  59.    for i := 0 to tpls^.Count - 1 do
  60.    begin
  61.      st := PString(tpls^.At(i))^;
  62.      DisposeStr(tpls^.At(i));
  63.      tpls^.Items^[i] := NewStr(FExpand(st));
  64.    end;
  65.  end;
  66.  
  67. begin
  68.   ctl := New(PCtl, Init(ExtractDir(ParamStr(0)) + 'mp3srch.ctl'));
  69.  
  70.   Result := ctl^.wasExist;
  71.  
  72.   if not ctl^.wasExist then
  73.   begin
  74.     tplBy := New(PStringCollection, Init(10, 10));
  75.     tplWhole := New(PStringCollection, Init(10, 10));
  76.     searchMask := New(PStringCollection, Init(10, 10));
  77.     searchMask^.Insert(NewStr('*.mp?'));
  78.     searchExclude := New(PStringCollection, Init(10, 10));
  79.     searchExcludePath := New(PStringCollection, Init(10, 10));
  80.   end;
  81.  
  82.   tplBy := ctl^.GetPool('templates.by');
  83.   tplWhole := ctl^.GetPool('templates.whole');
  84.   ExpandColl(tplBy);
  85.   ExpandColl(tplWhole);
  86.   searchMask := ctl^.GetPool('search.mask');
  87.   searchExclude := ctl^.GetPool('search.exclude');
  88.   searchExcludePath := ctl^.GetPool('search.excludepath');
  89.   searchExcludeNonMpegs := ctl^.GetBoolean('search.exclude.nonmpegs');
  90.  
  91.   mcLayerI := ctl^.GetString('templates.macro.layer.I');
  92.   mcLayerII := ctl^.GetString('templates.macro.layer.II');
  93.   mcLayerIII := ctl^.GetString('templates.macro.layer.III');
  94.   mcLayerUnknown := ctl^.GetString('templates.macro.layer.unknown');
  95.   mcMpeg10 := ctl^.GetString('templates.macro.mpegversion.1.0');
  96.   mcMpeg20 := ctl^.GetString('templates.macro.mpegversion.2.0');
  97.   mcMpeg25 := ctl^.GetString('templates.macro.mpegversion.2.5');
  98.   mcMpegUnknown := ctl^.GetString('templates.macro.mpegversion.unknown');
  99.   mcModeStereo := ctl^.GetString('templates.macro.channelmode.stereo');
  100.   mcModeJointStereo := ctl^.GetString('templates.macro.channelmode.jointstereo');
  101.   mcModeDualChannel := ctl^.GetString('templates.macro.channelmode.dualchannel');
  102.   mcModeSingleChannel := ctl^.GetString('templates.macro.channelmode.singlechannel');
  103.   tplMakeShortFilenames := ctl^.GetBoolean('templates.macro.shortfilenames');
  104.  
  105.   rpUseCodepage := ctl^.GetBoolean('representation.usecodepage');
  106.  
  107.   searchRange := ctl^.GetLongint('search.riff.header.searchrange');
  108.   headerSearchRange := searchRange;
  109.  
  110.   rpIgnoreCase := ctl^.GetBoolean('representation.sort.ignorecase');
  111.   cmpIgnoreCase := rpIgnoreCase;
  112.  
  113.   tempStr := stLocase(ctl^.GetString('representation.targetcodepage'));
  114.   if tempStr = '866' then
  115.     rpCodepage := cpAlt;
  116.   if tempStr = 'koi8-r' then
  117.     rpCodepage := cpKoi;
  118.   if tempStr = '1251' then
  119.     rpCodepage := cpWin;
  120.  
  121.   Dispose(ctl, Done);
  122. end;
  123.  
  124. procedure TMP3Searcher.Idle;
  125. begin
  126. //  inherited Idle;
  127.   Clock^.Update;
  128.   Heap^.Update;
  129.   give_up_cpu_time;
  130. end;
  131.  
  132. constructor TMP3Searcher.Init;
  133. var
  134.   R: TRect;
  135. begin
  136.   inherited Init;
  137.  
  138.   GetExtent(R);
  139.   R.A.X := R.B.X - 9; R.B.Y := R.A.Y + 1;
  140.   Clock := New(PClockView, Init(R));
  141.   Insert(Clock);
  142.  
  143.   GetExtent(R);
  144.   Dec(R.B.X);
  145.   R.A.X := R.B.X - 12; R.A.Y := R.B.Y - 1;
  146.   Heap := New(PHeapView, Init(R));
  147.   Heap^.Mode := HVComma;
  148.   Insert(Heap);
  149.  
  150.   if not ReadConfig then
  151.   begin
  152.     MessageBox(^C' An error occured while trying to read control file.', nil,
  153.       mfInformation or mfOkButton);
  154.   end;
  155. end;
  156.  
  157. destructor TMP3Searcher.Done;
  158. begin
  159.   if searchMask <> nil then
  160.     Dispose(searchMask, Done);
  161.   if tplBy <> nil then
  162.     Dispose(tplBy, Done);
  163.   if tplWhole <> nil then
  164.     Dispose(tplWhole, Done);
  165.   if searchExclude <> nil then
  166.     Dispose(searchExclude, Done);
  167.   if searchExcludePath <> nil then
  168.     Dispose(searchExcludePath, Done);
  169.   inherited Done;
  170. end;
  171.  
  172. procedure EnumProcedure(FoundFilename: string);
  173. var
  174.   fS, sS: string;
  175.   i: Integer;
  176.   mp3: Pmp3;
  177. begin
  178.   fS := stLocase(FoundFilename);
  179.   for i := 0 to searchExclude^.Count - 1 do
  180.   begin
  181.     fS := ExtractFileName(fS) + ExtractFileExt(fS);
  182.     sS := stLocase(PString(searchExclude^.Items^[i])^);
  183.     if CheckWildcard(fS, sS) then
  184.       Exit;
  185.   end;
  186.   for i := 0 to searchExcludePath^.Count - 1 do
  187.   begin
  188.     fS := ExtractDir(FoundFilename);
  189.     fS := Copy(fS, 1, Length(fS) - 1);
  190.     sS := stLocase(PString(searchExcludePath^.Items^[i])^);
  191.     if CheckWildcard(fS, sS) then
  192.       Exit;
  193.   end;
  194.   mp3 := New(Pmp3, Init(FoundFilename, true));
  195.   if (not mp3^.tag^.isMpeg) and searchExcludeNonMpegs then
  196.   begin
  197.     Dispose(mp3, Done);
  198.     Exit;
  199.   end;
  200.   inc(mp3Count);
  201.   with mp3Searcher^.mp3s^ do
  202.     Insert(mp3);
  203.   with mp3Searcher^.StatusWindow^.ALabel^ do
  204.   begin
  205.     DisposeStr(Text);
  206.     Text := NewStr('Scanning specified path... ' + IntToStr(mp3Count) +
  207.       ' MP3s found yet.');
  208.     Draw;
  209.   end;
  210.   mp3Searcher^.Idle;
  211. end;
  212.  
  213. procedure TMP3Searcher.HandleEvent(var Event: TEvent);
  214. var
  215.   DirName: DirStr;
  216.   srch: PSearcher;
  217.   i: Integer;
  218.   theFile: FNameStr;
  219.   f: Text;
  220.   line: string;
  221. begin
  222.   inherited HandleEvent(Event);
  223.   if Event.What = evCommand then
  224.   begin
  225.     case Event.Command of
  226.       cmOpen:
  227.         begin
  228.           if SelectDir(DirName, 213) then
  229.           begin
  230.             mp3Count := 0;
  231.             mp3s := New(PCollection, Init(10, 10));
  232.             StatusWindow := New(PStatusWindow, Init(
  233.               'Scanning specified path... 0 MP3s found yet.   '));
  234.             InsertWindow(StatusWindow);
  235.             srch := New(PSearcher, Init);
  236.             with srch^ do
  237.             begin
  238.              {$IFDEF bp}@{$ENDIF}EnumProc :=
  239.                {$IFDEF fpc}@{$ENDIF}{$IFDEF bp}@{$ENDIF}EnumProcedure;
  240.               for i := 0 to searchMask^.Count - 1 do
  241.               begin
  242.                 BeginningPath := DirName;
  243.                 Mask := PString(searchMask^.Items^[i])^;
  244.                 BeginSearch;
  245.               end;
  246.             end;
  247.             Dispose(srch, Done);
  248.             Dispose(StatusWindow, Done);
  249.  
  250.             MP3ListDialog := New(PMP3List, Init(mp3s));
  251.             ExecuteDialog(MP3ListDialog, nil);
  252.           end;
  253.           ClearEvent(Event);
  254.         end;
  255.       cmLoadM3U:
  256.         begin
  257.           theFile := '*.m3u';
  258.           if OpenFile(theFile, 216) then
  259.           begin
  260.             StatusWindow := New(PStatusWindow, Init(
  261.               'Scanning specified path... 0 MP3s found yet.   '));
  262.             InsertWindow(StatusWindow);
  263.             mp3s := New(PCollection, Init(10, 10));
  264.             mp3Count := 0;
  265.  
  266.             Assign(f, theFile);
  267.             Reset(f);
  268.  
  269.             while not EOF(f) do
  270.             begin
  271.               Readln(f, line);
  272.               if FileExists(line) then
  273.               begin
  274.                 EnumProcedure(line);
  275.               end;
  276.             end;
  277.  
  278.             Close(f);
  279.  
  280.             Dispose(StatusWindow, Done);
  281.             MP3ListDialog := New(PMP3List, Init(mp3s));
  282.             ExecuteDialog(MP3ListDialog, nil);
  283.           end;
  284.           ClearEvent(Event);
  285.         end;
  286.       cmAbout:
  287.         begin
  288.           MessageBox(#3'MPEG searcher ' + Version + ' (' +
  289.             Platform + ')' + #13 + #3'Copyright (c) 1999 by' + #13 +
  290.             #3'Alexander Trunov [2:5069/10]', nil,
  291.             mfInformation or mfOkButton);
  292.           ClearEvent(Event);
  293.         end;
  294.     end;
  295.   end;
  296. end;
  297.  
  298. procedure TMP3Searcher.InitMenuBar;
  299. var
  300.   R: TRect;
  301. begin
  302.   GetExtent(R);
  303.   R.B.Y := R.A.Y + 1;
  304.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  305.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  306.     NewItem('~O~pen path..', 'F3', kbF3, cmOpen, hcNoContext,
  307.     NewItem('~L~oad Internet-playlist..', 'F4', kbF4, cmLoadM3U, hcNoContext,
  308.     NewLine(
  309.     NewItem('~Q~uit', 'Alt-X', kbAltX, cmQuit, hcNoContext, nil))))),
  310.     NewItem('~A~bout', 'Alt-A', kbAltA, cmAbout, hcNoContext, nil)))));
  311. end;
  312.  
  313. begin
  314.   {$ifdef linux}
  315.   FileSystem := fsDos;
  316.   {$endif}
  317.  
  318.   RegisterObjects;
  319.   mp3Searcher := New(PMP3Searcher, Init);
  320.   mp3Searcher^.Run;
  321.   Dispose(mp3Searcher, Done);
  322.  {$IFDEF fpc}
  323.   {$IFDEF win32}
  324.   ClrScr;
  325.   {$ENDIF}
  326.  {$ENDIF}
  327. end.
  328.