infobox.c 11.9 KB
Newer Older
1
/* in_flac - Winamp2 FLAC input plugin
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2002,2003,2004,2005  Josh Coalson
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

19
20
21
22
23
#include <windows.h>
#include <stdio.h>
#include "FLAC/all.h"
#include "plugin_common/all.h"
#include "infobox.h"
24
#include "config.h"
25
26
27
28
29
#include "resource.h"


typedef struct
{
30
	char filename[MAX_PATH];
31
	FLAC__StreamMetadata tags;
32
33
} LOCALDATA;

34
static char buffer[8192];
35
static char *genres = NULL;
36
37
static DWORD genresSize = 0, genresCount = 0;
static BOOL genresChanged = FALSE, isNT;
38
39
40

static const char infoTitle[] = "FLAC File Info";

41
42
43
/*
 *  Genres
 */
44

45
/* TODO: write genres in utf-8 ? */
46
47
48

static __inline int GetGenresFileName(char *buffer, int size)
{
49
	char *c;
50

51
52
53
54
55
	if (!GetModuleFileName(NULL, buffer, size))
		return 0;
	c = strrchr(buffer, '\\');
	if (!c) return 0;
	strcpy(c+1, "genres.txt");
56

57
	return 1;
58
59
60
61
}

static void LoadGenres()
{
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
	HANDLE hFile;
	DWORD  spam;
	char  *c;

	FLAC__ASSERT(0 != genres);

	if (!GetGenresFileName(buffer, sizeof(buffer))) return;
	/* load file */
	hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE) return;
	genresSize = GetFileSize(hFile, 0);
	if (genresSize && (genres = (char*)malloc(genresSize+2)))
	{
		if (!ReadFile(hFile, genres, genresSize, &spam, NULL) || spam!=genresSize)
		{
			free(genres);
			genres = NULL;
		}
		else
		{
			genres[genresSize] = 0;
			genres[genresSize+1] = 0;
			/* replace newlines */
			genresChanged = FALSE;
			genresCount = 1;

			for (c=genres; *c; c++)
			{
				if (*c == 10)
				{
					*c = 0;
					if (*(c+1))
						genresCount++;
					else genresSize--;
				}
			}
		}
	}

	CloseHandle(hFile);
102
103
104
105
}

static void SaveGenres(HWND hlist)
{
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	HANDLE hFile;
	DWORD  spam;
	int i, count, len;

	if (!GetGenresFileName(buffer, sizeof(buffer))) return;
	/* write file */
	hFile = CreateFile(buffer, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE) return;

	count = SendMessage(hlist, CB_GETCOUNT, 0, 0);
	for (i=0; i<count; i++)
	{
		SendMessage(hlist, CB_GETLBTEXT, i, (LPARAM)buffer);
		len = strlen(buffer);
		if (i != count-1)
		{
			buffer[len] = 10;
			len++;
		}
		WriteFile(hFile, buffer, len, &spam, NULL);
	}

	CloseHandle(hFile);
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
static void AddGenre(HWND hwnd, const char *genre)
{
	HWND hgen = GetDlgItem(hwnd, IDC_GENRE);

	if (SendMessage(hgen, CB_FINDSTRINGEXACT, -1, (LPARAM)genre) == CB_ERR)
	{
		genresChanged = TRUE;
		SendMessage(hgen, CB_ADDSTRING, 0, (LPARAM)genre);
	}
}

static void InitGenres(HWND hwnd)
{
	HWND hgen = GetDlgItem(hwnd, IDC_GENRE);
	char *c;

	/* set text length limit to 64 chars */
	SendMessage(hgen, CB_LIMITTEXT, 64, 0);
	/* try to load genres */
	if (!genres)
		LoadGenres(hgen);
	/* add the to list */
	if (genres)
	{
		SendMessage(hgen, CB_INITSTORAGE, genresCount, genresSize);

		for (c = genres; *c; c += strlen(c)+1)
			SendMessage(hgen, CB_ADDSTRING, 0, (LPARAM)c);
	}
}

static void DeinitGenres(HWND hwnd, BOOL final)
{
	if (genresChanged && hwnd)
	{
		SaveGenres(GetDlgItem(hwnd, IDC_GENRE));
		genresChanged = FALSE;
		final = TRUE;
	}
	if (final)
	{
		free(genres);
		genres = 0;
	}
}

177
178
179
180
181
182
183
184
185
static wchar_t *AnsiToWide(const char *src)
{
	int len;
	wchar_t *dest;

	FLAC__ASSERT(0 != src);

	len = strlen(src) + 1;
	/* copy */
186
	dest = (wchar_t*)malloc(len*sizeof(wchar_t));
187
188
189
190
	if (dest) mbstowcs(dest, src, len);
	return dest;
}

191
192
193
194
/*
 *  Infobox helpers
 */

195
196
197
#define SetText(x,y)            ucs2 = FLAC_plugin__tags_get_tag_ucs2(data->tags, y); \
                                WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, ucs2, -1, buffer, sizeof(buffer), NULL, NULL); \
                                if(ucs2) free(ucs2); \
