barrier/src/lib/common/win32/DataDirectories.cpp

74 lines
2.1 KiB
C++

/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2018 Debauchee Open Source Group
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../DataDirectories.h"
#include "encoding_utilities.h"
#include <Shlobj.h>
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 = win_wchar_to_utf8(buffer);
CoTaskMemFree(buffer);
}
return path;
}
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;
}
const std::string& DataDirectories::global()
{
if (_global.empty())
_global = known_folder_path(FOLDERID_ProgramData) + "\\Barrier";
return _global;
}
const std::string& DataDirectories::global(const std::string& path)
{
_global = path;
return _global;
}
const std::string& DataDirectories::systemconfig()
{
// systemconfig() is a special case in that it will track the current value
// of global() unless and until it is explicitly set otherwise
// previously it would default to the windows folder which was horrible!
if (_systemconfig.empty())
return global();
return _systemconfig;
}
const std::string& DataDirectories::systemconfig(const std::string& path)
{
_systemconfig = path;
return _systemconfig;
}