/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "power.h"
#include "powermacrodata.h"

#include <QTranslator>
#include <QDebug>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusConnection>
#include <QSettings>
#include <QFormLayout>
#include <QFile>

#include "ukcccommon.h"
using namespace ukcc;

/* qt会将glib里的signals成员识别为宏，所以取消该宏
 * 后面如果用到signals时，使用Q_SIGNALS代替即可
 **/
#ifdef signals
#undef signals
#endif

#include "libupower-glib/upower.h"

typedef enum {
    BALANCE,
    SAVING,
    CUSTDOM
}MODE;

#define DISPLAY_BALANCE_BA 5 * 60
#define DISPLAY_BALANCE_AC 10 * 60
#define COMPUTER_BALANCE_BA 5 * 60
#define COMPUTER_BALANCE_AC 10 * 60
#define BRIGHTNESS_BALANCE 100
#define DISPLAY_SAVING 60
#define COMPUTER_SAVING 2 * 60
#define BRIGHTNESS_SAVING 20

int closeArray[7] = {5, 10, 15, 30, 60, 120, 0};

Power::Power() : mFirstLoad(true)
{
    QTranslator *translator = new QTranslator(this);
    translator->load("/usr/share/ukui-power-manager/power/translations/" + QLocale::system().name());
    QApplication::installTranslator(translator);

    pluginName = tr("Power");
    pluginType = SYSTEM;
    initInterface();
}

Power::~Power() {
    if (!mFirstLoad) {

    }
}

QString Power::plugini18nName() {
    return pluginName;
}

int Power::pluginTypes() {
    return pluginType;
}

QWidget * Power::pluginUi() {
    if (mFirstLoad) {
        pluginWidget = new QWidget;
        pluginWidget->setAttribute(Qt::WA_DeleteOnClose);

        const QByteArray styleID(STYLE_FONT_SCHEMA);
        const QByteArray id(POWERMANAGER_SCHEMA);
        const QByteArray iid(SESSION_SCHEMA);
        const QByteArray iiid(SCREENSAVER_SCHEMA);
        const QByteArray IVd(PERSONALSIE_SCHEMA);
        const QByteArray Vd(UKUI_QUICK_OPERATION_PANEL);
        const QByteArray procID(PROC_MANAGER);
        if (QGSettings::isSchemaInstalled(procID)) {
            mProcManagerSettings = new QGSettings(procID, QByteArray(), this);
        }

        if (QGSettings::isSchemaInstalled(id) && QGSettings::isSchemaInstalled(styleID) && QGSettings::isSchemaInstalled(iid)
                && QGSettings::isSchemaInstalled(iiid) && QGSettings::isSchemaInstalled(IVd) ) {
            settings = new QGSettings(id, QByteArray(), this);
            stylesettings = new QGSettings(styleID, QByteArray(), this);
            sessionsettings = new QGSettings(iid, QByteArray(), this);
            screensettings = new QGSettings(iiid, QByteArray(), this);
            m_centerSettings = new QGSettings(IVd,QByteArray(), this);
            if (QGSettings::isSchemaInstalled(Vd) && UkccCommon::isTablet()) {
                m_qsettings = new QGSettings(Vd,QByteArray(), this);
            } else {
                m_qsettings = nullptr;
            }

            connect(stylesettings,&QGSettings::changed,[=](QString key) {
                if("systemFont" == key || "systemFontSize" == key) {
                    retranslateUi();
                }
            });
            mKeys = settings->keys();

            hostLightStripDbus =  new QDBusInterface("com.control.center.qt.systemdbus",
                                                    "/",
                                                    "com.control.center.interface",
                                                    QDBusConnection::systemBus(), this);
            if (!hostLightStripDbus->isValid()){
                qCritical() << "Create Client Interface Failed:" << QDBusConnection::systemBus().lastError();
            } else {
                QDBusReply<bool> ret = hostLightStripDbus->call("isSupportHostLightStrip");
                isSupportHostLight = ret;
            }

            initUI(pluginWidget);
            isLidPresent();
            isExitBattery();
            setupComponent();
            initCustomPlanStatus();
            setupConnect();
            resetui();

            hideComponent();
            hideComponentByConf2();
        }
    }

    return pluginWidget;
}

const QString Power::name() const {
    return QStringLiteral("Power");
}

bool Power::isShowOnHomePage() const
{
    return true;
}

QIcon Power::icon() const
{
    return QIcon::fromTheme("system-shutdown-symbolic");
}

bool Power::isEnable() const
{
    return true;
}

