Files
FWS-PacManage/src/history-manager.cpp
T
BlackAngelTV 5054f04c54 refactor: replace plugin system with native pacman manager
Remove the dynamic plugin loader (pacman/apt/pkg/universal) and drive
the UI directly through PacmanManager. Rename sources and headers to
kebab-case to match the project's file naming convention.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-08-27 19:29:24 +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);
}
}