Make 'TorrentPersistentData' similar to 'Preferences' class.

This commit is contained in:
sledgehammer999 2015-01-27 17:28:21 +02:00
parent 70985480d6
commit eeee24a0b3
11 changed files with 235 additions and 223 deletions

View file

@ -227,7 +227,7 @@ void QBtSession::processBigRatios() {
if (h.is_seed()) {
const QString hash = h.hash();
const qreal ratio = getRealRatio(h.status(torrent_handle::query_accurate_download_counters));
qreal ratio_limit = TorrentPersistentData::instance().getRatioLimit(hash);
qreal ratio_limit = TorrentPersistentData::instance()->getRatioLimit(hash);
if (ratio_limit == TorrentPersistentData::USE_GLOBAL_RATIO)
ratio_limit = global_ratio_limit;
if (ratio_limit == TorrentPersistentData::NO_RATIO_LIMIT)
@ -830,7 +830,7 @@ void QBtSession::deleteTorrent(const QString &hash, bool delete_local_files) {
foreach (const QString &file, files) {
fsutils::forceRemove(torrentBackup.absoluteFilePath(file));
}
TorrentPersistentData::instance().deletePersistentData(hash);
TorrentPersistentData::instance()->deletePersistentData(hash);
TorrentTempData::deleteTempData(hash);
HiddenData::deleteData(hash);
// Remove tracker errors
@ -981,7 +981,7 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool f
}
if (savePath.isEmpty())
savePath = getSavePath(hash, false);
if (!defaultTempPath.isEmpty() && !TorrentPersistentData::instance().isSeed(hash)) {
if (!defaultTempPath.isEmpty() && !TorrentPersistentData::instance()->isSeed(hash)) {
qDebug("addMagnetURI: Temp folder is enabled.");
QString torrent_tmp_path = defaultTempPath;
p.save_path = fsutils::toNativePath(torrent_tmp_path).toUtf8().constData();
@ -1175,7 +1175,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
} else {
savePath = getSavePath(hash, fromScanDir, path, imported);
}
if (!imported && !defaultTempPath.isEmpty() && !TorrentPersistentData::instance().isSeed(hash)) {
if (!imported && !defaultTempPath.isEmpty() && !TorrentPersistentData::instance()->isSeed(hash)) {
qDebug("addTorrent::Temp folder is enabled.");
QString torrent_tmp_path = defaultTempPath;
p.save_path = fsutils::toNativePath(torrent_tmp_path).toUtf8().constData();
@ -1340,9 +1340,9 @@ void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool m
// Save persistent data for new torrent
qDebug("Saving torrent persistant data");
if (defaultTempPath.isEmpty())
TorrentPersistentData::instance().saveTorrentPersistentData(h, QString::null, magnet);
TorrentPersistentData::instance()->saveTorrentPersistentData(h, QString::null, magnet);
else
TorrentPersistentData::instance().saveTorrentPersistentData(h, fsutils::fromNativePath(savePath), magnet);
TorrentPersistentData::instance()->saveTorrentPersistentData(h, fsutils::fromNativePath(savePath), magnet);
}
void QBtSession::mergeTorrents(QTorrentHandle& h_ex, const QString& magnet_uri)
@ -1661,7 +1661,7 @@ void QBtSession::saveFastResumeData() {
continue;
try {
if (isQueueingEnabled())
TorrentPersistentData::instance().savePriority(h);
TorrentPersistentData::instance()->savePriority(h);
if (!h.has_metadata())
continue;
// Actually with should save fast resume data for paused files too
@ -1850,7 +1850,7 @@ void QBtSession::appendqBextensionToTorrent(const QTorrentHandle &h, bool append
void QBtSession::changeLabelInTorrentSavePath(const QTorrentHandle &h, QString old_label, QString new_label) {
if (!h.is_valid()) return;
if (!appendLabelToSavePath) return;
QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(h.hash()));
QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::instance()->getSavePath(h.hash()));
if (!old_save_path.startsWith(defaultSavePath)) return;
QString new_save_path = fsutils::updateLabelInSavePath(defaultSavePath, old_save_path, old_label, new_label);
if (new_save_path != old_save_path) {
@ -1863,10 +1863,11 @@ void QBtSession::changeLabelInTorrentSavePath(const QTorrentHandle &h, QString o
void QBtSession::appendLabelToTorrentSavePath(const QTorrentHandle& h) {
if (!h.is_valid()) return;
const QString label = TorrentPersistentData::instance().getLabel(h.hash());
const TorrentPersistentData* const TorPersistent = TorrentPersistentData::instance();
const QString label = TorPersistent->getLabel(h.hash());
if (label.isEmpty()) return;
// Current save path
QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(h.hash()));
QString old_save_path = fsutils::fromNativePath(TorPersistent->getSavePath(h.hash()));
QString new_save_path = fsutils::updateLabelInSavePath(defaultSavePath, old_save_path, "", label);
if (old_save_path != new_save_path) {
// Move storage
@ -1992,20 +1993,20 @@ void QBtSession::setMaxRatioPerTorrent(const QString &hash, qreal ratio)
ratio = MAX_RATIO;
qDebug("* Set individual max ratio for torrent %s to %.1f.",
qPrintable(hash), ratio);
TorrentPersistentData::instance().setRatioLimit(hash, ratio);
TorrentPersistentData::instance()->setRatioLimit(hash, ratio);
updateRatioTimer();
}
void QBtSession::removeRatioPerTorrent(const QString &hash)
{
qDebug("* Remove individual max ratio for torrent %s.", qPrintable(hash));
TorrentPersistentData::instance().setRatioLimit(hash, TorrentPersistentData::USE_GLOBAL_RATIO);
TorrentPersistentData::instance()->setRatioLimit(hash, TorrentPersistentData::USE_GLOBAL_RATIO);
updateRatioTimer();
}
qreal QBtSession::getMaxRatioPerTorrent(const QString &hash, bool *usesGlobalRatio) const
{
qreal ratio_limit = TorrentPersistentData::instance().getRatioLimit(hash);
qreal ratio_limit = TorrentPersistentData::instance()->getRatioLimit(hash);
if (ratio_limit == TorrentPersistentData::USE_GLOBAL_RATIO) {
ratio_limit = global_ratio_limit;
if (usesGlobalRatio)
@ -2019,7 +2020,7 @@ qreal QBtSession::getMaxRatioPerTorrent(const QString &hash, bool *usesGlobalRat
void QBtSession::updateRatioTimer()
{
if (global_ratio_limit == -1 && !TorrentPersistentData::instance().hasPerTorrentRatioLimit()) {
if (global_ratio_limit == -1 && !TorrentPersistentData::instance()->hasPerTorrentRatioLimit()) {
if (BigRatioTimer->isActive())
BigRatioTimer->stop();
} else if (!BigRatioTimer->isActive()) {
@ -2099,7 +2100,7 @@ void QBtSession::sendNotificationEmail(const QTorrentHandle &h) {
// Prepare mail content
QString content = tr("Torrent name: %1").arg(h.name()) + "\n";
content += tr("Torrent size: %1").arg(misc::friendlyUnit(status.total_wanted)) + "\n";
content += tr("Save path: %1").arg(TorrentPersistentData::instance().getSavePath(h.hash())) + "\n\n";
content += tr("Save path: %1").arg(TorrentPersistentData::instance()->getSavePath(h.hash())) + "\n\n";
content += tr("The torrent was downloaded in %1.", "The torrent was downloaded in 1 hour and 20 seconds").arg(misc::userFriendlyDuration(status.active_time)) + "\n\n\n";
content += tr("Thank you for using qBittorrent.") + "\n";
// Send the notification email
@ -2213,7 +2214,8 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert*
if (appendqBExtension)
appendqBextensionToTorrent(h, false);
const bool was_already_seeded = TorrentPersistentData::instance().isSeed(hash);
TorrentPersistentData* const TorPersistent = TorrentPersistentData::instance();
const bool was_already_seeded = TorPersistent->isSeed(hash);
qDebug("Was already seeded: %d", was_already_seeded);
if (!was_already_seeded) {
h.save_resume_data();
@ -2256,7 +2258,7 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert*
}
// Remember finished state
qDebug("Saving seed status");
TorrentPersistentData::instance().saveSeedStatus(h);
TorPersistent->saveSeedStatus(h);
// Recheck if the user asked to
Preferences* const pref = Preferences::instance();
if (pref->recheckTorrentsOnCompletion()) {
@ -2418,7 +2420,7 @@ void QBtSession::handleStorageMovedAlert(libtorrent::storage_moved_alert* p) {
}
if (defaultTempPath.isEmpty() || !new_save_path.startsWith(defaultTempPath)) {
qDebug("Storage has been moved, updating save path to %s", qPrintable(new_save_path));
TorrentPersistentData::instance().saveSavePath(h.hash(), new_save_path);
TorrentPersistentData::instance()->saveSavePath(h.hash(), new_save_path);
}
emit savePathChanged(h);
//h.force_recheck();
@ -2657,11 +2659,11 @@ void QBtSession::handleFastResumeRejectedAlert(libtorrent::fastresume_rejected_a
QTorrentHandle h(p->handle);
if (h.is_valid()) {
qDebug("/!\\ Fast resume failed for %s, reason: %s", qPrintable(h.name()), p->message().c_str());
if (p->error.value() == 134 && TorrentPersistentData::instance().isSeed(h.hash()) && h.has_missing_files()) {
if (p->error.value() == 134 && TorrentPersistentData::instance()->isSeed(h.hash()) && h.has_missing_files()) {
const QString hash = h.hash();
// Mismatching file size (files were probably moved
logger->addMessage(tr("File sizes mismatch for torrent %1, pausing it.").arg(h.name()));
TorrentPersistentData::instance().setErrorState(hash, true);
TorrentPersistentData::instance()->setErrorState(hash, true);
pauseTorrent(hash);
} else {
logger->addMessage(tr("Fast resume data was rejected for torrent %1, checking again...").arg(h.name()), Log::CRITICAL);
@ -2723,7 +2725,7 @@ void QBtSession::handleTorrentCheckedAlert(libtorrent::torrent_checked_alert* p)
const QString hash = h.hash();
qDebug("%s have just finished checking", qPrintable(hash));
// Save seed status
TorrentPersistentData::instance().saveSeedStatus(h);
TorrentPersistentData::instance()->saveSeedStatus(h);
// Move to temp directory if necessary
if (!h.is_seed() && !defaultTempPath.isEmpty()) {
// Check if directory is different
@ -2846,7 +2848,7 @@ QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString f
}
qDebug("getSavePath, got save_path from temp data: %s", qPrintable(savePath));
} else {
savePath = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(hash));
savePath = fsutils::fromNativePath(TorrentPersistentData::instance()->getSavePath(hash));
qDebug("SavePath got from persistant data is %s", qPrintable(savePath));
if (savePath.isEmpty()) {
if (fromScanDir && m_scanFolders->downloadInTorrentFolder(filePath)) {
@ -2856,7 +2858,7 @@ QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString f
}
}
if (!fromScanDir && appendLabelToSavePath) {
const QString label = TorrentPersistentData::instance().getLabel(hash);
const QString label = TorrentPersistentData::instance()->getLabel(hash);
if (!label.isEmpty()) {
qDebug("Torrent label is %s", qPrintable(label));
savePath = fsutils::updateLabelInSavePath(defaultSavePath, savePath, "", label);
@ -3011,7 +3013,8 @@ qreal QBtSession::getPayloadUploadRate() const {
void QBtSession::startUpTorrents() {
qDebug("Resuming unfinished torrents");
const QDir torrentBackup(fsutils::BTBackupLocation());
const QStringList known_torrents = TorrentPersistentData::instance().knownTorrents();
const TorrentPersistentData* const TorPersistent = TorrentPersistentData::instance();
const QStringList known_torrents = TorPersistent->knownTorrents();
// Safety measure because some people reported torrent loss since
// we switch the v1.5 way of resuming torrents on startup
@ -3032,7 +3035,7 @@ void QBtSession::startUpTorrents() {
if (isQueueingEnabled()) {
priority_queue<QPair<int, QString>, vector<QPair<int, QString> >, std::greater<QPair<int, QString> > > torrent_queue;
foreach (const QString &hash, known_torrents) {
const int prio = TorrentPersistentData::instance().getPriority(hash);
const int prio = TorPersistent->getPriority(hash);
torrent_queue.push(qMakePair(prio, hash));
}
qDebug("Priority_queue size: %ld", (long)torrent_queue.size());
@ -3041,8 +3044,8 @@ void QBtSession::startUpTorrents() {
const QString hash = torrent_queue.top().second;
torrent_queue.pop();
qDebug("Starting up torrent %s", qPrintable(hash));
if (TorrentPersistentData::instance().isMagnet(hash)) {
addMagnetUri(TorrentPersistentData::instance().getMagnetUri(hash), true);
if (TorPersistent->isMagnet(hash)) {
addMagnetUri(TorPersistent->getMagnetUri(hash), true);
} else {
addTorrent(torrentBackup.path()+"/"+hash+".torrent", false, QString(), true);
}
@ -3051,8 +3054,8 @@ void QBtSession::startUpTorrents() {
// Resume downloads
foreach (const QString &hash, known_torrents) {
qDebug("Starting up torrent %s", qPrintable(hash));
if (TorrentPersistentData::instance().isMagnet(hash))
addMagnetUri(TorrentPersistentData::instance().getMagnetUri(hash), true);
if (TorPersistent->isMagnet(hash))
addMagnetUri(TorPersistent->getMagnetUri(hash), true);
else
addTorrent(torrentBackup.path()+"/"+hash+".torrent", false, QString(), true);
}
@ -3106,7 +3109,8 @@ void QBtSession::handleIPFilterError()
}
void QBtSession::recoverPersistentData(const QString &hash, const std::vector<char> &buf) {
if (TorrentPersistentData::instance().isKnownTorrent(hash) || TorrentTempData::hasTempData(hash) || buf.empty())
TorrentPersistentData* const TorPersistent = TorrentPersistentData::instance();
if (TorPersistent->isKnownTorrent(hash) || TorrentTempData::hasTempData(hash) || buf.empty())
return;
libtorrent::lazy_entry fast;
@ -3123,20 +3127,21 @@ void QBtSession::recoverPersistentData(const QString &hash, const std::vector<ch
int priority = fast.dict_find_int_value("qBt-queuePosition");
bool seedStatus = fast.dict_find_int_value("qBt-seedStatus");
TorrentPersistentData::instance().saveSavePath(hash, savePath);
TorrentPersistentData::instance().setRatioLimit(hash, ratioLimit);
TorrentPersistentData::instance().setAddedDate(hash, addedDate);
TorrentPersistentData::instance().saveLabel(hash, label);
TorrentPersistentData::instance().savePriority(hash, priority);
TorrentPersistentData::instance().saveSeedStatus(hash, seedStatus);
TorPersistent->saveSavePath(hash, savePath);
TorPersistent->setRatioLimit(hash, ratioLimit);
TorPersistent->setAddedDate(hash, addedDate);
TorPersistent->saveLabel(hash, label);
TorPersistent->savePriority(hash, priority);
TorPersistent->saveSeedStatus(hash, seedStatus);
}
void QBtSession::backupPersistentData(const QString &hash, boost::shared_ptr<libtorrent::entry> data) {
(*data)["qBt-savePath"] = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(hash)).toUtf8().constData();
(*data)["qBt-ratioLimit"] = QString::number(TorrentPersistentData::instance().getRatioLimit(hash)).toUtf8().constData();
(*data)["qBt-label"] = TorrentPersistentData::instance().getLabel(hash).toUtf8().constData();
(*data)["qBt-queuePosition"] = TorrentPersistentData::instance().getPriority(hash);
(*data)["qBt-seedStatus"] = (int)TorrentPersistentData::instance().isSeed(hash);
const TorrentPersistentData* const TorPersistent = TorrentPersistentData::instance();
(*data)["qBt-savePath"] = fsutils::fromNativePath(TorPersistent->getSavePath(hash)).toUtf8().constData();
(*data)["qBt-ratioLimit"] = QString::number(TorPersistent->getRatioLimit(hash)).toUtf8().constData();
(*data)["qBt-label"] = TorPersistent->getLabel(hash).toUtf8().constData();
(*data)["qBt-queuePosition"] = TorPersistent->getPriority(hash);
(*data)["qBt-seedStatus"] = (int)TorPersistent->isSeed(hash);
}
void QBtSession::unhideMagnet(const QString &hash) {
@ -3191,7 +3196,7 @@ void QBtSession::unhideMagnet(const QString &hash) {
}
h.queue_position_bottom();
loadTorrentTempData(h, h.save_path(), !h.has_metadata()); //TempData are deleted by a call to TorrentPersistentData::instance().saveTorrentPersistentData()
loadTorrentTempData(h, h.save_path(), !h.has_metadata()); //TempData are deleted by a call to TorrentPersistentData::instance()->saveTorrentPersistentData()
if (!add_paused)
h.resume();
h.move_storage(save_path);