App: refactor license handling to reduce code duplication

This commit is contained in:
wmayer
2023-02-12 18:40:35 +01:00
committed by abdullahtahiriyo
parent 91048d7900
commit 4036613fd2
8 changed files with 254 additions and 214 deletions

View File

@@ -163,6 +163,8 @@ SET(Document_CPP_SRCS
TextDocument.cpp
Link.cpp
LinkBaseExtensionPyImp.cpp
License.cpp
License.h
)
SET(Document_HPP_SRCS

View File

@@ -102,6 +102,7 @@ recompute path. Also, it enables more complicated dependencies beyond trees.
#include "ExpressionParser.h"
#include "GeoFeature.h"
#include "GeoFeatureGroupExtension.h"
#include "License.h"
#include "Link.h"
#include "MergeDocuments.h"
#include "Origin.h"
@@ -1550,67 +1551,10 @@ Document::Document(const char *name)
ADD_PROPERTY_TYPE(Uid, (id), 0, Prop_ReadOnly, "UUID of the document");
// license stuff
ADD_PROPERTY_TYPE(License, ("CC-BY 3.0"), 0, Prop_None, "License string of the Item");
ADD_PROPERTY_TYPE(LicenseURL, ("https://creativecommons.org/licenses/by/3.0/"), 0, Prop_None, "URL to the license text/contract");
// license stuff
int licenseId = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document")->GetInt("prefLicenseType", 0);
std::string license;
std::string licenseUrl;
switch (licenseId) {
case 0:
license = "All rights reserved";
licenseUrl = "https://en.wikipedia.org/wiki/All_rights_reserved";
break;
case 1:
license = "Creative Commons Attribution";
licenseUrl = "https://creativecommons.org/licenses/by/4.0/";
break;
case 2:
license = "Creative Commons Attribution-ShareAlike";
licenseUrl = "https://creativecommons.org/licenses/by-sa/4.0/";
break;
case 3:
license = "Creative Commons Attribution-NoDerivatives";
licenseUrl = "https://creativecommons.org/licenses/by-nd/4.0/";
break;
case 4:
license = "Creative Commons Attribution-NonCommercial";
licenseUrl = "https://creativecommons.org/licenses/by-nc/4.0/";
break;
case 5:
license = "Creative Commons Attribution-NonCommercial-ShareAlike";
licenseUrl = "https://creativecommons.org/licenses/by-nc-sa/4.0/";
break;
case 6:
license = "Creative Commons Attribution-NonCommercial-NoDerivatives";
licenseUrl = "https://creativecommons.org/licenses/by-nc-nd/4.0/";
break;
case 7:
license = "Public Domain";
licenseUrl = "https://en.wikipedia.org/wiki/Public_domain";
break;
case 8:
license = "FreeArt";
licenseUrl = "https://artlibre.org/licence/lal";
break;
case 9:
license = "CERN Open Hardware Licence strongly-reciprocal";
licenseUrl = "https://cern-ohl.web.cern.ch/";
break;
case 10:
license = "CERN Open Hardware Licence weakly-reciprocal";
licenseUrl = "https://cern-ohl.web.cern.ch/";
break;
case 11:
license = "CERN Open Hardware Licence permissive";
licenseUrl = "https://cern-ohl.web.cern.ch/";
break;
default:
license = "Other";
break;
}
long licenseId = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document")->GetInt("prefLicenseType", 0);
App::License licenseType{licenseId};
std::string license = licenseType.getLicense();
std::string licenseUrl = licenseType.getUrl();
licenseUrl = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document")->GetASCII("prefLicenseUrl", licenseUrl.c_str());
ADD_PROPERTY_TYPE(License, (license.c_str()), 0, Prop_None, "License string of the Item");

114
src/App/License.cpp Normal file
View File

@@ -0,0 +1,114 @@
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <algorithm>
#endif
#include "License.h"
using namespace App;
std::map<License::Type, License::LicenseItem> License::licenseItems;
License::License(License::Type type)
: type{type}
{
init();
}
License::License(long id)
: type{static_cast<Type>(id)}
{
if (id < 0 || id > static_cast<long>(Type::Other)) {
type = Type::Other;
}
init();
}
License::License(int id)
: License(static_cast<long>(id))
{
}
void License::init()
{
static bool first = true;
if (!first)
return;
first = false;
licenseItems[Type::AllRightsReserved] = LicenseItem{"All rights reserved",
"https://en.wikipedia.org/wiki/All_rights_reserved"};
licenseItems[Type::CC_BY_40] = LicenseItem{"Creative Commons Attribution",
"https://creativecommons.org/licenses/by/4.0/"};
licenseItems[Type::CC_BY_SA_40] = LicenseItem{"Creative Commons Attribution-ShareAlike",
"https://creativecommons.org/licenses/by-sa/4.0/"};
licenseItems[Type::CC_BY_ND_40] = LicenseItem{"Creative Commons Attribution-NoDerivatives",
"https://creativecommons.org/licenses/by-nd/4.0/"};
licenseItems[Type::CC_BY_NC_40] = LicenseItem{"Creative Commons Attribution-NonCommercial",
"https://creativecommons.org/licenses/by-nc/4.0/"};
licenseItems[Type::CC_BY_NC_SA_40] = LicenseItem{"Creative Commons Attribution-NonCommercial-ShareAlike",
"https://creativecommons.org/licenses/by-nc-sa/4.0/"};
licenseItems[Type::CC_BY_NC_ND_40] = LicenseItem{"Creative Commons Attribution-NonCommercial-NoDerivatives",
"https://creativecommons.org/licenses/by-nc-nd/4.0/"};
licenseItems[Type::PublicDomain] = LicenseItem{"Public Domain",
"https://en.wikipedia.org/wiki/Public_domain"};
licenseItems[Type::FreeArt] = LicenseItem{"FreeArt", "https://artlibre.org/licence/lal"};
licenseItems[Type::CERN_OHS_S] = LicenseItem{"CERN Open Hardware Licence strongly-reciprocal",
"https://cern-ohl.web.cern.ch/"};
licenseItems[Type::CERN_OHS_W] = LicenseItem{"CERN Open Hardware Licence weakly-reciprocal",
"https://cern-ohl.web.cern.ch/"};
licenseItems[Type::CERN_OHS_P] = LicenseItem{"CERN Open Hardware Licence permissive",
"https://cern-ohl.web.cern.ch/"};
licenseItems[Type::Other] = LicenseItem{"Other", ""};
}
License::Type License::getType() const
{
return type;
}
std::string License::getLicense() const
{
return licenseItems.at(type).license;
}
std::string License::getUrl() const
{
return licenseItems.at(type).url;
}
std::vector<std::string> License::getLicenses()
{
init();
std::vector<std::string> output;
output.reserve(licenseItems.size());
using Value = std::pair<Type, LicenseItem>;
std::transform(licenseItems.cbegin(), licenseItems.cend(), std::back_inserter(output), [](const Value& val) {
return val.second.license;
});
return output;
}

80
src/App/License.h Normal file
View File

@@ -0,0 +1,80 @@
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef APP_LICENSE_H
#define APP_LICENSE_H
#include <FCGlobal.h>
#include <string>
#include <map>
#include <vector>
namespace App {
/*!
* \brief The License class
* Handling of standard licenses.
*/
class AppExport License
{
public:
enum class Type {
AllRightsReserved,
CC_BY_40,
CC_BY_SA_40,
CC_BY_ND_40,
CC_BY_NC_40,
CC_BY_NC_SA_40,
CC_BY_NC_ND_40,
PublicDomain,
FreeArt,
CERN_OHS_S,
CERN_OHS_W,
CERN_OHS_P,
Other
};
explicit License(Type);
explicit License(long);
explicit License(int);
Type getType() const;
std::string getLicense() const;
std::string getUrl() const;
static std::vector<std::string> getLicenses();
private:
static void init();
private:
struct LicenseItem{
std::string license;
std::string url;
};
Type type;
static std::map<Type, LicenseItem> licenseItems;
};
}
#endif // APP_LICENSE_H

View File

@@ -29,11 +29,28 @@
#endif
#include <App/Document.h>
#include <App/License.h>
#include "DlgProjectInformationImp.h"
#include "ui_DlgProjectInformation.h"
#if 0 // needed for Qt's lupdate utility
qApp->translate("Gui::Dialog::DlgSettingsDocument", "All rights reserved");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-ShareAlike");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NoDerivatives");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NonCommercial");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NonCommercial-ShareAlike");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NonCommercial-NoDerivatives");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Public Domain");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "FreeArt");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "CERN Open Hardware Licence strongly-reciprocal");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "CERN Open Hardware Licence weakly-reciprocal");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "CERN Open Hardware Licence permissive");
qApp->translate("Gui::Dialog::DlgSettingsDocument", "Other");
#endif
using namespace Gui::Dialog;
/* TRANSLATOR Gui::Dialog::DlgProjectInformationImp */
@@ -59,23 +76,10 @@ DlgProjectInformationImp::DlgProjectInformationImp(App::Document* doc, QWidget*
ui->lineEditLastModDate->setText(QString::fromUtf8(doc->LastModifiedDate.getValue()));
ui->lineEditCompany->setText(QString::fromUtf8(doc->Company.getValue()));
QList<QByteArray> rawLicenses; rawLicenses
<< "All rights reserved"
<< "Creative Commons Attribution"
<< "Creative Commons Attribution-ShareAlike"
<< "Creative Commons Attribution-NoDerivatives"
<< "Creative Commons Attribution-NonCommercial"
<< "Creative Commons Attribution-NonCommercial-ShareAlike"
<< "Creative Commons Attribution-NonCommercial-NoDerivatives"
<< "Public Domain"
<< "FreeArt"
<< "CERN Open Hardware Licence strongly-reciprocal"
<< "CERN Open Hardware Licence weakly-reciprocal"
<< "CERN Open Hardware Licence permissive"
<< "Other";
for (QList<QByteArray>::iterator it = rawLicenses.begin(); it != rawLicenses.end(); ++it) {
QString text = QApplication::translate("Gui::Dialog::DlgSettingsDocument", it->constData());
ui->comboLicense->addItem(text, *it);
auto rawLicenses = App::License::getLicenses();
for (const auto& it : rawLicenses) {
QString text = QApplication::translate("Gui::Dialog::DlgSettingsDocument", it.c_str());
ui->comboLicense->addItem(text, QByteArray(it.c_str()));
}
int index = ui->comboLicense->findData(QByteArray(doc->License.getValue()));
@@ -145,41 +149,13 @@ void DlgProjectInformationImp::accept()
void DlgProjectInformationImp::onLicenseTypeChanged(int index)
{
switch (index) {
case 0:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://en.wikipedia.org/wiki/All_rights_reserved"));
break;
case 1:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://creativecommons.org/licenses/by/4.0/"));
break;
case 2:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-sa/4.0/"));
break;
case 3:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nd/4.0/"));
break;
case 4:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nc/4.0/"));
break;
case 5:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nc-sa/4.0/"));
break;
case 6:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nc-nd/4.0/"));
break;
case 7:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://en.wikipedia.org/wiki/Public_domain"));
break;
case 8:
ui->lineEditLicenseURL->setText(QString::fromLatin1("http://artlibre.org/licence/lal"));
break;
case 9: case 10: case 11:
ui->lineEditLicenseURL->setText(QString::fromLatin1("https://cern-ohl.web.cern.ch/"));
break;
default:
ui->lineEditLicenseURL->setText(QString::fromUtf8(_doc->LicenseURL.getValue()));
break;
App::License license{index};
std::string url = license.getUrl();
if (license.getType() == App::License::Type::Other) {
url = _doc->LicenseURL.getValue();
}
ui->lineEditLicenseURL->setText(QString::fromStdString(url));
}
/**

View File

@@ -680,71 +680,6 @@ You can also use the form: John Doe &lt;john@doe.com&gt;</string>
<property name="prefPath" stdset="0">
<cstring>Document</cstring>
</property>
<item>
<property name="text">
<string>All rights reserved</string>
</property>
</item>
<item>
<property name="text">
<string>Creative Commons Attribution</string>
</property>
</item>
<item>
<property name="text">
<string>Creative Commons Attribution-ShareAlike</string>
</property>
</item>
<item>
<property name="text">
<string>Creative Commons Attribution-NoDerivatives</string>
</property>
</item>
<item>
<property name="text">
<string>Creative Commons Attribution-NonCommercial</string>
</property>
</item>
<item>
<property name="text">
<string>Creative Commons Attribution-NonCommercial-ShareAlike</string>
</property>
</item>
<item>
<property name="text">
<string>Creative Commons Attribution-NonCommercial-NoDerivatives</string>
</property>
</item>
<item>
<property name="text">
<string>Public Domain</string>
</property>
</item>
<item>
<property name="text">
<string>FreeArt</string>
</property>
</item>
<item>
<property name="text">
<string>CERN Open Hardware Licence strongly-reciprocal</string>
</property>
</item>
<item>
<property name="text">
<string>CERN Open Hardware Licence weakly-reciprocal</string>
</property>
</item>
<item>
<property name="text">
<string>CERN Open Hardware Licence permissive</string>
</property>
</item>
<item>
<property name="text">
<string>Other</string>
</property>
</item>
</widget>
</item>
<item row="3" column="0">

View File

@@ -27,6 +27,7 @@
#include "DlgSettingsDocumentImp.h"
#include "ui_DlgSettingsDocument.h"
#include "AutoSaver.h"
#include <App/License.h>
using namespace Gui::Dialog;
@@ -42,6 +43,8 @@ DlgSettingsDocumentImp::DlgSettingsDocumentImp( QWidget* parent )
, ui(new Ui_DlgSettingsDocument)
{
ui->setupUi(this);
addLicenseTypes();
ui->prefSaveTransaction->hide();
ui->prefDiscardTransaction->hide();
@@ -137,54 +140,39 @@ void DlgSettingsDocumentImp::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
int index = ui->prefLicenseType->currentIndex();
addLicenseTypes();
ui->prefLicenseType->setCurrentIndex(index);
}
else {
QWidget::changeEvent(e);
}
}
void DlgSettingsDocumentImp::addLicenseTypes()
{
ui->prefLicenseType->clear();
auto rawLicenses = App::License::getLicenses();
for (const auto& it : rawLicenses) {
QString text = QApplication::translate("Gui::Dialog::DlgSettingsDocument", it.c_str());
ui->prefLicenseType->addItem(text);
}
}
/**
* Set the correct URL depending on the license type
*/
void DlgSettingsDocumentImp::onLicenseTypeChanged(int index)
{
ui->prefLicenseUrl->setReadOnly(true);
switch (index) {
case 0:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://en.wikipedia.org/wiki/All_rights_reserved"));
break;
case 1:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://creativecommons.org/licenses/by/4.0/"));
break;
case 2:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-sa/4.0/"));
break;
case 3:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nd/4.0/"));
break;
case 4:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nc/4.0/"));
break;
case 5:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nc-sa/4.0/"));
break;
case 6:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://creativecommons.org/licenses/by-nc-nd/4.0/"));
break;
case 7:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://en.wikipedia.org/wiki/Public_domain"));
break;
case 8:
ui->prefLicenseUrl->setText(QString::fromLatin1("http://artlibre.org/licence/lal"));
break;
case 9: case 10: case 11:
ui->prefLicenseUrl->setText(QString::fromLatin1("https://cern-ohl.web.cern.ch/"));
break;
default:
ui->prefLicenseUrl->clear();
ui->prefLicenseUrl->setReadOnly(false);
break;
App::License license{index};
std::string url = license.getUrl();
if (license.getType() == App::License::Type::Other) {
ui->prefLicenseUrl->clear();
ui->prefLicenseUrl->setReadOnly(false);
}
else {
ui->prefLicenseUrl->setReadOnly(true);
ui->prefLicenseUrl->setText(QString::fromStdString(url));
}
}

View File

@@ -52,6 +52,7 @@ protected Q_SLOTS:
protected:
void changeEvent(QEvent *e) override;
void addLicenseTypes();
private:
std::unique_ptr<Ui_DlgSettingsDocument> ui;