From 1b2ef4240d01839d4fc9e4232bf5294baaae0f80 Mon Sep 17 00:00:00 2001
From: Grayson Riffe <grayson@graysonriffe.com>
Date: Tue, 21 Jan 2025 04:16:51 -0600
Subject: [PATCH] Remember last time file

---
 Mainspring/src/Application.cpp | 47 +++++++++++++++++++++++-----------
 Mainspring/src/Application.h   |  4 ++-
 Mainspring/src/pch.h           |  4 ++-
 Mainspring/src/utility.cpp     |  4 +++
 4 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/Mainspring/src/Application.cpp b/Mainspring/src/Application.cpp
index a57f46d..36699ea 100644
--- a/Mainspring/src/Application.cpp
+++ b/Mainspring/src/Application.cpp
@@ -32,7 +32,10 @@ namespace mainspring {
     }
 
     void Application::run() {
-        // Read last time here
+        std::string configPath = getConfigPath();
+        std::string recentFile;
+        if (readFile(configPath.c_str(), recentFile) && !recentFile.empty())
+            open(recentFile);
 
         m_dlg = CreateDialogParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast<DLGPROC>(mainDlgProc), reinterpret_cast<LPARAM>(this));
 
@@ -174,13 +177,17 @@ namespace mainspring {
                 return TRUE;
             }
 
-            case WM_CLOSE:
+            case WM_CLOSE: {
                 if (!app->m_file.empty())
                     app->save();
 
+                std::string configPath = app->getConfigPath();
+                writeFile(configPath.c_str(), app->m_file);
+
                 KillTimer(dlg, 1);
                 DestroyWindow(dlg);
                 return TRUE;
+            }
 
             case WM_DESTROY:
                 PostQuitMessage(0);
@@ -190,6 +197,12 @@ namespace mainspring {
         return FALSE;
     }
 
+    std::string Application::getConfigPath() {
+        char appDataPath[MAX_PATH] = {};
+        SHGetFolderPath(nullptr, CSIDL_APPDATA, nullptr, NULL, appDataPath);
+        return std::string(appDataPath) + "\\" + m_appName + "\\last";
+    }
+
     Seconds Application::getElapsed() {
         if (m_timing)
             return Duration(Clock::now() - m_startTime).count();
@@ -228,21 +241,25 @@ namespace mainspring {
         SetTimer(m_dlg, IDT_SAVEDTEXT, 3000, nullptr);
     }
 
-    void Application::open() {
-        OPENFILENAME ofn = {};
-        ofn.lStructSize = sizeof(ofn);
-        ofn.hwndOwner = m_dlg;
-        ofn.lpstrFilter = "Mainspring Time File (*.mspring)\0*.mspring\0\0";
-        ofn.lpstrDefExt = "mspring";
-        char fileBuf[MAX_PATH] = {};
-        ofn.lpstrFile = fileBuf;
-        ofn.nMaxFile = MAX_PATH;
-        ofn.Flags = OFN_FILEMUSTEXIST;
+    void Application::open(std::string path) {
+        if (path.empty()) {
+            OPENFILENAME ofn = {};
+            ofn.lStructSize = sizeof(ofn);
+            ofn.hwndOwner = m_dlg;
+            ofn.lpstrFilter = "Mainspring Time File (*.mspring)\0*.mspring\0\0";
+            ofn.lpstrDefExt = "mspring";
+            char fileBuf[MAX_PATH] = {};
+            ofn.lpstrFile = fileBuf;
+            ofn.nMaxFile = MAX_PATH;
+            ofn.Flags = OFN_FILEMUSTEXIST;
 
-        if (GetOpenFileName(&ofn))
-            m_file = fileBuf;
+            if (GetOpenFileName(&ofn))
+                m_file = fileBuf;
+            else
+                return;
+        }
         else
-            return;
+            m_file = path;
 
         std::string openData;
         if (readFile(m_file.c_str(), openData))
diff --git a/Mainspring/src/Application.h b/Mainspring/src/Application.h
index a9f434e..b5f6d72 100644
--- a/Mainspring/src/Application.h
+++ b/Mainspring/src/Application.h
@@ -15,11 +15,13 @@ namespace mainspring {
     private:
         static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
 
+        std::string getConfigPath();
+
         Seconds getElapsed(); // Gets the current timing session time
 
         void reset();
         void save(bool saveas = false);
-        void open();
+        void open(std::string path = "");
 
         const char* m_appName;
         const char* m_appVersion;
diff --git a/Mainspring/src/pch.h b/Mainspring/src/pch.h
index 861d084..56a0f7f 100644
--- a/Mainspring/src/pch.h
+++ b/Mainspring/src/pch.h
@@ -3,6 +3,7 @@
 // IO and strings
 #include <iostream>
 #include <fstream>
+#include <filesystem>
 #include <string>
 #include <format>
 
@@ -12,4 +13,5 @@
 // Windows
 #define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
-#include <commdlg.h>
\ No newline at end of file
+#include <commdlg.h>
+#include <ShlObj.h>
\ No newline at end of file
diff --git a/Mainspring/src/utility.cpp b/Mainspring/src/utility.cpp
index bc3bc60..96efd08 100644
--- a/Mainspring/src/utility.cpp
+++ b/Mainspring/src/utility.cpp
@@ -16,6 +16,10 @@ namespace mainspring {
     }
 
     bool writeFile(const char* filename, const std::string& in) {
+        std::string folder = filename;
+        folder.resize(folder.find_last_of('\\'));
+        if (!std::filesystem::exists(folder))
+            std::filesystem::create_directories(folder);
         std::ofstream fileStream(filename, std::ios::binary | std::ios::trunc);
         if (!fileStream.is_open())
             return false;