transmission/qt/MakeDialog.cc

334 lines
8.3 KiB
C++
Raw Normal View History

// This file Copyright © Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
2009-04-09 18:55:47 +00:00
#include "MakeDialog.h"
#include <chrono>
#include <cmath>
#include <future>
2022-08-17 16:08:36 +00:00
#include <utility>
Qt 6 support (#2069) * Bump minimum Qt version to 5.6 * Switch from QRegExp to QRegularExpression While still available, QRegExp has been moved to Qt6::Core5Compat module and is not part of Qt6::Core. * Use qIsEffectiveTLD instead of QUrl::topLevelDomain The latter is not part of Qt6::Core. The former is a private utility in Qt6::Network; using it for now, until (and if) we switch to something non-Qt-specific. * Use QStyle::State_Horizontal state when drawing progress bars Although available for a long time, this state either didn't apply to progress bars before Qt 6, or was deduced based on bar size. With Qt 6, failing to specify it results in bad rendering. * Don't use QStringRef (and associated methods) While still available, QStringRef has been moved to Qt6::Core5Compat module and is not part of Qt6::Core. Related method (e.g. QString::midRef) have been removed in Qt 6. * Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate The latter was deprecated and replaced with the former in Qt 5.6. * Don't use QApplication::globalStrut This property has been deprecated in Qt 5.15 and removed in Qt 6. * Use QImage::fromHICON instead of QtWin::fromHICON WinExtras module (providind the latter helper) has been removed in Qt 6. * Use QStringDecoder instead of QTextCodec While still available, QTextCodec has been moved to Qt6::Core5Compat module and is not part of Qt6::Core. * Don't forward-declare QStringList Instead of being a standalone class, its definition has changed to QList<QString> template specialization in Qt 6. * Use explicit (since Qt 6) QFileInfo constructor * Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t The latter was deprecated in Qt 5.8 and removed in Qt 6. * Don't use QFuture<>'s operator== It has been removed in Qt 6. Since the original issue this code was solving was caused by future reuse, just don't reuse futures and create new finished ones when necessary. * Use std::vector<> instead of QVector<> The latter has been changed to a typedef for QList<>, which might not be what one wants, and which also changed behavior a bit leading to compilation errors. * Don't use + for flags, cast to int explicitly Operator+ for enum values has been deleted in Qt 6, so using operator| instead. Then, there's no conversion from QFlags<> to QVariant, so need to cast to int. * Support Qt 6 in CMake and for MSI packaging * Remove extra (empty) CMake variable use when constructing Qt target names * Simplify logic in tr_qt_add_translation CMake helper Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-03 21:20:11 +00:00
#include <vector>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileInfo>
2013-07-27 21:58:14 +00:00
#include <QMimeData>
#include <QPushButton>
#include <QString>
#include <QTimer>
2009-04-09 18:55:47 +00:00
#include <libtransmission/error.h>
2009-04-09 18:55:47 +00:00
#include <libtransmission/makemeta.h>
#include <libtransmission/transmission.h>
2009-04-09 18:55:47 +00:00
#include "ColumnResizer.h"
#include "Formatter.h"
#include "Session.h"
#include "Utils.h"
2009-04-09 18:55:47 +00:00
#include "ui_MakeProgressDialog.h"
2009-04-09 18:55:47 +00:00
namespace
2009-04-09 18:55:47 +00:00
{
class MakeProgressDialog : public BaseDialog
{
Q_OBJECT
public:
MakeProgressDialog(
Session& session,
tr_metainfo_builder& builder,
std::future<tr_error> future,
QString outfile,
QWidget* parent = nullptr);
private slots:
void onButtonBoxClicked(QAbstractButton* button);
void onProgress();
private:
Session& session_;
tr_metainfo_builder& builder_;
std::future<tr_error> future_;
QString const outfile_;
Ui::MakeProgressDialog ui_ = {};
QTimer timer_;
};
} // namespace
MakeProgressDialog::MakeProgressDialog(
Session& session,
tr_metainfo_builder& builder,
std::future<tr_error> future,
QString outfile,
QWidget* parent)
: BaseDialog{ parent }
, session_{ session }
, builder_{ builder }
, future_{ std::move(future) }
, outfile_{ std::move(outfile) }
{
ui_.setupUi(this);
connect(ui_.dialogButtons, &QDialogButtonBox::clicked, this, &MakeProgressDialog::onButtonBoxClicked);
connect(&timer_, &QTimer::timeout, this, &MakeProgressDialog::onProgress);
timer_.start(100);
2009-04-09 18:55:47 +00:00
onProgress();
2009-04-09 18:55:47 +00:00
}
void MakeProgressDialog::onButtonBoxClicked(QAbstractButton* button)
2009-04-09 18:55:47 +00:00
{
switch (ui_.dialogButtons->standardButton(button))
{
case QDialogButtonBox::Open:
session_.addNewlyCreatedTorrent(outfile_, QFileInfo{ QString::fromStdString(builder_.top()) }.dir().path());
2013-09-14 22:45:04 +00:00
break;
2009-04-09 18:55:47 +00:00
case QDialogButtonBox::Abort:
builder_.cancel_checksums();
2013-09-14 22:45:04 +00:00
break;
default: // QDialogButtonBox::Ok:
2013-09-14 22:45:04 +00:00
break;
}
2013-09-14 22:45:04 +00:00
close();
2009-04-09 18:55:47 +00:00
}
void MakeProgressDialog::onProgress()
2009-04-09 18:55:47 +00:00
{
auto const is_done = !future_.valid() || future_.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
if (is_done)
{
timer_.stop();
}
// progress bar
fix: more clang-tidy warnings (#6608) * fix: readability-redundant-casting warnings in gtk * fix: bugprone-move-forwarding-reference warnings in gtk * fix: readability-redundant-casting warnings in qt * fix: bugprone-switch-missing-default-case warnings in qt * fix: readability-use-std-min-max warning in qt client * fix: readability-static-accessed-through-instance warning in qt client * fix: cppcoreguidelines-avoid-const-or-ref-data-members warning in qt client * fix: readability-avoid-nested-conditional-operator warning in qt client * fixup! fix: readability-use-std-min-max warning in qt client * fix: readability-redundant-member-init warnings in gtk client * fix: performance-avoid-endl warnings in gtk client * chore: disable readability-qualified-auto too many false warnings * chore: disable cppcoreguidelines-avoid-const-or-ref-data-members * chore: fix readability-duplicate-include warning in gtk client * chore: fix modernize-use-nodiscard warning in gtk client * chore: fix readability-convert-member-functions-to-static warning in gtk client * fixup! fix: bugprone-move-forwarding-reference warnings in gtk * chore: fix performance-enum-size warning in gtk client * fix: cppcoreguidelines-prefer-member-initializer warning in gtk client * fix: readability-identifier-naming warning in qt client * Revert "chore: fix performance-enum-size warning in gtk client" This reverts commit 5ce6b562f849c2499fa34f4d903234f1945f9c3e. * fix: readability-redundant-member-init warning in move tests * fix: readability-redundant-casting warnings in tests * fixup! fix: readability-identifier-naming warning in qt client * fixup! fix: readability-avoid-nested-conditional-operator warning in qt client * fix: readability-static-accessed-through-instance warning in qt client * fix: readability-redundant-casting warning in watchdir tests
2024-02-17 19:31:49 +00:00
auto progress = 100; // [0..100]
if (!is_done)
{
auto const [current, total] = builder_.checksum_status();
progress = static_cast<int>((100.0 * current) / total);
}
ui_.progressBar->setValue(progress);
// progress label
auto const top = QString::fromStdString(builder_.top());
auto const base = QFileInfo{ top }.completeBaseName();
QString str;
auto success = false;
if (!is_done)
{
str = tr("Creating \"%1\"").arg(base);
}
else
{
auto error = future_.get();
if (!error)
{
builder_.save(outfile_.toStdString(), &error);
}
if (!error)
{
str = tr("Created \"%1\"!").arg(base);
success = true;
}
else
{
auto err_msg = QString::fromUtf8(std::data(error.message()), std::size(error.message()));
str = tr("Couldn't create \"%1\": %2 (%3)").arg(base).arg(err_msg).arg(error.code());
}
}
ui_.progressLabel->setText(str);
// buttons
ui_.dialogButtons->button(QDialogButtonBox::Abort)->setEnabled(!is_done);
ui_.dialogButtons->button(QDialogButtonBox::Ok)->setEnabled(is_done);
ui_.dialogButtons->button(QDialogButtonBox::Open)->setEnabled(is_done && success);
2009-04-09 18:55:47 +00:00
}
#include "MakeDialog.moc"
/***
****
***/
void MakeDialog::makeTorrent()
2009-04-09 18:55:47 +00:00
{
if (!builder_)
{
return;
}
2013-09-14 22:45:04 +00:00
// get the announce list
auto trackers = tr_announce_list();
trackers.parse(ui_.trackersEdit->toPlainText().toStdString());
builder_->set_announce_list(std::move(trackers));
2009-04-09 18:55:47 +00:00
// the file to create
auto const path = QString::fromStdString(builder_->top());
auto const torrent_name = QFileInfo{ path }.completeBaseName() + QStringLiteral(".torrent");
auto const outfile = QDir{ ui_.destinationButton->path() }.filePath(torrent_name);
// comment
if (ui_.commentCheck->isChecked())
{
builder_->set_comment(ui_.commentEdit->text().toStdString());
}
// source
if (ui_.sourceCheck->isChecked())
{
builder_->set_source(ui_.sourceEdit->text().toStdString());
}
builder_->set_private(ui_.privateCheck->isChecked());
// pop up the dialog
auto* dialog = new MakeProgressDialog{ session_, *builder_, builder_->make_checksums(), outfile, this };
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->open();
2009-04-09 18:55:47 +00:00
}
/***
****
***/
QString MakeDialog::getSource() const
{
return (ui_.sourceFileRadio->isChecked() ? ui_.sourceFileButton : ui_.sourceFolderButton)->path();
2009-04-09 18:55:47 +00:00
}
/***
****
***/
void MakeDialog::onSourceChanged()
2009-04-09 18:55:47 +00:00
{
builder_.reset();
2009-04-09 18:55:47 +00:00
if (auto const filename = getSource(); !filename.isEmpty())
2013-09-14 22:45:04 +00:00
{
builder_.emplace(filename.toStdString());
2013-09-14 22:45:04 +00:00
}
updatePiecesLabel();
if (builder_)
{
ui_.pieceSizeSlider->setValue(log2(builder_->piece_size()));
2009-04-09 18:55:47 +00:00
}
}
MakeDialog::MakeDialog(Session& session, QWidget* parent)
: BaseDialog{ parent }
, session_{ session }
2009-04-09 18:55:47 +00:00
{
ui_.setupUi(this);
ui_.destinationButton->setMode(PathButton::DirectoryMode);
ui_.destinationButton->setPath(QDir::homePath());
ui_.sourceFolderButton->setMode(PathButton::DirectoryMode);
ui_.sourceFileButton->setMode(PathButton::FileMode);
auto* cr = new ColumnResizer{ this };
cr->addLayout(ui_.filesSectionLayout);
cr->addLayout(ui_.propertiesSectionLayout);
cr->update();
resize(minimumSizeHint());
connect(ui_.sourceFolderRadio, &QAbstractButton::toggled, this, &MakeDialog::onSourceChanged);
connect(ui_.sourceFolderButton, &PathButton::pathChanged, this, &MakeDialog::onSourceChanged);
connect(ui_.sourceFileRadio, &QAbstractButton::toggled, this, &MakeDialog::onSourceChanged);
connect(ui_.sourceFileButton, &PathButton::pathChanged, this, &MakeDialog::onSourceChanged);
connect(ui_.dialogButtons, &QDialogButtonBox::accepted, this, &MakeDialog::makeTorrent);
connect(ui_.dialogButtons, &QDialogButtonBox::rejected, this, &MakeDialog::close);
connect(ui_.pieceSizeSlider, &QSlider::valueChanged, this, &MakeDialog::onPieceSizeUpdated);
onSourceChanged();
2009-04-09 18:55:47 +00:00
}
/***
****
***/
void MakeDialog::dragEnterEvent(QDragEnterEvent* event)
{
QMimeData const* mime = event->mimeData();
if (!mime->urls().isEmpty() && QFileInfo{ mime->urls().front().path() }.exists())
{
event->acceptProposedAction();
}
}
void MakeDialog::dropEvent(QDropEvent* event)
{
auto const filename = event->mimeData()->urls().front().path();
auto const file_info = QFileInfo{ filename };
if (file_info.exists())
{
if (file_info.isDir())
{
ui_.sourceFolderRadio->setChecked(true);
ui_.sourceFolderButton->setPath(filename);
}
else // it's a file
{
ui_.sourceFileRadio->setChecked(true);
ui_.sourceFileButton->setPath(filename);
}
}
}
void MakeDialog::updatePiecesLabel()
{
QString text;
if (!builder_)
{
text = tr("<i>No source selected</i>");
ui_.pieceSizeSlider->setEnabled(false);
}
else
{
auto const files = tr("%Ln File(s)", nullptr, builder_->file_count());
auto const pieces = tr("%Ln Piece(s)", nullptr, builder_->piece_count());
text = tr("%1 in %2; %3 @ %4")
.arg(Formatter::storage_to_string(builder_->total_size()))
.arg(files)
.arg(pieces)
.arg(Formatter::memory_to_string(static_cast<uint64_t>(builder_->piece_size())));
ui_.pieceSizeSlider->setEnabled(true);
}
ui_.sourceSizeLabel->setText(text);
}
void MakeDialog::onPieceSizeUpdated(int value)
{
auto new_size = static_cast<uint64_t>(pow(2, value));
if (builder_)
{
builder_->set_piece_size(new_size);
}
updatePiecesLabel();
}