-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXdgInfo.cpp
93 lines (85 loc) · 2.26 KB
/
XdgInfo.cpp
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
#include "XdgInfo.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#include <QStringList>
#include <QProcessEnvironment>
#include <QFileInfo>
XdgInfo XdgInfo::xdginfo;
XdgInfo &XdgInfo::getInstance() {
return xdginfo;
}
XdgInfo::XdgInfo()
{
QProcessEnvironment env { QProcessEnvironment::systemEnvironment() };
if (m_xdg_data_dirs.size() == 0)
{
QString dir;
if((dir = env.value("XDG_DATA_HOME")).size() > 0)
{
m_xdg_data_dirs.push_back(dir);
}
else
{
if((dir = env.value("HOME")).size() > 0)
{
m_xdg_data_dirs.push_back(dir + "/.local/share");
}
else
{
// get the home directory of this user
struct passwd *info = ::getpwuid(::getuid());
if (info)
{
m_xdg_data_dirs.push_back(QString(info->pw_dir) + "/.local/share");
}
}
}
if((dir = env.value("XDG_DATA_DIRS")).size() > 0)
{
m_xdg_data_dirs << dir.split(':');
}
// Common paths
m_xdg_data_dirs.push_back("/usr/share");
// If all else fails, check the current directory
m_xdg_data_dirs.push_back(".");
}
}
QString XdgInfo::find(QString const &file,
QString const &subdir,
QString const &extension)
{
QString full_path;
QFileInfo fi { file };
if (fi.exists() && fi.isFile() && fi.isReadable())
{
// argument `file` may point to the full path:
full_path = fi.canonicalFilePath();
}
else
{
QString fil(file);
QString sub(subdir.size() ? QString("/") + subdir + "/" : "/");
QString ext(extension.size() ? ((extension[0] == QChar('.')) ? QString("") : QString(".")) + extension : "");
// Strip the extension, if it exists
if (fil.endsWith(ext))
fil.truncate(fil.indexOf(ext));
// now search in the XDG directories:
for(auto dir : m_xdg_data_dirs)
{
QString path(dir + sub + fil + ext);
QFileInfo fi { path };
if (fi.exists())
{
if (fi.isFile() && fi.isReadable())
{
// Found the file:
full_path = fi.canonicalFilePath();
break;
}
}
}
}
return full_path;
}