summaryrefslogtreecommitdiff
path: root/plugins/Watrack/lastfm/lastfm.pas
blob: 12a144a8a7bb988548458cb37fa58ee481d5c374 (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
unit lastfm;
{$include compilers.inc}
interface
{$Resource lastfm.res}
implementation

uses windows, messages, commctrl,
  common,
  m_api,dbsettings,wrapper,mirutils, datetime,
  wat_api,global;

{$include resource.inc}

const
  opt_ModStatus:PAnsiChar = 'module/lastfm';
const
  IcoLastFM:pAnsiChar = 'WATrack_lasfm';
var
  lfm_tries:integer;
  slastinf:THANDLE;
  slast:THANDLE;
const
  lfm_lang    :integer=0;
  lfm_on      :integer=0;
  hMenuLast   :HMENU = 0;
  lfm_login   :pAnsiChar=nil;
  lfm_password:pAnsiChar=nil;
  session_id  :pAnsiChar=nil;
  np_url      :pAnsiChar=nil;
  sub_url     :pAnsiChar=nil;

function GetModStatus:integer;
begin
  result:=DBReadByte(0,PluginShort,opt_ModStatus,1);
end;

procedure SetModStatus(stat:integer);
begin
  DBWriteByte(0,PluginShort,opt_ModStatus,stat);
end;

{$i i_const.inc}
{$i i_last_opt.inc}
{$i i_last_api.inc}

procedure ThScrobble(param:LPARAM); cdecl;
var
  count:integer;
  npisok:bool;
begin
  count:=lfm_tries;
  npisok:=false;
  while count>0 do
  begin
    if not npisok then
      npisok:=SendNowPlaying>=0;
    if Scrobble>=0 then break;
    HandShake(lfm_login,lfm_password, count=1); // just last time
    dec(count);
  end;
  if count=0 then ;
end;

const
  hTimer:THANDLE=0;

procedure TimerProc(wnd:HWND;uMsg:uint;idEvent:uint_ptr;dwTime:dword); stdcall;
begin
  if hTimer<>0 then
  begin
    KillTimer(0,hTimer);
    hTimer:=0;
  end;

  if (lfm_login   <>nil) and (lfm_login^   <>#0) and
     (lfm_password<>nil) and (lfm_password^<>#0) then
    {CloseHandle}(mir_forkthread(@ThScrobble,nil));
end;

function NewPlStatus(wParam:WPARAM;lParam:LPARAM):int;cdecl;
var
  flag:integer;
begin
  result:=0;
  case wParam of
    WAT_EVENT_NEWTRACK: begin
      if hTimer<>0 then
      begin
        KillTimer(0,hTimer);
        hTimer:=0;
      end;
      if lfm_on=0 then
        hTimer:=SetTimer(0,0,30000,@TimerProc)
    end;

    WAT_EVENT_PLUGINSTATUS: begin
      case lParam of
        dsEnabled: begin
          lfm_on:=lfm_on and not 2;
          flag:=0;
        end;
        dsPermanent: begin
          lfm_on:=lfm_on or 2;
          if hTimer<>0 then
          begin
            KillTimer(0,hTimer);
            hTimer:=0;
          end;
          flag:=CMIF_GRAYED;
        end;
      else // like 1
        exit
      end;
      Menu_ModifyItem(hMenuLast, nil, INVALID_HANDLE_VALUE, flag);
    end;

    WAT_EVENT_PLAYERSTATUS: begin
      case integer(loword(lParam)) of
        WAT_PLS_STOPPED,
        WAT_PLS_NOTFOUND: begin
          if hTimer<>0 then
          begin
            KillTimer(0,hTimer);
            hTimer:=0;
          end;
        end;
      end;
    end;
  end;
end;

{$i i_last_dlg.inc}

function SrvLastFMInfo(wParam:WPARAM;lParam:LPARAM):int;cdecl;
var
  data:tLastFMInfo;
begin
  case wParam of
    0: result:=GetArtistInfo(data,lParam);
    1: result:=GetAlbumInfo (data,lParam);
    2: result:=GetTrackInfo (data,lParam);
  else
    result:=0;
  end;
end;

function SrvLastFM(wParam:WPARAM;lParam:LPARAM):int;cdecl;
begin
  if odd(lfm_on) then
  begin
    Menu_ModifyItem(hMenuLast,'Disable scrobbling');
    lfm_on:=lfm_on and not 1;
  end
  else
  begin
    Menu_ModifyItem(hMenuLast,'Enable scrobbling');
    lfm_on:=lfm_on or 1;
    if hTimer<>0 then
    begin
      KillTimer(0,hTimer);
      hTimer:=0;
    end;
  end;
  result:=ord(not odd(lfm_on));
end;

procedure CreateMenus;
var
  mi:TCListMenuItem;
  sid:TSKINICONDESC;
begin
  FillChar(sid,SizeOf(TSKINICONDESC),0);
  sid.cx:=16;
  sid.cy:=16;
  sid.szSection.a:='WATrack';

  sid.hDefaultIcon   :=LoadImage(hInstance,'IDI_LAST',IMAGE_ICON,16,16,0);
  sid.pszName        :=IcoLastFM;
  sid.szDescription.a:='LastFM';
  Skin_AddIcon(@sid);
  DestroyIcon(sid.hDefaultIcon);

  FillChar(mi, sizeof(mi), 0);
  mi.szPopupName.a:=PluginShort;

  mi.hIcon        :=IcoLib_GetIcon(IcoLastFM,0);
  mi.szName.a     :='Disable scrobbling';
  mi.pszService   :=MS_WAT_LASTFM;
  mi.popupPosition:=500050000;
  hMenuLast:=Menu_AddMainMenuItem(@mi);
end;

// ------------ base interface functions -------------

function AddOptionsPage(var tmpl:pAnsiChar;var proc:pointer;var name:PAnsiChar):integer;
begin
  tmpl:=PAnsiChar(IDD_OPT_LASTFM);
  proc:=@DlgProcOptions;
  name:='LastFM';
  result:=0;
end;

function InitProc(aGetStatus:boolean=false):integer;
begin
  slastinf:=CreateServiceFunction(MS_WAT_LASTFMINFO,@SrvLastFMInfo);
  if aGetStatus then
  begin
    if GetModStatus=0 then
    begin
      result:=0;
      exit;
    end;
  end
  else
  begin
    SetModStatus(1);
    lfm_on:=lfm_on and not 4;
  end;
  result:=1;

  LoadOpt;

  slast:=CreateServiceFunction(MS_WAT_LASTFM,@SrvLastFM);
  if hMenuLast=0 then
    CreateMenus;
  if (lfm_on and 4)=0 then
    HookEvent(ME_WAT_NEWSTATUS,@NewPlStatus);
end;

procedure DeInitProc(aSetDisable:boolean);
begin
  if aSetDisable then
    SetModStatus(0)
  else
    DestroyServiceFunction(slastinf);

  Menu_RemoveItem(hMenuLast);
  hMenuLast:=0;
  DestroyServiceFunction(slast);

  if hTimer<>0 then
  begin
    KillTimer(0,hTimer);
    hTimer:=0;
  end;

  FreeOpt;

  mFreeMem(session_id);
  mFreeMem(np_url);
  mFreeMem(sub_url);

  lfm_on:=lfm_on or 4;
end;

var
  last:twModule;

procedure Init;
begin
  last.Next      :=ModuleLink;
  last.Init      :=@InitProc;
  last.DeInit    :=@DeInitProc;
  last.AddOption :=@AddOptionsPage;
  last.Check     :=nil;
  last.ModuleName:='Last.FM';
  ModuleLink     :=@last;

end;

begin
  Init;
end.