File: dirs.cpp

package info (click to toggle)
qtcurve 1.9-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,460 kB
  • sloc: cpp: 70,269; perl: 123; sh: 122; xml: 46; makefile: 15
file content (244 lines) | stat: -rw-r--r-- 7,458 bytes parent folder | download | duplicates (5)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*****************************************************************************
 *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU Lesser General Public License as          *
 *   published by the Free Software Foundation; either version 2.1 of the    *
 *   License, or (at your option) version 3, or any later version accepted   *
 *   by the membership of KDE e.V. (or its successor approved by the         *
 *   membership of KDE e.V.), which shall act as a proxy defined in          *
 *   Section 6 of version 3 of the license.                                  *
 *                                                                           *
 *   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       *
 *   Lesser General Public License for more details.                         *
 *                                                                           *
 *   You should have received a copy of the GNU Lesser General Public        *
 *   License along with this library. If not,                                *
 *   see <http://www.gnu.org/licenses/>.                                     *
 *****************************************************************************/

#include "dirs.h"
#include "log.h"
#include "strs.h"

#include <config.h>

#include <forward_list>

#include <sys/types.h>
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>

namespace QtCurve {

QTC_EXPORT void
makePath(const char *path, int mode)
{
    if (isDir(path)) {
        return;
    }
    Str::Buff<1024> opath(path);
    size_t len = opath.size() - 1;
    while (opath[len - 1] == '/') {
        opath[len - 1] = '\0';
        len--;
    }
    char *p = opath.get() + strspn(opath, "/");
    if (!*p) {
        return;
    }
    p += 1;
    for (;*p;p++) {
        if (*p == '/') {
            *p = '\0';
            if (access(opath, F_OK)) {
                mkdir(opath, mode | 0300);
            }
            *p = '/';
        }
    }
    if (access(opath, F_OK)) {
        mkdir(opath, mode);
    }
}

// Home
QTC_EXPORT const char*
getHome()
{
    static uniqueStr dir = [] {
        const char *env_home = getenv("HOME");
        if (qtcLikely(env_home && *env_home == '/')) {
            return Str::cat(env_home, "/");
        } else {
            struct passwd *pw = getpwuid(getuid());
            if (qtcLikely(pw && pw->pw_dir && *pw->pw_dir == '/')) {
                return Str::cat(pw->pw_dir, "/");
            }
        }
        return strdup("/tmp/");
    };
    return dir.get();
}

// XDG dirs
QTC_EXPORT const char*
getXDGConfigHome()
{
    static uniqueStr dir = [] {
        const char *env_home = getenv("XDG_CONFIG_HOME");
        if (env_home && *env_home == '/') {
            return Str::cat(env_home, "/");
        } else {
            return Str::cat(getHome(), ".config/");
        }
    };
    return dir.get();
}

QTC_EXPORT const char*
getXDGDataHome()
{
    static uniqueStr dir = [] {
        const char *env_home = getenv("XDG_DATA_HOME");
        if (env_home && *env_home == '/') {
            return Str::cat(env_home, "/");
        } else {
            return Str::cat(getHome(), ".local/share/");
        }
    };
    return dir.get();
}

// TODO
const std::forward_list<uniqueStr>&
getKDE4Home()
{
    static const std::forward_list<uniqueStr> homes = [] {
        std::forward_list<uniqueStr> res;
        auto add_dir = [&] (char *dir) {
            if (isDir(dir)) {
                res.emplace_front(dir);
            } else {
                free(dir);
            }
        };
        add_dir(Str::cat(getHome(), ".kde/"));
        add_dir(Str::cat(getHome(), ".kde4/"));
        char *env = getenv(getuid() ? "KDEHOME" : "KDEROOTHOME");
        if (env && env[0] == '/') {
            add_dir(Str::cat(env, "/"));
        } else {
#ifndef QTC_KDE4_DEFAULT_HOME_DEFAULT
            add_dir(Str::cat(getHome(), QTC_KDE4_DEFAULT_HOME "/"));
#endif
        }
        return res;
    }();
    return homes;
}

QTC_EXPORT const char*
getConfDir()
{
    static uniqueStr dir = [] {
        const char *env_home = getenv("QTCURVE_CONFIG_DIR");
        char *res = ((env_home && *env_home == '/') ? Str::cat(env_home, "/") :
                     Str::cat(getXDGConfigHome(), "qtcurve/"));
        makePath(res, 0700);
        return res;
    };
    return dir.get();
}

QTC_EXPORT char*
getConfFile(const char *file, char *buff)
{
    if (file[0] == '/')
        return Str::fill(buff, file);
    return Str::fill(buff, getConfDir(), file);
}

static const std::forward_list<std::string>&
getPresetDirs()
{
    static const std::forward_list<std::string> dirs = [] {
        std::forward_list<std::string> res;
        auto add_dir = [&] (const char *dir, bool needfree=true) {
            if (isDir(dir)) {
                res.emplace_front(dir);
            }
            if (needfree) {
                free(const_cast<char*>(dir));
            }
        };
        for (auto &kde_home: getKDE4Home()) {
            add_dir(Str::cat(kde_home.get(), "share/apps/QtCurve/"));
        }
        if (auto xdg_data_dirs = getenv("XDG_DATA_DIRS")) {
            while (true) {
                auto delim = strchr(xdg_data_dirs, ':');
                if (delim) {
                    auto origin = xdg_data_dirs;
                    xdg_data_dirs = delim + 1;
                    if (origin[0] != '/')
                        continue;
                    std::string dir(origin, delim - origin);
                    dir += "/QtCurve/";
                    if (isDir(dir.c_str())) {
                        res.push_front(std::move(dir));
                    }
                } else if (xdg_data_dirs[0] == '/') {
                    std::string dir(xdg_data_dirs);
                    dir += "/QtCurve/";
                    if (isDir(dir.c_str()))
                        res.push_front(std::move(dir));
                    break;
                } else {
                    break;
                }
            }
        } else {
            add_dir("/usr/local/share/QtCurve/", false);
            add_dir("/usr/share/QtCurve/", false);
        }
        auto xdg_home = Str::cat(getXDGDataHome(), "QtCurve/");
        makePath(xdg_home, 0700);
        add_dir(xdg_home);
        return res;
    }();
    return dirs;
}

QTC_EXPORT std::map<std::string, std::string>
getPresets()
{
    std::map<std::string, std::string> presets;

    for (auto &dir_name: getPresetDirs()) {
        DIR *dir = opendir(dir_name.c_str());
        if (!dir)
            continue;
        while (auto ent = readdir(dir)) {
            auto fname = ent->d_name;
            // Non preset files
            if (!Str::endsWith(fname, ".qtcurve"))
                continue;
            size_t name_len = strlen(fname) - strlen(".qtcurve");
            std::string name(fname, name_len);
            // preset already loaded
            if (presets.find(name) != presets.end())
                continue;
            presets[name] = dir_name + fname;
        }
        closedir(dir);
    }

    return presets;
}

}