mirror of
https://github.com/QuasarApp/CopyrightFixer.git
synced 2025-04-27 10:14:40 +00:00
ref #46 Added methods is fromJson and toJson, tests.
This commit is contained in:
parent
c0e4477605
commit
e7dae7c6da
@ -28,4 +28,30 @@ const int &Owner::getTimePoint() const {
|
|||||||
return _timePoint;
|
return _timePoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Owner::fromjson(const QJsonObject &objJs) {
|
||||||
|
if (objJs.contains("name") && objJs.value("name").isString()) {
|
||||||
|
_name = objJs.value("name").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (objJs.contains("timePoint")) {
|
||||||
|
_timePoint = objJs.value("timePoint").toInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const QJsonObject &Owner::toJson(QJsonObject &objJs) const {
|
||||||
|
|
||||||
|
objJs["name"] = _name;
|
||||||
|
objJs["timePoint"] = _timePoint;
|
||||||
|
|
||||||
|
return objJs;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Owner::isValid() const {
|
||||||
|
if (_name.size() != 0 && _timePoint > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include "CopyrighFixer_global.h"
|
#include "CopyrighFixer_global.h"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
namespace CopyrighFixer{
|
namespace CopyrighFixer{
|
||||||
|
|
||||||
@ -36,17 +37,36 @@ public:
|
|||||||
* @brief setTimeRange A method that allows you to set the time interval for using a file.
|
* @brief setTimeRange A method that allows you to set the time interval for using a file.
|
||||||
* @param interval This is a string value indicating the date of ownership of the file.
|
* @param interval This is a string value indicating the date of ownership of the file.
|
||||||
*/
|
*/
|
||||||
void setTimePoint(const QString &interval);
|
void setTimePoint(const int &interval);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief getTimeRange The method changes the timestamp of the usage.
|
* @brief getTimeRange The method changes the timestamp of the usage.
|
||||||
* @return the time interval when the file was modified
|
* @return the time interval when the file was modified
|
||||||
*/
|
*/
|
||||||
const QString& getTimePoint() const;
|
const int& getTimePoint() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief fromjson Reads data from json file.
|
||||||
|
* @param objJs It's object json that contains information about the owner.
|
||||||
|
*/
|
||||||
|
void fromjson(const QJsonObject &objJs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief toJson This method that converts object Owner to json object.
|
||||||
|
* @param objJs It's object json - container.
|
||||||
|
* @return Returns a json object with owner a information.
|
||||||
|
*/
|
||||||
|
const QJsonObject &toJson(QJsonObject &objJs) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief isValid Checks if an object is initialized.
|
||||||
|
* @return Returns true if object is initialized.
|
||||||
|
*/
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString _name;
|
QString _name = "";
|
||||||
int _timePoint;
|
int _timePoint = 1;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
#include <QJsonObject>
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
|
||||||
@ -57,8 +56,7 @@ bool Signature::fromJson(const QString &pathToFile) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray byteSignFile;
|
QByteArray byteSignFile(file.readAll());
|
||||||
byteSignFile = file.readAll();
|
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
QJsonDocument jsDoc(QJsonDocument::fromJson(byteSignFile));
|
QJsonDocument jsDoc(QJsonDocument::fromJson(byteSignFile));
|
||||||
@ -77,18 +75,18 @@ bool Signature::fromJson(const QString &pathToFile) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_licenseTitle = jsObj["license"].toString();
|
Owner ownObj;
|
||||||
_customMessage = jsObj["licenseText"].toString();
|
QJsonArray ownLst(jsObj.value("ownersList").toArray());
|
||||||
|
for (auto itemLst = ownLst.cbegin(); itemLst != ownLst.end(); ++itemLst) {
|
||||||
Owner OwnObj;
|
ownObj.fromjson(itemLst->toObject());
|
||||||
QMap<int, Owner> ownMap;
|
_ownersMap.insert(itemLst->toObject().value("timePoint").toInt(), ownObj);
|
||||||
|
}
|
||||||
// _ownersMap = jsObj["ownersList"]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
_licenseTitle = jsObj.value("license").toString();
|
||||||
|
_customMessage = jsObj.value("licenseText").toString();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -97,9 +95,14 @@ bool Signature::fromJson(const QString &pathToFile) {
|
|||||||
|
|
||||||
bool Signature::toJson(QString &pathToFile) const {
|
bool Signature::toJson(QString &pathToFile) const {
|
||||||
|
|
||||||
|
QFileInfo checkFile(pathToFile);
|
||||||
|
if (checkFile.exists() && checkFile.isFile()) {
|
||||||
|
QFile::remove(pathToFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QJsonArray lstObjown;
|
QJsonArray lstObjown;
|
||||||
QJsonObject objOwner;
|
QJsonObject objOwner;
|
||||||
|
|
||||||
for (auto obj = _ownersMap.cbegin(); obj != _ownersMap.end(); ++obj) {
|
for (auto obj = _ownersMap.cbegin(); obj != _ownersMap.end(); ++obj) {
|
||||||
objOwner["timePoint"] = obj.value().getTimePoint();
|
objOwner["timePoint"] = obj.value().getTimePoint();
|
||||||
objOwner["name"] = obj.value().getOwnerName();
|
objOwner["name"] = obj.value().getOwnerName();
|
||||||
@ -124,7 +127,19 @@ bool Signature::toJson(QString &pathToFile) const {
|
|||||||
saveFile.write(jsonDoc);
|
saveFile.write(jsonDoc);
|
||||||
saveFile.close();
|
saveFile.close();
|
||||||
|
|
||||||
return 1;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Signature::isValid() const {
|
||||||
|
if (_licenseTitle.size() != 0 || _customMessage != 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_ownersMap.cbegin().key() > 0 && _ownersMap.cbegin().value().isValid()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -70,10 +70,17 @@ public:
|
|||||||
*/
|
*/
|
||||||
bool toJson(QString &pathToFile) const;
|
bool toJson(QString &pathToFile) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief isValid Checks if an object is initialized.
|
||||||
|
* @return Returns true if object is initialized.
|
||||||
|
*/
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMap<int, Owner> _ownersMap;
|
QMap<int, Owner> _ownersMap;
|
||||||
QString _licenseTitle;
|
QString _licenseTitle = "";
|
||||||
QString _customMessage;
|
QString _customMessage = "";
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,23 @@ void SignTest::test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SignTest::testJsonObj() {
|
void SignTest::testJsonObj() {
|
||||||
CopyrighFixer::Signature sign_fromJson;
|
QString filename = "signature.json";
|
||||||
CopyrighFixer::Signature sign_toJson;
|
|
||||||
|
|
||||||
QVERIFY(sign_fromJson.fromJson() == sign_toJson.toJson());
|
CopyrighFixer::Owner ownerObj;
|
||||||
|
ownerObj.setName("QuasarApp");
|
||||||
|
ownerObj.setTimePoint(QDateTime::currentMSecsSinceEpoch());
|
||||||
|
|
||||||
|
QMap<int, CopyrighFixer::Owner> OwnerMap;
|
||||||
|
OwnerMap.insert(ownerObj.getTimePoint(), ownerObj);
|
||||||
|
|
||||||
|
CopyrighFixer::Signature sign_toJson;
|
||||||
|
sign_toJson.setLicenseTitle("Copyright (C) 2020-2021 QuasarApp.");
|
||||||
|
sign_toJson.setMessage("Distributed under the lgplv3 software license, see the accompany.");
|
||||||
|
sign_toJson.setMapOwners(OwnerMap);
|
||||||
|
sign_toJson.toJson(filename);
|
||||||
|
|
||||||
|
CopyrighFixer::Signature sign_fromJson;
|
||||||
|
sign_fromJson.fromJson(filename);
|
||||||
|
|
||||||
|
QVERIFY(sign_fromJson.isValid() == sign_toJson.isValid());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user