aboutsummaryrefslogtreecommitdiff
path: root/src/Cache.cpp
blob: d4c19fb354efdcefa154d1b954d6224620fc1b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "../include/Cache.hpp"
#include "../include/env.hpp"
#include <boost/filesystem/convenience.hpp>

#if OS_FAMILY == OS_FAMILY_POSIX
#include <pwd.h>
#else
#include <string>
#endif

using namespace std;

namespace dchat
{
    static boost::filesystem::path getHomeDir()
    {
    #if OS_FAMILY == OS_FAMILY_POSIX
        const char *homeDir = getenv("HOME");
        if(!homeDir)
        {
            passwd *pw = getpwuid(getuid());
            homeDir = pw->pw_dir;
        }
        return boost::filesystem::path(homeDir);
    #elif OS_FAMILY == OS_FAMILY_WINDOWS
        BOOL ret;
        HANDLE hToken;
        std::wstring homeDir;
        DWORD homeDirLen = MAX_PATH;
        homeDir.resize(homeDirLen);

        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken))
            return Result<FileString>::Err("Failed to open process token");

        if (!GetUserProfileDirectory(hToken, &homeDir[0], &homeDirLen))
        {
            CloseHandle(hToken);
            return Result<FileString>::Err("Failed to get home directory");
        }

        CloseHandle(hToken);
        homeDir.resize(wcslen(homeDir.c_str()));
        return boost::filesystem::path(homeDir);
    #endif
    }

    Cache::Cache()
    {

    }

    Cache::~Cache()
    {

    }
    
    boost::filesystem::path Cache::getDchatDir()
    {
        boost::filesystem::path dchatHomeDir = getHomeDir() / ".local" / "share" / "dchat";
        boost::filesystem::create_directories(dchatHomeDir);
        return dchatHomeDir;
    }
}