diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-08-26 04:21:06 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-08-26 04:21:06 -0400 |
| commit | 2eee72da6815f9d5c643b58c95f633e69da5150a (patch) | |
| tree | a358d98dbe638c2d353d8e0637cd0682acbf74bc /src | |
| parent | c42e5fb2fec9b20fe6956d8798b85874b2eff73e (diff) | |
moved code for byte sizes and speed to uibase
added scoped classes for QSettings groups and arrays
servers logged on startup
Diffstat (limited to 'src')
| -rw-r--r-- | src/downloadlist.cpp | 21 | ||||
| -rw-r--r-- | src/downloadlist.h | 2 | ||||
| -rw-r--r-- | src/downloadmanager.cpp | 19 | ||||
| -rw-r--r-- | src/settings.cpp | 262 | ||||
| -rw-r--r-- | src/settingsdialognexus.cpp | 2 |
5 files changed, 180 insertions, 126 deletions
diff --git a/src/downloadlist.cpp b/src/downloadlist.cpp index 36bc2b7f..6957f270 100644 --- a/src/downloadlist.cpp +++ b/src/downloadlist.cpp @@ -19,6 +19,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include "downloadlist.h"
#include "downloadmanager.h"
+#include <utility.h>
#include <log.h>
#include <QEvent>
#include <QColor>
@@ -121,7 +122,7 @@ QVariant DownloadList::data(const QModelIndex &index, int role) const return QString("%1").arg(m_Manager->getModID(index.row()));
}
}
- case COL_SIZE: return sizeFormat(m_Manager->getFileSize(index.row()));
+ case COL_SIZE: return MOBase::localizedByteSize(m_Manager->getFileSize(index.row()));
case COL_FILETIME: return m_Manager->getFileTime(index.row());
case COL_STATUS:
switch (m_Manager->getState(index.row())) {
@@ -195,21 +196,3 @@ void DownloadList::update(int row) else
log::error("invalid row {} in download list, update failed", row);
}
-
-QString DownloadList::sizeFormat(quint64 size) const
-{
- qreal calc = size;
- QStringList list;
- list << "MB" << "GB" << "TB";
-
- QStringListIterator i(list);
- QString unit("KB");
-
- calc /= 1024.0;
- while (calc >= 1024.0 && i.hasNext()) {
- unit = i.next();
- calc /= 1024.0;
- }
-
- return QString().setNum(calc, 'f', 2) + " " + unit;
-}
diff --git a/src/downloadlist.h b/src/downloadlist.h index 6f63f0c8..51ab4541 100644 --- a/src/downloadlist.h +++ b/src/downloadlist.h @@ -99,8 +99,6 @@ private: DownloadManager *m_Manager;
bool m_MetaDisplay;
-
- QString sizeFormat(quint64 size) const;
};
#endif // DOWNLOADLIST_H
diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 93ca1608..a5dc164c 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -1441,22 +1441,11 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) std::get<4>(info->m_SpeedDiff) = ((calc*0.5) + (std::get<4>(info->m_SpeedDiff)*1.5)) / 2; // calculate the download speed - double speed = (std::get<4>(info->m_SpeedDiff) * 1000.0) / (5 * 1000); + const double speed = (std::get<4>(info->m_SpeedDiff) * 1000.0) / (5 * 1000); - QString unit; - if (speed < 1000) { - unit = "B/s"; - } - else if (speed < 1000*1024) { - speed /= 1024; - unit = "KB/s"; - } - else { - speed /= 1024 * 1024; - unit = "MB/s"; - } - - info->m_Progress.second = QString::fromLatin1("%1% - %2 %3").arg(info->m_Progress.first).arg(QString::number(speed, 'f', 1)).arg(unit); + info->m_Progress.second = QString::fromLatin1("%1% - %2") + .arg(info->m_Progress.first) + .arg(MOBase::localizedByteSpeed(speed)); TaskProgressManager::instance().updateProgress(info->m_TaskProgressId, bytesReceived, bytesTotal); emit update(index); diff --git a/src/settings.cpp b/src/settings.cpp index b11bc61c..406544f7 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -27,6 +27,78 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. using namespace MOBase; +class ScopedGroup +{ +public: + ScopedGroup(QSettings& s, const QString& name) + : m_settings(s) + { + m_settings.beginGroup(name); + } + + ~ScopedGroup() + { + m_settings.endGroup(); + } + + ScopedGroup(const ScopedGroup&) = delete; + ScopedGroup& operator=(const ScopedGroup&) = delete; + +private: + QSettings& m_settings; +}; + + +class ScopedReadArray +{ +public: + ScopedReadArray(QSettings& s, const QString& name) + : m_settings(s), m_count(0) + { + m_count = m_settings.beginReadArray(name); + } + + ~ScopedReadArray() + { + m_settings.endArray(); + } + + ScopedReadArray(const ScopedReadArray&) = delete; + ScopedReadArray& operator=(const ScopedReadArray&) = delete; + + int count() const + { + return m_count; + } + +private: + QSettings& m_settings; + int m_count; +}; + + +class ScopedWriteArray +{ +public: + ScopedWriteArray(QSettings& s, const QString& name) + : m_settings(s) + { + m_settings.beginWriteArray(name); + } + + ~ScopedWriteArray() + { + m_settings.endArray(); + } + + ScopedWriteArray(const ScopedWriteArray&) = delete; + ScopedWriteArray& operator=(const ScopedWriteArray&) = delete; + +private: + QSettings& m_settings; +}; + + template <class T> std::optional<T> getOptional( const QSettings& s, const QString& name, std::optional<T> def={}) @@ -206,19 +278,21 @@ void Settings::processUpdates( } if (lastVersion < QVersionNumber(2, 2, 0)) { - m_Settings.beginGroup("Settings"); - m_Settings.remove("steam_password"); - m_Settings.remove("nexus_username"); - m_Settings.remove("nexus_password"); - m_Settings.remove("nexus_login"); - m_Settings.remove("nexus_api_key"); - m_Settings.remove("ask_for_nexuspw"); - m_Settings.remove("nmm_version"); - m_Settings.endGroup(); + { + ScopedGroup sg(m_Settings, "Settings"); + m_Settings.remove("steam_password"); + m_Settings.remove("nexus_username"); + m_Settings.remove("nexus_password"); + m_Settings.remove("nexus_login"); + m_Settings.remove("nexus_api_key"); + m_Settings.remove("ask_for_nexuspw"); + m_Settings.remove("nmm_version"); + } - m_Settings.beginGroup("Servers"); - m_Settings.remove(""); - m_Settings.endGroup(); + { + ScopedGroup sg(m_Settings, "Servers"); + m_Settings.remove(""); + } } if (lastVersion < QVersionNumber(2, 2, 1)) { @@ -251,12 +325,12 @@ void Settings::clearPlugins() m_PluginSettings.clear(); m_PluginBlacklist.clear(); - int count = m_Settings.beginReadArray("pluginBlacklist"); - for (int i = 0; i < count; ++i) { + + ScopedReadArray sra(m_Settings, "pluginBlacklist"); + for (int i = 0; i < sra.count(); ++i) { m_Settings.setArrayIndex(i); m_PluginBlacklist.insert(m_Settings.value("name").toString()); } - m_Settings.endArray(); } bool Settings::pluginBlacklisted(const QString &fileName) const @@ -876,46 +950,50 @@ ServerList Settings::getServers() const // in 2.2.1, one key per server is returned // getting the keys - m_Settings.beginGroup("Servers"); - const auto keys = m_Settings.childKeys(); - m_Settings.endGroup(); + QStringList keys; + + { + ScopedGroup sg(m_Settings, "Servers"); + keys = m_Settings.childKeys(); + } if (!keys.empty() && keys[0] != "size") { // old format return getServersFromOldMap(); } + // post 2.2.1 format, array of values ServerList list; - const int size = m_Settings.beginReadArray("Servers"); + { + ScopedReadArray sra(m_Settings, "Servers"); - for (int i=0; i<size; ++i) { - m_Settings.setArrayIndex(i); + for (int i=0; i<sra.count(); ++i) { + m_Settings.setArrayIndex(i); - ServerInfo::SpeedList lastDownloads; + ServerInfo::SpeedList lastDownloads; - const auto lastDownloadsString = m_Settings.value("lastDownloads").toString(); - for (const auto& s : lastDownloadsString.split(" ")) { - const auto bytesPerSecond = s.toInt(); - if (bytesPerSecond > 0) { - lastDownloads.push_back(bytesPerSecond); + const auto lastDownloadsString = m_Settings.value("lastDownloads").toString(); + for (const auto& s : lastDownloadsString.split(" ")) { + const auto bytesPerSecond = s.toInt(); + if (bytesPerSecond > 0) { + lastDownloads.push_back(bytesPerSecond); + } } - } - ServerInfo server( - m_Settings.value("name").toString(), - m_Settings.value("premium").toBool(), - QDate::fromString(m_Settings.value("lastSeen").toString(), Qt::ISODate), - m_Settings.value("preferred").toInt(), - lastDownloads); + ServerInfo server( + m_Settings.value("name").toString(), + m_Settings.value("premium").toBool(), + QDate::fromString(m_Settings.value("lastSeen").toString(), Qt::ISODate), + m_Settings.value("preferred").toInt(), + lastDownloads); - list.add(std::move(server)); + list.add(std::move(server)); + } } - m_Settings.endArray(); - return list; } @@ -924,8 +1002,7 @@ ServerList Settings::getServersFromOldMap() const // for 2.2.1 and before ServerList list; - - m_Settings.beginGroup("Servers"); + ScopedGroup sg(m_Settings, "Servers"); for (const QString &serverKey : m_Settings.childKeys()) { QVariantMap data = m_Settings.value(serverKey).toMap(); @@ -943,8 +1020,6 @@ ServerList Settings::getServersFromOldMap() const list.add(std::move(server)); } - m_Settings.endGroup(); - return list; } @@ -953,34 +1028,35 @@ void Settings::updateServers(ServerList servers) // clean up unavailable servers servers.cleanup(); - m_Settings.beginGroup("Servers"); - m_Settings.remove(""); - m_Settings.endGroup(); + { + ScopedGroup sg(m_Settings, "Servers"); + m_Settings.remove(""); + } - m_Settings.beginWriteArray("Servers"); + { + ScopedWriteArray swa(m_Settings, "Servers"); - int i=0; - for (const auto& server : servers) { - m_Settings.setArrayIndex(i); + int i=0; + for (const auto& server : servers) { + m_Settings.setArrayIndex(i); - m_Settings.setValue("name", server.name()); - m_Settings.setValue("premium", server.isPremium()); - m_Settings.setValue("lastSeen", server.lastSeen().toString(Qt::ISODate)); - m_Settings.setValue("preferred", server.preferred()); + m_Settings.setValue("name", server.name()); + m_Settings.setValue("premium", server.isPremium()); + m_Settings.setValue("lastSeen", server.lastSeen().toString(Qt::ISODate)); + m_Settings.setValue("preferred", server.preferred()); - QString lastDownloads; - for (const auto& speed : server.lastDownloads()) { - if (speed > 0) { - lastDownloads += QString("%1 ").arg(speed); + QString lastDownloads; + for (const auto& speed : server.lastDownloads()) { + if (speed > 0) { + lastDownloads += QString("%1 ").arg(speed); + } } - } - m_Settings.setValue("lastDownloads", lastDownloads.trimmed()); + m_Settings.setValue("lastDownloads", lastDownloads.trimmed()); - ++i; + ++i; + } } - - m_Settings.endArray(); } void Settings::addBlacklistPlugin(const QString &fileName) @@ -992,23 +1068,22 @@ void Settings::addBlacklistPlugin(const QString &fileName) void Settings::writePluginBlacklist() { m_Settings.remove("pluginBlacklist"); - m_Settings.beginWriteArray("pluginBlacklist"); + + ScopedWriteArray swa(m_Settings, "pluginBlacklist"); int idx = 0; for (const QString &plugin : m_PluginBlacklist) { m_Settings.setArrayIndex(idx++); m_Settings.setValue("name", plugin); } - - m_Settings.endArray(); } std::map<QString, QString> Settings::getRecentDirectories() const { std::map<QString, QString> map; - const int size = m_Settings.beginReadArray("recentDirectories"); + ScopedReadArray sra(m_Settings, "recentDirectories"); - for (int i=0; i<size; ++i) { + for (int i=0; i<sra.count(); ++i) { m_Settings.setArrayIndex(i); const QVariant name = m_Settings.value("name"); @@ -1019,15 +1094,14 @@ std::map<QString, QString> Settings::getRecentDirectories() const } } - m_Settings.endArray(); - return map; } void Settings::setRecentDirectories(const std::map<QString, QString>& map) { m_Settings.remove("recentDirectories"); - m_Settings.beginWriteArray("recentDirectories"); + + ScopedWriteArray swa(m_Settings, "recentDirectories"); int index = 0; for (auto&& p : map) { @@ -1037,16 +1111,14 @@ void Settings::setRecentDirectories(const std::map<QString, QString>& map) ++index; } - - m_Settings.endArray(); } std::vector<std::map<QString, QVariant>> Settings::getExecutables() const { - const int count = m_Settings.beginReadArray("customExecutables"); + ScopedReadArray sra(m_Settings, "customExecutables"); std::vector<std::map<QString, QVariant>> v; - for (int i=0; i<count; ++i) { + for (int i=0; i<sra.count(); ++i) { m_Settings.setArrayIndex(i); std::map<QString, QVariant> map; @@ -1059,15 +1131,14 @@ std::vector<std::map<QString, QVariant>> Settings::getExecutables() const v.push_back(map); } - m_Settings.endArray(); - return v; } void Settings::setExecutables(const std::vector<std::map<QString, QVariant>>& v) { m_Settings.remove("customExecutables"); - m_Settings.beginWriteArray("customExecutables"); + + ScopedWriteArray swa(m_Settings, "customExecutables"); int i = 0; @@ -1080,8 +1151,6 @@ void Settings::setExecutables(const std::vector<std::map<QString, QVariant>>& v) ++i; } - - m_Settings.endArray(); } bool Settings::isTutorialCompleted(const QString& windowName) const @@ -1154,9 +1223,8 @@ void Settings::setQuestionFileButton( void Settings::resetQuestionButtons() { - m_Settings.beginGroup("DialogChoices"); + ScopedGroup sg(m_Settings, "DialogChoices"); m_Settings.remove(""); - m_Settings.endGroup(); } std::optional<int> Settings::getIndex(const QComboBox* cb) const @@ -1248,17 +1316,34 @@ void Settings::dump() const log::debug("settings:"); - m_Settings.beginGroup("Settings"); + { + ScopedGroup sg(m_Settings, "Settings"); - for (auto k : m_Settings.allKeys()) { - if (ignore.contains(k, Qt::CaseInsensitive)) { - continue; - } + for (auto k : m_Settings.allKeys()) { + if (ignore.contains(k, Qt::CaseInsensitive)) { + continue; + } - log::debug(" . {}={}", k, m_Settings.value(k).toString()); + log::debug(" . {}={}", k, m_Settings.value(k).toString()); + } } - m_Settings.endGroup(); + log::debug("servers:"); + + for (const auto& server : getServers()) { + QString lastDownloads; + for (auto speed : server.lastDownloads()) { + lastDownloads += QString("%1 ").arg(speed); + } + + log::debug( + " . {} premium={} lastSeen={} preferred={} lastDownloads={}", + server.name(), + server.isPremium() ? "yes" : "no", + server.lastSeen().toString(Qt::ISODate), + server.preferred(), + lastDownloads.trimmed()); + } } @@ -1278,9 +1363,8 @@ void GeometrySettings::resetIfNeeded() return; } - m_Settings.beginGroup("geometry"); + ScopedGroup sg(m_Settings, "geometry"); m_Settings.remove(""); - m_Settings.endGroup(); } void GeometrySettings::saveGeometry(const QWidget* w) diff --git a/src/settingsdialognexus.cpp b/src/settingsdialognexus.cpp index f2bd3ab5..3de1a6ba 100644 --- a/src/settingsdialognexus.cpp +++ b/src/settingsdialognexus.cpp @@ -89,7 +89,7 @@ NexusSettingsTab::NexusSettingsTab(Settings& s, SettingsDialog& d) const auto averageSpeed = server.averageSpeed(); if (averageSpeed > 0) { - descriptor += QString(" (%1 kbps)").arg(averageSpeed / 1024); + descriptor += QString(" (%1)").arg(MOBase::localizedByteSpeed(averageSpeed)); } QListWidgetItem *newItem = new ServerItem<int>(descriptor, Qt::UserRole + 1); |
