Music player can now handle unicode filenames and m3u8 playlists, issue #261

This commit is contained in:
kervala 2016-02-20 19:12:55 +01:00
parent 399bf7e9ca
commit f4136bcdca

View file

@ -196,11 +196,6 @@ public:
InitMouseWithCursor (true); InitMouseWithCursor (true);
Driver->showCursor (true); Driver->showCursor (true);
if (false) //supportUnicode())
{
}
else
{
bool oggSupported = false; bool oggSupported = false;
bool mp3Supported = false; bool mp3Supported = false;
@ -219,12 +214,12 @@ public:
std::vector<std::string> filters; std::vector<std::string> filters;
// supported formats // supported formats
filters.push_back("All Supported Files"); filters.push_back("All Supported Files"); // TODO: translate
std::string filter; std::string filter;
if (mp3Supported) filter += "*.mp3;*.mp2;*.mp1;"; if (mp3Supported) filter += "*.mp3;*.mp2;*.mp1;";
if (oggSupported) filter += "*.ogg;"; if (oggSupported) filter += "*.ogg;";
filter += "*.m3u"; filter += "*.m3u;*.m3u8";
filters.push_back(filter); filters.push_back(filter);
@ -243,8 +238,8 @@ public:
} }
// playlist // playlist
filters.push_back("Playlist Files (*.m3u)"); filters.push_back("Playlist Files (*.m3u;*.m3u8)");
filters.push_back("*.m3u"); filters.push_back("*.m3u;*.m3u8");
// all files // all files
filters.push_back("All Files (*.*)"); filters.push_back("All Files (*.*)");
@ -252,23 +247,23 @@ public:
filters.push_back(""); filters.push_back("");
static char szFilter[1024] = { '\0' }; static wchar_t szFilter[1024] = { '\0' };
uint offset = 0; uint offset = 0;
for(uint i = 0; i < filters.size(); ++i) for(uint i = 0; i < filters.size(); ++i)
{ {
strcpy(szFilter + offset, filters[i].c_str()); wcscpy(szFilter + offset, utf8ToWide(filters[i]));
// move offset to string length + 1 for \0 // move offset to string length + 1 for \0
offset += filters[i].length() + 1; offset += filters[i].length() + 1;
} }
// Filename buffer // Filename buffer
char buffer[65535]; wchar_t buffer[1024];
buffer[0]=0; buffer[0]=0;
OPENFILENAME ofn; OPENFILENAMEW ofn;
memset (&ofn, 0, sizeof(OPENFILENAME)); memset (&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME); ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = Driver ? Driver->getDisplay():NULL; ofn.hwndOwner = Driver ? Driver->getDisplay():NULL;
@ -277,25 +272,27 @@ public:
ofn.nFilterIndex = 0; ofn.nFilterIndex = 0;
ofn.lpstrFile = buffer; ofn.lpstrFile = buffer;
ofn.nMaxFile = sizeof(buffer); ofn.nMaxFile = sizeof(buffer);
ofn.lpstrTitle = "Play songs"; ofn.lpstrTitle = (wchar_t*)NLMISC::CI18N::get("uiPlaySongs").c_str();
ofn.Flags = OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING|OFN_EXPLORER; ofn.Flags = OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT|OFN_ENABLESIZING|OFN_EXPLORER;
if (Driver) if (Driver)
Driver->beginDialogMode(); Driver->beginDialogMode();
if (GetOpenFileName (&ofn)) if (GetOpenFileNameW (&ofn))
{ {
bool useUtf8 = false;
// Skip the directory name // Skip the directory name
const char *bufferPtr = buffer; const wchar_t *bufferPtr = buffer;
// Multi filename ? // Multi filename ?
string path; string path;
if (ofn.nFileOffset>strlen(buffer)) if (ofn.nFileOffset>wcslen(buffer))
{ {
// Backup the path and point to the next filename // Backup the path and point to the next filename
path = buffer; path = wideToUtf8(buffer);
path += "\\"; path += "\\";
bufferPtr+=strlen(bufferPtr)+1; bufferPtr += wcslen(bufferPtr)+1;
} }
// Get selected files and playlists // Get selected files and playlists
@ -304,23 +301,34 @@ public:
while (*bufferPtr) while (*bufferPtr)
{ {
// Concat the directory name with the filename // Concat the directory name with the filename
if (toLower(CFile::getExtension(bufferPtr)) == "m3u") std::string ext = toLower(CFile::getExtension(wideToUtf8(bufferPtr)));
playlists.push_back (path+bufferPtr); if (ext == "m3u" || ext == "m3u8")
{
playlists.push_back (path + wideToUtf8(bufferPtr));
}
else else
filenames.push_back (path+bufferPtr); {
bufferPtr+=strlen(bufferPtr)+1; filenames.push_back (path + wideToUtf8(bufferPtr));
}
bufferPtr += wcslen(bufferPtr) + 1;
} }
// Sort songs by filename // Sort songs by filename
sort (filenames.begin(), filenames.end()); sort (filenames.begin(), filenames.end());
static uint8 utf8Header[] = { 0xefu, 0xbbu, 0xbfu };
// Add playlist // Add playlist
uint i; uint i;
for (i=0; i<playlists.size(); i++) for (i=0; i<playlists.size(); i++)
{ {
// Get the path of the playlist // Get the path of the playlist
string basePlaylist = CFile::getPath (playlists[i]); string basePlaylist = CFile::getPath (playlists[i]);
FILE *file = fopen (playlists[i].c_str(), "r"); FILE *file = nlfopen (playlists[i], "r");
bool useUtf8 = CFile::getExtension(playlists[i]) == "m3u8";
if (file) if (file)
{ {
char line[512]; char line[512];
@ -328,8 +336,15 @@ public:
{ {
// Not a comment line // Not a comment line
string lineStr = trim(std::string(line)); string lineStr = trim(std::string(line));
// id a UTF-8 BOM header is present, parse as UTF-8
if (!useUtf8 && lineStr.length() >= 3 && memcmp(line, utf8Header, 3) == 0)
useUtf8 = true;
if (!useUtf8) lineStr = ucstring(line).toUtf8();
if (lineStr[0] != '#') if (lineStr[0] != '#')
filenames.push_back (basePlaylist+lineStr); filenames.push_back (CPath::makePathAbsolute(lineStr, basePlaylist));
} }
fclose (file); fclose (file);
} }
@ -350,7 +365,6 @@ public:
if (Driver) if (Driver)
Driver->endDialogMode(); Driver->endDialogMode();
}
// Restore mouse // Restore mouse
InitMouseWithCursor (wasHardware); InitMouseWithCursor (wasHardware);