From 72cc7e3d89acb3b642e33479b0abc05d07d15bd8 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 29 Mar 2018 14:01:07 -0400 Subject: [PATCH] link gui with common; reimplement finding personal and profile directories on windows (not yet used) --- src/gui/CMakeLists.txt | 2 + src/lib/common/CMakeLists.txt | 11 +++++ src/lib/common/DataDirectories.h | 7 +++ src/lib/common/win32/DataDirectories.cpp | 54 ++++++++++++++++++++++++ src/lib/common/win32/DataDirectories.h | 20 +++++++++ 5 files changed, 94 insertions(+) create mode 100644 src/lib/common/DataDirectories.h create mode 100644 src/lib/common/win32/DataDirectories.cpp create mode 100644 src/lib/common/win32/DataDirectories.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 5d248912..9c902867 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -45,6 +45,8 @@ if (HAVE_X11) target_link_libraries (barrier X11) endif() +target_link_libraries (barrier common) + if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") install (TARGETS barrier DESTINATION ${BARRIER_BUNDLE_BINARY_DIR}) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux") diff --git a/src/lib/common/CMakeLists.txt b/src/lib/common/CMakeLists.txt index 56d9fc96..eda7fa1a 100644 --- a/src/lib/common/CMakeLists.txt +++ b/src/lib/common/CMakeLists.txt @@ -17,6 +17,17 @@ file(GLOB headers "*.h") file(GLOB sources "*.cpp") +if (WIN32) + file(GLOB arch_headers "win32/*.h") + file(GLOB arch_sources "win32/*.cpp") +elseif (UNIX) + file(GLOB arch_headers "unix/*.h") + file(GLOB arch_sources "unix/*.cpp") +endif() + +list(APPEND headers ${arch_headers}) +list(APPEND sources ${arch_sources}) + if (BARRIER_ADD_HEADERS) list(APPEND sources ${headers}) endif() diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h new file mode 100644 index 00000000..fad4b559 --- /dev/null +++ b/src/lib/common/DataDirectories.h @@ -0,0 +1,7 @@ +#pragma once + +#ifdef _WIN32 +#include "win32/DataDirectories.h" +#else +#include "unix/DataDirectories.h" +#endif diff --git a/src/lib/common/win32/DataDirectories.cpp b/src/lib/common/win32/DataDirectories.cpp new file mode 100644 index 00000000..832fd821 --- /dev/null +++ b/src/lib/common/win32/DataDirectories.cpp @@ -0,0 +1,54 @@ +#include "DataDirectories.h" + +#include + +// static member +std::string DataDirectories::_personal; +std::string DataDirectories::_profile; + +std::string unicode_to_mb(const WCHAR* utfStr) +{ + int utfLength = lstrlenW(utfStr); + int mbLength = WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, NULL, 0, NULL, NULL); + std::string mbStr(mbLength, 0); + WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, &mbStr[0], mbLength, NULL, NULL); + return mbStr; +} + +std::string known_folder_path(const KNOWNFOLDERID& id) +{ + std::string path; + WCHAR* buffer; + HRESULT result = SHGetKnownFolderPath(id, 0, NULL, &buffer); + if (result == S_OK) { + path = unicode_to_mb(buffer); + CoTaskMemFree(buffer); + } + return path; +} + +const std::string& DataDirectories::personal() +{ + if (_personal.empty()) + _personal = known_folder_path(FOLDERID_Documents); + return _personal; +} + +const std::string& DataDirectories::personal(const std::string& path) +{ + _personal = path; + return _personal; +} + +const std::string& DataDirectories::profile() +{ + if (_profile.empty()) + _profile = known_folder_path(FOLDERID_LocalAppData) + "\\Barrier"; + return _profile; +} + +const std::string& DataDirectories::profile(const std::string& path) +{ + _profile = path; + return _profile; +} \ No newline at end of file diff --git a/src/lib/common/win32/DataDirectories.h b/src/lib/common/win32/DataDirectories.h new file mode 100644 index 00000000..3b276162 --- /dev/null +++ b/src/lib/common/win32/DataDirectories.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +class DataDirectories +{ +public: + static const std::string& personal(); + static const std::string& personal(const std::string& path); + + static const std::string& profile(); + static const std::string& profile(const std::string& path); + +private: + // static class + DataDirectories() {} + + static std::string _personal; + static std::string _profile; +}; \ No newline at end of file