2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2019-01-26 17:18:26 +01:00
|
|
|
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <memory>
|
2020-07-19 22:43:58 +02:00
|
|
|
#include <functional>
|
2018-10-19 20:18:46 +02:00
|
|
|
#include <algorithm>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
2020-07-18 16:28:39 +02:00
|
|
|
#include <QtConcurrent>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QDialog>
|
2020-04-07 01:26:17 +02:00
|
|
|
#include <QScreen>
|
|
|
|
#include <QWindow>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QHash>
|
|
|
|
#include <QMap>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QAction>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QMenu>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QListWidget>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QPushButton>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QStackedWidget>
|
|
|
|
#include <QToolButton>
|
|
|
|
#include <QFlags>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QShowEvent>
|
2020-04-07 01:26:17 +02:00
|
|
|
#include <QCloseEvent>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QSettings>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/iconloader.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "core/musicstorage.h"
|
|
|
|
#include "core/tagreaderclient.h"
|
|
|
|
#include "core/utilities.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "widgets/freespacebar.h"
|
|
|
|
#include "widgets/linetextedit.h"
|
2019-03-25 00:53:12 +01:00
|
|
|
#include "collection/collectionbackend.h"
|
2020-08-04 21:18:14 +02:00
|
|
|
#include "organize.h"
|
|
|
|
#include "organizeformat.h"
|
|
|
|
#include "organizedialog.h"
|
|
|
|
#include "organizeerrordialog.h"
|
|
|
|
#include "ui_organizedialog.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
const char *OrganizeDialog::kDefaultFormat = "%albumartist/%album{ (Disc %disc)}/{%track - }{%albumartist - }%album{ (Disc %disc)} - %title.%extension";
|
2019-06-28 01:33:22 +02:00
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
const char *OrganizeDialog::kSettingsGroup = "OrganizeDialog";
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
OrganizeDialog::OrganizeDialog(TaskManager *task_manager, CollectionBackend *backend, QWidget *parentwindow, QWidget *parent)
|
2018-02-27 18:06:05 +01:00
|
|
|
: QDialog(parent),
|
2020-04-07 01:26:17 +02:00
|
|
|
parentwindow_(parentwindow),
|
2020-08-04 21:18:14 +02:00
|
|
|
ui_(new Ui_OrganizeDialog),
|
2018-02-27 18:06:05 +01:00
|
|
|
task_manager_(task_manager),
|
2019-03-25 00:53:12 +01:00
|
|
|
backend_(backend),
|
2019-03-22 23:18:14 +01:00
|
|
|
total_size_(0) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
ui_->setupUi(this);
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
setWindowFlags(windowFlags()|Qt::WindowMaximizeButtonHint);
|
|
|
|
|
2020-04-09 19:59:31 +02:00
|
|
|
QPushButton *button_save = ui_->button_box->addButton("Save settings", QDialogButtonBox::ApplyRole);
|
|
|
|
connect(button_save, SIGNAL(clicked()), SLOT(SaveSettings()));
|
|
|
|
button_save->setIcon(IconLoader::Load("document-save"));
|
|
|
|
ui_->button_box->button(QDialogButtonBox::RestoreDefaults)->setIcon(IconLoader::Load("edit-undo"));
|
|
|
|
connect(ui_->button_box->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), SLOT(RestoreDefaults()));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
ui_->aftercopying->setItemIcon(1, IconLoader::Load("edit-delete"));
|
|
|
|
|
|
|
|
// Valid tags
|
|
|
|
QMap<QString, QString> tags;
|
|
|
|
tags[tr("Title")] = "title";
|
|
|
|
tags[tr("Album")] = "album";
|
|
|
|
tags[tr("Artist")] = "artist";
|
|
|
|
tags[tr("Artist's initial")] = "artistinitial";
|
|
|
|
tags[tr("Album artist")] = "albumartist";
|
|
|
|
tags[tr("Composer")] = "composer";
|
|
|
|
tags[tr("Performer")] = "performer";
|
|
|
|
tags[tr("Grouping")] = "grouping";
|
|
|
|
tags[tr("Track")] = "track";
|
|
|
|
tags[tr("Disc")] = "disc";
|
|
|
|
tags[tr("Year")] = "year";
|
|
|
|
tags[tr("Original year")] = "originalyear";
|
|
|
|
tags[tr("Genre")] = "genre";
|
|
|
|
tags[tr("Comment")] = "comment";
|
|
|
|
tags[tr("Length")] = "length";
|
2020-08-04 21:18:14 +02:00
|
|
|
tags[tr("Bitrate", "Refers to bitrate in file organize dialog.")] = "bitrate";
|
2018-06-28 01:15:32 +02:00
|
|
|
tags[tr("Sample rate")] = "samplerate";
|
|
|
|
tags[tr("Bit depth")] = "bitdepth";
|
2018-02-27 18:06:05 +01:00
|
|
|
tags[tr("File extension")] = "extension";
|
|
|
|
|
|
|
|
// Naming scheme input field
|
2020-08-04 21:18:14 +02:00
|
|
|
new OrganizeFormat::SyntaxHighlighter(ui_->naming);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
connect(ui_->destination, SIGNAL(currentIndexChanged(int)), SLOT(UpdatePreviews()));
|
|
|
|
connect(ui_->naming, SIGNAL(textChanged()), SLOT(UpdatePreviews()));
|
2020-04-09 19:59:31 +02:00
|
|
|
connect(ui_->remove_problematic, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
|
2018-12-29 15:37:16 +01:00
|
|
|
connect(ui_->remove_non_fat, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
|
|
|
|
connect(ui_->remove_non_ascii, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
|
2019-03-22 23:18:14 +01:00
|
|
|
connect(ui_->allow_ascii_ext, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
|
2018-02-27 18:06:05 +01:00
|
|
|
connect(ui_->replace_spaces, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
|
2019-03-22 23:18:14 +01:00
|
|
|
connect(ui_->remove_non_ascii, SIGNAL(toggled(bool)), SLOT(AllowExtASCII(bool)));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Get the titles of the tags to put in the insert menu
|
|
|
|
QStringList tag_titles = tags.keys();
|
2018-10-19 20:18:46 +02:00
|
|
|
std::stable_sort(tag_titles.begin(), tag_titles.end());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Build the insert menu
|
|
|
|
QMenu *tag_menu = new QMenu(this);
|
|
|
|
for (const QString &title : tag_titles) {
|
2019-07-08 22:27:45 +02:00
|
|
|
QAction *action = tag_menu->addAction(title);
|
|
|
|
QString tag = tags[title];
|
|
|
|
connect(action, &QAction::triggered, [this, tag]() { InsertTag(tag); } );
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ui_->insert->setMenu(tag_menu);
|
2019-03-22 23:18:14 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
OrganizeDialog::~OrganizeDialog() {
|
2018-02-27 18:06:05 +01:00
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::SetDestinationModel(QAbstractItemModel *model, bool devices) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
ui_->destination->setModel(model);
|
|
|
|
|
|
|
|
ui_->eject_after->setVisible(devices);
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::showEvent(QShowEvent*) {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
LoadGeometry();
|
|
|
|
LoadSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::closeEvent(QCloseEvent*) {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
SaveGeometry();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::accept() {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
2020-04-09 19:59:31 +02:00
|
|
|
SaveGeometry();
|
2020-04-07 01:26:17 +02:00
|
|
|
SaveSettings();
|
|
|
|
|
|
|
|
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
|
|
|
|
std::shared_ptr<MusicStorage> storage = destination.data(MusicStorage::Role_StorageForceConnect).value<std::shared_ptr<MusicStorage>>();
|
|
|
|
|
|
|
|
if (!storage) return;
|
|
|
|
|
|
|
|
// It deletes itself when it's finished.
|
|
|
|
const bool copy = ui_->aftercopying->currentIndex() == 0;
|
2020-08-04 21:18:14 +02:00
|
|
|
Organize *organize = new Organize(task_manager_, storage, format_, copy, ui_->overwrite->isChecked(), ui_->mark_as_listened->isChecked(), ui_->albumcover->isChecked(), new_songs_info_, ui_->eject_after->isChecked(), playlist_);
|
|
|
|
connect(organize, SIGNAL(Finished(QStringList, QStringList)), SLOT(OrganizeFinished(QStringList, QStringList)));
|
|
|
|
connect(organize, SIGNAL(FileCopied(int)), this, SIGNAL(FileCopied(int)));
|
2020-04-07 01:26:17 +02:00
|
|
|
if (backend_)
|
2020-08-04 21:18:14 +02:00
|
|
|
connect(organize, SIGNAL(SongPathChanged(Song, QFileInfo)), backend_, SLOT(SongPathChanged(Song, QFileInfo)));
|
2020-04-07 01:26:17 +02:00
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
organize->Start();
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
QDialog::accept();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::reject() {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
SaveGeometry();
|
|
|
|
QDialog::reject();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::LoadGeometry() {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
if (parentwindow_) {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
if (s.contains("geometry")) {
|
|
|
|
restoreGeometry(s.value("geometry").toByteArray());
|
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
// Center the window on the same screen as the parentwindow.
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
|
|
|
QScreen *screen = parentwindow_->screen();
|
|
|
|
#else
|
|
|
|
QScreen *screen = (parentwindow_->window() && parentwindow_->window()->windowHandle() ? parentwindow_->window()->windowHandle()->screen() : nullptr);
|
|
|
|
#endif
|
|
|
|
if (screen) {
|
|
|
|
const QRect sr = screen->availableGeometry();
|
|
|
|
const QRect wr({}, size().boundedTo(sr.size()));
|
|
|
|
resize(wr.size());
|
|
|
|
move(sr.center() - wr.center());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::SaveGeometry() {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
if (parentwindow_) {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue("geometry", saveGeometry());
|
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::RestoreDefaults() {
|
2020-04-09 19:59:31 +02:00
|
|
|
|
|
|
|
ui_->naming->setPlainText(kDefaultFormat);
|
|
|
|
ui_->remove_problematic->setChecked(true);
|
|
|
|
ui_->remove_non_fat->setChecked(false);
|
|
|
|
ui_->remove_non_ascii->setChecked(false);
|
|
|
|
ui_->allow_ascii_ext->setChecked(false);
|
|
|
|
ui_->replace_spaces->setChecked(true);
|
|
|
|
ui_->overwrite->setChecked(false);
|
|
|
|
ui_->mark_as_listened->setChecked(false);
|
|
|
|
ui_->albumcover->setChecked(true);
|
|
|
|
ui_->eject_after->setChecked(false);
|
|
|
|
|
|
|
|
SaveSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::LoadSettings() {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
|
2020-04-09 19:59:31 +02:00
|
|
|
ui_->remove_problematic->setChecked(s.value("remove_problematic", true).toBool());
|
2020-04-07 01:26:17 +02:00
|
|
|
ui_->remove_non_fat->setChecked(s.value("remove_non_fat", false).toBool());
|
|
|
|
ui_->remove_non_ascii->setChecked(s.value("remove_non_ascii", false).toBool());
|
|
|
|
ui_->allow_ascii_ext->setChecked(s.value("allow_ascii_ext", false).toBool());
|
|
|
|
ui_->replace_spaces->setChecked(s.value("replace_spaces", true).toBool());
|
|
|
|
ui_->overwrite->setChecked(s.value("overwrite", false).toBool());
|
|
|
|
ui_->albumcover->setChecked(s.value("albumcover", true).toBool());
|
|
|
|
ui_->mark_as_listened->setChecked(s.value("mark_as_listened", false).toBool());
|
|
|
|
ui_->eject_after->setChecked(s.value("eject_after", false).toBool());
|
|
|
|
|
|
|
|
QString destination = s.value("destination").toString();
|
|
|
|
int index = ui_->destination->findText(destination);
|
|
|
|
if (index != -1 && !destination.isEmpty()) {
|
|
|
|
ui_->destination->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
AllowExtASCII(ui_->remove_non_ascii->isChecked());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::SaveSettings() {
|
2020-04-07 01:26:17 +02:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue("format", ui_->naming->toPlainText());
|
2020-04-09 19:59:31 +02:00
|
|
|
s.setValue("remove_problematic", ui_->remove_problematic->isChecked());
|
2020-04-07 01:26:17 +02:00
|
|
|
s.setValue("remove_non_fat", ui_->remove_non_fat->isChecked());
|
|
|
|
s.setValue("remove_non_ascii", ui_->remove_non_ascii->isChecked());
|
|
|
|
s.setValue("allow_ascii_ext", ui_->allow_ascii_ext->isChecked());
|
|
|
|
s.setValue("replace_spaces", ui_->replace_spaces->isChecked());
|
|
|
|
s.setValue("overwrite", ui_->overwrite->isChecked());
|
|
|
|
s.setValue("mark_as_listened", ui_->overwrite->isChecked());
|
|
|
|
s.setValue("albumcover", ui_->albumcover->isChecked());
|
|
|
|
s.setValue("destination", ui_->destination->currentText());
|
|
|
|
s.setValue("eject_after", ui_->eject_after->isChecked());
|
|
|
|
s.endGroup();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
bool OrganizeDialog::SetSongs(const SongList &songs) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
total_size_ = 0;
|
|
|
|
songs_.clear();
|
|
|
|
|
|
|
|
for (const Song &song : songs) {
|
2019-07-09 21:43:56 +02:00
|
|
|
if (!song.url().isLocalFile()) {
|
2018-02-27 18:06:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (song.filesize() > 0) total_size_ += song.filesize();
|
|
|
|
|
|
|
|
songs_ << song;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_->free_space->set_additional_bytes(total_size_);
|
|
|
|
UpdatePreviews();
|
|
|
|
SetLoadingSongs(false);
|
|
|
|
|
|
|
|
if (songs_future_.isRunning()) {
|
|
|
|
songs_future_.cancel();
|
|
|
|
}
|
|
|
|
songs_future_ = QFuture<SongList>();
|
|
|
|
|
|
|
|
return songs_.count();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
bool OrganizeDialog::SetUrls(const QList<QUrl> &urls) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QStringList filenames;
|
|
|
|
|
|
|
|
// Only add file:// URLs
|
|
|
|
for (const QUrl &url : urls) {
|
|
|
|
if (url.scheme() == "file") {
|
|
|
|
filenames << url.toLocalFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetFilenames(filenames);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
bool OrganizeDialog::SetFilenames(const QStringList &filenames) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
songs_future_ = QtConcurrent::run(std::bind(&OrganizeDialog::LoadSongsBlocking, this, filenames));
|
2020-07-19 19:07:12 +02:00
|
|
|
NewClosure(songs_future_, [=]() { SetSongs(songs_future_.result()); });
|
2020-07-18 16:28:39 +02:00
|
|
|
|
2020-07-19 19:07:12 +02:00
|
|
|
SetLoadingSongs(true);
|
2018-02-27 18:06:05 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::SetLoadingSongs(bool loading) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
ui_->preview_stack->setCurrentWidget(ui_->loading_page);
|
|
|
|
ui_->button_box->button(QDialogButtonBox::Ok)->setEnabled(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui_->preview_stack->setCurrentWidget(ui_->preview_page);
|
|
|
|
// The Ok button is enabled by UpdatePreviews
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
SongList OrganizeDialog::LoadSongsBlocking(const QStringList &filenames) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
SongList songs;
|
|
|
|
Song song;
|
|
|
|
|
|
|
|
QStringList filenames_copy = filenames;
|
|
|
|
while (!filenames_copy.isEmpty()) {
|
|
|
|
const QString filename = filenames_copy.takeFirst();
|
|
|
|
|
|
|
|
// If it's a directory, add all the files inside.
|
|
|
|
if (QFileInfo(filename).isDir()) {
|
|
|
|
const QDir dir(filename);
|
|
|
|
for (const QString &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable)) {
|
|
|
|
filenames_copy << dir.filePath(entry);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TagReaderClient::Instance()->ReadFileBlocking(filename, &song);
|
|
|
|
if (song.is_valid()) songs << song;
|
|
|
|
}
|
|
|
|
|
|
|
|
return songs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::SetCopy(bool copy) {
|
2019-08-07 17:13:40 +02:00
|
|
|
ui_->aftercopying->setCurrentIndex(copy ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::SetPlaylist(const QString &playlist)
|
2019-08-07 17:13:40 +02:00
|
|
|
{
|
|
|
|
playlist_ = playlist;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::InsertTag(const QString &tag) {
|
2018-02-27 18:06:05 +01:00
|
|
|
ui_->naming->insertPlainText("%" + tag);
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
Organize::NewSongInfoList OrganizeDialog::ComputeNewSongsFilenames(const SongList &songs, const OrganizeFormat &format) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Check if we will have multiple files with the same name.
|
|
|
|
// If so, they will erase each other if the overwrite flag is set.
|
|
|
|
// Better to rename them: e.g. foo.bar -> foo(2).bar
|
|
|
|
QHash<QString, int> filenames;
|
2020-08-04 21:18:14 +02:00
|
|
|
Organize::NewSongInfoList new_songs_info;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
for (const Song &song : songs) {
|
|
|
|
QString new_filename = format.GetFilenameForSong(song);
|
|
|
|
if (filenames.contains(new_filename)) {
|
|
|
|
QString song_number = QString::number(++filenames[new_filename]);
|
|
|
|
new_filename = Utilities::PathWithoutFilenameExtension(new_filename) + "(" + song_number + ")." + QFileInfo(new_filename).suffix();
|
|
|
|
}
|
|
|
|
filenames.insert(new_filename, 1);
|
2020-08-04 21:18:14 +02:00
|
|
|
new_songs_info << Organize::NewSongInfo(song, new_filename);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
return new_songs_info;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::UpdatePreviews() {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (songs_future_.isRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
|
|
|
|
std::shared_ptr<MusicStorage> storage;
|
|
|
|
bool has_local_destination = false;
|
|
|
|
|
|
|
|
if (destination.isValid()) {
|
|
|
|
storage = destination.data(MusicStorage::Role_Storage).value<std::shared_ptr<MusicStorage>>();
|
|
|
|
if (storage) {
|
|
|
|
has_local_destination = !storage->LocalPath().isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the free space bar
|
|
|
|
quint64 capacity = destination.data(MusicStorage::Role_Capacity).toLongLong();
|
|
|
|
quint64 free = destination.data(MusicStorage::Role_FreeSpace).toLongLong();
|
|
|
|
|
|
|
|
if (!capacity) {
|
|
|
|
ui_->free_space->hide();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui_->free_space->show();
|
|
|
|
ui_->free_space->set_free_bytes(free);
|
|
|
|
ui_->free_space->set_total_bytes(capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the format object
|
|
|
|
format_.set_format(ui_->naming->toPlainText());
|
2020-04-09 19:59:31 +02:00
|
|
|
format_.set_remove_problematic(ui_->remove_problematic->isChecked());
|
2018-12-29 15:37:16 +01:00
|
|
|
format_.set_remove_non_fat(ui_->remove_non_fat->isChecked());
|
|
|
|
format_.set_remove_non_ascii(ui_->remove_non_ascii->isChecked());
|
2019-03-22 23:18:14 +01:00
|
|
|
format_.set_allow_ascii_ext(ui_->allow_ascii_ext->isChecked());
|
2018-02-27 18:06:05 +01:00
|
|
|
format_.set_replace_spaces(ui_->replace_spaces->isChecked());
|
|
|
|
|
|
|
|
const bool format_valid = !has_local_destination || format_.IsValid();
|
|
|
|
|
2019-08-22 18:45:32 +02:00
|
|
|
// Are we going to enable the ok button?
|
2018-02-27 18:06:05 +01:00
|
|
|
bool ok = format_valid && !songs_.isEmpty();
|
|
|
|
if (capacity != 0 && total_size_ > free) ok = false;
|
|
|
|
|
|
|
|
ui_->button_box->button(QDialogButtonBox::Ok)->setEnabled(ok);
|
|
|
|
if (!format_valid) return;
|
|
|
|
|
|
|
|
new_songs_info_ = ComputeNewSongsFilenames(songs_, format_);
|
|
|
|
|
|
|
|
// Update the previews
|
|
|
|
ui_->preview->clear();
|
2018-12-29 15:37:16 +01:00
|
|
|
ui_->groupbox_preview->setVisible(has_local_destination);
|
|
|
|
ui_->groupbox_naming->setVisible(has_local_destination);
|
2018-02-27 18:06:05 +01:00
|
|
|
if (has_local_destination) {
|
2020-08-04 21:18:14 +02:00
|
|
|
for (const Organize::NewSongInfo &song_info : new_songs_info_) {
|
2018-02-27 18:06:05 +01:00
|
|
|
QString filename = storage->LocalPath() + "/" + song_info.new_filename_;
|
|
|
|
ui_->preview->addItem(QDir::toNativeSeparators(filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
QSize OrganizeDialog::sizeHint() const { return QSize(650, 0); }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::OrganizeFinished(const QStringList files_with_errors, const QStringList log) {
|
2018-02-27 18:06:05 +01:00
|
|
|
if (files_with_errors.isEmpty()) return;
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
error_dialog_.reset(new OrganizeErrorDialog);
|
|
|
|
error_dialog_->Show(OrganizeErrorDialog::Type_Copy, files_with_errors, log);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
void OrganizeDialog::AllowExtASCII(bool checked) {
|
2019-03-22 23:18:14 +01:00
|
|
|
ui_->allow_ascii_ext->setEnabled(checked);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|