M3U support in the UI from "Add Media" \o/

This commit is contained in:
John Maguire 2010-03-07 14:42:51 +00:00
parent 08416e2c51
commit 768bf85cd7
3 changed files with 36 additions and 15 deletions

View File

@ -10,11 +10,11 @@ M3UParser::M3UParser(QIODevice* device, const QDir& directory, QObject* parent)
} }
const QList<Song>& M3UParser::Parse() { const QList<Song>& M3UParser::Parse() {
QString line = QString::fromLatin1(device_->readLine()); QString line = QString::fromLatin1(device_->readLine()).trimmed();
if (line.startsWith("#EXTM3U")) { if (line.startsWith("#EXTM3U")) {
// This is in extended M3U format. // This is in extended M3U format.
type_ = EXTENDED; type_ = EXTENDED;
line = QString::fromLatin1(device_->readLine()); line = QString::fromLatin1(device_->readLine()).trimmed();
} }
forever { forever {
@ -35,17 +35,17 @@ const QList<Song>& M3UParser::Parse() {
QString location; QString location;
if (!ParseTrackLocation(line, &song)) { if (!ParseTrackLocation(line, &song)) {
qWarning() << "Failed to parse location: " << line; qWarning() << "Failed to parse location: " << line;
continue; } else {
}
songs_ << song; songs_ << song;
current_metadata_.artist.clear(); current_metadata_.artist.clear();
current_metadata_.title.clear(); current_metadata_.title.clear();
current_metadata_.length = -1; current_metadata_.length = -1;
} }
if (!device_->canReadLine()) { }
if (device_->atEnd()) {
break; break;
} }
line = QString::fromLatin1(device_->readLine()); line = QString::fromLatin1(device_->readLine()).trimmed();
} }
return songs_; return songs_;
@ -89,12 +89,19 @@ bool M3UParser::ParseTrackLocation(const QString& line, Song* song) const {
// Absolute path. // Absolute path.
// Fix windows \, eg. C:\foo -> C:/foo. // Fix windows \, eg. C:\foo -> C:/foo.
QString proper_path = QDir::fromNativeSeparators(line); QString proper_path = QDir::fromNativeSeparators(line);
if (!QFile::exists(proper_path)) {
return false;
}
song->set_filename(proper_path); song->set_filename(proper_path);
} else { } else {
// Relative path. // Relative path.
QString proper_path = QDir::fromNativeSeparators(line); QString proper_path = QDir::fromNativeSeparators(line);
QString absolute_path = directory_.absoluteFilePath(proper_path); QString absolute_path = directory_.absoluteFilePath(proper_path);
if (!QFile::exists(absolute_path)) {
return false;
}
song->set_filename(absolute_path); song->set_filename(absolute_path);
} }
song->InitFromFile(song->filename(), -1); song->InitFromFile(song->filename(), -1);
return true;
} }

View File

@ -20,6 +20,7 @@
#include "addstreamdialog.h" #include "addstreamdialog.h"
#include "stylesheetloader.h" #include "stylesheetloader.h"
#include "albumcovermanager.h" #include "albumcovermanager.h"
#include "m3uparser.h"
#include "qxtglobalshortcut.h" #include "qxtglobalshortcut.h"
@ -39,6 +40,8 @@
const int MainWindow::kStateVersion = 1; const int MainWindow::kStateVersion = 1;
const char* MainWindow::kSettingsGroup = "MainWindow"; const char* MainWindow::kSettingsGroup = "MainWindow";
const char* MainWindow::kMediaFilterSpec =
"Music (*.mp3 *.ogg *.flac *.mpc *.m4a *.aac *.wma);;Playlists (*.m3u)";
MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent) MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
: QMainWindow(parent), : QMainWindow(parent),
@ -616,7 +619,8 @@ void MainWindow::AddMedia() {
QString directory = settings_.value("add_media_path", QDir::currentPath()).toString(); QString directory = settings_.value("add_media_path", QDir::currentPath()).toString();
// Show dialog // Show dialog
QStringList file_names = QFileDialog::getOpenFileNames(this, "Add media", directory); QStringList file_names = QFileDialog::getOpenFileNames(
this, "Add media", directory, kMediaFilterSpec);
if (file_names.isEmpty()) if (file_names.isEmpty())
return; return;
@ -626,11 +630,20 @@ void MainWindow::AddMedia() {
// Add media // Add media
QList<QUrl> urls; QList<QUrl> urls;
foreach (const QString& path, file_names) { foreach (const QString& path, file_names) {
if (path.endsWith(".m3u")) {
QFile file(path);
QFileInfo info(file);
file.open(QIODevice::ReadOnly);
M3UParser parser(&file, info.dir());
const SongList& songs = parser.Parse();
current_playlist_->InsertSongs(songs);
} else {
QUrl url(path); QUrl url(path);
if (url.scheme().isEmpty()) if (url.scheme().isEmpty())
url.setScheme("file"); url.setScheme("file");
urls << url; urls << url;
} }
}
current_playlist_->InsertPaths(urls); current_playlist_->InsertPaths(urls);
} }

View File

@ -91,11 +91,12 @@ class MainWindow : public QMainWindow {
private: private:
void SaveGeometry(); void SaveGeometry();
void SetCurrentPlaylist(PlaylistView * pCurrent) ; void SetCurrentPlaylist(PlaylistView* current);
private: private:
static const int kStateVersion; static const int kStateVersion;
static const char* kSettingsGroup; static const char* kSettingsGroup;
static const char* kMediaFilterSpec;
Ui::MainWindow ui_; Ui::MainWindow ui_;
SystemTrayIcon* tray_icon_; SystemTrayIcon* tray_icon_;