aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_plg/IDScanner.cpp
blob: 2345ab06d3e6df4534e336adcb0c98f54993dffc (plain) (blame)
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
696
697
698
#include "IDScanner.h"
#include "main.h"
#include "../winamp/wa_ipc.h"
#include "api__ml_plg.h"
#include "playlist.h"
#include <assert.h>
#include <atlbase.h>
#include <strsafe.h>	// include this last

//#define DEBUG_CALLBACKS

IDScanner::IDScanner() : systemCallbacks(0)
{
	musicID=0;
	killswitch=0;
	filesComplete=0;
	filesTotal=0;
	state=STATE_IDLE;
	m_dwCookie=0;
	syscb_registered=false;

	// Create the stack that will hold our batched up files for step 4 processing
	//process_items;
}

IDScanner::~IDScanner()
{
	// ToDo: Make sure we clean up the processing stack here if we need to do that
	//Shutdown();
}

void IDScanner::Shutdown()
{
	if (musicID)
	{
		IConnectionPoint *icp = GetConnectionPoint(musicID, DIID__ICDDBMusicIDManagerEvents);
		if (icp)
		{
			icp->Unadvise(m_dwCookie);
			icp->Release(); 
		}
		musicID->Shutdown();
		musicID->Release();
	}
	musicID=0;

	// Deregister the system callbacks
	WASABI_API_SYSCB->syscb_deregisterCallback(this);
}

static HRESULT FillTag(ICddbFileInfo *info, BSTR filename)
{
	ICddbID3TagPtr infotag = NULL;
	infotag.CreateInstance(CLSID_CddbID3Tag);
	ICddbFileTag2_5Ptr tag2_5 = NULL;
	infotag->QueryInterface(&tag2_5);
	itemRecordW *record = AGAVE_API_MLDB->GetFile(filename);
	if (record && infotag && tag2_5)
	{
		wchar_t itemp[64] = {0};
		if (record->artist)
			infotag->put_LeadArtist(record->artist);

		if (record->album)
			infotag->put_Album(record->album);

		if (record->title)
			infotag->put_Title(record->title);

		if (record->genre)
			infotag->put_Genre(record->genre);

		if (record->track > 0)
			infotag->put_TrackPosition(_itow(record->track, itemp, 10));

// TODO:	if (record->tracks > 0)

		if (record->year > 0)
			infotag->put_Year(_itow(record->year, itemp, 10));

		if (record->publisher)
			infotag->put_Label(record->publisher);

		/*
		if (GetFileInfo(filename, L"ISRC", meta, 512) && meta[0])
			infotag->put_ISRC(meta);
		*/

		if (record->disc > 0)
			infotag->put_PartOfSet(_itow(record->disc, itemp, 10));

		if (record->albumartist)
			tag2_5->put_DiscArtist(record->albumartist);

		if (record->composer)
			tag2_5->put_Composer(record->composer);

		if (record->length > 0)
			tag2_5->put_LengthMS(_itow(record->length*1000, itemp, 10));

		if (record->bpm > 0)
			infotag->put_BeatsPerMinute(_itow(record->bpm, itemp, 10));

		/*
		if (GetFileInfo(filename, L"conductor", meta, 512) && meta[0])
					tag2_5->put_Conductor(meta);
					*/
		AGAVE_API_MLDB->FreeRecord(record);
	}

	if (info) info->put_Tag(infotag);

	return S_OK;
}

