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 summary() const { return !m_localized.summary.isEmpty() ? m_localized.summary : m_unlocalized.summary; } | ||||
51 | QString description() const { return !m_localized.description.isEmpty() ? m_localized.description : m_unlocalized.description; } | ||||
52 | QString developerName() const { return !m_localized.developerName.isEmpty() ? m_localized.developerName : m_unlocalized.developerName; } | ||||
53 | QString projectLicense() const { return m_projectLicense; } | ||||
54 | | ||||
55 | private: | ||||
56 | void extractDescription(const QDomElement& e, const QString& localeName); | ||||
57 | | ||||
58 | private: | ||||
59 | struct Data { | ||||
60 | QString summary; | ||||
61 | QString description; | ||||
62 | QString developerName; | ||||
63 | }; | ||||
64 | Data m_localized; | ||||
65 | Data m_unlocalized; | ||||
66 | QString m_projectLicense; | ||||
67 | }; | ||||
68 | | ||||
69 | | ||||
70 | AppDataParser::AppDataParser(const char* appImageFilePath, const QString& appdataFilePath) | ||||
71 | { | ||||
72 | if (appdataFilePath.isEmpty()) { | ||||
73 | return; | ||||
74 | } | ||||
75 | | ||||
76 | unsigned long size = 0L; | ||||
77 | char* buf = nullptr; | ||||
78 | bool ok = appimage_read_file_into_buffer_following_symlinks(appImageFilePath, | ||||
79 | qUtf8Printable(appdataFilePath), | ||||
80 | &buf, | ||||
81 | &size); | ||||
82 | | ||||
83 | QScopedPointer<char, QScopedPointerPodDeleter> cleanup(buf); | ||||
84 | | ||||
85 | if (!ok) { | ||||
86 | return; | ||||
87 | } | ||||
88 | | ||||
89 | QDomDocument domDocument; | ||||
90 | if (!domDocument.setContent(QByteArray::fromRawData(buf, size))) { | ||||
91 | return; | ||||
92 | } | ||||
93 | | ||||
94 | QDomElement docElem = domDocument.documentElement(); | ||||
95 | if (docElem.tagName() != QLatin1String("component")) { | ||||
96 | return; | ||||
97 | } | ||||
98 | | ||||
99 | const auto localeName = QLocale::system().bcp47Name(); | ||||
100 | | ||||
101 | QDomElement ec = docElem.firstChildElement(); | ||||
102 | while (!ec.isNull()) { | ||||
103 | const auto tagName = ec.tagName(); | ||||
104 | const auto hasLangAttribute = ec.hasAttribute(AttributeNames::xml_lang()); | ||||
105 | const auto matchingLocale = hasLangAttribute && (ec.attribute(AttributeNames::xml_lang()) == localeName); | ||||
106 | if (matchingLocale || !hasLangAttribute) { | ||||
107 | if (tagName == QLatin1String("summary")) { | ||||
108 | Data& data = hasLangAttribute ? m_localized : m_unlocalized; | ||||
109 | data.summary = ec.text(); | ||||
110 | } else if (tagName == QLatin1String("description")) { | ||||
111 | extractDescription(ec, localeName); | ||||
112 | } else if (tagName == QLatin1String("developer_name")) { | ||||
113 | Data& data = hasLangAttribute ? m_localized : m_unlocalized; | ||||
114 | data.developerName = ec.text(); | ||||
115 | } else if (tagName == QLatin1String("project_license")) { | ||||
116 | m_projectLicense = ec.text(); | ||||
117 | } | ||||
118 | } | ||||
119 | ec = ec.nextSiblingElement(); | ||||
120 | } | ||||
121 | } | ||||
122 | | ||||
123 | using DesriptionDomFilter = std::function<bool(const QDomElement& e)>; | ||||
124 | | ||||
125 | void stripDescriptionTextElements(QDomElement& element, const DesriptionDomFilter& stripFilter) | ||||
126 | { | ||||
127 | auto childElement = element.firstChildElement(); | ||||
128 | while (!childElement.isNull()) { | ||||
129 | auto nextChildElement = childElement.nextSiblingElement(); | ||||
130 | | ||||
131 | const auto tagName = childElement.tagName(); | ||||
132 | const bool isElementToFilter = (tagName == QLatin1String("p")) || (tagName == QLatin1String("li")); | ||||
133 | if (isElementToFilter && stripFilter(childElement)) { | ||||
134 | element.removeChild(childElement); | ||||
135 | } else { | ||||
136 | stripDescriptionTextElements(childElement, stripFilter); | ||||
137 | } | ||||
138 | | ||||
139 | childElement = nextChildElement; | ||||
140 | } | ||||
141 | } | ||||
142 | | ||||
143 | void AppDataParser::extractDescription(const QDomElement& e, const QString& localeName) | ||||
144 | { | ||||
145 | // create fake html from it and let QTextDocument transform it to plain text for us | ||||
146 | QDomDocument descriptionDocument; | ||||
147 | auto htmlElement = descriptionDocument.createElement(QStringLiteral("html")); | ||||
148 | descriptionDocument.appendChild(htmlElement); | ||||
149 | | ||||
150 | // first localized... | ||||
151 | auto clonedE = descriptionDocument.importNode(e, true).toElement(); | ||||
152 | clonedE.setTagName(QStringLiteral("body")); | ||||
153 | stripDescriptionTextElements(clonedE, [localeName](const QDomElement& e) { | ||||
154 | return !e.hasAttribute(AttributeNames::xml_lang()) || | ||||
155 | e.attribute(AttributeNames::xml_lang()) != localeName; | ||||
156 | }); | ||||
157 | htmlElement.appendChild(clonedE); | ||||
158 | | ||||
159 | QTextDocument textDocument; | ||||
160 | textDocument.setHtml(descriptionDocument.toString(-1)); | ||||
161 | | ||||
162 | m_localized.description = textDocument.toPlainText().trimmed(); | ||||
163 | | ||||
164 | if (!m_localized.description.isEmpty()) { | ||||
165 | // localized will be preferred, no need to calculate unlocalized one | ||||
166 | return; | ||||
167 | } | ||||
168 | | ||||
169 | // then unlocalized if still needed | ||||
170 | htmlElement.removeChild(clonedE); // reuse descriptionDocument | ||||
171 | clonedE = descriptionDocument.importNode(e, true).toElement(); | ||||
172 | clonedE.setTagName(QStringLiteral("body")); | ||||
173 | stripDescriptionTextElements(clonedE, [](const QDomElement& e) { | ||||
174 | return e.hasAttribute(AttributeNames::xml_lang()); | ||||
175 | }); | ||||
176 | htmlElement.appendChild(clonedE); | ||||
177 | | ||||
178 | textDocument.setHtml(descriptionDocument.toString(-1)); | ||||
179 | | ||||
180 | m_unlocalized.description = textDocument.toPlainText().trimmed(); | ||||
181 | } | ||||
182 | | ||||
183 | | ||||
184 | // helper class to extract the interesting data from the desktop file | ||||
185 | class DesktopFileParser | ||||
186 | { | ||||
187 | public: | ||||
188 | DesktopFileParser(const char* appImageFilePath, const QString& desktopFilePath); | ||||
189 | | ||||
190 | public: | ||||
191 | QString name; | ||||
192 | QString comment; | ||||
193 | }; | ||||
194 | | ||||
195 | | ||||
196 | DesktopFileParser::DesktopFileParser(const char* appImageFilePath, const QString& desktopFilePath) | ||||
197 | { | ||||
198 | if (desktopFilePath.isEmpty()) { | ||||
199 | return; | ||||
200 | } | ||||
201 | | ||||
202 | unsigned long size = 0L; | ||||
203 | char* buf = nullptr; | ||||
204 | bool ok = appimage_read_file_into_buffer_following_symlinks(appImageFilePath, | ||||
205 | qUtf8Printable(desktopFilePath), | ||||
206 | &buf, | ||||
207 | &size); | ||||
208 | | ||||
209 | QScopedPointer<char, QScopedPointerPodDeleter> cleanup(buf); | ||||
210 | | ||||
211 | if (!ok) { | ||||
212 | return; | ||||
213 | } | ||||
214 | | ||||
215 | // create real file, KDesktopFile needs that | ||||
216 | QTemporaryFile tmpDesktopFile; | ||||
217 | tmpDesktopFile.open(); | ||||
218 | tmpDesktopFile.write(buf, size); | ||||
219 | tmpDesktopFile.close(); | ||||
220 | | ||||
221 | KDesktopFile desktopFile(tmpDesktopFile.fileName()); | ||||
222 | name = desktopFile.readName(); | ||||
223 | comment = desktopFile.readComment(); | ||||
224 | } | ||||
225 | | ||||
226 | | ||||
227 | AppImageExtractor::AppImageExtractor(QObject* parent) | ||||
228 | : ExtractorPlugin(parent) | ||||
229 | { | ||||
230 | } | ||||
231 | | ||||
232 | QStringList AppImageExtractor::mimetypes() const | ||||
233 | { | ||||
234 | return QStringList{ | ||||
235 | QStringLiteral("application/x-iso9660-appimage"), | ||||
236 | QStringLiteral("application/vnd.appimage"), | ||||
237 | }; | ||||
238 | } | ||||
239 | | ||||
240 | void KFileMetaData::AppImageExtractor::extract(ExtractionResult* result) | ||||
241 | { | ||||
242 | const auto appImageFilePath = result->inputUrl().toUtf8(); | ||||
243 | const auto appImageType = appimage_get_type(appImageFilePath.constData(), false); | ||||
244 | // not a valid appimage file? | ||||
245 | if (appImageType <= 0) { | ||||
246 | return; | ||||
247 | } | ||||
248 | | ||||
249 | // find desktop file and appdata file | ||||
250 | // need to scan ourselves, given there are no fixed names in the spec yet defined | ||||
251 | // and we just can try as the other appimage tools to simply use the first file of the type found | ||||
252 | char** filePaths = appimage_list_files(appImageFilePath.constData()); | ||||
253 | if (!filePaths) { | ||||
254 | return; | ||||
255 | } | ||||
256 | | ||||
257 | QString desktopFilePath; | ||||
258 | QString appdataFilePath; | ||||
259 | for (int i = 0; filePaths[i] != nullptr; ++i) { | ||||
260 | const auto filePath = QString::fromUtf8(filePaths[i]); | ||||
261 | | ||||
262 | if (filePath.startsWith(QLatin1String("usr/share/metainfo/")) && | ||||
263 | filePath.endsWith(QLatin1String(".appdata.xml"))) { | ||||
264 | appdataFilePath = filePath; | ||||
265 | if (!desktopFilePath.isEmpty()) { | ||||
266 | break; | ||||
267 | } | ||||
268 | } | ||||
269 | | ||||
270 | if (filePath.endsWith(QLatin1String(".desktop")) && !filePath.contains(QLatin1Char('/'))) { | ||||
271 | desktopFilePath = filePath; | ||||
272 | if (!appdataFilePath.isEmpty()) { | ||||
273 | break; | ||||
274 | } | ||||
275 | } | ||||
276 | } | ||||
277 | | ||||
278 | appimage_string_list_free(filePaths); | ||||
279 | | ||||
280 | // extract data from both files... | ||||
281 | const AppDataParser appData(appImageFilePath.constData(), appdataFilePath); | ||||
282 | | ||||
283 | const DesktopFileParser desktopFileData(appImageFilePath.constData(), desktopFilePath); | ||||
284 | | ||||
285 | // ... and insert into the result | ||||
286 | result->add(Property::Title, desktopFileData.name); | ||||
287 | | ||||
288 | if (!desktopFileData.comment.isEmpty()) { | ||||
289 | result->add(Property::Comment, desktopFileData.comment); | ||||
290 | } else if (!appData.summary().isEmpty()) { | ||||
291 | result->add(Property::Comment, appData.summary()); | ||||
292 | } | ||||
293 | if (!appData.description().isEmpty()) { | ||||
294 | result->add(Property::Description, appData.description()); | ||||
295 | } | ||||
296 | if (!appData.projectLicense().isEmpty()) { | ||||
297 | result->add(Property::License, appData.projectLicense()); | ||||
298 | } | ||||
299 | if (!appData.developerName().isEmpty()) { | ||||
300 | result->add(Property::Author, appData.developerName()); | ||||
301 | } | ||||
302 | } |