198
199
200
                                SetDlgItemText(hwnd, x, buffer)

#define GetText(x,y)            GetDlgItemText(hwnd, x, buffer, sizeof(buffer));                        \
201
202
                                if (*buffer) { ucs2 = AnsiToWide(buffer); FLAC_plugin__tags_set_tag_ucs2(data->tags, y, ucs2, /*replace_all=*/false); free(ucs2); } \
                                else FLAC_plugin__tags_delete_tag(data->tags, y)
203

204
205
206
#define SetTextW(x,y)           ucs2 = FLAC_plugin__tags_get_tag_ucs2(data->tags, y)); \
                                SetDlgItemTextW(hwnd, x, ucs2); \
                                free(ucs2)
207
208

#define GetTextW(x,y)           GetDlgItemTextW(hwnd, x, (WCHAR*)buffer, sizeof(buffer)/2);                     \
209
210
                                if (*(WCHAR*)buffer) FLAC_plugin__tags_set_tag_ucs2(data->tags, y, (WCHAR*)buffer, /*replace_all=*/false); \
                                else FLAC_plugin__tags_delete_tag(data->tags, y)
211
212


213
static BOOL InitInfoboxInfo(HWND hwnd, const char *file)
214
{
215
	LOCALDATA *data = LocalAlloc(LPTR, sizeof(LOCALDATA));
216
	wchar_t *ucs2;
217
	FLAC__StreamMetadata streaminfo;
218
219
220
221
222
223
224
225
226
227
	DWORD    length, bps, ratio, rg;
	LONGLONG filesize;

	SetWindowLong(hwnd, GWL_USERDATA, (LONG)data);
	/* file name */
	strncpy(data->filename, file, sizeof(data->filename));
	SetDlgItemText(hwnd, IDC_NAME, file);
	/* stream data and vorbis comment */
	filesize = FileSize(file);
	if (!filesize) return FALSE;
228
	if (!FLAC__metadata_get_streaminfo(file, &streaminfo))
229
		return FALSE;
230
	ReadTags(file, &data->tags, false);
231
232
233
234

	length = (DWORD)(streaminfo.data.stream_info.total_samples / streaminfo.data.stream_info.sample_rate);
	bps = (DWORD)(filesize / (125*streaminfo.data.stream_info.total_samples/streaminfo.data.stream_info.sample_rate));
	ratio = bps*1000000 / (streaminfo.data.stream_info.sample_rate*streaminfo.data.stream_info.channels*streaminfo.data.stream_info.bits_per_sample);
235
236
	rg  = FLAC_plugin__tags_get_tag_utf8(data->tags, L"REPLAYGAIN_TRACK_GAIN") ? 1 : 0;
	rg |= FLAC_plugin__tags_get_tag_utf8(data->tags, L"REPLAYGAIN_ALBUM_GAIN") ? 2 : 0;
237
238
239
240
241
242
243
244
245
246
247
248
249

	sprintf(buffer, "Sample rate: %d Hz\nChannels: %d\nBits per sample: %d\nMin block size: %d\nMax block size: %d\n"
	                "File size: %I64d bytes\nTotal samples: %I64d\nLength: %d:%02d\nAvg. bitrate: %d\nCompression ratio: %d.%d%%\n"
	                "ReplayGain: %s\n",
	    streaminfo.data.stream_info.sample_rate, streaminfo.data.stream_info.channels, streaminfo.data.stream_info.bits_per_sample,
	    streaminfo.data.stream_info.min_blocksize, streaminfo.data.stream_info.max_blocksize, filesize, streaminfo.data.stream_info.total_samples,
	    length/60, length%60, bps, ratio/10, ratio%10,
	    rg==3 ? "track gain\nReplayGain: album gain" : rg==2 ? "album gain" : rg==1 ? "track gain" : "not present");

	SetDlgItemText(hwnd, IDC_INFO, buffer);
	/* tag */
	if (isNT)
	{
250
251
252
253
254
255
256
		SetTextW(IDC_TITLE,   "TITLE");
		SetTextW(IDC_ARTIST,  "ARTIST");
		SetTextW(IDC_ALBUM,   "ALBUM");
		SetTextW(IDC_COMMENT, "COMMENT");
		SetTextW(IDC_YEAR,    "DATE");
		SetTextW(IDC_TRACK,   "TRACKNUMBER");
		SetTextW(IDC_GENRE,   "GENRE");
257
258
259
	}
	else
	{
260
261
262
263
264
265
266
		SetText(IDC_TITLE,   "TITLE");
		SetText(IDC_ARTIST,  "ARTIST");
		SetText(IDC_ALBUM,   "ALBUM");
		SetText(IDC_COMMENT, "COMMENT");
		SetText(IDC_YEAR,    "DATE");
		SetText(IDC_TRACK,   "TRACKNUMBER");
		SetText(IDC_GENRE,   "GENRE");
267
268
269
	}

	return TRUE;
270
271
}