void IDScanner::CommitFileInfo(ICddbFileInfo *match)
{
	ICddbFileTagPtr tag;
	match->get_Tag(&tag);

	ICddbDisc2Ptr disc1, disc;
	match->get_Disc(&disc1);

	ICddbDisc2_5Ptr disc2_5;
	ICddbTrackPtr track;
	ICddbTrack2_5Ptr track2;
	if (disc1)
	{
		musicID->GetFullDisc(disc1, &disc);
		if (disc == 0)
			disc=disc1;
		disc->QueryInterface(&disc2_5);
		disc->GetTrack(1, &track);
		if (track)
			track->QueryInterface(&track2);
	}

	CComBSTR file, tagID, extData;
	match->get_Filename(&file);
	tag->get_FileId(&tagID);
	playlistMgr->FileSetTagID(file, tagID, CDDB_UPDATE_NONE);

	ICddbFileTag2_5Ptr tag2;
	tag->QueryInterface(&tag2);
	playlistMgr->FileSetFieldVal(file, gnpl_crit_field_xdev1, L"0"); // mark as done!

	if (tag2) // try tag first
		tag2->get_ExtDataSerialized(&extData);

	if (!extData && track2 != 0)  // WMA files don't get their tag object's extended data set correctly, so fallback to track extended data
		track2->get_ExtDataSerialized(&extData);

	if (!extData && disc2_5 != 0) // finally, fall back to disc extended data
		disc2_5->get_ExtDataSerialized(&extData);

	playlistMgr->FileSetExtDataSerialized(file, extData, CDDB_UPDATE_NONE);

	if (tagID)
		AGAVE_API_MLDB->SetField(file, "GracenoteFileID", tagID);
	if (extData)
		AGAVE_API_MLDB->SetField(file, "GracenoteExtData", extData);

	// TODO: if we don't have an artist & album, we might as well grab this out of the tag now

	// TODO: make thread-safe and optional
	/*
	updateFileInfo(file, L"GracenoteFileID", tagID);
	updateFileInfo(file, L"GracenoteExtData", extData);
	WriteFileInfo(file);
	*/
}

STDMETHODIMP STDMETHODCALLTYPE IDScanner::QueryInterface(REFIID riid, PVOID *ppvObject)
{
	if (!ppvObject)
		return E_POINTER;

	else if (IsEqualIID(riid, __uuidof(_ICDDBMusicIDManagerEvents)))
		*ppvObject = (_ICDDBMusicIDManagerEvents *)this;
	else if (IsEqualIID(riid, IID_IDispatch))
		*ppvObject = (IDispatch *)this;
	else if (IsEqualIID(riid, IID_IUnknown))
		*ppvObject = this;
	else
	{
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}

	AddRef();
	return S_OK;
}

ULONG STDMETHODCALLTYPE IDScanner::AddRef(void)
{
	return 1;
}

ULONG STDMETHODCALLTYPE IDScanner::Release(void)
{
	return 0;
}

