4
0
mirror of https://github.com/QuasarApp/CopyrightFixer.git synced 2025-05-06 22:49:44 +00:00

Merge branch 'main' into task_50

This commit is contained in:
IgorekLoschinin 2021-05-08 11:05:34 +03:00
commit cdae9a86e8
10 changed files with 402 additions and 49 deletions

@ -7,45 +7,24 @@
#include <quasarapp.h>
#include <iostream>
#include <CopyrighFixer/worker.h>
using namespace CopyrighFixer;
void helpCall();
int main(int argc, char *argv[]) {
Worker worker;
if (!QuasarAppUtils::Params::parseParams(argc, argv)) {
helpCall();
worker.printHelp();
return 1;
}
if (QuasarAppUtils::Params::isEndable("h") || QuasarAppUtils::Params::isEndable("help")) {
helpCall();
worker.printHelp();
return 0;
}
return 0;
}
/**
* @brief helpCall - a helper call that display infotmation
* about the arguments and how to use them
*/
void helpCall(){
QuasarAppUtils::Help::Charters help = {
{
"Part 0 General", {
{"h or help", "These arguments represent a helper call that describes the functionality of each method"},
{"-sourceDir path/to/source/dir", "This arrgument sets path to the source directory. By default it is sourceDir = PWD"},
{"-sign path/to/sign/file", "This argument sets path to the sign patern. This is a required argument"},
{"-currentOwner ownerName", "This argument sets name of the current owner of the code."},
}
}
};
help += QuasarAppUtils::Params::getparamsHelp();
QuasarAppUtils::Params::showHelp(help);
exit(0);
}

