summaryrefslogtreecommitdiff
path: root/src/utils/specialfolder.cpp
blob: c882534d2cb090d8435c9c8ff9083fc248d03137 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 *  The Mana Client
 *  Copyright (C) 2010-2024  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program 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 "specialfolder.h"

#ifdef _WIN32
#include <windows.h>
#include <stdlib.h>

#ifdef SPECIALFOLDERLOCATION_TEST
// compile with -DSPECIALFOLDERLOCATION_TEST to get a standalone
// binary for testing
#include <iostream>
#endif

/*
 * Retrieve the pathname of special folders on Windows, or an empty string
 * on error.
 *
 * See https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
 * for a list of folder IDs.
 */
std::string getSpecialFolderLocation(const KNOWNFOLDERID &folderId)
{
    std::string ret;
    PWSTR widePath = 0;

    HRESULT hr = SHGetKnownFolderPath(folderId, 0, NULL, &widePath);
    if (hr == S_OK)
    {
        // determine needed bytes
        size_t len;
        if (wcstombs_s(&len, nullptr, 0, widePath, 0) == 0)
        {
            ret.resize(len - 1);    // subtract null character
            if (wcstombs_s(nullptr, ret.data(), len, widePath, len - 1) != 0)
                ret.clear();
        }
    }

    CoTaskMemFree(widePath);

    return ret;
}

#ifdef SPECIALFOLDERLOCATION_TEST
int main()
{
    std::cout << "RoamingAppData " << getSpecialFolderLocation(FOLDERID_RoamingAppData)
              << std::endl;
    std::cout << "Desktop " << getSpecialFolderLocation(FOLDERID_Desktop)
              << std::endl;
    std::cout << "LocalAppData " << getSpecialFolderLocation(FOLDERID_LocalAppData)
              << std::endl;
    std::cout << "Pictures " << getSpecialFolderLocation(FOLDERID_Pictures)
              << std::endl;
    std::cout << "Documents " << getSpecialFolderLocation(FOLDERID_Documents)
              << std::endl;
}
#endif
#endif // _WIN32

#ifdef __APPLE__
#include <CoreFoundation/CFBundle.h>

std::string getResourcesLocation()
{
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path,
                                          PATH_MAX))
    {
        fprintf(stderr, "Can't find Resources directory\n");
    }
    CFRelease(resourcesURL);
    return path;
}
#endif // __APPLE__