void Power::initUI(QWidget *widget)
{
    // 整体布局
    QVBoxLayout *mverticalLayout = new QVBoxLayout(widget);
    mverticalLayout->setSpacing(8);
    mverticalLayout->setContentsMargins(0, 0, 0, 0);
    m_customTitleLabel = new KLabel(widget);
    m_customTitleLabel->setContentsMargins(16, 0, 0, 0);
    //~ contents_path /Power/General
    m_customTitleLabel->setText(tr("General"));
    mverticalLayout->addWidget(m_customTitleLabel);

    // 通用设置布局
    Powerwidget = new QFrame(widget);
    Powerwidget->setMinimumSize(QSize(550, 0));
    Powerwidget->setMaximumSize(QSize(16777215, 16777215));
    Powerwidget->setFrameShape(QFrame::Box);

    PowerLayout = new QVBoxLayout(Powerwidget);
    PowerLayout->setContentsMargins(0, 0, 0, 0);
    PowerLayout->setSpacing(0);

    mSleepPwdFrame = new QFrame(Powerwidget);
    setFrame_Noframe(mSleepPwdFrame);

    QHBoxLayout *mSleepPwdLayout = new QHBoxLayout(mSleepPwdFrame);
    mSleepPwdLayout->setContentsMargins(16, 0, 16, 0);

    mSleepPwdLabel = new QLabel(mSleepPwdFrame);
    mSleepPwdLabel->setFixedSize(550,60);

    mSleepPwdBtn = new KSwitchButton(mSleepPwdFrame);
    QSize size1 = mSleepPwdBtn->size();
    qDebug() << "mSleepPwdBtn size:" << size1.width() << "x" << size1.height();
    mSleepPwdBtn->setObjectName("sleepwaken");

    mSleepPwdLayout->addWidget(mSleepPwdLabel);
    mSleepPwdLayout->addStretch();
    mSleepPwdLayout->addWidget(mSleepPwdBtn);
    PowerLayout->addWidget(mSleepPwdFrame);

    line_1 = setLine(Powerwidget);
    PowerLayout->addWidget(line_1);


    mWakenPwdFrame = new QFrame(Powerwidget);
    setFrame_Noframe(mWakenPwdFrame);

    QHBoxLayout *mWakenPwdLayout = new QHBoxLayout(mWakenPwdFrame);
    mWakenPwdLayout->setContentsMargins(16, 0, 16, 0);

    mWakenPwdLabel = new QLabel(mWakenPwdFrame);
    mWakenPwdLabel->setFixedSize(550,49);///???????????

    mWakenPwdBtn = new KSwitchButton(mWakenPwdFrame);
    QSize size = mWakenPwdBtn->size();
    qDebug() << "mWakenPwdBtn size:" << size;

    mWakenPwdBtn->setObjectName("lockwaken");

    mWakenPwdLayout->addWidget(mWakenPwdLabel);
    mWakenPwdLayout->addStretch();
    mWakenPwdLayout->addWidget(mWakenPwdBtn);
    PowerLayout->addWidget(mWakenPwdFrame);

    line_2 = setLine(Powerwidget);
    PowerLayout->addWidget(line_2);

    mPowerKeyFrame = new QFrame(Powerwidget);
    // setFrame_Noframe(mSleepPwdFrame);
    setFrame_Noframe(mPowerKeyFrame);



    QHBoxLayout *mPowerKeyLayout = new QHBoxLayout(mPowerKeyFrame);
    mPowerKeyLayout->setContentsMargins(16, 0, 16, 0);

    mPowerKeyLabel = new QLabel(mPowerKeyFrame);
    mPowerKeyLabel->setFixedSize(550,60);//固定了高宽

    mPowerKeyComboBox = new QComboBox(mPowerKeyFrame);
    mPowerKeyComboBox->setObjectName("powerkey");//明天查一下
    mPowerKeyComboBox->setMinimumWidth(200);

    mPowerKeyLayout->addWidget(mPowerKeyLabel);
    mPowerKeyLayout->addWidget(mPowerKeyComboBox);

    PowerLayout->addWidget(mPowerKeyFrame);

    line_3 = setLine(Powerwidget);
    PowerLayout->addWidget(line_3);

    mCloseFrame = new QFrame(Powerwidget);
    setFrame_Noframe(mCloseFrame);

    QVBoxLayout *vCloseLayout = new QVBoxLayout(mCloseFrame);
    QHBoxLayout *mCloseLayout = new QHBoxLayout();
    QHBoxLayout *mHintLayout = new QHBoxLayout();
    vCloseLayout->setContentsMargins(16, 10, 16, 10);//??????

    mCloseLabel = new QLabel(mCloseFrame);
    mCloseLabel->setFixedWidth(550);//固定了宽度

    mCloseHintIcon = new QLabel;
    mCloseHintIcon->setPixmap(QIcon::fromTheme("dialog-warning").pixmap(QSize(16, 16)));
    mCloseHintLabel = new LightLabel(tr("The system will sleep before turning off the display"));

    mHintLayout->addWidget(mCloseHintIcon);
    mHintLayout->addWidget(mCloseHintLabel);
    mHintLayout->addStretch();

    mCloseComboBox = new QComboBox(mCloseFrame);
    mCloseComboBox->setObjectName("close");
    mCloseComboBox->setMinimumWidth(200);

    mCloseLayout->addWidget(mCloseLabel);
    mCloseLayout->addWidget(mCloseComboBox);

    vCloseLayout->addLayout(mCloseLayout);
    vCloseLayout->addLayout(mHintLayout);

    PowerLayout->addWidget(mCloseFrame);

    line_4 = setLine(Powerwidget);
    PowerLayout->addWidget(line_4);

    mSleepFrame = new QFrame(Powerwidget);
    // setFrame_Noframe(mSleepPwdFrame);
    setFrame_Noframe(mSleepFrame);

    QHBoxLayout *mSleepLayout = new QHBoxLayout(mSleepFrame);
    mSleepLayout->setContentsMargins(16, 0, 16, 0);

    mSleepLabel = new QLabel(mSleepFrame);
    mSleepLabel->setFixedSize(550,59);//固定了高宽

    mSleepComboBox = new QComboBox(mSleepFrame);
    mSleepComboBox->setObjectName("sleep");
    mSleepComboBox->setMinimumWidth(200);

    mSleepLayout->addWidget(mSleepLabel);
    mSleepLayout->addWidget(mSleepComboBox);

    PowerLayout->addWidget(mSleepFrame);

    line_5 = setLine(Powerwidget);
    PowerLayout->addWidget(line_5);

    mCloseLidFrame = new QFrame(Powerwidget);
    setFrame_Noframe(mCloseLidFrame);

    QHBoxLayout *mCloseLidLayout = new QHBoxLayout(mCloseLidFrame);
    mCloseLidLayout->setContentsMargins(16, 0, 16, 0);

    mCloseLidLabel = new QLabel(mCloseLidFrame);
    mCloseLidLabel->setFixedSize(550,59);

    mCloseLidComboBox = new QComboBox(mCloseLidFrame);
    mCloseLidComboBox->setObjectName("closelid");
    mCloseLidComboBox->setMinimumWidth(200);

    mCloseLidLayout->addWidget(mCloseLidLabel);
    mCloseLidLayout->addWidget(mCloseLidComboBox);

    PowerLayout->addWidget(mCloseLidFrame);

    mResourceFrame = new QFrame(widget);
    mResourceFrame->setMinimumSize(QSize(550, 80));
    mResourceFrame->setMaximumSize(QSize(16777215, 16777215));
    mResourceFrame->setFrameShape(QFrame::Box);

    QHBoxLayout *mResourceLayout = new QHBoxLayout(mResourceFrame);
    mResourceLayout->setContentsMargins(16, 0, 16, 0);

    mResourceLabel = new QLabel(mResourceFrame);
    mResourceLabel->setAlignment(Qt::AlignBottom);
    mResourceLabel->setFixedWidth(550);
    m_resourceDetailLabel = new LightLabel(widget);
    m_resourceDetailLabel->setText(tr("Reduce the occupation of backend running program resources and ensure smooth operation of key and focus applications."));
    m_resourceDetailLabel->setAlignment(Qt::AlignTop);

    QVBoxLayout *mLabelLayout = new QVBoxLayout(mResourceFrame);
    mLabelLayout->setSpacing(0);
    mLabelLayout->setContentsMargins(0,0,0,0);
    mLabelLayout->addWidget(mResourceLabel);
    mLabelLayout->addWidget(m_resourceDetailLabel);

    mResourceSButton = new KSwitchButton(mResourceFrame);
    mResourceSButton->setFixedSize(49, 24); 
    qDebug() << "mResourceSButton size:" << mResourceSButton->size();

    mResourceLayout->addLayout(mLabelLayout);
    mResourceLayout->addWidget(mResourceSButton);
    mverticalLayout->addWidget(Powerwidget);

    // 主机灯带设置布局
    mHostLightFrame = new QFrame(widget);
    mHostLightFrame->setMinimumSize(QSize(550, 0));
    mHostLightFrame->setMaximumSize(QSize(16777215, 16777215));
    mHostLightFrame->setFrameShape(QFrame::Box);

    QHBoxLayout *mHostLightLayout = new QHBoxLayout(mHostLightFrame);
    mHostLightLayout->setContentsMargins(16, 0, 16, 0);

    mHostLightLabel = new QLabel(mHostLightFrame);
    mHostLightLabel->setFixedSize(550,59);

    mHostLightSButton = new KSwitchButton(mHostLightFrame);

    mHostLightLayout->addWidget(mHostLightLabel);
    mHostLightLayout->addStretch();
    mHostLightLayout->addWidget(mHostLightSButton);

    mverticalLayout->addWidget(mHostLightFrame);
    mHostLightFrame->setVisible(isSupportHostLight);

    mverticalLayout->addWidget(mResourceFrame);
    mItem = new QSpacerItem(20, 24, QSizePolicy::Fixed);
    mverticalLayout->addSpacerItem(mItem);

    m_powerPlanTitleLabel = new KLabel(widget);
    m_powerPlanTitleLabel->setContentsMargins(16, 0, 0, 0);
    //~ contents_path /Power/Select Powerplan
    m_powerPlanTitleLabel->setText(tr("Select Powerplan"));
    mverticalLayout->addWidget(m_powerPlanTitleLabel);

    PowerPlanwidget = new QFrame(widget);
    PowerPlanwidget->setMinimumSize(QSize(550, 0));
    PowerPlanwidget->setMaximumSize(QSize(16777215, 16777215));
    PowerPlanwidget->setFrameShape(QFrame::Box);

    // 电源计划布局
    QVBoxLayout *PowerPlanLayout = new QVBoxLayout(PowerPlanwidget);
    PowerPlanLayout->setContentsMargins(0, 0, 0, 0);
    PowerPlanLayout->setSpacing(0);

    mPowerFrame = new QFrame(PowerPlanwidget);
    setFrame_Noframe(mPowerFrame);

    QHBoxLayout *mPowerLayout = new QHBoxLayout(mPowerFrame);
    mPowerLayout->setContentsMargins(16, 0, 16, 0);


    mPowerLabel = new QLabel(mPowerFrame);
    mPowerLabel->setFixedSize(550,60);

    mPowerComboBox = new QComboBox(mPowerFrame);
    mPowerComboBox->setObjectName("powermode");
    mPowerComboBox->setMinimumWidth(200);

    mPowerLayout->addWidget(mPowerLabel);
    mPowerLayout->addWidget(mPowerComboBox);

    PowerPlanLayout->addWidget(mPowerFrame);

    line_7 = setLine(PowerPlanwidget);
    PowerPlanLayout->addWidget(line_7);

    mBatteryFrame = new QFrame(PowerPlanwidget);
    setFrame_Noframe(mBatteryFrame);

    QHBoxLayout *mBatteryLayout = new QHBoxLayout(mBatteryFrame);
    mBatteryLayout->setContentsMargins(16, 0, 16, 0);

    mBatteryLabel = new QLabel(mBatteryFrame);
    mBatteryLabel->setFixedSize(550,59);

    mBatteryComboBox = new QComboBox(mBatteryFrame);
    mBatteryComboBox->setObjectName("batterymode");
    mBatteryComboBox->setMinimumWidth(200);

    mBatteryLayout->addWidget(mBatteryLabel);
    mBatteryLayout->addWidget(mBatteryComboBox);

    PowerPlanLayout->addWidget(mBatteryFrame);

    mverticalLayout->addWidget(PowerPlanwidget);
    mverticalLayout->addSpacing(24);

    m_batteryPlanTitleLabel = new KLabel(Powerwidget);
    m_batteryPlanTitleLabel->setContentsMargins(16, 0, 0, 0);
    //~ contents_path /Power/Battery saving plan
    m_batteryPlanTitleLabel->setText((tr("Battery saving plan")));
    mverticalLayout->addWidget(m_batteryPlanTitleLabel);

    // 电池节能计划
    Batterywidget = new QFrame(widget);
    Batterywidget->setMinimumSize(QSize(550, 0));
    Batterywidget->setMaximumSize(QSize(16777215, 16777215));
    Batterywidget->setFrameShape(QFrame::Box);

    BatteryLayout = new QVBoxLayout(Batterywidget);
    BatteryLayout->setContentsMargins(0, 0, 0, 0);
    BatteryLayout->setSpacing(0);

    mDarkenFrame = new QFrame(Batterywidget);
    setFrame_Noframe(mDarkenFrame);

    QHBoxLayout *mDarkenLayout = new QHBoxLayout(mDarkenFrame);
    mDarkenLayout->setContentsMargins(16, 0, 16, 0);

    mDarkenLabel = new QLabel(mDarkenFrame);
    mDarkenLabel->setFixedSize(550,59);

    mDarkenComboBox = new QComboBox(mDarkenFrame);
    mDarkenComboBox->setObjectName("darken");
    mDarkenComboBox->setMinimumWidth(200);

    mDarkenLayout->addWidget(mDarkenLabel);
    mDarkenLayout->addWidget(mDarkenComboBox);

    BatteryLayout->addWidget(mDarkenFrame);

    line_8 = setLine(Batterywidget);
    BatteryLayout->addWidget(line_8);

    mLowpowerFrame = new QFrame(Batterywidget);
    setFrame_Noframe(mLowpowerFrame);


    mLowpowerLabel1 = new QLabel(mLowpowerFrame);
    mLowpowerLabel1->setFixedSize(84,60);
    mLowpowerLabel2 = new QLabel(mLowpowerFrame);
    mLowpowerLabel2->setFixedSize(370,60);

    QHBoxLayout *mLowpowerLayout = new QHBoxLayout(mLowpowerFrame);
    mLowpowerLayout->setContentsMargins(16, 0, 16, 0);

    m_lowpowerComboBox1 = new QComboBox(mLowpowerFrame);
    m_lowpowerComboBox1->setObjectName("lowbatterylimit");
    m_lowpowerComboBox1->setFixedWidth(80);
    mLowpowerComboBox2 = new QComboBox(mLowpowerFrame);
    mLowpowerComboBox2->setObjectName("lowbatterysettings");
    mLowpowerComboBox2->setMinimumWidth(200);

    mLowpowerLayout->setSpacing(8);
    mLowpowerLayout->addWidget(mLowpowerLabel1);
    mLowpowerLayout->addWidget(m_lowpowerComboBox1);
    mLowpowerLayout->addWidget(mLowpowerLabel2);
    mLowpowerLayout->addWidget(mLowpowerComboBox2);

    BatteryLayout->addWidget(mLowpowerFrame);

    line_9 = setLine(Batterywidget);
    BatteryLayout->addWidget(line_9);

    mNoticeLFrame = new QFrame(Batterywidget);
    setFrame_Noframe(mNoticeLFrame);


    QHBoxLayout *mNoticeLayout = new QHBoxLayout(mNoticeLFrame);
    mNoticeLayout->setContentsMargins(16, 0, 16, 0);

    mNoticeLabel = new QLabel(mNoticeLFrame);
    mNoticeLabel->setFixedSize(550,59);

    mNoticeComboBox = new QComboBox(mNoticeLFrame);
    mNoticeComboBox->setObjectName("noticesettings");
    mNoticeComboBox->setMinimumWidth(200);

    mNoticeLayout->addWidget(mNoticeLabel);
    mNoticeLayout->addWidget(mNoticeComboBox);

    BatteryLayout->addWidget(mNoticeLFrame);

    line_10 = setLine(Batterywidget);
    BatteryLayout->addWidget(line_10);

    mLowSaveFrame = new QFrame(Batterywidget);
    setFrame_Noframe(mLowSaveFrame);

    QHBoxLayout *mLowSaveLayout = new QHBoxLayout(mLowSaveFrame);
    mLowSaveLayout->setContentsMargins(16, 0, 16, 0);

    mLowSaveLabel = new QLabel(mLowSaveFrame);
    mLowSaveLabel->setFixedSize(550,59);

    mLowSaveBtn = new KSwitchButton(mLowSaveFrame);
    mLowSaveBtn->setObjectName("lowbatterysave");

    mLowSaveLayout->addWidget(mLowSaveLabel);
    mLowSaveLayout->addStretch();
    mLowSaveLayout->addWidget(mLowSaveBtn);

    BatteryLayout->addWidget(mLowSaveFrame);

    line_11 = setLine(Batterywidget);
    BatteryLayout->addWidget(line_11);

    mBatterySaveFrame = new QFrame(Batterywidget);
    setFrame_Noframe(mBatterySaveFrame);

    QHBoxLayout *mBatterySaveLayout = new QHBoxLayout(mBatterySaveFrame);
    mBatterySaveLayout->setContentsMargins(16, 0, 16, 0);

    mBatterySaveLabel = new QLabel(mBatterySaveFrame);
    mBatterySaveLabel->setFixedSize(550,59);

    mBatterySaveBtn = new KSwitchButton(mBatterySaveFrame);
    mBatterySaveBtn->setObjectName("batterysave");

    mBatterySaveLayout->addWidget(mBatterySaveLabel);
    mBatterySaveLayout->addStretch();
    mBatterySaveLayout->addWidget(mBatterySaveBtn);

    BatteryLayout->addWidget(mBatterySaveFrame);

    line_12 = setLine(Batterywidget);
    BatteryLayout->addWidget(line_12);

    mDisplayTimeFrame = new QFrame(Batterywidget);
    setFrame_Noframe(mDisplayTimeFrame);

    QHBoxLayout *mDisplayTimeLayout = new QHBoxLayout(mDisplayTimeFrame);
    mDisplayTimeLayout->setContentsMargins(16, 0, 16, 0);

    mDisplayTimeLabel = new QLabel(mDisplayTimeFrame);
    mDisplayTimeLabel->setFixedSize(550,59);

    mDisplayTimeBtn = new KSwitchButton(mDisplayTimeFrame);
    mDisplayTimeBtn->setObjectName("powershowtime");

    mDisplayTimeLayout->addWidget(mDisplayTimeLabel);
    mDisplayTimeLayout->addStretch();
    mDisplayTimeLayout->addWidget(mDisplayTimeBtn);

    BatteryLayout->addWidget(mDisplayTimeFrame);

    mverticalLayout->addWidget(Batterywidget);
    mverticalLayout->addStretch();

    retranslateUi();
}