@ -20,12 +20,36 @@ const QString &Owner::getOwnerName() const {
return _name;
}
void Owner::setTimeRange(const QString &interval) {
_timeRange = interval;
void Owner::setTimePoint(int interval) {
_timePoint = interval;
}
const QString &Owner::getTimeRange() const {
return _timeRange;
int Owner::getTimePoint() const {
return _timePoint;
}
void Owner::fromJson(const QJsonObject &objJs) {
_name = objJs.value("name").toString();
_timePoint = objJs.value("timePoint").toInt();
}
void Owner::toJson(QJsonObject &objJs) const {
objJs["name"] = _name;
objJs["timePoint"] = _timePoint;
}
bool Owner::isValid() const {
if (_name.size() != 0 && _timePoint > 0) {
return true;
}
return _name.size() && _timePoint > 0 && _timePoint <= time(0);
}
CopyrighFixer_EXPORT bool operator== (const Owner &left, const Owner &right) {
return (left._name == right._name &&
left._timePoint == right._timePoint);
}
};

@ -10,14 +10,14 @@
#include "CopyrighFixer_global.h"
#include <QString>
#include <QJsonObject>
namespace CopyrighFixer{
/**
* @brief The Owner class for collect information about owner.
*/
class CopyrighFixer_EXPORT Owner
{
class CopyrighFixer_EXPORT Owner {
public:
Owner();
/**
@ -36,16 +36,44 @@ 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 setTimeRange(const QString &interval);
void setTimePoint(int interval);
/**
* @brief getTimeRange The method changes the timestamp of the usage.
* @return the time interval when the file was modified
*/
const QString& getTimeRange() 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.
*/
void toJson(QJsonObject &objJs) const;
/**
* @brief isValid Checks if an object is initialized.
* @return Returns true if object is initialized.
*/
bool isValid() const;
/**
* @brief operator == Comparison operator overloading method
* @param left Left value.
* @param right Right value.
* @return Returns true if they are equal.
*/
CopyrighFixer_EXPORT friend bool operator== (const Owner &left, const Owner &right);
private:
QString _name;
QString _timeRange;
QString _name = "";
int _timePoint = 0;
};

@ -7,14 +7,22 @@
#include "sign.h"
#include "owner.h"
#include <quasarapp.h>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonValue>
#include <QByteArray>
#include <QJsonDocument>
namespace CopyrighFixer {
Signature::Signature() {
}
void Signature::setlistOwners(const QList<Owner> &objOwner) {
_ownersList = objOwner;
void Signature::setMapOwners(const QMap<int, Owner> &objOwner) {
_ownersMap = objOwner;
}
void Signature::setLicenseTitle(const QString &strTitle) {
@ -25,8 +33,8 @@ void Signature::setMessage(const QString &strMsg) {
_customMessage = strMsg;
}
const QList<Owner> &Signature::getLstOwn() const {
return _ownersList;
const QMap<int, Owner> &Signature::getMapOwn() const {
return _ownersMap;
}
const QString &Signature::getLicenseTitle() const {
@ -37,4 +45,107 @@ const QString &Signature::getMessage() const {
return _customMessage;
}
bool Signature::fromJson(const QString &pathToFile) {
QFile file(pathToFile);
if (file.exists()) {
if (!file.open(QIODevice::ReadOnly)) {
QuasarAppUtils::Params::log("Json file couldn't be opened/found");
return false;
}
QJsonDocument jsDoc(QJsonDocument::fromJson(file.readAll()));
file.close();
if (jsDoc.isNull()) {
QuasarAppUtils::Params::log("Failed to create JSON doc.");
return false;
}
if (!jsDoc.isObject()) {
QuasarAppUtils::Params::log("JSON is not an object.");
return false;
}
QJsonObject jsObj = jsDoc.object();
if (jsObj.isEmpty()) {
QuasarAppUtils::Params::log("JSON object is empty.");
return false;
}
Owner ownObj;
QJsonArray ownLst(jsObj.value("ownersList").toArray());
for (auto itemLst = ownLst.cbegin(); itemLst != ownLst.cend(); ++itemLst) {
ownObj.fromJson(itemLst->toObject());
_ownersMap.insert(ownObj.getTimePoint(), ownObj);
}
_licenseTitle = jsObj.value("license").toString();
_customMessage = jsObj.value("licenseText").toString();
return true;
} else {
return false;
}
}
bool Signature::toJson(QString &pathToFile) const {
QFile file(pathToFile);
if (file.exists()) {
QFile::remove(pathToFile);
}
QJsonArray lstObjown;
QJsonObject objOwner;
for (auto obj = _ownersMap.cbegin(); obj != _ownersMap.cend(); ++obj) {
objOwner["timePoint"] = obj.value().getTimePoint();
objOwner["name"] = obj.value().getOwnerName();
lstObjown.append(objOwner);
}
QJsonObject signJson;
signJson["ownersList"] = lstObjown;
signJson["license"] = _licenseTitle;
signJson["licenseText"] = _customMessage;
QByteArray jsonDoc;
jsonDoc = QJsonDocument(signJson).toJson();
QFile saveFile(pathToFile);
if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QuasarAppUtils::Params::log(saveFile.errorString());
return false;
}
saveFile.write(jsonDoc);
saveFile.close();
return true;
}
bool Signature::isValid() const {
if (!(_licenseTitle.size() && _customMessage.size())) {
return false;
}
for (auto it = _ownersMap.cbegin(); it != _ownersMap.cend(); ++it) {
if (!it.value().isValid()) {
return false;
}
}
return true;
}
CopyrighFixer_EXPORT bool operator== (const Signature &left, const Signature &right) {
return (left._customMessage == right._customMessage &&
left._licenseTitle == right._licenseTitle &&
left._ownersMap == right._ownersMap);
}
}

@ -10,13 +10,26 @@
#include "CopyrighFixer_global.h"
#include "CopyrighFixer/owner.h"
#include <QList>
#include <QMap>
namespace CopyrighFixer {
/**
* @brief The Signature class adds a copyright signature to each file based on the collected owner information.
* ### Structure of the json file.
* @code{json}
{
"license": "lgplv3.",
"licenseText": "Distributed under the lgplv3 software license, see the accompany.",
"ownersList": [
{
"name": "QuasarApp",
"timePoint": 1620149717
}
]
}
* @endcode
*/
class CopyrighFixer_EXPORT Signature {
public:
@ -26,7 +39,7 @@ public:
* @brief setlistOwners The method generates a list of owners.
* @param objOwner This is a structure with information about the owner.
*/
void setlistOwners(const QList<Owner> &objOwner);
void setMapOwners(const QMap<int, Owner> &objOwner);
/**
* @brief setLicenseTitle The method sets the copyright message.
@ -44,7 +57,7 @@ public:
* @brief getLstOwn The method allows you to get the current list of owners.
* @return List of owners with full information.
*/
const QList<Owner>& getLstOwn() const;
const QMap<int, Owner>& getMapOwn() const;
/**
* @brief getLicenseTitle Allows you to get a license description.
@ -58,10 +71,37 @@ public:
*/
const QString& getMessage() const;
/**
* @brief fromJson Reads data from json file.
* @return True if everything is correct, otherwise false.
*/
bool fromJson(const QString &pathToFile);
/**
* @brief toJson Converts the QJsonDocument to an file JSON.
* @return Returns true if the object exists and is filled correctly, otherwise false.
*/
bool toJson(QString &pathToFile) const;
/**
* @brief isValid Checks if an object is initialized.
* @return Returns true if object is initialized.
*/
bool isValid() const;
/**
* @brief operator == Comparison operator overloading method
* @param left Left value.
* @param right Right value.
* @return Returns true if they are equal.
*/
CopyrighFixer_EXPORT friend bool operator== (const Signature &left, const Signature &right);
private:
QList<Owner> _ownersList;
QString _licenseTitle;
QString _customMessage;
QMap<int, Owner> _ownersMap;
QString _licenseTitle = "";
QString _customMessage = "";
};
}

@ -0,0 +1,42 @@
//#
//# Copyright (C) 2021-2021 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "config.h"
#include "worker.h"
#include <quasarapp.h>
namespace CopyrighFixer {
Worker::Worker() {
}
bool Worker::run() {
return false;
}
void Worker::printHelp() const {
QuasarAppUtils::Help::Charters help = {
{
"Part 0 General", {
{"h or help", "These arguments represent a helper call that describes the functionality of each method"},
{"-sourceDir path/to/source/dir", "This arrgument sets path to the source directory. By default it is sourceDir = PWD"},
{"-sign path/to/sign/file", "This argument sets path to the sign patern. This is a required argument"},
{"-currentOwner ownerName", "This argument sets name of the current owner of the code."},
}
}
};
help += QuasarAppUtils::Params::getparamsHelp();
QuasarAppUtils::Params::showHelp(help);
}
};

@ -0,0 +1,40 @@
//#
//# Copyright (C) 2021-2021 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef WORKER_H
#define WORKER_H
#include "CopyrighFixer_global.h"
namespace CopyrighFixer {
/**
* @brief The Worker class will be control all work process.
*/
class CopyrighFixer_EXPORT Worker {
public:
Worker();
/**
* @brief run It is main method for control of all parsing process.
* @return Returns false if the program terminates with an error.
*/
bool run();
/**
* @brief printHelp Display infotmation about the arguments and how to use them.
*/
void printHelp() const;
};
}
#endif // WORKER_H

@ -7,6 +7,7 @@
#include <QtTest>
#include "cfixertest.h"
#include "signtest.h"
// Use This macros for initialize your own test classes.
// Check exampletests
@ -30,7 +31,8 @@ public:
private slots:
// BEGIN TESTS CASES
TestCase(exampleTest, ExampleTest)
TestCase(exampleTest, ExampleTest);
TestCase(signTest, SignTest);
// END TEST CASES
private:

61
tests/units/signtest.cpp Normal file

@ -0,0 +1,61 @@
//#
//# Copyright (C) 2020-2021 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include <time.h>
#include "CopyrighFixer/sign.h"
#include "signtest.h"
SignTest::SignTest() {
}
SignTest::~SignTest() {
}
void SignTest::test() {
testJsonObj();
}
CopyrighFixer::Signature SignTest::generateRandomSign(QString& filename) const {
int unixTime = time(0);
CopyrighFixer::Owner ownerObj;
ownerObj.setName("QuasarApp");
ownerObj.setTimePoint(unixTime);
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);
return sign_toJson;
}
void SignTest::testJsonObj() {
QString filename = "signature.json";
CopyrighFixer::Signature baseSign = generateRandomSign(filename);
QVERIFY(baseSign.toJson(filename));
QVERIFY(baseSign.isValid());
CopyrighFixer::Signature signFromFile;
// After initialise the Signature object should be invalid.
QVERIFY(!signFromFile.isValid());
QVERIFY(signFromFile.fromJson(filename));
QVERIFY(signFromFile == baseSign);
}

26
tests/units/signtest.h Normal file

@ -0,0 +1,26 @@
//#
//# Copyright (C) 2020-2021 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef SIGNTEST_H
#define SIGNTEST_H
#include "test.h"
#include "testutils.h"
#include "CopyrighFixer/sign.h"
#include <QTest>
class SignTest: public Test, protected TestUtils {
public:
SignTest();
~SignTest();
void test();
CopyrighFixer::Signature generateRandomSign(QString& filename) const;
void testJsonObj();
};
#endif // SIGNTEST_H