Browse Source

Follow project coding style. Issue #2192.

adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
cbbf14b0f8
  1. 964
      src/misc.cpp
  2. 86
      src/misc.h
  3. 293
      src/torrentimportdlg.cpp
  4. 50
      src/torrentimportdlg.h

964
src/misc.cpp

File diff suppressed because it is too large Load Diff

86
src/misc.h

@ -48,12 +48,12 @@
namespace libtorrent { namespace libtorrent {
#if LIBTORRENT_VERSION_NUM < 10000 #if LIBTORRENT_VERSION_NUM < 10000
class big_number; class big_number;
typedef big_number sha1_hash; typedef big_number sha1_hash;
#else #else
class sha1_hash; class sha1_hash;
#endif #endif
struct lazy_entry; struct lazy_entry;
} }
const qlonglong MAX_ETA = 8640000; const qlonglong MAX_ETA = 8640000;
@ -62,58 +62,58 @@ enum shutDownAction { NO_SHUTDOWN, SHUTDOWN_COMPUTER, SUSPEND_COMPUTER, HIBERNAT
/* Miscellaneaous functions that can be useful */ /* Miscellaneaous functions that can be useful */
namespace misc namespace misc
{ {
QString toQString(const std::string &str); QString toQString(const std::string &str);
QString toQString(const char* str); QString toQString(const char* str);
QString toQStringU(const std::string &str); QString toQStringU(const std::string &str);
QString toQStringU(const char* str); QString toQStringU(const char* str);
QString toQString(const libtorrent::sha1_hash &hash); QString toQString(const libtorrent::sha1_hash &hash);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
void shutdownComputer(shutDownAction action=SHUTDOWN_COMPUTER); void shutdownComputer(shutDownAction action = SHUTDOWN_COMPUTER);
#endif #endif
QString parseHtmlLinks(const QString &raw_text); QString parseHtmlLinks(const QString &raw_text);
bool isUrl(const QString &s); bool isUrl(const QString &s);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
// Get screen center // Get screen center
QPoint screenCenter(QWidget *win); QPoint screenCenter(QWidget *win);
#endif #endif
int pythonVersion(); int pythonVersion();
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB) // return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
// use Binary prefix standards from IEC 60027-2 // use Binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte // see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes // value must be given in bytes
QString friendlyUnit(qreal val, bool is_speed = false); QString friendlyUnit(qreal val, bool is_speed = false);
bool isPreviewable(const QString& extension); bool isPreviewable(const QString& extension);
QString magnetUriToName(const QString& magnet_uri); QString magnetUriToName(const QString& magnet_uri);
QString magnetUriToHash(const QString& magnet_uri); QString magnetUriToHash(const QString& magnet_uri);
QList<QUrl> magnetUriToTrackers(const QString& magnet_uri); QList<QUrl> magnetUriToTrackers(const QString& magnet_uri);
QString bcLinkToMagnet(QString bc_link); QString bcLinkToMagnet(QString bc_link);
// Take a number of seconds and return an user-friendly // Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m". // time duration like "1d 2h 10m".
QString userFriendlyDuration(qlonglong seconds); QString userFriendlyDuration(qlonglong seconds);
QString getUserIDString(); QString getUserIDString();
// Convert functions // Convert functions
QStringList toStringList(const QList<bool> &l); QStringList toStringList(const QList<bool> &l);
QList<int> intListfromStringList(const QStringList &l); QList<int> intListfromStringList(const QStringList &l);
QList<bool> boolListfromStringList(const QStringList &l); QList<bool> boolListfromStringList(const QStringList &l);
QString toQString(time_t t); QString toQString(time_t t);
QString accurateDoubleToString(const double &n, const int &precision); QString accurateDoubleToString(const double &n, const int &precision);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
bool naturalSort(QString left, QString right, bool& result); bool naturalSort(QString left, QString right, bool& result);
#endif #endif
// Implements constant-time comparison to protect against timing attacks // Implements constant-time comparison to protect against timing attacks
// Taken from https://crackstation.net/hashing-security.htm // Taken from https://crackstation.net/hashing-security.htm
bool slowEquals(const QByteArray &a, const QByteArray &b); bool slowEquals(const QByteArray &a, const QByteArray &b);
void loadBencodedFile(const QString &filename, std::vector<char> &buffer, libtorrent::lazy_entry &entry, libtorrent::error_code &ec); void loadBencodedFile(const QString &filename, std::vector<char> &buffer, libtorrent::lazy_entry &entry, libtorrent::error_code &ec);
void msleep(unsigned long msecs); void msleep(unsigned long msecs);
} }
#endif #endif

