2010-06-24 18:26:49 +02:00
|
|
|
/* 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"
|
2010-06-24 20:33:38 +02:00
|
|
|
#include "core/organise.h"
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QMenu>
|
2010-06-24 19:34:23 +02:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSettings>
|
2010-06-24 18:26:49 +02:00
|
|
|
#include <QSignalMapper>
|
|
|
|
|
|
|
|
const int OrganiseDialog::kNumberOfPreviews = 5;
|
|
|
|
const char* OrganiseDialog::kDefaultFormat =
|
|
|
|
"%artist/%album{ (Disc %disc)}/{%track - }%title.%extension";
|
2010-06-24 19:34:23 +02:00
|
|
|
const char* OrganiseDialog::kSettingsGroup = "OrganiseDialog";
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2010-06-24 20:33:38 +02:00
|
|
|
OrganiseDialog::OrganiseDialog(TaskManager* task_manager, QWidget *parent)
|
2010-06-24 18:26:49 +02:00
|
|
|
: QDialog(parent),
|
2010-06-24 20:33:38 +02:00
|
|
|
ui_(new Ui_OrganiseDialog),
|
|
|
|
task_manager_(task_manager)
|
2010-06-24 18:26:49 +02:00
|
|
|
{
|
|
|
|
ui_->setupUi(this);
|
2010-06-24 19:34:23 +02:00
|
|
|
connect(ui_->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(Reset()));
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
// 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_->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_;
|
|
|
|
}
|
|
|
|
|
2010-06-24 19:34:23 +02:00
|
|
|
void OrganiseDialog::AddDirectoryModel(QAbstractItemModel *model) {
|
|
|
|
// TODO: Add this model to a proxy model that merges different models
|
|
|
|
// together, eg. from the local library and also removable devices.
|
|
|
|
ui_->destination->setModel(model);
|
|
|
|
}
|
|
|
|
|
2010-06-24 18:26:49 +02:00
|
|
|
void OrganiseDialog::SetUrls(const QList<QUrl> &urls) {
|
|
|
|
QStringList filenames;
|
|
|
|
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) {
|
|
|
|
Song song;
|
|
|
|
song.InitFromFile(filenames_[i], -1);
|
|
|
|
preview_songs_ << song;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdatePreviews();
|
|
|
|
}
|
|
|
|
|
2010-06-24 22:26:17 +02:00
|
|
|
void OrganiseDialog::SetCopy(bool copy) {
|
|
|
|
ui_->move->setChecked(!copy);
|
|
|
|
}
|
|
|
|
|
2010-06-24 18:26:49 +02:00
|
|
|
void OrganiseDialog::InsertTag(const QString &tag) {
|
|
|
|
ui_->naming->insertPlainText("%" + tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OrganiseDialog::UpdatePreviews() {
|
|
|
|
// 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());
|
|
|
|
|
2010-06-24 19:34:23 +02:00
|
|
|
const bool format_valid = format_.IsValid();
|
|
|
|
ui_->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
|
|
|
|
format_valid && !ui_->destination->currentText().isEmpty());
|
|
|
|
if (!format_valid)
|
2010-06-24 18:26:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
ui_->preview->clear();
|
|
|
|
foreach (const Song& song, preview_songs_) {
|
|
|
|
QString filename = format_.GetFilenameForSong(song);
|
|
|
|
ui_->preview->addItem(QDir::toNativeSeparators(filename));
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 19:34:23 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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-24 20:33:38 +02:00
|
|
|
// It deletes itself when it's finished.
|
|
|
|
Organise* organise = new Organise(
|
2010-06-24 22:26:17 +02:00
|
|
|
task_manager_, ui_->destination->currentText(), format_,
|
|
|
|
!ui_->move->isChecked(), ui_->overwrite->isChecked(), filenames_);
|
2010-06-24 20:33:38 +02:00
|
|
|
organise->Start();
|
|
|
|
|
2010-06-24 19:34:23 +02:00
|
|
|
QDialog::accept();
|
|
|
|
}
|