Browse Source

Fix several signals/slots bindings

Drop backward compatiblity functions for qBT <= 1.4.0
Clean up torrent loading code (Use priority queue instead of insertion sort)
adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
bea3c33a46
  1. 226
      src/bittorrent.cpp
  2. 5
      src/bittorrent.h
  3. 46
      src/misc.h
  4. 12
      src/transferlistwidget.cpp
  5. 10
      src/transferlistwidget.h

226
src/bittorrent.cpp

@ -61,6 +61,7 @@ @@ -61,6 +61,7 @@
#include <libtorrent/alert_types.hpp>
#include <libtorrent/torrent_info.hpp>
#include <boost/filesystem/exception.hpp>
#include <queue>
const int MAX_TRACKER_ERRORS = 2;
const float MAX_RATIO = 100.;
@ -1060,11 +1061,6 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr @@ -1060,11 +1061,6 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr
p.seed_mode=false;
}
#endif
// TODO: Remove in v1.6.0: For backward compatibility only
if(QFile::exists(misc::BTBackupLocation()+QDir::separator()+hash+".finished")) {
p.save_path = savePath.toLocal8Bit().constData();
QFile::remove(misc::BTBackupLocation()+QDir::separator()+hash+".finished");
}
p.ti = t;
// Preallocate all?
if(preAllocateAll)
@ -1965,7 +1961,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -1965,7 +1961,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
}*/
else if (tracker_error_alert* p = dynamic_cast<tracker_error_alert*>(a.get())) {
// Level: fatal
const QTorrentHandle &h(p->handle);
QTorrentHandle h(p->handle);
if(h.is_valid()){
// Authentication
if(p->status_code != 401) {
@ -2108,7 +2104,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -2108,7 +2104,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
if(TorrentTempData::hasTempData(hash)) {
savePath = TorrentTempData::getSavePath(hash);
if(savePath.isEmpty()) {
savePath = defaultSavePath;
savePath = defaultSavePath;
}
if(appendLabelToSavePath) {
qDebug("appendLabelToSavePath is true");
@ -2241,16 +2237,9 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -2241,16 +2237,9 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
// backup directory
void Bittorrent::startUpTorrents() {
qDebug("Resuming unfinished torrents");
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
const QDir &torrentBackup(misc::BTBackupLocation());
const QStringList &known_torrents = TorrentPersistentData::knownTorrents();
if(known_torrents.empty() && !settings.value("v1_4_x_torrent_imported", false).toBool()) {
qDebug("No known torrent, importing old torrents");
importOldTorrents();
return;
}
// Safety measure because some people reported torrent loss since
// we switch the v1.5 way of resuming torrents on startup
QStringList filters;
@ -2267,7 +2256,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -2267,7 +2256,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
qDebug("Starting up torrents");
if(isQueueingEnabled()) {
QList<QPair<int, QString> > hashes;
priority_queue<QPair<int, QString>, vector<QPair<int, QString> >, std::greater<QPair<int, QString> > > torrent_queue;
foreach(const QString &hash, known_torrents) {
QString filePath;
if(TorrentPersistentData::isMagnet(hash)) {
@ -2276,12 +2265,15 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -2276,12 +2265,15 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
filePath = torrentBackup.path()+QDir::separator()+hash+".torrent";
}
const int prio = TorrentPersistentData::getPriority(hash);
misc::insertSort2<QString>(hashes, qMakePair(prio, hash));
torrent_queue.push(qMakePair(prio, hash));
}
// Resume downloads
QPair<int, QString> couple;
foreach(couple, hashes) {
const QString &hash = couple.second;
int prio = -1;
while(!torrent_queue.empty()) {
int new_prio = torrent_queue.top().first;
Q_ASSERT(new_prio >= prio);
const QString &hash = torrent_queue.top().second;
torrent_queue.pop();
qDebug("Starting up torrent %s", qPrintable(hash));
if(TorrentPersistentData::isMagnet(hash)) {
addMagnetUri(TorrentPersistentData::getMagnetUri(hash), true);
@ -2301,199 +2293,3 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -2301,199 +2293,3 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
}
qDebug("Unfinished torrents resumed");
}
// Import torrents temp data from v1.4.0 or earlier: save_path, filtered pieces
// TODO: Remove in qBittorrent v2.2.0
void Bittorrent::importOldTempData(QString torrent_path) {
// Create torrent hash
try {
boost::intrusive_ptr<torrent_info> t = new torrent_info(torrent_path.toLocal8Bit().data());
const QString &hash = misc::toQString(t->info_hash());
// Load save path
QFile savepath_file(misc::BTBackupLocation()+QDir::separator()+hash+".savepath");
QByteArray line;
QString savePath;
if(savepath_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
line = savepath_file.readAll();
savepath_file.close();
qDebug(" -> Save path: %s", line.constData());
savePath = QString::fromUtf8(line.data());
qDebug("Imported the following save path: %s", qPrintable(savePath));
TorrentTempData::setSavePath(hash, savePath);
}
// Load pieces priority
QFile pieces_file(misc::BTBackupLocation()+QDir::separator()+hash+".priorities");
if(pieces_file.exists()){
// Read saved file
if(pieces_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QByteArray &pieces_priorities = pieces_file.readAll();
pieces_file.close();
const QList<QByteArray> &pieces_priorities_list = pieces_priorities.split('\n');
std::vector<int> pp;
for(int i=0; i<t->num_files(); ++i) {
int priority = pieces_priorities_list.at(i).toInt();
if( priority < 0 || priority > 7) {
priority = 1;
}
//qDebug("Setting piece piority to %d", priority);
pp.push_back(priority);
}
TorrentTempData::setFilesPriority(hash, pp);
qDebug("Successfuly imported pieces_priority");
}
}
// Load sequential
if(QFile::exists(misc::BTBackupLocation()+QDir::separator()+hash+".incremental")) {
qDebug("Imported torrent was sequential");
TorrentTempData::setSequential(hash, true);
}
} catch(std::exception&) {
}
}
// Trackers, web seeds, speed limits
// TODO: Remove in qBittorrent v2.2.0
void Bittorrent::applyFormerAttributeFiles(QTorrentHandle h) {
// Load trackers
const QDir &torrentBackup(misc::BTBackupLocation());
QFile tracker_file(torrentBackup.path()+QDir::separator()+ h.hash() + ".trackers");
if(tracker_file.exists()) {
if(tracker_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QStringList &lines = QString::fromUtf8(tracker_file.readAll().data()).split("\n");
std::vector<announce_entry> trackers;
foreach(const QString &line, lines) {
const QStringList &parts = line.split("|");
if(parts.size() != 2) continue;
announce_entry t(parts[0].toStdString());
t.tier = parts[1].toInt();
trackers.push_back(t);
}
if(!trackers.empty()) {
h.replace_trackers(trackers);
h.force_reannounce();
}
}
}
// Load Web seeds
QFile urlseeds_file(misc::BTBackupLocation()+QDir::separator()+h.hash()+".urlseeds");
if(urlseeds_file.exists()) {
if(urlseeds_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QByteArray &urlseeds_lines = urlseeds_file.readAll();
urlseeds_file.close();
const QList<QByteArray> &url_seeds = urlseeds_lines.split('\n');
// First remove from the torrent the url seeds that were deleted
// in a previous session
QStringList seeds_to_delete;
const QStringList &existing_seeds = h.url_seeds();
foreach(const QString &existing_seed, existing_seeds) {
if(!url_seeds.contains(existing_seed.toLocal8Bit())) {
seeds_to_delete << existing_seed;
}
}
foreach(const QString &existing_seed, seeds_to_delete) {
h.remove_url_seed(existing_seed);
}
// Add the ones that were added in a previous session
foreach(const QByteArray &url_seed, url_seeds) {
if(!url_seed.isEmpty()) {
// XXX: Should we check if it is already in the list before adding it
// or is libtorrent clever enough to know
h.add_url_seed(url_seed);
}
}
}
}
// Load speed limits
QFile speeds_file(misc::BTBackupLocation()+QDir::separator()+h.hash()+".speedLimits");
if(speeds_file.exists()) {
if(speeds_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QByteArray &speed_limits = speeds_file.readAll();
speeds_file.close();
const QList<QByteArray> &speeds = speed_limits.split(' ');
if(speeds.size() != 2) {
std::cerr << "Invalid .speedLimits file for " << qPrintable(h.hash()) << '\n';
return;
}
h.set_download_limit(speeds.at(0).toInt());
h.set_upload_limit(speeds.at(1).toInt());
}
}
}
// Import torrents from v1.4.0 or earlier
// TODO: Remove in qBittorrent v2.2.0
void Bittorrent::importOldTorrents() {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
Q_ASSERT(!settings.value("v1_4_x_torrent_imported", false).toBool());
// Import old torrent
const QDir &torrentBackup(misc::BTBackupLocation());
QStringList filters;
filters << "*.torrent";
const QStringList &fileNames = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted);
if(isQueueingEnabled()) {
QList<QPair<int, QString> > filePaths;
foreach(const QString &fileName, fileNames) {
QString filePath = torrentBackup.path()+QDir::separator()+fileName;
int prio = 99999;
// Get priority
const QString &prioPath = filePath.replace(".torrent", ".prio");
if(QFile::exists(prioPath)) {
QFile prio_file(prioPath);
if(prio_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
bool ok = false;
prio = prio_file.readAll().toInt(&ok);
if(!ok)
prio = 99999;
prio_file.close();
}
}
misc::insertSort2<QString>(filePaths, qMakePair(prio, filePath));
}
// Resume downloads
QPair<int, QString> fileName;
foreach(fileName, filePaths) {
importOldTempData(fileName.second);
QTorrentHandle h = addTorrent(fileName.second, false, QString(), true);
// Sequential download
if(TorrentTempData::hasTempData(h.hash())) {
qDebug("addTorrent: Setting download as sequential (from tmp data)");
h.set_sequential_download(TorrentTempData::isSequential(h.hash()));
}
applyFormerAttributeFiles(h);
const QString &savePath = TorrentTempData::getSavePath(h.hash());
// Save persistent data for new torrent
TorrentPersistentData::saveTorrentPersistentData(h);
// Save save_path
if(!defaultTempPath.isEmpty() && !savePath.isNull()) {
qDebug("addTorrent: Saving save_path in persistent data: %s", qPrintable(savePath));
TorrentPersistentData::saveSavePath(h.hash(), savePath);
}
}
} else {
QStringList filePaths;
foreach(const QString &fileName, fileNames) {
filePaths.append(torrentBackup.path()+QDir::separator()+fileName);
}
// Resume downloads
foreach(const QString &fileName, filePaths) {
importOldTempData(fileName);
QTorrentHandle h = addTorrent(fileName, false, QString(), true);
// Sequential download
if(TorrentTempData::hasTempData(h.hash())) {
qDebug("addTorrent: Setting download as sequential (from tmp data)");
h.set_sequential_download(TorrentTempData::isSequential(h.hash()));
}
applyFormerAttributeFiles(h);
const QString &savePath = TorrentTempData::getSavePath(h.hash());
// Save persistent data for new torrent
TorrentPersistentData::saveTorrentPersistentData(h);
// Save save_path
if(!defaultTempPath.isEmpty() && !savePath.isNull()) {
qDebug("addTorrent: Saving save_path in persistent data: %s", qPrintable(savePath));
TorrentPersistentData::saveSavePath(h.hash(), savePath);
}
}
}
settings.setValue("v1_4_x_torrent_imported", true);
std::cout << "Successfully imported torrents from v1.4.x (or previous) instance" << std::endl;
}

5
src/bittorrent.h

@ -119,9 +119,6 @@ public: @@ -119,9 +119,6 @@ public:
public slots:
QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false);
QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false);
void importOldTorrents();
void applyFormerAttributeFiles(QTorrentHandle h);
void importOldTempData(QString torrent_path);
void loadSessionState();
void saveSessionState();
void downloadFromUrl(QString url);
@ -204,7 +201,7 @@ signals: @@ -204,7 +201,7 @@ signals:
void finishedTorrent(QTorrentHandle& h);
void fullDiskError(QTorrentHandle& h, QString msg);
void trackerError(QString hash, QString time, QString msg);
void trackerAuthenticationRequired(const QTorrentHandle& h);
void trackerAuthenticationRequired(QTorrentHandle& h);
void newDownloadedTorrent(QString path, QString url);
void updateFileSize(QString hash);
void downloadFromUrlFailure(QString url, QString reason);

46
src/misc.h

@ -423,52 +423,6 @@ public: @@ -423,52 +423,6 @@ public:
return false;
}
// Insertion sort, used instead of bubble sort because it is
// approx. 5 times faster.
template <class T> static void insertSort(QList<QPair<int, T> > &list, const QPair<int, T>& value, Qt::SortOrder sortOrder) {
int i = 0;
if(sortOrder == Qt::AscendingOrder) {
while(i < list.size() and value.second > list.at(i).second) {
++i;
}
}else{
while(i < list.size() and value.second < list.at(i).second) {
++i;
}
}
list.insert(i, value);
}
template <class T> static void insertSort2(QList<QPair<int, T> > &list, const QPair<int, T>& value, Qt::SortOrder sortOrder=Qt::AscendingOrder) {
int i = 0;
if(sortOrder == Qt::AscendingOrder) {
while(i < list.size() and value.first > list.at(i).first) {
++i;
}
}else{
while(i < list.size() and value.first < list.at(i).first) {
++i;
}
}
list.insert(i, value);
}
// Can't use template class for QString because >,< use unicode code for sorting
// which is not what a human would expect when sorting strings.
static void insertSortString(QList<QPair<int, QString> > &list, const QPair<int, QString> &value, Qt::SortOrder sortOrder) {
int i = 0;
if(sortOrder == Qt::AscendingOrder) {
while(i < list.size() and QString::localeAwareCompare(value.second, list.at(i).second) > 0) {
++i;
}
}else{
while(i < list.size() and QString::localeAwareCompare(value.second, list.at(i).second) < 0) {
++i;
}
}
list.insert(i, value);
}
static bool removeEmptyTree(QString path) {
QDir dir(path);
foreach(const QString &child, dir.entryList(QDir::AllDirs)) {

12
src/transferlistwidget.cpp

@ -156,7 +156,7 @@ TransferListWidget::~TransferListWidget() { @@ -156,7 +156,7 @@ TransferListWidget::~TransferListWidget() {
delete listDelegate;
}
void TransferListWidget::addTorrent(const QTorrentHandle& h) {
void TransferListWidget::addTorrent(QTorrentHandle& h) {
if(!h.is_valid()) return;
// Check that the torrent is not already there
if(getRowFromHash(h.hash()) >= 0) return;
@ -235,7 +235,7 @@ void TransferListWidget::deleteTorrent(int row, bool refresh_list) { @@ -235,7 +235,7 @@ void TransferListWidget::deleteTorrent(int row, bool refresh_list) {
}
// Wrapper slot for bittorrent signal
void TransferListWidget::pauseTorrent(const QTorrentHandle &h) {
void TransferListWidget::pauseTorrent(QTorrentHandle &h) {
pauseTorrent(getRowFromHash(h.hash()));
}
@ -264,7 +264,7 @@ int TransferListWidget::getNbTorrents() const { @@ -264,7 +264,7 @@ int TransferListWidget::getNbTorrents() const {
}
// Wrapper slot for bittorrent signal
void TransferListWidget::resumeTorrent(const QTorrentHandle &h) {
void TransferListWidget::resumeTorrent(QTorrentHandle &h) {
resumeTorrent(getRowFromHash(h.hash()));
}
@ -283,7 +283,7 @@ void TransferListWidget::resumeTorrent(int row, bool refresh_list) { @@ -283,7 +283,7 @@ void TransferListWidget::resumeTorrent(int row, bool refresh_list) {
refreshList();
}
void TransferListWidget::updateMetadata(const QTorrentHandle &h) {
void TransferListWidget::updateMetadata(QTorrentHandle &h) {
const QString &hash = h.hash();
const int row = getRowFromHash(hash);
if(row != -1) {
@ -440,7 +440,7 @@ int TransferListWidget::updateTorrent(int row) { @@ -440,7 +440,7 @@ int TransferListWidget::updateTorrent(int row) {
return s;
}
void TransferListWidget::setFinished(const QTorrentHandle &h) {
void TransferListWidget::setFinished(QTorrentHandle &h) {
const int row = getRowFromHash(h.hash());
try {
if(row >= 0) {
@ -482,7 +482,7 @@ void TransferListWidget::refreshList() { @@ -482,7 +482,7 @@ void TransferListWidget::refreshList() {
std::vector<torrent_handle> torrents = BTSession->getSession()->get_torrents();
std::vector<torrent_handle>::iterator itr;
for(itr = torrents.begin(); itr != torrents.end(); itr++) {
const QTorrentHandle &h(*itr);
QTorrentHandle h(*itr);
if(h.is_valid() && getRowFromHash(h.hash()) < 0) {
addTorrent(h);
}

10
src/transferlistwidget.h

@ -54,9 +54,9 @@ public: @@ -54,9 +54,9 @@ public:
public slots:
void refreshList();
void addTorrent(const QTorrentHandle& h);
void pauseTorrent(const QTorrentHandle &h);
void setFinished(const QTorrentHandle &h);
void addTorrent(QTorrentHandle& h);
void pauseTorrent(QTorrentHandle &h);
void setFinished(QTorrentHandle &h);
void setSelectionLabel(QString label);
void setRefreshInterval(int t);
void startSelectedTorrents();
@ -102,9 +102,9 @@ protected slots: @@ -102,9 +102,9 @@ protected slots:
bool loadHiddenColumns();
void saveHiddenColumns() const;
void displayListMenu(const QPoint&);
void updateMetadata(const QTorrentHandle &h);
void updateMetadata(QTorrentHandle &h);
void currentChanged(const QModelIndex& current, const QModelIndex&);
void resumeTorrent(const QTorrentHandle &h);
void resumeTorrent(QTorrentHandle &h);
#ifdef LIBTORRENT_0_15
void toggleSelectedTorrentsSuperSeeding() const;
#endif

Loading…
Cancel
Save