293
src/torrentimportdlg.cpp

@ -42,175 +42,177 @@
using namespace libtorrent; using namespace libtorrent;
TorrentImportDlg::TorrentImportDlg(QWidget *parent) : TorrentImportDlg::TorrentImportDlg(QWidget *parent):
QDialog(parent), QDialog(parent),
ui(new Ui::TorrentImportDlg) ui(new Ui::TorrentImportDlg)
{ {
ui->setupUi(this); ui->setupUi(this);
// Icons // Icons
ui->lbl_info->setPixmap(IconProvider::instance()->getIcon("dialog-information").pixmap(ui->lbl_info->height())); ui->lbl_info->setPixmap(IconProvider::instance()->getIcon("dialog-information").pixmap(ui->lbl_info->height()));
ui->lbl_info->setFixedWidth(ui->lbl_info->height()); ui->lbl_info->setFixedWidth(ui->lbl_info->height());
ui->importBtn->setIcon(IconProvider::instance()->getIcon("document-import")); ui->importBtn->setIcon(IconProvider::instance()->getIcon("document-import"));
// Libtorrent < 0.15 does not support skipping file checking // Libtorrent < 0.15 does not support skipping file checking
loadSettings(); loadSettings();
} }
TorrentImportDlg::~TorrentImportDlg() TorrentImportDlg::~TorrentImportDlg()
{ {
delete ui; delete ui;
} }
void TorrentImportDlg::on_browseTorrentBtn_clicked() void TorrentImportDlg::on_browseTorrentBtn_clicked()
{ {
const QString default_dir = Preferences::instance()->getMainLastDir(); const QString default_dir = Preferences::instance()->getMainLastDir();
// Ask for a torrent file // Ask for a torrent file
m_torrentPath = QFileDialog::getOpenFileName(this, tr("Torrent file to import"), default_dir, tr("Torrent files")+QString(" (*.torrent)")); m_torrentPath = QFileDialog::getOpenFileName(this, tr("Torrent file to import"), default_dir, tr("Torrent files") + QString(" (*.torrent)"));
if (!m_torrentPath.isEmpty()) { if (!m_torrentPath.isEmpty())
loadTorrent(m_torrentPath); loadTorrent(m_torrentPath);
} else { else
ui->lineTorrent->clear(); ui->lineTorrent->clear();
}
} }
void TorrentImportDlg::on_browseContentBtn_clicked() void TorrentImportDlg::on_browseContentBtn_clicked()
{ {
const QString default_dir = Preferences::instance()->getTorImportLastContentDir(); const QString default_dir = Preferences::instance()->getTorImportLastContentDir();
// Test for multi-file taken from libtorrent/create_torrent.hpp -> create_torrent::create_torrent // Test for multi-file taken from libtorrent/create_torrent.hpp -> create_torrent::create_torrent
bool multifile = t->num_files() > 1; bool multifile = t->num_files() > 1;
#if LIBTORRENT_VERSION_NUM >= 1600 #if LIBTORRENT_VERSION_NUM >= 1600
if (!multifile && has_parent_path(t->files().file_path(*(t->files().begin())))) if (!multifile && has_parent_path(t->files().file_path(*(t->files().begin()))))
multifile = true; multifile = true;
#else #else
if (!multifile && t->file_at(0).path.has_parent_path()) if (!multifile && t->file_at(0).path.has_parent_path())
multifile = true; multifile = true;
#endif #endif
if (!multifile) { if (!multifile) {
// Single file torrent // Single file torrent
const QString file_name = fsutils::fileName(misc::toQStringU(t->file_at(0).path)); const QString file_name = fsutils::fileName(misc::toQStringU(t->file_at(0).path));
qDebug("Torrent has only one file: %s", qPrintable(file_name)); qDebug("Torrent has only one file: %s", qPrintable(file_name));
QString extension = fsutils::fileExtension(file_name); QString extension = fsutils::fileExtension(file_name);
qDebug("File extension is : %s", qPrintable(extension)); qDebug("File extension is : %s", qPrintable(extension));
QString filter; QString filter;
if (!extension.isEmpty()) { if (!extension.isEmpty()) {
extension = extension.toUpper(); extension = extension.toUpper();
filter = tr("%1 Files", "%1 is a file extension (e.g. PDF)").arg(extension)+" (*."+extension+")"; filter = tr("%1 Files", "%1 is a file extension (e.g. PDF)").arg(extension) + " (*." + extension + ")";
} }
m_contentPath = QFileDialog::getOpenFileName(this, tr("Please provide the location of %1", "%1 is a file name").arg(file_name), default_dir, filter); m_contentPath = QFileDialog::getOpenFileName(this, tr("Please provide the location of %1", "%1 is a file name").arg(file_name), default_dir, filter);
if (m_contentPath.isEmpty() || !QFile(m_contentPath).exists()) { if (m_contentPath.isEmpty() || !QFile(m_contentPath).exists()) {
m_contentPath = QString::null; m_contentPath = QString::null;
ui->importBtn->setEnabled(false); ui->importBtn->setEnabled(false);
ui->checkSkipCheck->setEnabled(false); ui->checkSkipCheck->setEnabled(false);
return; return;
} }
// Update display // Update display
ui->lineContent->setText(fsutils::toNativePath(m_contentPath)); ui->lineContent->setText(fsutils::toNativePath(m_contentPath));
// Check file size // Check file size
const qint64 file_size = QFile(m_contentPath).size(); const qint64 file_size = QFile(m_contentPath).size();
if (t->file_at(0).size == file_size) { if (t->file_at(0).size == file_size) {
qDebug("The file size matches, allowing fast seeding..."); qDebug("The file size matches, allowing fast seeding...");
ui->checkSkipCheck->setEnabled(true); ui->checkSkipCheck->setEnabled(true);
} else { }
qDebug("The file size does not match, forbidding fast seeding..."); else {
ui->checkSkipCheck->setChecked(false); qDebug("The file size does not match, forbidding fast seeding...");
ui->checkSkipCheck->setEnabled(false); ui->checkSkipCheck->setChecked(false);
ui->checkSkipCheck->setEnabled(false);
}
// Handle file renaming
QStringList parts = m_contentPath.split("/");
QString new_file_name = parts.takeLast();
if (new_file_name != file_name) {
qDebug("The file has been renamed");
QStringList new_parts = m_filesPath.first().split("/");
new_parts.replace(new_parts.count() - 1, new_file_name);
m_filesPath.replace(0, new_parts.join("/"));
}
m_contentPath = parts.join("/");
} }
// Handle file renaming else {
QStringList parts = m_contentPath.split("/"); // multiple files torrent
QString new_file_name = parts.takeLast(); m_contentPath = QFileDialog::getExistingDirectory(this, tr("Please point to the location of the torrent: %1").arg(misc::toQStringU(t->name())),
if (new_file_name != file_name) { default_dir);
qDebug("The file has been renamed"); if (m_contentPath.isEmpty() || !QDir(m_contentPath).exists()) {
QStringList new_parts = m_filesPath.first().split("/"); m_contentPath = QString::null;
new_parts.replace(new_parts.count()-1, new_file_name); ui->importBtn->setEnabled(false);
m_filesPath.replace(0, new_parts.join("/")); ui->checkSkipCheck->setEnabled(false);
return;
}
// Update the display
ui->lineContent->setText(fsutils::toNativePath(m_contentPath));
bool size_mismatch = false;
QDir content_dir(m_contentPath);
content_dir.cdUp();
// Check file sizes
for (int i = 0; i<t->num_files(); ++i) {
const QString rel_path = misc::toQStringU(t->file_at(i).path);
if (QFile(fsutils::expandPath(content_dir.absoluteFilePath(rel_path))).size() != t->file_at(i).size) {
qDebug("%s is %lld",
qPrintable(fsutils::expandPath(content_dir.absoluteFilePath(rel_path))), (long long int) QFile(fsutils::expandPath(content_dir.absoluteFilePath(rel_path))).size());
qDebug("%s is %lld",
qPrintable(rel_path), (long long int)t->file_at(i).size);
size_mismatch = true;
break;
}
}
if (size_mismatch) {
qDebug("The file size does not match, forbidding fast seeding...");
ui->checkSkipCheck->setChecked(false);
ui->checkSkipCheck->setEnabled(false);
}
else {
qDebug("The file size matches, allowing fast seeding...");
ui->checkSkipCheck->setEnabled(true);
}
} }
m_contentPath = parts.join("/"); // Enable the import button
} else { ui->importBtn->setEnabled(true);
// multiple files torrent
m_contentPath = QFileDialog::getExistingDirectory(this, tr("Please point to the location of the torrent: %1").arg(misc::toQStringU(t->name())),
default_dir);
if (m_contentPath.isEmpty() || !QDir(m_contentPath).exists()) {
m_contentPath = QString::null;
ui->importBtn->setEnabled(false);
ui->checkSkipCheck->setEnabled(false);
return;
}
// Update the display
ui->lineContent->setText(fsutils::toNativePath(m_contentPath));
bool size_mismatch = false;
QDir content_dir(m_contentPath);
content_dir.cdUp();
// Check file sizes
for (int i=0; i<t->num_files(); ++i) {
const QString rel_path = misc::toQStringU(t->file_at(i).path);
if (QFile(fsutils::expandPath(content_dir.absoluteFilePath(rel_path))).size() != t->file_at(i).size) {
qDebug("%s is %lld",
qPrintable(fsutils::expandPath(content_dir.absoluteFilePath(rel_path))), (long long int) QFile(fsutils::expandPath(content_dir.absoluteFilePath(rel_path))).size());
qDebug("%s is %lld",
qPrintable(rel_path), (long long int)t->file_at(i).size);
size_mismatch = true;
break;
}
}
if (size_mismatch) {
qDebug("The file size does not match, forbidding fast seeding...");
ui->checkSkipCheck->setChecked(false);
ui->checkSkipCheck->setEnabled(false);
} else {
qDebug("The file size matches, allowing fast seeding...");
ui->checkSkipCheck->setEnabled(true);
}
}
// Enable the import button
ui->importBtn->setEnabled(true);
} }
void TorrentImportDlg::on_importBtn_clicked() void TorrentImportDlg::on_importBtn_clicked()
{ {
saveSettings(); saveSettings();
// Has to be accept() and not close() // Has to be accept() and not close()
// or the torrent won't be imported // or the torrent won't be imported
accept(); accept();
} }
QString TorrentImportDlg::getTorrentPath() const QString TorrentImportDlg::getTorrentPath() const
{ {
return m_torrentPath; return m_torrentPath;
} }
QString TorrentImportDlg::getContentPath() const QString TorrentImportDlg::getContentPath() const
{ {
return m_contentPath; return m_contentPath;
} }
void TorrentImportDlg::importTorrent() void TorrentImportDlg::importTorrent()
{ {
qDebug() << Q_FUNC_INFO << "ENTER"; qDebug() << Q_FUNC_INFO << "ENTER";
TorrentImportDlg dlg; TorrentImportDlg dlg;
if (dlg.exec()) { if (dlg.exec()) {
qDebug() << "Loading the torrent file..."; qDebug() << "Loading the torrent file...";
boost::intrusive_ptr<libtorrent::torrent_info> t = dlg.torrent(); boost::intrusive_ptr<libtorrent::torrent_info> t = dlg.torrent();
if (!t->is_valid()) if (!t->is_valid())
return; return;
QString torrent_path = dlg.getTorrentPath(); QString torrent_path = dlg.getTorrentPath();
QString content_path = dlg.getContentPath(); QString content_path = dlg.getContentPath();
if (torrent_path.isEmpty() || content_path.isEmpty() || !QFile(torrent_path).exists()) { if (torrent_path.isEmpty() || content_path.isEmpty() || !QFile(torrent_path).exists()) {
qWarning() << "Incorrect input, aborting." << torrent_path << content_path; qWarning() << "Incorrect input, aborting." << torrent_path << content_path;
return; return;
}
const QString hash = misc::toQString(t->info_hash());
qDebug() << "Torrent hash is" << hash;
TorrentTempData::setSavePath(hash, content_path);
TorrentTempData::setSeedingMode(hash, dlg.skipFileChecking());
qDebug("Adding the torrent to the session...");
QBtSession::instance()->addTorrent(torrent_path, false, QString(), false, true);
// Remember the last opened folder
Preferences* const pref = Preferences::instance();
pref->setMainLastDir(fsutils::fromNativePath(torrent_path));
pref->setTorImportLastContentDir(fsutils::fromNativePath(content_path));
return;
} }
const QString hash = misc::toQString(t->info_hash()); qDebug() << Q_FUNC_INFO << "EXIT";
qDebug() << "Torrent hash is" << hash;
TorrentTempData::setSavePath(hash, content_path);
TorrentTempData::setSeedingMode(hash, dlg.skipFileChecking());
qDebug("Adding the torrent to the session...");
QBtSession::instance()->addTorrent(torrent_path, false, QString(), false, true);
// Remember the last opened folder
Preferences* const pref = Preferences::instance();
pref->setMainLastDir(fsutils::fromNativePath(torrent_path));
pref->setTorImportLastContentDir(fsutils::fromNativePath(content_path));
return; return;
}
qDebug() << Q_FUNC_INFO << "EXIT";
return;
} }
void TorrentImportDlg::loadTorrent(const QString &torrent_path) void TorrentImportDlg::loadTorrent(const QString &torrent_path)
@ -240,42 +242,41 @@ void TorrentImportDlg::loadTorrent(const QString &torrent_path)
void TorrentImportDlg::initializeFilesPath() void TorrentImportDlg::initializeFilesPath()
{ {
m_filesPath.clear(); m_filesPath.clear();
// Loads files path in the torrent // Loads files path in the torrent
for (int i=0; i<t->num_files(); ++i) { for (int i = 0; i<t->num_files(); ++i)
m_filesPath << fsutils::fromNativePath(misc::toQStringU(t->file_at(i).path)); m_filesPath << fsutils::fromNativePath(misc::toQStringU(t->file_at(i).path));
}
} }
bool TorrentImportDlg::fileRenamed() const bool TorrentImportDlg::fileRenamed() const
{ {
return m_fileRenamed; return m_fileRenamed;
} }
boost::intrusive_ptr<libtorrent::torrent_info> TorrentImportDlg::torrent() const boost::intrusive_ptr<libtorrent::torrent_info> TorrentImportDlg::torrent() const
{ {
return t; return t;
} }
bool TorrentImportDlg::skipFileChecking() const bool TorrentImportDlg::skipFileChecking() const
{ {
return ui->checkSkipCheck->isChecked(); return ui->checkSkipCheck->isChecked();
} }
void TorrentImportDlg::loadSettings() void TorrentImportDlg::loadSettings()
{ {
restoreGeometry(Preferences::instance()->getTorImportGeometry()); restoreGeometry(Preferences::instance()->getTorImportGeometry());
} }
void TorrentImportDlg::saveSettings() void TorrentImportDlg::saveSettings()
{ {
Preferences::instance()->setTorImportGeometry(saveGeometry()); Preferences::instance()->setTorImportGeometry(saveGeometry());
} }
void TorrentImportDlg::closeEvent(QCloseEvent *event) void TorrentImportDlg::closeEvent(QCloseEvent *event)
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
saveSettings(); saveSettings();
QDialog::closeEvent(event); QDialog::closeEvent(event);
} }