void Power::setVisibleBySecurity()
{
    // 安全管控 免密登录及自动登录设置项的显示与隐藏
    QVariantMap ModuleMap = UkccCommon::getModuleHideStatus();
    QString moduleSettings = ModuleMap.value(name().toLower() + "Settings").toString();
    qDebug()<<Q_FUNC_INFO<<"moduleSettings:"<<moduleSettings;
    QStringList setItems = moduleSettings.split(",");

    qDebug() << Q_FUNC_INFO << moduleSettings << "===" <<  name().toLower();

    foreach (QString setItem, setItems) {
        QStringList item = setItem.split(":");
        qDebug() << "set item Name: " << item.at(0);
        if (item.at(0) == "CloseDisplayFrame") {
            mCloseFrame->setVisible(item.at(1) == "true");
            line_4->setVisible(item.at(1) == "true");
        }
        if (item.at(0) == "SleepFrame") {
            mSleepFrame->setVisible(item.at(1) == "true");
            line_5->setVisible(item.at(1) == "true");
        }
        if (item.at(0) == "mSleepPwdFrame") {
            mSleepPwdFrame->setVisible(item.at(1) == "true");
            line_1->setVisible(item.at(1) == "true");
        }
        if (item.at(0) == "mWakenPwdFrame") {
            mWakenPwdFrame->setVisible(item.at(1) == "true");
            line_2->setVisible(item.at(1) == "true");
        }
    }

    if (UkccCommon::getCpuInfo().contains("9006C", Qt::CaseInsensitive)) {
        mResourceFrame->setVisible(false);
    }
}

