-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMimeInfoEditor.cpp
More file actions
97 lines (80 loc) · 3.75 KB
/
MimeInfoEditor.cpp
File metadata and controls
97 lines (80 loc) · 3.75 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
// libraries
#include <boost/property_tree/xml_parser.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <appimage_handler.h>
// local
#include "utils/Logger.h"
#include "MimeInfoEditor.h"
namespace appimage {
namespace desktop_integration {
namespace integrator {
MimeInfoEditor::MimeInfoEditor(std::string data) {
try {
std::stringstream in(data);
using boost::property_tree::ptree;
// populate tree structure pt
read_xml(in, pt);
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to read MimeInfo: ") + error.what());
}
}
std::string MimeInfoEditor::getResult() {
try {
using boost::property_tree::ptree;
std::stringstream out;
write_xml(out, pt);
return out.str();
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to edit MimeInfo: ") + error.what());
return std::string{};
}
}
void MimeInfoEditor::prependDeployIdToIcons(const std::string& deployId) {
try {
using boost::property_tree::ptree;
// traverse pt
for (auto& node: pt.get_child("mime-info")) {
if (node.first == "mime-type") {
auto& subTree = node.second;
// get original icon name from the icon entry
std::string originalName = subTree.get<std::string>("icon.<xmlattr>.name", "");
// or fallback to the mime-type name
if (originalName.empty()) {
originalName = subTree.get<std::string>("<xmlattr>.type");
boost::replace_all(originalName, "/", "-");
}
std::string newIconName = deployId + '_' + originalName;
subTree.put("icon.<xmlattr>.name", newIconName);
}
}
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to edit MimeInfo: ") + error.what());
}
}
std::list<std::string> MimeInfoEditor::getMimeTypeIconNames() const {
std::list<std::string> icons;
try {
using boost::property_tree::ptree;
// traverse pt
for (auto& node: pt.get_child("mime-info")) {
if (node.first == "mime-type") {
auto& subTree = node.second;
// get original icon name from the icon entry
std::string iconName = subTree.get<std::string>("icon.<xmlattr>.name", "");
// or fallback to the mime-type name
if (iconName.empty()) {
iconName = subTree.get<std::string>("<xmlattr>.type");
boost::replace_all(iconName, "/", "-");
}
icons.push_back(iconName);
}
}
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to read MimeInfo: ") + error.what());
}
return icons;
}
}
}
}