transmission/qt/OptionsDialog.cc

507 lines
13 KiB
C++
Raw Normal View History

2009-04-09 18:55:47 +00:00
/*
* This file Copyright (C) 2009-2015 Mnemosyne LLC
2009-04-09 18:55:47 +00:00
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
2009-04-09 18:55:47 +00:00
*
*/
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 <algorithm>
2009-10-06 00:27:26 +00:00
#include <QFileInfo>
2009-04-09 18:55:47 +00:00
#include <QPushButton>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> /* mime64 */
#include <libtransmission/variant.h>
2009-04-09 18:55:47 +00:00
#include "AddData.h"
#include "FreeSpaceLabel.h"
#include "OptionsDialog.h"
#include "Prefs.h"
#include "Session.h"
#include "Torrent.h"
#include "Utils.h"
#include "VariantHelpers.h"
using ::trqt::variant_helpers::dictAdd;
using ::trqt::variant_helpers::listAdd;
2009-04-09 18:55:47 +00:00
/***
****
***/
OptionsDialog::OptionsDialog(Session& session, Prefs const& prefs, AddData addme, QWidget* parent)
: BaseDialog(parent)
, add_(std::move(addme))
, verify_button_(new QPushButton(tr("&Verify Local Data"), this))
, session_(session)
, is_local_(session_.isLocal())
2009-04-09 18:55:47 +00:00
{
ui_.setupUi(this);
QString title;
if (add_.type == AddData::FILENAME)
{
title = tr("Open Torrent from File");
}
else
{
title = tr("Open Torrent from URL or Magnet Link");
}
setWindowTitle(title);
edit_timer_.setInterval(2000);
edit_timer_.setSingleShot(true);
connect(&edit_timer_, &QTimer::timeout, this, &OptionsDialog::onDestinationChanged);
if (add_.type == AddData::FILENAME)
{
ui_.sourceStack->setCurrentWidget(ui_.sourceButton);
ui_.sourceButton->setMode(PathButton::FileMode);
ui_.sourceButton->setTitle(tr("Open Torrent"));
ui_.sourceButton->setNameFilter(tr("Torrent Files (*.torrent);;All Files (*.*)"));
ui_.sourceButton->setPath(add_.filename);
connect(ui_.sourceButton, &PathButton::pathChanged, this, &OptionsDialog::onSourceChanged);
}
else
{
ui_.sourceStack->setCurrentWidget(ui_.sourceEdit);
ui_.sourceEdit->setText(add_.readableName());
ui_.sourceEdit->selectAll();
connect(ui_.sourceEdit, &QLineEdit::editingFinished, this, &OptionsDialog::onSourceChanged);
}
ui_.sourceStack->setFixedHeight(ui_.sourceStack->currentWidget()->sizeHint().height());
ui_.sourceLabel->setBuddy(ui_.sourceStack->currentWidget());
QFontMetrics const font_metrics(font());
int const width = font_metrics.size(0, QStringLiteral("This is a pretty long torrent filename indeed.torrent")).width();
ui_.sourceStack->setMinimumWidth(width);
QString const download_dir(Utils::removeTrailingDirSeparator(prefs.getString(Prefs::DOWNLOAD_DIR)));
ui_.freeSpaceLabel->setSession(session_);
ui_.freeSpaceLabel->setPath(download_dir);
ui_.destinationButton->setMode(PathButton::DirectoryMode);
ui_.destinationButton->setTitle(tr("Select Destination"));
ui_.destinationButton->setPath(download_dir);
ui_.destinationEdit->setText(download_dir);
if (is_local_)
{
local_destination_.setPath(download_dir);
}
connect(ui_.destinationButton, &PathButton::pathChanged, this, &OptionsDialog::onDestinationChanged);
connect(ui_.destinationEdit, &QLineEdit::textEdited, &edit_timer_, qOverload<>(&QTimer::start));
connect(ui_.destinationEdit, &QLineEdit::editingFinished, this, &OptionsDialog::onDestinationChanged);
ui_.filesView->setEditable(false);
ui_.priorityCombo->addItem(tr("High"), TR_PRI_HIGH);
ui_.priorityCombo->addItem(tr("Normal"), TR_PRI_NORMAL);
ui_.priorityCombo->addItem(tr("Low"), TR_PRI_LOW);
ui_.priorityCombo->setCurrentIndex(1); // Normal
ui_.dialogButtons->addButton(verify_button_, QDialogButtonBox::ActionRole);
connect(verify_button_, &QAbstractButton::clicked, this, &OptionsDialog::onVerify);
2009-04-09 18:55:47 +00:00
ui_.startCheck->setChecked(prefs.getBool(Prefs::START));
ui_.trashCheck->setChecked(prefs.getBool(Prefs::TRASH_ORIGINAL));
2009-04-09 18:55:47 +00:00
connect(ui_.dialogButtons, &QDialogButtonBox::rejected, this, &QObject::deleteLater);
connect(ui_.dialogButtons, &QDialogButtonBox::accepted, this, &OptionsDialog::onAccepted);
2009-04-09 18:55:47 +00:00
connect(ui_.filesView, &FileTreeView::priorityChanged, this, &OptionsDialog::onPriorityChanged);
connect(ui_.filesView, &FileTreeView::wantedChanged, this, &OptionsDialog::onWantedChanged);
connect(&verify_timer_, &QTimer::timeout, this, &OptionsDialog::onTimeout);
2009-04-09 18:55:47 +00:00
connect(&session_, &Session::sessionUpdated, this, &OptionsDialog::onSessionUpdated);
updateWidgetsLocality();
reload();
2009-04-09 18:55:47 +00:00
}
OptionsDialog::~OptionsDialog()
2009-04-09 18:55:47 +00:00
{
clearInfo();
2009-04-09 18:55:47 +00:00
}
/***
****
***/
void OptionsDialog::clearInfo()
2009-04-09 18:55:47 +00:00
{
if (have_info_)
{
tr_metainfoFree(&info_);
}
have_info_ = false;
files_.clear();
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::reload()
2009-04-09 18:55:47 +00:00
{
clearInfo();
clearVerify();
tr_ctor* ctor = tr_ctorNew(nullptr);
2009-04-09 18:55:47 +00:00
switch (add_.type)
{
case AddData::MAGNET:
tr_ctorSetMetainfoFromMagnetLink(ctor, add_.magnet.toUtf8().constData());
break;
case AddData::FILENAME:
tr_ctorSetMetainfoFromFile(ctor, add_.filename.toUtf8().constData());
break;
case AddData::METAINFO:
tr_ctorSetMetainfo(ctor, add_.metainfo.constData(), add_.metainfo.size());
break;
default:
break;
}
int const err = tr_torrentParse(ctor, &info_);
have_info_ = err == 0;
tr_ctorFree(ctor);
2009-04-09 18:55:47 +00:00
ui_.filesView->clear();
files_.clear();
priorities_.clear();
wanted_.clear();
2009-04-09 18:55:47 +00:00
bool const have_files_to_show = have_info_ && info_.fileCount > 0;
ui_.filesView->setVisible(have_files_to_show);
verify_button_->setEnabled(have_files_to_show);
layout()->setSizeConstraint(have_files_to_show ? QLayout::SetDefaultConstraint : QLayout::SetFixedSize);
if (have_info_)
2009-04-09 18:55:47 +00:00
{
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
priorities_.assign(info_.fileCount, TR_PRI_NORMAL);
wanted_.assign(info_.fileCount, true);
for (tr_file_index_t i = 0; i < info_.fileCount; ++i)
{
TorrentFile file;
file.index = i;
file.priority = priorities_[i];
file.wanted = wanted_[i];
file.size = info_.files[i].length;
file.have = 0;
file.filename = QString::fromUtf8(info_.files[i].name);
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
files_.push_back(file);
2009-04-09 18:55:47 +00:00
}
}
ui_.filesView->update(files_);
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::updateWidgetsLocality()
{
ui_.destinationStack->setCurrentWidget(is_local_ ? static_cast<QWidget*>(ui_.destinationButton) : ui_.destinationEdit);
ui_.destinationStack->setFixedHeight(ui_.destinationStack->currentWidget()->sizeHint().height());
ui_.destinationLabel->setBuddy(ui_.destinationStack->currentWidget());
// hide the % done when non-local, since we've no way of knowing
(ui_.filesView->*(is_local_ ? &QTreeView::showColumn : &QTreeView::hideColumn))(2);
verify_button_->setVisible(is_local_);
}
void OptionsDialog::onSessionUpdated()
{
bool const is_local = session_.isLocal();
if (is_local_ != is_local)
{
is_local_ = is_local;
updateWidgetsLocality();
}
}
void OptionsDialog::onPriorityChanged(QSet<int> const& file_indices, int priority)
2009-04-09 18:55:47 +00:00
{
for (int const i : file_indices)
{
priorities_[i] = priority;
}
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::onWantedChanged(QSet<int> const& file_indices, bool is_wanted)
2009-04-09 18:55:47 +00:00
{
for (int const i : file_indices)
{
wanted_[i] = is_wanted;
}
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::onAccepted()
2009-04-09 18:55:47 +00:00
{
// rpc spec section 3.4 "adding a torrent"
tr_variant args;
tr_variantInitDict(&args, 10);
QString download_dir;
// "download-dir"
if (ui_.destinationStack->currentWidget() == ui_.destinationButton)
{
download_dir = local_destination_.absolutePath();
}
else
{
download_dir = ui_.destinationEdit->text();
}
dictAdd(&args, TR_KEY_download_dir, download_dir);
// paused
dictAdd(&args, TR_KEY_paused, !ui_.startCheck->isChecked());
// priority
int const index = ui_.priorityCombo->currentIndex();
int const priority = ui_.priorityCombo->itemData(index).toInt();
dictAdd(&args, TR_KEY_bandwidthPriority, priority);
// files-unwanted
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
auto count = std::count(wanted_.begin(), wanted_.end(), false);
if (count > 0)
{
tr_variant* l = tr_variantDictAddList(&args, TR_KEY_files_unwanted, count);
for (int i = 0, n = wanted_.size(); i < n; ++i)
{
if (!wanted_.at(i))
{
listAdd(l, i);
}
}
2009-04-09 18:55:47 +00:00
}
// priority-low
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
count = std::count(priorities_.begin(), priorities_.end(), TR_PRI_LOW);
if (count > 0)
{
tr_variant* l = tr_variantDictAddList(&args, TR_KEY_priority_low, count);
for (int i = 0, n = priorities_.size(); i < n; ++i)
{
if (priorities_.at(i) == TR_PRI_LOW)
{
listAdd(l, i);
}
}
2009-04-09 18:55:47 +00:00
}
// priority-high
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
count = std::count(priorities_.begin(), priorities_.end(), TR_PRI_HIGH);
if (count > 0)
{
tr_variant* l = tr_variantDictAddList(&args, TR_KEY_priority_high, count);
for (int i = 0, n = priorities_.size(); i < n; ++i)
{
if (priorities_.at(i) == TR_PRI_HIGH)
{
listAdd(l, i);
}
}
2009-04-09 18:55:47 +00:00
}
session_.addTorrent(add_, &args, ui_.trashCheck->isChecked());
2009-04-09 18:55:47 +00:00
deleteLater();
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::onSourceChanged()
2009-04-09 18:55:47 +00:00
{
if (ui_.sourceStack->currentWidget() == ui_.sourceButton)
{
add_.set(ui_.sourceButton->path());
}
else
{
add_.set(ui_.sourceEdit->text());
}
reload();
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::onDestinationChanged()
2009-04-09 18:55:47 +00:00
{
if (ui_.destinationStack->currentWidget() == ui_.destinationButton)
{
local_destination_.setPath(ui_.destinationButton->path());
ui_.freeSpaceLabel->setPath(local_destination_.absolutePath());
}
else
{
ui_.freeSpaceLabel->setPath(ui_.destinationEdit->text());
}
}
2009-04-09 18:55:47 +00:00
/***
****
**** VERIFY
****
***/
void OptionsDialog::clearVerify()
2009-04-09 18:55:47 +00:00
{
verify_hash_.reset();
verify_file_.close();
verify_file_pos_ = 0;
verify_flags_.clear();
verify_file_index_ = 0;
verify_piece_index_ = 0;
verify_piece_pos_ = 0;
verify_timer_.stop();
for (TorrentFile& f : files_)
{
f.have = 0;
}
ui_.filesView->update(files_);
2009-04-09 18:55:47 +00:00
}
void OptionsDialog::onVerify()
2009-04-09 18:55:47 +00:00
{
clearVerify();
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
verify_flags_.assign(info_.pieceCount, false);
verify_timer_.setSingleShot(false);
verify_timer_.start(0);
2009-04-09 18:55:47 +00:00
}
namespace
{
uint64_t getPieceSize(tr_info const* info, tr_piece_index_t piece_index)
{
if (piece_index != info->pieceCount - 1)
{
return info->pieceSize;
}
return info->totalSize % info->pieceSize;
2009-04-09 18:55:47 +00:00
}
} // namespace
void OptionsDialog::onTimeout()
2009-04-09 18:55:47 +00:00
{
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
if (files_.empty())
{
verify_timer_.stop();
return;
}
tr_file const* file = &info_.files[verify_file_index_];
2009-04-09 18:55:47 +00:00
if (verify_file_pos_ == 0 && !verify_file_.isOpen())
2009-04-09 18:55:47 +00:00
{
QFileInfo const file_info(local_destination_, QString::fromUtf8(file->name));
verify_file_.setFileName(file_info.absoluteFilePath());
verify_file_.open(QIODevice::ReadOnly);
2009-04-09 18:55:47 +00:00
}
int64_t left_in_piece = getPieceSize(&info_, verify_piece_index_) - verify_piece_pos_;
int64_t left_in_file = file->length - verify_file_pos_;
int64_t bytes_this_pass = qMin(left_in_file, left_in_piece);
bytes_this_pass = qMin(bytes_this_pass, static_cast<int64_t>(sizeof(verify_buf_)));
2009-04-09 18:55:47 +00:00
if (verify_file_.isOpen() && verify_file_.seek(verify_file_pos_))
{
int64_t num_read = verify_file_.read(verify_buf_, bytes_this_pass);
if (num_read == bytes_this_pass)
{
verify_hash_.addData(verify_buf_, num_read);
}
2009-04-09 18:55:47 +00:00
}
left_in_piece -= bytes_this_pass;
left_in_file -= bytes_this_pass;
verify_piece_pos_ += bytes_this_pass;
verify_file_pos_ += bytes_this_pass;
2009-04-09 18:55:47 +00:00
verify_bins_[verify_file_index_] += bytes_this_pass;
2009-04-09 18:55:47 +00:00
if (left_in_piece == 0)
2009-04-09 18:55:47 +00:00
{
QByteArray const result(verify_hash_.result());
bool const matches = memcmp(result.constData(), std::data(info_.pieces[verify_piece_index_]), SHA_DIGEST_LENGTH) == 0;
verify_flags_[verify_piece_index_] = matches;
verify_piece_pos_ = 0;
++verify_piece_index_;
verify_hash_.reset();
FileList changed_files;
if (matches)
{
for (auto i = verify_bins_.begin(), end = verify_bins_.end(); i != end; ++i)
{
TorrentFile& f(files_[i.key()]);
f.have += i.value();
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
changed_files.push_back(f);
2009-04-09 18:55:47 +00:00
}
}
ui_.filesView->update(changed_files);
verify_bins_.clear();
2009-04-09 18:55:47 +00:00
}
if (left_in_file == 0)
2009-04-09 18:55:47 +00:00
{
verify_file_.close();
++verify_file_index_;
verify_file_pos_ = 0;
2009-04-09 18:55:47 +00:00
}
bool done = verify_piece_index_ >= info_.pieceCount;
if (done)
{
uint64_t have = 0;
for (TorrentFile const& f : files_)
{
have += f.have;
}
if (have == 0) // everything failed
{
// did the user accidentally specify the child directory instead of the parent?
QStringList const tokens = QString::fromUtf8(file->name).split(QLatin1Char('/'));
if (!tokens.empty() && local_destination_.dirName() == tokens.at(0))
{
// move up one directory and try again
local_destination_.cdUp();
onVerify();
done = false;
}
}
}
2009-04-09 18:55:47 +00:00
if (done)
{
verify_timer_.stop();
}
2009-04-09 18:55:47 +00:00
}