bool Power::kconf2Enable(const QString &cloudPlatForm) const
{
    if (cloudPlatForm == "huawei") {
        qDebug() << Q_FUNC_INFO << m_isExistHibernate << m_isExistHibernate;
        if ((!m_isExistHibernate) && (!m_isExistSuspend)) return false;
    }
    return true;
}

void Power::initInterface()
{
    if (mHibernateInterface == nullptr) {
        mHibernateInterface = new QDBusInterface("org.freedesktop.login1",
                                                 "/org/freedesktop/login1",
                                                 "org.freedesktop.login1.Manager",
                                                 QDBusConnection::systemBus(),this);

        isSuspendSupply();
        isHibernateSupply();
    }
}

void Power::retranslateUi()
{
    //~ contents_path /Power/Require password when suspend/hibernate
    QString str = (m_isExistHibernate && m_isExistSuspend) ? tr("Require password when suspend/hibernate")
                                                         : (m_isExistSuspend ? tr("Require password when suspend") :
                                                                              (m_isExistHibernate ? tr("Require password when hibernate") : ""));
    if (str.isEmpty()) {
        mSleepPwdFrame->setVisible(false);
    }
    if (QLabelSetText(mSleepPwdLabel, str)) {
        mSleepPwdLabel->setToolTip(str);
    }

    //~ contents_path /Power/Password required when waking up the screen
    if (QLabelSetText(mWakenPwdLabel, tr("Password required when waking up the screen"))) {
        mSleepPwdLabel->setToolTip(tr("Password required when waking up the screen"));
    }

    //~ contents_path /Power/Press the power button
    if (QLabelSetText(mPowerKeyLabel, tr("Press the power button"))) {
        mPowerKeyLabel->setToolTip("Press the power button");
    }

    //~ contents_path /Power/Time to close display
    if (QLabelSetText(mCloseLabel, tr("Time to close display"))) {
        mCloseLabel->setToolTip(tr("Time to close display"));
    }

    //~ contents_path /Power/Time to sleep
    if (QLabelSetText(mSleepLabel, tr("Time to sleep"))) {
        mSleepLabel->setToolTip(tr("Time to sleep"));
    }

    //~ contents_path /Power/Notebook cover
    if (QLabelSetText(mCloseLidLabel, tr("Notebook cover"))) {
        mCloseLidLabel->setToolTip(tr("Notebook cover"));
    }

    //~ contents_path /Power/Host light strip
    if (QLabelSetText(mHostLightLabel, tr("Host light strip"))) {
        mHostLightLabel->setToolTip(tr("Host light strip"));
    }

    //~ contents_path /Power/Dynamic resource scheduling
    if (QLabelSetText(mResourceLabel, tr("Dynamic resource scheduling"))) {
        mResourceLabel->setToolTip(tr("Dynamic resource scheduling"));
    }

    //~ contents_path /Power/Using power
    if (QLabelSetText(mPowerLabel, tr("Using power"))) {
        mPowerLabel->setToolTip(tr("Using power"));
    }

    //~ contents_path /Power/Using battery
    if (QLabelSetText(mBatteryLabel, tr("Using battery"))) {
        mBatteryLabel->setToolTip(tr("Using battery"));
    }

    if (QLabelSetText(mDarkenLabel, tr(" Time to darken"))) {
        mDarkenLabel->setToolTip(tr(" Time to darken"));
    }

    if (QLabelSetText(mLowpowerLabel1, tr("Battery level is lower than"))) {
        mLowpowerLabel1->setToolTip(tr("Battery level is lower than"));
    }

    mLowpowerLabel2->setText(tr("Run"));

    //~ contents_path /Power/Low battery notification
    if (QLabelSetText(mNoticeLabel, tr("Low battery notification"))) {
        mNoticeLabel->setToolTip(tr("Low battery notification"));
    }

    //~ contents_path /Power/"Automatically run saving mode when low battery
    if (QLabelSetText(mLowSaveLabel, tr("Automatically run saving mode when low battery"))) {
        mLowSaveLabel->setToolTip(tr("Automatically run saving mode when the low battery"));
    }

    if (QLabelSetText(mBatterySaveLabel, tr("Automatically run saving mode when using battery"))) {
        mBatterySaveLabel->setToolTip(tr("Automatically run saving mode when using battery"));
    }

    if (QLabelSetText(mDisplayTimeLabel, tr("Display remaining charging time and usage time"))) {
        mDisplayTimeLabel->setToolTip(tr("Display remaining charging time and usage time"));
    }
    
}

