feat: 初步实现保存配置文件

This commit is contained in:
刘旭 2022-05-25 10:02:16 +08:00
commit ba65e286ff
6 changed files with 54 additions and 5 deletions

View file

@ -7,6 +7,8 @@
#include "Utils.h"
#include "StrUtils.h"
#include "Logger.h"
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
using namespace winrt;
using namespace Windows::Foundation;
@ -60,8 +62,23 @@ bool Settings::Initialize() {
return true;
}
void Settings::Save() {
bool Settings::Save() {
std::wstring configPath = GetConfigPath(_isPortableMode);
if (!Utils::CreateDirRecursive(configPath.substr(0, configPath.find_last_of(L'\\')))) {
Logger::Get().Error("创建配置文件路径失败");
return false;
}
rapidjson::StringBuffer json;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(json);
writer.StartObject();
writer.Key("theme");
writer.Uint(_theme);
writer.EndObject();
Utils::WriteTextFile(configPath.c_str(), json.GetString());
return true;
}
}

View file

@ -17,7 +17,7 @@ struct Settings : SettingsT<Settings> {
_isPortableMode = value;
}
void Save();
bool Save();
private:
bool _isPortableMode = false;

View file

@ -6,7 +6,7 @@ namespace Magpie.App
Settings();
Boolean Initialize();
void Save();
Boolean Save();
Boolean IsPortableMode;
}

View file

@ -151,6 +151,8 @@ int XamlApp::Run() {
DispatchMessage(&msg);
}
_uwpApp.OnClose();
_xamlSourceNative2 = nullptr;
_xamlSource.Close();
_xamlSource = nullptr;
@ -224,7 +226,6 @@ LRESULT XamlApp::_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
break;
}
case WM_DESTROY:
_uwpApp.OnClose();
PostQuitMessage(0);
return 0;
}

View file

@ -209,6 +209,35 @@ RTL_OSVERSIONINFOW _GetOSVersion() noexcept {
return version;
}
bool Utils::CreateDirRecursive(const std::wstring& path) {
if (DirExists(path.c_str())) {
return true;
}
if (path.empty()) {
return false;
}
size_t searchOffset = 0;
do {
auto tokenPos = path.find_first_of(L"\\/", searchOffset);
// treat the entire path as a folder if no folder separator not found
if (tokenPos == std::wstring::npos) {
tokenPos = path.size();
}
std::wstring subdir = path.substr(0, tokenPos);
if (!subdir.empty() && !DirExists(subdir.c_str()) && !CreateDirectory(subdir.c_str(), nullptr)) {
Logger::Get().Win32Error(StrUtils::Concat("创建文件夹", StrUtils::UTF16ToUTF8(subdir), "失败"));
return false; // return error if failed creating dir
}
searchOffset = tokenPos + 1;
} while (searchOffset < path.size());
return true;
}
const RTL_OSVERSIONINFOW& Utils::GetOSVersion() noexcept {
static RTL_OSVERSIONINFOW version = _GetOSVersion();
return version;

View file

@ -54,6 +54,8 @@ struct Utils {
DWORD attrs = GetFileAttributes(fileName);
return (attrs != INVALID_FILE_ATTRIBUTES) && (attrs & FILE_ATTRIBUTE_DIRECTORY);
}
static bool CreateDirRecursive(const std::wstring& path);
static const RTL_OSVERSIONINFOW& GetOSVersion() noexcept;