50
src/torrentimportdlg.h

@ -39,51 +39,51 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
class TorrentImportDlg; class TorrentImportDlg;
} }
QT_END_NAMESPACE QT_END_NAMESPACE
class QBtSession; class QBtSession;
class TorrentImportDlg : public QDialog class TorrentImportDlg: public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit TorrentImportDlg(QWidget *parent = 0); explicit TorrentImportDlg(QWidget *parent = 0);
~TorrentImportDlg(); ~TorrentImportDlg();
static void importTorrent(); static void importTorrent();
QString getTorrentPath() const; QString getTorrentPath() const;
QString getContentPath() const; QString getContentPath() const;
bool fileRenamed() const; bool fileRenamed() const;
boost::intrusive_ptr<libtorrent::torrent_info> torrent() const; boost::intrusive_ptr<libtorrent::torrent_info> torrent() const;
bool skipFileChecking() const; bool skipFileChecking() const;
protected slots: protected slots:
void loadTorrent(const QString &torrent_path); void loadTorrent(const QString &torrent_path);
void initializeFilesPath(); void initializeFilesPath();
private slots: private slots:
void on_browseTorrentBtn_clicked(); void on_browseTorrentBtn_clicked();
void on_browseContentBtn_clicked(); void on_browseContentBtn_clicked();
void on_importBtn_clicked(); void on_importBtn_clicked();
protected: protected:
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
private: private:
void loadSettings(); void loadSettings();
void saveSettings(); void saveSettings();
private: private:
Ui::TorrentImportDlg *ui; Ui::TorrentImportDlg *ui;
boost::intrusive_ptr<libtorrent::torrent_info> t; boost::intrusive_ptr<libtorrent::torrent_info> t;
QStringList m_filesPath; QStringList m_filesPath;
QString m_contentPath; QString m_contentPath;
QString m_torrentPath; QString m_torrentPath;
bool m_fileRenamed; bool m_fileRenamed;
}; };
#endif // TORRENTIMPORTDLG_H #endif // TORRENTIMPORTDLG_H

Loading…
Cancel
Save