void Power::resetui()
{
    mBatterySaveFrame->hide();

    mDarkenFrame->hide();
    mDisplayTimeFrame->hide();
    line_8->hide();
    line_11->hide();
    line_12->hide();
    //不存在盖子隐藏该项
    if (!mIsExistLid) {
        mCloseLidFrame->hide();
        line_5->hide();
    }
    
    // 不存在电池隐藏这些设置项
    if (!mHasBat) {
        line_7->hide();
        mBatteryFrame->hide();
        m_batteryPlanTitleLabel->hide();
        clearAutoItem(BatteryLayout);
        Batterywidget->hide();
    }

    //Intel作如下处理
    if (UkccCommon::isTablet()) {
        mSleepPwdFrame->hide();
        mWakenPwdFrame->hide();
        mPowerKeyFrame->hide();
        mCloseLidFrame->hide();
        mDarkenFrame->hide();
        mLowpowerFrame->hide();
        mNoticeLFrame->hide();
        mDisplayTimeFrame->hide();
        mItem->changeSize(0, 0);
        line_1->hide();
        line_2->hide();
        line_3->hide();
        line_5->hide();
        line_8->hide();
        line_9->hide();
        line_10->hide();
        line_11->hide();
        line_12->hide();
    }
}

void Power::setupComponent()
{
    // 合盖
    closeLidStringList << tr("Nothing") << tr("Blank") << tr("Shutdown") << tr("Suspend") << tr("Hibernate");
    mCloseLidComboBox->insertItem(0, closeLidStringList.at(0), "nothing");
    mCloseLidComboBox->insertItem(1, closeLidStringList.at(1), "blank");
    mCloseLidComboBox->insertItem(2, closeLidStringList.at(2), "shutdown");
    if (m_isExistSuspend) mCloseLidComboBox->insertItem(3, closeLidStringList.at(3), "suspend");
    if (m_isExistHibernate) {
        mCloseLidComboBox->insertItem(4, closeLidStringList.at(4), "hibernate");
    }

    //按下电源键时
    buttonStringList << tr("Interactive") << tr("Shutdown") << tr("Suspend") << tr("Hibernate");
    mPowerKeyComboBox->insertItem(0, buttonStringList.at(0), "interactive");
    mPowerKeyComboBox->insertItem(1, buttonStringList.at(1), "shutdown");
    if (m_isExistSuspend) mPowerKeyComboBox->insertItem(2, buttonStringList.at(2), "suspend");
    if (m_isExistHibernate) mPowerKeyComboBox->insertItem(3, buttonStringList.at(3), "hibernate");

    //关闭显示器
    closeStringList <<tr("1min")<< tr("5min") << tr("10min") << tr("15min") << tr("30min") << tr("1h") << tr("2h") << tr("Never");
    mCloseComboBox->insertItem(0, closeStringList.at(0), QVariant::fromValue(1)); 
    mCloseComboBox->insertItem(1, closeStringList.at(1), QVariant::fromValue(5));
    mCloseComboBox->insertItem(2, closeStringList.at(2), QVariant::fromValue(10));
    mCloseComboBox->insertItem(3, closeStringList.at(3), QVariant::fromValue(15));
    mCloseComboBox->insertItem(4, closeStringList.at(4), QVariant::fromValue(30));
    mCloseComboBox->insertItem(5, closeStringList.at(5), QVariant::fromValue(60)); 
    mCloseComboBox->insertItem(6, closeStringList.at(6), QVariant::fromValue(120));    
    mCloseComboBox->insertItem(7, closeStringList.at(7), QVariant::fromValue(0));

    //睡眠
    sleepStringList << tr("10min") << tr("15min") << tr("30min") << tr("1h") << tr("2h") << tr("3h") << tr("Never");
    mSleepComboBox->insertItem(0, sleepStringList.at(0), QVariant::fromValue(10));
    mSleepComboBox->insertItem(1, sleepStringList.at(1), QVariant::fromValue(15));
    mSleepComboBox->insertItem(2, sleepStringList.at(2), QVariant::fromValue(30));
    mSleepComboBox->insertItem(3, sleepStringList.at(3), QVariant::fromValue(60));
    mSleepComboBox->insertItem(4, sleepStringList.at(4), QVariant::fromValue(120));
    mSleepComboBox->insertItem(5, sleepStringList.at(5), QVariant::fromValue(180));
    mSleepComboBox->insertItem(6, sleepStringList.at(6), QVariant::fromValue(0));

    //电源计划
    PowerplanStringList << tr("Balance") << tr("Energy Efficiency")<<tr("Performance");
    mPowerComboBox->insertItem(0, PowerplanStringList.at(0), "Balance");
    mPowerComboBox->insertItem(1, PowerplanStringList.at(1), "Energy Efficiency");
    mPowerComboBox->insertItem(2, PowerplanStringList.at(2), "Performance");

    BatteryplanStringList << tr("Balance") << tr("Energy Efficiency")<<tr("Performance");
    mBatteryComboBox->insertItem(0, BatteryplanStringList.at(0), "Balance");
    mBatteryComboBox->insertItem(1, BatteryplanStringList.at(1), "Energy Efficiency");
    mBatteryComboBox->insertItem(2, BatteryplanStringList.at(2), "Performance");

    //低电量时执行
    LowpowerStringList << tr("Nothing") << tr("Blank") << tr("Shutdown")  << tr("Suspend") << tr("Hibernate");
    mLowpowerComboBox2->insertItem(0, LowpowerStringList.at(0), "nothing");
    mLowpowerComboBox2->insertItem(1, LowpowerStringList.at(1), "blank");
    mLowpowerComboBox2->insertItem(2, LowpowerStringList.at(2), "shutdown");
    if (m_isExistSuspend) mLowpowerComboBox2->insertItem(3, LowpowerStringList.at(3), "suspend");
    if (m_isExistHibernate) mLowpowerComboBox2->insertItem(4, LowpowerStringList.at(4), "hibernate");

    //低电量通知
    for (int i = 1; i < 5; i++) {
        mNoticeComboBox->insertItem(i-1, QString("%1%").arg(i*10));
    }

    //电池低电量范围
    int batteryRemain = settings->get(PER_ACTION_CRI).toInt();
    for(int i = 0; i < batteryRemain; i++) {
        m_lowpowerComboBox1->insertItem(i, QString("%1%").arg(i+1));
    }
}

