Remove the max half-open connections option from GUI
[qBittorrent.git] / src / gui / torrentcreatordialog.cpp
blob00f59c783aae93da0cb34b674ae6550e72aa13cf
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 <QDebug>
34 #include <QFileDialog>
35 #include <QMessageBox>
36 #include <QMimeData>
37 #include <QUrl>
39 #include "base/bittorrent/session.h"
40 #include "base/bittorrent/torrentcreatorthread.h"
41 #include "base/bittorrent/torrentinfo.h"
42 #include "base/global.h"
43 #include "base/utils/fs.h"
44 #include "ui_torrentcreatordialog.h"
45 #include "utils.h"
47 #define SETTINGS_KEY(name) "TorrentCreator/" name
49 TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defaultPath)
50 : QDialog(parent)
51 , m_ui(new Ui::TorrentCreatorDialog)
52 , m_creatorThread(new BitTorrent::TorrentCreatorThread(this))
53 , m_storeDialogSize(SETTINGS_KEY("Dimension"))
54 , m_storePieceSize(SETTINGS_KEY("PieceSize"))
55 , m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent"))
56 , m_storeStartSeeding(SETTINGS_KEY("StartSeeding"))
57 , m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio"))
58 , m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"), true)
59 , m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath())
60 , m_storeTrackerList(SETTINGS_KEY("TrackerList"))
61 , m_storeWebSeedList(SETTINGS_KEY("WebSeedList"))
62 , m_storeComments(SETTINGS_KEY("Comments"))
63 , m_storeLastSavePath(SETTINGS_KEY("LastSavePath"), QDir::homePath())
64 , m_storeSource(SETTINGS_KEY("Source"))
66 m_ui->setupUi(this);
67 setAttribute(Qt::WA_DeleteOnClose);
68 setModal(false);
70 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent"));
72 connect(m_ui->addFileButton, &QPushButton::clicked, this, &TorrentCreatorDialog::onAddFileButtonClicked);
73 connect(m_ui->addFolderButton, &QPushButton::clicked, this, &TorrentCreatorDialog::onAddFolderButtonClicked);
74 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &TorrentCreatorDialog::onCreateButtonClicked);
75 connect(m_ui->buttonCalcTotalPieces, &QPushButton::clicked, this, &TorrentCreatorDialog::updatePiecesCount);
77 connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::creationSuccess, this, &TorrentCreatorDialog::handleCreationSuccess);
78 connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::creationFailure, this, &TorrentCreatorDialog::handleCreationFailure);
79 connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::updateProgress, this, &TorrentCreatorDialog::updateProgressBar);
81 loadSettings();
82 updateInputPath(defaultPath);
84 show();
87 TorrentCreatorDialog::~TorrentCreatorDialog()
89 saveSettings();
91 delete m_ui;
94 void TorrentCreatorDialog::updateInputPath(const QString &path)
96 if (path.isEmpty()) return;
97 m_ui->textInputPath->setText(Utils::Fs::toNativePath(path));
98 updateProgressBar(0);
101 void TorrentCreatorDialog::onAddFolderButtonClicked()
103 QString oldPath = m_ui->textInputPath->text();
104 QString path = QFileDialog::getExistingDirectory(this, tr("Select folder"), oldPath);
105 updateInputPath(path);
108 void TorrentCreatorDialog::onAddFileButtonClicked()
110 QString oldPath = m_ui->textInputPath->text();
111 QString path = QFileDialog::getOpenFileName(this, tr("Select file"), oldPath);
112 updateInputPath(path);
115 int TorrentCreatorDialog::getPieceSize() const
117 const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB
118 return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024;
121 void TorrentCreatorDialog::dropEvent(QDropEvent *event)
123 event->acceptProposedAction();
125 if (event->mimeData()->hasUrls()) {
126 // only take the first one
127 QUrl firstItem = event->mimeData()->urls().first();
128 QString path = (firstItem.scheme().compare("file", Qt::CaseInsensitive) == 0)
129 ? firstItem.toLocalFile() : firstItem.toString();
130 updateInputPath(path);
134 void TorrentCreatorDialog::dragEnterEvent(QDragEnterEvent *event)
136 if (event->mimeData()->hasFormat("text/plain") || event->mimeData()->hasFormat("text/uri-list"))
137 event->acceptProposedAction();
140 // Main function that create a .torrent file
141 void TorrentCreatorDialog::onCreateButtonClicked()
143 QString input = Utils::Fs::fromNativePath(m_ui->textInputPath->text()).trimmed();
145 // test if readable
146 const QFileInfo fi(input);
147 if (!fi.isReadable()) {
148 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Path to file/folder is not readable."));
149 return;
151 input = fi.canonicalFilePath();
153 // get save path
154 const QString savePath = QString(m_storeLastSavePath) + QLatin1Char('/') + fi.fileName() + QLatin1String(".torrent");
155 QString destination = QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), savePath, tr("Torrent Files (*.torrent)"));
156 if (destination.isEmpty())
157 return;
158 if (!destination.endsWith(C_TORRENT_FILE_EXTENSION, Qt::CaseInsensitive))
159 destination += C_TORRENT_FILE_EXTENSION;
160 m_storeLastSavePath = Utils::Fs::branchPath(destination);
162 // Disable dialog & set busy cursor
163 setInteractionEnabled(false);
164 setCursor(QCursor(Qt::WaitCursor));
166 const QStringList trackers = m_ui->trackersList->toPlainText().trimmed()
167 .replace(QRegularExpression("\n\n[\n]+"), "\n\n").split('\n');
168 const QStringList urlSeeds = m_ui->URLSeedsList->toPlainText().split('\n', QString::SkipEmptyParts);
169 const QString comment = m_ui->txtComment->toPlainText();
170 const QString source = m_ui->lineEditSource->text();
172 // run the creator thread
173 m_creatorThread->create({ m_ui->checkPrivate->isChecked()
174 , m_ui->checkOptimizeAlignment->isChecked(), getPieceSize()
175 , input, destination, comment, source, trackers, urlSeeds });
178 void TorrentCreatorDialog::handleCreationFailure(const QString &msg)
180 // Remove busy cursor
181 setCursor(QCursor(Qt::ArrowCursor));
182 QMessageBox::information(this, tr("Torrent creation failed"), tr("Reason: %1").arg(msg));
183 setInteractionEnabled(true);
186 void TorrentCreatorDialog::handleCreationSuccess(const QString &path, const QString &branchPath)
188 // Remove busy cursor
189 setCursor(QCursor(Qt::ArrowCursor));
190 if (m_ui->checkStartSeeding->isChecked()) {
191 // Create save path temp data
192 BitTorrent::TorrentInfo t = BitTorrent::TorrentInfo::loadFromFile(Utils::Fs::toNativePath(path));
193 if (!t.isValid()) {
194 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
195 return;
198 BitTorrent::AddTorrentParams params;
199 params.savePath = branchPath;
200 params.skipChecking = true;
201 params.ignoreShareLimits = m_ui->checkIgnoreShareLimits->isChecked();
203 BitTorrent::Session::instance()->addTorrent(t, params);
205 QMessageBox::information(this, tr("Torrent creator")
206 , QString("%1\n%2").arg(tr("Torrent created:"), Utils::Fs::toNativePath(path)));
207 setInteractionEnabled(true);
210 void TorrentCreatorDialog::updateProgressBar(int progress)
212 m_ui->progressBar->setValue(progress);
215 void TorrentCreatorDialog::updatePiecesCount()
217 const QString path = m_ui->textInputPath->text().trimmed();
218 const bool isAlignmentOptimized = m_ui->checkOptimizeAlignment->isChecked();
220 const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path, getPieceSize(), isAlignmentOptimized);
221 m_ui->labelTotalPieces->setText(QString::number(count));
224 void TorrentCreatorDialog::setInteractionEnabled(bool enabled)
226 m_ui->textInputPath->setEnabled(enabled);
227 m_ui->addFileButton->setEnabled(enabled);
228 m_ui->addFolderButton->setEnabled(enabled);
229 m_ui->trackersList->setEnabled(enabled);
230 m_ui->URLSeedsList->setEnabled(enabled);
231 m_ui->txtComment->setEnabled(enabled);
232 m_ui->comboPieceSize->setEnabled(enabled);
233 m_ui->checkPrivate->setEnabled(enabled);
234 m_ui->checkStartSeeding->setEnabled(enabled);
235 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
236 m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked());
239 void TorrentCreatorDialog::saveSettings()
241 m_storeLastAddPath = m_ui->textInputPath->text().trimmed();
243 m_storePieceSize = m_ui->comboPieceSize->currentIndex();
244 m_storePrivateTorrent = m_ui->checkPrivate->isChecked();
245 m_storeStartSeeding = m_ui->checkStartSeeding->isChecked();
246 m_storeIgnoreRatio = m_ui->checkIgnoreShareLimits->isChecked();
247 m_storeOptimizeAlignment = m_ui->checkOptimizeAlignment->isChecked();
249 m_storeTrackerList = m_ui->trackersList->toPlainText();
250 m_storeWebSeedList = m_ui->URLSeedsList->toPlainText();
251 m_storeComments = m_ui->txtComment->toPlainText();
252 m_storeSource = m_ui->lineEditSource->text();
254 m_storeDialogSize = size();
257 void TorrentCreatorDialog::loadSettings()
259 m_ui->textInputPath->setText(m_storeLastAddPath);
261 m_ui->comboPieceSize->setCurrentIndex(m_storePieceSize);
262 m_ui->checkPrivate->setChecked(m_storePrivateTorrent);
263 m_ui->checkStartSeeding->setChecked(m_storeStartSeeding);
264 m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio);
265 m_ui->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment);
266 m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked());
268 m_ui->trackersList->setPlainText(m_storeTrackerList);
269 m_ui->URLSeedsList->setPlainText(m_storeWebSeedList);
270 m_ui->txtComment->setPlainText(m_storeComments);
271 m_ui->lineEditSource->setText(m_storeSource);
273 Utils::Gui::resize(this, m_storeDialogSize);