32 #include <QCloseEvent>
33 #include <QFileDialog>
34 #include <QMessageBox>
42 #include "ui_torrentcreatordialog.h"
45 #define SETTINGS_KEY(name) "TorrentCreator/" name
50 , m_creatorThread(new
BitTorrent::TorrentCreatorThread(this))
56 #ifdef QBT_USES_LIBTORRENT2
59 , m_storeOptimizeAlignment(
SETTINGS_KEY(
"OptimizeAlignment"))
60 , m_paddedFileSizeLimit(
SETTINGS_KEY(
"PaddedFileSizeLimit"))
70 setAttribute(Qt::WA_DeleteOnClose);
73 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr(
"Create Torrent"));
87 #ifdef QBT_USES_LIBTORRENT2
88 m_ui->checkOptimizeAlignment->hide();
90 m_ui->widgetTorrentFormat->hide();
105 if (path.isEmpty())
return;
112 QString oldPath =
m_ui->textInputPath->text();
113 QString path = QFileDialog::getExistingDirectory(
this, tr(
"Select folder"), oldPath);
119 QString oldPath =
m_ui->textInputPath->text();
120 QString path = QFileDialog::getOpenFileName(
this, tr(
"Select file"), oldPath);
126 const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
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;
147 const int value =
m_ui->spinPaddedFileSizeLimit->value();
154 event->acceptProposedAction();
156 if (event->mimeData()->hasUrls())
159 QUrl firstItem =
event->mimeData()->urls().first();
160 QString path = (firstItem.scheme().compare(
"file", Qt::CaseInsensitive) == 0)
161 ? firstItem.toLocalFile() : firstItem.toString();
168 if (event->mimeData()->hasFormat(
"text/plain") || event->mimeData()->hasFormat(
"text/uri-list"))
169 event->acceptProposedAction();
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())
197 setCursor(QCursor(Qt::WaitCursor));
199 const QStringList trackers =
m_ui->trackersList->toPlainText().trimmed()
200 .replace(QRegularExpression(
"\n\n[\n]+"),
"\n\n").split(
'\n');
203 m_ui->checkPrivate->isChecked()
204 #ifdef QBT_USES_LIBTORRENT2
207 ,
m_ui->checkOptimizeAlignment->isChecked()
212 ,
m_ui->txtComment->toPlainText()
213 ,
m_ui->lineEditSource->text()
215 ,
m_ui->URLSeedsList->toPlainText().split(
'\n', Qt::SkipEmptyParts)
225 setCursor(QCursor(Qt::ArrowCursor));
226 QMessageBox::information(
this, tr(
"Torrent creation failed"), tr(
"Reason: %1").arg(msg));
233 setCursor(QCursor(Qt::ArrowCursor));
234 if (
m_ui->checkStartSeeding->isChecked())
240 QMessageBox::critical(
this, tr(
"Torrent creation failed"), tr(
"Reason: Created torrent is invalid. It won't be added to download list."));
247 if (
m_ui->checkIgnoreShareLimits->isChecked())
256 QMessageBox::information(
this, tr(
"Torrent creator")
263 m_ui->progressBar->setValue(progress);
268 const QString path =
m_ui->textInputPath->text().trimmed();
269 #ifdef QBT_USES_LIBTORRENT2
273 const bool isAlignmentOptimized =
m_ui->checkOptimizeAlignment->isChecked();
277 m_ui->labelTotalPieces->setText(QString::number(count));
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);
310 #ifdef QBT_USES_LIBTORRENT2
311 m_storeTorrentFormat =
m_ui->comboTorrentFormat->currentIndex();
333 m_ui->checkIgnoreShareLimits->setEnabled(
m_ui->checkStartSeeding->isChecked());
334 #ifdef QBT_USES_LIBTORRENT2
335 m_ui->comboTorrentFormat->setCurrentIndex(m_storeTorrentFormat.get(1));
static Session * instance()
bool addTorrent(const QString &source, const AddTorrentParams ¶ms=AddTorrentParams())
static int calculateTotalPieces(const QString &inputPath, const int pieceSize, const bool isAlignmentOptimized, int paddedFileSizeLimit)
void creationSuccess(const QString &path, const QString &branchPath)
void updateProgress(int progress)
void create(const TorrentCreatorParams ¶ms)
void creationFailure(const QString &msg)
static const int NO_SEEDING_TIME_LIMIT
static const qreal NO_RATIO_LIMIT
static nonstd::expected< TorrentInfo, QString > loadFromFile(const QString &path) noexcept
T get(const T &defaultValue={}) const
SettingValue< QString > m_storeComments
SettingValue< QString > m_storeWebSeedList
void updateInputPath(const QString &path)
SettingValue< bool > m_storeIgnoreRatio
void onAddFileButtonClicked()
void updateProgressBar(int progress)
~TorrentCreatorDialog() override
TorrentCreatorDialog(QWidget *parent=nullptr, const QString &defaultPath={})
SettingValue< QString > m_storeSource
void dropEvent(QDropEvent *event) override
void onCreateButtonClicked()
SettingValue< QString > m_storeTrackerList
SettingValue< bool > m_storeOptimizeAlignment
void dragEnterEvent(QDragEnterEvent *event) override
SettingValue< QString > m_storeLastSavePath
SettingValue< bool > m_storeStartSeeding
Ui::TorrentCreatorDialog * m_ui
BitTorrent::TorrentCreatorThread * m_creatorThread
SettingValue< QString > m_storeLastAddPath
void onAddFolderButtonClicked()
int getPaddedFileSizeLimit() const
SettingValue< QSize > m_storeDialogSize
void setInteractionEnabled(bool enabled) const
void handleCreationSuccess(const QString &path, const QString &branchPath)
SettingValue< int > m_storePieceSize
SettingValue< bool > m_storePrivateTorrent
SettingValue< int > m_paddedFileSizeLimit
void handleCreationFailure(const QString &msg)
const char C_TORRENT_FILE_EXTENSION[]
QString toUniformPath(const QString &path)
QString branchPath(const QString &filePath, QString *removed=nullptr)
QString toNativePath(const QString &path)
void resize(QWidget *widget, const QSize &newSize={})
T value(const QString &key, const T &defaultValue={})
std::optional< bool > useAutoTMM
#define SETTINGS_KEY(name)