summaryrefslogtreecommitdiffstats
path: root/src/lib/arch/win32/ArchFileWindows.cpp
blob: 53b4b594bca217a7b782d1edb5d5c36f1f89150e (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2002 Chris Schoeneman
 * 
 * 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 "arch/win32/ArchFileWindows.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <shlobj.h>
#include <tchar.h>
#include <string.h>

//
// ArchFileWindows
//

ArchFileWindows::ArchFileWindows()
{
    // do nothing
}

ArchFileWindows::~ArchFileWindows()
{
    // do nothing
}

const char*
ArchFileWindows::getBasename(const char* pathname)
{
    if (pathname == NULL) {
        return NULL;
    }

    // check for last slash
    const char* basename = strrchr(pathname, '/');
    if (basename != NULL) {
        ++basename;
    }
    else {
        basename = pathname;
    }

    // check for last backslash
    const char* basename2 = strrchr(pathname, '\\');
    if (basename2 != NULL && basename2 > basename) {
        basename = basename2 + 1;
    }

    return basename;
}

std::string
ArchFileWindows::getUserDirectory()
{
    // try %HOMEPATH%
    TCHAR dir[MAX_PATH];
    DWORD size   = sizeof(dir) / sizeof(TCHAR);
    DWORD result = GetEnvironmentVariable(_T("HOMEPATH"), dir, size);
    if (result != 0 && result <= size) {
        // sanity check -- if dir doesn't appear to start with a
        // drive letter and isn't a UNC name then don't use it
        // FIXME -- allow UNC names
        if (dir[0] != '\0' && (dir[1] == ':' ||
            ((dir[0] == '\\' || dir[0] == '/') &&
            (dir[1] == '\\' || dir[1] == '/')))) {
            return dir;
        }
    }

    // get the location of the personal files.  that's as close to
    // a home directory as we're likely to find.
    ITEMIDLIST* idl;
    if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &idl))) {
        TCHAR* path = NULL;
        if (SHGetPathFromIDList(idl, dir)) {
            DWORD attr = GetFileAttributes(dir);
            if (attr != 0xffffffff && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
                path = dir;
        }

        IMalloc* shalloc;
        if (SUCCEEDED(SHGetMalloc(&shalloc))) {
            shalloc->Free(idl);
            shalloc->Release();
        }

        if (path != NULL) {
            return path;
        }
    }

    // use root of C drive as a default
    return "C:";
}

std::string
ArchFileWindows::getSystemDirectory()
{
    // get windows directory
    char dir[MAX_PATH];
    if (GetWindowsDirectory(dir, sizeof(dir)) != 0) {
        return dir;
    }
    else {
        // can't get it.  use C:\ as a default.
        return "C:";
    }
}

std::string
ArchFileWindows::getInstalledDirectory()
{
    char fileNameBuffer[MAX_PATH];
    GetModuleFileName(NULL, fileNameBuffer, MAX_PATH);
    std::string fileName(fileNameBuffer);
    size_t lastSlash = fileName.find_last_of("\\");
    fileName = fileName.substr(0, lastSlash);

    return fileName;
}

std::string
ArchFileWindows::getLogDirectory()
{
    return getInstalledDirectory();
}

std::string
ArchFileWindows::getPluginDirectory()
{
    if (!m_pluginDirectory.empty()) {
        return m_pluginDirectory;
    }

    std::string dir = getProfileDirectory();
    dir.append("\\Plugins");
    return dir;
}

std::string
ArchFileWindows::getProfileDirectory()
{
    String dir;
    if (!m_profileDirectory.empty()) {
        dir = m_profileDirectory;
    }
    else {
        TCHAR result[MAX_PATH];
        if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, result))) {
            dir = result;
        }
        else {
            dir = getUserDirectory();
        }
    }

    // HACK: append program name, this seems wrong.
    dir.append("\\Barrier");

    return dir;
}

std::string
ArchFileWindows::concatPath(const std::string& prefix,
                const std::string& suffix)
{
    std::string path;
    path.reserve(prefix.size() + 1 + suffix.size());
    path += prefix;
    if (path.size() == 0 ||
        (path[path.size() - 1] != '\\' &&
        path[path.size() - 1] != '/')) {
        path += '\\';
    }
    path += suffix;
    return path;
}

void
ArchFileWindows::setProfileDirectory(const String& s)
{
    m_profileDirectory = s;
}

void
ArchFileWindows::setPluginDirectory(const String& s)
{
    m_pluginDirectory = s;
}