Clementine-audio-player-Mac.../src/ui/organisedialog.cpp

229 lines
7.3 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Clementine 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.
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "organisedialog.h"
#include "ui_organisedialog.h"
#include "core/musicstorage.h"
#include "core/organise.h"
#include <QDir>
#include <QFileInfo>
#include <QMenu>
#include <QPushButton>
#include <QSettings>
#include <QSignalMapper>
#include <QtDebug>
const int OrganiseDialog::kNumberOfPreviews = 5;
const char* OrganiseDialog::kDefaultFormat =
"%artist/%album{ (Disc %disc)}/{%track - }%title.%extension";
const char* OrganiseDialog::kSettingsGroup = "OrganiseDialog";
OrganiseDialog::OrganiseDialog(TaskManager* task_manager, QWidget *parent)
: QDialog(parent),
ui_(new Ui_OrganiseDialog),
task_manager_(task_manager)
{
ui_->setupUi(this);
connect(ui_->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(Reset()));
// 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("Track")] = "track";
tags[tr("Disc")] = "disc";
tags[tr("BPM")] = "bpm";
tags[tr("Year")] = "year";
tags[tr("Genre")] = "genre";
tags[tr("Comment")] = "comment";
tags[tr("Length")] = "length";
tags[tr("Bitrate")] = "bitrate";
tags[tr("Samplerate")] = "samplerate";
tags[tr("File extension")] = "extension";
// Naming scheme input field
new OrganiseFormat::SyntaxHighlighter(ui_->naming);
connect(ui_->destination, SIGNAL(currentIndexChanged(int)), SLOT(UpdatePreviews()));
connect(ui_->naming, SIGNAL(textChanged()), SLOT(UpdatePreviews()));
connect(ui_->replace_ascii, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->replace_the, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->replace_spaces, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
// Get the titles of the tags to put in the insert menu
QStringList tag_titles = tags.keys();
qStableSort(tag_titles);
// Build the insert menu
QMenu* tag_menu = new QMenu(this);
QSignalMapper* tag_mapper = new QSignalMapper(this);
foreach (const QString& title, tag_titles) {
QAction* action = tag_menu->addAction(title, tag_mapper, SLOT(map()));
tag_mapper->setMapping(action, tags[title]);
}
connect(tag_mapper, SIGNAL(mapped(QString)), SLOT(InsertTag(QString)));
ui_->insert->setMenu(tag_menu);
}
OrganiseDialog::~OrganiseDialog() {
delete ui_;
}
void OrganiseDialog::SetDestinationModel(QAbstractItemModel *model) {
ui_->destination->setModel(model);
}
void OrganiseDialog::SetUrls(const QList<QUrl> &urls) {
QStringList filenames;
// Only add file:// URLs
foreach (const QUrl& url, urls) {
if (url.scheme() != "file")
continue;
filenames << url.toLocalFile();
}
SetFilenames(filenames);
}
void OrganiseDialog::SetFilenames(const QStringList& filenames) {
filenames_ = filenames;
preview_songs_.clear();
// Load some of the songs to show in the preview
const int n = qMin(filenames_.count(), kNumberOfPreviews);
for (int i=0 ; i<n ; ++i) {
LoadPreviewSongs(filenames_[i]);
}
UpdatePreviews();
}
void OrganiseDialog::LoadPreviewSongs(const QString& filename) {
if (preview_songs_.count() >= kNumberOfPreviews)
return;
if (QFileInfo(filename).isDir()) {
QDir dir(filename);
QStringList entries = dir.entryList(
QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
foreach (const QString& entry, entries) {
LoadPreviewSongs(filename + "/" + entry);
}
return;
}
Song song;
song.InitFromFile(filename, -1);
if (song.is_valid())
preview_songs_ << song;
}
void OrganiseDialog::SetCopy(bool copy) {
ui_->move->setChecked(!copy);
}
void OrganiseDialog::InsertTag(const QString &tag) {
ui_->naming->insertPlainText("%" + tag);
}
void OrganiseDialog::UpdatePreviews() {
const QModelIndex destination = ui_->destination->model()->index(
ui_->destination->currentIndex(), 0);
if (!destination.isValid())
return;
const MusicStorage* storage =
destination.data(MusicStorage::kStorageRole).value<MusicStorage*>();
const bool has_local_destination = !storage->LocalPath().isEmpty();
// Update the format object
format_.set_format(ui_->naming->toPlainText());
format_.set_replace_non_ascii(ui_->replace_ascii->isChecked());
format_.set_replace_spaces(ui_->replace_spaces->isChecked());
format_.set_replace_the(ui_->replace_the->isChecked());
const bool format_valid = format_.IsValid();
ui_->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(format_valid);
if (!format_valid)
return;
// Update the previews
ui_->preview->clear();
ui_->preview->setVisible(has_local_destination);
if (has_local_destination) {
foreach (const Song& song, preview_songs_) {
QString filename = storage->LocalPath() + "/" +
format_.GetFilenameForSong(song);
ui_->preview->addItem(QDir::toNativeSeparators(filename));
}
}
}
void OrganiseDialog::Reset() {
ui_->naming->setPlainText(kDefaultFormat);
ui_->replace_ascii->setChecked(false);
ui_->replace_spaces->setChecked(false);
ui_->replace_the->setChecked(false);
ui_->overwrite->setChecked(true);
}
void OrganiseDialog::showEvent(QShowEvent *) {
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
ui_->replace_ascii->setChecked(s.value("replace_ascii", false).toBool());
ui_->replace_spaces->setChecked(s.value("replace_spaces", false).toBool());
ui_->replace_the->setChecked(s.value("replace_the", false).toBool());
ui_->overwrite->setChecked(s.value("overwrite", true).toBool());
2010-06-25 01:40:04 +02:00
QString destination = s.value("destination").toString();
int index = ui_->destination->findText(destination);
if (index != -1 && !destination.isEmpty()) {
ui_->destination->setCurrentIndex(index);
}
}
void OrganiseDialog::accept() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("format", ui_->naming->toPlainText());
s.setValue("replace_ascii", ui_->replace_ascii->isChecked());
s.setValue("replace_spaces", ui_->replace_spaces->isChecked());
s.setValue("replace_the", ui_->replace_the->isChecked());
s.setValue("overwrite", ui_->overwrite->isChecked());
2010-06-25 01:40:04 +02:00
s.setValue("destination", ui_->destination->currentText());
const QModelIndex destination = ui_->destination->model()->index(
ui_->destination->currentIndex(), 0);
MusicStorage* storage =
destination.data(MusicStorage::kStorageRole).value<MusicStorage*>();
// It deletes itself when it's finished.
Organise* organise = new Organise(
task_manager_, storage, format_,
!ui_->move->isChecked(), ui_->overwrite->isChecked(), filenames_);
organise->Start();
QDialog::accept();
}