-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathdesktopfile.cpp
More file actions
142 lines (105 loc) · 4.55 KB
/
desktopfile.cpp
File metadata and controls
142 lines (105 loc) · 4.55 KB
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
// library headers
#include <INI.h>
// local headers
#include "linuxdeploy/core/desktopfile.h"
#include "linuxdeploy/core/log.h"
using namespace linuxdeploy::core;
using namespace linuxdeploy::core::log;
namespace bf = boost::filesystem;
namespace linuxdeploy {
namespace core {
namespace desktopfile {
class DesktopFile::PrivateData {
public:
bf::path path;
INI<> ini;
public:
PrivateData() : path(), ini("", false) {};
};
DesktopFile::DesktopFile() {
d = new PrivateData();
}
DesktopFile::DesktopFile(const bf::path& path) : DesktopFile() {
if (!read(path))
throw std::runtime_error("Failed to read desktop file");
};
bool DesktopFile::read(const boost::filesystem::path& path) {
setPath(path);
clear();
// nothing to do
if (!bf::exists(path))
return true;
std::ifstream ifs(path.string());
if (!ifs)
return false;
d->ini.parse(ifs);
return true;
}
boost::filesystem::path DesktopFile::path() const {
return d->path;
}
void DesktopFile::setPath(const boost::filesystem::path& path) {
d->path = path;
}
void DesktopFile::clear() {
d->ini.clear();
}
bool DesktopFile::save() const {
return save(d->path);
}
bool DesktopFile::save(const boost::filesystem::path& path) const {
return d->ini.save(path.string());
}
bool DesktopFile::entryExists(const std::string& section, const std::string& key) const {
if (!d->ini.select(section))
return false;
std::string absolutelyUnlikeValue = "<>!§$%&/()=?+'#-.,_:;'*¹²³½¬{[]}^°|";
auto value = d->ini.get<std::string, std::string, std::string>(section, key, absolutelyUnlikeValue);
return value != absolutelyUnlikeValue;
}
bool DesktopFile::setEntry(const std::string& section, const std::string& key, const std::string& value) {
// check if value exists -- used for return value
auto rv = entryExists(section, key);
// set key
d->ini[section][key] = value;
return rv;
}
bool DesktopFile::getEntry(const std::string& section, const std::string& key, std::string& value) const {
if (!entryExists(section, key))
return false;
if (!d->ini.select(section))
return false;
value = d->ini.get(key);
return true;
}
bool DesktopFile::addDefaultKeys(const std::string& executableFileName) {
ldLog() << "Adding default values to desktop file:" << path() << std::endl;
auto rv = true;
auto setDefault = [&rv, this](const std::string& section, const std::string& key, const std::string& value) {
if (entryExists(section, key)) {
std::string currentValue;
if (!getEntry(section, key, currentValue))
ldLog() << LD_ERROR << "This should never happen" << std::endl;
ldLog() << LD_WARNING << "Key exists, not modified:" << key << "(current value:" << currentValue << LD_NO_SPACE << ")" << std::endl;
rv = false;
} else {
if (setEntry(section, key, value)) {
// *should* be unreachable
rv = false;
}
}
};
setDefault("Desktop Entry", "Name", executableFileName);
setDefault("Desktop Entry", "Exec", executableFileName);
setDefault("Desktop Entry", "Icon", executableFileName);
setDefault("Desktop Entry", "Type", "Application");
setDefault("Desktop Entry", "Categories", "Utility;");
return rv;
}
bool DesktopFile::validate() const {
// FIXME: call desktop-file-validate
return true;
}
}
}
}