diff --git a/src/App/CMakeLists.txt b/src/App/CMakeLists.txt index f014d2bf38..1232bc2946 100644 --- a/src/App/CMakeLists.txt +++ b/src/App/CMakeLists.txt @@ -163,6 +163,8 @@ SET(Document_CPP_SRCS TextDocument.cpp Link.cpp LinkBaseExtensionPyImp.cpp + License.cpp + License.h ) SET(Document_HPP_SRCS diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 4446d2a3fd..12db4a3a22 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -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"); diff --git a/src/App/License.cpp b/src/App/License.cpp new file mode 100644 index 0000000000..079ebdb126 --- /dev/null +++ b/src/App/License.cpp @@ -0,0 +1,114 @@ +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * 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 +#endif + +#include "License.h" + +using namespace App; + +std::map License::licenseItems; + +License::License(License::Type type) + : type{type} +{ + init(); +} + +License::License(long id) + : type{static_cast(id)} +{ + if (id < 0 || id > static_cast(Type::Other)) { + type = Type::Other; + } + + init(); +} + +License::License(int id) + : License(static_cast(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 License::getLicenses() +{ + init(); + std::vector output; + output.reserve(licenseItems.size()); + using Value = std::pair; + std::transform(licenseItems.cbegin(), licenseItems.cend(), std::back_inserter(output), [](const Value& val) { + return val.second.license; + }); + + return output; +} diff --git a/src/App/License.h b/src/App/License.h new file mode 100644 index 0000000000..795a96d50c --- /dev/null +++ b/src/App/License.h @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * 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 +#include +#include +#include + +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 getLicenses(); + +private: + static void init(); + +private: + struct LicenseItem{ + std::string license; + std::string url; + }; + Type type; + + static std::map licenseItems; +}; + +} + +#endif // APP_LICENSE_H diff --git a/src/Gui/DlgProjectInformationImp.cpp b/src/Gui/DlgProjectInformationImp.cpp index 7139b8833a..978b1b700e 100644 --- a/src/Gui/DlgProjectInformationImp.cpp +++ b/src/Gui/DlgProjectInformationImp.cpp @@ -29,11 +29,28 @@ #endif #include +#include #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 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::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)); } /** diff --git a/src/Gui/DlgSettingsDocument.ui b/src/Gui/DlgSettingsDocument.ui index 810e580f4b..2d90093047 100644 --- a/src/Gui/DlgSettingsDocument.ui +++ b/src/Gui/DlgSettingsDocument.ui @@ -680,71 +680,6 @@ You can also use the form: John Doe <john@doe.com> Document - - - 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 - - diff --git a/src/Gui/DlgSettingsDocumentImp.cpp b/src/Gui/DlgSettingsDocumentImp.cpp index b70b6b5865..c41d9ebef0 100644 --- a/src/Gui/DlgSettingsDocumentImp.cpp +++ b/src/Gui/DlgSettingsDocumentImp.cpp @@ -27,6 +27,7 @@ #include "DlgSettingsDocumentImp.h" #include "ui_DlgSettingsDocument.h" #include "AutoSaver.h" +#include 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)); } } diff --git a/src/Gui/DlgSettingsDocumentImp.h b/src/Gui/DlgSettingsDocumentImp.h index 71c922b6f5..0ccee6b560 100644 --- a/src/Gui/DlgSettingsDocumentImp.h +++ b/src/Gui/DlgSettingsDocumentImp.h @@ -52,6 +52,7 @@ protected Q_SLOTS: protected: void changeEvent(QEvent *e) override; + void addLicenseTypes(); private: std::unique_ptr ui;