add 2007 copyright
[flac.git] / src / plugin_winamp2 / infobox.c
index 26a0d4b..c4b8034 100644 (file)
@@ -1,37 +1,41 @@
 /* in_flac - Winamp2 FLAC input plugin\r
- * Copyright (C) 2002,2003,2004  Josh Coalson\r
+ * Copyright (C) 2002,2003,2004,2005,2006,2007  Josh Coalson\r
  *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
  *\r
- * This program is distributed in the hope that it will be useful,\r
+ * This library is distributed in the hope that it will be useful,\r
  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
  *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
  */\r
 \r
+#if HAVE_CONFIG_H\r
+#  include <config.h>\r
+#endif\r
+\r
 #include <windows.h>\r
 #include <stdio.h>\r
 #include "FLAC/all.h"\r
 #include "plugin_common/all.h"\r
 #include "infobox.h"\r
-#include "config.h"\r
+#include "configure.h"\r
 #include "resource.h"\r
 \r
 \r
 typedef struct\r
 {\r
        char filename[MAX_PATH];\r
-       FLAC_Plugin__CanonicalTag tag;\r
+       FLAC__StreamMetadata *tags;\r
 } LOCALDATA;\r
 \r
-static char buffer[1024];\r
+static char buffer[8192];\r
 static char *genres = NULL;\r
 static DWORD genresSize = 0, genresCount = 0;\r
 static BOOL genresChanged = FALSE, isNT;\r
@@ -174,27 +178,46 @@ static void DeinitGenres(HWND hwnd, BOOL final)
        }\r
 }\r
 \r
+static wchar_t *AnsiToWide(const char *src)\r
+{\r
+       int len;\r
+       wchar_t *dest;\r
+\r
+       FLAC__ASSERT(0 != src);\r
+\r
+       len = strlen(src) + 1;\r
+       /* copy */\r
+       dest = (wchar_t*)malloc(len*sizeof(wchar_t));\r
+       if (dest) mbstowcs(dest, src, len);\r
+       return dest;\r
+}\r
+\r
 /*\r
  *  Infobox helpers\r
  */\r
 \r
-#define SetText(x,y)            WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, FLAC_plugin__canonical_get(&data->tag, y), -1, buffer, sizeof(buffer), NULL, NULL); \\r
+#define SetText(x,y)            ucs2 = FLAC_plugin__tags_get_tag_ucs2(data->tags, y); \\r
+                                WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, ucs2, -1, buffer, sizeof(buffer), NULL, NULL); \\r
+                                if(ucs2) free(ucs2); \\r
                                 SetDlgItemText(hwnd, x, buffer)\r
 \r
 #define GetText(x,y)            GetDlgItemText(hwnd, x, buffer, sizeof(buffer));                        \\r
-                                if (*buffer) FLAC_plugin__canonical_set_ansi(&data->tag, y, buffer);    \\r
-                                else FLAC_plugin__canonical_remove_all(&data->tag, y)\r
+                                if (*buffer) { ucs2 = AnsiToWide(buffer); FLAC_plugin__tags_set_tag_ucs2(data->tags, y, ucs2, /*replace_all=*/false); free(ucs2); } \\r
+                                else FLAC_plugin__tags_delete_tag(data->tags, y)\r
 \r
-#define SetTextW(x,y)           SetDlgItemTextW(hwnd, x, FLAC_plugin__canonical_get(&data->tag, y))\r
+#define SetTextW(x,y)           ucs2 = FLAC_plugin__tags_get_tag_ucs2(data->tags, y); \\r
+                                SetDlgItemTextW(hwnd, x, ucs2); \\r
+                                free(ucs2)\r
 \r
 #define GetTextW(x,y)           GetDlgItemTextW(hwnd, x, (WCHAR*)buffer, sizeof(buffer)/2);                     \\r
