-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.cpp
More file actions
316 lines (257 loc) · 7.8 KB
/
Copy pathIndex.cpp
File metadata and controls
316 lines (257 loc) · 7.8 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
libmimeapps, an implementation of
http://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-latest.html
Copyright (c) 2015, Piotr Wójcik
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Index.h"
#include "ConfigReader.h"
namespace LibMimeApps
{
std::vector<Index::lookupDirectory>Index::directoryPatterns_ = Index::initDirectoryPatterns();
Index::Index()
{
findDirectories();
createBase();
}
Index::Index(const std::string &language):
language_(language)
{
findDirectories();
createBase();
}
Index::~Index()
{
std::map<std::string, DesktopEntry*>::iterator application;
for (application = knownApplications_.begin(); application != knownApplications_.end(); ++application)
{
delete application->second;
}
}
std::vector<DesktopEntry> Index::appsForMime(const std::string &type) const
{
std::vector<DesktopEntry> result;
if (applicationsCache_.find(type) != applicationsCache_.end())
{
std::list<DesktopEntry*> list = applicationsCache_.at(type);
for (std::list<DesktopEntry*>::iterator entry = list.begin(); entry != list.end(); ++entry)
{
result.push_back(**entry);
}
}
return result;
}
void Index::findDirectories()
{
directories_ = directoryPatterns_;
bool again;
do
{
std::vector<lookupDirectory> directories;
again = false;
for (std::vector<lookupDirectory>::size_type i = 0; i < directories_.size(); ++i)
{
std::vector<std::string> unfolded = unfoldVariable(directories_.at(i).path);
if (unfolded.size() > 0 && unfolded.at(0) != directories_.at(i).path)
{
again = true;
}
for (std::vector<std::string>::size_type j = 0; j < unfolded.size(); ++j)
{
directories.push_back(lookupDirectory(directories_.at(i).withSubdirectories, unfolded.at(j)));
}
}
directories_.swap(directories);
directories.clear();
} while (again);
}
void Index::createBase()
{
for (std::vector<std::string>::size_type i = 0; i < directories_.size(); ++i)
{
processDirectory(directories_.at(i), std::string());
}
removeUnused();
}
void Index::processDirectory(const lookupDirectory &baseDirectory, const std::string &relative)
{
std::string directory = baseDirectory.path + relative;
std::vector<file> content = directoryEntries(directory);
if (baseDirectory.withSubdirectories)
{
for (std::vector<file>::size_type i = 0; i < content.size(); ++i)
{
if (content.at(i).type == FileType::Directory)
{
processDirectory(baseDirectory, relative + content.at(i).name + "/");
}
}
}
processDesktopInDirectory(baseDirectory.path, relative, content);
processMimeApps(directory + "mimeapps.list");
}
void Index::processDesktopInDirectory(const std::string &baseDirectory, const std::string &relative, const std::vector<file> &content)
{
for (std::vector<std::string>::size_type i = 0; i < content.size(); ++i)
{
if (content.at(i).type == FileType::File && endsWith(content.at(i).name, std::string(".desktop")))
{
processDesktopFile(baseDirectory, relative + content.at(i).name);
}
}
}
void Index::processMimeApps(const std::string &path)
{
ConfigReader config(path);
std::vector<std::string> types;
types = config.keys("Added Associations");
for (std::vector<std::string>::size_type i = 0; i < types.size(); ++i)
{
std::vector<std::string> identifiers = split(config.value("Added Associations", types.at(i)), ';');
for (std::vector<std::string>::reverse_iterator it = identifiers.rbegin(); it != identifiers.rend(); ++it)
{
if (knownApplications_.find(*it) != knownApplications_.end())
{
addToType(types[i], knownApplications_.at(*it));
}
}
}
types = config.keys("Removed Associations");
for (std::vector<std::string>::size_type i = 0; i < types.size(); ++i)
{
std::vector<std::string> identifiers = split(config.value("Removed Associations", types.at(i)), ';');
for (std::vector<std::string>::size_type j = 0; j < identifiers.size(); ++j)
{
removeFromType(types[i], identifiers[j]);
}
}
}
void Index::processDesktopFile(const std::string &baseDirectory, const std::string &relative)
{
DesktopEntry *entry = new DesktopEntry(baseDirectory, relative, language_);
if (entry->hidden())
{
removeApplication(entry->identifier());
}
else if (!entry->noDisplay())
{
addApplication(entry);
}
else
{
delete entry;
}
}
void Index::addApplication(DesktopEntry *entry)
{
removeApplication(entry->identifier());
knownApplications_[entry->identifier()] = entry;
for (std::vector<std::string>::size_type i = 0; i < entry->types().size(); ++i)
{
addToType(entry->types().at(i), entry);
}
}
void Index::addToType(const std::string &type, DesktopEntry *entry)
{
if (applicationsCache_.find(type) != applicationsCache_.end())
{
removeFromType(type, entry->identifier());
}
applicationsCache_[type].push_front(entry);
entry->types_.push_back(type);
}
void Index::removeApplication(const std::string &entryId)
{
for (std::map<std::string, std::list<DesktopEntry*> >::iterator type = applicationsCache_.begin(); type != applicationsCache_.end(); ++type)
{
removeFromType(type->first, entryId);
}
}
void Index::removeFromType(const std::string &type, const std::string &entryId)
{
if (applicationsCache_.find(type) != applicationsCache_.end())
{
for (std::list<DesktopEntry*>::iterator it = applicationsCache_.at(type).begin(); it != applicationsCache_.at(type).end();)
{
if ((*it)->identifier() == entryId)
{
applicationsCache_.at(type).erase(it++);
}
else
{
++it;
}
}
}
if (knownApplications_.find(entryId) != knownApplications_.end())
{
std::vector<std::string> &types = knownApplications_.at(entryId)->types_;
for (std::vector<std::string>::iterator it = types.begin(); it != types.end();)
{
if (*it == type)
{
if (it == types.begin())
{
types.erase(it);
it = types.begin();
}
else
{
std::vector<std::string>::iterator temp = it;
--temp;
types.erase(it);
it = ++temp;
}
}
else
{
++it;
}
}
}
}
void Index::removeUnused()
{
std::map<std::string, DesktopEntry*>::iterator application;
for (application = knownApplications_.begin(); application != knownApplications_.end();)
{
if (application->second->types().empty())
{
delete application->second;
knownApplications_.erase(application++);
}
else
{
++application;
}
}
}
std::vector<Index::lookupDirectory> Index::initDirectoryPatterns()
{
std::vector<lookupDirectory> result;
result.push_back(lookupDirectory(true, "$XDG_DATA_DIRS/applications/"));
result.push_back(lookupDirectory(true, "$XDG_DATA_HOME/applications/"));
result.push_back(lookupDirectory(false, "$XDG_CONFIG_DIRS/"));
result.push_back(lookupDirectory(false, "$XDG_CONFIG_HOME/"));
return result;
}
}