aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_cdda/DB.Cpp
blob: 71bfdd99858444520c4d96f6332c0aa576334da1 (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
#include "main.h"
#include "cddb.h"
#include "../Winamp/wa_ipc.h"
#include "../nu/AutoChar.h"
#include "../nu/AutoWide.h"
#include "api__in_cdda.h"

#include <api/service/waservicefactory.h>
#include <shlwapi.h>
#include <atlbase.h>
#include "resource.h"
#include <commctrl.h>
#include <strsafe.h>

char config_use_cddb = 1;

#define MAX_CACHE_SIZE	8


typedef struct _CBDATA
{
	DINFO	*ps;
	CHAR	cLetter;
	DWORD	cddbid;
} CBDATA;

typedef struct _CACHEREC
{
	DINFO		discInfo;
	HRESULT		result;
} CACHEREC;

static CACHEREC cddb_cache[MAX_CACHE_SIZE];
static int cddb_cache_cursor = 0;

void DefaultValues(DINFO *ps)
{
	ps->Reset();
	ps->compilation = false;
	ps->discnum = 0;
	ps->numdiscs = 0;

	ps->populated = true;
}

static bool IsInternetAvailable()
{
	return !!SendMessage(line.hMainWindow, WM_WA_IPC, 0, IPC_INETAVAILABLE);
}

static DWORD failid[26] ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

#define WRITEINFO(obj, field, name) { obj->get_ ## field(&str); fwprintf(fp, name L": %s\r\n", str.m_str); }
#define WRITEINFO_LONG(obj, field, name) { long val; obj->get_ ## field(&val); fwprintf(fp, name L": %d\r\n", val); }

#ifndef IGNORE_API_GRACENOTE
static void DumpInfoFile(ICddbDiscPtr disc)
{
#if 0 // keep this code, but we'll wait until a later version to implement more data
	long numTracks;
	FILE *fp = fopen("c:\\cdinfo.txt", "wt");
	//		wchar_t BOM[] = {0xFEFF, 0};
	//	fwrite(BOM, 2, 1, fp);
	CComBSTR str;

	WRITEINFO(disc, Toc, L"Disc TOC");
	WRITEINFO(disc, Artist, L"Disc Artist");
	WRITEINFO(disc, Title, L"Disc Title");
	WRITEINFO(disc, Label, L"Disc Label");
	WRITEINFO(disc, Year, L"Disc Year");
	WRITEINFO(disc, MediaId, L"Disc MediaId");
	WRITEINFO(disc, Notes, L"Disc Notes");
	WRITEINFO(disc, GenreId, L"Disc GenreId");
	WRITEINFO(disc, SecondaryGenreId, L"Disc Secondary GenreId");
	WRITEINFO(disc, RegionId, L"Disc RegionId");
	WRITEINFO(disc, Revision, L"Disc Revision");
	WRITEINFO(disc, TotalInSet, L"Disc Discs");
	WRITEINFO(disc, NumberInSet, L"Disc Disc #");
	WRITEINFO(disc, LanguageId, L"Disc Language Id");
	WRITEINFO(disc, RevisionTag, L"Disc Revision Tag");
	WRITEINFO_LONG(disc, Compilation, L"Disc Compilation");

	ICddbDisc2 *disc2;
	disc->QueryInterface(&disc2);
	WRITEINFO(disc2, ReleaseDate, L"Disc Release Date");
	WRITEINFO(disc2, RecordingDate, L"Disc Recording Date");
	WRITEINFO(disc2, ProductCode, L"Disc Product Code");
	WRITEINFO(disc2, YearComposed, L"Disc Year Composed");
	disc2->Release();

	ICddbCredits *credits;
	disc->get_Credits(&credits);
	long creditCount;
	credits->get_Count(&creditCount);
	for (long c = 0;c < creditCount;c++)
	{
		ICddbCredit *credit;
		credits->GetCredit(c + 1, &credit);
		WRITEINFO(credit, Id, L"Credit ID");
		WRITEINFO(credit, Name, L"Credit Name");
		WRITEINFO(credit, Notes, L"Credit Notes");
		credit->Release();
	}
	credits->Release();

	ICddbTracks *tracks;
	disc->get_Tracks(&tracks);
	tracks->get_Count(&numTracks);
	for (int i = 0;i < numTracks;i++)
	{
		ICddbTrack *track;
		tracks->GetTrack(i + 1, &track);
		fputws(L"-----------------\r\n", fp);
		WRITEINFO(track, Title, L"Track Title");
		WRITEINFO(track, Artist, L"Track Artist");
		WRITEINFO(track, Year, L"Track Year");
		WRITEINFO(track, Label, L"Track Label");
		WRITEINFO(track, Notes, L"Track Notes");
		WRITEINFO(track, GenreId, L"Track GenreId");
		WRITEINFO(track, SecondaryGenreId, L"Track SecondaryGenreId");
		WRITEINFO(track, Lyrics, L"Track Lyrics");
		WRITEINFO(track, BeatsPerMinute, L"Track BPM");
		WRITEINFO(track, ISRC, L"Track ISRC");

		ICddbTrack2 *track2;
		track->QueryInterface(&track2);
		WRITEINFO(track2, ReleaseDate, L"Release Date");
		WRITEINFO(track2, RecordingDate, L"Recording Date");
		WRITEINFO(track2, YearComposed, L"Year Composed");
		track2->Release();

		ICddbCredits *credits;
		track->get_Credits(&credits);
		if (credits)
		{
			long creditCount;
			credits->get_Count(&creditCount);
			for (long c = 0;c < creditCount;c++)
			{
				ICddbCredit *credit;
				credits->GetCredit(c + 1, &credit);
				WRITEINFO(credit, Id, L"Credit ID");
				WRITEINFO(credit, Name, L"Credit Name");
				WRITEINFO(credit, Notes, L"Credit Notes");
				credit->Release();
			}
			credits->Release();
		}
		track->Release();
	}

	tracks->Release();
	fclose(fp);
#endif
}
#endif

#ifdef IGNORE_API_GRACENOTE
static HRESULT CALLBACK Cddb_ResultCallback(HRESULT result, ICddbDisc *pDisc, DWORD *pdwAutoCloseDelay, ULONG_PTR user)
{
	int index;
	CACHEREC *pRec;
	CBDATA *pData = (CBDATA*)user;
	if (!pData) return E_POINTER;
	DINFO *pDI = NULL;

	for (index = 0, pRec = &cddb_cache[0]; index < MAX_CACHE_SIZE && 0 != pRec->discInfo.CDDBID; pRec++, index++)
	{
		if (pRec->discInfo.CDDBID == pData->cddbid)
		{
			pDI = &pRec->discInfo;

			break;
		}
	}
	if (!pDI) return S_OK;

	if (S_OK == result)
	{
		#ifndef IGNORE_API_GRACENOTE
		if (pDisc)
		{
			DumpInfoFile(pDisc);
			GetDiscInfo(pDisc, pDI);
			StoreDisc(pDI->CDDBID, pDisc);
			ICddbCacheManager* pCache;
			HRESULT hr = Cddb_GetICacheManger((void**)&pCache);
			if (SUCCEEDED(hr))
			{
				CComBSTR toc;
				pDisc->get_Toc(&toc);
				pCache->StoreDiscByToc(toc, pDisc);
				pCache->Release();
			}
			pDI->populated = true;
		}
		#endif
	}
	else
	{
		#ifndef IGNORE_API_GRACENOTE
		if (DoCDText(pDI, pData->cLetter))
		{
			StoreCDText(pDI->CDDBID, pData->cLetter);
			result = S_OK;
		}
		#endif
	}

	CddbCache_SetDisc(pDI, result);

	if (pData->ps->CDDBID == pDI->CDDBID)
	{
		*pData->ps = *pDI;
	}
	return S_OK;
}
#endif

void InitializeCddbCache(void)
{
	ZeroMemory(cddb_cache, sizeof(CACHEREC)*MAX_CACHE_SIZE);
	cddb_cache_cursor = 0;
}

void UninitializeCddbCache(void)
{
	ZeroMemory(cddb_cache, sizeof(CACHEREC)*MAX_CACHE_SIZE);
	cddb_cache_cursor = 0;
}

HRESULT CddbCache_SetDisc(DINFO *pDiscInfo,  HRESULT lookupResult)
{
	int index;
	CACHEREC *pRec;
	if (!pDiscInfo || 0 == pDiscInfo->CDDBID) return E_FAIL;

	for (index = 0, pRec = &cddb_cache[0]; index < MAX_CACHE_SIZE && 0 != pRec->discInfo.CDDBID; pRec++, index++)
	{
		if (pRec->discInfo.CDDBID == pDiscInfo->CDDBID) break;
	}
	if (0 == pRec->discInfo.CDDBID || MAX_CACHE_SIZE == index)
	{
		pRec = &cddb_cache[cddb_cache_cursor];
		cddb_cache_cursor++;
		if (cddb_cache_cursor >= MAX_CACHE_SIZE) cddb_cache_cursor = 0;
	}

	if (pRec)
	{
		pRec->result = lookupResult;
		pRec->discInfo = *pDiscInfo;
	}

	return S_OK;
}

int GetCDDBInfo(DINFO *ps, wchar_t device)
{
	int index;
	CACHEREC *pRec;
	//HRESULT hr;

	//wchar_t szTOC[2048] = {0};

	if (ps->populated) return 0;

	/* first, look in our memory cache */
	for (index = 0, pRec = &cddb_cache[0]; index < MAX_CACHE_SIZE && 0 != pRec->discInfo.CDDBID; pRec++, index++)
	{
		if (pRec->discInfo.CDDBID == ps->CDDBID)
		{
			if (S_OK == pRec->result)
				*ps = pRec->discInfo;
			else
				DefaultValues(ps);
			return 0; // no need to notify about changes

		}
	}

	/* look it up in the local database */
	if (QueryDINFO(ps->CDDBID, ps))
	{
		CddbCache_SetDisc(ps, S_OK);
		return 0;
	}

	/* check CDDB & CD-Text */
	/*CddbCache_SetDisc(ps, E_PENDING);
	if (Cddb_CalculateTOC(ps, szTOC, sizeof(szTOC)/sizeof(wchar_t)))
	{
		ULONG flags;
		CBDATA data;
		data.ps = ps;
		data.cLetter = device;
		data.cddbid = ps->CDDBID;
		flags = CDDB_UI_MODAL | CDDB_UI_MULTIPLE | CDDB_UI_NOMATCH;
		if (!config_use_cddb || !IsInternetAvailable()) flags |= CDDB_NOINET;
		hr = Cddb_DoLookup(szTOC, line.hMainWindow, Cddb_ResultCallback, flags, (ULONG_PTR)&data);
		if (FAILED(hr)) Cddb_DisplayResultDlg(line.hMainWindow, hr, AUTOCLOSE_NEVER, flags);
	}
	else hr = CDDB_E_BADTOC;
	if (FAILED(hr)) CddbCache_SetDisc(ps, hr);*/
	return 1;//SUCCEEDED(hr);
}