void Power::setupConnect()
{
    connect(mSleepPwdBtn,&KSwitchButton::stateChanged, [=](bool checked){
        UkccCommon::buriedSettings(name(), "whether password is required for wake-up after sleep", QString("settings"), checked ? "true" : "false");
        screensettings->set(SLEEP_ACTIVATION_ENABLED,checked);
    });

    if (screensettings->keys().contains("closeActivationEnabled")) {
        connect(mWakenPwdBtn,&KSwitchButton::stateChanged, [=](bool checked){
            UkccCommon::buriedSettings(name(), "whether password is required for wake-up screen", QString("settings"), checked ? "true" : "false");
            screensettings->set(CLOSE_ACTIVATION_ENABLED,checked);
        });
    }

    connect(mPowerKeyComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        UkccCommon::buriedSettings(name(), "execute when pressing the power key", QString("select"), mPowerKeyComboBox->itemData(index).toString());
        settings->set(BUTTON_POWER_KEY, mPowerKeyComboBox->itemData(index));
    });

    connect(mCloseComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        Q_UNUSED(index)
        UkccCommon::buriedSettings(name(), "turn off the display after this time period", QString("select"), mCloseComboBox->currentData().toString());
        if (mCloseComboBox->currentData(Qt::UserRole).toInt() == 0) {
            settings->set(SLEEP_DISPLAY_AC_KEY, -1);
            qDebug()<<"sleep-display-ac : "<<settings->trySet(SLEEP_DISPLAY_AC_KEY, -1);
        } else {
            settings->set(SLEEP_DISPLAY_AC_KEY, QVariant(mCloseComboBox->currentData(Qt::UserRole).toInt() * 60));
            qDebug()<<"sleep-display-ac : "<<settings->trySet(SLEEP_DISPLAY_AC_KEY, mCloseComboBox->currentData(Qt::UserRole).toInt() * 60);
        }
        setHintLable();
    });

    connect(mCloseComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        Q_UNUSED(index)
        if (mCloseComboBox->currentData(Qt::UserRole).toInt() == 0) {
            settings->set(SLEEP_DISPLAY_BATT_KEY, -1);
            qDebug()<<"sleep-display-battery : "<<settings->trySet(SLEEP_DISPLAY_BATT_KEY, -1);
        } else {
            settings->set(SLEEP_DISPLAY_BATT_KEY, QVariant(mCloseComboBox->currentData(Qt::UserRole).toInt() * 60));
            qDebug()<<"sleep-display-battery : "<<settings->trySet(SLEEP_DISPLAY_BATT_KEY, mCloseComboBox->currentData(Qt::UserRole).toInt() * 60);
        }
    });

    connect(mSleepComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        Q_UNUSED(index)
        UkccCommon::buriedSettings(name(), "the system go to sleep after this time period", QString("select"), mSleepComboBox->currentData().toString());
        if (mSleepComboBox->currentData(Qt::UserRole).toInt() == 0) {
            settings->set(SLEEP_COMPUTER_AC_KEY, -1);
            qDebug()<<"sleep-computer-ac : "<<settings->trySet(SLEEP_COMPUTER_AC_KEY, -1);
        } else {
            settings->set(SLEEP_COMPUTER_AC_KEY, QVariant(mSleepComboBox->currentData(Qt::UserRole).toInt() * 60));
            qDebug()<<"sleep-computer-ac : "<<settings->trySet(SLEEP_COMPUTER_AC_KEY, mSleepComboBox->currentData(Qt::UserRole).toInt() * 60);
        }
    });

    connect(mSleepComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        Q_UNUSED(index)
        if (mSleepComboBox->currentData(Qt::UserRole).toInt() == 0) {
            settings->set(SLEEP_COMPUTER_BATT_KEY, -1);
            qDebug()<<"sleep-computer-battery : "<<settings->trySet(SLEEP_COMPUTER_BATT_KEY, -1);
        } else {
            settings->set(SLEEP_COMPUTER_BATT_KEY, QVariant(mSleepComboBox->currentData(Qt::UserRole).toInt() * 60));
            qDebug()<<"sleep-computer-battery : "<<settings->trySet(SLEEP_COMPUTER_BATT_KEY, mSleepComboBox->currentData(Qt::UserRole).toInt() * 60);
        }
        setHintLable();
    });

    connect(mCloseLidComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        UkccCommon::buriedSettings(name(), "execute when the notebook is closed", QString("select"), mCloseLidComboBox->currentData().toString());
        settings->set(BUTTON_LID_AC_KEY, mCloseLidComboBox->itemData(index));
        settings->set(BUTTON_LID_BATT_KET, mCloseLidComboBox->itemData(index));
    });

    connect(mHostLightSButton, &KSwitchButton::stateChanged, this, [=](bool checked){
        UkccCommon::buriedSettings(name(), "Host light strip", QString("settings"), checked ? "true":"false");
        QDBusReply<bool> ret = hostLightStripDbus->call("setHostLightStrip", checked);
        if (!ret) {
            qDebug() << "setHostLightStrip failed!";
            mHostLightSButton->blockSignals(true);
            mHostLightSButton->setChecked(!checked);
            mHostLightSButton->blockSignals(false);
        }
    });


    connect(mResourceSButton, &KSwitchButton::clicked, this, [=](bool checked){
        if (mProcManagerSettings != nullptr && mProcManagerSettings->keys().contains("resourceLimitEnabled")) {
            mProcManagerSettings->set(RESOURCE_LIMIT_ENABLED, checked);
        }
    });

    if (mKeys.contains("powerPolicyAc") && mKeys.contains("powerPolicyBattery")) {
        connect(mPowerComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
            UkccCommon::buriedSettings(name(), "mode when using power supply", QString("select"), mPowerComboBox->currentData().toString());
            if (index == 0) {
                settings->set(POWER_POLICY_AC, 1);
            }  else if (index == 1) {
                settings->set(POWER_POLICY_AC, 2);
            } else {
                settings->set(POWER_POLICY_AC, 0);
            }
        });

        connect(mBatteryComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
            UkccCommon::buriedSettings(name(), "mode when using battery supply", QString("select"), mBatteryComboBox->currentData().toString());
            if (index == 0) {
                settings->set(POWER_POLICY_BATTARY, 1);
            } else if (index == 1) {
                settings->set(POWER_POLICY_BATTARY, 2);
            } else {
                settings->set(POWER_POLICY_BATTARY, 0);
            }
        });
    }

    connect(mDarkenComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        Q_UNUSED(index)
        UkccCommon::buriedSettings(name(), "reduce screen brightness after this time period", QString("select"), mBatteryComboBox->currentData().toString());
        if (mDarkenComboBox->currentData(Qt::UserRole).toInt() == 0) {
            settings->set(IDLE_DIM_TIME_KEY, -1);
        } else {
            settings->set(IDLE_DIM_TIME_KEY, QVariant(mDarkenComboBox->currentData(Qt::UserRole).toInt() * 60));
        }
    });

    connect(m_lowpowerComboBox1, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        UkccCommon::buriedSettings(name(), "low battery value", QString("select"), m_lowpowerComboBox1->currentData().toString());
       settings->set(PER_ACTION_KEY, index + 1); 
    });

    connect(mLowpowerComboBox2, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        UkccCommon::buriedSettings(name(), "execute below low battery level", QString("select"), mLowpowerComboBox2->currentData().toString());
        settings->set(ACTION_CRI_BTY, mLowpowerComboBox2->itemData(index));
    });

    connect(mNoticeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
        UkccCommon::buriedSettings(name(), "notify when the power is lower than this value", QString("select"), mNoticeComboBox->currentText());
        settings->set(PERCENTAGE_LOW, (index + 1)*10);
    });

    connect(mLowSaveBtn,&KSwitchButton::stateChanged, [=](bool checked){
        UkccCommon::buriedSettings(name(), "whether the saving mode is autoturned on when the battery is low", QString("select"), checked ? "true" : "false");
        settings->set(LOW_BATTERY_AUTO_SAVE,checked);
    });

    connect(settings, &QGSettings::changed, this, &Power::initCustomPlanStatus);

    connect(screensettings,&QGSettings::changed,[=](){
        initCustomPlanStatus();
    });

    if (mProcManagerSettings != nullptr && mProcManagerSettings->keys().contains("resourceLimitEnabled")) {
        connect(mProcManagerSettings, &QGSettings::changed, this, &Power::initCustomPlanStatus);
    }

    setVisibleBySecurity();
    QDBusConnection::sessionBus().connect(QString(), QString("/"), "org.ukui.ukcc.session.interface", "configChanged", this, SLOT(setVisibleBySecurity()));
    setEnableBySecurity();
    QDBusConnection::sessionBus().connect(QString(), QString("/"), "org.ukui.ukcc.session.interface", "configChanged", this, SLOT(setEnableBySecurity()));
}