-                                if (*(WCHAR*)buffer) FLAC_plugin__canonical_set(&data->tag, y, (WCHAR*)buffer); \\r
-                                else FLAC_plugin__canonical_remove_all(&data->tag, y)\r
+                                if (*(WCHAR*)buffer) FLAC_plugin__tags_set_tag_ucs2(data->tags, y, (WCHAR*)buffer, /*replace_all=*/false); \\r
+                                else FLAC_plugin__tags_delete_tag(data->tags, y)\r
 \r
 \r
 static BOOL InitInfoboxInfo(HWND hwnd, const char *file)\r
 {\r
        LOCALDATA *data = LocalAlloc(LPTR, sizeof(LOCALDATA));\r
+       wchar_t *ucs2;\r
        FLAC__StreamMetadata streaminfo;\r
        DWORD    length, bps, ratio, rg;\r
        LONGLONG filesize;\r
@@ -208,13 +231,13 @@ static BOOL InitInfoboxInfo(HWND hwnd, const char *file)
        if (!filesize) return FALSE;\r
        if (!FLAC__metadata_get_streaminfo(file, &streaminfo))\r
                return FALSE;\r
-       ReadTags(file, &data->tag, false);\r
+       ReadTags(file, &data->tags, false);\r
 \r
        length = (DWORD)(streaminfo.data.stream_info.total_samples / streaminfo.data.stream_info.sample_rate);\r
        bps = (DWORD)(filesize / (125*streaminfo.data.stream_info.total_samples/streaminfo.data.stream_info.sample_rate));\r
        ratio = bps*1000000 / (streaminfo.data.stream_info.sample_rate*streaminfo.data.stream_info.channels*streaminfo.data.stream_info.bits_per_sample);\r
-       rg  = FLAC_plugin__canonical_get(&data->tag, L"REPLAYGAIN_TRACK_GAIN") ? 1 : 0;\r
-       rg |= FLAC_plugin__canonical_get(&data->tag, L"REPLAYGAIN_ALBUM_GAIN") ? 2 : 0;\r
+       rg  = FLAC_plugin__tags_get_tag_utf8(data->tags, "REPLAYGAIN_TRACK_GAIN") ? 1 : 0;\r
+       rg |= FLAC_plugin__tags_get_tag_utf8(data->tags, "REPLAYGAIN_ALBUM_GAIN") ? 2 : 0;\r
 \r
        sprintf(buffer, "Sample rate: %d Hz\nChannels: %d\nBits per sample: %d\nMin block size: %d\nMax block size: %d\n"\r
                        "File size: %I64d bytes\nTotal samples: %I64d\nLength: %d:%02d\nAvg. bitrate: %d\nCompression ratio: %d.%d%%\n"\r
@@ -228,33 +251,33 @@ static BOOL InitInfoboxInfo(HWND hwnd, const char *file)
        /* tag */\r
        if (isNT)\r
        {\r
-               SetTextW(IDC_TITLE,   L"TITLE");\r
-               SetTextW(IDC_ARTIST,  L"ARTIST");\r
-               SetTextW(IDC_ALBUM,   L"ALBUM");\r
-               SetTextW(IDC_COMMENT, L"COMMENT");\r
-               SetTextW(IDC_YEAR,    L"DATE");\r
-               SetTextW(IDC_TRACK,   L"TRACKNUMBER");\r
-               SetTextW(IDC_GENRE,   L"GENRE");\r
+               SetTextW(IDC_TITLE,   "TITLE");\r
+               SetTextW(IDC_ARTIST,  "ARTIST");\r
+               SetTextW(IDC_ALBUM,   "ALBUM");\r
+               SetTextW(IDC_COMMENT, "COMMENT");\r
+               SetTextW(IDC_YEAR,    "DATE");\r
+               SetTextW(IDC_TRACK,   "TRACKNUMBER");\r
+               SetTextW(IDC_GENRE,   "GENRE");\r
        }\r
        else\r
        {\r
-               SetText(IDC_TITLE,   L"TITLE");\r
-               SetText(IDC_ARTIST,  L"ARTIST");\r
-               SetText(IDC_ALBUM,   L"ALBUM");\r
-               SetText(IDC_COMMENT, L"COMMENT");\r
-               SetText(IDC_YEAR,    L"DATE");\r
-               SetText(IDC_TRACK,   L"TRACKNUMBER");\r
-               SetText(IDC_GENRE,   L"GENRE");\r
+               SetText(IDC_TITLE,   "TITLE");\r
+               SetText(IDC_ARTIST,  "ARTIST");\r
+               SetText(IDC_ALBUM,   "ALBUM");\r
+               SetText(IDC_COMMENT, "COMMENT");\r
+               SetText(IDC_YEAR,    "DATE");\r
+               SetText(IDC_TRACK,   "TRACKNUMBER");\r
+               SetText(IDC_GENRE,   "GENRE");\r
        }\r
 \r
        return TRUE;\r
 }\r
 \r
-static void __inline SetTag(HWND hwnd, const char *filename, FLAC_Plugin__CanonicalTag *tag)\r
+static void __inline SetTag(HWND hwnd, const char *filename, FLAC__StreamMetadata *tags)\r
 {\r
        strcpy(buffer, infoTitle);\r
 \r
-       if (FLAC_plugin__vorbiscomment_set(filename, tag))\r
+       if (FLAC_plugin__tags_set(filename, tags))\r
                strcat(buffer, " [Updated]");\r
        else strcat(buffer, " [Failed]");\r
 \r
@@ -264,42 +287,45 @@ static void __inline SetTag(HWND hwnd, const char *filename, FLAC_Plugin__Canoni
 static void UpdateTag(HWND hwnd)\r
 {\r
        LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);\r
+       wchar_t *ucs2;\r
 \r
        /* get fields */\r
        if (isNT)\r
        {\r
-               GetTextW(IDC_TITLE,   L"TITLE");\r
-               GetTextW(IDC_ARTIST,  L"ARTIST");\r
-               GetTextW(IDC_ALBUM,   L"ALBUM");\r
-               GetTextW(IDC_COMMENT, L"COMMENT");\r
-               GetTextW(IDC_YEAR,    L"DATE");\r
-               GetTextW(IDC_TRACK,   L"TRACKNUMBER");\r
-               GetTextW(IDC_GENRE,   L"GENRE");\r
-\r
-               WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, FLAC_plugin__canonical_get(&data->tag, L"GENRE"), -1, buffer, sizeof(buffer), NULL, NULL);\r
+               GetTextW(IDC_TITLE,   "TITLE");\r
+               GetTextW(IDC_ARTIST,  "ARTIST");\r
+               GetTextW(IDC_ALBUM,   "ALBUM");\r
+               GetTextW(IDC_COMMENT, "COMMENT");\r
+               GetTextW(IDC_YEAR,    "DATE");\r
+               GetTextW(IDC_TRACK,   "TRACKNUMBER");\r
+               GetTextW(IDC_GENRE,   "GENRE");\r
+\r
+               ucs2 = FLAC_plugin__tags_get_tag_ucs2(data->tags, "GENRE");\r
+               WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, ucs2, -1, buffer, sizeof(buffer), NULL, NULL);\r
+               free(ucs2);\r
        }\r
        else\r
        {\r
-               GetText(IDC_TITLE,   L"TITLE");\r
-               GetText(IDC_ARTIST,  L"ARTIST");\r
-               GetText(IDC_ALBUM,   L"ALBUM");\r
-               GetText(IDC_COMMENT, L"COMMENT");\r
-               GetText(IDC_YEAR,    L"DATE");\r
-               GetText(IDC_TRACK,   L"TRACKNUMBER");\r
-               GetText(IDC_GENRE,   L"GENRE");\r
+               GetText(IDC_TITLE,   "TITLE");\r
+               GetText(IDC_ARTIST,  "ARTIST");\r
+               GetText(IDC_ALBUM,   "ALBUM");\r
+               GetText(IDC_COMMENT, "COMMENT");\r
+               GetText(IDC_YEAR,    "DATE");\r
+               GetText(IDC_TRACK,   "TRACKNUMBER");\r
+               GetText(IDC_GENRE,   "GENRE");\r
        }\r
 \r
        /* update genres list (buffer should contain genre) */\r
        if (buffer[0]) AddGenre(hwnd, buffer);\r
 \r
        /* write tag */\r
-       SetTag(hwnd, data->filename, &data->tag);\r
+       SetTag(hwnd, data->filename, data->tags);\r
 }\r
 \r
 static void RemoveTag(HWND hwnd)\r
 {\r
        LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);\r