HRESULT STDMETHODCALLTYPE IDScanner::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
{
	switch (dispid)
	{
		case 1: // OnTrackIDStatusUpdate, params: CddbMusicIDStatus Status, BSTR filename, long* Abort
		{
			//long *abort = pdispparams->rgvarg[0].plVal;
			// TODO: is this safe to put here?  Or does this make us get partial results
		}
		break;
		case 2: // OnAlbumIDStatusUpdate, params: CddbMusicIDStatus Status, BSTR filename, long current_file, long total_files, long* Abort
		{
			long *abort = pdispparams->rgvarg[0].plVal;
			/*long total_files = pdispparams->rgvarg[1].lVal;
			long current_file= pdispparams->rgvarg[2].lVal;*/
			CddbMusicIDStatus status = (CddbMusicIDStatus)pdispparams->rgvarg[4].lVal;
			BSTR filename = pdispparams->rgvarg[3].bstrVal;

			// TODO: is this safe to put here?  Or does this make us get partial results
			if (killswitch)
				*abort = 1;
		}
		break;
		case 3: // OnTrackIDComplete, params: LONG match_code, ICddbFileInfo* pInfoIn, ICddbFileInfoList* pListOut
			break;
		case 4:
			break;// OnAlbumIDComplete, params: LONG match_code, ICddbFileInfoList* pListIn, ICddbFileInfoLists* pListsOut
		case 5:
			break; // OnGetFingerprint
		case 6:
			break;
		case 7://OnLibraryIDListStarted
			break;
		case 8: // OnLibraryIDListComplete
		{

			long *abort  = pdispparams->rgvarg[0].plVal;
			if (killswitch)
				*abort = 1;
			/*long FilesError =pdispparams->rgvarg[1].lVal;
			long FilesNoMatch=pdispparams->rgvarg[2].lVal;
			long FilesFuzzy=pdispparams->rgvarg[3].lVal;
			long FilesExact=pdispparams->rgvarg[4].lVal;*/
			filesTotal=pdispparams->rgvarg[5].lVal;
			filesComplete=pdispparams->rgvarg[6].lVal;
			IDispatch *disp = pdispparams->rgvarg[7].pdispVal;
			if (disp)
			{
				ICddbFileInfoList* matchList=0;
				disp->QueryInterface(&matchList);
				if (matchList)
				{
					long matchcount;
					matchList->get_Count(&matchcount);
					for (int j = 1;j <= matchcount;j++)
					{
						ICddbFileInfoPtr match;
						matchList->GetFileInfo(j, &match);
						CommitFileInfo(match);
					}

					matchList->Release();
				}
				return S_OK;
			}
			else
				return E_FAIL;

		}
		break;
		case 9: //OnLibraryIDComplete
			break;
		case 10: // OnGetFingerprintInfo
		{
			long *abort = pdispparams->rgvarg[0].plVal;
			IDispatch *disp = pdispparams->rgvarg[1].pdispVal;
			BSTR filename = pdispparams->rgvarg[2].bstrVal;

			ICddbFileInfoPtr info;
			disp->QueryInterface(&info);
			return AGAVE_API_GRACENOTE->CreateFingerprint(musicID, AGAVE_API_DECODE, info, filename, abort);
		}
		break;
		case 11: // OnGetTagInfo
		{
			pdispparams->rgvarg[0].plVal;
			IDispatch *disp = pdispparams->rgvarg[1].pdispVal;
			BSTR filename = pdispparams->rgvarg[2].bstrVal;

			ICddbFileInfoPtr info;
			disp->QueryInterface(&info);
			return FillTag(info, filename);
		}
		break;
	}
	return DISP_E_MEMBERNOTFOUND;
}

HRESULT STDMETHODCALLTYPE IDScanner::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
{
	*rgdispid = DISPID_UNKNOWN;
	return DISP_E_UNKNOWNNAME;
}