void Power::initCustomPlanStatus()
{
    // 信号阻塞
    mPowerKeyComboBox->blockSignals(true);
    mCloseComboBox->blockSignals(true);
    mSleepComboBox->blockSignals(true);
    mCloseLidComboBox->blockSignals(true);
    mPowerComboBox->blockSignals(true);
    mBatteryComboBox->blockSignals(true);
    m_lowpowerComboBox1->blockSignals(true);
    mLowpowerComboBox2->blockSignals(true);
    mNoticeComboBox->blockSignals(true);
    mSleepPwdBtn->blockSignals(true);
    mWakenPwdBtn->blockSignals(true);
    mLowSaveBtn->blockSignals(true);
    mBatterySaveBtn->blockSignals(true);

    mPowerKeyComboBox->setCurrentIndex(mPowerKeyComboBox->findData(settings->get(BUTTON_POWER_KEY).toString()));
    if (settings->get(SLEEP_COMPUTER_AC_KEY).toInt() == -1) {
        mSleepComboBox->setCurrentIndex(mSleepComboBox->findData(0));
    } else {
        mSleepComboBox->setCurrentIndex(mSleepComboBox->findData(settings->get(SLEEP_COMPUTER_AC_KEY).toInt() / FIXES));
    }

    if (settings->get(SLEEP_DISPLAY_AC_KEY).toInt() == -1) {
        mCloseComboBox->setCurrentIndex(mCloseComboBox->findData(0));
    } else {
        mCloseComboBox->setCurrentIndex(mCloseComboBox->findData(settings->get(SLEEP_DISPLAY_AC_KEY).toInt() / FIXES));;
    }

    setHintLable();

    mCloseLidComboBox->setCurrentIndex(mCloseLidComboBox->findData(settings->get(BUTTON_LID_AC_KEY).toString()));

    if (isSupportHostLight && hostLightStripDbus != nullptr) {
        mHostLightSButton->blockSignals(true);
        QDBusReply<bool> ret = hostLightStripDbus->call("getHostLightStripStatus");
        mHostLightSButton->setChecked(ret);
        mHostLightSButton->blockSignals(false);
    }

    if (mProcManagerSettings != nullptr && mProcManagerSettings->keys().contains("resourceLimitEnabled")) {
        mResourceSButton->blockSignals(true);
        mResourceSButton->setChecked(mProcManagerSettings->get(RESOURCE_LIMIT_ENABLED).toBool());
        mResourceSButton->blockSignals(false);
    }

    if (mKeys.contains("powerPolicyAc") && mKeys.contains("powerPolicyBattery")) {
        if (1 == settings->get(POWER_POLICY_AC).toInt()) {
            mPowerComboBox->setCurrentIndex(0);
        } else if (2 == settings->get(POWER_POLICY_AC).toInt()){
            mPowerComboBox->setCurrentIndex(1);
        } else {
            mPowerComboBox->setCurrentIndex(2);
        }
        if (1 == settings->get(POWER_POLICY_BATTARY).toInt()) {
            mBatteryComboBox->setCurrentIndex(0);
        } else if (2 == settings->get(POWER_POLICY_BATTARY).toInt()) {
            mBatteryComboBox->setCurrentIndex(1);
        } else {
            mBatteryComboBox->setCurrentIndex(2);
        }
    } else {
        mPowerComboBox->setEnabled(false);
        mBatteryComboBox->setEnabled(false);
    }
    m_lowpowerComboBox1->setCurrentIndex(settings->get(PER_ACTION_KEY).toInt() - 1);
    mLowpowerComboBox2->setCurrentIndex(mLowpowerComboBox2->findData(settings->get(ACTION_CRI_BTY).toString()));
    mNoticeComboBox->setCurrentIndex(settings->get(PERCENTAGE_LOW).toInt()/10 - 1);

    if (screensettings->keys().contains("sleepActivationEnabled")) {
        mSleepPwdBtn->setChecked(screensettings->get(SLEEP_ACTIVATION_ENABLED).toBool());
    }

    if (screensettings->keys().contains("closeActivationEnabled"))
        mWakenPwdBtn->setChecked(screensettings->get(CLOSE_ACTIVATION_ENABLED).toBool());

    if (mKeys.contains("lowBatteryAutoSave")) {
        mLowSaveBtn->setChecked(settings->get(LOW_BATTERY_AUTO_SAVE).toBool());
    } else {
        mLowSaveFrame->hide();
        line_10->hide();
    }

    // 信号阻塞解除
    mPowerKeyComboBox->blockSignals(false);
    mCloseComboBox->blockSignals(false);
    mSleepComboBox->blockSignals(false);
    mCloseLidComboBox->blockSignals(false);
    mPowerComboBox->blockSignals(false);
    mBatteryComboBox->blockSignals(false);
    m_lowpowerComboBox1->blockSignals(false);
    mLowpowerComboBox2->blockSignals(false);
    mNoticeComboBox->blockSignals(false);
    mSleepPwdBtn->blockSignals(false);
    mWakenPwdBtn->blockSignals(false);
    mLowSaveBtn->blockSignals(false);
    mBatterySaveBtn->blockSignals(false);
}

