Some work about adaptive color scheme for Web UI (PR #19901)
http://[316:c51a:62a3:8b9::4]/d4708/qBittorrent/src/branch/adaptive-webui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
11 KiB
274 lines
11 KiB
18 years ago
|
/*
|
||
8 years ago
|
* Bittorrent Client using Qt and libtorrent.
|
||
|
* Copyright (C) 2017 Mike Tzou (Chocobo1)
|
||
|
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
|
||
18 years ago
|
*
|
||
18 years ago
|
* This program is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU General Public License
|
||
|
* as published by the Free Software Foundation; either version 2
|
||
|
* of the License, or (at your option) any later version.
|
||
18 years ago
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
18 years ago
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
*
|
||
16 years ago
|
* In addition, as a special exception, the copyright holders give permission to
|
||
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
||
|
* and distribute the linked executables. You must obey the GNU General Public
|
||
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
||
|
* modify file(s), you may extend this exception to your version of the file(s),
|
||
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||
|
* exception statement from your version.
|
||
18 years ago
|
*/
|
||
|
|
||
7 years ago
|
#include "torrentcreatordialog.h"
|
||
9 years ago
|
|
||
|
#include <QCloseEvent>
|
||
18 years ago
|
#include <QFileDialog>
|
||
|
#include <QMessageBox>
|
||
9 years ago
|
#include <QMimeData>
|
||
|
#include <QUrl>
|
||
18 years ago
|
|
||
9 years ago
|
#include "base/bittorrent/session.h"
|
||
|
#include "base/bittorrent/torrentcreatorthread.h"
|
||
9 years ago
|
#include "base/bittorrent/torrentinfo.h"
|
||
|
#include "base/global.h"
|
||
9 years ago
|
#include "base/utils/fs.h"
|
||
7 years ago
|
#include "ui_torrentcreatordialog.h"
|
||
7 years ago
|
#include "utils.h"
|
||
16 years ago
|
|
||
9 years ago
|
#define SETTINGS_KEY(name) "TorrentCreator/" name
|
||
|
|
||
7 years ago
|
TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defaultPath)
|
||
9 years ago
|
: QDialog(parent)
|
||
7 years ago
|
, m_ui(new Ui::TorrentCreatorDialog)
|
||
9 years ago
|
, m_creatorThread(new BitTorrent::TorrentCreatorThread(this))
|
||
9 years ago
|
, m_storeDialogSize(SETTINGS_KEY("Dimension"))
|
||
|
, m_storePieceSize(SETTINGS_KEY("PieceSize"))
|
||
|
, m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent"))
|
||
|
, m_storeStartSeeding(SETTINGS_KEY("StartSeeding"))
|
||
|
, m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio"))
|
||
7 years ago
|
, m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"), true)
|
||
9 years ago
|
, m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath())
|
||
|
, m_storeTrackerList(SETTINGS_KEY("TrackerList"))
|
||
|
, m_storeWebSeedList(SETTINGS_KEY("WebSeedList"))
|
||
|
, m_storeComments(SETTINGS_KEY("Comments"))
|
||
|
, m_storeLastSavePath(SETTINGS_KEY("LastSavePath"), QDir::homePath())
|
||
7 years ago
|
, m_storeSource(SETTINGS_KEY("Source"))
|
||
9 years ago
|
{
|
||
|
m_ui->setupUi(this);
|
||
10 years ago
|
setAttribute(Qt::WA_DeleteOnClose);
|
||
9 years ago
|
setModal(false);
|
||
9 years ago
|
|
||
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent"));
|
||
|
|
||
7 years ago
|
connect(m_ui->addFileButton, &QPushButton::clicked, this, &TorrentCreatorDialog::onAddFileButtonClicked);
|
||
|
connect(m_ui->addFolderButton, &QPushButton::clicked, this, &TorrentCreatorDialog::onAddFolderButtonClicked);
|
||
|
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &TorrentCreatorDialog::onCreateButtonClicked);
|
||
|
connect(m_ui->buttonCalcTotalPieces, &QPushButton::clicked, this, &TorrentCreatorDialog::updatePiecesCount);
|
||
7 years ago
|
|
||
7 years ago
|
connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::creationSuccess, this, &TorrentCreatorDialog::handleCreationSuccess);
|
||
|
connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::creationFailure, this, &TorrentCreatorDialog::handleCreationFailure);
|
||
|
connect(m_creatorThread, &BitTorrent::TorrentCreatorThread::updateProgress, this, &TorrentCreatorDialog::updateProgressBar);
|
||
9 years ago
|
|
||
10 years ago
|
loadSettings();
|
||
9 years ago
|
updateInputPath(defaultPath);
|
||
|
|
||
10 years ago
|
show();
|
||
18 years ago
|
}
|
||
|
|
||
7 years ago
|
TorrentCreatorDialog::~TorrentCreatorDialog()
|
||
10 years ago
|
{
|
||
9 years ago
|
saveSettings();
|
||
|
|
||
|
delete m_ui;
|
||
17 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::updateInputPath(const QString &path)
|
||
9 years ago
|
{
|
||
|
if (path.isEmpty()) return;
|
||
|
m_ui->textInputPath->setText(Utils::Fs::toNativePath(path));
|
||
|
updateProgressBar(0);
|
||
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::onAddFolderButtonClicked()
|
||
10 years ago
|
{
|
||
9 years ago
|
QString oldPath = m_ui->textInputPath->text();
|
||
|
QString path = QFileDialog::getExistingDirectory(this, tr("Select folder"), oldPath);
|
||
9 years ago
|
updateInputPath(path);
|
||
18 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::onAddFileButtonClicked()
|
||
10 years ago
|
{
|
||
9 years ago
|
QString oldPath = m_ui->textInputPath->text();
|
||
|
QString path = QFileDialog::getOpenFileName(this, tr("Select file"), oldPath);
|
||
9 years ago
|
updateInputPath(path);
|
||
18 years ago
|
}
|
||
|
|
||
7 years ago
|
int TorrentCreatorDialog::getPieceSize() const
|
||
10 years ago
|
{
|
||
7 years ago
|
const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB
|
||
9 years ago
|
return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024;
|
||
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::dropEvent(QDropEvent *event)
|
||
9 years ago
|
{
|
||
|
event->acceptProposedAction();
|
||
|
|
||
|
if (event->mimeData()->hasUrls()) {
|
||
|
// only take the first one
|
||
|
QUrl firstItem = event->mimeData()->urls().first();
|
||
|
QString path = (firstItem.scheme().compare("file", Qt::CaseInsensitive) == 0)
|
||
|
? firstItem.toLocalFile() : firstItem.toString();
|
||
9 years ago
|
updateInputPath(path);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::dragEnterEvent(QDragEnterEvent *event)
|
||
9 years ago
|
{
|
||
|
if (event->mimeData()->hasFormat("text/plain") || event->mimeData()->hasFormat("text/uri-list"))
|
||
|
event->acceptProposedAction();
|
||
18 years ago
|
}
|
||
|
|
||
18 years ago
|
// Main function that create a .torrent file
|
||
7 years ago
|
void TorrentCreatorDialog::onCreateButtonClicked()
|
||
10 years ago
|
{
|
||
9 years ago
|
QString input = Utils::Fs::fromNativePath(m_ui->textInputPath->text()).trimmed();
|
||
|
|
||
|
// test if readable
|
||
7 years ago
|
const QFileInfo fi(input);
|
||
9 years ago
|
if (!fi.isReadable()) {
|
||
7 years ago
|
QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Path to file/folder is not readable."));
|
||
10 years ago
|
return;
|
||
|
}
|
||
9 years ago
|
input = fi.canonicalFilePath();
|
||
10 years ago
|
|
||
9 years ago
|
// get save path
|
||
7 years ago
|
const QString savePath = QString(m_storeLastSavePath) + QLatin1Char('/') + fi.fileName() + QLatin1String(".torrent");
|
||
|
QString destination = QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), savePath, tr("Torrent Files (*.torrent)"));
|
||
10 years ago
|
if (destination.isEmpty())
|
||
|
return;
|
||
9 years ago
|
if (!destination.endsWith(C_TORRENT_FILE_EXTENSION, Qt::CaseInsensitive))
|
||
|
destination += C_TORRENT_FILE_EXTENSION;
|
||
9 years ago
|
m_storeLastSavePath = Utils::Fs::branchPath(destination);
|
||
14 years ago
|
|
||
9 years ago
|
// Disable dialog & set busy cursor
|
||
10 years ago
|
setInteractionEnabled(false);
|
||
|
setCursor(QCursor(Qt::WaitCursor));
|
||
9 years ago
|
|
||
7 years ago
|
const QStringList trackers = m_ui->trackersList->toPlainText().trimmed()
|
||
|
.replace(QRegularExpression("\n\n[\n]+"), "\n\n").split('\n');
|
||
|
const QStringList urlSeeds = m_ui->URLSeedsList->toPlainText().split('\n', QString::SkipEmptyParts);
|
||
|
const QString comment = m_ui->txtComment->toPlainText();
|
||
7 years ago
|
const QString source = m_ui->lineEditSource->text();
|
||
9 years ago
|
|
||
9 years ago
|
// run the creator thread
|
||
7 years ago
|
m_creatorThread->create({ m_ui->checkPrivate->isChecked()
|
||
|
, m_ui->checkOptimizeAlignment->isChecked(), getPieceSize()
|
||
7 years ago
|
, input, destination, comment, source, trackers, urlSeeds });
|
||
17 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::handleCreationFailure(const QString &msg)
|
||
10 years ago
|
{
|
||
|
// Remove busy cursor
|
||
|
setCursor(QCursor(Qt::ArrowCursor));
|
||
7 years ago
|
QMessageBox::information(this, tr("Torrent creation failed"), tr("Reason: %1").arg(msg));
|
||
10 years ago
|
setInteractionEnabled(true);
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
void TorrentCreatorDialog::handleCreationSuccess(const QString &path, const QString &branchPath)
|
||
10 years ago
|
{
|
||
|
// Remove busy cursor
|
||
|
setCursor(QCursor(Qt::ArrowCursor));
|
||
9 years ago
|
if (m_ui->checkStartSeeding->isChecked()) {
|
||
10 years ago
|
// Create save path temp data
|
||
|
BitTorrent::TorrentInfo t = BitTorrent::TorrentInfo::loadFromFile(Utils::Fs::toNativePath(path));
|
||
|
if (!t.isValid()) {
|
||
7 years ago
|
QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
BitTorrent::AddTorrentParams params;
|
||
9 years ago
|
params.savePath = branchPath;
|
||
10 years ago
|
params.skipChecking = true;
|
||
9 years ago
|
params.ignoreShareLimits = m_ui->checkIgnoreShareLimits->isChecked();
|
||
10 years ago
|
|
||
10 years ago
|
BitTorrent::Session::instance()->addTorrent(t, params);
|
||
|
}
|
||
7 years ago
|
QMessageBox::information(this, tr("Torrent creator")
|
||
|
, QString("%1\n%2").arg(tr("Torrent created:"), Utils::Fs::toNativePath(path)));
|
||
9 years ago
|
setInteractionEnabled(true);
|
||
15 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::updateProgressBar(int progress)
|
||
10 years ago
|
{
|
||
9 years ago
|
m_ui->progressBar->setValue(progress);
|
||
17 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::updatePiecesCount()
|
||
8 years ago
|
{
|
||
|
const QString path = m_ui->textInputPath->text().trimmed();
|
||
7 years ago
|
const bool isAlignmentOptimized = m_ui->checkOptimizeAlignment->isChecked();
|
||
8 years ago
|
|
||
7 years ago
|
const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path, getPieceSize(), isAlignmentOptimized);
|
||
8 years ago
|
m_ui->labelTotalPieces->setText(QString::number(count));
|
||
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::setInteractionEnabled(bool enabled)
|
||
14 years ago
|
{
|
||
9 years ago
|
m_ui->textInputPath->setEnabled(enabled);
|
||
8 years ago
|
m_ui->addFileButton->setEnabled(enabled);
|
||
|
m_ui->addFolderButton->setEnabled(enabled);
|
||
|
m_ui->trackersList->setEnabled(enabled);
|
||
|
m_ui->URLSeedsList->setEnabled(enabled);
|
||
|
m_ui->txtComment->setEnabled(enabled);
|
||
9 years ago
|
m_ui->comboPieceSize->setEnabled(enabled);
|
||
8 years ago
|
m_ui->checkPrivate->setEnabled(enabled);
|
||
9 years ago
|
m_ui->checkStartSeeding->setEnabled(enabled);
|
||
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
|
||
|
m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked());
|
||
14 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::saveSettings()
|
||
14 years ago
|
{
|
||
9 years ago
|
m_storeLastAddPath = m_ui->textInputPath->text().trimmed();
|
||
9 years ago
|
|
||
9 years ago
|
m_storePieceSize = m_ui->comboPieceSize->currentIndex();
|
||
8 years ago
|
m_storePrivateTorrent = m_ui->checkPrivate->isChecked();
|
||
9 years ago
|
m_storeStartSeeding = m_ui->checkStartSeeding->isChecked();
|
||
|
m_storeIgnoreRatio = m_ui->checkIgnoreShareLimits->isChecked();
|
||
7 years ago
|
m_storeOptimizeAlignment = m_ui->checkOptimizeAlignment->isChecked();
|
||
9 years ago
|
|
||
8 years ago
|
m_storeTrackerList = m_ui->trackersList->toPlainText();
|
||
|
m_storeWebSeedList = m_ui->URLSeedsList->toPlainText();
|
||
|
m_storeComments = m_ui->txtComment->toPlainText();
|
||
7 years ago
|
m_storeSource = m_ui->lineEditSource->text();
|
||
9 years ago
|
|
||
9 years ago
|
m_storeDialogSize = size();
|
||
16 years ago
|
}
|
||
|
|
||
7 years ago
|
void TorrentCreatorDialog::loadSettings()
|
||
14 years ago
|
{
|
||
9 years ago
|
m_ui->textInputPath->setText(m_storeLastAddPath);
|
||
9 years ago
|
|
||
9 years ago
|
m_ui->comboPieceSize->setCurrentIndex(m_storePieceSize);
|
||
8 years ago
|
m_ui->checkPrivate->setChecked(m_storePrivateTorrent);
|
||
9 years ago
|
m_ui->checkStartSeeding->setChecked(m_storeStartSeeding);
|
||
|
m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio);
|
||
7 years ago
|
m_ui->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment);
|
||
9 years ago
|
m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked());
|
||
|
|
||
8 years ago
|
m_ui->trackersList->setPlainText(m_storeTrackerList);
|
||
|
m_ui->URLSeedsList->setPlainText(m_storeWebSeedList);
|
||
|
m_ui->txtComment->setPlainText(m_storeComments);
|
||
7 years ago
|
m_ui->lineEditSource->setText(m_storeSource);
|
||
14 years ago
|
|
||
7 years ago
|
Utils::Gui::resize(this, m_storeDialogSize);
|
||
14 years ago
|
}
|