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
|
#include "extralicensemanager.h"
#include <QDebug>
#include <QFile>
ExtraLicenseManager::ExtraLicenseManager()
{
}
ExtraLicenseManager::~ExtraLicenseManager()
{
for (License* lic : licenses.values())
delete lic;
licenses.clear();
}
bool ExtraLicenseManager::addLicense(const QString& title, const QString& filePath)
{
return addLicense(title, filePath, Type::FILE);
}
bool ExtraLicenseManager::addLicenseContents(const QString& title, const QString& contents)
{
return addLicense(title, contents, Type::CONTENTS);
}
void ExtraLicenseManager::setViolatedLicense(const QString& title, const QString& violationMessage)
{
if (!licenses.contains(title))
return;
License* lic = licenses[title];
lic->violated = true;
lic->violationMessage = violationMessage;
}
void ExtraLicenseManager::unsetViolatedLicense(const QString& title)
{
if (!licenses.contains(title))
return;
License* lic = licenses[title];
lic->violated = false;
lic->violationMessage = QString();
}
bool ExtraLicenseManager::isViolatedLicense(const QString& title)
{
if (!licenses.contains(title))
return false;
return licenses[title]->violated;
}
QString ExtraLicenseManager::getViolationMessage(const QString& title)
{
if (!licenses.contains(title))
return QString();
return licenses[title]->violationMessage;
}
bool ExtraLicenseManager::removeLicense(const QString& title)
{
if (!licenses.contains(title))
return false;
delete licenses[title];
licenses.remove(title);
return true;
}
QHash<QString, QString> ExtraLicenseManager::getLicensesContents() const
{
QHash<QString, QString> result;
License* lic = nullptr;
for (const QString& title : licenses.keys())
{
lic = licenses[title];
switch (lic->type)
{
case Type::CONTENTS:
result[title] = lic->data;
break;
case Type::FILE:
result[title] = readLicenseFile(lic->data);
break;
}
}
return result;
}
bool ExtraLicenseManager::addLicense(const QString& title, const QString& data, ExtraLicenseManager::Type type)
{
if (licenses.contains(title))
return false;
License* lic = new License;
lic->title = title;
lic->data = data;
lic->type = type;
licenses[title] = lic;
return true;
}
QString ExtraLicenseManager::readLicenseFile(const QString& path) const
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
{
qCritical() << "Error opening" << file.fileName();
return QString();
}
QString contents = QString::fromLatin1(file.readAll());
file.close();
return contents;
}
|