diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index d2ae12887..436398d1e 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -147,9 +147,10 @@ void bittorrent::deleteBigRatios() { void bittorrent::setDownloadLimit(QString hash, long val) { QTorrentHandle h = getTorrentHandle(hash); - if(h.is_valid()) + if(h.is_valid()) { h.set_download_limit(val); - saveTorrentSpeedLimits(hash); + TorrentPersistentData::saveSpeedLimits(h); + } } bool bittorrent::isQueueingEnabled() const { @@ -172,9 +173,10 @@ void bittorrent::decreaseDlTorrentPriority(QString hash) { void bittorrent::setUploadLimit(QString hash, long val) { qDebug("Set upload limit rate to %ld", val); QTorrentHandle h = getTorrentHandle(hash); - if(h.is_valid()) + if(h.is_valid()) { h.set_upload_limit(val); - saveTorrentSpeedLimits(hash); + TorrentPersistentData::saveSpeedLimits(h); + } } void bittorrent::handleDownloadFailure(QString url, QString reason) { @@ -821,36 +823,11 @@ bool bittorrent::enableDHT(bool b) { return true; } -void bittorrent::saveTorrentSpeedLimits(QString hash) { - qDebug("Saving speedLimits file for %s", hash.toLocal8Bit().data()); - QTorrentHandle h = getTorrentHandle(hash); - int download_limit = h.download_limit(); - int upload_limit = h.upload_limit(); - QFile speeds_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".speedLimits"); - if(!speeds_file.open(QIODevice::WriteOnly | QIODevice::Text)) { - qDebug("* Error: Couldn't open speed limits file for torrent: %s", hash.toLocal8Bit().data()); - return; - } - speeds_file.write(misc::toQByteArray(download_limit)+QByteArray(" ")+misc::toQByteArray(upload_limit)); - speeds_file.close(); -} - void bittorrent::loadTorrentSpeedLimits(QString hash) { - // qDebug("Loading speedLimits file for %s", hash.toLocal8Bit().data()); QTorrentHandle h = getTorrentHandle(hash); - QFile speeds_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".speedLimits"); - if(!speeds_file.open(QIODevice::ReadOnly | QIODevice::Text)) { - return; - } - QByteArray speed_limits = speeds_file.readAll(); - speeds_file.close(); - QList speeds = speed_limits.split(' '); - if(speeds.size() != 2) { - std::cerr << "Invalid .speedLimits file for " << hash.toStdString() << '\n'; - return; - } - h.set_download_limit(speeds.at(0).toInt()); - h.set_upload_limit(speeds.at(1).toInt()); + qDebug("Loading speedLimits file for %s", hash.toLocal8Bit().data()); + h.set_download_limit(TorrentPersistentData::getDownloadLimit(hash)); + h.set_upload_limit(TorrentPersistentData::getUploadLimit(hash)); } // Read pieces priorities from hard disk @@ -1674,6 +1651,22 @@ void bittorrent::applyFormerAttributeFiles(QTorrentHandle h) { } urlseeds_file.remove(); } + // Load speed limits + QFile speeds_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+h.hash()+".speedLimits"); + if(speeds_file.exists()) { + if(speeds_file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QByteArray speed_limits = speeds_file.readAll(); + speeds_file.close(); + QList speeds = speed_limits.split(' '); + if(speeds.size() != 2) { + std::cerr << "Invalid .speedLimits file for " << h.hash().toStdString() << '\n'; + return; + } + h.set_download_limit(speeds.at(0).toInt()); + h.set_upload_limit(speeds.at(1).toInt()); + } + speeds_file.remove(); + } } // Import torrents from v1.4.0 or earlier diff --git a/src/bittorrent.h b/src/bittorrent.h index da5ee6a44..75648ea8b 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -136,7 +136,6 @@ class bittorrent : public QObject { void enableIPFilter(QString filter); void disableIPFilter(); void setQueueingEnabled(bool enable); - void saveTorrentSpeedLimits(QString hash); void loadTorrentSpeedLimits(QString hash); void handleDownloadFailure(QString url, QString reason); void loadWebSeeds(QString fileHash); diff --git a/src/torrentPersistentData.h b/src/torrentPersistentData.h index d76cae16a..4a8622243 100644 --- a/src/torrentPersistentData.h +++ b/src/torrentPersistentData.h @@ -183,7 +183,11 @@ public: } data["url_seeds"] = url_seeds; } + // Sequential download data["sequential"] = h.is_sequential_download(); + // Speed limits + data["up_speed"] = h.upload_limit(); + data["dl_speed"] = h.download_limit(); // Save data all_data[h.hash()] = data; settings.setValue("torrents", all_data); @@ -236,6 +240,17 @@ public: qDebug("TorrentPersistentData: Saving save_path: %s, hash: %s", save_path.toLocal8Bit().data(), hash.toLocal8Bit().data()); } + static void saveSpeedLimits(QTorrentHandle h) { + Q_ASSERT(!hash.isEmpty()); + QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + QHash all_data = settings.value("torrents", QHash()).toHash(); + QHash data = all_data[h.hash()].toHash(); + data["up_speed"] = h.upload_limit(); + data["dl_speed"] = h.download_limit(); + all_data[h.hash()] = data; + settings.setValue("torrents", all_data); + } + static void saveUrlSeeds(QTorrentHandle h) { QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); QHash all_data = settings.value("torrents", QHash()).toHash(); @@ -291,6 +306,20 @@ public: return data["files_priority"].toList(); } + static int getUploadLimit(QString hash) { + QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + QHash all_data = settings.value("torrents", QHash()).toHash(); + QHash data = all_data[hash].toHash(); + return data.value("up_speed", -1).toInt(); + } + + static int getDownloadLimit(QString hash) { + QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + QHash all_data = settings.value("torrents", QHash()).toHash(); + QHash data = all_data[hash].toHash(); + return data.value("dl_speed", -1).toInt(); + } + static QString getSavePath(QString hash) { QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); QHash all_data = settings.value("torrents", QHash()).toHash();