HRESULT STDMETHODCALLTYPE IDScanner::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
{
	return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE IDScanner::GetTypeInfoCount(unsigned int FAR * pctinfo)
{
	return E_NOTIMPL;
}

void IDScanner::SetGracenoteData(BSTR filename, BSTR tagID, BSTR extData)
{
	bool foundExt=false;
	if (extData && extData[0])
	{
		playlistMgr->FileSetExtDataSerialized(filename, extData, CDDB_UPDATE_NONE);
		CComBSTR test;
		playlistMgr->FileGetExtDataSerialized(filename, &test, 0); // benski> 24 Jul 2007 - there is a currently a bug that makes this always E_FAIL
		if (test)
			foundExt=true;
	}

	if (!foundExt) // no Extended Data (or invalid), but we have a Tag ID, we'll ask the playlist SDK to do a quick lookup
	{
		playlistMgr->FileSetTagID(filename, tagID, CDDB_UPDATE_EXTENDED);

		// write back to Media Library database
		CComBSTR extData;
		playlistMgr->FileGetExtDataSerialized(filename, &extData, 0); // benski> 24 Jul 2007 - there is a currently a bug that makes this always E_FAIL
		if (extData)
			AGAVE_API_MLDB->SetField(filename, "GracenoteExtData", extData);
	}
	else
		playlistMgr->FileSetTagID(filename, tagID, CDDB_UPDATE_NONE);
}

/*
//void IDScanner::ProcessDatabaseDifferences(Device * dev, C_ItemList * ml0,C_ItemList * itemRecordsOnDevice, C_ItemList * itemRecordsNotOnDevice, C_ItemList * songsInML,  C_ItemList * songsNotInML)
void IDScanner::ProcessDatabaseDifferences(Device * dev, C_ItemList * ml0,C_ItemList * itemRecordsOnDevice, C_ItemList * itemRecordsNotOnDevice, C_ItemList * songsInML,  C_ItemList * songsNotInML)
{
  C_ItemList device2;
	C_ItemList *device0=&device2;

   int l = dev->getPlaylistLength(0);
   for(int i=0; i<l; i++) device0->Add((void*)dev->getPlaylistTrack(0,i));


  qsort(ml0->GetAll(),ml0->GetSize(),sizeof(void*),sortfunc_ItemRecords);
	nu::qsort(device0->GetAll(), device0->GetSize(), sizeof(void*), dev, compareSongs);

  C_ItemList *ml = new C_ItemList;
  C_ItemList *device = new C_ItemList;
  
  int i,j;
  
  {
    itemRecordW * lastice = NULL;
    songid_t lastsong = NULL;
    for(i=0; i<ml0->GetSize(); i++) {
      itemRecordW * it = (itemRecordW*)ml0->Get(i);
      if(lastice) if(compareItemRecords(lastice,it)==0) continue;
      ml->Add(it);
      lastice = it;
    }
    for(i=0; i<device0->GetSize(); i++) {
      songid_t song = (songid_t)device0->Get(i);
      if(lastsong) if(compareSongs((void*)&song,(void*)&lastsong, dev)==0) continue;
      device->Add((void*)song);
      lastsong = song;
    }
  }

  i=0,j=0;
  int li = device->GetSize();
  int lj = ml->GetSize();
  while(i<li && j<lj) {
    itemRecordW * it = (itemRecordW*)ml->Get(j);
    songid_t song = (songid_t)device->Get(i);
    
    int cmp = compareItemRecordAndSongId(it,song, dev);
    if(cmp == 0) { // song on both
      if(itemRecordsOnDevice) itemRecordsOnDevice->Add(it);
      if(songsInML) songsInML->Add((void*)song);
      i++;
      j++;
    }
    else if(cmp > 0) { //song in ml and not on device
      if(itemRecordsNotOnDevice) itemRecordsNotOnDevice->Add(it);
      j++;
    }
    else { // song on device but not in ML
      if(songsNotInML) songsNotInML->Add((void*)song);
      i++;
    }
  }
  // any leftovers?
  if(songsNotInML) while(i<li) { 
    songid_t song = (songid_t)device->Get(i++);
    
    songsNotInML->Add((void*)song); 
  }
  if(itemRecordsNotOnDevice) while(j<lj) {
    itemRecordW * it = (itemRecordW *)ml->Get(j++);
    
    itemRecordsNotOnDevice->Add(it); 
  }
  delete ml; delete device;
}
*/

/*
2-pass strategy
Pass 1:  Find all tracks with Gracenote Extended Data
Pass 2:  Find File ID & extended data by fingerprint
*/
void IDScanner::ScanDatabase()
{
	filesComplete=0;
	filesTotal=0;
	state=STATE_INITIALIZING;
	killswitch=0; // reset just in case

	if (SetupPlaylistSDK())
	{
		// If this is our first time running then lets register the wasabi system callbacks for adding and removing tracks
		if (!syscb_registered)
		{
			WASABI_API_SYSCB->syscb_registerCallback(this);
			syscb_registered = true;
		}

		// Set up the MLDB manager
		InitializeMLDBManager();

		state=STATE_SYNC;
		/* Get a list of files in the media library database */
		itemRecordListW *results = AGAVE_API_MLDB->Query(L"type=0");
		if (results)
		{
			filesTotal=results->Size;
			for (int i=0;i<results->Size;i++)
			{
				if (killswitch)
					break;
				wchar_t * filename = results->Items[i].filename;
				HRESULT hr=playlistMgr->AddEntry(filename);					// Add entry to gracenote DB
				assert(SUCCEEDED(S_OK));
				if (hr == S_OK)
				{
					// Fill in Artist & Album info since we have it in the itemRecordList anyway
					// TODO: probably want to use SKIP_THE_AND_WHITESPACE here
					if (results->Items[i].album && results->Items[i].album[0])
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_album_name, results->Items[i].album);

					if (results->Items[i].artist && results->Items[i].artist[0])
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_track_artist_name, results->Items[i].artist);

					// Populate title information so that we have more complete data.
					if (results->Items[i].title && results->Items[i].title[0])
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_track_name, results->Items[i].title);

					wchar_t storage[64] = {0};
					// Populate the file length in milliseconds
					if (results->Items[i].length > 0)
					{
						_itow(results->Items[i].length,storage, 10);
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_file_length, storage);
					}

					// Populate the file size in kilobytes
					if (results->Items[i].filesize > 0)
					{
						_itow(results->Items[i].filesize,storage, 10);
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_file_size, storage);
					}

					wchar_t *tagID = getRecordExtendedItem(&results->Items[i], L"GracenoteFileID");
					if (tagID && tagID[0])
					{
						SetGracenoteData(filename, tagID, getRecordExtendedItem(&results->Items[i], L"GracenoteExtData"));
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_xdev1, L"0"); // done with this file!
					}
					else
						hr=playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_xdev1, L"1"); // move to phase 1
				}
				filesComplete=i+1;
			}

			AGAVE_API_MLDB->FreeRecordList(results);
			state=STATE_METADATA;
			filesComplete=0;
			if (!killswitch)
				Pass1();
			filesComplete=0;
			state=STATE_MUSICID;
			if (!killswitch)
				Pass2();
			state=STATE_DONE;
			if (!killswitch)
				AGAVE_API_MLDB->Sync();
		}
		// Set the pass 2 flag back so that on next generation we dont try to run it
		run_pass2_flag = false;
	}
	else
		state=STATE_ERROR;
}

