~ubuntu-branches/ubuntu/karmic/flac/karmic

« back to all changes in this revision

Viewing changes to src/plugin_winamp2/infobox.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2005-01-08 15:08:22 UTC
  • mto: (8.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050108150822-yvinilrafylazcyv
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* in_flac - Winamp2 FLAC input plugin
 
2
 * Copyright (C) 2002,2003,2004  Josh Coalson
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
17
 */
 
18
 
1
19
#include <windows.h>
2
20
#include <stdio.h>
3
21
#include "FLAC/all.h"
4
22
#include "plugin_common/all.h"
5
23
#include "infobox.h"
 
24
#include "config.h"
6
25
#include "resource.h"
7
26
 
8
27
 
9
28
typedef struct
10
29
{
11
 
    char filename[MAX_PATH];
 
30
        char filename[MAX_PATH];
 
31
        FLAC_Plugin__CanonicalTag tag;
12
32
} LOCALDATA;
13
33
 
14
 
static char buffer[256];
 
34
static char buffer[1024];
15
35
static char *genres = NULL;
16
 
static int  genresSize = 0, genresCount = 0, genresChanged = 0;
 
36
static DWORD genresSize = 0, genresCount = 0;
 
37
static BOOL genresChanged = FALSE, isNT;
17
38
 
18
39
static const char infoTitle[] = "FLAC File Info";
19
40
 
20
 
//fixme int64
21
 
static __inline DWORD FileSize(const char *file)
22
 
{
23
 
    HANDLE hFile = CreateFile(file, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
24
 
    DWORD res;
 
41
/*
 
42
 *  Genres
 
43
 */
25
44
 
26
 
    if (hFile == INVALID_HANDLE_VALUE) return 0;
27
 
    res = GetFileSize(hFile, 0);
28
 
    CloseHandle(hFile);
29
 
    return res;
30
 
}
 
45
/* TODO: write genres in utf-8 ? */
31
46
 
32
47
static __inline int GetGenresFileName(char *buffer, int size)
33
48
{
34
 
    char *c;
35
 
 
36
 
    if (!GetModuleFileName(NULL, buffer, size))
37
 
        return 0;
38
 
    c = strrchr(buffer, '\\');
39
 
    if (!c) return 0;
40
 
    strcpy(c+1, "genres.txt");
41
 
 
42
 
    return 1;
 
49
        char *c;
 
50
 
 
51
        if (!GetModuleFileName(NULL, buffer, size))
 
52
                return 0;
 
53
        c = strrchr(buffer, '\\');
 
54
        if (!c) return 0;
 
55
        strcpy(c+1, "genres.txt");
 
56
 
 
57
        return 1;
43
58
}
44
59
 
45
60
static void LoadGenres()
46
61
{
47
 
    HANDLE hFile;
48
 
    DWORD  spam;
49
 
    char  *c;
50
 
 
51
 
    if (!GetGenresFileName(buffer, sizeof(buffer))) return;
52
 
    // load file
53
 
    hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
54
 
    if (hFile == INVALID_HANDLE_VALUE) return;
55
 
    genresSize = GetFileSize(hFile, 0);
56
 
    if (!genresSize) return;
57
 
    genres = (char*)malloc(genresSize+2);
58
 
    if (!genres) return;
59
 
    if (!ReadFile(hFile, genres, genresSize, &spam, NULL))
60
 
    {
61
 
        free(genres);
62
 
        genres = NULL;
63
 
        return;
64
 
    }
65
 
    genres[genresSize] = 0;
66
 
    genres[genresSize+1] = 0;
67
 
    // replace newlines
68
 
    genresChanged = 0;
69
 
    genresCount = 1;
70
 
 
71
 
    for (c=genres; *c; c++)
72
 
    {
73
 
        if (*c == 10)
74
 
        {
75
 
            *c = 0;
76
 
            if (*(c+1))
77
 
                genresCount++;
78
 
            else genresSize--;
79
 
        }
80
 
    }
81
 
 
82
 
    CloseHandle(hFile);
 
62
        HANDLE hFile;
 
63
        DWORD  spam;
 
64
        char  *c;
 
65
 
 
66
        FLAC__ASSERT(0 != genres);
 
67
 
 
68
        if (!GetGenresFileName(buffer, sizeof(buffer))) return;
 
69
        /* load file */
 
70
        hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
71
        if (hFile == INVALID_HANDLE_VALUE) return;
 
72
        genresSize = GetFileSize(hFile, 0);
 
73
        if (genresSize && (genres = (char*)malloc(genresSize+2)))
 
74
        {
 
75
                if (!ReadFile(hFile, genres, genresSize, &spam, NULL) || spam!=genresSize)
 
76
                {
 
77
                        free(genres);
 
78
                        genres = NULL;
 
79
                }
 
80
                else
 
81
                {
 
82
                        genres[genresSize] = 0;
 
83
                        genres[genresSize+1] = 0;
 
84
                        /* replace newlines */
 
85
                        genresChanged = FALSE;
 
86
                        genresCount = 1;
 
87
 
 
88
                        for (c=genres; *c; c++)
 
89
                        {
 
90
                                if (*c == 10)
 
91
                                {
 
92
                                        *c = 0;
 
93
                                        if (*(c+1))
 
94
                                                genresCount++;
 
95
                                        else genresSize--;
 
96
                                }
 
97
                        }
 
98
                }
 
99
        }
 
100
 
 
101
        CloseHandle(hFile);
83
102
}
84
103
 
85
104
static void SaveGenres(HWND hlist)
86
105
{
87
 
    HANDLE hFile;
88
 
    DWORD  spam;
89
 
    int i, count, len;
90
 
 
91
 
    if (!GetGenresFileName(buffer, sizeof(buffer))) return;
92
 
    // write file
93
 
    hFile = CreateFile(buffer, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
94
 
    if (hFile == INVALID_HANDLE_VALUE) return;
95
 
 
96
 
    count = SendMessage(hlist, CB_GETCOUNT, 0, 0);
97
 
    for (i=0; i<count; i++)
98
 
    {
99
 
        SendMessage(hlist, CB_GETLBTEXT, i, (LPARAM)buffer);
100
 
        len = strlen(buffer);
101
 
        if (i != count-1)
102
 
        {
103
 
            buffer[len] = 10;
104
 
            len++;
105
 
        }
106
 
        WriteFile(hFile, buffer, len, &spam, NULL);
107
 
    }
108
 
 
109
 
    CloseHandle(hFile);
110
 
}
111
 
 
112
 
 
113
 
#define SetText(x,y)            SetDlgItemText(hwnd, x, y)
114
 
#define GetText(x,y)            (GetDlgItemText(hwnd, x, buffer, sizeof(buffer)), y = buffer[0] ? strdup(buffer) : 0)
115
 
 
116
 
static BOOL InitInfobox(HWND hwnd, const char *file)
117
 
{
118
 
    LOCALDATA *data = LocalAlloc(LPTR, sizeof(LOCALDATA));
 
106
        HANDLE hFile;
 
107
        DWORD  spam;
 
108
        int i, count, len;
 
109
 
 
110
        if (!GetGenresFileName(buffer, sizeof(buffer))) return;
 
111
        /* write file */
 
112
        hFile = CreateFile(buffer, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
113
        if (hFile == INVALID_HANDLE_VALUE) return;
 
114
 
 
115
        count = SendMessage(hlist, CB_GETCOUNT, 0, 0);
 
116
        for (i=0; i<count; i++)
 
117
        {
 
118
                SendMessage(hlist, CB_GETLBTEXT, i, (LPARAM)buffer);
 
119
                len = strlen(buffer);
 
120
                if (i != count-1)
 
121
                {
 
122
                        buffer[len] = 10;
 
123
                        len++;
 
124
                }
 
125
                WriteFile(hFile, buffer, len, &spam, NULL);
 
126
        }
 
127
 
 
128
        CloseHandle(hFile);
 
129
}
 
130
 
 
131
static void AddGenre(HWND hwnd, const char *genre)
 
132
{
 
133
        HWND hgen = GetDlgItem(hwnd, IDC_GENRE);
 
134
 
 
135
        if (SendMessage(hgen, CB_FINDSTRINGEXACT, -1, (LPARAM)genre) == CB_ERR)
 
136
        {
 
137
                genresChanged = TRUE;
 
138
                SendMessage(hgen, CB_ADDSTRING, 0, (LPARAM)genre);
 
139
        }
 
140
}
 
141
 
 
142
static void InitGenres(HWND hwnd)
 
143
{
 
144
        HWND hgen = GetDlgItem(hwnd, IDC_GENRE);
 
145
        char *c;
 
146
 
 
147
        /* set text length limit to 64 chars */
 
148
        SendMessage(hgen, CB_LIMITTEXT, 64, 0);
 
149
        /* try to load genres */
 
150
        if (!genres)
 
151
                LoadGenres(hgen);
 
152
        /* add the to list */
 
153
        if (genres)
 
154
        {
 
155
                SendMessage(hgen, CB_INITSTORAGE, genresCount, genresSize);
 
156
 
 
157
                for (c = genres; *c; c += strlen(c)+1)
 
158
                        SendMessage(hgen, CB_ADDSTRING, 0, (LPARAM)c);
 
159
        }
 
160
}
 
161
 
 
162
static void DeinitGenres(HWND hwnd, BOOL final)
 
163
{
 
164
        if (genresChanged && hwnd)
 
165
        {
 
166
                SaveGenres(GetDlgItem(hwnd, IDC_GENRE));
 
167
                genresChanged = FALSE;
 
168
                final = TRUE;
 
169
        }
 
170
        if (final)
 
171
        {
 
172
                free(genres);
 
173
                genres = 0;
 
174
        }
 
175
}
 
176
 
 
177
/*
 
178
 *  Infobox helpers
 
179
 */
 
180
 
 
181
#define SetText(x,y)            WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, FLAC_plugin__canonical_get(&data->tag, y), -1, buffer, sizeof(buffer), NULL, NULL); \
 
182
                                SetDlgItemText(hwnd, x, buffer)
 
183
 
 
184
#define GetText(x,y)            GetDlgItemText(hwnd, x, buffer, sizeof(buffer));                        \
 
185
                                if (*buffer) FLAC_plugin__canonical_set_ansi(&data->tag, y, buffer);    \
 
186
                                else FLAC_plugin__canonical_remove_all(&data->tag, y)
 
187
 
 
188
#define SetTextW(x,y)           SetDlgItemTextW(hwnd, x, FLAC_plugin__canonical_get(&data->tag, y))
 
189
 
 
190
#define GetTextW(x,y)           GetDlgItemTextW(hwnd, x, (WCHAR*)buffer, sizeof(buffer)/2);                     \
 
191
                                if (*(WCHAR*)buffer) FLAC_plugin__canonical_set(&data->tag, y, (WCHAR*)buffer); \
 
192
                                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));
119
198
        FLAC__StreamMetadata streaminfo;
120
 
    FLAC_Plugin__CanonicalTag tag;
121
 
    DWORD filesize, length, bps, ratio;
 
199
        DWORD    length, bps, ratio, rg;
 
200
        LONGLONG filesize;
122
201
 
123
 
    SetWindowLong(hwnd, GWL_USERDATA, (LONG)data);
124
 
    // file name
125
 
    strncpy(data->filename, file, sizeof(data->filename));
126
 
    SetDlgItemText(hwnd, IDC_NAME, file);
127
 
    // stream data
128
 
    filesize = FileSize(file);
129
 
    if (!filesize) return FALSE;
 
202
        SetWindowLong(hwnd, GWL_USERDATA, (LONG)data);
 
203
        /* file name */
 
204
        strncpy(data->filename, file, sizeof(data->filename));
 
205
        SetDlgItemText(hwnd, IDC_NAME, file);
 
206
        /* stream data and vorbis comment */
 
207
        filesize = FileSize(file);
 
208
        if (!filesize) return FALSE;
130
209
        if (!FLAC__metadata_get_streaminfo(file, &streaminfo))
131
 
        return FALSE;
132
 
 
133
 
    length = (DWORD)(streaminfo.data.stream_info.total_samples / streaminfo.data.stream_info.sample_rate);
134
 
    bps = (DWORD)(filesize / (125*streaminfo.data.stream_info.total_samples/streaminfo.data.stream_info.sample_rate));
135
 
    ratio = bps*1000000 / (streaminfo.data.stream_info.sample_rate*streaminfo.data.stream_info.channels*streaminfo.data.stream_info.bits_per_sample);
136
 
 
137
 
    sprintf(buffer, "Sample rate: %d Hz\nChannels: %d\nBits per sample: %d\nMin block size: %d\nMax block size: %d\n"
138
 
                    "File size: %d bytes\nTotal samples: %I64d\nLength: %d:%02d\nAvg. bitrate: %d\nCompression ratio: %d.%d%%\n",
139
 
        streaminfo.data.stream_info.sample_rate, streaminfo.data.stream_info.channels, streaminfo.data.stream_info.bits_per_sample,
140
 
        streaminfo.data.stream_info.min_blocksize, streaminfo.data.stream_info.max_blocksize, filesize, streaminfo.data.stream_info.total_samples,
141
 
        length/60, length%60, bps, ratio/10, ratio%10);
142
 
    //todo: replaygain
143
 
 
144
 
    SetDlgItemText(hwnd, IDC_INFO, buffer);
145
 
    // tag
146
 
        FLAC_plugin__canonical_tag_init(&tag);
147
 
        FLAC_plugin__canonical_tag_get_combined(file, &tag);
148
 
 
149
 
    SetText(IDC_TITLE,   tag.title);
150
 
    SetText(IDC_ARTIST,  tag.performer ? tag.performer : tag.composer);
151
 
    SetText(IDC_ALBUM,   tag.album);
152
 
    SetText(IDC_COMMENT, tag.comment);
153
 
    SetText(IDC_YEAR,    tag.year_recorded ? tag.year_recorded : tag.year_performed);
154
 
    SetText(IDC_TRACK,   tag.track_number);
155
 
    SetText(IDC_GENRE,   tag.genre);
156
 
 
157
 
        FLAC_plugin__canonical_tag_clear(&tag);
158
 
 
159
 
    return TRUE;
 
210
                return FALSE;
 
211
        ReadTags(file, &data->tag, false);
 
212
 
 
213
        length = (DWORD)(streaminfo.data.stream_info.total_samples / streaminfo.data.stream_info.sample_rate);
 
214
        bps = (DWORD)(filesize / (125*streaminfo.data.stream_info.total_samples/streaminfo.data.stream_info.sample_rate));
 
215
        ratio = bps*1000000 / (streaminfo.data.stream_info.sample_rate*streaminfo.data.stream_info.channels*streaminfo.data.stream_info.bits_per_sample);
 
216
        rg  = FLAC_plugin__canonical_get(&data->tag, L"REPLAYGAIN_TRACK_GAIN") ? 1 : 0;
 
217
        rg |= FLAC_plugin__canonical_get(&data->tag, L"REPLAYGAIN_ALBUM_GAIN") ? 2 : 0;
 
218
 
 
219
        sprintf(buffer, "Sample rate: %d Hz\nChannels: %d\nBits per sample: %d\nMin block size: %d\nMax block size: %d\n"
 
220
                        "File size: %I64d bytes\nTotal samples: %I64d\nLength: %d:%02d\nAvg. bitrate: %d\nCompression ratio: %d.%d%%\n"
 
221
                        "ReplayGain: %s\n",
 
222
            streaminfo.data.stream_info.sample_rate, streaminfo.data.stream_info.channels, streaminfo.data.stream_info.bits_per_sample,
 
223
            streaminfo.data.stream_info.min_blocksize, streaminfo.data.stream_info.max_blocksize, filesize, streaminfo.data.stream_info.total_samples,
 
224
            length/60, length%60, bps, ratio/10, ratio%10,
 
225
            rg==3 ? "track gain\nReplayGain: album gain" : rg==2 ? "album gain" : rg==1 ? "track gain" : "not present");
 
226
 
 
227
        SetDlgItemText(hwnd, IDC_INFO, buffer);
 
228
        /* tag */
 
229
        if (isNT)
 
230
        {
 
231
                SetTextW(IDC_TITLE,   L"TITLE");
 
232
                SetTextW(IDC_ARTIST,  L"ARTIST");
 
233
                SetTextW(IDC_ALBUM,   L"ALBUM");
 
234
                SetTextW(IDC_COMMENT, L"COMMENT");
 
235
                SetTextW(IDC_YEAR,    L"DATE");
 
236
                SetTextW(IDC_TRACK,   L"TRACKNUMBER");
 
237
                SetTextW(IDC_GENRE,   L"GENRE");
 
238
        }
 
239
        else
 
240
        {
 
241
                SetText(IDC_TITLE,   L"TITLE");
 
242
                SetText(IDC_ARTIST,  L"ARTIST");
 
243
                SetText(IDC_ALBUM,   L"ALBUM");
 
244
                SetText(IDC_COMMENT, L"COMMENT");
 
245
                SetText(IDC_YEAR,    L"DATE");
 
246
                SetText(IDC_TRACK,   L"TRACKNUMBER");
 
247
                SetText(IDC_GENRE,   L"GENRE");
 
248
        }
 
249
 
 
250
        return TRUE;
160
251
}
161
252
 
162
253
static void __inline SetTag(HWND hwnd, const char *filename, FLAC_Plugin__CanonicalTag *tag)
163
254
{
164
 
    strcpy(buffer, infoTitle);
 
255
        strcpy(buffer, infoTitle);
165
256
 
166
257
        if (FLAC_plugin__vorbiscomment_set(filename, tag))
167
 
        strcat(buffer, " [Updated]");
168
 
    else strcat(buffer, " [Failed]");
 
258
                strcat(buffer, " [Updated]");
 
259
        else strcat(buffer, " [Failed]");
169
260
 
170
 
    SetWindowText(hwnd, buffer);
 
261
        SetWindowText(hwnd, buffer);
171
262
}
172
263
 
173
264
static void UpdateTag(HWND hwnd)
174
265
{
175
 
    LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
176
 
    FLAC_Plugin__CanonicalTag tag;
177
 
        FLAC_plugin__canonical_tag_init(&tag);
178
 
 
179
 
    // get fields
180
 
    GetText(IDC_TITLE,   tag.title);
181
 
    GetText(IDC_ARTIST,  tag.composer);
182
 
    GetText(IDC_ALBUM,   tag.album);
183
 
    GetText(IDC_COMMENT, tag.comment);
184
 
    GetText(IDC_YEAR,    tag.year_recorded);
185
 
    GetText(IDC_TRACK,   tag.track_number);
186
 
    GetText(IDC_GENRE,   tag.genre);
187
 
 
188
 
    // update genres list
189
 
    if (tag.genre)
190
 
    {
191
 
        HWND hgen = GetDlgItem(hwnd, IDC_GENRE);
192
 
 
193
 
        if (SendMessage(hgen, CB_FINDSTRINGEXACT, -1, (LPARAM)tag.genre) == CB_ERR)
194
 
        {
195
 
            genresChanged = 1;
196
 
            SendMessage(hgen, CB_ADDSTRING, 0, (LPARAM)tag.genre);
197
 
        }
198
 
    }
199
 
 
200
 
    // write tag
201
 
    SetTag(hwnd, data->filename, &tag);
202
 
        FLAC_plugin__canonical_tag_clear(&tag);
 
266
        LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
 
267
 
 
268
        /* get fields */
 
269
        if (isNT)
 
270
        {
 
271
                GetTextW(IDC_TITLE,   L"TITLE");
 
272
                GetTextW(IDC_ARTIST,  L"ARTIST");
 
273
                GetTextW(IDC_ALBUM,   L"ALBUM");
 
274
                GetTextW(IDC_COMMENT, L"COMMENT");
 
275
                GetTextW(IDC_YEAR,    L"DATE");
 
276
                GetTextW(IDC_TRACK,   L"TRACKNUMBER");
 
277
                GetTextW(IDC_GENRE,   L"GENRE");
 
278
 
 
279
                WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, FLAC_plugin__canonical_get(&data->tag, L"GENRE"), -1, buffer, sizeof(buffer), NULL, NULL);
 
280
        }
 
281
        else
 
282
        {
 
283
                GetText(IDC_TITLE,   L"TITLE");
 
284
                GetText(IDC_ARTIST,  L"ARTIST");
 
285
                GetText(IDC_ALBUM,   L"ALBUM");
 
286
                GetText(IDC_COMMENT, L"COMMENT");
 
287
                GetText(IDC_YEAR,    L"DATE");
 
288
                GetText(IDC_TRACK,   L"TRACKNUMBER");
 
289
                GetText(IDC_GENRE,   L"GENRE");
 
290
        }
 
291
 
 
292
        /* update genres list (buffer should contain genre) */
 
293
        if (buffer[0]) AddGenre(hwnd, buffer);
 
294
 
 
295
        /* write tag */
 
296
        SetTag(hwnd, data->filename, &data->tag);
203
297
}
204
298
 