272
static void __inline SetTag(HWND hwnd, const char *filename, FLAC__StreamMetadata *tags)
273
{
274
	strcpy(buffer, infoTitle);
275

276
	if (FLAC_plugin__tags_set(filename, tags))
277
278
		strcat(buffer, " [Updated]");
	else strcat(buffer, " [Failed]");
279

280
	SetWindowText(hwnd, buffer);
281
282
283
284
}

static void UpdateTag(HWND hwnd)
{
285
	LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
286
	wchar_t *ucs2;
287
288
289
290

	/* get fields */
	if (isNT)
	{
291
292
293
294
295
296
297
298
299
300
301
		GetTextW(IDC_TITLE,   "TITLE");
		GetTextW(IDC_ARTIST,  "ARTIST");
		GetTextW(IDC_ALBUM,   "ALBUM");
		GetTextW(IDC_COMMENT, "COMMENT");
		GetTextW(IDC_YEAR,    "DATE");
		GetTextW(IDC_TRACK,   "TRACKNUMBER");
		GetTextW(IDC_GENRE,   "GENRE");

		ucs2 = FLAC_plugin__tags_get_tag_ucs2(data->tags, "GENRE");
		WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, ucs2, -1, buffer, sizeof(buffer), NULL, NULL);
		free(ucs2);
302
303
304
	}
	else
	{
305
306
307
308
309
310
311
		GetText(IDC_TITLE,   "TITLE");
		GetText(IDC_ARTIST,  "ARTIST");
		GetText(IDC_ALBUM,   "ALBUM");
		GetText(IDC_COMMENT, "COMMENT");
		GetText(IDC_YEAR,    "DATE");
		GetText(IDC_TRACK,   "TRACKNUMBER");
		GetText(IDC_GENRE,   "GENRE");
312
313
314
315
316
317
	}

	/* update genres list (buffer should contain genre) */
	if (buffer[0]) AddGenre(hwnd, buffer);

	/* write tag */
