Add option to auto hide zero status filters
[qBittorrent.git] / src / gui / torrentcreatordialog.cpp
blobc91953be581400f0224f45ecaed62b11b08682ac
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2017 Mike Tzou (Chocobo1)
4 * Copyright (C) 2010 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 "torrentcreatordialog.h"
32 #include <QCloseEvent>
33 #include <QFileDialog>
34 #include <QMessageBox>
35 #include <QMimeData>
36 #include <QUrl>
38 #include "base/bittorrent/session.h"
39 #include "base/bittorrent/torrentinfo.h"
40 #include "base/global.h"
41 #include "base/utils/fs.h"
42 #include "ui_torrentcreatordialog.h"
43 #include "utils.h"
45 #define SETTINGS_KEY(name) u"TorrentCreator/" name
47 TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const Path &defaultPath)
48 : QDialog(parent)
49 , m_ui(new Ui::TorrentCreatorDialog)
50 , m_creatorThread(new BitTorrent::TorrentCreatorThread(this))
51 , m_storeDialogSize(SETTINGS_KEY(u"Size"_qs))
52 , m_storePieceSize(SETTINGS_KEY(u"PieceSize"_qs))
53 , m_storePrivateTorrent(SETTINGS_KEY(u"PrivateTorrent"_qs))
54 , m_storeStartSeeding(SETTINGS_KEY(u"StartSeeding"_qs))
55 , m_storeIgnoreRatio(SETTINGS_KEY(u"IgnoreRatio"_qs))
56 #ifdef QBT_USES_LIBTORRENT2
57 , m_storeTorrentFormat(SETTINGS_KEY(u"TorrentFormat"_qs))
58 #else
59 , m_storeOptimizeAlignment(SETTINGS_KEY(u"OptimizeAlignment"_qs))
60 , m_paddedFileSizeLimit(SETTINGS_KEY(u"PaddedFileSizeLimit"_qs))
61 #endif
62 , m_storeLastAddPath(SETTINGS_KEY(u"LastAddPath"_qs))
63 , m_storeTrackerList(SETTINGS_KEY(u"TrackerList"_qs))
64 , m_storeWebSeedList(SETTINGS_KEY(u"WebSeedList"_qs))
65 , m_storeComments(SETTINGS_KEY(u"Comments"_qs))
66 , m_storeLastSavePath(SETTINGS_KEY(u"LastSavePath"_qs))
67 , m_storeSource(SETTINGS_KEY(u"Source"_qs))
69 m_ui->setupUi(this);
71 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent"));
73 connect(m_ui->addFileButton, &QPushButton::clicked, this, &TorrentCreatorDialog::onAddFileButtonClicked);
74 connect(m_ui->addFolderButton, &QPushButton::clicked, this, &TorrentCreatorDialog::onAddFolderButtonClicked);
75 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &TorrentCreatorDialog::onCreateButtonClicked);
76 connect(m_ui->buttonCalcTotalPieces, &QPushButton::clicked, this, &TorrentCreatorDialog::updatePiecesCount);
78 connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::creationSuccess, this, &TorrentCreatorDialog::handleCreationSuccess);
79 connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::creationFailure, this, &TorrentCreatorDialog::handleCreationFailure);
80 connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::updateProgress, this, &TorrentCreatorDialog::updateProgressBar);
82 loadSettings();
83 updateInputPath(defaultPath);
85 #ifdef QBT_USES_LIBTORRENT2
86 m_ui->checkOptimizeAlignment->hide();
87 #else
88 m_ui->widgetTorrentFormat->hide();
89 #endif
92 TorrentCreatorDialog::~TorrentCreatorDialog()
94 saveSettings();
96 delete m_ui;
99 void TorrentCreatorDialog::updateInputPath(const Path &path)
101 if (path.isEmpty()) return;
102 m_ui->textInputPath->setText(path.toString());
103 updateProgressBar(0);
106 void TorrentCreatorDialog::onAddFolderButtonClicked()
108 const QString oldPath = m_ui->textInputPath->text();
109 const Path path {QFileDialog::getExistingDirectory(this, tr("Select folder"), oldPath)};
110 updateInputPath(path);
113 void TorrentCreatorDialog::onAddFileButtonClicked()
115 const QString oldPath = m_ui->textInputPath->text();
116 const Path path {QFileDialog::getOpenFileName(this, tr("Select file"), oldPath)};
117 updateInputPath(path);
120 int TorrentCreatorDialog::getPieceSize() const
122 const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB
123 return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024;
126 #ifdef QBT_USES_LIBTORRENT2
127 BitTorrent::TorrentFormat TorrentCreatorDialog::getTorrentFormat() const
129 switch (m_ui->comboTorrentFormat->currentIndex())
131 case 0:
132 return BitTorrent::TorrentFormat::V2;
133 case 1:
134 return BitTorrent::TorrentFormat::Hybrid;
135 case 2:
136 return BitTorrent::TorrentFormat::V1;
138 return BitTorrent::TorrentFormat::Hybrid;
140 #else
141 int TorrentCreatorDialog::getPaddedFileSizeLimit() const
143 const int value = m_ui->spinPaddedFileSizeLimit->value();
144 return ((value >= 0) ? (value * 1024) : -1);
146 #endif
148 void TorrentCreatorDialog::dropEvent(QDropEvent *event)
150 event->acceptProposedAction();
152 if (event->mimeData()->hasUrls())
154 // only take the first one
155 const QUrl firstItem = event->mimeData()->urls().first();
156 const Path path {
157 (firstItem.scheme().compare(u"file", Qt::CaseInsensitive) == 0)
158 ? firstItem.toLocalFile() : firstItem.toString()
160 updateInputPath(path);
164 void TorrentCreatorDialog::dragEnterEvent(QDragEnterEvent *event)
166 if (event->mimeData()->hasFormat(u"text/plain"_qs) || event->mimeData()->hasFormat(u"text/uri-list"_qs))
167 event->acceptProposedAction();
170 // Main function that create a .torrent file
171 void TorrentCreatorDialog::onCreateButtonClicked()
173 const auto inputPath = Utils::Fs::toCanonicalPath(Path(m_ui->textInputPath->text().trimmed()));
175 // test if readable
176 if (!Utils::Fs::isReadable(inputPath))
178 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Path to file/folder is not readable."));
179 return;
182 // get save path
183 const Path lastSavePath = (m_storeLastSavePath.get(Utils::Fs::homePath()) / Path(inputPath.filename() + u".torrent"));
184 Path destPath {QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), lastSavePath.data(), tr("Torrent Files (*.torrent)"))};
185 if (destPath.isEmpty())
186 return;
187 if (!destPath.hasExtension(TORRENT_FILE_EXTENSION))
188 destPath += TORRENT_FILE_EXTENSION;
189 m_storeLastSavePath = destPath.parentPath();
191 // Disable dialog & set busy cursor
192 setInteractionEnabled(false);
193 setCursor(QCursor(Qt::WaitCursor));
195 const QStringList trackers = m_ui->trackersList->toPlainText().trimmed()
196 .replace(QRegularExpression(u"\n\n[\n]+"_qs), u"\n\n"_qs).split(u'\n');
197 const BitTorrent::TorrentCreatorParams params
199 m_ui->checkPrivate->isChecked()
200 #ifdef QBT_USES_LIBTORRENT2
201 , getTorrentFormat()
202 #else
203 , m_ui->checkOptimizeAlignment->isChecked()
204 , getPaddedFileSizeLimit()
205 #endif
206 , getPieceSize()
207 , inputPath
208 , destPath
209 , m_ui->txtComment->toPlainText()
210 , m_ui->lineEditSource->text()
211 , trackers
212 , m_ui->URLSeedsList->toPlainText().split(u'\n', Qt::SkipEmptyParts)
215 // run the creator thread
216 m_creatorThread->create(params);
219 void TorrentCreatorDialog::handleCreationFailure(const QString &msg)
221 // Remove busy cursor
222 setCursor(QCursor(Qt::ArrowCursor));
223 QMessageBox::information(this, tr("Torrent creation failed"), tr("Reason: %1").arg(msg));
224 setInteractionEnabled(true);
227 void TorrentCreatorDialog::handleCreationSuccess(const Path &path, const Path &branchPath)
229 // Remove busy cursor
230 setCursor(QCursor(Qt::ArrowCursor));
231 if (m_ui->checkStartSeeding->isChecked())
233 // Create save path temp data
234 const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::loadFromFile(path);
235 if (!result)
237 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
238 return;
241 BitTorrent::AddTorrentParams params;
242 params.savePath = branchPath;
243 params.skipChecking = true;
244 if (m_ui->checkIgnoreShareLimits->isChecked())
246 params.ratioLimit = BitTorrent::Torrent::NO_RATIO_LIMIT;
247 params.seedingTimeLimit = BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT;
249 params.useAutoTMM = false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path
251 BitTorrent::Session::instance()->addTorrent(result.value(), params);
253 QMessageBox::information(this, tr("Torrent creator")
254 , u"%1\n%2"_qs.arg(tr("Torrent created:"), path.toString()));
255 setInteractionEnabled(true);
258 void TorrentCreatorDialog::updateProgressBar(int progress)
260 m_ui->progressBar->setValue(progress);
263 void TorrentCreatorDialog::updatePiecesCount()
265 const Path path {m_ui->textInputPath->text().trimmed()};
266 #ifdef QBT_USES_LIBTORRENT2
267 const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(
268 path, getPieceSize(), getTorrentFormat());
269 #else
270 const bool isAlignmentOptimized = m_ui->checkOptimizeAlignment->isChecked();
271 const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path
272 , getPieceSize(), isAlignmentOptimized, getPaddedFileSizeLimit());
273 #endif
274 m_ui->labelTotalPieces->setText(QString::number(count));
277 void TorrentCreatorDialog::setInteractionEnabled(const bool enabled) const
279 m_ui->textInputPath->setEnabled(enabled);
280 m_ui->addFileButton->setEnabled(enabled);
281 m_ui->addFolderButton->setEnabled(enabled);
282 m_ui->trackersList->setEnabled(enabled);
283 m_ui->URLSeedsList->setEnabled(enabled);
284 m_ui->txtComment->setEnabled(enabled);
285 m_ui->comboPieceSize->setEnabled(enabled);
286 m_ui->buttonCalcTotalPieces->setEnabled(enabled);
287 m_ui->checkPrivate->setEnabled(enabled);
288 m_ui->checkStartSeeding->setEnabled(enabled);
289 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
290 m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked());
291 #ifdef QBT_USES_LIBTORRENT2
292 m_ui->widgetTorrentFormat->setEnabled(enabled);
293 #else
294 m_ui->checkOptimizeAlignment->setEnabled(enabled);
295 m_ui->spinPaddedFileSizeLimit->setEnabled(enabled);
296 #endif
299 void TorrentCreatorDialog::saveSettings()
301 m_storeLastAddPath = Path(m_ui->textInputPath->text().trimmed());
303 m_storePieceSize = m_ui->comboPieceSize->currentIndex();
304 m_storePrivateTorrent = m_ui->checkPrivate->isChecked();
305 m_storeStartSeeding = m_ui->checkStartSeeding->isChecked();
306 m_storeIgnoreRatio = m_ui->checkIgnoreShareLimits->isChecked();
307 #ifdef QBT_USES_LIBTORRENT2
308 m_storeTorrentFormat = m_ui->comboTorrentFormat->currentIndex();
309 #else
310 m_storeOptimizeAlignment = m_ui->checkOptimizeAlignment->isChecked();
311 m_paddedFileSizeLimit = m_ui->spinPaddedFileSizeLimit->value();
312 #endif
314 m_storeTrackerList = m_ui->trackersList->toPlainText();
315 m_storeWebSeedList = m_ui->URLSeedsList->toPlainText();
316 m_storeComments = m_ui->txtComment->toPlainText();
317 m_storeSource = m_ui->lineEditSource->text();
319 m_storeDialogSize = size();
322 void TorrentCreatorDialog::loadSettings()
324 m_ui->textInputPath->setText(m_storeLastAddPath.get(Utils::Fs::homePath()).toString());
326 m_ui->comboPieceSize->setCurrentIndex(m_storePieceSize);
327 m_ui->checkPrivate->setChecked(m_storePrivateTorrent);
328 m_ui->checkStartSeeding->setChecked(m_storeStartSeeding);
329 m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio);
330 m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked());
331 #ifdef QBT_USES_LIBTORRENT2
332 m_ui->comboTorrentFormat->setCurrentIndex(m_storeTorrentFormat.get(1));
333 #else
334 m_ui->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment.get(true));
335 m_ui->spinPaddedFileSizeLimit->setValue(m_paddedFileSizeLimit.get(-1));
336 #endif
338 m_ui->trackersList->setPlainText(m_storeTrackerList);
339 m_ui->URLSeedsList->setPlainText(m_storeWebSeedList);
340 m_ui->txtComment->setPlainText(m_storeComments);
341 m_ui->lineEditSource->setText(m_storeSource);
343 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
344 resize(dialogSize);