2010-05-03 20:52:35 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
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 "transcodedialog.h"
|
|
|
|
#include "transcoder.h"
|
2011-04-17 01:04:15 +02:00
|
|
|
#include "transcoderoptionsdialog.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "ui_transcodedialog.h"
|
|
|
|
#include "ui_transcodelogdialog.h"
|
|
|
|
#include "ui/mainwindow.h"
|
2012-01-29 21:10:00 +01:00
|
|
|
#include "widgets/fileview.h"
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QSettings>
|
2010-05-08 17:36:12 +02:00
|
|
|
#include <QDateTime>
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-05-03 22:01:23 +02:00
|
|
|
// winspool.h defines this :(
|
|
|
|
#ifdef AddJob
|
|
|
|
# undef AddJob
|
|
|
|
#endif
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
const char* TranscodeDialog::kSettingsGroup = "Transcoder";
|
2010-08-30 15:03:21 +02:00
|
|
|
const int TranscodeDialog::kProgressInterval = 500;
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-08-22 02:27:14 +02:00
|
|
|
static bool ComparePresetsByName(const TranscoderPreset& left,
|
|
|
|
const TranscoderPreset& right) {
|
|
|
|
return left.name_ < right.name_;
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TranscodeDialog::TranscodeDialog(QWidget *parent)
|
|
|
|
: QDialog(parent),
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_(new Ui_TranscodeDialog),
|
|
|
|
log_ui_(new Ui_TranscodeLogDialog),
|
2010-05-08 17:36:12 +02:00
|
|
|
log_dialog_(new QDialog(this)),
|
|
|
|
transcoder_(new Transcoder(this)),
|
|
|
|
queued_(0),
|
|
|
|
finished_success_(0),
|
|
|
|
finished_failed_(0)
|
2010-05-03 20:52:35 +02:00
|
|
|
{
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->setupUi(this);
|
|
|
|
ui_->files->header()->setResizeMode(QHeaderView::ResizeToContents);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
log_ui_->setupUi(log_dialog_);
|
2010-05-08 17:36:12 +02:00
|
|
|
|
2010-08-22 02:27:14 +02:00
|
|
|
// Get presets
|
2010-08-29 17:32:36 +02:00
|
|
|
QList<TranscoderPreset> presets = Transcoder::GetAllPresets();
|
2010-08-22 02:27:14 +02:00
|
|
|
qSort(presets.begin(), presets.end(), ComparePresetsByName);
|
|
|
|
foreach (const TranscoderPreset& preset, presets) {
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->format->addItem(
|
2010-08-22 02:27:14 +02:00
|
|
|
QString("%1 (.%2)").arg(preset.name_, preset.extension_),
|
|
|
|
QVariant::fromValue(preset));
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load settings
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
last_add_dir_ = s.value("last_add_dir", QDir::homePath()).toString();
|
|
|
|
|
2010-05-08 16:09:56 +02:00
|
|
|
QString last_output_format = s.value("last_output_format", "ogg").toString();
|
2010-05-10 23:50:31 +02:00
|
|
|
for (int i=0 ; i<ui_->format->count() ; ++i) {
|
2010-05-08 16:09:56 +02:00
|
|
|
if (last_output_format ==
|
2010-08-22 02:27:14 +02:00
|
|
|
ui_->format->itemData(i).value<TranscoderPreset>().extension_) {
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->format->setCurrentIndex(i);
|
2010-05-08 16:09:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
// Add a start button
|
2010-05-10 23:50:31 +02:00
|
|
|
start_button_ = ui_->button_box->addButton(
|
2010-05-03 20:52:35 +02:00
|
|
|
tr("Start transcoding"), QDialogButtonBox::ActionRole);
|
2010-05-10 23:50:31 +02:00
|
|
|
cancel_button_ = ui_->button_box->button(QDialogButtonBox::Cancel);
|
|
|
|
close_button_ = ui_->button_box->button(QDialogButtonBox::Close);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-12-14 19:23:14 +01:00
|
|
|
close_button_->setShortcut(QKeySequence::Close);
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
// Hide elements
|
|
|
|
cancel_button_->hide();
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->progress_group->hide();
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
// Connect stuff
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->add, SIGNAL(clicked()), SLOT(Add()));
|
|
|
|
connect(ui_->remove, SIGNAL(clicked()), SLOT(Remove()));
|
2010-05-03 20:52:35 +02:00
|
|
|
connect(start_button_, SIGNAL(clicked()), SLOT(Start()));
|
|
|
|
connect(cancel_button_, SIGNAL(clicked()), SLOT(Cancel()));
|
|
|
|
connect(close_button_, SIGNAL(clicked()), SLOT(hide()));
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->details, SIGNAL(clicked()), log_dialog_, SLOT(show()));
|
2011-04-17 01:04:15 +02:00
|
|
|
connect(ui_->options, SIGNAL(clicked()), SLOT(Options()));
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
connect(transcoder_, SIGNAL(JobComplete(QString,bool)), SLOT(JobComplete(QString,bool)));
|
2010-05-08 17:36:12 +02:00
|
|
|
connect(transcoder_, SIGNAL(LogLine(QString)), SLOT(LogLine(QString)));
|
2010-05-03 20:52:35 +02:00
|
|
|
connect(transcoder_, SIGNAL(AllJobsComplete()), SLOT(AllJobsComplete()));
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
TranscodeDialog::~TranscodeDialog() {
|
|
|
|
delete log_ui_;
|
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
2010-05-03 20:52:35 +02:00
|
|
|
void TranscodeDialog::SetWorking(bool working) {
|
|
|
|
start_button_->setVisible(!working);
|
|
|
|
cancel_button_->setVisible(working);
|
|
|
|
close_button_->setVisible(!working);
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->input_group->setEnabled(!working);
|
|
|
|
ui_->output_group->setEnabled(!working);
|
|
|
|
ui_->progress_group->setVisible(true);
|
2010-08-30 15:03:21 +02:00
|
|
|
|
|
|
|
if (working)
|
|
|
|
progress_timer_.start(kProgressInterval, this);
|
|
|
|
else
|
|
|
|
progress_timer_.stop();
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::Start() {
|
|
|
|
SetWorking(true);
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
QAbstractItemModel* file_model = ui_->files->model();
|
2010-08-22 02:27:14 +02:00
|
|
|
TranscoderPreset preset = ui_->format->itemData(
|
|
|
|
ui_->format->currentIndex()).value<TranscoderPreset>();
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-05-08 16:09:56 +02:00
|
|
|
// Add jobs to the transcoder
|
2010-05-03 20:52:35 +02:00
|
|
|
for (int i=0 ; i<file_model->rowCount() ; ++i) {
|
|
|
|
QString filename = file_model->index(i, 0).data(Qt::UserRole).toString();
|
2010-08-22 02:27:14 +02:00
|
|
|
transcoder_->AddJob(filename, preset);
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
|
|
|
|
2010-05-08 16:09:56 +02:00
|
|
|
// Set up the progressbar
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->progress_bar->setValue(0);
|
2010-08-30 15:03:21 +02:00
|
|
|
ui_->progress_bar->setMaximum(file_model->rowCount() * 100);
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-05-08 16:09:56 +02:00
|
|
|
// Reset the UI
|
2010-05-08 17:36:12 +02:00
|
|
|
queued_ = file_model->rowCount();
|
|
|
|
finished_success_ = 0;
|
|
|
|
finished_failed_ = 0;
|
|
|
|
UpdateStatusText();
|
2010-05-08 16:09:56 +02:00
|
|
|
|
|
|
|
// Start transcoding
|
2010-05-03 20:52:35 +02:00
|
|
|
transcoder_->Start();
|
2010-05-08 16:09:56 +02:00
|
|
|
|
|
|
|
// Save the last output format
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
2010-08-22 02:27:14 +02:00
|
|
|
s.setValue("last_output_format", preset.extension_);
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::Cancel() {
|
2010-05-06 18:28:19 +02:00
|
|
|
transcoder_->Cancel();
|
2010-05-03 20:52:35 +02:00
|
|
|
SetWorking(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::JobComplete(const QString& filename, bool success) {
|
2010-05-08 17:45:09 +02:00
|
|
|
(*(success ? &finished_success_ : &finished_failed_)) ++;
|
2010-05-08 17:36:12 +02:00
|
|
|
queued_ --;
|
|
|
|
|
|
|
|
UpdateStatusText();
|
2010-08-30 15:03:21 +02:00
|
|
|
UpdateProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::UpdateProgress() {
|
|
|
|
int progress = (finished_success_ + finished_failed_) * 100;
|
|
|
|
|
|
|
|
QMap<QString, float> current_jobs = transcoder_->GetProgress();
|
|
|
|
foreach (float value, current_jobs.values()) {
|
|
|
|
progress += qBound(0, int(value * 100), 99);
|
|
|
|
}
|
2010-05-08 17:36:12 +02:00
|
|
|
|
2010-08-30 15:03:21 +02:00
|
|
|
ui_->progress_bar->setValue(progress);
|
2010-05-08 17:36:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::UpdateStatusText() {
|
|
|
|
QStringList sections;
|
2010-05-03 20:52:35 +02:00
|
|
|
|
2010-05-08 17:45:09 +02:00
|
|
|
if (queued_) {
|
2010-05-08 17:36:12 +02:00
|
|
|
sections << "<font color=\"#3467c8\">" +
|
|
|
|
tr("%n remaining", "", queued_) + "</font>";
|
2010-05-08 17:45:09 +02:00
|
|
|
}
|
2010-05-08 17:36:12 +02:00
|
|
|
|
2010-05-08 17:45:09 +02:00
|
|
|
if (finished_success_) {
|
2010-05-08 17:36:12 +02:00
|
|
|
sections << "<font color=\"#02b600\">" +
|
|
|
|
tr("%n finished", "", finished_success_) + "</font>";
|
2010-05-08 17:45:09 +02:00
|
|
|
}
|
2010-05-08 17:36:12 +02:00
|
|
|
|
2010-05-08 17:45:09 +02:00
|
|
|
if (finished_failed_) {
|
2010-05-08 17:36:12 +02:00
|
|
|
sections << "<font color=\"#b60000\">" +
|
|
|
|
tr("%n failed", "", finished_failed_) + "</font>";
|
2010-05-08 17:45:09 +02:00
|
|
|
}
|
2010-05-08 17:36:12 +02:00
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->progress_text->setText(sections.join(", "));
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::AllJobsComplete() {
|
|
|
|
SetWorking(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::Add() {
|
|
|
|
QStringList filenames = QFileDialog::getOpenFileNames(
|
|
|
|
this, tr("Add files to transcode"), last_add_dir_,
|
2012-01-29 21:10:00 +01:00
|
|
|
QString("%1 (%2);;%3").arg(
|
|
|
|
tr("Music"),
|
|
|
|
FileView::kFileFilter,
|
|
|
|
tr(MainWindow::kAllFilesFilterSpec)));
|
2010-05-03 20:52:35 +02:00
|
|
|
|
|
|
|
if (filenames.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
foreach (const QString& filename, filenames) {
|
|
|
|
QString name = filename.section('/', -1, -1);
|
|
|
|
QString path = filename.section('/', 0, -2);
|
|
|
|
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->files, QStringList() << name << path);
|
2010-05-03 20:52:35 +02:00
|
|
|
item->setData(0, Qt::UserRole, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_add_dir_ = filenames[0];
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue("last_add_dir", last_add_dir_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranscodeDialog::Remove() {
|
2010-05-10 23:50:31 +02:00
|
|
|
qDeleteAll(ui_->files->selectedItems());
|
2010-05-03 20:52:35 +02:00
|
|
|
}
|
2010-05-08 17:36:12 +02:00
|
|
|
|
|
|
|
void TranscodeDialog::LogLine(const QString &message) {
|
|
|
|
QString date(QDateTime::currentDateTime().toString(Qt::TextDate));
|
2010-05-10 23:50:31 +02:00
|
|
|
log_ui_->log->appendPlainText(QString("%1: %2").arg(date, message));
|
2010-05-08 17:36:12 +02:00
|
|
|
}
|
2010-08-30 15:03:21 +02:00
|
|
|
|
|
|
|
void TranscodeDialog::timerEvent(QTimerEvent* e) {
|
|
|
|
QDialog::timerEvent(e);
|
|
|
|
|
|
|
|
if (e->timerId() == progress_timer_.timerId()) {
|
|
|
|
UpdateProgress();
|
|
|
|
}
|
|
|
|
}
|
2011-04-17 01:04:15 +02:00
|
|
|
|
|
|
|
void TranscodeDialog::Options() {
|
|
|
|
TranscoderPreset preset = ui_->format->itemData(
|
|
|
|
ui_->format->currentIndex()).value<TranscoderPreset>();
|
|
|
|
|
|
|
|
TranscoderOptionsDialog dialog(preset.type_, this);
|
|
|
|
if (dialog.is_valid()) {
|
|
|
|
dialog.exec();
|
|
|
|
}
|
|
|
|
}
|