LCOV - code coverage report
Current view: top level - src - session_manager.h (source / functions) Coverage Total Hit
Test: merged.info Lines: 95.2 % 42 40
Test Date: 2026-06-03 09:13:19 Functions: 100.0 % 8 8

            Line data    Source code
       1              : #pragma once
       2              : #include <string>
       3              : #include <fstream>
       4              : #include <chrono>
       5              : #include <filesystem>
       6              : #include <SDL.h>
       7              : #include <nlohmann/json.hpp>
       8              : 
       9              : namespace fs = std::filesystem;
      10              : 
      11              : namespace Amplitron {
      12              : 
      13              :     class SessionManager {
      14              :     private:
      15              :         fs::path autoSavePath;
      16              :         fs::path tempSavePath;
      17              :         std::chrono::steady_clock::time_point lastSaveTime;
      18            3 :         const int autoSaveIntervalSeconds = 30;
      19              : 
      20              :     public:
      21           12 :         SessionManager(const std::string& orgName, const std::string& appName) 
      22            9 :             : lastSaveTime(std::chrono::steady_clock::now()) 
      23            3 :         {
      24            9 :             char* prefPath = SDL_GetPrefPath(orgName.c_str(), appName.c_str());
      25            9 :             if (prefPath) {
      26            9 :                 std::string basePath(prefPath);
      27            9 :                 SDL_free(prefPath);
      28              :                 
      29           15 :                 autoSavePath = fs::path(basePath) / "autosave.json";
      30           18 :                 tempSavePath = fs::path(basePath) / "autosave.tmp";
      31            9 :             } else {
      32            0 :                 autoSavePath = "autosave.json";
      33            0 :                 tempSavePath = "autosave.tmp";
      34              :             }
      35           12 :         }
      36              : 
      37            8 :         bool hasUnsavedSession() const {
      38            9 :             return fs::exists(autoSavePath);
      39              :         }
      40              : 
      41            3 :         bool shouldSave() const {
      42            3 :             auto now = std::chrono::steady_clock::now();
      43            3 :             return std::chrono::duration_cast<std::chrono::seconds>(now - lastSaveTime).count() >= autoSaveIntervalSeconds;
      44              :         }
      45              : 
      46            3 :         void saveSession(const nlohmann::json& state) {
      47            3 :             std::ofstream tempFile(tempSavePath);
      48            3 :             if (tempFile.is_open()) {
      49            4 :                 tempFile << state.dump(4);
      50            3 :                 tempFile.close();
      51              : 
      52            3 :                 std::error_code ec;
      53            3 :                 fs::rename(tempSavePath, autoSavePath, ec);
      54            1 :             }
      55            3 :             lastSaveTime = std::chrono::steady_clock::now();
      56            3 :         }
      57              : 
      58            6 :         nlohmann::json loadSession() {
      59            6 :             nlohmann::json state;
      60            6 :             std::ifstream file(autoSavePath);
      61            6 :             if (file.is_open()) {
      62            3 :                 file >> state;
      63            1 :             }
      64            8 :             return state;
      65            6 :         }
      66              : 
      67           12 :         void clearSession() {
      68           12 :             std::error_code ec;
      69           12 :             if (fs::exists(autoSavePath)) fs::remove(autoSavePath, ec);
      70           12 :             if (fs::exists(tempSavePath)) fs::remove(tempSavePath, ec);
      71           12 :         }
      72              :     };
      73              : 
      74              : }
        

Generated by: LCOV version 2.0-1