Skip to content

Commit

Permalink
Use wxStandardPaths (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
vktr committed Apr 26, 2018
1 parent f419619 commit 848f84d
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions src/picotorrent/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <ShlObj.h>
#include <Shlwapi.h>

#pragma warning( push )
#pragma warning( disable : 4996)
#include <wx/stdpaths.h>
#pragma warning( pop )

using pt::Environment;

fs::path Environment::GetApplicationDataPath()
Expand All @@ -22,36 +27,20 @@ fs::path Environment::GetApplicationDataPath()

fs::path Environment::GetKnownFolderPath(Environment::KnownFolder knownFolder)
{
KNOWNFOLDERID rfid;
wxStandardPaths& paths = wxStandardPaths::Get();
paths.UseAppInfo(wxStandardPaths::AppInfo_None);

switch (knownFolder)
{
case KnownFolder::LocalAppData:
rfid = FOLDERID_LocalAppData;
break;
return std::string(paths.GetUserLocalDataDir().ToUTF8());

case KnownFolder::UserDownloads:
rfid = FOLDERID_Downloads;
break;
return std::string(paths.GetUserDir(wxStandardPaths::Dir_Downloads).ToUTF8());

default:
throw std::runtime_error("Unknown folder");
}

PWSTR buf;
HRESULT hResult = SHGetKnownFolderPath(
rfid,
0,
NULL,
&buf);

if (hResult != S_OK)
{
throw std::runtime_error(std::to_string(hResult).c_str());
}

std::wstring res = buf;
CoTaskMemFree(buf);

return res;
}

bool Environment::IsAppContainerProcess()
Expand All @@ -68,49 +57,50 @@ bool Environment::IsAppContainerProcess()
bool Environment::IsInstalled()
{
HKEY hKey = NULL;

LSTATUS lStatus = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("Software\\PicoTorrent"),
0,
KEY_READ,
&hKey);

if (lStatus != ERROR_SUCCESS)
{
if (hKey != NULL) { RegCloseKey(hKey); }
return false;
}

TCHAR installDirectory[MAX_PATH];
DWORD bufSize = MAX_PATH;

lStatus = RegQueryValueEx(
hKey,
L"InstallDirectory",
NULL,
NULL,
(LPBYTE)installDirectory,
&bufSize);

if (lStatus != ERROR_SUCCESS)
{
if (hKey != NULL) { RegCloseKey(hKey); }
return false;
}

TCHAR currentLocation[MAX_PATH];
GetModuleFileName(NULL, currentLocation, ARRAYSIZE(currentLocation));

TCHAR installedFile[MAX_PATH];
PathCombine(installedFile, installDirectory, TEXT("PicoTorrent.exe"));

if (StrCmp(currentLocation, installedFile) == 0)
{
if (hKey != NULL) { RegCloseKey(hKey); }
return true;
}

if (hKey != NULL) { RegCloseKey(hKey); }

return false;
}

0 comments on commit 848f84d

Please sign in to comment.