From a774b3d24816aa0010e1d5f67b20acb99e7181ac Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 12 Jan 2014 18:28:42 +0000 Subject: Show Mojang service statuses in status bar --- logic/status/StatusChecker.cpp | 138 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 logic/status/StatusChecker.cpp (limited to 'logic/status/StatusChecker.cpp') diff --git a/logic/status/StatusChecker.cpp b/logic/status/StatusChecker.cpp new file mode 100644 index 00000000..54cf077a --- /dev/null +++ b/logic/status/StatusChecker.cpp @@ -0,0 +1,138 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "StatusChecker.h" + +#include + +#include +#include + +#include + +StatusChecker::StatusChecker() +{ + +} + +void StatusChecker::reloadStatus() +{ + if (isLoadingStatus()) + { + QLOG_INFO() << "Ignored request to reload status. Currently reloading already."; + return; + } + + QLOG_INFO() << "Reloading status."; + + NetJob* job = new NetJob("Status JSON"); + job->addNetAction(ByteArrayDownload::make(URLConstants::MOJANG_STATUS_URL)); + QObject::connect(job, &NetJob::succeeded, this, &StatusChecker::statusDownloadFinished); + QObject::connect(job, &NetJob::failed, this, &StatusChecker::statusDownloadFailed); + m_statusNetJob.reset(job); + job->start(); +} + +void StatusChecker::statusDownloadFinished() +{ + // Parse the XML file and process the RSS feed entries. + QLOG_DEBUG() << "Finished loading status JSON."; + + QByteArray data; + { + ByteArrayDownloadPtr dl = std::dynamic_pointer_cast(m_statusNetJob->first()); + data = dl->m_data; + m_statusNetJob.reset(); + } + + QJsonParseError jsonError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + + if (jsonError.error != QJsonParseError::NoError) + { + fail("Error parsing status JSON:" + jsonError.errorString()); + return; + } + + if (!jsonDoc.isArray()) + { + fail("Error parsing status JSON: JSON root is not an array"); + return; + } + + QJsonArray root = jsonDoc.array(); + + for(auto status = root.begin(); status != root.end(); ++status) + { + QVariantMap map = (*status).toObject().toVariantMap(); + + for (QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) + { + QString key = iter.key(); + QVariant value = iter.value(); + + if(value.type() == QVariant::Type::String) + { + m_statusEntries.insert(key, value.toString()); + QLOG_DEBUG() << "Status JSON object: " << key << m_statusEntries[key]; + } + else + { + fail("Malformed status JSON: expected status type to be a string."); + return; + } + } + } + + succeed(); +} + +void StatusChecker::statusDownloadFailed() +{ + fail("Failed to load status JSON."); +} + + +QMap StatusChecker::getStatusEntries() const +{ + return m_statusEntries; +} + +bool StatusChecker::isLoadingStatus() const +{ + return m_statusNetJob.get() != nullptr; +} + +QString StatusChecker::getLastLoadErrorMsg() const +{ + return m_lastLoadError; +} + +void StatusChecker::succeed() +{ + m_lastLoadError = ""; + QLOG_DEBUG() << "Status loading succeeded."; + m_statusNetJob.reset(); + emit statusLoaded(); +} + +void StatusChecker::fail(const QString& errorMsg) +{ + m_lastLoadError = errorMsg; + QLOG_DEBUG() << "Failed to load status:" << errorMsg; + m_statusNetJob.reset(); + emit statusLoadingFailed(errorMsg); +} + -- cgit From 500581d095b634998f4d6db70276905cbfcf5d21 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 12 Jan 2014 18:42:02 +0000 Subject: More comments removal --- logic/status/StatusChecker.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'logic/status/StatusChecker.cpp') diff --git a/logic/status/StatusChecker.cpp b/logic/status/StatusChecker.cpp index 54cf077a..7c856d3f 100644 --- a/logic/status/StatusChecker.cpp +++ b/logic/status/StatusChecker.cpp @@ -47,7 +47,6 @@ void StatusChecker::reloadStatus() void StatusChecker::statusDownloadFinished() { - // Parse the XML file and process the RSS feed entries. QLOG_DEBUG() << "Finished loading status JSON."; QByteArray data; -- cgit From 7b96d74d3b197c23324c5a364809a91ea6800e4e Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Thu, 16 Jan 2014 23:06:07 +0100 Subject: Sort forge versions right. Do not spam the multimc log with mc server status stuff. --- logic/BaseVersion.h | 9 +++++++++ logic/lists/ForgeVersionList.cpp | 8 ++------ logic/lists/ForgeVersionList.h | 21 +++++++++++++++++---- logic/status/StatusChecker.cpp | 6 +++--- 4 files changed, 31 insertions(+), 13 deletions(-) (limited to 'logic/status/StatusChecker.cpp') diff --git a/logic/BaseVersion.h b/logic/BaseVersion.h index 1864c94c..43f5942a 100644 --- a/logic/BaseVersion.h +++ b/logic/BaseVersion.h @@ -39,6 +39,15 @@ struct BaseVersion * the kind of version this is (Stable, Beta, Snapshot, whatever) */ virtual QString typeString() const = 0; + + virtual bool operator<(BaseVersion &a) + { + return name() < a.name(); + }; + virtual bool operator>(BaseVersion &a) + { + return name() > a.name(); + }; }; typedef std::shared_ptr BaseVersionPtr; diff --git a/logic/lists/ForgeVersionList.cpp b/logic/lists/ForgeVersionList.cpp index 56eca744..38e033fa 100644 --- a/logic/lists/ForgeVersionList.cpp +++ b/logic/lists/ForgeVersionList.cpp @@ -404,12 +404,8 @@ void ForgeListLoadTask::listDownloaded() { return; } - - qSort(list.begin(), list.end(), [](const BaseVersionPtr & p1, const BaseVersionPtr & p2) - { - // TODO better comparison (takes major/minor/build number into account) - return p1->name() > p2->name(); - }); + std::sort(list.begin(), list.end(), [](const BaseVersionPtr & l, const BaseVersionPtr & r) + { return (*l > *r); }); m_list->updateListData(list); diff --git a/logic/lists/ForgeVersionList.h b/logic/lists/ForgeVersionList.h index 924084ae..b19d3f56 100644 --- a/logic/lists/ForgeVersionList.h +++ b/logic/lists/ForgeVersionList.h @@ -29,25 +29,38 @@ typedef std::shared_ptr ForgeVersionPtr; struct ForgeVersion : public BaseVersion { - virtual QString descriptor() + virtual QString descriptor() override { return filename; } ; - virtual QString name() + virtual QString name() override { return "Forge " + jobbuildver; } ; - virtual QString typeString() const + virtual QString typeString() const override { if (installer_url.isEmpty()) return "Universal"; else return "Installer"; } - ; + virtual bool operator<(BaseVersion &a) override + { + ForgeVersion *pa = dynamic_cast(&a); + if(!pa) + return true; + return m_buildnr < pa->m_buildnr; + } + virtual bool operator>(BaseVersion &a) override + { + ForgeVersion *pa = dynamic_cast(&a); + if(!pa) + return false; + return m_buildnr > pa->m_buildnr; + } int m_buildnr = 0; QString universal_url; QString changelog_url; diff --git a/logic/status/StatusChecker.cpp b/logic/status/StatusChecker.cpp index 7c856d3f..66f800ae 100644 --- a/logic/status/StatusChecker.cpp +++ b/logic/status/StatusChecker.cpp @@ -31,11 +31,11 @@ void StatusChecker::reloadStatus() { if (isLoadingStatus()) { - QLOG_INFO() << "Ignored request to reload status. Currently reloading already."; + // QLOG_INFO() << "Ignored request to reload status. Currently reloading already."; return; } - QLOG_INFO() << "Reloading status."; + // QLOG_INFO() << "Reloading status."; NetJob* job = new NetJob("Status JSON"); job->addNetAction(ByteArrayDownload::make(URLConstants::MOJANG_STATUS_URL)); @@ -85,7 +85,7 @@ void StatusChecker::statusDownloadFinished() if(value.type() == QVariant::Type::String) { m_statusEntries.insert(key, value.toString()); - QLOG_DEBUG() << "Status JSON object: " << key << m_statusEntries[key]; + //QLOG_DEBUG() << "Status JSON object: " << key << m_statusEntries[key]; } else { -- cgit