M3U support in the UI from "Add Media" \o/
This commit is contained in:
parent
08416e2c51
commit
768bf85cd7
@ -10,11 +10,11 @@ M3UParser::M3UParser(QIODevice* device, const QDir& directory, QObject* parent)
|
||||
}
|
||||
|
||||
const QList<Song>& M3UParser::Parse() {
|
||||
QString line = QString::fromLatin1(device_->readLine());
|
||||
QString line = QString::fromLatin1(device_->readLine()).trimmed();
|
||||
if (line.startsWith("#EXTM3U")) {
|
||||
// This is in extended M3U format.
|
||||
type_ = EXTENDED;
|
||||
line = QString::fromLatin1(device_->readLine());
|
||||
line = QString::fromLatin1(device_->readLine()).trimmed();
|
||||
}
|
||||
|
||||
forever {
|
||||
@ -35,17 +35,17 @@ const QList<Song>& M3UParser::Parse() {
|
||||
QString location;
|
||||
if (!ParseTrackLocation(line, &song)) {
|
||||
qWarning() << "Failed to parse location: " << line;
|
||||
continue;
|
||||
} else {
|
||||
songs_ << song;
|
||||
current_metadata_.artist.clear();
|
||||
current_metadata_.title.clear();
|
||||
current_metadata_.length = -1;
|
||||
}
|
||||
songs_ << song;
|
||||
current_metadata_.artist.clear();
|
||||
current_metadata_.title.clear();
|
||||
current_metadata_.length = -1;
|
||||
}
|
||||
if (!device_->canReadLine()) {
|
||||
if (device_->atEnd()) {
|
||||
break;
|
||||
}
|
||||
line = QString::fromLatin1(device_->readLine());
|
||||
line = QString::fromLatin1(device_->readLine()).trimmed();
|
||||
}
|
||||
|
||||
return songs_;
|
||||
@ -89,12 +89,19 @@ bool M3UParser::ParseTrackLocation(const QString& line, Song* song) const {
|
||||
// Absolute path.
|
||||
// Fix windows \, eg. C:\foo -> C:/foo.
|
||||
QString proper_path = QDir::fromNativeSeparators(line);
|
||||
if (!QFile::exists(proper_path)) {
|
||||
return false;
|
||||
}
|
||||
song->set_filename(proper_path);
|
||||
} else {
|
||||
// Relative path.
|
||||
QString proper_path = QDir::fromNativeSeparators(line);
|
||||
QString absolute_path = directory_.absoluteFilePath(proper_path);
|
||||
if (!QFile::exists(absolute_path)) {
|
||||
return false;
|
||||
}
|
||||
song->set_filename(absolute_path);
|
||||
}
|
||||
song->InitFromFile(song->filename(), -1);
|
||||
return true;
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "addstreamdialog.h"
|
||||
#include "stylesheetloader.h"
|
||||
#include "albumcovermanager.h"
|
||||
#include "m3uparser.h"
|
||||
|
||||
#include "qxtglobalshortcut.h"
|
||||
|
||||
@ -39,6 +40,8 @@
|
||||
|
||||
const int MainWindow::kStateVersion = 1;
|
||||
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)
|
||||
: QMainWindow(parent),
|
||||
@ -616,7 +619,8 @@ void MainWindow::AddMedia() {
|
||||
QString directory = settings_.value("add_media_path", QDir::currentPath()).toString();
|
||||
|
||||
// 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())
|
||||
return;
|
||||
|
||||
@ -626,10 +630,19 @@ void MainWindow::AddMedia() {
|
||||
// Add media
|
||||
QList<QUrl> urls;
|
||||
foreach (const QString& path, file_names) {
|
||||
QUrl url(path);
|
||||
if (url.scheme().isEmpty())
|
||||
url.setScheme("file");
|
||||
urls << url;
|
||||
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);
|
||||
if (url.scheme().isEmpty())
|
||||
url.setScheme("file");
|
||||
urls << url;
|
||||
}
|
||||
}
|
||||
current_playlist_->InsertPaths(urls);
|
||||
}
|
||||
|
@ -91,11 +91,12 @@ class MainWindow : public QMainWindow {
|
||||
private:
|
||||
void SaveGeometry();
|
||||
|
||||
void SetCurrentPlaylist(PlaylistView * pCurrent) ;
|
||||
void SetCurrentPlaylist(PlaylistView* current);
|
||||
|
||||
private:
|
||||
static const int kStateVersion;
|
||||
static const char* kSettingsGroup;
|
||||
static const char* kMediaFilterSpec;
|
||||
|
||||
Ui::MainWindow ui_;
|
||||
SystemTrayIcon* tray_icon_;
|
||||
|
Loading…
x
Reference in New Issue
Block a user