diff --git a/src/CopyrighFixer/CopyrighFixer/owner.cpp b/src/CopyrighFixer/CopyrighFixer/owner.cpp index dd8a233..208b49d 100644 --- a/src/CopyrighFixer/CopyrighFixer/owner.cpp +++ b/src/CopyrighFixer/CopyrighFixer/owner.cpp @@ -28,4 +28,30 @@ const int &Owner::getTimePoint() const { 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; +} + }; diff --git a/src/CopyrighFixer/CopyrighFixer/owner.h b/src/CopyrighFixer/CopyrighFixer/owner.h index 8f8a3f1..14f6fa8 100644 --- a/src/CopyrighFixer/CopyrighFixer/owner.h +++ b/src/CopyrighFixer/CopyrighFixer/owner.h @@ -10,6 +10,7 @@ #include "CopyrighFixer_global.h" #include +#include namespace CopyrighFixer{ @@ -36,17 +37,36 @@ public: * @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. */ - void setTimePoint(const QString &interval); + void setTimePoint(const int &interval); /** * @brief getTimeRange The method changes the timestamp of the usage. * @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: - QString _name; - int _timePoint; + QString _name = ""; + int _timePoint = 1; }; diff --git a/src/CopyrighFixer/CopyrighFixer/sign.cpp b/src/CopyrighFixer/CopyrighFixer/sign.cpp index cbddd22..9664e77 100644 --- a/src/CopyrighFixer/CopyrighFixer/sign.cpp +++ b/src/CopyrighFixer/CopyrighFixer/sign.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -57,8 +56,7 @@ bool Signature::fromJson(const QString &pathToFile) { return false; } - QByteArray byteSignFile; - byteSignFile = file.readAll(); + QByteArray byteSignFile(file.readAll()); file.close(); QJsonDocument jsDoc(QJsonDocument::fromJson(byteSignFile)); @@ -77,18 +75,18 @@ bool Signature::fromJson(const QString &pathToFile) { return false; } - _licenseTitle = jsObj["license"].toString(); - _customMessage = jsObj["licenseText"].toString(); - - Owner OwnObj; - QMap ownMap; - -// _ownersMap = jsObj["ownersList"] - - + Owner ownObj; + QJsonArray ownLst(jsObj.value("ownersList").toArray()); + for (auto itemLst = ownLst.cbegin(); itemLst != ownLst.end(); ++itemLst) { + ownObj.fromjson(itemLst->toObject()); + _ownersMap.insert(itemLst->toObject().value("timePoint").toInt(), ownObj); + } + _licenseTitle = jsObj.value("license").toString(); + _customMessage = jsObj.value("licenseText").toString(); return true; + } else { return false; } @@ -97,9 +95,14 @@ bool Signature::fromJson(const QString &pathToFile) { bool Signature::toJson(QString &pathToFile) const { + QFileInfo checkFile(pathToFile); + if (checkFile.exists() && checkFile.isFile()) { + QFile::remove(pathToFile); + } + + QJsonArray lstObjown; QJsonObject objOwner; - for (auto obj = _ownersMap.cbegin(); obj != _ownersMap.end(); ++obj) { objOwner["timePoint"] = obj.value().getTimePoint(); objOwner["name"] = obj.value().getOwnerName(); @@ -124,7 +127,19 @@ bool Signature::toJson(QString &pathToFile) const { saveFile.write(jsonDoc); 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; } } diff --git a/src/CopyrighFixer/CopyrighFixer/sign.h b/src/CopyrighFixer/CopyrighFixer/sign.h index 9c429ca..92ef0a2 100644 --- a/src/CopyrighFixer/CopyrighFixer/sign.h +++ b/src/CopyrighFixer/CopyrighFixer/sign.h @@ -70,10 +70,17 @@ public: */ bool toJson(QString &pathToFile) const; + /** + * @brief isValid Checks if an object is initialized. + * @return Returns true if object is initialized. + */ + bool isValid() const; + private: QMap _ownersMap; - QString _licenseTitle; - QString _customMessage; + QString _licenseTitle = ""; + QString _customMessage = ""; + }; } diff --git a/tests/units/signtest.cpp b/tests/units/signtest.cpp index 1a558c4..f6acc49 100644 --- a/tests/units/signtest.cpp +++ b/tests/units/signtest.cpp @@ -21,8 +21,23 @@ void SignTest::test() { } void SignTest::testJsonObj() { - CopyrighFixer::Signature sign_fromJson; - CopyrighFixer::Signature sign_toJson; + QString filename = "signature.json"; - QVERIFY(sign_fromJson.fromJson() == sign_toJson.toJson()); + CopyrighFixer::Owner ownerObj; + ownerObj.setName("QuasarApp"); + ownerObj.setTimePoint(QDateTime::currentMSecsSinceEpoch()); + + QMap 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()); }