Follow project coding style. Issue #2192.

This commit is contained in:
sledgehammer999 2015-01-27 15:30:49 +02:00
parent dcf5d4d0ee
commit 70985480d6
2 changed files with 447 additions and 375 deletions

View file

@ -46,67 +46,96 @@ QHash<QString, bool> HiddenData::data = QHash<QString, bool>();
unsigned int HiddenData::metadata_counter = 0;
TorrentPersistentData* TorrentPersistentData::m_instance = 0;
bool TorrentTempData::hasTempData(const QString &hash) {
TorrentTempData::TorrentData::TorrentData()
: sequential(false)
, seed(false)
, add_paused(Preferences::instance()->addTorrentsInPause())
{
}
TorrentTempData::TorrentMoveState::TorrentMoveState(QString oldPath, QString newPath)
: oldPath(oldPath)
, newPath(newPath)
{
}
bool TorrentTempData::hasTempData(const QString &hash)
{
return data.contains(hash);
}
void TorrentTempData::deleteTempData(const QString &hash) {
void TorrentTempData::deleteTempData(const QString &hash)
{
data.remove(hash);
}
void TorrentTempData::setFilesPriority(const QString &hash, const std::vector<int> &pp) {
void TorrentTempData::setFilesPriority(const QString &hash, const std::vector<int> &pp)
{
data[hash].files_priority = pp;
}
void TorrentTempData::setFilesPath(const QString &hash, const QStringList &path_list) {
void TorrentTempData::setFilesPath(const QString &hash, const QStringList &path_list)
{
data[hash].path_list = path_list;
}
void TorrentTempData::setSavePath(const QString &hash, const QString &save_path) {
void TorrentTempData::setSavePath(const QString &hash, const QString &save_path)
{
data[hash].save_path = save_path;
}
void TorrentTempData::setLabel(const QString &hash, const QString &label) {
void TorrentTempData::setLabel(const QString &hash, const QString &label)
{
data[hash].label = label;
}
void TorrentTempData::setSequential(const QString &hash, const bool &sequential) {
void TorrentTempData::setSequential(const QString &hash, const bool &sequential)
{
data[hash].sequential = sequential;
}
bool TorrentTempData::isSequential(const QString &hash) {
bool TorrentTempData::isSequential(const QString &hash)
{
return data.value(hash).sequential;
}
void TorrentTempData::setSeedingMode(const QString &hash, const bool &seed) {
void TorrentTempData::setSeedingMode(const QString &hash, const bool &seed)
{
data[hash].seed = seed;
}
bool TorrentTempData::isSeedingMode(const QString &hash) {
bool TorrentTempData::isSeedingMode(const QString &hash)
{
return data.value(hash).seed;
}
QString TorrentTempData::getSavePath(const QString &hash) {
QString TorrentTempData::getSavePath(const QString &hash)
{
return data.value(hash).save_path;
}
QStringList TorrentTempData::getFilesPath(const QString &hash) {
QStringList TorrentTempData::getFilesPath(const QString &hash)
{
return data.value(hash).path_list;
}
QString TorrentTempData::getLabel(const QString &hash) {
QString TorrentTempData::getLabel(const QString &hash)
{
return data.value(hash).label;
}
void TorrentTempData::getFilesPriority(const QString &hash, std::vector<int> &fp) {
void TorrentTempData::getFilesPriority(const QString &hash, std::vector<int> &fp)
{
fp = data.value(hash).files_priority;
}
bool TorrentTempData::isMoveInProgress(const QString &hash) {
bool TorrentTempData::isMoveInProgress(const QString &hash)
{
return torrentMoveStates.find(hash) != torrentMoveStates.end();
}
void TorrentTempData::enqueueMove(const QString &hash, const QString &queuedPath) {
void TorrentTempData::enqueueMove(const QString &hash, const QString &queuedPath)
{
QHash<QString, TorrentMoveState>::iterator i = torrentMoveStates.find(hash);
if (i == torrentMoveStates.end()) {
Q_ASSERT(false);
@ -115,7 +144,8 @@ void TorrentTempData::enqueueMove(const QString &hash, const QString &queuedPath
i->queuedPath = queuedPath;
}
void TorrentTempData::startMove(const QString &hash, const QString &oldPath, const QString& newPath) {
void TorrentTempData::startMove(const QString &hash, const QString &oldPath, const QString& newPath)
{
QHash<QString, TorrentMoveState>::iterator i = torrentMoveStates.find(hash);
if (i != torrentMoveStates.end()) {
Q_ASSERT(false);
@ -125,7 +155,8 @@ void TorrentTempData::startMove(const QString &hash, const QString &oldPath, con
torrentMoveStates.insert(hash, TorrentMoveState(oldPath, newPath));
}
void TorrentTempData::finishMove(const QString &hash) {
void TorrentTempData::finishMove(const QString &hash)
{
QHash<QString, TorrentMoveState>::iterator i = torrentMoveStates.find(hash);
if (i == torrentMoveStates.end()) {
Q_ASSERT(false);
@ -134,7 +165,8 @@ void TorrentTempData::finishMove(const QString &hash) {
torrentMoveStates.erase(i);
}
QString TorrentTempData::getOldPath(const QString &hash) {
QString TorrentTempData::getOldPath(const QString &hash)
{
QHash<QString, TorrentMoveState>::iterator i = torrentMoveStates.find(hash);
if (i == torrentMoveStates.end()) {
Q_ASSERT(false);
@ -143,7 +175,8 @@ QString TorrentTempData::getOldPath(const QString &hash) {
return i->oldPath;
}
QString TorrentTempData::getNewPath(const QString &hash) {
QString TorrentTempData::getNewPath(const QString &hash)
{
QHash<QString, TorrentMoveState>::iterator i = torrentMoveStates.find(hash);
if (i == torrentMoveStates.end()) {
Q_ASSERT(false);
@ -152,7 +185,8 @@ QString TorrentTempData::getNewPath(const QString &hash) {
return i->newPath;
}
QString TorrentTempData::getQueuedPath(const QString &hash) {
QString TorrentTempData::getQueuedPath(const QString &hash)
{
QHash<QString, TorrentMoveState>::iterator i = torrentMoveStates.find(hash);
if (i == torrentMoveStates.end()) {
Q_ASSERT(false);
@ -161,37 +195,45 @@ QString TorrentTempData::getQueuedPath(const QString &hash) {
return i->queuedPath;
}
void TorrentTempData::setAddPaused(const QString &hash, const bool &paused) {
void TorrentTempData::setAddPaused(const QString &hash, const bool &paused)
{
data[hash].add_paused = paused;
}
bool TorrentTempData::isAddPaused(const QString &hash) {
bool TorrentTempData::isAddPaused(const QString &hash)
{
return data.value(hash).add_paused;
}
void HiddenData::addData(const QString &hash) {
void HiddenData::addData(const QString &hash)
{
data[hash] = false;
}
bool HiddenData::hasData(const QString &hash) {
bool HiddenData::hasData(const QString &hash)
{
return data.contains(hash);
}
void HiddenData::deleteData(const QString &hash) {
void HiddenData::deleteData(const QString &hash)
{
if (data.value(hash, false))
metadata_counter--;
data.remove(hash);
}
int HiddenData::getSize() {
int HiddenData::getSize()
{
return data.size();
}
int HiddenData::getDownloadingSize() {
int HiddenData::getDownloadingSize()
{
return data.size() - metadata_counter;
}
void HiddenData::gotMetadata(const QString &hash) {
void HiddenData::gotMetadata(const QString &hash)
{
if (!data.contains(hash))
return;
data[hash] = true;
@ -206,12 +248,14 @@ TorrentPersistentData::TorrentPersistentData()
m_instance = this;
}
TorrentPersistentData::~TorrentPersistentData() {
TorrentPersistentData::~TorrentPersistentData()
{
save();
m_instance = 0;
}
void TorrentPersistentData::save() {
void TorrentPersistentData::save()
{
if (dirty)
saveImpl();
}
@ -222,38 +266,42 @@ TorrentPersistentData& TorrentPersistentData::instance()
return *m_instance;
}
bool TorrentPersistentData::isKnownTorrent(QString hash) {
bool TorrentPersistentData::isKnownTorrent(QString hash)
{
return all_data.contains(hash);
}
QStringList TorrentPersistentData::knownTorrents() {
QStringList TorrentPersistentData::knownTorrents()
{
return all_data.keys();
}
void TorrentPersistentData::setRatioLimit(const QString &hash, const qreal &ratio) {
void TorrentPersistentData::setRatioLimit(const QString &hash, const qreal &ratio)
{
QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["max_ratio"] = ratio;
all_data[hash] = data;
markDirty();
}
qreal TorrentPersistentData::getRatioLimit(const QString &hash) {
qreal TorrentPersistentData::getRatioLimit(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("max_ratio", USE_GLOBAL_RATIO).toReal();
}
bool TorrentPersistentData::hasPerTorrentRatioLimit() {
bool TorrentPersistentData::hasPerTorrentRatioLimit()
{
QHash<QString, QVariant>::ConstIterator it = all_data.constBegin();
QHash<QString, QVariant>::ConstIterator itend = all_data.constEnd();
for ( ; it != itend; ++it) {
if (it.value().toHash().value("max_ratio", USE_GLOBAL_RATIO).toReal() >= 0) {
for (; it != itend; ++it)
if (it.value().toHash().value("max_ratio", USE_GLOBAL_RATIO).toReal() >= 0)
return true;
}
}
return false;
}
void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &time) {
void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &time)
{
QHash<QString, QVariant> data = all_data.value(hash).toHash();
if (!data.contains("add_date")) {
data["add_date"] = time;
@ -262,7 +310,8 @@ void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &t
}
}
QDateTime TorrentPersistentData::getAddedDate(const QString &hash) {
QDateTime TorrentPersistentData::getAddedDate(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
QDateTime dt = data.value("add_date").toDateTime();
if (!dt.isValid()) {
@ -272,45 +321,50 @@ QDateTime TorrentPersistentData::getAddedDate(const QString &hash) {
return dt;
}
void TorrentPersistentData::setErrorState(const QString &hash, const bool has_error) {
void TorrentPersistentData::setErrorState(const QString &hash, const bool has_error)
{
QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["has_error"] = has_error;
all_data[hash] = data;
markDirty();
}
bool TorrentPersistentData::hasError(const QString &hash) {
bool TorrentPersistentData::hasError(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("has_error", false).toBool();
}
QDateTime TorrentPersistentData::getSeedDate(const QString &hash) {
QDateTime TorrentPersistentData::getSeedDate(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("seed_date").toDateTime();
}
void TorrentPersistentData::deletePersistentData(const QString &hash) {
void TorrentPersistentData::deletePersistentData(const QString &hash)
{
if (all_data.contains(hash)) {
all_data.remove(hash);
markDirty();
}
}
void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, const QString &save_path, const bool is_magnet) {
void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, const QString &save_path, const bool is_magnet)
{
Q_ASSERT(h.is_valid());
qDebug("Saving persistent data for %s", qPrintable(h.hash()));
// Save persistent data
QHash<QString, QVariant> data = all_data.value(h.hash()).toHash();
data["is_magnet"] = is_magnet;
if (is_magnet) {
if (is_magnet)
data["magnet_uri"] = misc::toQString(make_magnet_uri(h));
}
data["seed"] = h.is_seed();
data["priority"] = h.queue_position();
if (save_path.isEmpty()) {
qDebug("TorrentPersistantData: save path is %s", qPrintable(h.save_path()));
data["save_path"] = h.save_path();
} else {
}
else {
qDebug("TorrentPersistantData: overriding save path is %s", qPrintable(save_path));
data["save_path"] = save_path; // Override torrent save path (e.g. because it is a temp dir)
}
@ -326,7 +380,8 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c
TorrentTempData::deleteTempData(h.hash());
}
void TorrentPersistentData::saveSavePath(const QString &hash, const QString &save_path) {
void TorrentPersistentData::saveSavePath(const QString &hash, const QString &save_path)
{
Q_ASSERT(!hash.isEmpty());
qDebug("TorrentPersistentData::saveSavePath(%s)", qPrintable(save_path));
QHash<QString, QVariant> data = all_data.value(hash).toHash();
@ -336,7 +391,8 @@ void TorrentPersistentData::saveSavePath(const QString &hash, const QString &sav
qDebug("TorrentPersistentData: Saving save_path: %s, hash: %s", qPrintable(save_path), qPrintable(hash));
}
void TorrentPersistentData::saveLabel(const QString &hash, const QString &label) {
void TorrentPersistentData::saveLabel(const QString &hash, const QString &label)
{
Q_ASSERT(!hash.isEmpty());
QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["label"] = label;
@ -344,7 +400,8 @@ void TorrentPersistentData::saveLabel(const QString &hash, const QString &label)
markDirty();
}
void TorrentPersistentData::saveName(const QString &hash, const QString &name) {
void TorrentPersistentData::saveName(const QString &hash, const QString &name)
{
Q_ASSERT(!hash.isEmpty());
QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["name"] = name;
@ -352,21 +409,24 @@ void TorrentPersistentData::saveName(const QString &hash, const QString &name) {
markDirty();
}
void TorrentPersistentData::savePriority(const QTorrentHandle &h) {
void TorrentPersistentData::savePriority(const QTorrentHandle &h)
{
QHash<QString, QVariant> data = all_data[h.hash()].toHash();
data["priority"] = h.queue_position();
all_data[h.hash()] = data;
markDirty();
}
void TorrentPersistentData::savePriority(const QString &hash, const int &queue_pos) {
void TorrentPersistentData::savePriority(const QString &hash, const int &queue_pos)
{
QHash<QString, QVariant> data = all_data[hash].toHash();
data["priority"] = queue_pos;
all_data[hash] = data;
markDirty();
}
void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h) {
void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h)
{
QHash<QString, QVariant> data = all_data[h.hash()].toHash();
bool was_seed = data.value("seed", false).toBool();
if (was_seed != h.is_seed()) {
@ -376,58 +436,68 @@ void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h) {
}
}
void TorrentPersistentData::saveSeedStatus(const QString &hash, const bool seedStatus) {
void TorrentPersistentData::saveSeedStatus(const QString &hash, const bool seedStatus)
{
QHash<QString, QVariant> data = all_data[hash].toHash();
data["seed"] = seedStatus;
all_data[hash] = data;
markDirty();
}
QString TorrentPersistentData::getSavePath(const QString &hash) {
QString TorrentPersistentData::getSavePath(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
//qDebug("TorrentPersistentData: getSavePath %s", data["save_path"].toString().toLocal8Bit().data());
return data.value("save_path").toString();
}
QString TorrentPersistentData::getLabel(const QString &hash) {
QString TorrentPersistentData::getLabel(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("label", "").toString();
}
QString TorrentPersistentData::getName(const QString &hash) {
QString TorrentPersistentData::getName(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("name", "").toString();
}
int TorrentPersistentData::getPriority(const QString &hash) {
int TorrentPersistentData::getPriority(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("priority", -1).toInt();
}
bool TorrentPersistentData::isSeed(const QString &hash) {
bool TorrentPersistentData::isSeed(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("seed", false).toBool();
}
bool TorrentPersistentData::isMagnet(const QString &hash) {
bool TorrentPersistentData::isMagnet(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("is_magnet", false).toBool();
}
QString TorrentPersistentData::getMagnetUri(const QString &hash) {
QString TorrentPersistentData::getMagnetUri(const QString &hash)
{
const QHash<QString, QVariant> data = all_data.value(hash).toHash();
Q_ASSERT(data.value("is_magnet", false).toBool());
return data.value("magnet_uri").toString();
}
void TorrentPersistentData::markDirty() {
void TorrentPersistentData::markDirty()
{
if (!dirty) {
dirty = true;
timer.start(1000);
}
}
void TorrentPersistentData::saveImpl() {
void TorrentPersistentData::saveImpl()
{
Q_ASSERT(dirty);
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));

View file

@ -44,7 +44,8 @@ QT_END_NAMESPACE
class QTorrentHandle;
class TorrentTempData {
class TorrentTempData
{
// This class stores strings w/o modifying separators
public:
static bool hasTempData(const QString &hash);
@ -72,8 +73,9 @@ public:
static bool isAddPaused(const QString &hash);
private:
struct TorrentData {
TorrentData(): sequential(false), seed(false), add_paused(Preferences::instance()->addTorrentsInPause()) {}
struct TorrentData
{
TorrentData();
std::vector<int> files_priority;
QStringList path_list;
QString save_path;
@ -83,12 +85,9 @@ private:
bool add_paused;
};
struct TorrentMoveState {
TorrentMoveState(QString oldPath, QString newPath)
: oldPath(oldPath)
, newPath(newPath)
{}
struct TorrentMoveState
{
TorrentMoveState(QString oldPath, QString newPath);
// the moving occurs from oldPath to newPath
// queuedPath is where files should be moved to, when current moving is completed
QString oldPath;
@ -100,7 +99,8 @@ private:
static QHash<QString, TorrentMoveState> torrentMoveStates;
};
class HiddenData {
class HiddenData
{
public:
static void addData(const QString &hash);
static bool hasData(const QString &hash);
@ -114,12 +114,14 @@ private:
static unsigned int metadata_counter;
};
class TorrentPersistentData : QObject {
class TorrentPersistentData: QObject
{
Q_OBJECT
Q_DISABLE_COPY(TorrentPersistentData)
// This class stores strings w/o modifying separators
public:
enum RatioLimit {
enum RatioLimit
{
USE_GLOBAL_RATIO = -2,
NO_RATIO_LIMIT = -1
};
@ -136,7 +138,7 @@ public:
QStringList knownTorrents();
void setRatioLimit(const QString &hash, const qreal &ratio);
qreal getRatioLimit(const QString &hash);
bool hasPerTorrentRatioLimit() ;
bool hasPerTorrentRatioLimit();
void setAddedDate(const QString &hash, const QDateTime &time);
QDateTime getAddedDate(const QString &hash);
void setErrorState(const QString &hash, const bool has_error);