From d375c590f87b366dd466bf63d846d3b00b8bd743 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 30 Apr 2023 22:10:21 +0200 Subject: [PATCH] auto sync runes --- include/config.h | 11 +++++++- include/runemanager.h | 4 +++ src/config.cpp | 59 +++++++++++++++++++++++++---------------- src/runemanager.cpp | 61 ++++++++++++++++++++++++++++++++++++++++--- ui/runemanager.ui | 10 +++---- 5 files changed, 111 insertions(+), 34 deletions(-) diff --git a/include/config.h b/include/config.h index a671a08..9c82a33 100644 --- a/include/config.h +++ b/include/config.h @@ -38,6 +38,15 @@ public: RunePage runepage; }; + struct GeneralRunePageConfig { + GeneralRunePageConfig(); + GeneralRunePageConfig(const QJsonObject&); + operator QJsonObject() const; + + bool autoSync; + std::vector> runePages; + }; + struct RootConfig { RootConfig(); RootConfig(const QJsonObject&); @@ -46,7 +55,7 @@ public: std::shared_ptr getPositionConfig(Position position); std::vector> positionConfigs; - std::vector> runePages; + GeneralRunePageConfig runepagesConfig; bool enabledAutoAccept; bool enabledSmiteWarn; diff --git a/include/runemanager.h b/include/runemanager.h index 000e60e..3eb31b4 100644 --- a/include/runemanager.h +++ b/include/runemanager.h @@ -34,7 +34,11 @@ private slots: void deleteRunepageClient(int id); void deleteRunepageAA(int id); + void autoSyncToggled(); + private: + void syncRunes(); + Ui::RuneManager* ui; std::shared_ptr client; Config* config = nullptr; diff --git a/src/config.cpp b/src/config.cpp index e051d02..47d0817 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -89,6 +89,35 @@ Config::RootConfig::RootConfig() {} Config::RunePageConfig::RunePageConfig(QString name, const RunePage& rp) : name(name), runepage(rp) {} +Config::GeneralRunePageConfig::GeneralRunePageConfig() {} +Config::GeneralRunePageConfig::GeneralRunePageConfig(const QJsonObject& j) { + auto runepagesRef = j["pages"]; + if(runepagesRef.isArray()) { + QJsonArray jpages = runepagesRef.toArray(); + + runePages.reserve(jpages.size()); + for(QJsonValue val : jpages) { + if(val.isObject()) { + runePages.push_back(std::make_shared(val.toObject())); + } + } + } + + autoSync = getValue(j, "autosync", true); +} +Config::GeneralRunePageConfig::operator QJsonObject() const { + QJsonObject out; + + QJsonArray runepagesArr; + for(std::shared_ptr rp : runePages) { + runepagesArr.push_back((QJsonObject) *rp); + } + out.insert("pages", runepagesArr); + out.insert("autosync", autoSync); + + return out; +} + Config::RootConfig::RootConfig(const QJsonObject& j) { if(j.contains("version")) { int version = j["version"].toInt(); @@ -114,17 +143,7 @@ Config::RootConfig::RootConfig(const QJsonObject& j) { } } - auto runepagesRef = j["runepages"]; - if(runepagesRef.isArray()) { - QJsonArray jpages = runepagesRef.toArray(); - - runePages.reserve(jpages.size()); - for(QJsonValue val : jpages) { - if(val.isObject()) { - runePages.push_back(std::make_shared(val.toObject())); - } - } - } + runepagesConfig = getValue(j, "runepages"); enabledAutoAccept = getValue(j, "enabledAutoAccept", true); enabledSmiteWarn = getValue(j, "enabledSmiteWarn", true); @@ -140,13 +159,8 @@ Config::RootConfig::operator QJsonObject() const { positionarr.push_back((QJsonObject) *pos.get()); } - QJsonArray runepagesArr; - for(std::shared_ptr rp : runePages) { - runepagesArr.push_back((QJsonObject) *rp); - } - out["positions"] = positionarr; - out["runepages"] = runepagesArr; + out["runepages"] = (QJsonObject) runepagesConfig; out.insert("enabledAutoAccept", enabledAutoAccept); out.insert("enabledSmiteWarn", enabledSmiteWarn); out.insert("enabledAutoWrite", enabledAutoWrite); @@ -182,30 +196,29 @@ Config::~Config() {} bool Config::load() { QFile conffile(QString::fromStdString(configFilePath)); if(!conffile.open(QIODevice::ReadOnly)) { - Log::error << "could not open configfile: " << configFilePath; + qCritical() << "could not open configfile: " << configFilePath; return false; } QJsonParseError err; QJsonDocument doc = QJsonDocument::fromJson(conffile.readAll(), &err); if(err.error != QJsonParseError::NoError) { - Log::error << "config parse error: " << err.errorString().toStdString() << " position: " << err.offset; + qCritical() << "config parse error: " << err.errorString() << " position: " << err.offset; return false; } if(doc.isObject()) { // implicit cast root = doc.object(); - Log::info << "config loaded"; + qInfo() << "config loaded"; return true; } - Log::error << "config is not a json object!"; + qCritical() << "config is not a json object!"; return false; } void Config::save() { - Log::note << "Config::save()"; mkdirs(configFolderPath); QFile conffile(QString::fromStdString(configFilePath)); @@ -217,7 +230,7 @@ void Config::save() { QJsonDocument doc; doc.setObject(root); conffile.write(doc.toJson()); - Log::info << "config saved"; + qInfo() << "config saved"; } Config::RootConfig& Config::getConfig() { diff --git a/src/runemanager.cpp b/src/runemanager.cpp index 4e75dad..448858e 100644 --- a/src/runemanager.cpp +++ b/src/runemanager.cpp @@ -20,6 +20,8 @@ RuneManager::RuneManager(QWidget* parent) : QWidget(parent), ui(new Ui::RuneMana QObject::connect(ui->listaaRunes, &RunePageList::runepageDeleted, this, &RuneManager::deleteRunepageAA); QObject::connect(ui->listClientRunes, &RunePageList::runepageDeleted, this, &RuneManager::deleteRunepageClient); + QObject::connect(ui->chkAutoCopy, &QCheckBox::clicked, this, &RuneManager::autoSyncToggled); + loadRunes(); } @@ -29,7 +31,13 @@ RuneManager::~RuneManager() { void RuneManager::setConfig(Config& config) { this->config = &config; - ui->listaaRunes->loadRunePages(config.getConfig().runePages); + Config::GeneralRunePageConfig& rpc = config.getConfig().runepagesConfig; + ui->listaaRunes->loadRunePages(rpc.runePages); + ui->chkAutoCopy->setChecked(rpc.autoSync); + + if(rpc.autoSync) { + syncRunes(); + } } void RuneManager::loadRunes() { @@ -47,7 +55,9 @@ void RuneManager::loadRunes() { if(client) { // load meta data runeInfo = client->getAllRuneAspekts(); + QCoreApplication::processEvents(); runeStyles = client->getAllRuneStyles(); + QCoreApplication::processEvents(); this->ui->listClientRunes->setRuneInfos(runeInfo, runeStyles); this->ui->listaaRunes->setRuneInfos(runeInfo, runeStyles); @@ -55,6 +65,11 @@ void RuneManager::loadRunes() { // load runepages const std::vector runePages = client->getAllRunePages(); ui->listClientRunes->loadRunePages(runePages); + + // check if autosync is enabled + if(config && config->getConfig().runepagesConfig.autoSync) { + syncRunes(); + } } setRunesEnabled(!!client); // cast to bool @@ -94,7 +109,7 @@ void RuneManager::saveRunePageAA(int id, QString name, const RunePage& rp) { if(!config) return; Config::RootConfig& rc = config->getConfig(); - auto& pages = rc.runePages; + auto& pages = rc.runepagesConfig.runePages; if(id == -1) { // int newId = pages.size(); pages.push_back(std::make_shared(name, rp)); @@ -126,7 +141,7 @@ void RuneManager::deleteRunepageAA(int id) { if(!config) return; Config::RootConfig& rc = config->getConfig(); - auto& pages = rc.runePages; + auto& pages = rc.runepagesConfig.runePages; if((int) pages.size() > id && id >= 0) { pages.erase(pages.begin() + id); @@ -138,3 +153,43 @@ void RuneManager::deleteRunepageAA(int id) { qWarning() << "unknown runepage id:" << id; } } + +void RuneManager::autoSyncToggled() { + bool autoSync = (ui->chkAutoCopy->isChecked()); + if(config) { + config->getConfig().runepagesConfig.autoSync = autoSync; + config->save(); + } + + if(autoSync) { + syncRunes(); + } +} + +void RuneManager::syncRunes() { + qInfo() << "syncing" << ui->listClientRunes->count() << "runes"; + + std::vector>& configs = config->getConfig().runepagesConfig.runePages; + bool changed = false; + + for(int i = 0; i < ui->listClientRunes->count(); ++i) { + const QListWidgetItem* item = ui->listClientRunes->item(i); + QString name = item->text(); + const RunePage* rp = (RunePage*) item->data(RunePageList::RolePointer).toULongLong(); + + auto itFound = std::find_if(configs.cbegin(), configs.cend(), [name, rp](const std::shared_ptr& rpc){ + return rpc->name == name && *rp == rpc->runepage; + }); + + if(itFound == configs.cend()) { + // no duplicate found -> add it + configs.push_back(std::make_shared(name, *rp)); + changed = true; + } + } + + if(changed) { + config->save(); + ui->listaaRunes->loadRunePages(configs); + } +} diff --git a/ui/runemanager.ui b/ui/runemanager.ui index 614eab5..46fd892 100644 --- a/ui/runemanager.ui +++ b/ui/runemanager.ui @@ -51,19 +51,15 @@ Reload - + + .. - - false - - Runes from the client get copied to the autoacceptor automatically. - -This Feature is in development. + Runes from the client get copied to the autoacceptor automatically. Auto Copy Runes