void Power::isLidPresent()
{
    QDBusInterface *LidInterface = new QDBusInterface("org.freedesktop.UPower",
                       "/org/freedesktop/UPower",
                       "org.freedesktop.DBus.Properties",
                        QDBusConnection::systemBus(),this);


    if (!LidInterface->isValid()) {
        qDebug() << "Create UPower Lid Interface Failed : " <<
            QDBusConnection::systemBus().lastError();
        return;
    }
    QDBusReply<QVariant> LidInfo;
    LidInfo = LidInterface->call("Get", "org.freedesktop.UPower", "LidIsPresent");
    mIsExistLid = LidInfo.value().toBool();
}

bool Power::isHibernateSupply()
{
    if (!mHibernateInterface->isValid()) {
        qDebug() << "Create login1 Hibernate Interface Failed : " <<
            QDBusConnection::systemBus().lastError();
        return false;
    }
    QDBusReply<QString> HibernateInfo;
    HibernateInfo = mHibernateInterface->call("CanHibernate");
    m_isExistHibernate = HibernateInfo == "yes" ? true : false;

    return m_isExistHibernate;
}

// 是否支持睡眠
bool Power::isSuspendSupply()
{
    if (!mHibernateInterface->isValid()) {
        qDebug() << "Create login1 CanSuspend Interface Failed : " <<
            QDBusConnection::systemBus().lastError();
        return false;
    }
    QDBusReply<QString> HibernateInfo;
    HibernateInfo = mHibernateInterface->call("CanSuspend");
    if (HibernateInfo == "yes" || HibernateInfo == "challenge") { // bug#200148
        m_isExistSuspend = true;
    } else {
        m_isExistSuspend = false;
    }

    return m_isExistSuspend;
}

bool Power::isExitBattery()
{
    /* 默认机器没有电池 */
    mHasBat = false;
    QDBusInterface *brightnessInterface = new QDBusInterface("org.freedesktop.UPower",
                                     "/org/freedesktop/UPower/devices/DisplayDevice",
                                     "org.freedesktop.DBus.Properties",
                                     QDBusConnection::systemBus(), this);
    if (!brightnessInterface->isValid()) {
        qDebug() << "Create UPower Interface Failed : " << QDBusConnection::systemBus().lastError();
        return false;
    }

    QDBusReply<QVariant> briginfo;
    briginfo  = brightnessInterface ->call("Get", "org.freedesktop.UPower.Device", "PowerSupply");

    if (briginfo.value().toBool()) {
        mHasBat = true ;
    }

    return mHasBat;
}

bool Power::QLabelSetText(QLabel *label, QString string)
{
    bool is_over_length = false;
    QFontMetrics fontMetrics(label->font());
    int fontSize = fontMetrics.width(string);
    QString str = string;
    if (fontSize > (label->width()-5)) {
        str = fontMetrics.elidedText(string, Qt::ElideRight, label->width());
        is_over_length = true;
    }
    label->setText(str);
    return is_over_length;
}

void Power::clearAutoItem(QVBoxLayout *mLyt)
{
    if (mLyt->layout() != NULL) {
        QLayoutItem *item;
        while ((item = mLyt->layout()->takeAt(0)) != NULL)
        {
            if(item->widget()) {
               item->widget()->setParent(NULL);
            }
            delete item;
            item = nullptr;
        }
    }
}

void Power::setFrame_Noframe(QFrame *frame)
{
    frame->setMinimumSize(QSize(550, 60));
    frame->setFrameShape(QFrame::NoFrame);
}

QFrame *Power::setLine(QFrame *frame)
{
    QFrame *line = new QFrame(frame);
    line->setMinimumSize(QSize(0, 1));
    line->setMaximumSize(QSize(16777215, 1));
    line->setLineWidth(0);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    return line;
}

void Power::setHintLable()
{
    if ((((mCloseComboBox->currentData().toInt() > mSleepComboBox->currentData().toInt()) && mSleepComboBox->currentData().toInt() !=0) ||
         (mCloseComboBox->currentData().toInt() == 0 && mSleepComboBox->currentData().toInt() != 0)) && m_isExistSuspend) {

        mCloseHintIcon->setVisible(true);
        mCloseHintLabel->setVisible(true);
        mCloseFrame->setMinimumHeight(100);
    } else {
        mCloseHintIcon->setVisible(false);
        mCloseHintLabel->setVisible(false);
        mCloseFrame->setMinimumHeight(60);
    }
}

void Power::hideComponent()
{
    line_4->setVisible(m_isExistSuspend);
    mSleepFrame->setVisible(m_isExistSuspend);
}

void Power::hideComponentByConf2()
{
    QString cloudPlatForm = QString(QLatin1String(kdk_system_get_hostCloudPlatform()));
    if (cloudPlatForm == "huawei") {
        m_powerPlanTitleLabel->setVisible(false);
        m_batteryPlanTitleLabel->setVisible(false);

        PowerPlanwidget->setVisible(false);
        Batterywidget->setVisible(false);
        mWakenPwdFrame->setVisible(false);
        mPowerKeyFrame->setVisible(false);
        mCloseLidFrame->setVisible(false);
        mSleepFrame->setVisible(false);
        line_2->setVisible(false);
        line_3->setVisible(false);
        line_4->setVisible(false);
        line_5->setVisible(false);
        mResourceFrame->setVisible(false);
    }
}

void Power::setEnableBySecurity() {
    QVariantMap ModuleMap = UkccCommon::getModuleHideStatus();
    QString moduleSettings = ModuleMap.value("powerEnable").toString();
    QStringList setItems = moduleSettings.split(",");
    qDebug() << Q_FUNC_INFO << moduleSettings;
    foreach (QString setItem, setItems) {
        QStringList item = setItem.split(":");
        if (item.size() != 2) {
            continue;
        }
        qDebug() << "set item Name: " << item.at(0);
        if (item.at(0) == "mSleepPwdFrame") {
            mSleepPwdFrame->setEnabled(item.at(1) == "true");
        }
        if (item.at(0) == "mWakenPwdFrame") {
            mWakenPwdFrame->setEnabled(item.at(1) == "true");
        }
    }
}

QString Power::translationPath() const { 
    return "/usr/share/ukui-power-manager/power/translations/%1.ts"; 
}