-       FLAC_plugin__canonical_tag_clear(&data->tag);\r
+       FLAC_plugin__tags_delete_all(data->tags);\r
 \r
        SetDlgItemText(hwnd, IDC_TITLE,   "");\r
        SetDlgItemText(hwnd, IDC_ARTIST,  "");\r
@@ -309,7 +335,7 @@ static void RemoveTag(HWND hwnd)
        SetDlgItemText(hwnd, IDC_TRACK,   "");\r
        SetDlgItemText(hwnd, IDC_GENRE,   "");\r
 \r
-       SetTag(hwnd, data->filename, &data->tag);\r
+       SetTag(hwnd, data->filename, data->tags);\r
 }\r
 \r
 \r
@@ -329,7 +355,7 @@ static INT_PTR CALLBACK InfoProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPar
        case WM_DESTROY:\r
                {\r
                        LOCALDATA *data = (LOCALDATA*)GetWindowLong(hwnd, GWL_USERDATA);\r
-                       FLAC_plugin__canonical_tag_clear(&data->tag);\r
+                       FLAC_plugin__tags_destroy(&data->tags);\r
                        LocalFree(data);\r
                        DeinitGenres(hwnd, FALSE);\r
                }\r
@@ -390,20 +416,25 @@ static __inline char *GetFileName(const char *fullname)
        return (char*)c;\r
 }\r
 \r
