infobox.c 11.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* in_flac - Winamp2 FLAC input plugin
 * Copyright (C) 2002,2003,2004  Josh Coalson
 *
 * 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
31
	char filename[MAX_PATH];
	FLAC_Plugin__CanonicalTag tag;
32
33
} LOCALDATA;

34
static char buffer[1024];
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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;
	}
}

/*
 *  Infobox helpers
 */

#define SetText(x,y)            WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, FLAC_plugin__canonical_get(&data->tag, y), -1, buffer, sizeof(buffer), NULL, NULL); \
                                SetDlgItemText(hwnd, x, buffer)

#define GetText(x,y)            GetDlgItemText(hwnd, x, buffer, sizeof(buffer));                        \
                                if (*buffer) FLAC_plugin__canonical_set_ansi(&data->tag, y, buffer);    \
                                else FLAC_plugin__canonical_remove_all(&data->tag, y)

#define SetTextW(x,y)           SetDlgItemTextW(hwnd, x, FLAC_plugin__canonical_get(&data->tag, y))

#define GetTextW(x,y)           GetDlgItemTextW(hwnd, x, (WCHAR*)buffer, sizeof(buffer)/2);                     \
                                if (*(WCHAR*)buffer) FLAC_plugin__canonical_set(&data->tag, y, (WCHAR*)buffer); \
                                else FLAC_plugin__canonical_remove_all(&data->tag, y)
193
194


195
static BOOL InitInfoboxInfo(HWND hwnd, const char *file)
196
{
197
	LOCALDATA *data = LocalAlloc(LPTR, sizeof(LOCALDATA));
198
	FLAC__StreamMetadata streaminfo;
199
200
201
202
203
204
205
206
207
208
	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;
209
	if (!FLAC__metadata_get_streaminfo(file, &streaminfo))
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
		return FALSE;
	ReadTags(file, &data->tag, false);

	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);
	rg  = FLAC_plugin__canonical_get(&data->tag, L"REPLAYGAIN_TRACK_GAIN") ? 1 : 0;
	rg |= FLAC_plugin__canonical_get(&data->tag, L"REPLAYGAIN_ALBUM_GAIN") ? 2 : 0;

	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)
	{
		SetTextW(IDC_TITLE,   L"TITLE");
		SetTextW(IDC_ARTIST,  L"ARTIST");
		SetTextW(IDC_ALBUM,   L"ALBUM");
234
		SetTextW(IDC_COMMENT, L"COMMENT");
235
236
237
238
239
240
241
242
243
		SetTextW(IDC_YEAR,    L"DATE");
		SetTextW(IDC_TRACK,   L"TRACKNUMBER");
		SetTextW(IDC_GENRE,   L"GENRE");
	}
	else
	{
		SetText(IDC_TITLE,   L"TITLE");
		SetText(IDC_ARTIST,  L"ARTIST");
		SetText(IDC_ALBUM,   L"ALBUM");
244
		SetText(IDC_COMMENT, L"COMMENT");
245
246
247
248
249
250
		SetText(IDC_YEAR,    L"DATE");
		SetText(IDC_TRACK,   L"TRACKNUMBER");
		SetText(IDC_GENRE,   L"GENRE");
	}

	return TRUE;
251
252
253
254
}

static void __inline SetTag(HWND hwnd, const char *filename, FLAC_Plugin__CanonicalTag *tag)
{
255
	strcpy(buffer, infoTitle);
256
257

	if (FLAC_plugin__vorbiscomment_set(filename, tag))
258
259
		strcat(buffer, " [Updated]");
	else strcat(buffer, " [Failed]");
260

261
	SetWindowText(hwnd, buffer);
262
263
264
265
}

static void UpdateTag(HWND hwnd)
{
266
267
268
269
270
271
272
273
	LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);

	/* get fields */
	if (isNT)
	{
		GetTextW(IDC_TITLE,   L"TITLE");
		GetTextW(IDC_ARTIST,  L"ARTIST");
		GetTextW(IDC_ALBUM,   L"ALBUM");
274
		GetTextW(IDC_COMMENT, L"COMMENT");
275
276
277
278
279
280
281
282
283
284
285
		GetTextW(IDC_YEAR,    L"DATE");
		GetTextW(IDC_TRACK,   L"TRACKNUMBER");
		GetTextW(IDC_GENRE,   L"GENRE");

		WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, FLAC_plugin__canonical_get(&data->tag, L"GENRE"), -1, buffer, sizeof(buffer), NULL, NULL);
	}
	else
	{
		GetText(IDC_TITLE,   L"TITLE");
		GetText(IDC_ARTIST,  L"ARTIST");
		GetText(IDC_ALBUM,   L"ALBUM");
286
		GetText(IDC_COMMENT, L"COMMENT");
287
288
289
290
291
292
293
294
295
296
		GetText(IDC_YEAR,    L"DATE");
		GetText(IDC_TRACK,   L"TRACKNUMBER");
		GetText(IDC_GENRE,   L"GENRE");
	}

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

	/* write tag */
	SetTag(hwnd, data->filename, &data->tag);
297
298
299
300
}

static void RemoveTag(HWND hwnd)
{
301
302
303
304
305
306
307
308
309
310
311
312
	LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
	FLAC_plugin__canonical_tag_clear(&data->tag);

	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,   "");

	SetTag(hwnd, data->filename, &data->tag);
313
314
315
316
317
}


static INT_PTR CALLBACK InfoProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
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
	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);
			FLAC_plugin__canonical_tag_clear(&data->tag);
			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;
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
/*
 *  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;
}

void ReadTags(const char *fileName, FLAC_Plugin__CanonicalTag *tag, BOOL forDisplay)
{
	FLAC_plugin__canonical_tag_init(tag);
	FLAC_plugin__vorbiscomment_get(fileName, tag, forDisplay ? flac_cfg.title.sep : NULL);

	/* add file name */
	if (forDisplay)
	{
		char *c;
		FLAC_plugin__canonical_set_ansi(tag, L"filepath", fileName);

		strcpy(buffer, GetFileName(fileName));
		if (c = strrchr(buffer, '.')) *c = 0;
		FLAC_plugin__canonical_set_ansi(tag, L"filename", buffer);
	}
}

/*
 *  Front-end
 */

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

void DeinitInfobox()
{
	DeinitGenres(NULL, true);
}
423
424
425

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