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
|
#include "DeviceView.h"
#include <time.h>
#include <shlwapi.h>
#include "SkinnedListView.h"
#include "metadata_utils.h"
#include "IconStore.h"
#include "api__ml_pmp.h"
#include "resource1.h"
#include "main.h"
static void TransferCallback(void * callBackContext, wchar_t * status);
extern void TransfersListUpdateItem(CopyInst * item);
void TransfersListUpdateItem(CopyInst * item, DeviceView *view);
extern void TransfersListPushPopItem(CopyInst * item);
void TransfersListPushPopItem(CopyInst * item, DeviceView *view);
extern HWND mainMessageWindow;
extern HANDLE hMainThread;
/*
How to add new ways of copying files.
Subclass CopyInst, over-ride CopyAction and Equals
Optionally over-ride PreCopyAction, PostCopyAction and Cancelled
Add to transfer queue as normal.
*/
SongCopyInst::SongCopyInst(DeviceView * dev, itemRecordW * song0)
{
usesPreCopy = false;
usesPostCopy = true;
this->dev = dev;
equalsType = 0;
res = 0;
copyRecord(&song, song0);
songid = NULL;
status = STATUS_WAITING;
// status caption
WASABI_API_LNGSTRINGW_BUF(IDS_WAITING, statusCaption, sizeof(statusCaption)/sizeof(wchar_t));
SYSTEMTIME system_time;
GetLocalTime(&system_time);
GetTimeFormat(LOCALE_INVARIANT, NULL, &system_time, NULL, lastChanged, sizeof(lastChanged)/sizeof(wchar_t));
// make the itemRecord a little safer
if(!song.album) song.album = _wcsdup(L"");
if(!song.artist) song.artist = _wcsdup(L"");
if(!song.title) song.title = _wcsdup(L"");
if(!song.genre) song.genre = _wcsdup(L"");
if(!song.filename) song.filename = _wcsdup(L"");
if(!song.comment) song.comment = _wcsdup(L"");
if(!song.albumartist) song.albumartist = _wcsdup(L"");
if(!song.publisher) song.publisher = _wcsdup(L"");
if(!song.composer) song.composer = _wcsdup(L"");
// track caption
lstrcpyn(trackCaption, song.artist, 128);
int l = lstrlen(trackCaption);
if(128 - l > 1) lstrcpyn(trackCaption + l, L" - ", 128-l);
l = lstrlen(trackCaption);
if(128 - l > 1) lstrcpyn(trackCaption + l, song.title, 128 - l);
// type caption
WASABI_API_LNGSTRINGW_BUF(IDS_TRANSFER, typeCaption, sizeof(typeCaption)/sizeof(wchar_t));
// TODO fill out when other actions become done
// source and destination details
//this->dev->GetDisplayName(sourceDevice, sizeof(sourceDevice)/sizeof(wchar_t));
WASABI_API_LNGSTRINGW_BUF(IDS_LOCAL_MACHINE, sourceDevice, ARRAYSIZE(sourceDevice));
this->dev->GetDisplayName(destDevice, ARRAYSIZE(destDevice));
lstrcpynW(sourceFile, song.filename, sizeof(sourceFile)/sizeof(wchar_t));
}
SongCopyInst::~SongCopyInst()
{
freeRecord(&song);
}
void SongCopyInst::Cancelled() {
// helps us to do appropriate handling
if (status == STATUS_TRANSFERRING)
{
dev->threadKillswitch = -2;
}
status = STATUS_CANCELLED;
WASABI_API_LNGSTRINGW_BUF(IDS_UPLOAD_CANCELLED, statusCaption, ARRAYSIZE(statusCaption));
SYSTEMTIME system_time = {0};
GetLocalTime(&system_time);
GetTimeFormat(LOCALE_INVARIANT, NULL, &system_time, NULL, lastChanged, ARRAYSIZE(lastChanged));
dev->dev->trackRemovedFromTransferQueue(&song);
}
static void TransferCallback(void * callBackContext, wchar_t * status)
{
CopyInst * c = (CopyInst *)callBackContext;
if(!wcscmp(status, c->statusCaption)) return;
if (status && *status) lstrcpyn(c->statusCaption, status, sizeof(c->statusCaption)/sizeof(wchar_t));
TransfersListUpdateItem(c);
TransfersListUpdateItem(c, c->dev);
int pc=0;
// copes with 'transferring %'
if(swscanf(status,L"%*s %d%%",&pc))
{
if (c->dev->isCloudDevice) cloudTransferProgress = pc;
else c->dev->currentTransferProgress = pc;
}
// copes with 'transferring (%)'
else if(swscanf(status,L"%*s %*1c %d%%",&pc))
{
if (c->dev->isCloudDevice) cloudTransferProgress = pc;
else c->dev->currentTransferProgress = pc;
}
c->dev->UpdateSpaceInfo(TRUE, TRUE);
}
bool SongCopyInst::CopyAction()
{
int r = dev->dev->transferTrackToDevice(&song,this,TransferCallback,&songid,&dev->threadKillswitch);
if (r==0 && AGAVE_API_STATS)
AGAVE_API_STATS->IncrementStat(api_stats::PMP_TRANSFER_COUNT);
dev->dev->trackRemovedFromTransferQueue(&song);
return r!=0;
}
void SongCopyInst::PostCopyAction()
{
if(status == STATUS_DONE && songid)
{
dev->dev->addTrackToPlaylist(0, songid);
if (dev->metadata_fields & SUPPORTS_ALBUMART)
{
int w,h;
ARGB32 *bits;
if (AGAVE_API_ALBUMART->GetAlbumArt_NoAMG(song.filename, L"cover", &w, &h, &bits) == ALBUMART_SUCCESS)
{
dev->dev->setArt(songid,bits,w,h);
WASABI_API_MEMMGR->sysFree(bits);
}
}
}
}
bool SongCopyInst::Equals(CopyInst * b) {
if(this->equalsType == b->equalsType) {
SongCopyInst * c = (SongCopyInst*)b;
bool ret = (compareItemRecords(&this->song,&c->song) == 0);
// for cloud then we do some extra checks to allow different formats
// and also for sending the same file to a different cloud device...
if (c->dev->isCloudDevice)
{
const wchar_t * mime_1 = getRecordExtendedItem(&c->song, L"mime");
const wchar_t * mime_2 = getRecordExtendedItem(&this->song, L"mime");
int mime_match = lstrcmpiW(mime_1 ? mime_1 : L"", mime_2 ? mime_2 : L"");
int device_match = lstrcmpiW(c->destDevice ? c->destDevice : L"", this->destDevice ? this->destDevice : L"");
if (!device_match)
{
if (!mime_match)
{
return ret;
}
else
{
return false;
}
}
else
{
return false;
}
}
return ret;
}
return false;
}
PlaylistCopyInst::PlaylistCopyInst(DeviceView * dev, itemRecordW * song, wchar_t * plName0, int plid0) : SongCopyInst(dev,song)
{
lstrcpyn(plName,plName0,255);
plid=plid0;
plAddSongs = NULL;
usesPreCopy = false;
usesPostCopy = true;
}
PlaylistCopyInst::~PlaylistCopyInst()
{
SongCopyInst::~SongCopyInst();
if(plAddSongs) delete plAddSongs;
}
bool PlaylistCopyInst::PreCopyAction() {return false;}
void PlaylistCopyInst::PostCopyAction() {
SongCopyInst::PostCopyAction();
if(!plAddSongs || plid == -1) return;
if(plid >= dev->dev->getPlaylistCount()) return;
int l = plAddSongs->GetSize();
wchar_t pln[256] = {0};
dev->dev->getPlaylistName(plid,pln,255);
if(wcscmp(pln,plName) == 0) {
if(status == STATUS_DONE && songid) dev->dev->addTrackToPlaylist(plid,songid);
for(int i=0; i < l; i++) {
songid_t s = (songid_t)plAddSongs->Get(i);
if(s) dev->dev->addTrackToPlaylist(plid,s);
}
}
}
ReverseCopyInst::ReverseCopyInst(DeviceView * dev, const wchar_t * filepath, const wchar_t * format, songid_t song, bool addToLibrary, bool uppercaseext) : uppercaseext(uppercaseext) {
usesPreCopy = false;
usesPostCopy = addToLibrary;
this->dev = dev;
equalsType = 1;
this->songid = song;
status = STATUS_WAITING;
WASABI_API_LNGSTRINGW_BUF(IDS_WAITING, statusCaption, sizeof(statusCaption)/sizeof(wchar_t));
WASABI_API_LNGSTRINGW_BUF(IDS_COPY_TO_LIBRARY, typeCaption, sizeof(typeCaption)/sizeof(wchar_t));
dev->dev->getTrackArtist(song,trackCaption,128);
int l = lstrlen(trackCaption);
if(128 - l > 1) lstrcpyn(trackCaption + l,L" - ",128-l);
l = lstrlen(trackCaption);
if(128 - l > 1) dev->dev->getTrackTitle(song,trackCaption + l,128 - l);
// find path for song
lstrcpyn(path,format,2036);
FixReplacementVars(path,2036,dev->dev,song);
PathCombine(path,filepath,path);
}
bool ReverseCopyInst::Equals(CopyInst *b) {
if(this->equalsType == b->equalsType) {
ReverseCopyInst* c = (ReverseCopyInst*)b;
return (c->dev == this->dev) && (c->songid == this->songid);
}
return false;
}
bool ReverseCopyInst::CopyAction() { //Return true if failed.
wchar_t * lastslash = wcsrchr(path,L'\\');
if(!lastslash) {
WASABI_API_LNGSTRINGW_BUF(IDS_INVALID_PATH, statusCaption, sizeof(statusCaption)/sizeof(wchar_t));
return true;
}
*lastslash=0;
if(RecursiveCreateDirectory(path)) {
WASABI_API_LNGSTRINGW_BUF(IDS_INVALID_PATH, statusCaption, sizeof(statusCaption)/sizeof(wchar_t));
return true;
}
*lastslash=L'\\';
// path created, copy file over.
int r = dev->dev->copyToHardDrive(songid, path, this, TransferCallback, &dev->threadKillswitch);
return r!=0;
}
void ReverseCopyInst::PostCopyAction()
{
itemRecordW ice={0};
filenameToItemRecord(path,&ice);
ice.rating = dev->dev->getTrackRating(songid);
ice.playcount = dev->dev->getTrackPlayCount(songid);
ice.lastplay = dev->dev->getTrackLastPlayed(songid);
ice.lastupd = dev->dev->getTrackLastUpdated(songid);
ice.type = dev->dev->getTrackType(songid);
SendMessage(plugin.hwndLibraryParent,WM_ML_IPC,(WPARAM)&ice,ML_IPC_DB_ADDORUPDATEITEMW);
freeRecord(&ice);
}
ReversePlaylistCopyInst::ReversePlaylistCopyInst(DeviceView * dev, const wchar_t * filepath, const wchar_t * format, songid_t song, wchar_t * playlistFile0, wchar_t * playlistName0, bool last,bool addToLibrary)
: ReverseCopyInst(dev,filepath,format,song,addToLibrary,false), last(last) {
lstrcpyn(playlistFile,playlistFile0,MAX_PATH);
lstrcpyn(playlistName,playlistName0,128);
}
bool ReversePlaylistCopyInst::CopyAction()
{
bool r = ReverseCopyInst::CopyAction();
return r;
}
void ReversePlaylistCopyInst::PostCopyAction()
{
ReverseCopyInst::PostCopyAction();
FILE * f = _wfopen(playlistFile,L"at");
if(f) {
fputws(L"#EXTINF:",f);
wchar_t buf[100] = {0};
wsprintf(buf,L"%d",dev->dev->getTrackLength(songid)/1000);
fputws(buf,f);
fputws(L",",f);
wchar_t title[2048] = {0};
getTitle(dev->dev,songid,path,title,2048);
fputws(title,f);
fputws(L"\n",f);
fputws(path,f);
fputws(L"\n",f);
fclose(f);
}
if(last) {
mlAddPlaylist a = {sizeof(mlAddPlaylist),playlistName,playlistFile,PL_FLAG_SHOW | PL_FLAGS_IMPORT,-1,-1};
SendMessage(plugin.hwndLibraryParent,WM_ML_IPC,(WPARAM)&a,ML_IPC_PLAYLIST_ADD);
_wunlink(playlistFile);
}
}
// HERE BE DRAGONS
static VOID CALLBACK APC_PreCopy(ULONG_PTR dwParam) {
CopyInst * a = (CopyInst *)dwParam;
a->res = a->PreCopyAction()?2:1;
}
static VOID CALLBACK APC_PostCopy(ULONG_PTR dwParam) {
CopyInst * a = (CopyInst *)dwParam;
a->PostCopyAction();
a->res = 1;
}
void CALLBACK TransferNavTimer(HWND hwnd, UINT uMsg, UINT_PTR eventId, ULONG elapsed)
{
TransferContext *context = (TransferContext *)eventId;
if (context && context->dev->queueTreeItem)
{
NAVITEM item;
item.cbSize = sizeof(NAVITEM);
item.hItem = context->dev->queueTreeItem;
item.iSelectedImage = item.iImage = icon_store.GetQueueIcon(context->dev->queueActiveIcon);
context->dev->queueActiveIcon = (context->dev->queueActiveIcon + 1) % 4;
item.mask = NIMF_IMAGE | NIMF_IMAGESEL;
MLNavItem_SetInfo(plugin.hwndLibraryParent, &item);
}
}
void TransferContext::DoOneTransfer(HANDLE handle)
{
if (TryEnterCriticalSection(&transfer_lock))
{
if(dev->threadKillswitch == 1)
{
WASABI_API_THREADPOOL->RemoveHandle(transfer_thread, handle);
dev->threadKillswitch = 100;
SetEvent(killer);
LeaveCriticalSection(&transfer_lock);
return;
}
if (IsPaused())
{
LeaveCriticalSection(&transfer_lock);
return;
}
LinkedQueue * txQueue = getTransferQueue(this->dev);
CopyInst * c = (txQueue ? (CopyInst *)txQueue->Peek() : NULL);
if (c)
{
if (c->res != 2 && c->status != STATUS_CANCELLED)
{
c->status = STATUS_TRANSFERRING;
start = time(NULL);
TransferCallback(c, WASABI_API_LNGSTRINGW_BUF(IDS_STARTING_TRANSFER, c->statusCaption, sizeof(c->statusCaption)/sizeof(wchar_t)));
c->res = 0;
if(c->usesPreCopy)
SynchronousProcedureCall(APC_PreCopy,(ULONG_PTR)c);
}
if(dev->threadKillswitch)
{
WASABI_API_THREADPOOL->RemoveHandle(transfer_thread, handle);
dev->threadKillswitch = 100;
SetEvent(killer);
LeaveCriticalSection(&transfer_lock);
return;
}
if(c->res == 2)
{ // dupe
WASABI_API_LNGSTRINGW_BUF((dev->isCloudDevice ? IDS_ALREADY_UPLOADED : IDS_DUPLICATE), c->statusCaption, sizeof(c->statusCaption)/sizeof(wchar_t));
c->status = STATUS_DONE;
}
else if (c->status != STATUS_CANCELLED)
{
// do the transfer
int r = c->CopyAction();
c->status = (r == -1 ? STATUS_ERROR : STATUS_DONE);
SYSTEMTIME system_time = {0};
GetLocalTime(&system_time);
GetTimeFormat(LOCALE_INVARIANT, NULL, &system_time, NULL, c->lastChanged, sizeof(c->lastChanged)/sizeof(wchar_t));
// Now do whatever needs to be done post-copy (add to playlist or whatever)
c->res = 0;
if(c->usesPostCopy && c->status == STATUS_DONE)
SynchronousProcedureCall(APC_PostCopy,(ULONG_PTR)c);
// now work out the moving average time per transfer
end = time(NULL);
if(c->status == STATUS_DONE)
{
times[numTransfers % AVERAGEBASIS] = (int)((long)end - (long)start);
numTransfers++;
}
int n = min(AVERAGEBASIS,numTransfers);
if(n > 0)
{
int t = 0;
for(int i = 0; i < n; i++) t += times[i];
dev->transferRate = ((double)t) / ((double)n);
}
}
if(dev->threadKillswitch == 2)
{ // a transfer has been cancelled part way through
dev->threadKillswitch = 0;
delete txQueue->Poll();
}
else
{
LinkedQueue * finishedTX = getFinishedTransferQueue(this->dev);
if (finishedTX)
{
txQueue->lock();
finishedTX->lock();
finishedTX->Offer(txQueue->Poll());
finishedTX->unlock();
txQueue->unlock();
TransfersListPushPopItem(c);
TransfersListPushPopItem(c, dev);
}
}
dev->commitNeeded = true;
if (dev->isCloudDevice) cloudTransferProgress = 0;
else dev->currentTransferProgress = 0;
LeaveCriticalSection(&transfer_lock);
SetEvent(handle);
}
else
{
if(dev->commitNeeded)
PostMessage(mainMessageWindow,WM_TIMER,COMMITTIMERID,0);
if (dev->isCloudDevice) cloudTransferProgress = 0;
else dev->currentTransferProgress = 0;
dev->UpdateActivityState();
LeaveCriticalSection(&transfer_lock);
}
}
}
int TransferThreadPoolFunc(HANDLE handle, void *user_data, intptr_t id)
{
TransferContext *context = (TransferContext *)user_data;
SetTimer(plugin.hwndLibraryParent, (UINT_PTR)user_data, 1000, TransferNavTimer);
// allows cancels to continue even if a cloud device upload had been cancelled
if (context->dev->threadKillswitch == -2) context->dev->threadKillswitch = 0;
context->DoOneTransfer(handle);
KillTimer(plugin.hwndLibraryParent, (UINT_PTR)user_data);
return 0;
}
bool TransferContext::IsPaused()
{
return (paused_all || paused);
}
void TransferContext::Pause()
{
if (1 == InterlockedIncrement(&paused))
{
if(dev->commitNeeded)
PostMessage(mainMessageWindow,WM_TIMER,COMMITTIMERID,0);
SetEvent(notifier);
}
}
void TransferContext::Resume()
{
if (0 == InterlockedDecrement(&paused))
{
SetEvent(notifier);
}
}
bool TransferContext::IsAllPaused()
{
return paused_all?true:false;
}
void TransferContext::PauseAll()
{
InterlockedIncrement(&paused_all);
}
void TransferContext::ResumeAll()
{
InterlockedDecrement(&paused_all);
}
|