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>
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"
45 #define SETTINGS_KEY(name) "TorrentCreator/" name
47 TorrentCreatorDialog::TorrentCreatorDialog(QWidget
*parent
, const QString
&defaultPath
)
49 , m_ui(new Ui::TorrentCreatorDialog
)
50 , m_creatorThread(new BitTorrent::TorrentCreatorThread(this))
51 , m_storeDialogSize(SETTINGS_KEY("Size"))
52 , m_storePieceSize(SETTINGS_KEY("PieceSize"))
53 , m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent"))
54 , m_storeStartSeeding(SETTINGS_KEY("StartSeeding"))
55 , m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio"))
56 #ifdef QBT_USES_LIBTORRENT2
57 , m_storeTorrentFormat(SETTINGS_KEY("TorrentFormat"))
59 , m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"))
60 , m_paddedFileSizeLimit(SETTINGS_KEY("PaddedFileSizeLimit"))
62 , m_storeLastAddPath(SETTINGS_KEY("LastAddPath"))
63 , m_storeTrackerList(SETTINGS_KEY("TrackerList"))
64 , m_storeWebSeedList(SETTINGS_KEY("WebSeedList"))
65 , m_storeComments(SETTINGS_KEY("Comments"))
66 , m_storeLastSavePath(SETTINGS_KEY("LastSavePath"))
67 , m_storeSource(SETTINGS_KEY("Source"))
70 setAttribute(Qt::WA_DeleteOnClose
);
73 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setText(tr("Create Torrent"));
75 connect(m_ui
->addFileButton
, &QPushButton::clicked
, this, &TorrentCreatorDialog::onAddFileButtonClicked
);
76 connect(m_ui
->addFolderButton
, &QPushButton::clicked
, this, &TorrentCreatorDialog::onAddFolderButtonClicked
);
77 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &TorrentCreatorDialog::onCreateButtonClicked
);
78 connect(m_ui
->buttonCalcTotalPieces
, &QPushButton::clicked
, this, &TorrentCreatorDialog::updatePiecesCount
);
80 connect(m_creatorThread
, &BitTorrent::TorrentCreatorThread::creationSuccess
, this, &TorrentCreatorDialog::handleCreationSuccess
);
81 connect(m_creatorThread
, &BitTorrent::TorrentCreatorThread::creationFailure
, this, &TorrentCreatorDialog::handleCreationFailure
);
82 connect(m_creatorThread
, &BitTorrent::TorrentCreatorThread::updateProgress
, this, &TorrentCreatorDialog::updateProgressBar
);
85 updateInputPath(defaultPath
);
87 #ifdef QBT_USES_LIBTORRENT2
88 m_ui
->checkOptimizeAlignment
->hide();
90 m_ui
->widgetTorrentFormat
->hide();
96 TorrentCreatorDialog::~TorrentCreatorDialog()
103 void TorrentCreatorDialog::updateInputPath(const QString
&path
)
105 if (path
.isEmpty()) return;
106 m_ui
->textInputPath
->setText(Utils::Fs::toNativePath(path
));
107 updateProgressBar(0);
110 void TorrentCreatorDialog::onAddFolderButtonClicked()
112 QString oldPath
= m_ui
->textInputPath
->text();
113 QString path
= QFileDialog::getExistingDirectory(this, tr("Select folder"), oldPath
);
114 updateInputPath(path
);
117 void TorrentCreatorDialog::onAddFileButtonClicked()
119 QString oldPath
= m_ui
->textInputPath
->text();
120 QString path
= QFileDialog::getOpenFileName(this, tr("Select file"), oldPath
);
121 updateInputPath(path
);
124 int TorrentCreatorDialog::getPieceSize() const
126 const int pieceSizes
[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB
127 return pieceSizes
[m_ui
->comboPieceSize
->currentIndex()] * 1024;
130 #ifdef QBT_USES_LIBTORRENT2
131 BitTorrent::TorrentFormat
TorrentCreatorDialog::getTorrentFormat() const
133 switch (m_ui
->comboTorrentFormat
->currentIndex())
136 return BitTorrent::TorrentFormat::V2
;
138 return BitTorrent::TorrentFormat::Hybrid
;
140 return BitTorrent::TorrentFormat::V1
;
142 return BitTorrent::TorrentFormat::Hybrid
;
145 int TorrentCreatorDialog::getPaddedFileSizeLimit() const
147 const int value
= m_ui
->spinPaddedFileSizeLimit
->value();
148 return ((value
>= 0) ? (value
* 1024) : -1);
152 void TorrentCreatorDialog::dropEvent(QDropEvent
*event
)
154 event
->acceptProposedAction();
156 if (event
->mimeData()->hasUrls())
158 // only take the first one
159 QUrl firstItem
= event
->mimeData()->urls().first();
160 QString path
= (firstItem
.scheme().compare("file", Qt::CaseInsensitive
) == 0)
161 ? firstItem
.toLocalFile() : firstItem
.toString();
162 updateInputPath(path
);
166 void TorrentCreatorDialog::dragEnterEvent(QDragEnterEvent
*event
)
168 if (event
->mimeData()->hasFormat("text/plain") || event
->mimeData()->hasFormat("text/uri-list"))
169 event
->acceptProposedAction();
172 // Main function that create a .torrent file
173 void TorrentCreatorDialog::onCreateButtonClicked()
175 QString input
= Utils::Fs::toUniformPath(m_ui
->textInputPath
->text()).trimmed();
178 const QFileInfo
fi(input
);
179 if (!fi
.isReadable())
181 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Path to file/folder is not readable."));
184 input
= fi
.canonicalFilePath();
187 const QString savePath
= m_storeLastSavePath
.get(QDir::homePath()) + QLatin1Char('/') + fi
.fileName() + QLatin1String(".torrent");
188 QString destination
= QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), savePath
, tr("Torrent Files (*.torrent)"));
189 if (destination
.isEmpty())
191 if (!destination
.endsWith(C_TORRENT_FILE_EXTENSION
, Qt::CaseInsensitive
))
192 destination
+= C_TORRENT_FILE_EXTENSION
;
193 m_storeLastSavePath
= Utils::Fs::branchPath(destination
);
195 // Disable dialog & set busy cursor
196 setInteractionEnabled(false);
197 setCursor(QCursor(Qt::WaitCursor
));
199 const QStringList trackers
= m_ui
->trackersList
->toPlainText().trimmed()
200 .replace(QRegularExpression("\n\n[\n]+"), "\n\n").split('\n');
201 const BitTorrent::TorrentCreatorParams params
203 m_ui
->checkPrivate
->isChecked()
204 #ifdef QBT_USES_LIBTORRENT2
207 , m_ui
->checkOptimizeAlignment
->isChecked()
208 , getPaddedFileSizeLimit()
212 , m_ui
->txtComment
->toPlainText()
213 , m_ui
->lineEditSource
->text()
215 , m_ui
->URLSeedsList
->toPlainText().split('\n', Qt::SkipEmptyParts
)
218 // run the creator thread
219 m_creatorThread
->create(params
);
222 void TorrentCreatorDialog::handleCreationFailure(const QString
&msg
)
224 // Remove busy cursor
225 setCursor(QCursor(Qt::ArrowCursor
));
226 QMessageBox::information(this, tr("Torrent creation failed"), tr("Reason: %1").arg(msg
));
227 setInteractionEnabled(true);
230 void TorrentCreatorDialog::handleCreationSuccess(const QString
&path
, const QString
&branchPath
)
232 // Remove busy cursor
233 setCursor(QCursor(Qt::ArrowCursor
));
234 if (m_ui
->checkStartSeeding
->isChecked())
236 // Create save path temp data
237 const BitTorrent::TorrentInfo info
= BitTorrent::TorrentInfo::loadFromFile(Utils::Fs::toNativePath(path
));
240 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
244 BitTorrent::AddTorrentParams params
;
245 params
.savePath
= branchPath
;
246 params
.skipChecking
= true;
247 if (m_ui
->checkIgnoreShareLimits
->isChecked())
249 params
.ratioLimit
= BitTorrent::Torrent::NO_RATIO_LIMIT
;
250 params
.seedingTimeLimit
= BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT
;
252 params
.useAutoTMM
= false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path
254 BitTorrent::Session::instance()->addTorrent(info
, params
);
256 QMessageBox::information(this, tr("Torrent creator")
257 , QString::fromLatin1("%1\n%2").arg(tr("Torrent created:"), Utils::Fs::toNativePath(path
)));
258 setInteractionEnabled(true);
261 void TorrentCreatorDialog::updateProgressBar(int progress
)
263 m_ui
->progressBar
->setValue(progress
);
266 void TorrentCreatorDialog::updatePiecesCount()
268 const QString path
= m_ui
->textInputPath
->text().trimmed();
269 #ifdef QBT_USES_LIBTORRENT2
270 const int count
= BitTorrent::TorrentCreatorThread::calculateTotalPieces(
271 path
, getPieceSize(), getTorrentFormat());
273 const bool isAlignmentOptimized
= m_ui
->checkOptimizeAlignment
->isChecked();
274 const int count
= BitTorrent::TorrentCreatorThread::calculateTotalPieces(path
275 , getPieceSize(), isAlignmentOptimized
, getPaddedFileSizeLimit());
277 m_ui
->labelTotalPieces
->setText(QString::number(count
));
280 void TorrentCreatorDialog::setInteractionEnabled(const bool enabled
) const
282 m_ui
->textInputPath
->setEnabled(enabled
);
283 m_ui
->addFileButton
->setEnabled(enabled
);
284 m_ui
->addFolderButton
->setEnabled(enabled
);
285 m_ui
->trackersList
->setEnabled(enabled
);
286 m_ui
->URLSeedsList
->setEnabled(enabled
);
287 m_ui
->txtComment
->setEnabled(enabled
);
288 m_ui
->comboPieceSize
->setEnabled(enabled
);
289 m_ui
->buttonCalcTotalPieces
->setEnabled(enabled
);
290 m_ui
->checkPrivate
->setEnabled(enabled
);
291 m_ui
->checkStartSeeding
->setEnabled(enabled
);
292 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(enabled
);
293 m_ui
->checkIgnoreShareLimits
->setEnabled(enabled
&& m_ui
->checkStartSeeding
->isChecked());
294 #ifdef QBT_USES_LIBTORRENT2
295 m_ui
->widgetTorrentFormat
->setEnabled(enabled
);
297 m_ui
->checkOptimizeAlignment
->setEnabled(enabled
);
298 m_ui
->spinPaddedFileSizeLimit
->setEnabled(enabled
);
302 void TorrentCreatorDialog::saveSettings()
304 m_storeLastAddPath
= m_ui
->textInputPath
->text().trimmed();
306 m_storePieceSize
= m_ui
->comboPieceSize
->currentIndex();
307 m_storePrivateTorrent
= m_ui
->checkPrivate
->isChecked();
308 m_storeStartSeeding
= m_ui
->checkStartSeeding
->isChecked();
309 m_storeIgnoreRatio
= m_ui
->checkIgnoreShareLimits
->isChecked();
310 #ifdef QBT_USES_LIBTORRENT2
311 m_storeTorrentFormat
= m_ui
->comboTorrentFormat
->currentIndex();
313 m_storeOptimizeAlignment
= m_ui
->checkOptimizeAlignment
->isChecked();
314 m_paddedFileSizeLimit
= m_ui
->spinPaddedFileSizeLimit
->value();
317 m_storeTrackerList
= m_ui
->trackersList
->toPlainText();
318 m_storeWebSeedList
= m_ui
->URLSeedsList
->toPlainText();
319 m_storeComments
= m_ui
->txtComment
->toPlainText();
320 m_storeSource
= m_ui
->lineEditSource
->text();
322 m_storeDialogSize
= size();
325 void TorrentCreatorDialog::loadSettings()
327 m_ui
->textInputPath
->setText(m_storeLastAddPath
.get(QDir::homePath()));
329 m_ui
->comboPieceSize
->setCurrentIndex(m_storePieceSize
);
330 m_ui
->checkPrivate
->setChecked(m_storePrivateTorrent
);
331 m_ui
->checkStartSeeding
->setChecked(m_storeStartSeeding
);
332 m_ui
->checkIgnoreShareLimits
->setChecked(m_storeIgnoreRatio
);
333 m_ui
->checkIgnoreShareLimits
->setEnabled(m_ui
->checkStartSeeding
->isChecked());
334 #ifdef QBT_USES_LIBTORRENT2
335 m_ui
->comboTorrentFormat
->setCurrentIndex(m_storeTorrentFormat
.get(1));
337 m_ui
->checkOptimizeAlignment
->setChecked(m_storeOptimizeAlignment
.get(true));
338 m_ui
->spinPaddedFileSizeLimit
->setValue(m_paddedFileSizeLimit
.get(-1));
341 m_ui
->trackersList
->setPlainText(m_storeTrackerList
);
342 m_ui
->URLSeedsList
->setPlainText(m_storeWebSeedList
);
343 m_ui
->txtComment
->setPlainText(m_storeComments
);
344 m_ui
->lineEditSource
->setText(m_storeSource
);
346 Utils::Gui::resize(this, m_storeDialogSize
);