bool IDScanner::GetStatus(long *pass, long *track, long *tracks)
{
	*pass = state;
	*track = filesComplete;
	*tracks = filesTotal;
	return true;
}

// System callback handlers from WASABI

FOURCC IDScanner::GetEventType()
{
	return api_mldb::SYSCALLBACK;
}
int IDScanner::notify(int msg, intptr_t param1, intptr_t param2)
{
	wchar_t *filename = (wchar_t *)param1;

	switch (msg)
	{
		case api_mldb::MLDB_FILE_ADDED:
		{

			DebugCallbackMessage(param1, L"File Added: '%s'");

			// Call the add/update function that needs to run on our lonesome playlist generator thread
			WASABI_API_THREADPOOL->RunFunction(plg_thread, IDScanner::MLDBFileAddedOnThread, _wcsdup(filename), (intptr_t)this, api_threadpool::FLAG_REQUIRE_COM_STA);
		}
		break;
		case api_mldb::MLDB_FILE_REMOVED_PRE:
		{
			// We are not concerned with the PRE scenario
			//DebugCallbackMessage(param1, L"File Removed PRE: '%s'");
		}
		break;
		case api_mldb::MLDB_FILE_REMOVED_POST:
		{
			WASABI_API_THREADPOOL->RunFunction(plg_thread, IDScanner::MLDBFileRemovedOnThread, _wcsdup(filename), (intptr_t)this, api_threadpool::FLAG_REQUIRE_COM_STA);
			// We will only care about the post scenario since we just need to remove the file entry from gracenote.
			//DebugCallbackMessage(param1, L"File Removed POST: '%s'");
		}
		break;
		case api_mldb::MLDB_FILE_UPDATED:
		{
			// For now we call the add method even on an update
			WASABI_API_THREADPOOL->RunFunction(plg_thread, IDScanner::MLDBFileAddedOnThread, _wcsdup(filename), (intptr_t)this, api_threadpool::FLAG_REQUIRE_COM_STA);
			//DebugCallbackMessage(param1, L"File Updated: '%s'");
		}
		break;
		case api_mldb::MLDB_CLEARED:
		{
			WASABI_API_THREADPOOL->RunFunction(plg_thread, IDScanner::MLDBClearedOnThread, 0, (intptr_t)this, api_threadpool::FLAG_REQUIRE_COM_STA);
			//DebugCallbackMessage(param1, L"MLDB Cleared");
		}
		break;
		default: return 0;
	}
	return 1;
}