-void ReadTags(const char *fileName, FLAC_Plugin__CanonicalTag *tag, BOOL forDisplay)\r
+void ReadTags(const char *fileName, FLAC__StreamMetadata **tags, BOOL forDisplay)\r
 {\r
-       FLAC_plugin__canonical_tag_init(tag);\r
-       FLAC_plugin__vorbiscomment_get(fileName, tag, forDisplay ? flac_cfg.title.sep : NULL);\r
-\r
-       /* add file name */\r
-       if (forDisplay)\r
-       {\r
-               char *c;\r
-               FLAC_plugin__canonical_set_ansi(tag, L"filepath", fileName);\r
+       if(FLAC_plugin__tags_get(fileName, tags)) {\r
 \r
-               strcpy(buffer, GetFileName(fileName));\r
-               if (c = strrchr(buffer, '.')) *c = 0;\r
-               FLAC_plugin__canonical_set_ansi(tag, L"filename", buffer);\r
+               /* add file name */\r
+               if (forDisplay)\r
+               {\r
+                       char *c;\r
+                       wchar_t *ucs2;\r
+                       ucs2 = AnsiToWide(fileName);\r
+                       FLAC_plugin__tags_set_tag_ucs2(*tags, "filepath", ucs2, /*replace_all=*/true);\r
+                       free(ucs2);\r
+\r
+                       strcpy(buffer, GetFileName(fileName));\r
+                       if (c = strrchr(buffer, '.')) *c = 0;\r
+                       ucs2 = AnsiToWide(buffer);\r
+                       FLAC_plugin__tags_set_tag_ucs2(*tags, "filename", ucs2, /*replace_all=*/true);\r
+                       free(ucs2);\r
+               }\r
        }\r
 }\r
 \r