1 /*
2  * %kadu copyright begin%
3  * Copyright 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "plugin-metadata-reader.h"
21 
22 #include "configuration/configuration.h"
23 #include "configuration/deprecated-configuration-api.h"
24 #include "core/version-service.h"
25 #include "plugin/metadata/plugin-metadata-builder.h"
26 #include "plugin/metadata/plugin-metadata-reader-exception.h"
27 #include "plugin/metadata/plugin-metadata.h"
28 
29 #include <QtCore/QFileInfo>
30 #include <QtCore/QSettings>
31 
PluginMetadataReader(QObject * parent)32 PluginMetadataReader::PluginMetadataReader(QObject *parent) noexcept :
33 		QObject(parent)
34 {
35 }
36 
~PluginMetadataReader()37 PluginMetadataReader::~PluginMetadataReader() noexcept
38 {
39 }
40 
setConfiguration(Configuration * configuration)41 void PluginMetadataReader::setConfiguration(Configuration *configuration)
42 {
43 	m_configuration = configuration;
44 }
45 
setVersionService(VersionService * versionService)46 void PluginMetadataReader::setVersionService(VersionService *versionService)
47 {
48 	m_versionService = versionService;
49 }
50 
readPluginMetadata(const QString & pluginName,const QString & filePath)51 PluginMetadata PluginMetadataReader::readPluginMetadata(const QString &pluginName, const QString &filePath) noexcept(false)
52 {
53 	auto fileInfo = QFileInfo{filePath};
54 	if (!fileInfo.exists() || !fileInfo.isReadable())
55 		throw PluginMetadataReaderException{};
56 
57 	auto const lang = m_configuration->deprecatedApi()->readEntry("General", "Language");
58 	QSettings file{filePath, QSettings::IniFormat};
59 	file.setIniCodec("UTF-8");
60 
61 	auto builder = PluginMetadataBuilder{};
62 	return builder
63 			.setName(pluginName)
64 			.setDisplayName(file.value("Module/DisplayName[" + lang + ']', file.value("Module/DisplayName")).toString())
65 			.setCategory(file.value("Module/Category").toString())
66 			.setType(file.value("Module/Type").toString())
67 			.setDescription(file.value("Module/Description[" + lang + ']', file.value("Module/Description")).toString())
68 			.setAuthor(file.value("Module/Author").toString())
69 			.setVersion(file.value("Module/Version").toString() == "core"
70 					? m_versionService->version()
71 					: file.value("Module/Version").toString())
72 			.setProvides(file.value("Module/Provides").toString())
73 			.setDependencies(file.value("Module/Dependencies").toString().split(' ', QString::SkipEmptyParts))
74 			.setReplaces(file.value("Module/Replaces").toString().split(' ', QString::SkipEmptyParts))
75 			.setLoadByDefault(file.value("Module/LoadByDefault").toBool())
76 			.create();
77 }
78