205
299
static void RemoveTag(HWND hwnd)
206
300
{
207
 
    LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
208
 
    FLAC_Plugin__CanonicalTag tag;
209
 
        FLAC_plugin__canonical_tag_init(&tag);
210
 
 
211
 
    SetText(IDC_TITLE,   "");
212
 
    SetText(IDC_ARTIST,  "");
213
 
    SetText(IDC_ALBUM,   "");
214
 
    SetText(IDC_COMMENT, "");
215
 
    SetText(IDC_YEAR,    "");
216
 
    SetText(IDC_TRACK,   "");
217
 
    SetText(IDC_GENRE,   "");
218
 
 
219
 
    SetTag(hwnd, data->filename, &tag);
 
301
        LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
 
302
        FLAC_plugin__canonical_tag_clear(&data->tag);
 
303
 
 
304
        SetDlgItemText(hwnd, IDC_TITLE,   "");
 
305
        SetDlgItemText(hwnd, IDC_ARTIST,  "");
 
306
        SetDlgItemText(hwnd, IDC_ALBUM,   "");
 
307
        SetDlgItemText(hwnd, IDC_COMMENT, "");
 
308
        SetDlgItemText(hwnd, IDC_YEAR,    "");
 
309
        SetDlgItemText(hwnd, IDC_TRACK,   "");
 
310
        SetDlgItemText(hwnd, IDC_GENRE,   "");
 
311
 
 
312
        SetTag(hwnd, data->filename, &data->tag);
220
313
}
221
314
 
