1 #include "extralicensemanager.h"
2 #include <QDebug>
3 #include <QFile>
4
ExtraLicenseManager()5 ExtraLicenseManager::ExtraLicenseManager()
6 {
7 }
8
~ExtraLicenseManager()9 ExtraLicenseManager::~ExtraLicenseManager()
10 {
11 for (License* lic : licenses.values())
12 delete lic;
13
14 licenses.clear();
15 }
16
addLicense(const QString & title,const QString & filePath)17 bool ExtraLicenseManager::addLicense(const QString& title, const QString& filePath)
18 {
19 return addLicense(title, filePath, Type::FILE);
20 }
21
addLicenseContents(const QString & title,const QString & contents)22 bool ExtraLicenseManager::addLicenseContents(const QString& title, const QString& contents)
23 {
24 return addLicense(title, contents, Type::CONTENTS);
25 }
26
setViolatedLicense(const QString & title,const QString & violationMessage)27 void ExtraLicenseManager::setViolatedLicense(const QString& title, const QString& violationMessage)
28 {
29 if (!licenses.contains(title))
30 return;
31
32 License* lic = licenses[title];
33 lic->violated = true;
34 lic->violationMessage = violationMessage;
35 }
36
unsetViolatedLicense(const QString & title)37 void ExtraLicenseManager::unsetViolatedLicense(const QString& title)
38 {
39 if (!licenses.contains(title))
40 return;
41
42 License* lic = licenses[title];
43 lic->violated = false;
44 lic->violationMessage = QString();
45 }
46
isViolatedLicense(const QString & title)47 bool ExtraLicenseManager::isViolatedLicense(const QString& title)
48 {
49 if (!licenses.contains(title))
50 return false;
51
52 return licenses[title]->violated;
53 }
54
getViolationMessage(const QString & title)55 QString ExtraLicenseManager::getViolationMessage(const QString& title)
56 {
57 if (!licenses.contains(title))
58 return QString();
59
60 return licenses[title]->violationMessage;
61 }
62
removeLicense(const QString & title)63 bool ExtraLicenseManager::removeLicense(const QString& title)
64 {
65 if (!licenses.contains(title))
66 return false;
67
68 delete licenses[title];
69 licenses.remove(title);
70 return true;
71 }
72
getLicensesContents() const73 QHash<QString, QString> ExtraLicenseManager::getLicensesContents() const
74 {
75 QHash<QString, QString> result;
76 License* lic = nullptr;
77 for (const QString& title : licenses.keys())
78 {
79 lic = licenses[title];
80 switch (lic->type)
81 {
82 case Type::CONTENTS:
83 result[title] = lic->data;
84 break;
85 case Type::FILE:
86 result[title] = readLicenseFile(lic->data);
87 break;
88 }
89 }
90 return result;
91 }
92
addLicense(const QString & title,const QString & data,ExtraLicenseManager::Type type)93 bool ExtraLicenseManager::addLicense(const QString& title, const QString& data, ExtraLicenseManager::Type type)
94 {
95 if (licenses.contains(title))
96 return false;
97
98 License* lic = new License;
99 lic->title = title;
100 lic->data = data;
101 lic->type = type;
102 licenses[title] = lic;
103 return true;
104 }
105
readLicenseFile(const QString & path) const106 QString ExtraLicenseManager::readLicenseFile(const QString& path) const
107 {
108 QFile file(path);
109 if (!file.open(QIODevice::ReadOnly))
110 {
111 qCritical() << "Error opening" << file.fileName();
112 return QString();
113 }
114 QString contents = QString::fromLatin1(file.readAll());
115 file.close();
116 return contents;
117 }
118