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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: CloudCompare project #
//# #
//##########################################################################
#include <QDebug>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include "ccDefaultPluginInterface.h"
#include "ccLog.h"
// This class keeps JSON from being included in the header
class ccDefaultPluginData
{
public:
inline QString field( const QString &fieldName )
{
return doc.object().value( fieldName ).toString();
}
ccPluginInterface::ReferenceList references( const QString &fieldName )
{
ccPluginInterface::ReferenceList list;
const QJsonArray array = doc.object().value( fieldName ).toArray();
for ( const QJsonValue &value : array )
{
const QJsonObject object = value.toObject();
list += ccPluginInterface::Reference{
object["text"].toString(),
object["url"].toString()
};
}
return list;
}
ccPluginInterface::ContactList contacts( const QString &fieldName )
{
ccPluginInterface::ContactList list;
const QJsonArray array = doc.object().value( fieldName ).toArray();
for ( const QJsonValue &value : array )
{
const QJsonObject object = value.toObject();
list += ccPluginInterface::Contact{
object["name"].toString(),
object["email"].toString()
};
}
return list;
}
QJsonDocument doc;
};
ccDefaultPluginInterface::ccDefaultPluginInterface( const QString &resourcePath ) :
m_data( new ccDefaultPluginData )
{
if ( resourcePath.isNull() )
{
return;
}
QFile myFile( resourcePath );
bool opened = myFile.open( QIODevice::ReadOnly );
if ( !opened )
{
ccLog::Error( QStringLiteral( "Could not load plugin resources: %1" ).arg( resourcePath ) );
return;
}
QByteArray json = myFile.readAll();
QJsonParseError jsonError;
m_data->doc = QJsonDocument::fromJson( json, &jsonError );
if ( jsonError.error != QJsonParseError::NoError )
{
ccLog::Error( QStringLiteral( "Could not parse plugin info: %1" ).arg( jsonError.errorString() ) );
return;
}
}
ccDefaultPluginInterface::~ccDefaultPluginInterface()
{
delete m_data;
}
bool ccDefaultPluginInterface::isCore() const
{
return m_data->doc.object().value( "core" ).toBool();
}
QString ccDefaultPluginInterface::getName() const
{
return m_data->field( "name" );
}
QString ccDefaultPluginInterface::getDescription() const
{
return m_data->field( "description" );
}
QIcon ccDefaultPluginInterface::getIcon() const
{
return QIcon( m_data->field( "icon" ) );
}
ccPluginInterface::ReferenceList ccDefaultPluginInterface::getReferences() const
{
return m_data->references( "references" );
}
ccPluginInterface::ContactList ccDefaultPluginInterface::getAuthors() const
{
return m_data->contacts( "authors" );
}
ccPluginInterface::ContactList ccDefaultPluginInterface::getMaintainers() const
{
return m_data->contacts( "maintainers" );
}
|