diff --git a/include/HistoryManager.h b/include/HistoryManager.h new file mode 100644 index 0000000..4850ff4 --- /dev/null +++ b/include/HistoryManager.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +struct HistoryEntry { + std::string timestamp; + std::string action; // "install", "remove", "update" + std::string package; + std::string plugin; + bool success; +}; + +class HistoryManager { +public: + HistoryManager(const std::string& historyFile = "~/.local/share/PackageMapper/history.csv"); + ~HistoryManager(); + + void record(const HistoryEntry& entry); + const std::vector& getHistory() const; + void clear(); + void save(); + void load(); + +private: + std::string m_historyFile; + std::vector m_history; + + std::string expandPath(const std::string& path); + std::string getCurrentTimestamp(); +}; diff --git a/src/HistoryManager.cpp b/src/HistoryManager.cpp new file mode 100644 index 0000000..1eb150c --- /dev/null +++ b/src/HistoryManager.cpp @@ -0,0 +1,98 @@ +#include "HistoryManager.h" +#include +#include +#include + +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& 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); + } +}