summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/downloadmanager.cpp17
-rw-r--r--src/downloadmanager.h17
-rw-r--r--src/mainwindow.cpp31
-rw-r--r--src/organizercore.cpp1
-rw-r--r--src/serverinfo.cpp72
-rw-r--r--src/serverinfo.h21
-rw-r--r--src/settings.cpp51
-rw-r--r--src/settingsdialognexus.cpp6
8 files changed, 143 insertions, 73 deletions
diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp
index 45cfdeed..93ca1608 100644
--- a/src/downloadmanager.cpp
+++ b/src/downloadmanager.cpp
@@ -288,12 +288,6 @@ void DownloadManager::setOutputDirectory(const QString &outputDirectory)
}
-void DownloadManager::setServers(const ServerList& servers)
-{
- m_Servers = servers;
-}
-
-
void DownloadManager::setSupportedExtensions(const QStringList &extensions)
{
m_SupportedExtensions = extensions;
@@ -1669,7 +1663,7 @@ void DownloadManager::nxmFileInfoAvailable(QString gameName, int modID, int file
static int evaluateFileInfoMap(
const QVariantMap &map,
- const QList<ServerInfo>& preferredServers)
+ const ServerList::container& preferredServers)
{
int preference = 0;
bool found = false;
@@ -1692,8 +1686,9 @@ static int evaluateFileInfoMap(
}
// sort function to sort by best download server
-bool DownloadManager::ServerByPreference(
- const QList<ServerInfo>& preferredServers,
+//
+bool ServerByPreference(
+ const ServerList::container& preferredServers,
const QVariant &LHS, const QVariant &RHS)
{
const auto a = evaluateFileInfoMap(LHS.toMap(), preferredServers);
@@ -1747,10 +1742,12 @@ void DownloadManager::nxmDownloadURLsAvailable(QString gameName, int modID, int
return;
}
+ const auto servers = m_OrganizerCore->settings().getServers();
+
std::sort(
resultList.begin(),
resultList.end(),
- boost::bind(&DownloadManager::ServerByPreference, m_Servers.getPreferred(), _1, _2));
+ boost::bind(&ServerByPreference, servers.getPreferred(), _1, _2));
info->userData["downloadMap"] = resultList;
diff --git a/src/downloadmanager.h b/src/downloadmanager.h
index f739f4f0..bed1b3cc 100644
--- a/src/downloadmanager.h
+++ b/src/downloadmanager.h
@@ -175,11 +175,6 @@ public:
QString getOutputDirectory() const { return m_OutputDirectory; }
/**
- * @brief sets the list of servers
- */
- void setServers(const ServerList& servers);
-
- /**
* @brief set the list of supported extensions
* @param extensions list of supported extensions
*/
@@ -361,17 +356,6 @@ public:
*/
void refreshList();
- /**
- * @brief Sort function for download servers
- * @param LHS
- * @param RHS
- * @return
- */
- static bool ServerByPreference(
- const QList<ServerInfo>& preferredServers,
- const QVariant &LHS, const QVariant &RHS);
-
-
virtual int startDownloadURLs(const QStringList &urls);
virtual int startDownloadNexusFile(int modID, int fileID);
@@ -551,7 +535,6 @@ private:
QVector<DownloadInfo*> m_ActiveDownloads;
QString m_OutputDirectory;
- ServerList m_Servers;
QStringList m_SupportedExtensions;
std::set<int> m_RequestIDs;
QVector<int> m_AlphabeticalTranslation;
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 79203d29..4c2594b8 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -5050,7 +5050,6 @@ void MainWindow::on_actionSettings_triggered()
dlManager->setOutputDirectory(settings.getDownloadDirectory());
}
}
- dlManager->setServers(settings.getServers());
if ((settings.getModDirectory() != oldModDirectory)
|| (settings.displayForeign() != oldDisplayForeign)) {
@@ -5904,20 +5903,32 @@ void MainWindow::nxmTrackedModsAvailable(QVariant userData, QVariant resultData,
void MainWindow::nxmDownloadURLs(QString, int, int, QVariant, QVariant resultData, int)
{
- ServerList servers;
+ auto servers = m_OrganizerCore.settings().getServers();
for (const QVariant &var : resultData.toList()) {
const QVariantMap map = var.toMap();
- ServerInfo server(
- map["short_name"].toString(),
- map["name"].toString().contains("Premium", Qt::CaseInsensitive),
- QDate::currentDate(),
- map["short_name"].toString().contains("CDN", Qt::CaseInsensitive) ? 1 : 0,
- map["downloadCount"].toInt(),
- map["downloadSpeed"].toDouble());
+ const auto name = map["short_name"].toString();
+ const auto isPremium = map["name"].toString().contains("Premium", Qt::CaseInsensitive);
+ const auto isCDN = map["short_name"].toString().contains("CDN", Qt::CaseInsensitive);
- servers.add(std::move(server));
+ bool found = false;
+
+ for (auto& server : servers) {
+ if (server.name() == name) {
+ // already exists, update
+ server.setPremium(isPremium);
+ server.updateLastSeen();
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ // new server
+ ServerInfo server(name, isPremium, QDate::currentDate(), isCDN ? 1 : 0, {});
+ servers.add(std::move(server));
+ }
}
m_OrganizerCore.settings().updateServers(servers);
diff --git a/src/organizercore.cpp b/src/organizercore.cpp
index 522d28be..ec13ca9c 100644
--- a/src/organizercore.cpp
+++ b/src/organizercore.cpp
@@ -276,7 +276,6 @@ OrganizerCore::OrganizerCore(Settings &settings)
, m_PluginListsWriter(std::bind(&OrganizerCore::savePluginList, this))
{
m_DownloadManager.setOutputDirectory(m_Settings.getDownloadDirectory());
- m_DownloadManager.setServers(m_Settings.getServers());
NexusInterface::instance(m_PluginContainer)->setCacheDirectory(m_Settings.getCacheDirectory());
diff --git a/src/serverinfo.cpp b/src/serverinfo.cpp
index 16e65f52..aece61da 100644
--- a/src/serverinfo.cpp
+++ b/src/serverinfo.cpp
@@ -3,17 +3,23 @@
using namespace MOBase;
+const std::size_t MaxDownloadCount = 5;
+
+
ServerInfo::ServerInfo()
- : ServerInfo({}, false, {}, 0, 0, 0.0)
+ : ServerInfo({}, false, {}, 0, {})
{
}
ServerInfo::ServerInfo(
QString name, bool premium, QDate last, int preferred,
- int count, double speed) :
+ SpeedList lastDownloads) :
m_name(std::move(name)), m_premium(premium), m_lastSeen(std::move(last)),
- m_preferred(preferred), m_downloadCount(count), m_downloadSpeed(speed)
+ m_preferred(preferred), m_lastDownloads(std::move(lastDownloads))
{
+ if (m_lastDownloads.size() > MaxDownloadCount) {
+ m_lastDownloads.resize(MaxDownloadCount);
+ }
}
const QString& ServerInfo::name() const
@@ -26,29 +32,77 @@ bool ServerInfo::isPremium() const
return m_premium;
}
+void ServerInfo::setPremium(bool b)
+{
+ m_premium = b;
+}
+
const QDate& ServerInfo::lastSeen() const
{
return m_lastSeen;
}
+void ServerInfo::updateLastSeen()
+{
+ m_lastSeen = QDate::currentDate();
+}
+
int ServerInfo::preferred() const
{
return m_preferred;
}
-int ServerInfo::downloadCount() const
+void ServerInfo::setPreferred(int i)
{
- return m_downloadCount;
+ m_preferred = i;
}
-double ServerInfo::downloadSpeed() const
+const ServerInfo::SpeedList& ServerInfo::lastDownloads() const
{
- return m_downloadSpeed;
+ return m_lastDownloads;
}
-void ServerInfo::setPreferred(int i)
+int ServerInfo::averageSpeed() const
{
- m_preferred = i;
+ int count = 0;
+ int total = 0;
+
+ for (const auto& s : m_lastDownloads) {
+ if (s > 0) {
+ ++count;
+ total += s;
+ }
+ }
+
+ if (count > 0) {
+ return static_cast<double>(total) / count;
+ }
+
+ return 0;
+}
+
+void ServerInfo::addDownload(int bytesPerSecond)
+{
+ if (bytesPerSecond <= 0) {
+ log::error(
+ "trying to add download with {} B/s to server '{}'; ignoring",
+ bytesPerSecond, m_name);
+
+ return;
+ }
+
+ if (m_lastDownloads.size() == MaxDownloadCount) {
+ std::rotate(
+ m_lastDownloads.begin(),
+ m_lastDownloads.begin() + 1,
+ m_lastDownloads.end());
+
+ m_lastDownloads.back() = bytesPerSecond;
+ } else {
+ m_lastDownloads.push_back(bytesPerSecond);
+ }
+
+ log::debug("added download at {} B/s to server '{}'", bytesPerSecond, m_name);
}
diff --git a/src/serverinfo.h b/src/serverinfo.h
index c6e3b640..af8f77c8 100644
--- a/src/serverinfo.h
+++ b/src/serverinfo.h
@@ -8,27 +8,34 @@
class ServerInfo
{
public:
+ using SpeedList = std::vector<int>;
+
ServerInfo();
ServerInfo(
QString name, bool premium, QDate lastSeen, int preferred,
- int downloadCount, double downloadSpeed);
+ SpeedList lastDownloads);
const QString& name() const;
+
bool isPremium() const;
+ void setPremium(bool b);
+
const QDate& lastSeen() const;
- int preferred() const;
- int downloadCount() const;
- double downloadSpeed() const;
+ void updateLastSeen();
+ int preferred() const;
void setPreferred(int i);
+ const SpeedList& lastDownloads() const;
+ int averageSpeed() const;
+ void addDownload(int bytesPerSecond);
+
private:
QString m_name;
bool m_premium;
QDate m_lastSeen;
int m_preferred;
- int m_downloadCount;
- double m_downloadSpeed;
+ SpeedList m_lastDownloads;
};
Q_DECLARE_METATYPE(ServerInfo)
@@ -37,7 +44,7 @@ Q_DECLARE_METATYPE(ServerInfo)
class ServerList
{
public:
- using container = QList<ServerInfo>;
+ using container = std::vector<ServerInfo>;
using iterator = container::iterator;
using const_iterator = container::const_iterator;
diff --git a/src/settings.cpp b/src/settings.cpp
index 3a7bda75..b11bc61c 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -850,21 +850,21 @@ void Settings::setLanguage(const QString& name)
m_Settings.setValue("Settings/language", name);
}
-void Settings::setDownloadSpeed(const QString &serverName, int bytesPerSecond)
+void Settings::setDownloadSpeed(const QString& name, int bytesPerSecond)
{
- m_Settings.beginGroup("Servers");
+ auto servers = getServers();
- for (const QString &serverKey : m_Settings.childKeys()) {
- QVariantMap data = m_Settings.value(serverKey).toMap();
- if (serverKey == serverName) {
- data["downloadCount"] = data["downloadCount"].toInt() + 1;
- data["downloadSpeed"] = data["downloadSpeed"].toDouble() + static_cast<double>(bytesPerSecond);
- m_Settings.setValue(serverKey, data);
+ for (auto& server : servers) {
+ if (server.name() == name) {
+ server.addDownload(bytesPerSecond);
+ updateServers(servers);
+ return;
}
}
- m_Settings.endGroup();
- m_Settings.sync();
+ log::error(
+ "server '{}' not found while trying to add a download with bps {}",
+ name, bytesPerSecond);
}
ServerList Settings::getServers() const
@@ -885,6 +885,7 @@ ServerList Settings::getServers() const
return getServersFromOldMap();
}
+ // post 2.2.1 format, array of values
ServerList list;
@@ -893,13 +894,22 @@ ServerList Settings::getServers() const
for (int i=0; i<size; ++i) {
m_Settings.setArrayIndex(i);
+ 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);
+ }
+ }
+
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(),
- m_Settings.value("downloadCount").toInt(),
- m_Settings.value("downloadSpeed").toDouble());
+ lastDownloads);
list.add(std::move(server));
}
@@ -925,8 +935,10 @@ ServerList Settings::getServersFromOldMap() const
data["premium"].toBool(),
data["lastSeen"].toDate(),
data["preferred"].toInt(),
- data["downloadCount"].toInt(),
- data["downloadSpeed"].toDouble());
+ {});
+
+ // ignoring download count and speed, it's now a list of values instead of
+ // a total
list.add(std::move(server));
}
@@ -955,8 +967,15 @@ void Settings::updateServers(ServerList servers)
m_Settings.setValue("premium", server.isPremium());
m_Settings.setValue("lastSeen", server.lastSeen().toString(Qt::ISODate));
m_Settings.setValue("preferred", server.preferred());
- m_Settings.setValue("downloadCount", server.downloadCount());
- m_Settings.setValue("downloadSpeed", server.downloadSpeed());
+
+ QString lastDownloads;
+ for (const auto& speed : server.lastDownloads()) {
+ if (speed > 0) {
+ lastDownloads += QString("%1 ").arg(speed);
+ }
+ }
+
+ m_Settings.setValue("lastDownloads", lastDownloads.trimmed());
++i;
}
diff --git a/src/settingsdialognexus.cpp b/src/settingsdialognexus.cpp
index 926ea9a6..f2bd3ab5 100644
--- a/src/settingsdialognexus.cpp
+++ b/src/settingsdialognexus.cpp
@@ -87,9 +87,9 @@ NexusSettingsTab::NexusSettingsTab(Settings& s, SettingsDialog& d)
descriptor += QStringLiteral(" (automatic)");
}
- if (server.downloadSpeed() > 0 && server.downloadCount() > 0) {
- const int bps = static_cast<int>(server.downloadSpeed() / server.downloadCount());
- descriptor += QString(" (%1 kbps)").arg(bps / 1024);
+ const auto averageSpeed = server.averageSpeed();
+ if (averageSpeed > 0) {
+ descriptor += QString(" (%1 kbps)").arg(averageSpeed / 1024);
}
QListWidgetItem *newItem = new ServerItem<int>(descriptor, Qt::UserRole + 1);