summaryrefslogtreecommitdiff
path: root/src/managers
diff options
context:
space:
mode:
authorVaxry <43317083+vaxerski@users.noreply.github.com>2025-01-10 19:09:40 +0100
committerGitHub <noreply@github.com>2025-01-10 18:09:40 +0000
commitb5fb6110ab1f75440b4b90d09f95d304d8c2080b (patch)
treedd1d01b42da490b8e0925b1d913744739d79f9a8 /src/managers
parentda9252a23e69fc4d34beca73ebcc90b03af0ed2a (diff)
core: Add a periodic donation request (#8981)
Will fire once in december and july. Disableable with `ecosystem:no_donation:nag`
Diffstat (limited to 'src/managers')
-rw-r--r--src/managers/DonationNagManager.cpp114
-rw-r--r--src/managers/DonationNagManager.hpp16
-rw-r--r--src/managers/VersionKeeperManager.cpp92
-rw-r--r--src/managers/VersionKeeperManager.hpp11
4 files changed, 147 insertions, 86 deletions
diff --git a/src/managers/DonationNagManager.cpp b/src/managers/DonationNagManager.cpp
new file mode 100644
index 00000000..d7eab9ae
--- /dev/null
+++ b/src/managers/DonationNagManager.cpp
@@ -0,0 +1,114 @@
+#include "DonationNagManager.hpp"
+#include "../debug/Log.hpp"
+#include "VersionKeeperManager.hpp"
+#include "eventLoop/EventLoopManager.hpp"
+#include "../config/ConfigValue.hpp"
+
+#include <chrono>
+#include <format>
+
+#include "../helpers/fs/FsUtils.hpp"
+
+#include <hyprutils/os/Process.hpp>
+using namespace Hyprutils::OS;
+
+constexpr const char* LAST_NAG_FILE_NAME = "lastNag";
+constexpr uint64_t DAY_IN_SECONDS = 3600ULL * 24;
+constexpr uint64_t MONTH_IN_SECONDS = DAY_IN_SECONDS * 30;
+
+struct SNagDatePoint {
+ // Counted from 1, as in Jan 1st is 1, 1
+ // No month-boundaries because I am lazy
+ uint8_t month = 0, dayStart = 0, dayEnd = 0;
+};
+
+// clang-format off
+const std::vector<SNagDatePoint> NAG_DATE_POINTS = {
+ SNagDatePoint {
+ 7, 20, 31,
+ },
+ SNagDatePoint {
+ 12, 1, 28
+ },
+};
+// clang-format on
+
+CDonationNagManager::CDonationNagManager() {
+ static auto PNONAG = CConfigValue<Hyprlang::INT>("ecosystem:no_donation_nag");
+
+ if (g_pVersionKeeperMgr->fired() || *PNONAG)
+ return;
+
+ const auto DATAROOT = NFsUtils::getDataHome();
+
+ if (!DATAROOT)
+ return;
+
+ const auto EPOCH = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+
+ const auto LASTNAGSTR = NFsUtils::readFileAsString(*DATAROOT + "/" + LAST_NAG_FILE_NAME);
+
+ if (!LASTNAGSTR) {
+ const auto EPOCHSTR = std::format("{}", EPOCH);
+ NFsUtils::writeToFile(*DATAROOT + "/" + LAST_NAG_FILE_NAME, EPOCHSTR);
+ return;
+ }
+
+ uint64_t LAST_EPOCH = 0;
+
+ try {
+ LAST_EPOCH = std::stoull(*LASTNAGSTR);
+ } catch (std::exception& e) {
+ Debug::log(ERR, "DonationNag: Last epoch invalid? Failed to parse \"{}\". Setting to today.", *LASTNAGSTR);
+ const auto EPOCHSTR = std::format("{}", EPOCH);
+ NFsUtils::writeToFile(*DATAROOT + "/" + LAST_NAG_FILE_NAME, EPOCHSTR);
+ return;
+ }
+
+ // don't nag if the last nag was less than a month ago. This is
+ // mostly for first-time nags, as other nags happen in specific time frames shorter than a month
+ if (EPOCH - LAST_EPOCH < MONTH_IN_SECONDS) {
+ Debug::log(LOG, "DonationNag: last nag was {} days ago, too early for a nag.", (int)std::round((EPOCH - LAST_EPOCH) / (double)MONTH_IN_SECONDS));
+ return;
+ }
+
+ if (!NFsUtils::executableExistsInPath("hyprland-donate-screen")) {
+ Debug::log(ERR, "DonationNag: executable doesn't exist, skipping.");
+ return;
+ }
+
+ auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
+ auto local = *localtime(&tt);
+
+ const auto MONTH = local.tm_mon + 1;
+ const auto DAY = local.tm_mday;
+
+ for (const auto& nagPoint : NAG_DATE_POINTS) {
+ if (MONTH != nagPoint.month)
+ continue;
+
+ if (DAY < nagPoint.dayStart || DAY > nagPoint.dayEnd)
+ continue;
+
+ Debug::log(LOG, "DonationNag: hit nag month {} days {}-{}, it's {} today, nagging", MONTH, nagPoint.dayStart, nagPoint.dayEnd, DAY);
+
+ m_bFired = true;
+
+ const auto EPOCHSTR = std::format("{}", EPOCH);
+ NFsUtils::writeToFile(*DATAROOT + "/" + LAST_NAG_FILE_NAME, EPOCHSTR);
+
+ g_pEventLoopManager->doLater([] {
+ CProcess proc("hyprland-donate-screen", {});
+ proc.runAsync();
+ });
+
+ break;
+ }
+
+ if (!m_bFired)
+ Debug::log(LOG, "DonationNag: didn't hit any nagging periods");
+}
+
+bool CDonationNagManager::fired() {
+ return m_bFired;
+} \ No newline at end of file
diff --git a/src/managers/DonationNagManager.hpp b/src/managers/DonationNagManager.hpp
new file mode 100644
index 00000000..e296d815
--- /dev/null
+++ b/src/managers/DonationNagManager.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <memory>
+
+class CDonationNagManager {
+ public:
+ CDonationNagManager();
+
+ // whether the donation nag was shown this boot.
+ bool fired();
+
+ private:
+ bool m_bFired = false;
+};
+
+inline std::unique_ptr<CDonationNagManager> g_pDonationNagManager; \ No newline at end of file
diff --git a/src/managers/VersionKeeperManager.cpp b/src/managers/VersionKeeperManager.cpp
index 1ef1040e..cc03a7b9 100644
--- a/src/managers/VersionKeeperManager.cpp
+++ b/src/managers/VersionKeeperManager.cpp
@@ -6,6 +6,7 @@
#include "../helpers/varlist/VarList.hpp"
#include "eventLoop/EventLoopManager.hpp"
#include "../config/ConfigValue.hpp"
+#include "../helpers/fs/FsUtils.hpp"
#include <filesystem>
#include <fstream>
@@ -20,12 +21,12 @@ constexpr const char* VERSION_FILE_NAME = "lastVersion";
CVersionKeeperManager::CVersionKeeperManager() {
static auto PNONOTIFY = CConfigValue<Hyprlang::INT>("ecosystem:no_update_news");
- const auto DATAROOT = getDataHome();
+ const auto DATAROOT = NFsUtils::getDataHome();
if (!DATAROOT)
return;
- const auto LASTVER = getDataLastVersion(*DATAROOT);
+ const auto LASTVER = NFsUtils::readFileAsString(*DATAROOT + "/" + VERSION_FILE_NAME);
if (!LASTVER)
return;
@@ -35,101 +36,26 @@ CVersionKeeperManager::CVersionKeeperManager() {
return;
}
- writeVersionToVersionFile(*DATAROOT);
+ NFsUtils::writeToFile(*DATAROOT + "/" + VERSION_FILE_NAME, HYPRLAND_VERSION);
if (*PNONOTIFY) {
Debug::log(LOG, "CVersionKeeperManager: updated, but update news is disabled in the config :(");
return;
}
- if (!executableExistsInPath("hyprland-update-screen")) {
+ if (!NFsUtils::executableExistsInPath("hyprland-update-screen")) {
Debug::log(ERR, "CVersionKeeperManager: hyprland-update-screen doesn't seem to exist, skipping notif about update...");
return;
}
+ m_bFired = true;
+
g_pEventLoopManager->doLater([]() {
CProcess proc("hyprland-update-screen", {"--new-version", HYPRLAND_VERSION});
proc.runAsync();
});
}
-std::optional<std::string> CVersionKeeperManager::getDataHome() {
- const auto DATA_HOME = getenv("XDG_DATA_HOME");
-
- std::string dataRoot;
-
- if (!DATA_HOME) {
- const auto HOME = getenv("HOME");
-
- if (!HOME) {
- Debug::log(ERR, "CVersionKeeperManager: can't get data home: no $HOME or $XDG_DATA_HOME");
- return std::nullopt;
- }
-
- dataRoot = HOME + std::string{"/.local/share/"};
- } else
- dataRoot = DATA_HOME + std::string{"/"};
-
- std::error_code ec;
- if (!std::filesystem::exists(dataRoot, ec) || ec) {
- Debug::log(ERR, "CVersionKeeperManager: can't get data home: inaccessible / missing");
- return std::nullopt;
- }
-
- dataRoot += "hyprland/";
-
- if (!std::filesystem::exists(dataRoot, ec) || ec) {
- Debug::log(LOG, "CVersionKeeperManager: no hyprland data home, creating.");
- std::filesystem::create_directory(dataRoot, ec);
- if (ec) {
- Debug::log(ERR, "CVersionKeeperManager: can't create new data home for hyprland");
- return std::nullopt;
- }
- std::filesystem::permissions(dataRoot, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write | std::filesystem::perms::owner_exec, ec);
- if (ec)
- Debug::log(WARN, "CVersionKeeperManager: couldn't set perms on hyprland data store. Proceeding anyways.");
- }
-
- if (!std::filesystem::exists(dataRoot, ec) || ec) {
- Debug::log(ERR, "CVersionKeeperManager: no hyprland data home, failed to create.");
- return std::nullopt;
- }
-
- return dataRoot;
-}
-
-std::optional<std::string> CVersionKeeperManager::getDataLastVersion(const std::string& dataRoot) {
- std::error_code ec;
- std::string lastVerFile = dataRoot + "/" + VERSION_FILE_NAME;
-
- if (!std::filesystem::exists(lastVerFile, ec) || ec) {
- Debug::log(LOG, "CVersionKeeperManager: no hyprland last version file, creating.");
- writeVersionToVersionFile(dataRoot);
-
- return "0.0.0";
- }
-
- std::ifstream file(lastVerFile);
- if (!file.good()) {
- Debug::log(ERR, "CVersionKeeperManager: couldn't open an ifstream for reading the version file.");
- return std::nullopt;
- }
-
- return trim(std::string((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>())));
-}
-
-void CVersionKeeperManager::writeVersionToVersionFile(const std::string& dataRoot) {
- std::string lastVerFile = dataRoot + "/" + VERSION_FILE_NAME;
- std::ofstream of(lastVerFile, std::ios::trunc);
- if (!of.good()) {
- Debug::log(ERR, "CVersionKeeperManager: couldn't open an ofstream for writing the version file.");
- return;
- }
-
- of << HYPRLAND_VERSION;
- of.close();
-}
-
bool CVersionKeeperManager::isVersionOlderThanRunning(const std::string& ver) {
const CVarList verStrings(ver, 0, '.', true);
@@ -151,3 +77,7 @@ bool CVersionKeeperManager::isVersionOlderThanRunning(const std::string& ver) {
return true;
return false;
}
+
+bool CVersionKeeperManager::fired() {
+ return m_bFired;
+}
diff --git a/src/managers/VersionKeeperManager.hpp b/src/managers/VersionKeeperManager.hpp
index f0dc05ce..eb404d88 100644
--- a/src/managers/VersionKeeperManager.hpp
+++ b/src/managers/VersionKeeperManager.hpp
@@ -1,17 +1,18 @@
#pragma once
#include <memory>
-#include <optional>
class CVersionKeeperManager {
public:
CVersionKeeperManager();
+ // whether the update screen was shown this boot.
+ bool fired();
+
private:
- std::optional<std::string> getDataHome();
- std::optional<std::string> getDataLastVersion(const std::string& dataRoot);
- void writeVersionToVersionFile(const std::string& dataRoot);
- bool isVersionOlderThanRunning(const std::string& ver);
+ bool isVersionOlderThanRunning(const std::string& ver);
+
+ bool m_bFired = false;
};
inline std::unique_ptr<CVersionKeeperManager> g_pVersionKeeperMgr; \ No newline at end of file