// Outputs a messagebox with a filename to know when callbacks are being triggered
inline void IDScanner::DebugCallbackMessage(const intptr_t text, const wchar_t *message)
{
//#ifdef DEBUG_CALLBACKS
#if defined(DEBUG) && defined(DEBUG_CALLBACKS)
	const int size = MAX_PATH + 256;
	wchar_t *filename = (wchar_t *)text;
	wchar_t buff[size] = {0};

	//wsprintf(buff, size, message, filename);
	StringCchPrintf(buff, size, message, filename);
	MessageBox(0, buff, L"Wasabi Callback Debug", 0);
#endif
}

int IDScanner::MLDBFileAddedOnThread(HANDLE handle, void *user_data, intptr_t id)
{
	if (!playlistMgr) return 0;

	// Variables to hold information about the file query
	wchar_t *filename = (wchar_t *)user_data;
	IDScanner *scanner = (IDScanner *)id;

	wchar_t buff[1024] = {0};
	_ltow(scanner->state, buff, 10);
		
	itemRecordW *result = AGAVE_API_MLDB->GetFile(filename);
	
	HRESULT hr=playlistMgr->AddEntry(filename);	// Add the file entry to the gracenote DB
	
	assert(SUCCEEDED(S_OK));
	
	if (hr == S_OK /*&& results->Size == 1*/)
	{
		// Fill in Artist & Album info since we have it in the itemRecordList anyway
		// TODO: probably want to use SKIP_THE_AND_WHITESPACE here
		if (result->album && result->album[0])
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_album_name, result->album);

		if (result->artist && result->artist[0])
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_track_artist_name, result->artist);

		// Populate title, file size, and length information so that we have more complete data.
		if (result->title && result->title[0])
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_track_name, result->title);


		wchar_t storage[64] = {0};
		// Populate the file length in milliseconds
		if (result->length > 0)
		{
			_itow(result->length,storage, 10);
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_file_length, storage);
		}

		// Populate the file size in kilobytes
		if (result->filesize > 0)
		{
			_itow(result->filesize,storage, 10);
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_file_size, storage);
		}

		wchar_t *tagID = getRecordExtendedItem(result, L"GracenoteFileID");
		if (tagID && tagID[0])
		{
			scanner->SetGracenoteData(filename, tagID, getRecordExtendedItem(result, L"GracenoteExtData"));
			// We have everything we need at this point in the gracenote DB
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_xdev1, L"0"); // done with this file!
		}
		else		// Set it to the final scan 
		{
			playlistMgr->FileSetFieldVal(filename, gnpl_crit_field_xdev1, L"2"); // move to phase 2, we can skip phase 1 
			// Add the current file to the step 4 processing stack
			// TODOD is there a mem leak here??
			ProcessItem *itemz = new ProcessItem();
			itemz->filename = filename;
			//scanner->process_items.push(*itemz);			// Add the current item coming in to the queue

			// Set the flag so that we know we will need to rerun step 4 (pass 2) on a playlist regeneration, this only needs to happen if there is an actual change.
			run_pass2_flag = true;
		}
	}

	if (result)
		AGAVE_API_MLDB->FreeRecord(result);

	// ToDo: We need to do this free when we pop it off of the processing stack later
	free(filename);	// Clean up the user data
	return 0;
}

int IDScanner::MLDBFileRemovedOnThread(HANDLE handle, void *user_data, intptr_t id)
{
	wchar_t *filename = (wchar_t *)user_data;

	HRESULT hr = playlistMgr->DeleteFile(filename);

	if (hr == S_OK)
		return 0;
	else
		return 1;

	free(filename);	// Clean up the user data
}

int IDScanner::MLDBClearedOnThread(HANDLE handle, void *user_data, intptr_t id)
{
	return ResetDB(false);
}

int IDScanner::ProcessStackItems(void)
{
	// ToDo: Run through the stack items and process stage 4 on them
	//this->
	return 1;
}

#define CBCLASS IDScanner
START_DISPATCH;
CB(SYSCALLBACK_GETEVENTTYPE, GetEventType);
CB(SYSCALLBACK_NOTIFY, notify);
END_DISPATCH;
#undef CBCLASS