From 19ed0f093a304c99f7b54dc66ef0c05ceb3162bd Mon Sep 17 00:00:00 2001 From: liuxinhao Date: Mon, 7 Nov 2022 15:44:34 +0800 Subject: [PATCH 1/5] feat(license): briefly displays the license information,provide access to the activation interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简要展示激活信息,提供激活信息入口 --- plugins/system/CMakeLists.txt | 6 + .../data/com.kylinsec.Kiran.LicenseObject.xml | 45 +++++++ .../system-information/system-information.cpp | 124 +++++++++++++++--- .../system-information/system-information.h | 8 +- .../system-information/system-information.ui | 85 ++++++++++++ 5 files changed, 249 insertions(+), 19 deletions(-) create mode 100644 plugins/system/data/com.kylinsec.Kiran.LicenseObject.xml diff --git a/plugins/system/CMakeLists.txt b/plugins/system/CMakeLists.txt index fb49f4b..619f469 100644 --- a/plugins/system/CMakeLists.txt +++ b/plugins/system/CMakeLists.txt @@ -18,9 +18,15 @@ file(GLOB_RECURSE SYSTEM_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp qt5_create_translation(SYSTEM_QM ${CMAKE_CURRENT_SOURCE_DIR} .//translations/kiran-cpanel-system.zh_CN.ts) +kiran_qt5_add_dbus_interface_ex(LICENSE_DBUS_SRC + data/com.kylinsec.Kiran.LicenseObject.xml + dbus_license_dbus + DBusLicenseObject) + add_library(${TARGET_NAME} SHARED ${SYSTEM_SRC} ${SYSTEM_QM} + ${LICENSE_DBUS_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/data/res.qrc) target_include_directories(${TARGET_NAME} PRIVATE diff --git a/plugins/system/data/com.kylinsec.Kiran.LicenseObject.xml b/plugins/system/data/com.kylinsec.Kiran.LicenseObject.xml new file mode 100644 index 0000000..cf3760f --- /dev/null +++ b/plugins/system/data/com.kylinsec.Kiran.LicenseObject.xml @@ -0,0 +1,45 @@ + + + + + + + + The license info contain activation status, service status, activation code, machine code and activation type etc.. + + Get the license info. + + + + + The activation code. + + Activate by activation code. + + + + + The usb key info contain authorized points and remaining points. + + Activate by usb key. + + + + + The host used to request activation code. + + + The service uid to request activation code. + + Activate on-line. + + + + + The placeholder. + + The license info is changed, you need call function GetLicense to get the newest license info. + + + + diff --git a/plugins/system/src/pages/system-information/system-information.cpp b/plugins/system/src/pages/system-information/system-information.cpp index 4409040..bf36086 100644 --- a/plugins/system/src/pages/system-information/system-information.cpp +++ b/plugins/system/src/pages/system-information/system-information.cpp @@ -14,11 +14,14 @@ #include "system-information.h" #include "dbus-wrapper/system-info-dbus.h" +#include "dbus_license_dbus.h" #include "license-agreement.h" #include "ui_system-information.h" - #include +#include +#include +#include #include #include #include @@ -26,7 +29,10 @@ #include #include #include -#include +#include +#include +#include + #define HOST_NAME "host_name" #define ARCH "arch" #define KERNEL_VERSION "kernel_version" @@ -34,7 +40,7 @@ #define KERNEL_RELEASE "kernel_release" #define PRODUCT_RELEASE "product_release" -SystemInformation::SystemInformation(QWidget *parent) +SystemInformation::SystemInformation(QWidget* parent) : QWidget(parent), ui(new Ui::SystemInformation), hostNameWidget(nullptr), licenseAgreement(nullptr) { ui->setupUi(this); @@ -78,9 +84,10 @@ void SystemInformation::init() }); // clang-format on connect(ui->btn_change_name, &QPushButton::clicked, this, &SystemInformation::handleChangeHostName); - Kiran::StylePropertyHelper::setButtonType(ui->btn_change_name,Kiran::BUTTON_Default); - Kiran::StylePropertyHelper::setButtonType(ui->btn_EULA,Kiran::BUTTON_Default); - Kiran::StylePropertyHelper::setButtonType(ui->btn_version_license,Kiran::BUTTON_Default); + Kiran::StylePropertyHelper::setButtonType(ui->btn_change_name, Kiran::BUTTON_Default); + Kiran::StylePropertyHelper::setButtonType(ui->btn_EULA, Kiran::BUTTON_Default); + Kiran::StylePropertyHelper::setButtonType(ui->btn_version_license, Kiran::BUTTON_Default); + Kiran::StylePropertyHelper::setButtonType(ui->btn_license_show, Kiran::BUTTON_Default); } bool SystemInformation::initUI() @@ -97,7 +104,7 @@ bool SystemInformation::initUI() } else { - QString hostname,arch,systemVersion,kernelVersion; + QString hostname, arch, systemVersion, kernelVersion; parseSoftwareInfoJson(systemInfoJson, hostname, arch, @@ -108,16 +115,26 @@ bool SystemInformation::initUI() ui->lab_system_arch_info->setText(arch); ui->lab_system_version_info->setText(systemVersion); ui->lab_core_version_info->setText(kernelVersion); + } + QString licenseDesc; + if (!checkLicensEnable() || !getLicenseDesc(licenseDesc)) + { + ui->widget_license->setVisible(false); + } + else + { + ui->lab_license_status->setText(licenseDesc); + connect(ui->btn_license_show, &QPushButton::clicked, this, &SystemInformation::handleShowLicenseDialog); } - QList labels = {ui->lab_name_info,ui->lab_core_version_info, ui->lab_system_arch_info,ui->lab_system_version_info}; - for (auto label : labels) + QList labels = {ui->lab_name_info, ui->lab_core_version_info, ui->lab_system_arch_info, ui->lab_system_version_info, ui->lab_license_status}; + for (QLabel* label : labels) { label->setStyleSheet("color:#919191;font-family: \"Noto Sans CJK SC regular\";"); } - auto kiranFrames = findChildren(); + auto kiranFrames = findChildren(); for (auto frame : kiranFrames) { frame->setRadius(6); @@ -151,25 +168,90 @@ void SystemInformation::parseSoftwareInfoJson(QString jsonString, return; } QJsonObject rootObject = jsonDocument.object(); - if( rootObject.contains("host_name") && rootObject["host_name"].isString() ) + if (rootObject.contains("host_name") && rootObject["host_name"].isString()) { hostName = rootObject["host_name"].toString(); } - if( rootObject.contains("arch") && rootObject["arch"].isString() ) + if (rootObject.contains("arch") && rootObject["arch"].isString()) { arch = rootObject["arch"].toString(); } - if( rootObject.contains("product_release") && rootObject["product_release"].isString() ) + if (rootObject.contains("product_release") && rootObject["product_release"].isString()) { systemVersion = rootObject["product_release"].toString(); } - if( rootObject.contains("kernal_name") && rootObject["kernal_name"].isString() && - rootObject.contains("kernel_release") && rootObject["kernel_release"].isString() ) + if (rootObject.contains("kernal_name") && rootObject["kernal_name"].isString() && + rootObject.contains("kernel_release") && rootObject["kernel_release"].isString()) { kernelVersion = rootObject["kernal_name"].toString() + " " + rootObject["kernel_release"].toString(); } } +bool SystemInformation::checkLicensEnable() +{ + QDBusConnection dbusConn = QDBusConnection::systemBus(); + return dbusConn.interface()->isServiceRegistered("com.kylinsec.Kiran.LicenseManager"); +} + +bool SystemInformation::getLicenseDesc(QString& licenseStatus) +{ + DBusLicenseObject dBusLicenseObject("com.kylinsec.Kiran.LicenseManager", + "/com/kylinsec/Kiran/LicenseObject/KylinSecOS", + QDBusConnection::systemBus()); + auto reply = dBusLicenseObject.GetLicense(); + reply.waitForFinished(); + if (reply.isError()) + { + KLOG_ERROR() << "KylinSecOS GetLicense failed:" << reply.error(); + return false; + } + + QString licenseJson = reply.value(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(licenseJson.toUtf8()); + QJsonObject rootObj = jsonDocument.object(); + QStringList keys = rootObj.keys(); + + QSet keySet = {"expired_time","activation_status"}; + for( auto key:keySet ) + { + if( !keys.contains(key) ) + { + KLOG_ERROR() << "KylinSecOS GetLicense missing key:" << key; + return false; + } + } + + QVariant expiredTimeVar = rootObj["expired_time"].toVariant(); + qlonglong expiredTimeSinceEpoch = expiredTimeVar.toULongLong(); + + QVariant activationStatusVar = rootObj["activation_status"].toVariant(); + qulonglong activationStatus = activationStatusVar.toULongLong(); + + QDateTime expiredTime = QDateTime::fromSecsSinceEpoch(expiredTimeSinceEpoch); + if (activationStatus == 0) //未激活 + { + licenseStatus = tr("UnActivated"); + } + else + { + QDateTime currentDateTime = QDateTime::currentDateTime(); + + if( currentDateTime > expiredTime ) //激活码已过期 + { + licenseStatus = tr("Activation code has expired"); + } + else if( expiredTime.date().year() >= 2100 ) //永久激活 + { + licenseStatus = tr("Permanently activated"); + } + else //已激活 + { + licenseStatus = tr("Activated"); + } + } + return true; +} + /** * @brief SystemInformation::onBtnchangeHostName: 当点击更改用户名后的槽函数 */ @@ -200,13 +282,21 @@ void SystemInformation::updateHostName(bool isChanged, QString name) } } +void SystemInformation::handleShowLicenseDialog() +{ + if( !QProcess::startDetached("/usr/bin/ksl-os-gui") ) + { + KiranMessageBox::message(this, tr("Error"), tr("Failed to open the license activator"),KiranMessageBox::Ok); + } +} + /** * @brief 事件监听,当收到激活向导窗口或者授权信息窗口的关闭事件时,释放窗口内存 * @param obj 事件对象 * @param obj 事件 * @return 是否过滤 */ -bool SystemInformation::eventFilter(QObject *obj, QEvent *event) +bool SystemInformation::eventFilter(QObject* obj, QEvent* event) { if (obj == hostNameWidget && event->type() == QEvent::Close) { @@ -218,5 +308,5 @@ bool SystemInformation::eventFilter(QObject *obj, QEvent *event) QSize SystemInformation::sizeHint() const { - return {500,657}; + return {500, 657}; } diff --git a/plugins/system/src/pages/system-information/system-information.h b/plugins/system/src/pages/system-information/system-information.h index c15561c..d46ad74 100644 --- a/plugins/system/src/pages/system-information/system-information.h +++ b/plugins/system/src/pages/system-information/system-information.h @@ -43,9 +43,13 @@ private: QString &systemVersion, QString &kernelVersion); -private slots : - void handleChangeHostName(void); + bool checkLicensEnable(); + bool getLicenseDesc(QString& licenseStatus); + +private slots: + void handleChangeHostName(void); void updateHostName(bool isChanged, QString name); + void handleShowLicenseDialog(); protected: bool eventFilter(QObject *obj, QEvent *event); diff --git a/plugins/system/src/pages/system-information/system-information.ui b/plugins/system/src/pages/system-information/system-information.ui index b036ab6..7dd5cdd 100644 --- a/plugins/system/src/pages/system-information/system-information.ui +++ b/plugins/system/src/pages/system-information/system-information.ui @@ -423,6 +423,91 @@ + + + + + 0 + 40 + + + + + 16777215 + 40 + + + + + 0 + + + 10 + + + 0 + + + 10 + + + 0 + + + + + Activation status: + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + TextLabel + + + + + + + + 0 + 0 + + + + + 56 + 28 + + + + + 56 + 28 + + + + Show + + + + + + -- 2.33.0