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) u"TorrentCreator/" name
49 // When the root file/directory of the created torrent is a symlink, we want to keep the symlink name in the torrent.
50 // On Windows, however, QFileDialog::DontResolveSymlinks disables shortcuts (.lnk files) expansion, making it impossible to pick a file if its path contains a shortcut.
51 // As of NTFS symlinks, they don't seem to be resolved anyways.
53 const QFileDialog::Options FILE_DIALOG_OPTIONS
{QFileDialog::DontResolveSymlinks
};
55 const QFileDialog::Options FILE_DIALOG_OPTIONS
{};
59 TorrentCreatorDialog::TorrentCreatorDialog(QWidget
*parent
, const Path
&defaultPath
)
61 , m_ui(new Ui::TorrentCreatorDialog
)
62 , m_creatorThread(new BitTorrent::TorrentCreatorThread(this))
63 , m_storeDialogSize(SETTINGS_KEY(u
"Size"_s
))
64 , m_storePieceSize(SETTINGS_KEY(u
"PieceSize"_s
))
65 , m_storePrivateTorrent(SETTINGS_KEY(u
"PrivateTorrent"_s
))
66 , m_storeStartSeeding(SETTINGS_KEY(u
"StartSeeding"_s
))
67 , m_storeIgnoreRatio(SETTINGS_KEY(u
"IgnoreRatio"_s
))
68 #ifdef QBT_USES_LIBTORRENT2
69 , m_storeTorrentFormat(SETTINGS_KEY(u
"TorrentFormat"_s
))
71 , m_storeOptimizeAlignment(SETTINGS_KEY(u
"OptimizeAlignment"_s
))
72 , m_paddedFileSizeLimit(SETTINGS_KEY(u
"PaddedFileSizeLimit"_s
))
74 , m_storeLastAddPath(SETTINGS_KEY(u
"LastAddPath"_s
))
75 , m_storeTrackerList(SETTINGS_KEY(u
"TrackerList"_s
))
76 , m_storeWebSeedList(SETTINGS_KEY(u
"WebSeedList"_s
))
77 , m_storeComments(SETTINGS_KEY(u
"Comments"_s
))
78 , m_storeLastSavePath(SETTINGS_KEY(u
"LastSavePath"_s
))
79 , m_storeSource(SETTINGS_KEY(u
"Source"_s
))
83 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setText(tr("Create Torrent"));
84 m_ui
->textInputPath
->setMode(FileSystemPathEdit::Mode::ReadOnly
);
86 connect(m_ui
->addFileButton
, &QPushButton::clicked
, this, &TorrentCreatorDialog::onAddFileButtonClicked
);
87 connect(m_ui
->addFolderButton
, &QPushButton::clicked
, this, &TorrentCreatorDialog::onAddFolderButtonClicked
);
88 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &TorrentCreatorDialog::onCreateButtonClicked
);
89 connect(m_ui
->buttonBox
, &QDialogButtonBox::rejected
, this, &QDialog::reject
);
90 connect(m_ui
->buttonCalcTotalPieces
, &QPushButton::clicked
, this, &TorrentCreatorDialog::updatePiecesCount
);
91 connect(m_ui
->checkStartSeeding
, &QCheckBox::clicked
, m_ui
->checkIgnoreShareLimits
, &QWidget::setEnabled
);
93 connect(m_creatorThread
, &BitTorrent::TorrentCreatorThread::creationSuccess
, this, &TorrentCreatorDialog::handleCreationSuccess
);
94 connect(m_creatorThread
, &BitTorrent::TorrentCreatorThread::creationFailure
, this, &TorrentCreatorDialog::handleCreationFailure
);
95 connect(m_creatorThread
, &BitTorrent::TorrentCreatorThread::updateProgress
, this, &TorrentCreatorDialog::updateProgressBar
);
98 updateInputPath(defaultPath
);
100 #ifdef QBT_USES_LIBTORRENT2
101 m_ui
->checkOptimizeAlignment
->hide();
103 m_ui
->widgetTorrentFormat
->hide();
107 TorrentCreatorDialog::~TorrentCreatorDialog()
114 void TorrentCreatorDialog::updateInputPath(const Path
&path
)
116 if (path
.isEmpty()) return;
117 m_ui
->textInputPath
->setSelectedPath(path
);
118 updateProgressBar(0);
121 void TorrentCreatorDialog::onAddFolderButtonClicked()
123 const QString oldPath
= m_ui
->textInputPath
->selectedPath().data();
124 const Path path
{QFileDialog::getExistingDirectory(this, tr("Select folder")
125 , oldPath
, (QFileDialog::ShowDirsOnly
| FILE_DIALOG_OPTIONS
))};
126 updateInputPath(path
);
129 void TorrentCreatorDialog::onAddFileButtonClicked()
131 const QString oldPath
= m_ui
->textInputPath
->selectedPath().data();
132 const Path path
{QFileDialog::getOpenFileName(this, tr("Select file"), oldPath
, QString(), nullptr, FILE_DIALOG_OPTIONS
)};
133 updateInputPath(path
);
136 int TorrentCreatorDialog::getPieceSize() const
138 const int pieceSizes
[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB
139 return pieceSizes
[m_ui
->comboPieceSize
->currentIndex()] * 1024;
142 #ifdef QBT_USES_LIBTORRENT2
143 BitTorrent::TorrentFormat
TorrentCreatorDialog::getTorrentFormat() const
145 switch (m_ui
->comboTorrentFormat
->currentIndex())
148 return BitTorrent::TorrentFormat::V2
;
150 return BitTorrent::TorrentFormat::Hybrid
;
152 return BitTorrent::TorrentFormat::V1
;
154 return BitTorrent::TorrentFormat::Hybrid
;
157 int TorrentCreatorDialog::getPaddedFileSizeLimit() const
159 const int value
= m_ui
->spinPaddedFileSizeLimit
->value();
160 return ((value
>= 0) ? (value
* 1024) : -1);
164 void TorrentCreatorDialog::dropEvent(QDropEvent
*event
)
166 event
->acceptProposedAction();
168 if (event
->mimeData()->hasUrls())
170 // only take the first one
171 const QUrl firstItem
= event
->mimeData()->urls().first();
173 (firstItem
.scheme().compare(u
"file", Qt::CaseInsensitive
) == 0)
174 ? firstItem
.toLocalFile() : firstItem
.toString()
176 updateInputPath(path
);
180 void TorrentCreatorDialog::dragEnterEvent(QDragEnterEvent
*event
)
182 if (event
->mimeData()->hasFormat(u
"text/plain"_s
) || event
->mimeData()->hasFormat(u
"text/uri-list"_s
))
183 event
->acceptProposedAction();
186 // Main function that create a .torrent file
187 void TorrentCreatorDialog::onCreateButtonClicked()
190 // Resolve the path in case it contains a shortcut (otherwise, the following usages will consider it invalid)
191 const Path inputPath
= Utils::Fs::toCanonicalPath(m_ui
->textInputPath
->selectedPath());
193 const Path inputPath
= m_ui
->textInputPath
->selectedPath();
197 if (!Utils::Fs::isReadable(inputPath
))
199 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Path to file/folder is not readable."));
204 const Path lastSavePath
= (m_storeLastSavePath
.get(Utils::Fs::homePath()) / Path(inputPath
.filename() + u
".torrent"));
205 Path destPath
{QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), lastSavePath
.data(), tr("Torrent Files (*.torrent)"))};
206 if (destPath
.isEmpty())
208 if (!destPath
.hasExtension(TORRENT_FILE_EXTENSION
))
209 destPath
+= TORRENT_FILE_EXTENSION
;
210 m_storeLastSavePath
= destPath
.parentPath();
212 // Disable dialog & set busy cursor
213 setInteractionEnabled(false);
214 setCursor(QCursor(Qt::WaitCursor
));
216 const QStringList trackers
= m_ui
->trackersList
->toPlainText().trimmed()
217 .replace(QRegularExpression(u
"\n\n[\n]+"_s
), u
"\n\n"_s
).split(u
'\n');
218 const BitTorrent::TorrentCreatorParams params
220 m_ui
->checkPrivate
->isChecked()
221 #ifdef QBT_USES_LIBTORRENT2
224 , m_ui
->checkOptimizeAlignment
->isChecked()
225 , getPaddedFileSizeLimit()
230 , m_ui
->txtComment
->toPlainText()
231 , m_ui
->lineEditSource
->text()
233 , m_ui
->URLSeedsList
->toPlainText().split(u
'\n', Qt::SkipEmptyParts
)
236 // run the creator thread
237 m_creatorThread
->create(params
);
240 void TorrentCreatorDialog::handleCreationFailure(const QString
&msg
)
242 // Remove busy cursor
243 setCursor(QCursor(Qt::ArrowCursor
));
244 QMessageBox::information(this, tr("Torrent creation failed"), tr("Reason: %1").arg(msg
));
245 setInteractionEnabled(true);
248 void TorrentCreatorDialog::handleCreationSuccess(const Path
&path
, const Path
&branchPath
)
250 // Remove busy cursor
251 setCursor(QCursor(Qt::ArrowCursor
));
252 if (m_ui
->checkStartSeeding
->isChecked())
254 // Create save path temp data
255 const nonstd::expected
<BitTorrent::TorrentInfo
, QString
> result
= BitTorrent::TorrentInfo::loadFromFile(path
);
258 QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
262 BitTorrent::AddTorrentParams params
;
263 params
.savePath
= branchPath
;
264 params
.skipChecking
= true;
265 if (m_ui
->checkIgnoreShareLimits
->isChecked())
267 params
.ratioLimit
= BitTorrent::Torrent::NO_RATIO_LIMIT
;
268 params
.seedingTimeLimit
= BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT
;
269 params
.inactiveSeedingTimeLimit
= BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT
;
271 params
.useAutoTMM
= false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path
273 BitTorrent::Session::instance()->addTorrent(result
.value(), params
);
275 QMessageBox::information(this, tr("Torrent creator")
276 , u
"%1\n%2"_s
.arg(tr("Torrent created:"), path
.toString()));
277 setInteractionEnabled(true);
280 void TorrentCreatorDialog::updateProgressBar(int progress
)
282 m_ui
->progressBar
->setValue(progress
);
285 void TorrentCreatorDialog::updatePiecesCount()
287 const Path path
= m_ui
->textInputPath
->selectedPath();
288 #ifdef QBT_USES_LIBTORRENT2
289 const int count
= BitTorrent::TorrentCreatorThread::calculateTotalPieces(
290 path
, getPieceSize(), getTorrentFormat());
292 const bool isAlignmentOptimized
= m_ui
->checkOptimizeAlignment
->isChecked();
293 const int count
= BitTorrent::TorrentCreatorThread::calculateTotalPieces(path
294 , getPieceSize(), isAlignmentOptimized
, getPaddedFileSizeLimit());
296 m_ui
->labelTotalPieces
->setText(QString::number(count
));
299 void TorrentCreatorDialog::setInteractionEnabled(const bool enabled
) const
301 m_ui
->textInputPath
->setEnabled(enabled
);
302 m_ui
->addFileButton
->setEnabled(enabled
);
303 m_ui
->addFolderButton
->setEnabled(enabled
);
304 m_ui
->trackersList
->setEnabled(enabled
);
305 m_ui
->URLSeedsList
->setEnabled(enabled
);
306 m_ui
->txtComment
->setEnabled(enabled
);
307 m_ui
->comboPieceSize
->setEnabled(enabled
);
308 m_ui
->buttonCalcTotalPieces
->setEnabled(enabled
);
309 m_ui
->checkPrivate
->setEnabled(enabled
);
310 m_ui
->checkStartSeeding
->setEnabled(enabled
);
311 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(enabled
);
312 m_ui
->checkIgnoreShareLimits
->setEnabled(enabled
&& m_ui
->checkStartSeeding
->isChecked());
313 #ifdef QBT_USES_LIBTORRENT2
314 m_ui
->widgetTorrentFormat
->setEnabled(enabled
);
316 m_ui
->checkOptimizeAlignment
->setEnabled(enabled
);
317 m_ui
->spinPaddedFileSizeLimit
->setEnabled(enabled
);
321 void TorrentCreatorDialog::saveSettings()
323 m_storeLastAddPath
= m_ui
->textInputPath
->selectedPath();
325 m_storePieceSize
= m_ui
->comboPieceSize
->currentIndex();
326 m_storePrivateTorrent
= m_ui
->checkPrivate
->isChecked();
327 m_storeStartSeeding
= m_ui
->checkStartSeeding
->isChecked();
328 m_storeIgnoreRatio
= m_ui
->checkIgnoreShareLimits
->isChecked();
329 #ifdef QBT_USES_LIBTORRENT2
330 m_storeTorrentFormat
= m_ui
->comboTorrentFormat
->currentIndex();
332 m_storeOptimizeAlignment
= m_ui
->checkOptimizeAlignment
->isChecked();
333 m_paddedFileSizeLimit
= m_ui
->spinPaddedFileSizeLimit
->value();
336 m_storeTrackerList
= m_ui
->trackersList
->toPlainText();
337 m_storeWebSeedList
= m_ui
->URLSeedsList
->toPlainText();
338 m_storeComments
= m_ui
->txtComment
->toPlainText();
339 m_storeSource
= m_ui
->lineEditSource
->text();
341 m_storeDialogSize
= size();
344 void TorrentCreatorDialog::loadSettings()
346 m_ui
->textInputPath
->setSelectedPath(m_storeLastAddPath
.get(Utils::Fs::homePath()));
348 m_ui
->comboPieceSize
->setCurrentIndex(m_storePieceSize
);
349 m_ui
->checkPrivate
->setChecked(m_storePrivateTorrent
);
350 m_ui
->checkStartSeeding
->setChecked(m_storeStartSeeding
);
351 m_ui
->checkIgnoreShareLimits
->setChecked(m_storeIgnoreRatio
);
352 m_ui
->checkIgnoreShareLimits
->setEnabled(m_ui
->checkStartSeeding
->isChecked());
353 #ifdef QBT_USES_LIBTORRENT2
354 m_ui
->comboTorrentFormat
->setCurrentIndex(m_storeTorrentFormat
.get(1));
356 m_ui
->checkOptimizeAlignment
->setChecked(m_storeOptimizeAlignment
.get(true));
357 m_ui
->spinPaddedFileSizeLimit
->setValue(m_paddedFileSizeLimit
.get(-1));
360 m_ui
->trackersList
->setPlainText(m_storeTrackerList
);
361 m_ui
->URLSeedsList
->setPlainText(m_storeWebSeedList
);
362 m_ui
->txtComment
->setPlainText(m_storeComments
);
363 m_ui
->lineEditSource
->setText(m_storeSource
);
365 if (const QSize dialogSize
= m_storeDialogSize
; dialogSize
.isValid())