Browse Source

Speedup persistent data

Conflicts:
	src/mainwindow.cpp
	src/mainwindow.h
adaptive-webui-19844
Ivan Sorokin 10 years ago
parent
commit
70a114eb13
  1. 11
      src/mainwindow.cpp
  2. 2
      src/mainwindow.h
  3. 92
      src/torrentpersistentdata.cpp
  4. 7
      src/torrentpersistentdata.h

11
src/mainwindow.cpp

@ -243,6 +243,11 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine): QMai
preventTimer = new QTimer(this); preventTimer = new QTimer(this);
connect(preventTimer, SIGNAL(timeout()), SLOT(checkForActiveTorrents())); connect(preventTimer, SIGNAL(timeout()), SLOT(checkForActiveTorrents()));
// Initialization of persistent data
TorrentPersistentData::load();
connect(&persistentDataSaver, SIGNAL(timeout()), SLOT(savePersistentData()));
persistentDataSaver.start(10000);
// Configure BT session according to options // Configure BT session according to options
loadPreferences(false); loadPreferences(false);
@ -459,6 +464,7 @@ void MainWindow::shutdownCleanUp()
QBtSession::drop(); QBtSession::drop();
// Save window size, columns size // Save window size, columns size
writeSettings(); writeSettings();
TorrentPersistentData::save();
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
// Workaround to avoid bug http://bugreports.qt.nokia.com/browse/QTBUG-7305 // Workaround to avoid bug http://bugreports.qt.nokia.com/browse/QTBUG-7305
setUnifiedTitleAndToolBarOnMac(false); setUnifiedTitleAndToolBarOnMac(false);
@ -1586,6 +1592,11 @@ void MainWindow::handleUpdateCheckFinished(bool update_available, QString new_ve
} }
#endif #endif
void MainWindow::savePersistentData()
{
TorrentPersistentData::save();
}
void MainWindow::on_actionDonate_money_triggered() void MainWindow::on_actionDonate_money_triggered()
{ {
QDesktopServices::openUrl(QUrl("http://sourceforge.net/donate/index.php?group_id=163414")); QDesktopServices::openUrl(QUrl("http://sourceforge.net/donate/index.php?group_id=163414"));

2
src/mainwindow.h

@ -143,6 +143,7 @@ protected slots:
#if defined(Q_OS_WIN) || defined(Q_OS_MAC) #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
void handleUpdateCheckFinished(bool update_available, QString new_version, bool invokedByUser); void handleUpdateCheckFinished(bool update_available, QString new_version, bool invokedByUser);
#endif #endif
void savePersistentData();
protected: protected:
void closeEvent(QCloseEvent *); void closeEvent(QCloseEvent *);
@ -216,6 +217,7 @@ private:
bool has_python; bool has_python;
#endif #endif
QMenu* toolbarMenu; QMenu* toolbarMenu;
QTimer persistentDataSaver;
private slots: private slots:
void on_actionSearch_engine_triggered(); void on_actionSearch_engine_triggered();

92
src/torrentpersistentdata.cpp

@ -44,6 +44,8 @@ QHash<QString, TorrentTempData::TorrentData> TorrentTempData::data = QHash<QStri
QHash<QString, TorrentTempData::TorrentMoveState> TorrentTempData::torrentMoveStates = QHash<QString, TorrentTempData::TorrentMoveState>(); QHash<QString, TorrentTempData::TorrentMoveState> TorrentTempData::torrentMoveStates = QHash<QString, TorrentTempData::TorrentMoveState>();
QHash<QString, bool> HiddenData::data = QHash<QString, bool>(); QHash<QString, bool> HiddenData::data = QHash<QString, bool>();
unsigned int HiddenData::metadata_counter = 0; unsigned int HiddenData::metadata_counter = 0;
QHash<QString, QVariant> TorrentPersistentData::all_data = QHash<QString, QVariant>();
bool TorrentPersistentData::dirty = false;
bool TorrentTempData::hasTempData(const QString &hash) { bool TorrentTempData::hasTempData(const QString &hash) {
return data.contains(hash); return data.contains(hash);
@ -197,37 +199,41 @@ void HiddenData::gotMetadata(const QString &hash) {
metadata_counter++; metadata_counter++;
} }
bool TorrentPersistentData::isKnownTorrent(QString hash) { void TorrentPersistentData::load() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
all_data = settings.value("torrents").toHash();
dirty = false;
}
void TorrentPersistentData::save() {
if (dirty) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash(); settings.setValue("torrents", all_data);
dirty = false;
}
}
bool TorrentPersistentData::isKnownTorrent(QString hash) {
return all_data.contains(hash); return all_data.contains(hash);
} }
QStringList TorrentPersistentData::knownTorrents() { QStringList TorrentPersistentData::knownTorrents() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
return all_data.keys(); return all_data.keys();
} }
void TorrentPersistentData::setRatioLimit(const QString &hash, const qreal &ratio) { void TorrentPersistentData::setRatioLimit(const QString &hash, const qreal &ratio) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash(); QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["max_ratio"] = ratio; data["max_ratio"] = ratio;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
qreal TorrentPersistentData::getRatioLimit(const QString &hash) { qreal TorrentPersistentData::getRatioLimit(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("max_ratio", USE_GLOBAL_RATIO).toReal(); return data.value("max_ratio", USE_GLOBAL_RATIO).toReal();
} }
bool TorrentPersistentData::hasPerTorrentRatioLimit() { bool TorrentPersistentData::hasPerTorrentRatioLimit() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant>::ConstIterator it = all_data.constBegin(); QHash<QString, QVariant>::ConstIterator it = all_data.constBegin();
QHash<QString, QVariant>::ConstIterator itend = all_data.constEnd(); QHash<QString, QVariant>::ConstIterator itend = all_data.constEnd();
for ( ; it != itend; ++it) { for ( ; it != itend; ++it) {
@ -239,19 +245,15 @@ bool TorrentPersistentData::hasPerTorrentRatioLimit() {
} }
void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &time) { void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &time) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash(); QHash<QString, QVariant> data = all_data.value(hash).toHash();
if (!data.contains("add_date")) { if (!data.contains("add_date")) {
data["add_date"] = time; data["add_date"] = time;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
} }
QDateTime TorrentPersistentData::getAddedDate(const QString &hash) { QDateTime TorrentPersistentData::getAddedDate(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
QDateTime dt = data.value("add_date").toDateTime(); QDateTime dt = data.value("add_date").toDateTime();
if (!dt.isValid()) { if (!dt.isValid()) {
@ -262,34 +264,26 @@ QDateTime TorrentPersistentData::getAddedDate(const QString &hash) {
} }
void TorrentPersistentData::setErrorState(const QString &hash, const bool has_error) { void TorrentPersistentData::setErrorState(const QString &hash, const bool has_error) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash(); QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["has_error"] = has_error; data["has_error"] = has_error;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
bool TorrentPersistentData::hasError(const QString &hash) { bool TorrentPersistentData::hasError(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("has_error", false).toBool(); return data.value("has_error", false).toBool();
} }
QDateTime TorrentPersistentData::getSeedDate(const QString &hash) { QDateTime TorrentPersistentData::getSeedDate(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("seed_date").toDateTime(); return data.value("seed_date").toDateTime();
} }
void TorrentPersistentData::deletePersistentData(const QString &hash) { void TorrentPersistentData::deletePersistentData(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
if (all_data.contains(hash)) { if (all_data.contains(hash)) {
all_data.remove(hash); all_data.remove(hash);
settings.setValue("torrents", all_data); dirty = true;
} }
} }
@ -297,8 +291,6 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c
Q_ASSERT(h.is_valid()); Q_ASSERT(h.is_valid());
qDebug("Saving persistent data for %s", qPrintable(h.hash())); qDebug("Saving persistent data for %s", qPrintable(h.hash()));
// Save persistent data // Save persistent data
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(h.hash()).toHash(); QHash<QString, QVariant> data = all_data.value(h.hash()).toHash();
data["is_magnet"] = is_magnet; data["is_magnet"] = is_magnet;
if (is_magnet) { if (is_magnet) {
@ -317,7 +309,7 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c
data["label"] = TorrentTempData::getLabel(h.hash()); data["label"] = TorrentTempData::getLabel(h.hash());
// Save data // Save data
all_data[h.hash()] = data; all_data[h.hash()] = data;
settings.setValue("torrents", all_data); dirty = true;
qDebug("TorrentPersistentData: Saving save_path %s, hash: %s", qPrintable(h.save_path()), qPrintable(h.hash())); qDebug("TorrentPersistentData: Saving save_path %s, hash: %s", qPrintable(h.save_path()), qPrintable(h.hash()));
// Set Added date // Set Added date
setAddedDate(h.hash(), QDateTime::currentDateTime()); setAddedDate(h.hash(), QDateTime::currentDateTime());
@ -328,120 +320,92 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c
void TorrentPersistentData::saveSavePath(const QString &hash, const QString &save_path) { void TorrentPersistentData::saveSavePath(const QString &hash, const QString &save_path) {
Q_ASSERT(!hash.isEmpty()); Q_ASSERT(!hash.isEmpty());
qDebug("TorrentPersistentData::saveSavePath(%s)", qPrintable(save_path)); qDebug("TorrentPersistentData::saveSavePath(%s)", qPrintable(save_path));
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash(); QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["save_path"] = save_path; data["save_path"] = save_path;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
qDebug("TorrentPersistentData: Saving save_path: %s, hash: %s", qPrintable(save_path), qPrintable(hash)); 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()); Q_ASSERT(!hash.isEmpty());
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash(); QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["label"] = label; data["label"] = label;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
void TorrentPersistentData::saveName(const QString &hash, const QString &name) { void TorrentPersistentData::saveName(const QString &hash, const QString &name) {
Q_ASSERT(!hash.isEmpty()); Q_ASSERT(!hash.isEmpty());
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash(); QHash<QString, QVariant> data = all_data.value(hash).toHash();
data["name"] = name; data["name"] = name;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
void TorrentPersistentData::savePriority(const QTorrentHandle &h) { void TorrentPersistentData::savePriority(const QTorrentHandle &h) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data[h.hash()].toHash(); QHash<QString, QVariant> data = all_data[h.hash()].toHash();
data["priority"] = h.queue_position(); data["priority"] = h.queue_position();
all_data[h.hash()] = data; all_data[h.hash()] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
void TorrentPersistentData::savePriority(const QString &hash, const int &queue_pos) { void TorrentPersistentData::savePriority(const QString &hash, const int &queue_pos) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data[hash].toHash(); QHash<QString, QVariant> data = all_data[hash].toHash();
data["priority"] = queue_pos; data["priority"] = queue_pos;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h) { void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data[h.hash()].toHash(); QHash<QString, QVariant> data = all_data[h.hash()].toHash();
bool was_seed = data.value("seed", false).toBool(); bool was_seed = data.value("seed", false).toBool();
if (was_seed != h.is_seed()) { if (was_seed != h.is_seed()) {
data["seed"] = !was_seed; data["seed"] = !was_seed;
all_data[h.hash()] = data; all_data[h.hash()] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
} }
void TorrentPersistentData::saveSeedStatus(const QString &hash, const bool seedStatus) { void TorrentPersistentData::saveSeedStatus(const QString &hash, const bool seedStatus) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant> data = all_data[hash].toHash(); QHash<QString, QVariant> data = all_data[hash].toHash();
data["seed"] = seedStatus; data["seed"] = seedStatus;
all_data[hash] = data; all_data[hash] = data;
settings.setValue("torrents", all_data); dirty = true;
} }
QString TorrentPersistentData::getSavePath(const QString &hash) { QString TorrentPersistentData::getSavePath(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
//qDebug("TorrentPersistentData: getSavePath %s", data["save_path"].toString().toLocal8Bit().data()); //qDebug("TorrentPersistentData: getSavePath %s", data["save_path"].toString().toLocal8Bit().data());
return data.value("save_path").toString(); return data.value("save_path").toString();
} }
QString TorrentPersistentData::getLabel(const QString &hash) { QString TorrentPersistentData::getLabel(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("label", "").toString(); return data.value("label", "").toString();
} }
QString TorrentPersistentData::getName(const QString &hash) { QString TorrentPersistentData::getName(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("name", "").toString(); return data.value("name", "").toString();
} }
int TorrentPersistentData::getPriority(const QString &hash) { int TorrentPersistentData::getPriority(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("priority", -1).toInt(); return data.value("priority", -1).toInt();
} }
bool TorrentPersistentData::isSeed(const QString &hash) { bool TorrentPersistentData::isSeed(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("seed", false).toBool(); return data.value("seed", false).toBool();
} }
bool TorrentPersistentData::isMagnet(const QString &hash) { bool TorrentPersistentData::isMagnet(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
return data.value("is_magnet", false).toBool(); return data.value("is_magnet", false).toBool();
} }
QString TorrentPersistentData::getMagnetUri(const QString &hash) { QString TorrentPersistentData::getMagnetUri(const QString &hash) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
const QHash<QString, QVariant> data = all_data.value(hash).toHash(); const QHash<QString, QVariant> data = all_data.value(hash).toHash();
Q_ASSERT(data.value("is_magnet", false).toBool()); Q_ASSERT(data.value("is_magnet", false).toBool());
return data.value("magnet_uri").toString(); return data.value("magnet_uri").toString();

7
src/torrentpersistentdata.h

@ -121,6 +121,9 @@ public:
}; };
public: public:
static void load();
static void save();
static bool isKnownTorrent(QString hash); static bool isKnownTorrent(QString hash);
static QStringList knownTorrents(); static QStringList knownTorrents();
static void setRatioLimit(const QString &hash, const qreal &ratio); static void setRatioLimit(const QString &hash, const qreal &ratio);
@ -151,6 +154,10 @@ public:
static bool isSeed(const QString &hash); static bool isSeed(const QString &hash);
static bool isMagnet(const QString &hash); static bool isMagnet(const QString &hash);
static QString getMagnetUri(const QString &hash); static QString getMagnetUri(const QString &hash);
private:
static QHash<QString, QVariant> all_data;
static bool dirty;
}; };
#endif // TORRENTPERSISTENTDATA_H #endif // TORRENTPERSISTENTDATA_H

Loading…
Cancel
Save