222
315
 
223
316
static INT_PTR CALLBACK InfoProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
224
317
{
225
 
    switch (msg)
226
 
    {
227
 
    // init
228
 
    case WM_INITDIALOG:
229
 
        SetWindowText(hwnd, infoTitle);
230
 
        // init genres list
231
 
        {
232
 
            HWND hgen = GetDlgItem(hwnd, IDC_GENRE);
233
 
            char *c;
234
 
 
235
 
            // set text length limit to 64 chars
236
 
            SendMessage(hgen, CB_LIMITTEXT, 64, 0);
237
 
            // try to load genres
238
 
            if (!genres) LoadGenres(hgen);
239
 
            // add the to list
240
 
            if (genres)
241
 
            {
242
 
                SendMessage(hgen, CB_INITSTORAGE, genresCount, genresSize);
243
 
 
244
 
                for (c = genres; *c; c += strlen(c)+1)
245
 
                    SendMessage(hgen, CB_ADDSTRING, 0, (LPARAM)c);
246
 
            }
247
 
        }
248
 
        // init fields
249
 
        if (!InitInfobox(hwnd, (const char*)lParam))
250
 
            PostMessage(hwnd, WM_CLOSE, 0, 0);
251
 
        return TRUE;
252
 
    // destroy
253
 
    case WM_DESTROY:
254
 
        if (genresChanged)
255
 
        {
256
 
            SaveGenres(GetDlgItem(hwnd, IDC_GENRE));
257
 
            free(genres);
258
 
            genres = 0;
259
 
        }
260
 
        LocalFree((LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA));
261
 
        break;
262
 
    // commands
263
 
    case WM_COMMAND:
264
 
        switch (LOWORD(wParam))
265
 
        {
266
 
        // ok/cancel
267
 
        case IDOK:
268
 
        case IDCANCEL:
269
 
            EndDialog(hwnd, LOWORD(wParam));
270
 
            return TRUE;
271
 
        // save
272
 
        case IDC_UPDATE:
273
 
            UpdateTag(hwnd);
274
 
            break;
275
 
        // remove
276
 
        case IDC_REMOVE:
277
 
            RemoveTag(hwnd);
278
 
            break;
279
 
        }
280
 
        break;
281
 
    }
282
 
 
283
 
    return 0;
284
 
}
285
 
 
 