318
	SetTag(hwnd, data->filename, data->tags);
319
320
321
322
}

static void RemoveTag(HWND hwnd)
{
323
	LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
324
	FLAC_plugin__tags_delete_all(data->tags);
325
326
327
328
329
330
331
332
333

	SetDlgItemText(hwnd, IDC_TITLE,   "");
	SetDlgItemText(hwnd, IDC_ARTIST,  "");
	SetDlgItemText(hwnd, IDC_ALBUM,   "");
	SetDlgItemText(hwnd, IDC_COMMENT, "");
	SetDlgItemText(hwnd, IDC_YEAR,    "");
	SetDlgItemText(hwnd, IDC_TRACK,   "");
	SetDlgItemText(hwnd, IDC_GENRE,   "");

334
	SetTag(hwnd, data->filename, data->tags);
335
336
337
338
339
}


static INT_PTR CALLBACK InfoProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	switch (msg)
	{
	/* init */
	case WM_INITDIALOG:
		SetWindowText(hwnd, infoTitle);
		InitGenres(hwnd);
		/* init fields */
		if (!InitInfoboxInfo(hwnd, (const char*)lParam))
			PostMessage(hwnd, WM_CLOSE, 0, 0);
		return TRUE;
	/* destroy */
	case WM_DESTROY:
		{
			LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
354
			FLAC_plugin__tags_destroy(&data->tags);
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
			LocalFree(data);
			DeinitGenres(hwnd, FALSE);
		}
		break;
	/* commands */
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		/* ok/cancel */
		case IDOK:
		case IDCANCEL:
			EndDialog(hwnd, LOWORD(wParam));
			return TRUE;
		/* save */
		case IDC_UPDATE:
			UpdateTag(hwnd);
			break;
		/* remove */
		case IDC_REMOVE:
			RemoveTag(hwnd);
			break;
		}
		break;
	}

	return 0;
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
/*
 *  Helpers
 */

ULONGLONG FileSize(const char *fileName)
{
	LARGE_INTEGER res;
	HANDLE hFile = CreateFile(fileName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE) return 0;
	res.LowPart = GetFileSize(hFile, &res.HighPart);
	CloseHandle(hFile);
	return res.QuadPart;
}

static __inline char *GetFileName(const char *fullname)
{
	const char *c = fullname + strlen(fullname) - 1;

	while (c > fullname)
	{
		if (*c=='\\' || *c=='/')
		{
			c++;
			break;
		}
		c--;
	}

	return (char*)c;
}

415
void ReadTags(const char *fileName, FLAC__StreamMetadata **tags, BOOL forDisplay)
416
{
417
	if(FLAC_plugin__tags_get(fileName, tags, forDisplay ? flac_cfg.title.sep : NULL)) {
418

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
		/* add file name */
		if (forDisplay)
		{
			char *c;
			wchar_t *ucs2;
			ucs2 = AnsiToWide(fileName);
			FLAC_plugin__tags_set_tag_ucs2(*tags, "filepath", ucs2);
			free(ucs2);

			strcpy(buffer, GetFileName(fileName));
			if (c = strrchr(buffer, '.')) *c = 0;
			ucs2 = AnsiToWide(buffer);
			FLAC_plugin__tags_set_tag_ucs2(*tags, "filename", ucs2);
			free(ucs2);
		}
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
	}
}

/*
 *  Front-end
 */

void InitInfobox()
{
	isNT = !(GetVersion() & 0x80000000);
}

void DeinitInfobox()
{
	DeinitGenres(NULL, true);
}
450
451
452

void DoInfoBox(HINSTANCE inst, HWND hwnd, const char *filename)
{
453
	DialogBoxParam(inst, MAKEINTRESOURCE(IDD_INFOBOX), hwnd, InfoProc, (LONG)filename);
454
}