summaryrefslogtreecommitdiff
path: root/plugins/Watrack/watrack.dpr
blob: d766d53b06dd3a4fb733f1c43cceb90fe226f059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
{$include compilers.inc}
{$IFDEF COMPILER_16_UP}
  {$WEAKLINKRTTI ON}
  {.$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
{$ENDIF}
{$IMAGEBASE $13000000}
library WATrack;
uses
// FastMM not compatible with FPC, internal for delphi xe
//  {$IFNDEF COMPILER_16_UP}{$IFNDEF FPC}fastmm4,{$ENDIF}{$ENDIF}
  m_api,dbsettings,{activex,}winampapi,
  Windows,messages,commctrl,//uxtheme,
  srv_format,srv_player,srv_getinfo,wat_api,wrapper,
  common,syswin,HlpDlg,mirutils
  ,global,waticons,io,macros, msninfo
  ,myshows   in 'myshows\myshows.pas'
  ,lastfm    in 'lastfm\lastfm.pas'
  ,statlog   in 'stat\statlog.pas'
  ,popups    in 'popup\popups.pas'
  ,proto     in 'proto\proto.pas'
  ,status    in 'status\status.pas'
  ,tmpl      in 'status\tmpl.pas'
  ,templates in 'templates\templates.pas'
{$IFDEF KOL_MCK}
  ,kolframe  in 'kolframe\kolframe.pas'
{$ENDIF}
  {$include lst_players.inc}
  {$include lst_formats.inc}
;

{$include res\i_const.inc}
{$include resource.inc}

{$Resource res\watrack.res}

{$include i_vars.inc}

const
  MenuDisablePos = 500050000;

function MirandaPluginInfoEx(mirandaVersion:dword):PPLUGININFOEX; cdecl;
begin
  result:=@PluginInfo;
  PluginInfo.cbSize     :=SizeOf(TPLUGININFOEX);
  PluginInfo.shortName  :=PluginName;
  PluginInfo.version    :=$0000060C;
  PluginInfo.description:='Paste played music info into message window or status text';
  PluginInfo.author     :='Awkward';
  PluginInfo.authorEmail:='panda75@bk.ru; awk1975@ya.ru';
  PluginInfo.copyright  :='(c) 2005-2012 Awkward';
  PluginInfo.homepage   :='http://code.google.com/p/delphi-miranda-plugins/';
  PluginInfo.flags      :=UNICODE_AWARE;
  PluginInfo.uuid       :=MIID_WATRACK;
end;

{$include i_options.inc}
{$include i_timer.inc}
{$include i_gui.inc}
{$include i_opt_dlg.inc}
{$include i_cover.inc}

function ReturnInfo(enc:WPARAM;cp:LPARAM=CP_ACP):pointer;
begin
  if enc<>WAT_INF_UNICODE then
  begin
    ClearSongInfoData(tSongInfo(SongInfoA));
    move(SongInfo,SongInfoA,SizeOf(tSongInfo));
    with SongInfoA do
    begin
      if enc=WAT_INF_ANSI then
      begin
        WideToAnsi(SongInfo.artist ,artist ,cp);
        WideToAnsi(SongInfo.title  ,title  ,cp);
        WideToAnsi(SongInfo.album  ,album  ,cp);
        WideToAnsi(SongInfo.genre  ,genre  ,cp);
        WideToAnsi(SongInfo.comment,comment,cp);
        WideToAnsi(SongInfo.year   ,year   ,cp);
        WideToAnsi(SongInfo.mfile  ,mfile  ,cp);
        WideToAnsi(SongInfo.wndtext,wndtext,cp);
        WideToAnsi(SongInfo.player ,player ,cp);
        WideToAnsi(SongInfo.txtver ,txtver ,cp);
        WideToAnsi(SongInfo.lyric  ,lyric  ,cp);
        WideToAnsi(SongInfo.cover  ,cover  ,cp);
        WideToAnsi(SongInfo.url    ,url    ,cp);
      end
      else
      begin
        WideToUTF8(SongInfo.artist ,artist);
        WideToUTF8(SongInfo.title  ,title);
        WideToUTF8(SongInfo.album  ,album);
        WideToUTF8(SongInfo.genre  ,genre);
        WideToUTF8(SongInfo.comment,comment);
        WideToUTF8(SongInfo.year   ,year);
        WideToUTF8(SongInfo.mfile  ,mfile);
        WideToUTF8(SongInfo.wndtext,wndtext);
        WideToUTF8(SongInfo.player ,player);
        WideToUTF8(SongInfo.txtver ,txtver);
        WideToUTF8(SongInfo.lyric  ,lyric);
        WideToUTF8(SongInfo.cover  ,cover);
        WideToUTF8(SongInfo.url    ,url);
      end;
    end;
    result:=@SongInfoA;
  end
  else
    result:=@SongInfo;
end;

function WATReturnGlobal(wParam:WPARAM;lParam:LPARAM):int_ptr;cdecl;
begin
  if wParam=0 then wParam:=WAT_INF_UNICODE;
  if lParam=0 then lParam:=MirandaCP;

  result:=int_ptr(ReturnInfo(wParam,lParam));
end;

function WATGetFileInfo(wParam:WPARAM;lParam:LPARAM):int_ptr;cdecl;
var
//  si:TSongInfo;
  dst:pSongInfo;
  extw:array [0..7] of WideChar;
  f:THANDLE;
  p:PWideChar;
begin
  result:=1;
  if (lParam=0) or (pSongInfo(lParam).mfile=nil) then exit;
  dst:=pointer(lParam);

  p:=dst^.mfile;
  ClearTrackInfo(dst^);
  dst^.mfile:=p;

//  FillChar(dst,SizeOf(dst),0);
//  FillChar(si,SizeOf(si),0);
{
  if flags and WAT_INF_ANSI<>0 then
    AnsiToWide(dst^.mfile,si.mfile)
  else if flags and WAT_INF_UTF<>0 then
    UTFToWide(dst^.mfile,si.mfile)
  else
    si.mfile:=dst^.mfile;
}
  f:=Reset(dst^.mfile);
  if THANDLE(f)<>INVALID_HANDLE_VALUE then
    GetFileTime(f,nil,nil,@dst^.date);
  CloseHandle(f);
  dst^.fsize:=GetFSize(dst^.mfile);
  GetExt(dst^.mfile,extw);
  if GetFileFormatInfo(dst^)<>WAT_RES_NOTFOUND then
  begin
    with dst^ do
    begin
      if (cover=nil) or (cover^=#0) then
        GetCover(cover,mfile);
      if (lyric=nil) or (lyric^=#0) then
        GetLyric(lyric,mfile);
    end;
    result:=0;
//    ReturnInfo(si,dst,wParam and $FF);
  end;
end;

function WATGetMusicInfo(wParam:WPARAM;lParam:LPARAM):int_ptr;cdecl;
type
  ppointer = ^pointer;
const
  giused:cardinal=0;
var
  flags:cardinal;
  buf:PWideChar;

  newplayer,
  newstatus,
  newtrack :bool;

  OldPlayerStatus:integer;
begin
  result:=WAT_RES_NOTFOUND;
  if DisablePlugin=dsPermanent then
    exit;

  //----- Return old info if main timer -----

  if giused<>0 then
  begin
    result:=WAT_RES_OK;
    if lParam<>0 then
      ppointer(lParam)^:=ReturnInfo(wParam and $FF);
    exit;
  end;

  giused:=1;

  OldPlayerStatus:=WorkSI.status;

  newstatus:=false;
  newtrack :=false;

  //----- Checking player -----

  flags:=0;
  if CheckAll<>BST_UNCHECKED then flags:=flags or WAT_OPT_CHECKALL;

  result:=GetPlayerInfo(WorkSI,flags);

  newplayer:=result=WAT_RES_NEWPLAYER;
  if newplayer then
    result:=WAT_RES_OK;

  //----- Checking player status -----

  if result=WAT_RES_OK then
  begin
    // player stopped - no need file info
    if WorkSI.status=WAT_PLS_STOPPED then
    begin
      newstatus:=OldPlayerStatus<>WAT_PLS_STOPPED;
    end
    //----- Get file (no file, new file, maybe new) -----
    else
    begin
      // file info will be replaced (name most important only)
      flags:=0;
      if CheckUnknown<>BST_UNCHECKED then flags:=flags or WAT_OPT_UNKNOWNFMT;
      if CheckTime   <>BST_UNCHECKED then flags:=flags or WAT_OPT_CHECKTIME;
      if UseImplant  <>BST_UNCHECKED then flags:=flags or WAT_OPT_IMPLANTANT;
      if KeepOld     <>BST_UNCHECKED then flags:=flags or WAT_OPT_KEEPOLD;

      // requirement - old file name
      result:=GetFileInfo(WorkSI,flags,0);

      if (WorkSI.status=WAT_PLS_UNKNOWN ) and  // player in unknown state
         (result       =WAT_RES_NOTFOUND) then // and known media not found
        WorkSI.status:=WAT_PLS_STOPPED;

      newstatus:=OldPlayerStatus<>WorkSI.status;

      // now time for changes (window text, volume)
      // just when music presents
      if WorkSI.status<>WAT_PLS_STOPPED then
      begin
        GetChangingInfo(WorkSI,flags);
        // full info requires
        // "no music" case blocked
{??
       WorkSI.status=WAT_PLS_PLAYING and result=WAT_RES_UNKNOWN
}
        if (result=WAT_RES_NEWFILE) or           // new file
           (result=WAT_RES_UNKNOWN) or           // unknown file (enabled by flag in GetFileInfo)
           ((result=WAT_RES_OK) and              // if not new but...
           (((wParam and WAT_INF_CHANGES)=0) or  // ... ask for full info
           (StrPosW(WorkSI.mfile,'://')<>nil) or // ... or remote file
           isContainer(WorkSI.mfile))) then      // ... or container like CUE
        begin
          // requirement: old artist/title for remote files
          newtrack:=result=WAT_RES_NEWFILE;
          result:=GetInfo(WorkSI,flags);
          if not newtrack then
            newtrack:=result=WAT_RES_NEWFILE;
        end;
      end;
    end;

  end
  //----- Player not found -----
  else
  begin
    if OldPlayerStatus<>WorkSI.status then
    begin
      WorkSI.status:=WAT_PLS_NOTFOUND;
      newstatus:=true;
    end;
  end;

  //----- Copy all data to public (WorkSI to SongInfo) -----

  SongInfo.status:=WorkSI.status;

  if WorkSI.status=WAT_PLS_NOTFOUND then
  begin
    ClearSongInfoData(WorkSI);

    ClearSongInfoData(SongInfo);

    if lParam<>0 then
      ppointer(lParam)^:=nil;
  end
  else
  begin
    if not newplayer then // for another player instance
      SongInfo.plwnd:=WorkSI.plwnd
    else
    begin
      ClearPlayerInfo(SongInfo);
      CopyPlayerInfo (WorkSI,SongInfo);
    end;

    if (WorkSI.status=WAT_PLS_STOPPED) or // no music
       (result=WAT_RES_NOTFOUND) then     // or unknown media file
    begin
      ClearFileInfo    (WorkSI);
      ClearChangingInfo(WorkSI);
      ClearTrackInfo   (WorkSI);

      ClearFileInfo    (SongInfo);
      ClearChangingInfo(SongInfo);
      ClearTrackInfo   (SongInfo);
    end
    else
    begin
      ClearChangingInfo(SongInfo);
      CopyChangingInfo (WorkSI,SongInfo);

      if newtrack then
      begin
        // covers
        if (WorkSI.cover=nil) or (WorkSI.cover^=#0) then
          GetCover(WorkSI.cover,WorkSI.mfile)
        else
        begin
          mGetMem(buf,MAX_PATH*SizeOf(WideChar));
          GetTempPathW(MAX_PATH,buf);
          if StrCmpW(buf,WorkSI.cover,StrLenW(buf))=0 then
          begin
            GetExt(WorkSI.cover,StrCatEW(buf,'\wat_cover.'));
            DeleteFileW(buf);
            MoveFileW(WorkSI.cover,buf);
            mFreeMem(WorkSI.cover);
            WorkSI.cover:=buf;
          end
          else
            mFreeMem(buf);
        end;
        // lyric
        if (WorkSI.lyric=nil) or (WorkSI.lyric^=#0) then
          GetLyric(WorkSI.lyric,WorkSI.mfile);

        ClearFileInfo(SongInfo);
        CopyFileInfo (WorkSI,SongInfo);

        ClearTrackInfo(SongInfo);
        CopyTrackInfo (WorkSI,SongInfo);
      end;
    end;

    if lParam<>0 then
      ppointer(lParam)^:=ReturnInfo(wParam and $FF);

  end;

  //----- Events -----

  if newplayer then NotifyEventHooks(hHookWATStatus,WAT_EVENT_NEWPLAYER   ,tlparam(@SongInfo));
  if newstatus then NotifyEventHooks(hHookWATStatus,WAT_EVENT_PLAYERSTATUS,SongInfo.status);
  if newtrack  then NotifyEventHooks(hHookWATStatus,WAT_EVENT_NEWTRACK    ,tlparam(@SongInfo));

  giused:=0;
end;

function PressButton(wParam:WPARAM;lParam:LPARAM):int_ptr;cdecl;
var
  flags:integer;
begin
  if DisablePlugin=dsPermanent then
    result:=0
  else
  begin
    flags:=0;
    if UseImplant<>BST_UNCHECKED then flags:=flags or WAT_OPT_IMPLANTANT;
    if mmkeyemu  <>BST_UNCHECKED then flags:=flags or WAT_OPT_APPCOMMAND;
    if CheckAll  <>BST_UNCHECKED then flags:=flags or WAT_OPT_CHECKALL;
    result:=SendCommand(wParam,lParam,flags);
  end;
end;

function WATPluginStatus(wParam:WPARAM;lParam:LPARAM):int;cdecl;
var
  f1:integer;
begin
  if wParam=2 then
  begin
    result:=PluginInfo.version;
    exit;
  end;
  if DisablePlugin=dsPermanent then
    result:=1
  else
    result:=0;
  if (integer(wParam)<0) or (wParam=MenuDisablePos) then
  begin
    if result=0 then
      wParam:=1
    else
      wParam:=0;
  end;
  case wParam of
    0: begin
      if DisablePlugin=dsPermanent then //??
      begin
        StartTimer;
        DisablePlugin:=dsEnabled;
      end;
      f1:=0;
    end;
    1: begin
      StopTimer;
      DisablePlugin:=dsPermanent;
      f1:=CMIF_CHECKED;
    end;
  else
    exit;
  end;
  DBWriteByte(0,PluginShort,opt_disable,DisablePlugin);

  ChangeMenuIcons(f1);

  NotifyEventHooks(hHookWATStatus,WAT_EVENT_PLUGINSTATUS,DisablePlugin);
end;

function WaitAllModules(wParam:WPARAM;lParam:LPARAM):int;cdecl;
var
  ptr:pwModule;
begin
  result:=0;

  CallService(MS_SYSTEM_REMOVEWAIT,wParam,0);

  ptr:=ModuleLink;
  while ptr<>nil do
  begin
    if @ptr^.Init<>nil then
      ptr^.ModuleStat:=ptr^.Init(true);
    ptr:=ptr^.Next;
  end;

  if mTimer<>0 then
    TimerProc(0,0,0,0);

  StartTimer;

  NotifyEventHooks(hHookWATLoaded,0,0);
  CloseHandle(hEvent);
end;

procedure DoTheDew(load:boolean);
var
  ptr:pwModule;
  newstate:boolean;
begin
  ptr:=ModuleLink;
  while ptr<>nil do
  begin
    if @ptr^.Check<>nil then
      ptr^.Check(load);
    ptr:=ptr^.Next;
  end;

  // TTB
  newstate:=ServiceExists(MS_TTB_ADDBUTTON)<>0;
  if newstate=(ttbState<>0) then
    exit;

  if ttbState=0 then
  begin
{
    onloadhook:=0;
    OnTTBLoaded(0,0);
    if ttbState=0 then
}
    HookEvent(ME_TTB_MODULELOADED,@OnTTBLoaded);
  end
  else
  begin
    if ServiceExists(MS_TTB_REMOVEBUTTON)>0 then
      CallService(MS_TTB_REMOVEBUTTON,WPARAM(ttbState),0);
    ttbState:=0;
  end;
end;

function OnPluginLoad(wParam:WPARAM;lParam:LPARAM):int;cdecl;
begin
  DoTheDew(true);
  result:=0;
end;

function OnPluginUnload(wParam:WPARAM;lParam:LPARAM):int;cdecl;
begin
  DoTheDew(false);
  result:=0;
end;

type
  tdbetd = record
    flags:dword;
    event:int;
    descr:pAnsiChar;
  end;

const
  cdbetd: array [0..3] of tdbetd = (
    (flags:DETF_HISTORY or DETF_MSGWINDOW; event:EVENTTYPE_WAT_REQUEST; descr:'WATrack: information request'),
    (flags:DETF_HISTORY or DETF_MSGWINDOW; event:EVENTTYPE_WAT_ANSWER ; descr:nil),
    (flags:DETF_HISTORY or DETF_MSGWINDOW; event:EVENTTYPE_WAT_ERROR  ; descr:'WATrack: request denied'),
    (flags:DETF_HISTORY or DETF_NONOTIFY ; event:EVENTTYPE_WAT_MESSAGE; descr:nil)
  );

function OnModulesLoaded(wParam:WPARAM;lParam:LPARAM):int;cdecl;
var
  p:PAnsiChar;
  dbetd:TDBEVENTTYPEDESCR;
  i:integer;
begin

  hTimer:=0;

//!!  OleInitialize(nil);

  if RegisterIcons then
    HookEvent(ME_SKIN2_ICONSCHANGED,@IconChanged);

  CreateMenus;

  if ServiceExists(MS_TTB_ADDBUTTON)<>0 then
    HookEvent(ME_TTB_MODULELOADED,@OnTTBLoaded)
  else
    ttbState:=0;

  ProcessFormatLink;
  ProcessPlayerLink;
  // next 2 blocks here coz GetAddonFileName uses some Miranda things
  p:=GetAddonFileName(nil,'player','plugins','ini');
  if p<>nil then
  begin
    LoadFromFile(p);
    mFreeMem(p);
  end;

  p:=GetAddonFileName(nil,'watrack_icons','icons','dll');
  if p<>nil then
  begin
    SetPlayerIcons(p);
    mFreeMem(p);
  end;

  IsMultiThread:=true;

  // Register WATrack events
  dbetd.cbSize     :=SizeOf(TDBEVENTTYPEDESCR);
  dbetd.module     :=PluginShort;
  dbetd.textService:=nil;
  dbetd.iconService:=nil;
  p:='WATrack_Info';
  dbetd.eventIcon  :=IcoLib_GetIcon(p,0);

  for i:=0 to HIGH(cdbetd) do
  begin
    dbetd.flags      :=cdbetd[i].flags;
    dbetd.eventType  :=cdbetd[i].event;
    dbetd.descr      :=cdbetd[i].descr;
    DbEvent_RegisterType(@dbetd);
  end;

  // Load WATrack modules
  hEvent:=CreateEvent(nil,true,true,nil);
  if hEvent<>0 then
  begin
    p:='WAT_INIT';
    CreateServiceFunction(p,@WaitAllModules);
    CallService(MS_SYSTEM_WAITONHANDLE,hEvent,tlparam(p));
  end;

  LoadOpt;
  if DisablePlugin=dsPermanent then
    CallService(MS_WAT_PLUGINSTATUS,1,0);

  StartMSNHook;

  result:=0;

  HookEvent(ME_SYSTEM_MODULELOAD  ,@OnPluginLoad);
  HookEvent(ME_SYSTEM_MODULEUNLOAD,@OnPluginUnload);
end;

procedure FreeVariables;
begin
  ClearSongInfoData(SongInfo);
  ClearSongInfoData(tSongInfo(SongInfoA));
  ClearSongInfoData(WorkSI);
  mFreeMem(CoverPaths);
  ClearFormats;
  ClearPlayers;
end;

function PreShutdown(wParam:WPARAM;lParam:LPARAM):int;cdecl;
var
  buf:array [0..511] of WideChar;
  fdata:WIN32_FIND_DATAW;
  fi:THANDLE;
  p:PWideChar;
  ptr:pwModule;
begin
  StopMSNHook;

  NotifyEventHooks(hHookWATStatus,WAT_EVENT_PLAYERSTATUS,WAT_PLS_NOTFOUND);

  if hwndTooltip<>0 then
    DestroyWindow(hwndTooltip);

  if ttbState<>0 then
  begin
    if ServiceExists(MS_TTB_REMOVEBUTTON)>0 then
      CallService(MS_TTB_REMOVEBUTTON,TWPARAM(ttbState),0);
    ttbState:=0;
  end;

  StopTimer;
  ptr:=ModuleLink;
  while ptr<>nil do
  begin
    if @ptr^.DeInit<>nil then
      ptr^.DeInit(false);
    ptr:=ptr^.Next;
  end;

  FreeVariables;

  DestroyHookableEvent(hHookWATLoaded);
  DestroyHookableEvent(hHookWATStatus);

//!!  OleUnInitialize;

  //delete cover files
  buf[0]:=#0;
  GetTempPathW(511,buf);
  p:=StrEndW(buf);
  StrCopyW(p,'wat_cover.*');

  fi:=FindFirstFileW(buf,fdata);
  if fi<>THANDLE(INVALID_HANDLE_VALUE) then
  begin
    repeat
      StrCopyW(p,fdata.cFileName);
      DeleteFileW(buf);
    until not FindNextFileW(fi,fdata);
    FindClose(fi);
  end;

  result:=0;
end;

function Load:int; cdecl;
begin
  result:=0;
  Langpack_register;

  DisablePlugin:=dsPermanent;

  hMenuRoot:=Menu_CreateRoot(MO_MAIN, 'WATrack', 500050000, 0, 0);

  hHookWATLoaded:=CreateHookableEvent(ME_WAT_MODULELOADED);
  hHookWATStatus:=CreateHookableEvent(ME_WAT_NEWSTATUS);

  HookEvent(ME_SYSTEM_OKTOEXIT,@PreShutdown);
  HookEvent(ME_OPT_INITIALISE ,@OnOptInitialise);

  CreateServiceFunction(MS_WAT_GETFILEINFO  ,@WATGetFileInfo);
  CreateServiceFunction(MS_WAT_RETURNGLOBAL ,@WATReturnGlobal);

  CreateServiceFunction(MS_WAT_GETMUSICINFO ,@WATGetMusicInfo);
  CreateServiceFunction(MS_WAT_PLUGINSTATUS ,@WATPluginStatus);
  CreateServiceFunction(MS_WAT_PRESSBUTTON  ,@PressButton);
  CreateServiceFunction(MS_WAT_WINAMPINFO   ,@WinampGetInfo);
  CreateServiceFunction(MS_WAT_WINAMPCOMMAND,@WinampCommand);

  CreateServiceFunction(MS_WAT_FORMAT,@ServiceFormat);
  CreateServiceFunction(MS_WAT_PLAYER,@ServicePlayer);

  FillChar(SongInfoA,SizeOf(SongInfoA),0);
  FillChar(SongInfo ,SizeOf(SongInfo ),0);
  FillChar(WorkSI   ,SizeOf(SongInfo ),0);
  HookEvent(ME_SYSTEM_MODULESLOADED,@OnModulesLoaded);
end;

function Unload:int; cdecl;
begin
  result:=0;
end;

exports
  Load, Unload,
  MirandaPluginInfoEx;

begin
  DisableThreadLibraryCalls(hInstance);
end.