mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
[feat] add PathManager class in order to manage config/log/cache file path
This commit is contained in:
77
src/path_manager/path_manager.cpp
Normal file
77
src/path_manager/path_manager.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "path_manager.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
PathManager::PathManager(const std::string& app_name) : app_name_(app_name) {}
|
||||
|
||||
std::filesystem::path PathManager::GetConfigPath() {
|
||||
#ifdef _WIN32
|
||||
return GetKnownFolder(FOLDERID_RoamingAppData) / app_name_;
|
||||
#elif __APPLE__
|
||||
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") / app_name_;
|
||||
#else
|
||||
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") / app_name_;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path PathManager::GetCachePath() {
|
||||
#ifdef _WIN32
|
||||
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "Cache";
|
||||
#elif __APPLE__
|
||||
return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_;
|
||||
#else
|
||||
return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path PathManager::GetLogPath() {
|
||||
#ifdef _WIN32
|
||||
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "Logs";
|
||||
#elif __APPLE__
|
||||
return GetHome() + "/Library/Logs/" + app_name_;
|
||||
#else
|
||||
return GetCachePath() / "logs";
|
||||
#endif
|
||||
}
|
||||
|
||||
bool PathManager::CreateDirectories(const std::filesystem::path& p) {
|
||||
std::error_code ec;
|
||||
bool created = std::filesystem::create_directories(p, ec);
|
||||
if (ec) {
|
||||
return false;
|
||||
}
|
||||
return created || std::filesystem::exists(p);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
std::filesystem::path PathManager::GetKnownFolder(REFKNOWNFOLDERID id) {
|
||||
PWSTR path = NULL;
|
||||
if (SUCCEEDED(SHGetKnownFolderPath(id, 0, NULL, &path))) {
|
||||
std::wstring wpath(path);
|
||||
CoTaskMemFree(path);
|
||||
return std::filesystem::path(wpath);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string PathManager::GetHome() {
|
||||
if (const char* home = getenv("HOME")) {
|
||||
return std::string(home);
|
||||
}
|
||||
#ifdef _WIN32
|
||||
char path[MAX_PATH];
|
||||
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path)))
|
||||
return std::string(path);
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
std::filesystem::path PathManager::GetEnvOrDefault(const char* env_var,
|
||||
const std::string& def) {
|
||||
if (const char* val = getenv(env_var)) {
|
||||
return std::filesystem::path(val);
|
||||
}
|
||||
|
||||
return std::filesystem::path(def);
|
||||
}
|
||||
42
src/path_manager/path_manager.h
Normal file
42
src/path_manager/path_manager.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2025-07-16
|
||||
* Copyright (c) 2025 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _PATH_MANAGER_H_
|
||||
#define _PATH_MANAGER_H_
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#ifdef _WIN32
|
||||
#include <shlobj.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
class PathManager {
|
||||
public:
|
||||
explicit PathManager(const std::string& app_name);
|
||||
|
||||
std::filesystem::path GetConfigPath();
|
||||
|
||||
std::filesystem::path GetCachePath();
|
||||
|
||||
std::filesystem::path GetLogPath();
|
||||
|
||||
bool CreateDirectories(const std::filesystem::path& p);
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
std::filesystem::path GetKnownFolder(REFKNOWNFOLDERID id);
|
||||
#endif
|
||||
|
||||
std::string GetHome();
|
||||
std::filesystem::path GetEnvOrDefault(const char* env_var,
|
||||
const std::string& def);
|
||||
|
||||
private:
|
||||
std::string app_name_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -60,6 +60,13 @@ target("common")
|
||||
add_files("src/common/*.cpp")
|
||||
add_includedirs("src/common", {public = true})
|
||||
|
||||
target("path_manager")
|
||||
set_kind("object")
|
||||
add_deps("rd_log")
|
||||
add_includedirs("src/path_manager", {public = true})
|
||||
add_files("src/path_manager/*.cpp")
|
||||
add_includedirs("src/path_manager", {public = true})
|
||||
|
||||
target("screen_capturer")
|
||||
set_kind("object")
|
||||
add_deps("rd_log", "common")
|
||||
@@ -130,7 +137,7 @@ target("single_window")
|
||||
set_kind("object")
|
||||
add_packages("libyuv", "openssl3")
|
||||
add_deps("rd_log", "common", "localization", "config_center", "minirtc",
|
||||
"screen_capturer", "speaker_capturer", "device_controller")
|
||||
"path_manager", "screen_capturer", "speaker_capturer", "device_controller")
|
||||
add_files("src/single_window/*.cpp")
|
||||
add_includedirs("src/single_window", {public = true})
|
||||
add_includedirs("fonts", {public = true})
|
||||
|
||||
Reference in New Issue
Block a user