PlaylistParser: Refactor code and exclude CUE from save playlist filters
Fixes #953
This commit is contained in:
@@ -111,9 +111,6 @@ const QRgb Playlist::kDynamicHistoryColor = qRgb(0x80, 0x80, 0x80);
|
||||
|
||||
const char *Playlist::kSettingsGroup = "Playlist";
|
||||
|
||||
const char *Playlist::kPathType = "path_type";
|
||||
const char *Playlist::kWriteMetadata = "write_metadata";
|
||||
|
||||
const int Playlist::kUndoStackSize = 20;
|
||||
const int Playlist::kUndoItemLimit = 500;
|
||||
|
||||
|
||||
@@ -143,13 +143,6 @@ class Playlist : public QAbstractListModel {
|
||||
Role_CanSetRating,
|
||||
};
|
||||
|
||||
enum Path {
|
||||
Path_Automatic = 0, // Automatically select path type
|
||||
Path_Absolute, // Always use absolute paths
|
||||
Path_Relative, // Always use relative paths
|
||||
Path_Ask_User, // Only used in preferences: to ask user which of the previous values he wants to use.
|
||||
};
|
||||
|
||||
enum AutoScroll {
|
||||
AutoScroll_Never,
|
||||
AutoScroll_Maybe,
|
||||
@@ -168,9 +161,6 @@ class Playlist : public QAbstractListModel {
|
||||
|
||||
static const char *kSettingsGroup;
|
||||
|
||||
static const char *kPathType;
|
||||
static const char *kWriteMetadata;
|
||||
|
||||
static const int kUndoStackSize;
|
||||
static const int kUndoItemLimit;
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ void PlaylistContainer::NewPlaylist() { manager_->New(tr("Playlist")); }
|
||||
void PlaylistContainer::LoadPlaylist() {
|
||||
|
||||
QString filename = settings_.value("last_load_playlist").toString();
|
||||
filename = QFileDialog::getOpenFileName(this, tr("Load playlist"), filename, manager_->parser()->filters());
|
||||
filename = QFileDialog::getOpenFileName(this, tr("Load playlist"), filename, manager_->parser()->filters(PlaylistParser::Type_Load));
|
||||
|
||||
if (filename.isNull()) return;
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#include <QScrollBar>
|
||||
#include <QSettings>
|
||||
#include <QtDebug>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "core/application.h"
|
||||
#include "core/logging.h"
|
||||
@@ -55,6 +56,7 @@
|
||||
#include "covermanager/albumcoverloaderresult.h"
|
||||
#include "covermanager/currentalbumcoverloader.h"
|
||||
#include "organize/organizeformat.h"
|
||||
#include "settings/playlistsettingspage.h"
|
||||
#include "playlist.h"
|
||||
#include "playlistbackend.h"
|
||||
#include "playlistcontainer.h"
|
||||
@@ -200,22 +202,22 @@ void PlaylistManager::New(const QString &name, const SongList &songs, const QStr
|
||||
|
||||
void PlaylistManager::Load(const QString &filename) {
|
||||
|
||||
QFileInfo info(filename);
|
||||
QFileInfo fileinfo(filename);
|
||||
|
||||
int id = playlist_backend_->CreatePlaylist(info.completeBaseName(), QString());
|
||||
int id = playlist_backend_->CreatePlaylist(fileinfo.completeBaseName(), QString());
|
||||
|
||||
if (id == -1) {
|
||||
emit Error(tr("Couldn't create playlist"));
|
||||
return;
|
||||
}
|
||||
|
||||
Playlist *playlist = AddPlaylist(id, info.completeBaseName(), QString(), QString(), false);
|
||||
Playlist *playlist = AddPlaylist(id, fileinfo.completeBaseName(), QString(), QString(), false);
|
||||
|
||||
playlist->InsertUrls(QList<QUrl>() << QUrl::fromLocalFile(filename));
|
||||
|
||||
}
|
||||
|
||||
void PlaylistManager::Save(const int id, const QString &filename, const Playlist::Path path_type) {
|
||||
void PlaylistManager::Save(const int id, const QString &filename, const PlaylistSettingsPage::PathType path_type) {
|
||||
|
||||
if (playlists_.contains(id)) {
|
||||
parser_->Save(playlist(id)->GetAllSongs(), filename, path_type);
|
||||
@@ -237,7 +239,7 @@ void PlaylistManager::Save(const int id, const QString &filename, const Playlist
|
||||
|
||||
}
|
||||
|
||||
void PlaylistManager::ItemsLoadedForSavePlaylist(const SongList &songs, const QString &filename, const Playlist::Path path_type) {
|
||||
void PlaylistManager::ItemsLoadedForSavePlaylist(const SongList &songs, const QString &filename, const PlaylistSettingsPage::PathType path_type) {
|
||||
|
||||
parser_->Save(songs, filename, path_type);
|
||||
|
||||
@@ -245,67 +247,43 @@ void PlaylistManager::ItemsLoadedForSavePlaylist(const SongList &songs, const QS
|
||||
|
||||
void PlaylistManager::SaveWithUI(const int id, const QString &playlist_name) {
|
||||
|
||||
QSettings settings;
|
||||
settings.beginGroup(Playlist::kSettingsGroup);
|
||||
QString filename = settings.value("last_save_playlist").toString();
|
||||
QString extension = settings.value("last_save_extension", parser()->default_extension()).toString();
|
||||
QString filter = settings.value("last_save_filter", parser()->default_filter()).toString();
|
||||
QSettings s;
|
||||
s.beginGroup(Playlist::kSettingsGroup);
|
||||
QString last_save_filter = s.value("last_save_filter", parser()->default_filter()).toString();
|
||||
QString last_save_path = s.value("last_save_path", QDir::homePath()).toString();
|
||||
QString last_save_extension = s.value("last_save_extension", parser()->default_extension()).toString();
|
||||
s.endGroup();
|
||||
|
||||
QString suggested_filename = playlist_name;
|
||||
suggested_filename = suggested_filename.remove(OrganizeFormat::kProblematicCharacters);
|
||||
QString filename = last_save_path + "/" + suggested_filename.remove(OrganizeFormat::kProblematicCharacters) + "." + last_save_extension;
|
||||
|
||||
qLog(Debug) << "Using extension:" << extension;
|
||||
|
||||
// We want to use the playlist tab name (with disallowed characters removed)
|
||||
// as a default filename, but in the same directory as the last saved file.
|
||||
|
||||
// Strip off filename components until we find something that's a folder
|
||||
QFileInfo fileinfo;
|
||||
forever {
|
||||
QFileInfo fileinfo(filename);
|
||||
if (filename.isEmpty() || fileinfo.isDir()) break;
|
||||
|
||||
filename = filename.section('/', 0, -2);
|
||||
filename = QFileDialog::getSaveFileName(nullptr, tr("Save playlist", "Title of the playlist save dialog."), filename, parser()->filters(PlaylistParser::Type_Save), &last_save_filter);
|
||||
if (filename.isEmpty()) return;
|
||||
fileinfo.setFile(filename);
|
||||
ParserBase *parser = parser_->ParserForExtension(PlaylistParser::Type_Save, fileinfo.suffix());
|
||||
if (parser) break;
|
||||
QMessageBox::warning(nullptr, tr("Unknown playlist extension"), tr("Unknown file extension for playlist."));
|
||||
}
|
||||
|
||||
// Use the home directory as a fallback in case the path is empty.
|
||||
if (filename.isEmpty()) filename = QDir::homePath();
|
||||
|
||||
// Add the suggested filename
|
||||
filename += "/" + suggested_filename + "." + extension;
|
||||
qLog(Debug) << "Suggested filename:" << filename;
|
||||
|
||||
filename = QFileDialog::getSaveFileName(nullptr, tr("Save playlist", "Title of the playlist save dialog."), filename, parser()->filters(), &filter);
|
||||
|
||||
if (filename.isNull()) {
|
||||
return;
|
||||
s.beginGroup(PlaylistSettingsPage::kSettingsGroup);
|
||||
PlaylistSettingsPage::PathType path_type = static_cast<PlaylistSettingsPage::PathType>(s.value("path_type", PlaylistSettingsPage::PathType_Automatic).toInt());
|
||||
s.endGroup();
|
||||
if (path_type == PlaylistSettingsPage::PathType_Ask_User) {
|
||||
PlaylistSaveOptionsDialog optionsdialog(nullptr);
|
||||
optionsdialog.setModal(true);
|
||||
if (optionsdialog.exec() != QDialog::Accepted) return;
|
||||
path_type = optionsdialog.path_type();
|
||||
}
|
||||
|
||||
// Check if the file extension is valid. Fallback to the default if not.
|
||||
QFileInfo info(filename);
|
||||
ParserBase *parser = parser_->ParserForExtension(info.suffix());
|
||||
if (!parser) {
|
||||
qLog(Warning) << "Unknown file extension:" << info.suffix();
|
||||
filename = info.absolutePath() + "/" + info.fileName() + "." + parser_->default_extension();
|
||||
info.setFile(filename);
|
||||
filter = info.suffix();
|
||||
}
|
||||
s.beginGroup(Playlist::kSettingsGroup);
|
||||
s.setValue("last_save_filter", last_save_filter);
|
||||
s.setValue("last_save_path", fileinfo.path());
|
||||
s.setValue("last_save_extension", fileinfo.suffix());
|
||||
s.endGroup();
|
||||
|
||||
int p = settings.value(Playlist::kPathType, Playlist::Path_Automatic).toInt();
|
||||
Playlist::Path path = static_cast<Playlist::Path>(p);
|
||||
if (path == Playlist::Path_Ask_User) {
|
||||
PlaylistSaveOptionsDialog optionsDialog(nullptr);
|
||||
optionsDialog.setModal(true);
|
||||
if (optionsDialog.exec() != QDialog::Accepted) {
|
||||
return;
|
||||
}
|
||||
path = optionsDialog.path_type();
|
||||
}
|
||||
|
||||
settings.setValue("last_save_playlist", filename);
|
||||
settings.setValue("last_save_filter", filter);
|
||||
settings.setValue("last_save_extension", info.suffix());
|
||||
|
||||
Save(id == -1 ? current_id() : id, filename, path);
|
||||
Save(id == -1 ? current_id() : id, filename, path_type);
|
||||
|
||||
}
|
||||
|
||||
@@ -658,7 +636,7 @@ void PlaylistManager::SaveAllPlaylists() {
|
||||
for (QMap<int, Data>::const_iterator it = playlists_.constBegin(); it != playlists_.constEnd(); ++it) {
|
||||
const Data &data = *it;
|
||||
const QString filepath = path + "/" + data.name + ".m3u";
|
||||
Save(it.key(), filepath, Playlist::Path_Absolute);
|
||||
Save(it.key(), filepath, PlaylistSettingsPage::PathType_Absolute);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <QUrl>
|
||||
|
||||
#include "core/song.h"
|
||||
#include "settings/playlistsettingspage.h"
|
||||
#include "playlist.h"
|
||||
#include "smartplaylists/playlistgenerator.h"
|
||||
|
||||
@@ -83,7 +84,7 @@ class PlaylistManagerInterface : public QObject {
|
||||
public slots:
|
||||
virtual void New(const QString &name, const SongList &songs = SongList(), const QString &special_type = QString()) = 0;
|
||||
virtual void Load(const QString &filename) = 0;
|
||||
virtual void Save(const int id, const QString &filename, const Playlist::Path path_type) = 0;
|
||||
virtual void Save(const int id, const QString &filename, const PlaylistSettingsPage::PathType path_type) = 0;
|
||||
virtual void Rename(const int id, const QString &new_name) = 0;
|
||||
virtual void Delete(const int id) = 0;
|
||||
virtual bool Close(const int id) = 0;
|
||||
@@ -181,7 +182,7 @@ class PlaylistManager : public PlaylistManagerInterface {
|
||||
public slots:
|
||||
void New(const QString &name, const SongList &songs = SongList(), const QString &special_type = QString()) override;
|
||||
void Load(const QString &filename) override;
|
||||
void Save(const int id, const QString &filename, const Playlist::Path path_type) override;
|
||||
void Save(const int id, const QString &filename, const PlaylistSettingsPage::PathType path_type) override;
|
||||
// Display a file dialog to let user choose a file before saving the file
|
||||
void SaveWithUI(const int id, const QString &playlist_name);
|
||||
void Rename(const int id, const QString &new_name) override;
|
||||
@@ -232,7 +233,7 @@ class PlaylistManager : public PlaylistManagerInterface {
|
||||
void OneOfPlaylistsChanged();
|
||||
void UpdateSummaryText();
|
||||
void SongsDiscovered(const SongList &songs);
|
||||
void ItemsLoadedForSavePlaylist(const SongList &songs, const QString &filename, const Playlist::Path path_type);
|
||||
void ItemsLoadedForSavePlaylist(const SongList &songs, const QString &filename, const PlaylistSettingsPage::PathType path_type);
|
||||
void PlaylistLoaded();
|
||||
|
||||
private:
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <QComboBox>
|
||||
#include <QSettings>
|
||||
|
||||
#include "playlist.h"
|
||||
#include "settings/playlistsettingspage.h"
|
||||
#include "playlistsaveoptionsdialog.h"
|
||||
#include "ui_playlistsaveoptionsdialog.h"
|
||||
|
||||
@@ -37,9 +37,10 @@ PlaylistSaveOptionsDialog::PlaylistSaveOptionsDialog(QWidget *parent) : QDialog(
|
||||
|
||||
ui->setupUi(this);
|
||||
|
||||
ui->filePaths->addItem(tr("Automatic"), Playlist::Path_Automatic);
|
||||
ui->filePaths->addItem(tr("Relative"), Playlist::Path_Relative);
|
||||
ui->filePaths->addItem(tr("Absolute"), Playlist::Path_Absolute);
|
||||
ui->filePaths->addItem(tr("Automatic"), PlaylistSettingsPage::PathType_Automatic);
|
||||
ui->filePaths->addItem(tr("Relative"), PlaylistSettingsPage::PathType_Relative);
|
||||
ui->filePaths->addItem(tr("Absolute"), PlaylistSettingsPage::PathType_Absolute);
|
||||
|
||||
}
|
||||
|
||||
PlaylistSaveOptionsDialog::~PlaylistSaveOptionsDialog() { delete ui; }
|
||||
@@ -48,14 +49,15 @@ void PlaylistSaveOptionsDialog::accept() {
|
||||
|
||||
if (ui->remember_user_choice->isChecked()) {
|
||||
QSettings s;
|
||||
s.beginGroup(Playlist::kSettingsGroup);
|
||||
s.setValue(Playlist::kPathType, ui->filePaths->itemData(ui->filePaths->currentIndex()).toInt());
|
||||
s.beginGroup(PlaylistSettingsPage::kSettingsGroup);
|
||||
s.setValue("path_type", ui->filePaths->itemData(ui->filePaths->currentIndex()).toInt());
|
||||
s.endGroup();
|
||||
}
|
||||
|
||||
QDialog::accept();
|
||||
|
||||
}
|
||||
|
||||
Playlist::Path PlaylistSaveOptionsDialog::path_type() const {
|
||||
return static_cast<Playlist::Path>(ui->filePaths->itemData(ui->filePaths->currentIndex()).toInt());
|
||||
PlaylistSettingsPage::PathType PlaylistSaveOptionsDialog::path_type() const {
|
||||
return static_cast<PlaylistSettingsPage::PathType>(ui->filePaths->itemData(ui->filePaths->currentIndex()).toInt());
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <QDialog>
|
||||
#include <QString>
|
||||
|
||||
#include "playlist.h"
|
||||
#include "settings/playlistsettingspage.h"
|
||||
|
||||
class QWidget;
|
||||
|
||||
@@ -43,7 +43,7 @@ class PlaylistSaveOptionsDialog : public QDialog {
|
||||
~PlaylistSaveOptionsDialog() override;
|
||||
|
||||
void accept() override;
|
||||
Playlist::Path path_type() const;
|
||||
PlaylistSettingsPage::PathType path_type() const;
|
||||
|
||||
private:
|
||||
static const char *kSettingsGroup;
|
||||
|
||||
Reference in New Issue
Block a user