318
        switch (msg)
 
319
        {
 
320
        /* init */
 
321
        case WM_INITDIALOG:
 
322
                SetWindowText(hwnd, infoTitle);
 
323
                InitGenres(hwnd);
 
324
                /* init fields */
 
325
                if (!InitInfoboxInfo(hwnd, (const char*)lParam))
 
326
                        PostMessage(hwnd, WM_CLOSE, 0, 0);
 
327
                return TRUE;
 
328
        /* destroy */
 
329
        case WM_DESTROY:
 
330
                {
 
331
                        LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);
 
332
                        FLAC_plugin__canonical_tag_clear(&data->tag);
 
333
                        LocalFree(data);
 
334
                        DeinitGenres(hwnd, FALSE);
 
335
                }
 
336
                break;
 
337
        /* commands */
 
338
        case WM_COMMAND:
 
339
                switch (LOWORD(wParam))
 
340
                {
 
341
                /* ok/cancel */
 
342
                case IDOK:
 
343
                case IDCANCEL:
 
344
                        EndDialog(hwnd, LOWORD(wParam));
 
345
                        return TRUE;
 
346
                /* save */
 
347
                case IDC_UPDATE:
 
348
                        UpdateTag(hwnd);
 
349
                        break;
 
350
                /* remove */
 
351
                case IDC_REMOVE:
 
352
                        RemoveTag(hwnd);
 
353
                        break;
 
354
                }
 
