diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2020-04-17 16:25:25 +0300 |
---|---|---|
committer | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2020-04-28 11:56:44 +0300 |
commit | 550186ed78a023374239c1f8494188db030f035d (patch) | |
tree | 2d96ab24f87535bf42e746970679217777571acc | |
parent | 4f5eace163eeb34504cb8044c42b147df03fafed (diff) |
Add possibility to accept licenses from CLI
License agreements can be accepted automatically by using command
line option --accept-licenses or interactively by answering
queries with either "Accept", "Reject" or "Show" selections.
Task-number: QTIFW-1742
Task-number: QTIFW-1745
Change-Id: Iff46b44f91faddb7f163b299994167ab16df68b3
Reviewed-by: Katja Marttila <katja.marttila@qt.io>
-rw-r--r-- | src/libs/installer/commandlineparser.cpp | 2 | ||||
-rw-r--r-- | src/libs/installer/constants.h | 1 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.cpp | 8 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.h | 2 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore_p.cpp | 60 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore_p.h | 3 | ||||
-rw-r--r-- | src/sdk/sdkapp.h | 3 | ||||
-rw-r--r-- | tests/auto/installer/installer.pro | 3 | ||||
-rw-r--r-- | tests/auto/installer/licenseagreement/data/repository/A/1.0.2-1meta.7z | bin | 0 -> 11570 bytes | |||
-rw-r--r-- | tests/auto/installer/licenseagreement/data/repository/Updates.xml | 16 | ||||
-rw-r--r-- | tests/auto/installer/licenseagreement/licenseagreement.pro | 10 | ||||
-rw-r--r-- | tests/auto/installer/licenseagreement/settings.qrc | 6 | ||||
-rw-r--r-- | tests/auto/installer/licenseagreement/tst_licenseagreement.cpp | 72 |
13 files changed, 184 insertions, 2 deletions
diff --git a/src/libs/installer/commandlineparser.cpp b/src/libs/installer/commandlineparser.cpp index 9aa6c762f..6191c302d 100644 --- a/src/libs/installer/commandlineparser.cpp +++ b/src/libs/installer/commandlineparser.cpp @@ -158,6 +158,8 @@ CommandLineParser::CommandLineParser() QLatin1String("identifier=value"))); m_parser.addOption(QCommandLineOption(QStringList() << CommandLineOptions::scMessageDefaultAnswer, QLatin1String("Automatically answers to message queries with their default values."))); + m_parser.addOption(QCommandLineOption(QStringList() << CommandLineOptions::scAcceptLicenses, + QLatin1String("Accepts all licenses without user input."))); // Developer options m_parser.addOption(QCommandLineOption(QStringList() diff --git a/src/libs/installer/constants.h b/src/libs/installer/constants.h index 636c8687e..05728bff3 100644 --- a/src/libs/installer/constants.h +++ b/src/libs/installer/constants.h @@ -156,6 +156,7 @@ static const QLatin1String scAcceptMessageQuery("accept-messages"); static const QLatin1String scRejectMessageQuery("reject-messages"); static const QLatin1String scMessageAutomaticAnswer("auto-answer"); static const QLatin1String scMessageDefaultAnswer("default-answer"); +static const QLatin1String scAcceptLicenses("accept-licenses"); // Misc installation options static const QLatin1String scRootShort("t"); diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index a4989efce..db144806f 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -614,6 +614,14 @@ void PackageManagerCore::acceptMessageBoxDefaultButton() } /*! + Automatically accept all licenses required by components to install. +*/ +void PackageManagerCore::setAutoAcceptLicenses() +{ + d->m_autoAcceptLicenses = true; +} + +/*! Returns the size of the component \a component as \a value. */ quint64 PackageManagerCore::size(QInstaller::Component *component, const QString &value) const diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index 36ef735e3..dceaa4fef 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -183,6 +183,8 @@ public: Q_INVOKABLE void setMessageBoxAutomaticAnswer(const QString &identifier, int button); Q_INVOKABLE void acceptMessageBoxDefaultButton(); + Q_INVOKABLE void setAutoAcceptLicenses(); + quint64 size(QInstaller::Component *component, const QString &value) const; Q_INVOKABLE bool isFileExtensionRegistered(const QString &extension) const; diff --git a/src/libs/installer/packagemanagercore_p.cpp b/src/libs/installer/packagemanagercore_p.cpp index 09aa57650..8323928f9 100644 --- a/src/libs/installer/packagemanagercore_p.cpp +++ b/src/libs/installer/packagemanagercore_p.cpp @@ -219,6 +219,7 @@ PackageManagerCorePrivate::PackageManagerCorePrivate(PackageManagerCore *core) , m_commandLineInstance(false) , m_userSetBinaryMarker(false) , m_checkAvailableSpace(true) + , m_autoAcceptLicenses(false) { } @@ -253,6 +254,7 @@ PackageManagerCorePrivate::PackageManagerCorePrivate(PackageManagerCore *core, q , m_commandLineInstance(false) , m_userSetBinaryMarker(false) , m_checkAvailableSpace(true) + , m_autoAcceptLicenses(false) { foreach (const OperationBlob &operation, performedOperations) { QScopedPointer<QInstaller::Operation> op(KDUpdater::UpdateOperationFactory::instance() @@ -2510,11 +2512,67 @@ bool PackageManagerCorePrivate::calculateComponentsAndRun() QString htmlOutput; bool componentsOk = m_core->calculateComponents(&htmlOutput); qCDebug(QInstaller::lcInstallerInstallLog).noquote() << htmlToString(htmlOutput); - if (componentsOk) { + if (componentsOk && acceptLicenseAgreements()) { return m_core->run(); } return false; } +bool PackageManagerCorePrivate::acceptLicenseAgreements() +{ + // Always skip for uninstaller + if (isUninstaller()) + return true; + + typedef QHash<QString, QPair<QString, QString> > LicensesHash; + foreach (Component *component, m_core->orderedComponentsToInstall()) { + // Package manager or updater, no need to accept again as long as + // the component is installed. + if (m_core->isMaintainer() && component->isInstalled()) + continue; + + LicensesHash hash = component->licenses(); + for (LicensesHash::iterator it = hash.begin(); it != hash.end(); ++it) { + if (m_autoAcceptLicenses || askUserAcceptLicense(it.key(), it.value().second)) { + qCDebug(QInstaller::lcInstallerInstallLog) << "License" + << it.key() << "accepted by user."; + } else { + qCDebug(QInstaller::lcInstallerInstallLog) << "License" + << it.key() << "not accepted by user. Aborting."; + return false; + } + } + } + return true; +} + +bool PackageManagerCorePrivate::askUserAcceptLicense(const QString &name, const QString &content) +{ + qCDebug(QInstaller::lcInstallerInstallLog) << "You must accept " + "the terms contained in the following license agreement " + "before continuing with the installation:" << name; + + forever { + qCDebug(QInstaller::lcInstallerInstallLog) << "Accept|Reject|Show"; + + QTextStream stream(stdin); + QString input; + stream.readLineInto(&input); + + if (QString::compare(input, QLatin1String("Accept"), Qt::CaseInsensitive) == 0 + || QString::compare(input, QLatin1String("A"), Qt::CaseInsensitive) == 0) { + return true; + } else if (QString::compare(input, QLatin1String("Reject"), Qt::CaseInsensitive) == 0 + || QString::compare(input, QLatin1String("R"), Qt::CaseInsensitive) == 0) { + return false; + } else if (QString::compare(input, QLatin1String("Show"), Qt::CaseInsensitive) == 0 + || QString::compare(input, QLatin1String("S"), Qt::CaseInsensitive) == 0) { + qCDebug(QInstaller::lcInstallerInstallLog).noquote() << content; + } else { + qCDebug(QInstaller::lcInstallerInstallLog) << "Unknown answer:" << input; + } + } +} + } // namespace QInstaller diff --git a/src/libs/installer/packagemanagercore_p.h b/src/libs/installer/packagemanagercore_p.h index dc5ad6af4..5bdba6a7d 100644 --- a/src/libs/installer/packagemanagercore_p.h +++ b/src/libs/installer/packagemanagercore_p.h @@ -207,6 +207,7 @@ public: bool m_dependsOnLocalInstallerBinary; QStringList m_allowedRunningProcesses; + bool m_autoAcceptLicenses; private slots: void infoMessage(Job *, const QString &message) { @@ -244,6 +245,8 @@ private: void findExecutablesRecursive(const QString &path, const QStringList &excludeFiles, QStringList *result); QStringList runningInstallerProcesses(const QStringList &exludeFiles); bool calculateComponentsAndRun(); + bool acceptLicenseAgreements(); + bool askUserAcceptLicense(const QString &name, const QString &content); private: PackageManagerCore *m_core; diff --git a/src/sdk/sdkapp.h b/src/sdk/sdkapp.h index 145f4e7ea..863058df2 100644 --- a/src/sdk/sdkapp.h +++ b/src/sdk/sdkapp.h @@ -274,6 +274,9 @@ public: .isSet(CommandLineOptions::scCreateLocalRepositoryLong) || m_core->settings().createLocalRepository()); + if (m_parser.isSet(CommandLineOptions::scAcceptLicenses)) + m_core->setAutoAcceptLicenses(); + if (m_parser.isSet(CommandLineOptions::scAcceptMessageQuery)) m_core->autoAcceptMessageBoxes(); if (m_parser.isSet(CommandLineOptions::scRejectMessageQuery)) diff --git a/tests/auto/installer/installer.pro b/tests/auto/installer/installer.pro index ecba4f9bb..81def2f76 100644 --- a/tests/auto/installer/installer.pro +++ b/tests/auto/installer/installer.pro @@ -34,7 +34,8 @@ SUBDIRS += \ copydirectoryoperation \ commandlineupdate \ moveoperation \ - environmentvariableoperation + environmentvariableoperation \ + licenseagreement win32 { SUBDIRS += registerfiletypeoperation \ diff --git a/tests/auto/installer/licenseagreement/data/repository/A/1.0.2-1meta.7z b/tests/auto/installer/licenseagreement/data/repository/A/1.0.2-1meta.7z Binary files differnew file mode 100644 index 000000000..d473ac487 --- /dev/null +++ b/tests/auto/installer/licenseagreement/data/repository/A/1.0.2-1meta.7z diff --git a/tests/auto/installer/licenseagreement/data/repository/Updates.xml b/tests/auto/installer/licenseagreement/data/repository/Updates.xml new file mode 100644 index 000000000..2afd2a741 --- /dev/null +++ b/tests/auto/installer/licenseagreement/data/repository/Updates.xml @@ -0,0 +1,16 @@ +<Updates> + <ApplicationName>{AnyApplication}</ApplicationName> + <ApplicationVersion>1.0.0</ApplicationVersion> + <Checksum>false</Checksum> + <PackageUpdate> + <Name>A</Name> + <DisplayName>A</DisplayName> + <Description>Example component A</Description> + <Version>1.0.2-1</Version> + <ReleaseDate>2015-01-01</ReleaseDate> + <Default>true</Default> + <Licenses> + <License name="GNU GENERAL PUBLIC LICENSE Version 3" file="gpl3.txt"/> + </Licenses> + </PackageUpdate> +</Updates> diff --git a/tests/auto/installer/licenseagreement/licenseagreement.pro b/tests/auto/installer/licenseagreement/licenseagreement.pro new file mode 100644 index 000000000..1d8c3c451 --- /dev/null +++ b/tests/auto/installer/licenseagreement/licenseagreement.pro @@ -0,0 +1,10 @@ +include(../../qttest.pri) + +QT -= gui +QT += testlib + +SOURCES += tst_licenseagreement.cpp + +RESOURCES += \ + settings.qrc \ + ..\shared\config.qrc diff --git a/tests/auto/installer/licenseagreement/settings.qrc b/tests/auto/installer/licenseagreement/settings.qrc new file mode 100644 index 000000000..d030220ab --- /dev/null +++ b/tests/auto/installer/licenseagreement/settings.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/"> + <file>data/repository/Updates.xml</file> + <file>data/repository/A/1.0.2-1meta.7z</file> + </qresource> +</RCC> diff --git a/tests/auto/installer/licenseagreement/tst_licenseagreement.cpp b/tests/auto/installer/licenseagreement/tst_licenseagreement.cpp new file mode 100644 index 000000000..fad2faf0b --- /dev/null +++ b/tests/auto/installer/licenseagreement/tst_licenseagreement.cpp @@ -0,0 +1,72 @@ +/************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#include <packagemanagercore.h> +#include <binarycontent.h> +#include <settings.h> +#include <fileutils.h> +#include <init.h> + +#include <QObject> +#include <QFile> +#include <QTest> + +using namespace KDUpdater; +using namespace QInstaller; + +class tst_licenseagreement : public QObject +{ + Q_OBJECT + +private slots: + void testAutoAcceptFromCLI() + { + QInstaller::init(); //This will eat debug output + PackageManagerCore *core = new PackageManagerCore(BinaryContent::MagicInstallerMarker, QList<OperationBlob> ()); + QSet<Repository> repoList; + Repository repo = Repository::fromUserInput(":///data/repository"); + repoList.insert(repo); + core->settings().setDefaultRepositories(repoList); + + QString installDir = QInstaller::generateTemporaryFileName(); + QDir().mkpath(installDir); + core->setValue(scTargetDir, installDir); + core->setAutoAcceptLicenses(); + core->installDefaultComponentsSilently(); + + QFile file(installDir + "/Licenses/gpl3.txt"); + QVERIFY(file.exists()); + QDir dir(installDir); + QVERIFY(dir.removeRecursively()); + core->deleteLater(); + } +}; + +QTEST_MAIN(tst_licenseagreement) + +#include "tst_licenseagreement.moc" |