Changeset View
Changeset View
Standalone View
Standalone View
src/extractors/appimageextractor.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2019 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Lesser General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Lesser General Public | ||||
15 | License along with this library; if not, write to the Free Software | ||||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
17 | */ | ||||
18 | | ||||
19 | #include "appimageextractor.h" | ||||
20 | | ||||
21 | // KF | ||||
22 | #include <KDesktopFile> | ||||
23 | // Qt | ||||
24 | #include <QTextDocument> | ||||
25 | #include <QDomDocument> | ||||
26 | #include <QTemporaryFile> | ||||
27 | #include <QLocale> | ||||
28 | #include <QDebug> | ||||
29 | // libappimage | ||||
30 | #include <appimage/appimage.h> | ||||
31 | | ||||
32 | using namespace KFileMetaData; | ||||
33 | | ||||
34 | | ||||
35 | namespace { | ||||
36 | namespace AttributeNames { | ||||
37 | QString xml_lang() { return QStringLiteral("xml:lang"); } | ||||
38 | } | ||||
39 | } | ||||
40 | | ||||
41 | | ||||
42 | // helper class to extract the interesting data from the appdata file | ||||
43 | // prefers localized strings over unlocalized, using system locale | ||||
44 | class AppDataParser | ||||
45 | { | ||||
46 | public: | ||||
47 | AppDataParser(const char* appImageFilePath, const QString& appdataFilePath); | ||||
48 | | ||||
49 | public: | ||||
50 | QString name() const { return !m_localized.name.isEmpty() ? m_localized.name : m_unlocalized.name; } | ||||
51 | QString summary() const { return !m_localized.summary.isEmpty() ? m_localized.summary : m_unlocalized.summary; } | ||||
52 | QString description() const { return !m_localized.description.isEmpty() ? m_localized.description : m_unlocalized.description; } | ||||
53 | QString developerName() const { return !m_localized.developerName.isEmpty() ? m_localized.developerName : m_unlocalized.developerName; } | ||||
54 | QString projectLicense() const { return m_projectLicense; } | ||||
55 | | ||||
56 | private: | ||||
57 | void extractDescription(const QDomElement& e, const QString& localeName); | ||||
58 | | ||||
59 | private: | ||||
60 | struct Data { | ||||
61 | QString name; | ||||
62 | QString summary; | ||||
63 | QString description; | ||||
64 | QString developerName; | ||||
65 | }; | ||||
66 | Data m_localized; | ||||
67 | Data m_unlocalized; | ||||
68 | QString m_projectLicense; | ||||
69 | }; | ||||
70 | | ||||
71 | | ||||
72 | AppDataParser::AppDataParser(const char* appImageFilePath, const QString& appdataFilePath) | ||||
73 | { | ||||
74 | if (appdataFilePath.isEmpty()) { | ||||
75 | return; | ||||
76 | } | ||||
77 | | ||||
78 | unsigned long size = 0L; | ||||
79 | char* buf = nullptr; | ||||
80 | bool ok = appimage_read_file_into_buffer_following_symlinks(appImageFilePath, | ||||
81 | qUtf8Printable(appdataFilePath), | ||||
82 | &buf, | ||||
83 | &size); | ||||
84 | | ||||
85 | QScopedPointer<char, QScopedPointerPodDeleter> cleanup(buf); | ||||
86 | | ||||
87 | if (!ok) { | ||||
88 | return; | ||||
89 | } | ||||
90 | | ||||
91 | QDomDocument domDocument; | ||||
92 | if (!domDocument.setContent(QByteArray::fromRawData(buf, size))) { | ||||
93 | return; | ||||
94 | } | ||||
95 | | ||||
96 | QDomElement docElem = domDocument.documentElement(); | ||||
97 | if (docElem.tagName() != QLatin1String("component")) { | ||||
98 | return; | ||||
99 | } | ||||
100 | | ||||
101 | const auto localeName = QLocale::system().bcp47Name(); | ||||
102 | | ||||
103 | QDomElement ec = docElem.firstChildElement(); | ||||
104 | while (!ec.isNull()) { | ||||
105 | const auto tagName = ec.tagName(); | ||||
106 | const auto hasLangAttribute = ec.hasAttribute(AttributeNames::xml_lang()); | ||||
107 | const auto matchingLocale = hasLangAttribute && (ec.attribute(AttributeNames::xml_lang()) == localeName); | ||||
108 | if (matchingLocale || !hasLangAttribute) { | ||||
109 | if (tagName == QLatin1String("name")) { | ||||
110 | Data& data = hasLangAttribute ? m_localized : m_unlocalized; | ||||
111 | data.name = ec.text(); | ||||
112 | } else if (tagName == QLatin1String("summary")) { | ||||
113 | Data& data = hasLangAttribute ? m_localized : m_unlocalized; | ||||
114 | data.summary = ec.text(); | ||||
115 | } else if (tagName == QLatin1String("description")) { | ||||
116 | extractDescription(ec, localeName); | ||||
117 | } else if (tagName == QLatin1String("developer_name")) { | ||||
118 | Data& data = hasLangAttribute ? m_localized : m_unlocalized; | ||||
119 | data.developerName = ec.text(); | ||||
120 | } else if (tagName == QLatin1String("project_license")) { | ||||
121 | m_projectLicense = ec.text(); | ||||
122 | } | ||||
123 | } | ||||
124 | ec = ec.nextSiblingElement(); | ||||
125 | } | ||||
126 | } | ||||
127 | | ||||
128 | using DesriptionDomFilter = std::function<bool(const QDomElement& e)>; | ||||
129 | | ||||
130 | void stripDescriptionTextElements(QDomElement& element, const DesriptionDomFilter& stripFilter) | ||||
131 | { | ||||
132 | auto childElement = element.firstChildElement(); | ||||
133 | while (!childElement.isNull()) { | ||||
134 | auto nextChildElement = childElement.nextSiblingElement(); | ||||
135 | | ||||
136 | const auto tagName = childElement.tagName(); | ||||
137 | const bool isElementToFilter = (tagName == QLatin1String("p")) || (tagName == QLatin1String("li")); | ||||
138 | if (isElementToFilter && stripFilter(childElement)) { | ||||
139 | element.removeChild(childElement); | ||||
140 | } else { | ||||
141 | stripDescriptionTextElements(childElement, stripFilter); | ||||
142 | } | ||||
143 | | ||||
144 | childElement = nextChildElement; | ||||
145 | } | ||||
146 | } | ||||
147 | | ||||
148 | void AppDataParser::extractDescription(const QDomElement& e, const QString& localeName) | ||||
149 | { | ||||
150 | // create fake html from it and let QTextDocument transform it to plain text for us | ||||
151 | QDomDocument descriptionDocument; | ||||
152 | auto htmlElement = descriptionDocument.createElement(QStringLiteral("html")); | ||||
153 | descriptionDocument.appendChild(htmlElement); | ||||
154 | | ||||
155 | // first localized... | ||||
156 | auto clonedE = descriptionDocument.importNode(e, true).toElement(); | ||||
157 | clonedE.setTagName(QStringLiteral("body")); | ||||
158 | stripDescriptionTextElements(clonedE, [localeName](const QDomElement& e) { | ||||
159 | return !e.hasAttribute(AttributeNames::xml_lang()) || | ||||
160 | e.attribute(AttributeNames::xml_lang()) != localeName; | ||||
161 | }); | ||||
162 | htmlElement.appendChild(clonedE); | ||||
163 | | ||||
164 | QTextDocument textDocument; | ||||
165 | textDocument.setHtml(descriptionDocument.toString(-1)); | ||||
166 | | ||||
167 | m_localized.description = textDocument.toPlainText().trimmed(); | ||||
168 | | ||||
169 | if (!m_localized.description.isEmpty()) { | ||||
170 | // localized will be preferred, no need to calculate unlocalized one | ||||
171 | return; | ||||
172 | } | ||||
173 | | ||||
174 | // then unlocalized if still needed | ||||
175 | htmlElement.removeChild(clonedE); // reuse descriptionDocument | ||||
176 | clonedE = descriptionDocument.importNode(e, true).toElement(); | ||||
177 | clonedE.setTagName(QStringLiteral("body")); | ||||
178 | stripDescriptionTextElements(clonedE, [](const QDomElement& e) { | ||||
179 | return e.hasAttribute(AttributeNames::xml_lang()); | ||||
180 | }); | ||||
181 | htmlElement.appendChild(clonedE); | ||||
182 | | ||||
183 | textDocument.setHtml(descriptionDocument.toString(-1)); | ||||
184 | | ||||
185 | m_unlocalized.description = textDocument.toPlainText().trimmed(); | ||||
186 | } | ||||
187 | | ||||
188 | | ||||
189 | // helper class to extract the interesting data from the desktop file | ||||
190 | class DesktopFileParser | ||||
191 | { | ||||
192 | public: | ||||
193 | DesktopFileParser(const char* appImageFilePath, const QString& desktopFilePath); | ||||
194 | | ||||
195 | public: | ||||
196 | QString name; | ||||
197 | QString comment; | ||||
198 | }; | ||||
199 | | ||||
200 | | ||||
201 | DesktopFileParser::DesktopFileParser(const char* appImageFilePath, const QString& desktopFilePath) | ||||
202 | { | ||||
203 | if (desktopFilePath.isEmpty()) { | ||||
204 | return; | ||||
205 | } | ||||
206 | | ||||
207 | unsigned long size = 0L; | ||||
208 | char* buf = nullptr; | ||||
209 | bool ok = appimage_read_file_into_buffer_following_symlinks(appImageFilePath, | ||||
210 | qUtf8Printable(desktopFilePath), | ||||
211 | &buf, | ||||
212 | &size); | ||||
213 | | ||||
214 | QScopedPointer<char, QScopedPointerPodDeleter> cleanup(buf); | ||||
215 | | ||||
216 | if (!ok) { | ||||
217 | return; | ||||
218 | } | ||||
219 | | ||||
220 | // create real file, KDesktopFile needs that | ||||
221 | QTemporaryFile tmpDesktopFile; | ||||
222 | tmpDesktopFile.open(); | ||||
223 | tmpDesktopFile.write(buf, size); | ||||
224 | tmpDesktopFile.close(); | ||||
225 | | ||||
226 | KDesktopFile desktopFile(tmpDesktopFile.fileName()); | ||||
227 | name = desktopFile.readName(); | ||||
228 | comment = desktopFile.readComment(); | ||||
229 | } | ||||
230 | | ||||
231 | | ||||
232 | AppImageExtractor::AppImageExtractor(QObject* parent) | ||||
233 | : ExtractorPlugin(parent) | ||||
234 | { | ||||
235 | } | ||||
236 | | ||||
237 | QStringList AppImageExtractor::mimetypes() const | ||||
238 | { | ||||
239 | return QStringList{ | ||||
240 | QStringLiteral("application/x-iso9660-appimage"), | ||||
241 | QStringLiteral("application/vnd.appimage"), | ||||
242 | }; | ||||
243 | } | ||||
244 | | ||||
245 | void KFileMetaData::AppImageExtractor::extract(ExtractionResult* result) | ||||
246 | { | ||||
247 | const auto appImageFilePath = result->inputUrl().toUtf8(); | ||||
248 | const auto appImageType = appimage_get_type(appImageFilePath.constData(), false); | ||||
249 | // not a valid appimage file? | ||||
250 | if (appImageType <= 0) { | ||||
251 | return; | ||||
252 | } | ||||
253 | | ||||
254 | // find desktop file and appdata file | ||||
255 | // need to scan ourselves, given there are no fixed names in the spec yet defined | ||||
256 | // and we just can try as the other appimage tools to simply use the first file of the type found | ||||
257 | char** filePaths = appimage_list_files(appImageFilePath.constData()); | ||||
258 | if (!filePaths) { | ||||
259 | return; | ||||
260 | } | ||||
261 | | ||||
262 | QString desktopFilePath; | ||||
263 | QString appdataFilePath; | ||||
264 | for (int i = 0; filePaths[i] != nullptr; ++i) { | ||||
265 | const auto filePath = QString::fromUtf8(filePaths[i]); | ||||
266 | | ||||
267 | if (filePath.startsWith(QLatin1String("usr/share/metainfo/")) && | ||||
268 | filePath.endsWith(QLatin1String(".appdata.xml"))) { | ||||
269 | appdataFilePath = filePath; | ||||
270 | if (!desktopFilePath.isEmpty()) { | ||||
271 | break; | ||||
272 | } | ||||
273 | } | ||||
274 | | ||||
275 | if (filePath.endsWith(QLatin1String(".desktop")) && !filePath.contains(QLatin1Char('/'))) { | ||||
276 | desktopFilePath = filePath; | ||||
277 | if (!appdataFilePath.isEmpty()) { | ||||
278 | break; | ||||
279 | } | ||||
280 | } | ||||
281 | } | ||||
282 | | ||||
283 | appimage_string_list_free(filePaths); | ||||
284 | | ||||
285 | // extract data from both files... | ||||
286 | const AppDataParser appData(appImageFilePath.constData(), appdataFilePath); | ||||
287 | | ||||
288 | const DesktopFileParser desktopFileData(appImageFilePath.constData(), desktopFilePath); | ||||
289 | | ||||
290 | // ... and insert into the result | ||||
291 | result->add(Property::Title, desktopFileData.name); | ||||
292 | | ||||
293 | if (!desktopFileData.comment.isEmpty()) { | ||||
294 | result->add(Property::Comment, desktopFileData.comment); | ||||
295 | } else if (!appData.summary().isEmpty()) { | ||||
296 | result->add(Property::Comment, appData.summary()); | ||||
297 | } | ||||
298 | if (!appData.description().isEmpty()) { | ||||
299 | result->add(Property::Description, appData.description()); | ||||
300 | } | ||||
301 | if (!appData.projectLicense().isEmpty()) { | ||||
302 | result->add(Property::License, appData.projectLicense()); | ||||
303 | } | ||||
304 | if (!appData.developerName().isEmpty()) { | ||||
305 | result->add(Property::Author, appData.developerName()); | ||||
306 | } | ||||
307 | } |