355
                break;
 
356
        }
 
357
 
 
358
        return 0;
 
359
}
 
360
 
 
361
/*
 
362
 *  Helpers
 
363
 */
 
364
 
 
365
ULONGLONG FileSize(const char *fileName)
 
366
{
 
367
        LARGE_INTEGER res;
 
368
        HANDLE hFile = CreateFile(fileName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
369
 
 
370
        if (hFile == INVALID_HANDLE_VALUE) return 0;
 
371
        res.LowPart = GetFileSize(hFile, &res.HighPart);
 
372
        CloseHandle(hFile);
 
373
        return res.QuadPart;
 
374
}
 
375
 
 
376
static __inline char *GetFileName(const char *fullname)
 
377
{
 
378
        const char *c = fullname + strlen(fullname) - 1;
 
379
 
 
380
        while (c > fullname)
 
381
        {
 
382
                if (*c=='\\' || *c=='/')
 
383
                {
 
384
                        c++;
 
385
                        break;
 
386
                }
 
387
                c--;
 
388
        }
 
389
 
 
390
        return (char*)c;
 
391
}
 
392
 
 
393
void ReadTags(const char *fileName, FLAC_Plugin__CanonicalTag *tag, BOOL forDisplay)
 
394
{
 
395
        FLAC_plugin__canonical_tag_init(tag);
 
396
        FLAC_plugin__vorbiscomment_get(fileName, tag, forDisplay ? flac_cfg.title.sep : NULL);
 
397
 
 
398
        /* add file name */
 
399
        if (forDisplay)
 
400
        {
 
401
                char *c;
 
402
                FLAC_plugin__canonical_set_ansi(tag, L"filepath", fileName);
 
403
 
 
404
                strcpy(buffer, GetFileName(fileName));
 
405
                if (c = strrchr(buffer, '.')) *c = 0;
 
406
                FLAC_plugin__canonical_set_ansi(tag, L"filename", buffer);
 
407
        }
 
408
}
 
409
 
 
410
/*
 
411
 *  Front-end
 
412
 */
 
413
 
 
414
void InitInfobox()
 
415
{
 
416
        isNT = !(GetVersion() & 0x80000000);
 
417
}
 
418
 
 
419
void DeinitInfobox()
 
420
{
 
421
        DeinitGenres(NULL, true);
 
422
}
286
423
 
287
424
void DoInfoBox(HINSTANCE inst, HWND hwnd, const char *filename)
288
425
{
289
 
    DialogBoxParam(inst, MAKEINTRESOURCE(IDD_INFOBOX), hwnd, InfoProc, (LONG)filename);
 
426
        DialogBoxParam(inst, MAKEINTRESOURCE(IDD_INFOBOX), hwnd, InfoProc, (LONG)filename);
290
427
}