Files
FWS-PacManage/history/history-manager.cpp
T
BlackAngelTV 2395ba3f17 refactor(tree): give the history, the archive and the self-update a folder
Three self-contained things shared src/ with the rest: the operation
history, the zip reader behind the .fwspacm installer, and the
self-update. Each moves to its own folder — history/, archive/ and
update/ — so a change to one does not have to reason about the others.
2026-09-29 21:25:03 +02:00

99 lines
2.4 KiB
C++

#include "history-manager.h"
#include <filesystem>
#include <cstring>
#include <iomanip>
namespace fs = std::filesystem;
HistoryManager::HistoryManager(const std::string& historyFile)
: m_historyFile(expandPath(historyFile)) {
load();
}
HistoryManager::~HistoryManager() {
save();
}
std::string HistoryManager::expandPath(const std::string& path) {
if (path.empty()) return path;
std::string result = path;
if (result[0] == '~') {
const char* home = getenv("HOME");
if (home) {
result = std::string(home) + result.substr(1);
}
}
// Ensure directory exists
auto dir = fs::path(result).parent_path();
if (!fs::exists(dir)) {
fs::create_directories(dir);
}
return result;
}
std::string HistoryManager::getCurrentTimestamp() {
auto now = std::time(nullptr);
auto* tm = std::localtime(&now);
char buffer[64];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm);
return std::string(buffer);
}
void HistoryManager::record(const HistoryEntry& entry) {
HistoryEntry e = entry;
if (e.timestamp.empty()) {
e.timestamp = getCurrentTimestamp();
}
m_history.push_back(e);
save();
}
const std::vector<HistoryEntry>& HistoryManager::getHistory() const {
return m_history;
}
void HistoryManager::clear() {
m_history.clear();
save();
}
void HistoryManager::save() {
std::ofstream file(m_historyFile);
if (!file.is_open()) return;
file << "timestamp,action,package,plugin,success\n";
for (const auto& entry : m_history) {
file << entry.timestamp << ","
<< entry.action << ","
<< entry.package << ","
<< entry.plugin << ","
<< (entry.success ? "1" : "0") << "\n";
}
}
void HistoryManager::load() {
std::ifstream file(m_historyFile);
if (!file.is_open()) return;
std::string line;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
std::istringstream ss(line);
HistoryEntry entry;
std::string successStr;
std::getline(ss, entry.timestamp, ',');
std::getline(ss, entry.action, ',');
std::getline(ss, entry.package, ',');
std::getline(ss, entry.plugin, ',');
std::getline(ss, successStr, ',');
entry.success = (successStr == "1");
m_history.push_back(entry);
}
}