Skip to content

Commit 36c2c20

Browse files
committed
MimeInfoEditor::getMimeTypeIconNames
1 parent f71fa26 commit 36c2c20

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

src/libappimage/desktop_integration/integrator/MimeInfoEditor.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// libraries
22
#include <boost/property_tree/xml_parser.hpp>
3-
#include <boost/property_tree/ptree.hpp>
43
#include <boost/algorithm/string.hpp>
54
#include <iostream>
5+
#include <appimage_handler.h>
66

77
// local
88
#include "utils/Logger.h"
@@ -11,18 +11,21 @@
1111
namespace appimage {
1212
namespace desktop_integration {
1313
namespace integrator {
14-
MimeInfoEditor::MimeInfoEditor(std::string data, std::string deployId)
15-
: data(std::move(data)), deployId(std::move(deployId)) {}
16-
17-
std::string MimeInfoEditor::edit() {
18-
std::stringstream in(data), out;
19-
14+
MimeInfoEditor::MimeInfoEditor(std::string data) {
2015
try {
16+
std::stringstream in(data);
2117
using boost::property_tree::ptree;
2218

2319
// populate tree structure pt
24-
ptree pt;
2520
read_xml(in, pt);
21+
} catch (const std::runtime_error& error) {
22+
appimage::utils::Logger::warning(std::string("Unable to read MimeInfo: ") + error.what());
23+
}
24+
}
25+
26+
std::string MimeInfoEditor::edit() {
27+
try {
28+
using boost::property_tree::ptree;
2629

2730
// traverse pt
2831
for (auto& node: pt.get_child("mime-info")) {
@@ -43,13 +46,46 @@ namespace appimage {
4346
}
4447
}
4548

49+
std::stringstream out;
4650
write_xml(out, pt);
51+
return out.str();
4752
} catch (const std::runtime_error& error) {
4853
appimage::utils::Logger::warning(std::string("Unable to edit MimeInfo: ") + error.what());
49-
return data;
54+
return std::string{};
55+
}
56+
}
57+
58+
void MimeInfoEditor::setDeployId(const std::string& deployId) {
59+
MimeInfoEditor::deployId = deployId;
60+
}
61+
62+
std::list<std::string> MimeInfoEditor::getMimeTypeIconNames() {
63+
std::list<std::string> icons;
64+
65+
try {
66+
using boost::property_tree::ptree;
67+
68+
// traverse pt
69+
for (auto& node: pt.get_child("mime-info")) {
70+
if (node.first == "mime-type") {
71+
auto& subTree = node.second;
72+
// get original icon name from the icon entry
73+
std::string iconName = subTree.get<std::string>("icon.<xmlattr>.name", "");
74+
75+
// or fallback to the mime-type name
76+
if (iconName.empty()) {
77+
iconName = subTree.get<std::string>("<xmlattr>.type");
78+
boost::replace_all(iconName, "/", "-");
79+
}
80+
81+
icons.push_back(iconName);
82+
}
83+
}
84+
} catch (const std::runtime_error& error) {
85+
appimage::utils::Logger::warning(std::string("Unable to read MimeInfo: ") + error.what());
5086
}
5187

52-
return out.str();
88+
return icons;
5389
}
5490
}
5591
}

src/libappimage/desktop_integration/integrator/MimeInfoEditor.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#pragma once
22

33
// system
4+
#include <list>
45
#include <string>
56

7+
// libraries
8+
#include <boost/property_tree/ptree.hpp>
9+
610
namespace appimage {
711
namespace desktop_integration {
812
namespace integrator {
@@ -18,12 +22,16 @@ namespace appimage {
1822
* @param data
1923
* @param deployId
2024
*/
21-
MimeInfoEditor(std::string data, std::string deployId);
25+
MimeInfoEditor(std::string data);
26+
27+
void setDeployId(const std::string& deployId);
2228

2329
std::string edit();
2430

31+
std::list<std::string> getMimeTypeIconNames();
32+
2533
private:
26-
std::string data;
34+
boost::property_tree::ptree pt;
2735
std::string deployId;
2836
};
2937
}

tests/libappimage/desktop_integration/integrator/TestMimeInfoEditor.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class MimeInfoEditorTests : public ::testing::Test {
5757
};
5858

5959
TEST_F(MimeInfoEditorTests, setIcon) {
60-
MimeInfoEditor editor(mimeInfo.str(), "appimaged-d41d8cd98f00b204e9800998ecf8427e");
60+
MimeInfoEditor editor(mimeInfo.str());
61+
editor.setDeployId("appimaged-d41d8cd98f00b204e9800998ecf8427e");
6162
std::string result = editor.edit();
6263

6364

@@ -75,7 +76,8 @@ TEST_F(MimeInfoEditorTests, setIcon) {
7576
}
7677

7778
TEST_F(MimeInfoEditorTests, updateIcon) {
78-
MimeInfoEditor editor(mimeInfoWithIconEntry.str(), "appimaged-d41d8cd98f00b204e9800998ecf8427e");
79+
MimeInfoEditor editor(mimeInfoWithIconEntry.str());
80+
editor.setDeployId("appimaged-d41d8cd98f00b204e9800998ecf8427e");
7981
std::string result = editor.edit();
8082

8183

@@ -91,3 +93,19 @@ TEST_F(MimeInfoEditorTests, updateIcon) {
9193

9294
ASSERT_EQ(resultPt, expectedPt);
9395
}
96+
97+
TEST_F(MimeInfoEditorTests, getIconNamesFromMimeTypeType) {
98+
MimeInfoEditor editor(mimeInfo.str());
99+
std::list<std::string> iconNames = editor.getMimeTypeIconNames();
100+
std::list<std::string> expectedIconNames = {"application-x-starbright-file"};
101+
102+
ASSERT_EQ(iconNames, expectedIconNames);
103+
}
104+
105+
TEST_F(MimeInfoEditorTests, getIconNamesFromMimeTypeIconName) {
106+
MimeInfoEditor editor(mimeInfoWithIconEntry.str());
107+
std::list<std::string> iconNames = editor.getMimeTypeIconNames();
108+
std::list<std::string> expectedIconNames = {"application-x-starbright-file"};
109+
110+
ASSERT_EQ(iconNames, expectedIconNames);
111+
}

0 commit comments

Comments
 (0)