aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_local/ReIndexUI.cpp
blob: 83e0c9680417d6db493b380c2d1f5c066d375c2b (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
#include "Main.h"
#include "ml_local.h"
#include "resource.h"

static INT_PTR CALLBACK CompactWndProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_INITDIALOG:
		{
			SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
			SendDlgItemMessage(hwndDlg, IDC_PROGRESS1, PBM_SETRANGE, 0, MAKELPARAM(0, 400));
			SendDlgItemMessage(hwndDlg, IDC_PROGRESS1, PBM_SETPOS, 0, 0);

			int *progress = (int *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
			if (progress[1] == -102)
			{
				progress[1] = -101;
				SetWindowTextW(hwndDlg, WASABI_API_LNGSTRINGW(IDS_REFRESH_FILESIZE_DATEADDED));
			}

			SetTimer(hwndDlg, 666, 500, 0);
			break;
		}
		case WM_TIMER:
			if (wParam == 666)
			{
				int *progress = (int *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
				if (progress[0] == 666)
				{
					KillTimer(hwndDlg, 666);
					EndDialog(hwndDlg, 0);
				}
				else if (progress[1] != -101)
				{
					SendDlgItemMessage(hwndDlg, IDC_PROGRESS1, PBM_SETPOS, progress[1] + 300, 0);
				}
				else
				{
					SendDlgItemMessage(hwndDlg, IDC_PROGRESS1, PBM_SETPOS, progress[0] + 100, 0);
				}
			}
			break;
	}
	return 0;
}

static DWORD CALLBACK CompactThread(LPVOID param)
{
	WASABI_API_DIALOGBOXPARAMW(IDD_REINDEX, NULL, CompactWndProc, (LPARAM)param);
	return 0;
}

static int sortFunc(const void *a, const void *b)
{
	const wchar_t **fn1 = (const wchar_t **)a;
	const wchar_t **fn2 = (const wchar_t **)b;
	return _wcsicmp(*fn1, *fn2);
}

void RetypeFilename(nde_table_t table)
{
	int totalRecords = NDE_Table_GetRecordsCount(g_table);
	if (totalRecords == 0) // bail out early so we don't flash a dialog
		return;

	int progress[2] = {-100, -101};
	DWORD threadId = 0;
	HANDLE compactThread = 0;

	nde_scanner_t pruneScanner = NDE_Table_CreateScanner(table);
	if (pruneScanner)
	{
		bool first=true;
		int recordNum = 0;
		NDE_Scanner_First(pruneScanner);
		while (!NDE_Scanner_EOF(pruneScanner))
		{
			progress[0] = MulDiv(recordNum, 100, totalRecords)-100;
			nde_field_t f = NDE_Scanner_GetFieldByID(pruneScanner, MAINTABLE_ID_FILENAME);
			if (f && NDE_Field_GetType(f) == FIELD_STRING)
			{
				wchar_t *s = NDE_StringField_GetString(f);
				ndestring_retain(s);

				NDE_Scanner_DeleteField(pruneScanner, f);

				nde_field_t new_f = NDE_Scanner_NewFieldByID(pruneScanner, MAINTABLE_ID_FILENAME);
				NDE_StringField_SetString(new_f, s);

				ndestring_release(s);
				NDE_Scanner_Post(pruneScanner);
			}
			else if (f)
			{
				first = false; // skips creating the thread
				break;
			}

			recordNum++;
			NDE_Scanner_Next(pruneScanner);
			if (first)
			{
				compactThread = CreateThread(0, 0, CompactThread, progress, 0, &threadId);
				DumpArtCache(); // go ahead and dump the album art cache if we had to rebuild this table.  ideally we could perform the same logic but this is easier and it's 1 in the morning and i don't feel like doing it :)
			}
			first=false;
		}

		NDE_Table_DestroyScanner(table, pruneScanner);
		if (compactThread)
			NDE_Table_Sync(table);
	}
	progress[0] = 666;
	if (compactThread)
	{
		WaitForSingleObject(compactThread, INFINITE);
		CloseHandle(compactThread);
	}
}

void RefreshFileSizeAndDateAddedTable(nde_table_t table)
{
	int totalRecords = NDE_Table_GetRecordsCount(g_table);
	if (totalRecords == 0) // bail out early so we don't flash a dialog
		return;

	int progress[2] = {-100, -102};
	DWORD threadId = 0;
	HANDLE compactThread = 0;

	nde_scanner_t scanner = NDE_Table_CreateScanner(table);
	if (scanner)
	{
		bool first=true;
		int recordNum = 0;
		NDE_Scanner_First(scanner);
		while (!NDE_Scanner_EOF(scanner))
		{
			progress[0] = MulDiv(recordNum, 400, totalRecords);

			// converts filesize from a int and kb scaled value to the actual filesize as a int64
			nde_field_t f = NDE_Scanner_GetFieldByID(scanner, MAINTABLE_ID_FILESIZE);
			if (f && NDE_Field_GetType(f) == FIELD_INTEGER)
			{
				__int64 size = NDE_IntegerField_GetValue(f);
				if (size) size *= 1024;

				NDE_Scanner_DeleteField(scanner, f);

				nde_field_t new_f = NDE_Scanner_NewFieldByType(scanner, FIELD_INT64, MAINTABLE_ID_FILESIZE);
				NDE_Int64Field_SetValue(new_f, size);

				NDE_Scanner_Post(scanner);
			}

			// takes the lastupd value and applies it to dateadded so we've got something to use
			f = NDE_Scanner_GetFieldByID(scanner, MAINTABLE_ID_LASTUPDTIME);
			if (f && NDE_Field_GetType(f) == FIELD_DATETIME)
			{
				int lastupd = NDE_IntegerField_GetValue(f);
				nde_field_t new_f = NDE_Scanner_NewFieldByID(scanner, MAINTABLE_ID_DATEADDED);
				NDE_IntegerField_SetValue(new_f, lastupd);

				NDE_Scanner_Post(scanner);
			}

			NDE_Scanner_Next(scanner);
			recordNum++;
			if (first)
			{
				compactThread = CreateThread(0, 0, CompactThread, progress, 0, &threadId);
			}
			first=false;
		}

		NDE_Table_DestroyScanner(table, scanner);
		if (compactThread)
			NDE_Table_Sync(table);
	}
	progress[0] = 666;
	if (compactThread)
	{
		WaitForSingleObject(compactThread, INFINITE);
		CloseHandle(compactThread);
	}
}

void ReindexTable(nde_table_t table)
{
	int totalRecords = NDE_Table_GetRecordsCount(g_table);
	if (totalRecords == 0) // bail out early so we don't flash a dialog
		return;

	int progress[2] = {-100, -101};
	DWORD threadId;
	HANDLE compactThread = CreateThread(0, 0, CompactThread, progress, 0, &threadId);

	nde_scanner_t pruneScanner = NDE_Table_CreateScanner(table);
	if (pruneScanner)
	{
		int recordNum = 0;
		NDE_Scanner_First(pruneScanner);
		while (!NDE_Scanner_EOF(pruneScanner))
		{
			int total = MulDiv(recordNum, 100, totalRecords);
			progress[0] = total - 100;
			#ifndef elementsof
			#define elementsof(x) (sizeof(x)/sizeof(*x))
			#endif
			unsigned char STR_IDS[] = {MAINTABLE_ID_TITLE, MAINTABLE_ID_ARTIST, MAINTABLE_ID_ALBUM, MAINTABLE_ID_GENRE,
			                           MAINTABLE_ID_COMMENT, MAINTABLE_ID_GRACENOTE_ID, MAINTABLE_ID_ALBUMARTIST,
									   MAINTABLE_ID_TRACKGAIN, MAINTABLE_ID_PUBLISHER, MAINTABLE_ID_COMPOSER,
									   MAINTABLE_ID_PODCASTCHANNEL, MAINTABLE_ID_GRACENOTEFILEID, MAINTABLE_ID_GRACENOTEEXTDATA,
									   MAINTABLE_ID_CATEGORY, MAINTABLE_ID_CODEC, MAINTABLE_ID_DIRECTOR, MAINTABLE_ID_PRODUCER
			                          };
			for (size_t i = 0;i != elementsof(STR_IDS);i++)
			{
				nde_field_t f = NDE_Scanner_GetFieldByID(pruneScanner, STR_IDS[i]);
				if (f)
				{
					const wchar_t *s = NDE_StringField_GetString(f);
					if (!s || !*s)
					{
						NDE_Scanner_DeleteField(pruneScanner, f);
						NDE_Scanner_Post(pruneScanner);
					}
				}
			}

			unsigned char INT_IDS_ZEROOK[] = {MAINTABLE_ID_LENGTH, MAINTABLE_ID_PLAYCOUNT, MAINTABLE_ID_FILESIZE,
											  MAINTABLE_ID_TYPE, MAINTABLE_ID_ISPODCAST, MAINTABLE_ID_LOSSLESS
											 };
			for (size_t i = 0;i != elementsof(INT_IDS_ZEROOK);i++)
			{
				nde_field_t f = NDE_Scanner_GetFieldByID(pruneScanner, INT_IDS_ZEROOK[i]);
				if (f)
				{
					int s = NDE_IntegerField_GetValue(f);
					if (s < 0)
					{
						NDE_Scanner_DeleteField(pruneScanner, f);
						NDE_Scanner_Post(pruneScanner);

					}
				}
			}

			unsigned char INT_IDS[] = {MAINTABLE_ID_TRACKNB, MAINTABLE_ID_LASTUPDTIME, MAINTABLE_ID_LASTPLAY, MAINTABLE_ID_RATING,
			                           MAINTABLE_ID_FILETIME, MAINTABLE_ID_BITRATE, MAINTABLE_ID_DISC, MAINTABLE_ID_BPM, MAINTABLE_ID_DISCS,
									   MAINTABLE_ID_TRACKS, MAINTABLE_ID_PODCASTPUBDATE, MAINTABLE_ID_FILESIZE, MAINTABLE_ID_DATEADDED
			                          };
			for (size_t i = 0;i != elementsof(INT_IDS);i++)
			{
				nde_field_t f = NDE_Scanner_GetFieldByID(pruneScanner, INT_IDS[i]);
				if (f)
				{
					int s = NDE_IntegerField_GetValue(f);
					if (s <= 0)
					{
						NDE_Scanner_DeleteField(pruneScanner, f);
						NDE_Scanner_Post(pruneScanner);
					}
				}
			}
			NDE_Scanner_Next(pruneScanner);
			recordNum++;
		}

		NDE_Table_DestroyScanner(table, pruneScanner);
		NDE_Table_Sync(table);
	}

	NDE_Table_Compact(table, &progress[0]);
	assert(((Table *)table)->CheckIndexing());
	// now remove duplicates
	nde_scanner_t dupscanner = NDE_Table_CreateScanner(table);
	if (dupscanner)
	{
		int count = NDE_Scanner_GetRecordsCount(dupscanner);
		wchar_t **filenames = new wchar_t *[count];
		int i = 0;
		for (NDE_Scanner_First(dupscanner);!NDE_Scanner_EOF(dupscanner);NDE_Scanner_Next(dupscanner))
		{
			nde_field_t fileName = NDE_Scanner_GetFieldByID(dupscanner, MAINTABLE_ID_FILENAME);
			if (fileName)
			{
				filenames[i] = NDE_StringField_GetString(fileName);
				ndestring_retain(filenames[i]);
				i++;
			}
		}
		count = i;
		if (count)
		{
			qsort(filenames, count, sizeof(wchar_t *), sortFunc);
			for (int x = 0;x < (count - 1);x++)
			{
				int total = MulDiv(x, 100, count);
				progress[1] = total - 100;

				if (!_wcsicmp(filenames[x], filenames[x+1]))
				{
					wchar_t query[1024] = {0};
					wnsprintfW(query, 1024, L"filename == \"%s\"", filenames[x]);

					nde_scanner_t scanner = NDE_Table_CreateScanner(table);
					NDE_Scanner_Query(scanner, query);

					NDE_Scanner_First(scanner);
					NDE_Scanner_Next(scanner);
					while (!NDE_Scanner_EOF(scanner))
					{
						NDE_Scanner_Delete(scanner);
						NDE_Scanner_Post(scanner);
						NDE_Scanner_Next(scanner);
					}
					NDE_Table_DestroyScanner(table, scanner);
				}
				ndestring_release(filenames[x]);
				filenames[x]=0;
			}
		}
		delete[] filenames;
	}
	NDE_Table_DestroyScanner(table, dupscanner);
	NDE_Table_Sync(table);
	NDE_Table_Compact(table, &progress[1]);

	progress[0] = 666;
	WaitForSingleObject(compactThread, INFINITE);
	CloseHandle(compactThread);
}