Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / addnewtorrentdialog.cpp
blob11b083c3e78c343bc9621f113a388c9c70209cd9
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022-2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "addnewtorrentdialog.h"
32 #include <algorithm>
33 #include <functional>
35 #include <QAction>
36 #include <QByteArray>
37 #include <QDateTime>
38 #include <QDebug>
39 #include <QDir>
40 #include <QFileDialog>
41 #include <QList>
42 #include <QMenu>
43 #include <QMessageBox>
44 #include <QPushButton>
45 #include <QShortcut>
46 #include <QSignalBlocker>
47 #include <QSize>
48 #include <QString>
49 #include <QUrl>
51 #include "base/bittorrent/addtorrentparams.h"
52 #include "base/bittorrent/downloadpriority.h"
53 #include "base/bittorrent/infohash.h"
54 #include "base/bittorrent/session.h"
55 #include "base/bittorrent/torrent.h"
56 #include "base/bittorrent/torrentcontenthandler.h"
57 #include "base/bittorrent/torrentcontentlayout.h"
58 #include "base/bittorrent/torrentdescriptor.h"
59 #include "base/global.h"
60 #include "base/preferences.h"
61 #include "base/settingsstorage.h"
62 #include "base/torrentfileguard.h"
63 #include "base/utils/compare.h"
64 #include "base/utils/fs.h"
65 #include "base/utils/misc.h"
66 #include "base/utils/string.h"
67 #include "filterpatternformatmenu.h"
68 #include "lineedit.h"
69 #include "torrenttagsdialog.h"
71 #include "ui_addnewtorrentdialog.h"
73 namespace
75 #define SETTINGS_KEY(name) u"AddNewTorrentDialog/" name
76 const QString KEY_SAVEPATHHISTORY = SETTINGS_KEY(u"SavePathHistory"_s);
77 const QString KEY_DOWNLOADPATHHISTORY = SETTINGS_KEY(u"DownloadPathHistory"_s);
79 // just a shortcut
80 inline SettingsStorage *settings()
82 return SettingsStorage::instance();
85 // savePath is a folder, not an absolute file path
86 int indexOfPath(const FileSystemPathComboEdit *fsPathEdit, const Path &savePath)
88 for (int i = 0; i < fsPathEdit->count(); ++i)
90 if (fsPathEdit->item(i) == savePath)
91 return i;
93 return -1;
96 qint64 queryFreeDiskSpace(const Path &path)
98 const Path root = path.rootItem();
99 Path current = path;
100 qint64 freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
102 // for non-existent directories (which will be created on demand) `Utils::Fs::freeDiskSpaceOnPath`
103 // will return invalid value so instead query its parent/ancestor paths
104 while ((freeSpace < 0) && (current != root))
106 current = current.parentPath();
107 freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
109 return freeSpace;
112 void setPath(FileSystemPathComboEdit *fsPathEdit, const Path &newPath)
114 int existingIndex = indexOfPath(fsPathEdit, newPath);
115 if (existingIndex < 0)
117 // New path, prepend to combo box
118 fsPathEdit->insertItem(0, newPath);
119 existingIndex = 0;
122 fsPathEdit->setCurrentIndex(existingIndex);
125 void updatePathHistory(const QString &settingsKey, const Path &path, const int maxLength)
127 // Add last used save path to the front of history
129 auto pathList = settings()->loadValue<QStringList>(settingsKey);
131 const int selectedSavePathIndex = pathList.indexOf(path.toString());
132 if (selectedSavePathIndex > -1)
133 pathList.move(selectedSavePathIndex, 0);
134 else
135 pathList.prepend(path.toString());
137 settings()->storeValue(settingsKey, QStringList(pathList.mid(0, maxLength)));
141 class AddNewTorrentDialog::TorrentContentAdaptor final
142 : public BitTorrent::TorrentContentHandler
144 public:
145 TorrentContentAdaptor(const BitTorrent::TorrentInfo &torrentInfo, PathList &filePaths
146 , QList<BitTorrent::DownloadPriority> &filePriorities, std::function<void ()> onFilePrioritiesChanged)
147 : m_torrentInfo {torrentInfo}
148 , m_filePaths {filePaths}
149 , m_filePriorities {filePriorities}
150 , m_onFilePrioritiesChanged {std::move(onFilePrioritiesChanged)}
152 Q_ASSERT(filePaths.isEmpty() || (filePaths.size() == m_torrentInfo.filesCount()));
154 m_originalRootFolder = Path::findRootFolder(m_torrentInfo.filePaths());
155 m_currentContentLayout = (m_originalRootFolder.isEmpty()
156 ? BitTorrent::TorrentContentLayout::NoSubfolder
157 : BitTorrent::TorrentContentLayout::Subfolder);
159 if (const int fileCount = filesCount(); !m_filePriorities.isEmpty() && (fileCount >= 0))
160 m_filePriorities.resize(fileCount, BitTorrent::DownloadPriority::Normal);
163 bool hasMetadata() const override
165 return m_torrentInfo.isValid();
168 int filesCount() const override
170 return m_torrentInfo.filesCount();
173 qlonglong fileSize(const int index) const override
175 Q_ASSERT((index >= 0) && (index < filesCount()));
176 return m_torrentInfo.fileSize(index);
179 Path filePath(const int index) const override
181 Q_ASSERT((index >= 0) && (index < filesCount()));
182 return (m_filePaths.isEmpty() ? m_torrentInfo.filePath(index) : m_filePaths.at(index));
185 PathList filePaths() const
187 return (m_filePaths.isEmpty() ? m_torrentInfo.filePaths() : m_filePaths);
190 void renameFile(const int index, const Path &newFilePath) override
192 Q_ASSERT((index >= 0) && (index < filesCount()));
193 const Path currentFilePath = filePath(index);
194 if (currentFilePath == newFilePath)
195 return;
197 if (m_filePaths.isEmpty())
198 m_filePaths = m_torrentInfo.filePaths();
200 m_filePaths[index] = newFilePath;
203 void applyContentLayout(const BitTorrent::TorrentContentLayout contentLayout)
205 Q_ASSERT(hasMetadata());
206 Q_ASSERT(!m_filePaths.isEmpty());
208 const auto originalContentLayout = (m_originalRootFolder.isEmpty()
209 ? BitTorrent::TorrentContentLayout::NoSubfolder
210 : BitTorrent::TorrentContentLayout::Subfolder);
211 const auto newContentLayout = ((contentLayout == BitTorrent::TorrentContentLayout::Original)
212 ? originalContentLayout : contentLayout);
213 if (newContentLayout != m_currentContentLayout)
215 if (newContentLayout == BitTorrent::TorrentContentLayout::NoSubfolder)
217 Path::stripRootFolder(m_filePaths);
219 else
221 const auto rootFolder = ((originalContentLayout == BitTorrent::TorrentContentLayout::Subfolder)
222 ? m_originalRootFolder : m_filePaths.at(0).removedExtension());
223 Path::addRootFolder(m_filePaths, rootFolder);
226 m_currentContentLayout = newContentLayout;
230 QList<BitTorrent::DownloadPriority> filePriorities() const override
232 return m_filePriorities.isEmpty()
233 ? QList<BitTorrent::DownloadPriority>(filesCount(), BitTorrent::DownloadPriority::Normal)
234 : m_filePriorities;
237 QList<qreal> filesProgress() const override
239 return QList<qreal>(filesCount(), 0);
242 QList<qreal> availableFileFractions() const override
244 return QList<qreal>(filesCount(), 0);
247 void fetchAvailableFileFractions(std::function<void (QList<qreal>)> resultHandler) const override
249 resultHandler(availableFileFractions());
252 void prioritizeFiles(const QList<BitTorrent::DownloadPriority> &priorities) override
254 Q_ASSERT(priorities.size() == filesCount());
255 m_filePriorities = priorities;
256 if (m_onFilePrioritiesChanged)
257 m_onFilePrioritiesChanged();
260 Path actualStorageLocation() const override
262 return {};
265 Path actualFilePath([[maybe_unused]] int fileIndex) const override
267 return {};
270 void flushCache() const override
274 private:
275 const BitTorrent::TorrentInfo &m_torrentInfo;
276 PathList &m_filePaths;
277 QList<BitTorrent::DownloadPriority> &m_filePriorities;
278 std::function<void ()> m_onFilePrioritiesChanged;
279 Path m_originalRootFolder;
280 BitTorrent::TorrentContentLayout m_currentContentLayout;
283 struct AddNewTorrentDialog::Context
285 BitTorrent::TorrentDescriptor torrentDescr;
286 BitTorrent::AddTorrentParams torrentParams;
289 AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::TorrentDescriptor &torrentDescr
290 , const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
291 : QDialog(parent)
292 , m_ui {new Ui::AddNewTorrentDialog}
293 , m_filterLine {new LineEdit(this)}
294 , m_storeDialogSize {SETTINGS_KEY(u"DialogSize"_s)}
295 , m_storeDefaultCategory {SETTINGS_KEY(u"DefaultCategory"_s)}
296 , m_storeRememberLastSavePath {SETTINGS_KEY(u"RememberLastSavePath"_s)}
297 , m_storeTreeHeaderState {u"GUI/Qt6/" SETTINGS_KEY(u"TreeHeaderState"_s)}
298 , m_storeSplitterState {u"GUI/Qt6/" SETTINGS_KEY(u"SplitterState"_s)}
299 , m_storeFilterPatternFormat {u"GUI/" SETTINGS_KEY(u"FilterPatternFormat"_s)}
301 m_ui->setupUi(this);
303 m_ui->savePath->setMode(FileSystemPathEdit::Mode::DirectorySave);
304 m_ui->savePath->setDialogCaption(tr("Choose save path"));
305 m_ui->savePath->setMaxVisibleItems(20);
307 m_ui->downloadPath->setMode(FileSystemPathEdit::Mode::DirectorySave);
308 m_ui->downloadPath->setDialogCaption(tr("Choose save path"));
309 m_ui->downloadPath->setMaxVisibleItems(20);
311 m_ui->stopConditionComboBox->setToolTip(
312 u"<html><body><p><b>" + tr("None") + u"</b> - " + tr("No stop condition is set.") + u"</p><p><b>"
313 + tr("Metadata received") + u"</b> - " + tr("Torrent will stop after metadata is received.")
314 + u" <em>" + tr("Torrents that have metadata initially will be added as stopped.") + u"</em></p><p><b>"
315 + tr("Files checked") + u"</b> - " + tr("Torrent will stop after files are initially checked.")
316 + u" <em>" + tr("This will also download metadata if it wasn't there initially.") + u"</em></p></body></html>");
317 m_ui->stopConditionComboBox->addItem(tr("None"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::None));
318 m_ui->stopConditionComboBox->addItem(tr("Files checked"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::FilesChecked));
320 m_ui->checkBoxRememberLastSavePath->setChecked(m_storeRememberLastSavePath);
321 m_ui->doNotDeleteTorrentCheckBox->setVisible(TorrentFileGuard::autoDeleteMode() != TorrentFileGuard::Never);
323 // Torrent content filtering
324 m_filterLine->setPlaceholderText(tr("Filter files..."));
325 m_filterLine->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
326 m_filterLine->setContextMenuPolicy(Qt::CustomContextMenu);
327 connect(m_filterLine, &QWidget::customContextMenuRequested, this, &AddNewTorrentDialog::showContentFilterContextMenu);
328 m_ui->contentFilterLayout->insertWidget(3, m_filterLine);
329 const auto *focusSearchHotkey = new QShortcut(QKeySequence::Find, this);
330 connect(focusSearchHotkey, &QShortcut::activated, this, [this]()
332 m_filterLine->setFocus();
333 m_filterLine->selectAll();
336 loadState();
338 if (const QByteArray state = m_storeTreeHeaderState; !state.isEmpty())
339 m_ui->contentTreeView->header()->restoreState(state);
340 // Hide useless columns after loading the header state
341 m_ui->contentTreeView->hideColumn(TorrentContentWidget::Progress);
342 m_ui->contentTreeView->hideColumn(TorrentContentWidget::Remaining);
343 m_ui->contentTreeView->hideColumn(TorrentContentWidget::Availability);
344 m_ui->contentTreeView->setColumnsVisibilityMode(TorrentContentWidget::ColumnsVisibilityMode::Locked);
345 m_ui->contentTreeView->setDoubleClickAction(TorrentContentWidget::DoubleClickAction::Rename);
347 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
348 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
349 connect(m_ui->buttonSave, &QPushButton::clicked, this, &AddNewTorrentDialog::saveTorrentFile);
350 connect(m_ui->savePath, &FileSystemPathEdit::selectedPathChanged, this, &AddNewTorrentDialog::onSavePathChanged);
351 connect(m_ui->downloadPath, &FileSystemPathEdit::selectedPathChanged, this, &AddNewTorrentDialog::onDownloadPathChanged);
352 connect(m_ui->groupBoxDownloadPath, &QGroupBox::toggled, this, &AddNewTorrentDialog::onUseDownloadPathChanged);
353 connect(m_ui->comboTMM, &QComboBox::currentIndexChanged, this, &AddNewTorrentDialog::TMMChanged);
354 connect(m_ui->startTorrentCheckBox, &QCheckBox::toggled, this, [this](const bool checked)
356 m_ui->stopConditionLabel->setEnabled(checked);
357 m_ui->stopConditionComboBox->setEnabled(checked);
359 connect(m_ui->contentLayoutComboBox, &QComboBox::currentIndexChanged, this, &AddNewTorrentDialog::contentLayoutChanged);
360 connect(m_ui->categoryComboBox, &QComboBox::currentIndexChanged, this, &AddNewTorrentDialog::categoryChanged);
361 connect(m_ui->tagsEditButton, &QAbstractButton::clicked, this, [this]
363 auto *dlg = new TorrentTagsDialog(m_currentContext->torrentParams.tags, this);
364 dlg->setAttribute(Qt::WA_DeleteOnClose);
365 connect(dlg, &TorrentTagsDialog::accepted, this, [this, dlg]
367 m_currentContext->torrentParams.tags = dlg->tags();
368 m_ui->tagsLineEdit->setText(Utils::String::joinIntoString(m_currentContext->torrentParams.tags, u", "_s));
370 dlg->open();
372 connect(m_filterLine, &LineEdit::textChanged, this, &AddNewTorrentDialog::setContentFilterPattern);
373 connect(m_ui->buttonSelectAll, &QPushButton::clicked, m_ui->contentTreeView, &TorrentContentWidget::checkAll);
374 connect(m_ui->buttonSelectNone, &QPushButton::clicked, m_ui->contentTreeView, &TorrentContentWidget::checkNone);
375 connect(Preferences::instance(), &Preferences::changed, []
377 const int length = Preferences::instance()->addNewTorrentDialogSavePathHistoryLength();
378 settings()->storeValue(KEY_SAVEPATHHISTORY
379 , QStringList(settings()->loadValue<QStringList>(KEY_SAVEPATHHISTORY).mid(0, length)));
382 setCurrentContext(std::make_shared<Context>(Context {torrentDescr, inParams}));
385 AddNewTorrentDialog::~AddNewTorrentDialog()
387 saveState();
388 delete m_ui;
391 bool AddNewTorrentDialog::isDoNotDeleteTorrentChecked() const
393 return m_ui->doNotDeleteTorrentCheckBox->isChecked();
396 void AddNewTorrentDialog::loadState()
398 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
399 resize(dialogSize);
401 m_ui->splitter->restoreState(m_storeSplitterState);;
404 void AddNewTorrentDialog::saveState()
406 Q_ASSERT(m_currentContext);
407 if (!m_currentContext) [[unlikely]]
408 return;
410 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
411 const bool hasMetadata = torrentDescr.info().has_value();
413 m_storeDialogSize = size();
414 m_storeSplitterState = m_ui->splitter->saveState();
415 if (hasMetadata)
416 m_storeTreeHeaderState = m_ui->contentTreeView->header()->saveState();
419 void AddNewTorrentDialog::showEvent(QShowEvent *event)
421 QDialog::showEvent(event);
422 if (!Preferences::instance()->isAddNewTorrentDialogTopLevel())
423 return;
425 activateWindow();
426 raise();
429 void AddNewTorrentDialog::setCurrentContext(const std::shared_ptr<Context> context)
431 Q_ASSERT(context);
432 if (!context) [[unlikely]]
433 return;
435 m_currentContext = context;
437 const QSignalBlocker comboTMMSignalBlocker {m_ui->comboTMM};
438 const QSignalBlocker startTorrentCheckBoxSignalBlocker {m_ui->startTorrentCheckBox};
439 const QSignalBlocker contentLayoutComboBoxSignalBlocker {m_ui->contentLayoutComboBox};
440 const QSignalBlocker categoryComboBoxSignalBlocker {m_ui->categoryComboBox};
442 const BitTorrent::AddTorrentParams &addTorrentParams = m_currentContext->torrentParams;
443 const auto *session = BitTorrent::Session::instance();
445 // TODO: set dialog file properties using m_torrentParams.filePriorities
447 m_ui->comboTMM->setCurrentIndex(addTorrentParams.useAutoTMM.value_or(!session->isAutoTMMDisabledByDefault()) ? 1 : 0);
448 m_ui->addToQueueTopCheckBox->setChecked(addTorrentParams.addToQueueTop.value_or(session->isAddTorrentToQueueTop()));
449 m_ui->startTorrentCheckBox->setChecked(!addTorrentParams.addStopped.value_or(session->isAddTorrentStopped()));
450 m_ui->stopConditionLabel->setEnabled(m_ui->startTorrentCheckBox->isChecked());
451 m_ui->stopConditionComboBox->setEnabled(m_ui->startTorrentCheckBox->isChecked());
452 m_ui->contentLayoutComboBox->setCurrentIndex(
453 static_cast<int>(addTorrentParams.contentLayout.value_or(session->torrentContentLayout())));
454 m_ui->sequentialCheckBox->setChecked(addTorrentParams.sequential);
455 m_ui->firstLastCheckBox->setChecked(addTorrentParams.firstLastPiecePriority);
456 m_ui->skipCheckingCheckBox->setChecked(addTorrentParams.skipChecking);
457 m_ui->tagsLineEdit->setText(Utils::String::joinIntoString(addTorrentParams.tags, u", "_s));
459 // Load categories
460 QStringList categories = session->categories();
461 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
462 const QString defaultCategory = m_storeDefaultCategory;
464 if (!addTorrentParams.category.isEmpty())
465 m_ui->categoryComboBox->addItem(addTorrentParams.category);
466 if (!defaultCategory.isEmpty())
467 m_ui->categoryComboBox->addItem(defaultCategory);
468 m_ui->categoryComboBox->addItem(u""_s);
470 for (const QString &category : asConst(categories))
472 if ((category != defaultCategory) && (category != addTorrentParams.category))
473 m_ui->categoryComboBox->addItem(category);
476 m_filterLine->blockSignals(true);
477 m_filterLine->clear();
479 // Default focus
480 if (m_ui->comboTMM->currentIndex() == 0) // 0 is Manual mode
481 m_ui->savePath->setFocus();
482 else
483 m_ui->categoryComboBox->setFocus();
485 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
486 const BitTorrent::InfoHash infoHash = torrentDescr.infoHash();
487 const bool hasMetadata = torrentDescr.info().has_value();
489 if (hasMetadata)
490 m_ui->stopConditionComboBox->removeItem(m_ui->stopConditionComboBox->findData(QVariant::fromValue(BitTorrent::Torrent::StopCondition::MetadataReceived)));
491 else
492 m_ui->stopConditionComboBox->insertItem(1, tr("Metadata received"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::MetadataReceived));
493 const auto stopCondition = addTorrentParams.stopCondition.value_or(session->torrentStopCondition());
494 if (hasMetadata && (stopCondition == BitTorrent::Torrent::StopCondition::MetadataReceived))
496 m_ui->startTorrentCheckBox->setChecked(false);
497 m_ui->stopConditionComboBox->setCurrentIndex(m_ui->stopConditionComboBox->findData(QVariant::fromValue(BitTorrent::Torrent::StopCondition::None)));
499 else
501 m_ui->startTorrentCheckBox->setChecked(!addTorrentParams.addStopped.value_or(session->isAddTorrentStopped()));
502 m_ui->stopConditionComboBox->setCurrentIndex(m_ui->stopConditionComboBox->findData(QVariant::fromValue(stopCondition)));
505 m_ui->labelInfohash1Data->setText(infoHash.v1().isValid() ? infoHash.v1().toString() : tr("N/A"));
506 m_ui->labelInfohash2Data->setText(infoHash.v2().isValid() ? infoHash.v2().toString() : tr("N/A"));
508 if (hasMetadata)
510 m_ui->lblMetaLoading->setVisible(false);
511 m_ui->progMetaLoading->setVisible(false);
512 m_ui->buttonSave->setVisible(false);
513 setupTreeview();
515 else
517 // Set dialog title
518 const QString torrentName = torrentDescr.name();
519 setWindowTitle(torrentName.isEmpty() ? tr("Magnet link") : torrentName);
520 m_ui->labelCommentData->setText(tr("Not Available", "This comment is unavailable"));
521 m_ui->labelDateData->setText(tr("Not Available", "This date is unavailable"));
522 updateDiskSpaceLabel();
523 setMetadataProgressIndicator(true, tr("Retrieving metadata..."));
526 TMMChanged(m_ui->comboTMM->currentIndex());
529 void AddNewTorrentDialog::updateCurrentContext()
531 Q_ASSERT(m_currentContext);
532 if (!m_currentContext) [[unlikely]]
533 return;
535 BitTorrent::AddTorrentParams &addTorrentParams = m_currentContext->torrentParams;
537 addTorrentParams.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
539 // Category
540 addTorrentParams.category = m_ui->categoryComboBox->currentText();
541 if (m_ui->defaultCategoryCheckbox->isChecked())
542 m_storeDefaultCategory = addTorrentParams.category;
544 m_storeRememberLastSavePath = m_ui->checkBoxRememberLastSavePath->isChecked();
546 addTorrentParams.addToQueueTop = m_ui->addToQueueTopCheckBox->isChecked();
547 addTorrentParams.addStopped = !m_ui->startTorrentCheckBox->isChecked();
548 addTorrentParams.stopCondition = m_ui->stopConditionComboBox->currentData().value<BitTorrent::Torrent::StopCondition>();
549 addTorrentParams.contentLayout = static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex());
551 addTorrentParams.sequential = m_ui->sequentialCheckBox->isChecked();
552 addTorrentParams.firstLastPiecePriority = m_ui->firstLastCheckBox->isChecked();
554 const bool useAutoTMM = (m_ui->comboTMM->currentIndex() == 1); // 1 is Automatic mode. Handle all non 1 values as manual mode.
555 addTorrentParams.useAutoTMM = useAutoTMM;
556 if (!useAutoTMM)
558 const int savePathHistoryLength = Preferences::instance()->addNewTorrentDialogSavePathHistoryLength();
559 const Path savePath = m_ui->savePath->selectedPath();
560 addTorrentParams.savePath = savePath;
561 updatePathHistory(KEY_SAVEPATHHISTORY, savePath, savePathHistoryLength);
563 addTorrentParams.useDownloadPath = m_ui->groupBoxDownloadPath->isChecked();
564 if (addTorrentParams.useDownloadPath)
566 const Path downloadPath = m_ui->downloadPath->selectedPath();
567 addTorrentParams.downloadPath = downloadPath;
568 updatePathHistory(KEY_DOWNLOADPATHHISTORY, downloadPath, savePathHistoryLength);
570 else
572 addTorrentParams.downloadPath = Path();
575 else
577 addTorrentParams.savePath = Path();
578 addTorrentParams.downloadPath = Path();
579 addTorrentParams.useDownloadPath = std::nullopt;
583 void AddNewTorrentDialog::updateDiskSpaceLabel()
585 Q_ASSERT(m_currentContext);
586 if (!m_currentContext) [[unlikely]]
587 return;
589 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
590 const bool hasMetadata = torrentDescr.info().has_value();
592 // Determine torrent size
593 qlonglong torrentSize = 0;
594 if (hasMetadata)
596 const auto torrentInfo = *torrentDescr.info();
597 const QList<BitTorrent::DownloadPriority> &priorities = m_contentAdaptor->filePriorities();
598 Q_ASSERT(priorities.size() == torrentInfo.filesCount());
599 for (int i = 0; i < priorities.size(); ++i)
601 if (priorities[i] > BitTorrent::DownloadPriority::Ignored)
602 torrentSize += torrentInfo.fileSize(i);
606 const QString freeSpace = Utils::Misc::friendlyUnit(queryFreeDiskSpace(m_ui->savePath->selectedPath()));
607 const QString sizeString = tr("%1 (Free space on disk: %2)").arg(
608 ((torrentSize > 0) ? Utils::Misc::friendlyUnit(torrentSize) : tr("Not available", "This size is unavailable."))
609 , freeSpace);
610 m_ui->labelSizeData->setText(sizeString);
613 void AddNewTorrentDialog::onSavePathChanged([[maybe_unused]] const Path &newPath)
615 // Remember index
616 m_savePathIndex = m_ui->savePath->currentIndex();
617 updateDiskSpaceLabel();
620 void AddNewTorrentDialog::onDownloadPathChanged([[maybe_unused]] const Path &newPath)
622 // Remember index
623 const int currentPathIndex = m_ui->downloadPath->currentIndex();
624 if (currentPathIndex >= 0)
625 m_downloadPathIndex = m_ui->downloadPath->currentIndex();
628 void AddNewTorrentDialog::onUseDownloadPathChanged(const bool checked)
630 m_useDownloadPath = checked;
631 m_ui->downloadPath->setCurrentIndex(checked ? m_downloadPathIndex : -1);
634 void AddNewTorrentDialog::categoryChanged([[maybe_unused]] const int index)
636 if (m_ui->comboTMM->currentIndex() == 1)
638 const auto *btSession = BitTorrent::Session::instance();
639 const QString categoryName = m_ui->categoryComboBox->currentText();
641 const Path savePath = btSession->categorySavePath(categoryName);
642 m_ui->savePath->setSelectedPath(savePath);
644 const Path downloadPath = btSession->categoryDownloadPath(categoryName);
645 m_ui->downloadPath->setSelectedPath(downloadPath);
647 m_ui->groupBoxDownloadPath->setChecked(!m_ui->downloadPath->selectedPath().isEmpty());
649 updateDiskSpaceLabel();
653 void AddNewTorrentDialog::contentLayoutChanged()
655 Q_ASSERT(m_currentContext);
656 if (!m_currentContext) [[unlikely]]
657 return;
659 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
660 const bool hasMetadata = torrentDescr.info().has_value();
662 if (!hasMetadata)
663 return;
665 const auto contentLayout = static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex());
666 m_contentAdaptor->applyContentLayout(contentLayout);
667 m_ui->contentTreeView->setContentHandler(m_contentAdaptor.get()); // to cause reloading
670 void AddNewTorrentDialog::saveTorrentFile()
672 Q_ASSERT(m_currentContext);
673 if (!m_currentContext) [[unlikely]]
674 return;
676 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
677 const bool hasMetadata = torrentDescr.info().has_value();
679 Q_ASSERT(hasMetadata);
680 if (!hasMetadata) [[unlikely]]
681 return;
683 const auto torrentInfo = *torrentDescr.info();
685 const QString filter {tr("Torrent file (*%1)").arg(TORRENT_FILE_EXTENSION)};
687 Path path {QFileDialog::getSaveFileName(this, tr("Save as torrent file")
688 , QDir::home().absoluteFilePath(torrentInfo.name() + TORRENT_FILE_EXTENSION)
689 , filter)};
690 if (path.isEmpty())
691 return;
693 if (!path.hasExtension(TORRENT_FILE_EXTENSION))
694 path += TORRENT_FILE_EXTENSION;
696 if (const auto result = torrentDescr.saveToFile(path); !result)
698 QMessageBox::critical(this, tr("I/O Error")
699 , tr("Couldn't export torrent metadata file '%1'. Reason: %2.").arg(path.toString(), result.error()));
703 void AddNewTorrentDialog::showContentFilterContextMenu()
705 QMenu *menu = m_filterLine->createStandardContextMenu();
707 auto *formatMenu = new FilterPatternFormatMenu(m_storeFilterPatternFormat.get(FilterPatternFormat::Wildcards), menu);
708 connect(formatMenu, &FilterPatternFormatMenu::patternFormatChanged, this, [this](const FilterPatternFormat format)
710 m_storeFilterPatternFormat = format;
711 setContentFilterPattern();
714 menu->addSeparator();
715 menu->addMenu(formatMenu);
716 menu->setAttribute(Qt::WA_DeleteOnClose);
717 menu->popup(QCursor::pos());
720 void AddNewTorrentDialog::setContentFilterPattern()
722 m_ui->contentTreeView->setFilterPattern(m_filterLine->text(), m_storeFilterPatternFormat.get(FilterPatternFormat::Wildcards));
725 void AddNewTorrentDialog::populateSavePaths()
727 Q_ASSERT(m_currentContext);
728 if (!m_currentContext) [[unlikely]]
729 return;
731 const BitTorrent::AddTorrentParams &addTorrentParams = m_currentContext->torrentParams;
732 const auto *btSession = BitTorrent::Session::instance();
734 m_ui->savePath->blockSignals(true);
735 m_ui->savePath->clear();
736 const auto savePathHistory = settings()->loadValue<QStringList>(KEY_SAVEPATHHISTORY);
737 if (savePathHistory.size() > 0)
739 for (const QString &path : savePathHistory)
740 m_ui->savePath->addItem(Path(path));
742 else
744 m_ui->savePath->addItem(btSession->savePath());
747 if (m_savePathIndex >= 0)
749 m_ui->savePath->setCurrentIndex(std::min(m_savePathIndex, (m_ui->savePath->count() - 1)));
751 else
753 if (!addTorrentParams.savePath.isEmpty())
754 setPath(m_ui->savePath, addTorrentParams.savePath);
755 else if (!m_storeRememberLastSavePath)
756 setPath(m_ui->savePath, btSession->savePath());
757 else
758 m_ui->savePath->setCurrentIndex(0);
760 m_savePathIndex = m_ui->savePath->currentIndex();
763 m_ui->savePath->blockSignals(false);
765 m_ui->downloadPath->blockSignals(true);
766 m_ui->downloadPath->clear();
767 const auto downloadPathHistory = settings()->loadValue<QStringList>(KEY_DOWNLOADPATHHISTORY);
768 if (downloadPathHistory.size() > 0)
770 for (const QString &path : downloadPathHistory)
771 m_ui->downloadPath->addItem(Path(path));
773 else
775 m_ui->downloadPath->addItem(btSession->downloadPath());
778 if (m_downloadPathIndex >= 0)
780 m_ui->downloadPath->setCurrentIndex(m_useDownloadPath ? std::min(m_downloadPathIndex, (m_ui->downloadPath->count() - 1)) : -1);
781 m_ui->groupBoxDownloadPath->setChecked(m_useDownloadPath);
783 else
785 const bool useDownloadPath = addTorrentParams.useDownloadPath.value_or(btSession->isDownloadPathEnabled());
786 m_ui->groupBoxDownloadPath->setChecked(useDownloadPath);
788 if (!addTorrentParams.downloadPath.isEmpty())
789 setPath(m_ui->downloadPath, addTorrentParams.downloadPath);
790 else if (!m_storeRememberLastSavePath)
791 setPath(m_ui->downloadPath, btSession->downloadPath());
792 else
793 m_ui->downloadPath->setCurrentIndex(0);
795 m_downloadPathIndex = m_ui->downloadPath->currentIndex();
796 if (!useDownloadPath)
797 m_ui->downloadPath->setCurrentIndex(-1);
800 m_ui->downloadPath->blockSignals(false);
801 m_ui->groupBoxDownloadPath->blockSignals(false);
804 void AddNewTorrentDialog::accept()
806 Q_ASSERT(m_currentContext);
807 if (!m_currentContext) [[unlikely]]
808 return;
810 updateCurrentContext();
811 emit torrentAccepted(m_currentContext->torrentDescr, m_currentContext->torrentParams);
813 Preferences::instance()->setAddNewTorrentDialogEnabled(!m_ui->checkBoxNeverShow->isChecked());
815 QDialog::accept();
818 void AddNewTorrentDialog::reject()
820 Q_ASSERT(m_currentContext);
821 if (!m_currentContext) [[unlikely]]
822 return;
824 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
825 const bool hasMetadata = torrentDescr.info().has_value();
826 if (!hasMetadata)
828 setMetadataProgressIndicator(false);
829 BitTorrent::Session::instance()->cancelDownloadMetadata(torrentDescr.infoHash().toTorrentID());
832 QDialog::reject();
835 void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &metadata)
837 Q_ASSERT(m_currentContext);
838 if (!m_currentContext) [[unlikely]]
839 return;
841 Q_ASSERT(metadata.isValid());
842 if (!metadata.isValid()) [[unlikely]]
843 return;
845 BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
846 Q_ASSERT(metadata.matchesInfoHash(torrentDescr.infoHash()));
847 if (!metadata.matchesInfoHash(torrentDescr.infoHash())) [[unlikely]]
848 return;
850 torrentDescr.setTorrentInfo(metadata);
851 setMetadataProgressIndicator(true, tr("Parsing metadata..."));
853 // Update UI
854 setupTreeview();
855 setMetadataProgressIndicator(false, tr("Metadata retrieval complete"));
857 if (const auto stopCondition = m_ui->stopConditionComboBox->currentData().value<BitTorrent::Torrent::StopCondition>()
858 ; stopCondition == BitTorrent::Torrent::StopCondition::MetadataReceived)
860 m_ui->startTorrentCheckBox->setChecked(false);
862 const auto index = m_ui->stopConditionComboBox->currentIndex();
863 m_ui->stopConditionComboBox->setCurrentIndex(m_ui->stopConditionComboBox->findData(
864 QVariant::fromValue(BitTorrent::Torrent::StopCondition::None)));
865 m_ui->stopConditionComboBox->removeItem(index);
868 m_ui->buttonSave->setVisible(true);
869 if (torrentDescr.infoHash().v2().isValid())
871 m_ui->buttonSave->setEnabled(false);
872 m_ui->buttonSave->setToolTip(tr("Cannot create v2 torrent until its data is fully downloaded."));
876 void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText)
878 // Always show info label when waiting for metadata
879 m_ui->lblMetaLoading->setVisible(true);
880 m_ui->lblMetaLoading->setText(labelText);
881 m_ui->progMetaLoading->setVisible(visibleIndicator);
884 void AddNewTorrentDialog::setupTreeview()
886 Q_ASSERT(m_currentContext);
887 if (!m_currentContext) [[unlikely]]
888 return;
890 const BitTorrent::TorrentDescriptor &torrentDescr = m_currentContext->torrentDescr;
891 const bool hasMetadata = torrentDescr.info().has_value();
893 Q_ASSERT(hasMetadata);
894 if (!hasMetadata) [[unlikely]]
895 return;
897 // Set dialog title
898 setWindowTitle(torrentDescr.name());
900 // Set torrent information
901 m_ui->labelCommentData->setText(Utils::Misc::parseHtmlLinks(torrentDescr.comment().toHtmlEscaped()));
902 m_ui->labelDateData->setText(!torrentDescr.creationDate().isNull() ? QLocale().toString(torrentDescr.creationDate(), QLocale::ShortFormat) : tr("Not available"));
904 const auto &torrentInfo = *torrentDescr.info();
906 BitTorrent::AddTorrentParams &addTorrentParams = m_currentContext->torrentParams;
907 if (addTorrentParams.filePaths.isEmpty())
908 addTorrentParams.filePaths = torrentInfo.filePaths();
910 m_contentAdaptor = std::make_unique<TorrentContentAdaptor>(torrentInfo, addTorrentParams.filePaths
911 , addTorrentParams.filePriorities, [this] { updateDiskSpaceLabel(); });
913 const auto contentLayout = static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex());
914 m_contentAdaptor->applyContentLayout(contentLayout);
916 if (BitTorrent::Session::instance()->isExcludedFileNamesEnabled())
918 // Check file name blacklist for torrents that are manually added
919 QList<BitTorrent::DownloadPriority> priorities = m_contentAdaptor->filePriorities();
920 BitTorrent::Session::instance()->applyFilenameFilter(m_contentAdaptor->filePaths(), priorities);
921 m_contentAdaptor->prioritizeFiles(priorities);
924 m_ui->contentTreeView->setContentHandler(m_contentAdaptor.get());
926 m_filterLine->blockSignals(false);
928 updateDiskSpaceLabel();
931 void AddNewTorrentDialog::TMMChanged(int index)
933 if (index != 1)
934 { // 0 is Manual mode and 1 is Automatic mode. Handle all non 1 values as manual mode.
935 populateSavePaths();
936 m_ui->groupBoxSavePath->setEnabled(true);
938 else
940 const auto *session = BitTorrent::Session::instance();
942 m_ui->groupBoxSavePath->setEnabled(false);
944 m_ui->savePath->blockSignals(true);
945 m_ui->savePath->clear();
946 const Path savePath = session->categorySavePath(m_ui->categoryComboBox->currentText());
947 m_ui->savePath->addItem(savePath);
949 m_ui->downloadPath->blockSignals(true);
950 m_ui->downloadPath->clear();
951 const Path downloadPath = session->categoryDownloadPath(m_ui->categoryComboBox->currentText());
952 m_ui->downloadPath->addItem(downloadPath);
954 m_ui->groupBoxDownloadPath->blockSignals(true);
955 m_ui->groupBoxDownloadPath->setChecked(!downloadPath.isEmpty());
958 updateDiskSpaceLabel();