 /*
  * Copyright (C) 2018 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 3, 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, see <http://www.gnu.org/licenses/>.
  *
 **/

#include <QLabel>
#include <QVBoxLayout>
#include <QDBusReply>
#include <QPixmap>
#include <QPushButton>
#include <QEvent>
#include <QListWidgetItem>
#include <QListWidget>
#include <QDebug>
#include <QDBusInterface>
#include "powermanager.h"
#include "lockchecker.h"
#include "klabel.h"

const static QString login1Service = QStringLiteral("org.freedesktop.login1");
const static QString login1Path = QStringLiteral("/org/freedesktop/login1");
const static QString login1ManagerInterface = QStringLiteral("org.freedesktop.login1.Manager");

#define NORMAL "normal"
#define FOCUS "focus"

#ifdef USE_INTEL
PowerManager::PowerManager(QWidget *parent)
 : QWidget(parent),
   lasttime(QTime::currentTime())
{
    resize((ITEM_WIDTH*4 + ITEM_SPACING*3), ITEM_HEIGHT);
    initUI();
    setQSS();
}
#else
PowerManager::PowerManager(int num, QWidget *parent)
    : QListWidget(parent),
      lasttime(QTime::currentTime())
{
  //  resize(ITEM_WIDTH*7, ITEM_HEIGHT);
    QFile qssFile(":/qss/assets/authdialog.qss");
    if(qssFile.open(QIODevice::ReadOnly)) {
        this->setStyleSheet(qssFile.readAll());
    }
    setObjectName("PowerManager");
    setFlow(QListWidget::LeftToRight);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setSelectionMode(QListWidget::NoSelection);


    //QObject::connect(this,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(powerClicked(QListWidgetItem*)));

    sessionInterface = new QDBusInterface("org.gnome.SessionManager",
                                          "/org/gnome/SessionManager",
                                          "org.gnome.SessionManager",
                                          QDBusConnection::sessionBus(),
                                          this);

    loginInterface = new QDBusInterface(login1Service,
                                        login1Path,
                                        login1ManagerInterface,
                                        QDBusConnection::systemBus(),
                                        this);

    QDBusReply<QString> stateReply = loginInterface->call("CanSuspend");
    if(stateReply.isValid() && stateReply.value() == "yes"){
        canSuspend = true;
    }else{
        canSuspend = false;
    }

    QDBusReply<QString> stateReply1 = loginInterface->call("CanHibernate");
    if(stateReply1.isValid() && stateReply1.value() == "yes"){
        canHibernate = true;
    }else{
        canHibernate = false;
    }

    initUI();
    resize((ITEM_WIDTH+ITEM_SPACING*2)*this->count()-ITEM_SPACING*2, ITEM_HEIGHT+ITEM_SPACING*2);
    //setCurrentRow(0);
    loginedNum = num;
}

bool PowerManager::eventFilter(QObject *obj, QEvent *event)
{
   /*这里之前使用点击事件，再某些机器上会出现点击睡眠后，先睡眠，再自动唤醒的问题,
    * 原因不明，改为鼠标释放事件时无此问题*/
    if(event->type() != QEvent::MouseButtonRelease){
        return QWidget::eventFilter(obj, event);
    }

    QString name = obj->objectName();

    if(name == "switchFace")
        switchWidgetClicked();
    else if(name == "logoutFace")
        logoutWidgetCliced();
    else if(name == "rebootFace")
        rebootWidgetClicked();
    else if(name == "shutdownFace")
        shutdownWidgetClicked();
    else if(name == "suspendFace")
        suspendWidgetClicked();
    else if(name == "hibernateFace")
        hibernateWidgetClicked();

    return QWidget::eventFilter(obj, event);
}

void PowerManager::onGlobalKeyPress(const quint8 &key)
{
}

void PowerManager::onGlobalkeyRelease(const quint8 &key)
{
//    if (key == 9) { // "Escape"

//    } else if (key == 113 || key == 114 || key == 36 || key == 104) {    // "Left" "Right" "Return" "KP_Enter"
//        keyBdRelease(key);
//    }
}

void PowerManager::keyBdRelease(int key)
{
    if(key == Qt::Key_Right){ // "Right"
        if(nowAt == -1){
            nowAt = 0;
            setButtonStyle(FOCUS);
        } else if(nowAt == 0){
            setButtonStyle(NORMAL);
            nowAt = 1;
            setButtonStyle(FOCUS);
        } else if(nowAt == 1){
            setButtonStyle(NORMAL);
            nowAt = 2;
            setButtonStyle(FOCUS);
        } else if(nowAt == 2){
            setButtonStyle(NORMAL);
            nowAt = 3;
            setButtonStyle(FOCUS);
        } else if(nowAt == 3){
            setButtonStyle(NORMAL);
            nowAt = 0;
            setButtonStyle(FOCUS);
        }
    } else if(key == Qt::Key_Left) { // "Left"
        if(nowAt == -1){
            nowAt = 3;
            setButtonStyle(FOCUS);
        } else if(nowAt == 3){
            setButtonStyle(NORMAL);
            nowAt = 2;
            setButtonStyle(FOCUS);
        } else if(nowAt == 2){
            setButtonStyle(NORMAL);
            nowAt = 1;
            setButtonStyle(FOCUS);
        } else if(nowAt == 1){
            setButtonStyle(NORMAL);
            nowAt = 0;
            setButtonStyle(FOCUS);
        } else if(nowAt == 0){
            setButtonStyle(NORMAL);
            nowAt = 3;
            setButtonStyle(FOCUS);
        }
    } else if(key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Space){ // "Return" "KP_Enter" "Space"
        if(nowAt == 0 && this->isVisible())
            hibernateWidgetClicked();
        else if(nowAt == 1 && this->isVisible())
            suspendWidgetClicked();
        else if(nowAt == 2 && this->isVisible())
            rebootWidgetClicked();
        else if(nowAt == 3 && this->isVisible())
            shutdownWidgetClicked();
    }
}

void PowerManager::clearStatus()
{
    for(nowAt = 0; nowAt < listLabel.count(); nowAt++) {
        setButtonStyle(NORMAL);
    }
    nowAt = -1;
}

void::PowerManager::setButtonStyle(QString Style)
{
    if(Style == "normal")
        listLabel.at(nowAt)->setStyleSheet("background-color: rgba(255, 255, 255, 15%);\
                                            QPushButton:hover{background-color:rgba(255,255,255,40%)};QPushButton:pressed:!hover{background-color:rgba(255,255,255,30%)}");
    else if(Style == "focus")
        listLabel.at(nowAt)->setStyleSheet("background-color: rgba(255, 255, 255, 20%);border: 1px solid #296CD9; border-radius: 64px;");
}

void PowerManager::powerClicked(QListWidgetItem *item)
{
    int interval = lasttime.msecsTo(QTime::currentTime());
    if(interval < 200 && interval > -200)
        return ;
    lasttime = QTime::currentTime();
    
    QString name = itemWidget(item)->objectName();
//    if(name == lockWidget->objectName())
//        lockWidgetClicked();
    if(switchWidget && name == switchWidget->objectName())
        switchWidgetClicked();
    else if(name == logoutWidget->objectName())
        logoutWidgetCliced();
    else if(name == rebootWidget->objectName())
        rebootWidgetClicked();
    else if(name == shutdownWidget->objectName())
        shutdownWidgetClicked();
    else if(suspendWidget &&  name == suspendWidget->objectName())
        suspendWidgetClicked();
    else if(hibernateWidget && name == hibernateWidget->objectName())
        hibernateWidgetClicked();
}
#endif

void PowerManager::lockWidgetClicked()
{
    emit lock();
}

void PowerManager::switchWidgetClicked()
{
    emit switchToUser();
}

#ifdef USE_INTEL
void PowerManager::shutdownWidgetClicked()
{
    QDBusInterface *interface = new QDBusInterface("org.gnome.SessionManager",
                                                   "/org/gnome/SessionManager",
                                                   "org.gnome.SessionManager",
                                                   QDBusConnection::sessionBus(),
                                                   this);

    QDBusMessage msg = interface->call("powerOff");
    qDebug() << "[PowerManager] [shutdownWidgetClicked]" << msg.errorMessage();
}

void PowerManager::rebootWidgetClicked()
{
    QDBusInterface *interface = new QDBusInterface("org.gnome.SessionManager",
                                                   "/org/gnome/SessionManager",
                                                   "org.gnome.SessionManager",
                                                   QDBusConnection::sessionBus(),
                                                   this);

    QDBusMessage msg = interface->call("reboot");
    qDebug() << "[PowerManager] [rebootWidgetClicked]" << msg.errorMessage();
}

void PowerManager::logoutWidgetCliced()
{
    QDBusInterface *interface = new QDBusInterface("org.gnome.SessionManager",
                                                   "/org/gnome/SessionManager",
                                                   "org.gnome.SessionManager",
                                                   QDBusConnection::sessionBus(),
                                                   this);

    QDBusMessage msg = interface->call("logout");
    qDebug() << "[PowerManager] [logoutWidgetCliced]" << msg.errorMessage();
}

void PowerManager::initUI()
{
    this->setContentsMargins(0,0,0,0);
    QHBoxLayout *main_layout = new QHBoxLayout(this);
    main_layout->setContentsMargins(0,0,0,0);
    main_layout->setSpacing(0);

    lockWidget = new QWidget(this);
    lockWidget->setContentsMargins(0,0,0,0);
    lockWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    QPushButton *lockPB = new QPushButton(lockWidget);
    QLabel *lockLabel = new QLabel(lockWidget);
    lockPB->setProperty("class", "PowerManagerPB");
    lockPB->setIcon(QIcon(QPixmap(":/image/assets/intel/lock.png").scaled(40,40)));
    lockLabel->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
    lockLabel->setText(tr("lock"));
    QVBoxLayout *locklayout = new QVBoxLayout(lockWidget);
    locklayout->setContentsMargins(0,0,0,0);
    locklayout->addWidget(lockPB);
    locklayout->addWidget(lockLabel);
    connect(lockPB, &QPushButton::clicked, this, [=]{
        qDebug() << "PowerManager lock clicked";
        Q_EMIT lock();
    });

    logoutWidget = new QWidget(this);
    logoutWidget->setContentsMargins(0,0,0,0);
    logoutWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    QPushButton *logoutPB = new QPushButton(logoutWidget);
    QLabel *logoutLabel = new QLabel(this);
    logoutPB->setProperty("class", "PowerManagerPB");
    logoutPB->setIcon(QIcon(QPixmap(":/image/assets/intel/logout.png").scaled(40,40)));
    logoutLabel->setText(tr("Log Out"));
    logoutLabel->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
    QVBoxLayout *logoutlayout = new QVBoxLayout(logoutWidget);
    logoutlayout->setContentsMargins(0,0,0,0);
    logoutlayout->addWidget(logoutPB);
    logoutlayout->addWidget(logoutLabel);
    connect(logoutPB, &QPushButton::clicked, this, [=]{
        qDebug() << "PowerManager logout clicked";
        logoutWidgetCliced();;
    });

    rebootWidget = new QWidget(this);
    rebootWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    rebootWidget->setContentsMargins(0,0,0,0);
    QPushButton *rebootPB = new QPushButton(rebootWidget);
    QLabel *rebootLabel = new QLabel(this);
    rebootPB->setProperty("class", "PowerManagerPB");
    rebootPB->setIcon(QIcon(QPixmap(":/image/assets/intel/reboot.png").scaled(40,40)));
    rebootLabel->setText(tr("Restart"));
    rebootLabel->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);

    QVBoxLayout *rebootlayout = new QVBoxLayout(rebootWidget);
    rebootlayout->setContentsMargins(0,0,0,0);
    rebootlayout->addWidget(rebootPB);
    rebootlayout->addWidget(rebootLabel);
    connect(rebootPB, &QPushButton::clicked, this, [=]{
        qDebug() << "PowerManager reboot clicked";
        //rebootWidgetClicked();
        reboot();
    });

    shutdownWidget = new QWidget(this);
    shutdownWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    shutdownWidget->setObjectName("shutdownWidget");
    QPushButton *shutdownPB = new QPushButton(shutdownWidget);
    QLabel *shutdownLabel = new QLabel(shutdownWidget);
    shutdownPB->setProperty("class", "PowerManagerPB");
    shutdownPB->setIcon(QIcon(QPixmap(":/image/assets/intel/shutdown.png").scaled(40,40)));
    shutdownLabel->setText(tr("Power Off"));
    shutdownLabel->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
    QVBoxLayout *shutdownlayout = new QVBoxLayout(shutdownWidget);
    shutdownlayout->setContentsMargins(0,0,0,0);
    shutdownlayout->addWidget(shutdownPB);
    shutdownlayout->addWidget(shutdownLabel);
    connect(shutdownPB, &QPushButton::clicked, this, [=]{
        qDebug() << "PowerManager shutdown clicked";
        //shutdownWidgetClicked();
        powerOff();
    });

    main_layout->addWidget(lockWidget);
    main_layout->addSpacing(ITEM_SPACING);
    main_layout->addWidget(logoutWidget);
    main_layout->addSpacing(ITEM_SPACING);
    main_layout->addWidget(rebootWidget);
    main_layout->addSpacing(ITEM_SPACING);
    main_layout->addWidget(shutdownWidget);
    adjustSize();
}

//息屏，休眠状态Hibernate/挂起Suspend
bool PowerManager::hibernate()
{
    const QString service = "org.freedesktop.login1";
    const QString path = "/org/freedesktop/login1";
    const QString interface = "org.freedesktop.login1.Manager";
    QString command = QLatin1String("Suspend");

    QDBusInterface dbus(service, path, interface, QDBusConnection::systemBus());
    if (!dbus.isValid()) {
        qWarning() << "dbusCall: QDBusInterface is invalid" << service<< path
                   << interface << "Suspend";
        return false;
    }
    QDBusMessage msg = dbus.call(command, QVariant(true));

    if (!msg.errorName().isEmpty()) {
        qWarning() << "Debus error: " << msg;
    }

    if (msg.arguments().isEmpty() || msg.arguments().constFirst().isNull())
        return true;

    QString response = msg.arguments().constFirst().toString();
    qDebug() << "systemd:" << QLatin1String("Suspend") << "=" << response;
    return response == QLatin1String("yes") || response == QLatin1String("challenge");
}

bool PowerManager::reboot()
{
    const QString service = "org.freedesktop.login1";
    const QString path = "/org/freedesktop/login1";
    const QString interface = "org.freedesktop.login1.Manager";
    QString command = QLatin1String("Reboot");

    QDBusInterface dbus(service, path, interface, QDBusConnection::systemBus());
    if (!dbus.isValid()) {
        qWarning() << "dbusCall: QDBusInterface is invalid" << service<< path
                   << interface << "Reboot";
        return false;
    }
    QDBusMessage msg = dbus.call(command, QVariant(true));

    if (!msg.errorName().isEmpty()) {
        qWarning() << "Debus error: " << msg;
    }

    if (msg.arguments().isEmpty() || msg.arguments().constFirst().isNull())
        return true;

    QString response = msg.arguments().constFirst().toString();
    qDebug() << "systemd:" << QLatin1String("Reboot") << "=" << response;
    return response == QLatin1String("yes") || response == QLatin1String("challenge");
}

bool PowerManager::powerOff()
{
    const QString service = "org.freedesktop.login1";
    const QString path = "/org/freedesktop/login1";
    const QString interface = "org.freedesktop.login1.Manager";
    QString command = QLatin1String("PowerOff");

    QDBusInterface dbus(service, path, interface, QDBusConnection::systemBus());
    if (!dbus.isValid()) {
        qWarning() << "dbusCall: QDBusInterface is invalid" << service<< path
                   << interface << "PowerOff";
        return false;
    }
    QDBusMessage msg = dbus.call(command, QVariant(true));

    if (!msg.errorName().isEmpty()) {
        qWarning() << "Debus error: " << msg;
    }

    if (msg.arguments().isEmpty() || msg.arguments().constFirst().isNull())
        return true;

    QString response = msg.arguments().constFirst().toString();
    qDebug() << "systemd:" << QLatin1String("PowerOff") << "=" << response;
    return response == QLatin1String("yes") || response == QLatin1String("challenge");
}

void PowerManager::setQSS()
{
    //设置电源管理按键样式
    QString style_sheet = ".PowerManagerPB{"
                          "background:rgba(255,255,255,38);"
                          "min-width: 128px;"
                          "max-width: 128px;"
                          "min-height: 128px;"
                          "max-height: 128px;"
                          "border-radius: 64px;"
                          "icon-size: 40px;"
                          "font-family: NotoSansCJKsc-Regular, NotoSansCJKsc;"
                          "}"
                          ".PowerManagerPB:hover{"
                          "background:rgba(255,255,255,89);"
                          "}"
                          ".PowerManagerPB:pressed{"
                          "background:rgba(255,255,255,12);"
                          "}";
    setStyleSheet(style_sheet);
}
#else

void PowerManager::doEvent(int type)
{
    switch (type) {
    case SLEEP:
        loginInterface->call("Suspend",true);
        emit lock();
        break;
    case HIBERNATE:
        loginInterface->call("Hibernate",true);
        emit lock();
        break;
    case REBOOT:
        if (is_hibernate && loginedNum > 1) {
            Q_EMIT mulUsersLogined(REBOOT);
            is_hibernate = false;
        } else
            loginInterface->call("Reboot", true);
        break;
    case SHUTDOWN:
        if (is_hibernate && loginedNum > 1) {
            Q_EMIT mulUsersLogined(SHUTDOWN);
            is_hibernate = false;
        } else
            loginInterface->call("PowerOff", true);
        break;
    default:
        break;
    }
}

void PowerManager::setStartupMode(bool mode)
{
    m_isStartupMode = mode;
}

void PowerManager::suspendWidgetClicked()
{
    QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("sleep");
    if (!res.isEmpty()) {
        Q_EMIT showInhibitWarning(res, SLEEP);
        return;
    }
    emit lock();
    Q_EMIT clickedSuspend();
    loginInterface->call("Suspend",true);
}

void PowerManager::hibernateWidgetClicked()
{
    QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("sleep");
    if (!res.isEmpty()) {
        Q_EMIT showInhibitWarning(res, HIBERNATE);
        return;
    }
    emit lock();
    loginInterface->call("Hibernate",true);
}

void PowerManager::shutdownWidgetClicked()
{
    QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("shutdown");
    if (!res.isEmpty()) {
        Q_EMIT showInhibitWarning(res, SHUTDOWN);
        is_hibernate = true;
        return;
    } else if(loginedNum > 1){
        Q_EMIT mulUsersLogined(SHUTDOWN);
        is_hibernate = false;
        return;
    }
    if (m_isStartupMode) {
        loginInterface->call("PowerOff", true);
    } else {
        sessionInterface->call("powerOff");
    }
}

void PowerManager::rebootWidgetClicked()
{
    QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("shutdown");
    if (!res.isEmpty()) {
        Q_EMIT showInhibitWarning(res, REBOOT);
        is_hibernate = true;
        return;
    } else if(loginedNum > 1){
        Q_EMIT mulUsersLogined(REBOOT);
        is_hibernate = false;
        return;
    }
    if (m_isStartupMode) {
        loginInterface->call("Reboot", true);
    } else {
        sessionInterface->call("reboot");
    }
}

void PowerManager::logoutWidgetCliced()
{
    sessionInterface->call("logout");
}

void PowerManager::showSmallSize()
{
    for(int i = 0;i<count();i++){
        QListWidgetItem *item = this->item(i);
        item->setSizeHint(QSize(ITEM_WIDTH*0.8,ITEM_HEIGHT));
        itemWidget(item)->setFixedSize(ITEM_WIDTH*0.8,ITEM_HEIGHT);
    }
    resize(ITEM_WIDTH*this->count()*0.8,ITEM_HEIGHT);
}

void PowerManager::showNormalSize()
{
    for(int i = 0;i<count();i++){
        QListWidgetItem *item = this->item(i);
        item->setSizeHint(QSize(ITEM_WIDTH,ITEM_HEIGHT));
        itemWidget(item)->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    }
  //  resize(ITEM_WIDTH*this->count(),ITEM_HEIGHT);
   // resize((ITEM_WIDTH+ITEM_SPACING)*this->count(), ITEM_HEIGHT+ITEM_SPACING);
    resize((ITEM_WIDTH+ITEM_SPACING*2)*this->count()-ITEM_SPACING*2, ITEM_HEIGHT+ITEM_SPACING*2);
}

void PowerManager::initUI()
{
    setSpacing(30);

//    actService = new QDBusInterface("org.freedesktop.Accounts",
//                                    "/org/freedesktop/Accounts",
//                                    "org.freedesktop.Accounts",
//                                    QDBusConnection::systemBus());

//    QDBusMessage ret = actService->call("ListCachedUsers");
//    QList<QVariant> outArgs = ret.arguments();
//    QVariant first = outArgs.at(0);
//    const QDBusArgument &dbusArgs = first.value<QDBusArgument>();
//    dbusArgs.beginArray();
//    QDBusObjectPath path;
//    int userCount =0;
//    while (!dbusArgs.atEnd())
//    {
//        userCount++;
//        dbusArgs >> path;
//    }
//    dbusArgs.endArray();
//    switchWidget=nullptr;
//    if(userCount>1){
//        switchWidget = new QWidget(this);
//        switchWidget->setObjectName("switchWidget");
//        QLabel *switchFace = new QLabel(switchWidget);
//        QLabel *switchLabel =  new QLabel(switchWidget);
//        switchFace->setAlignment(Qt::AlignCenter);
//        switchFace->setObjectName("switchFace");
//        switchFace->installEventFilter(this);
//        switchLabel->setAlignment(Qt::AlignCenter);
//        switchFace->setPixmap(QPixmap(":/image/assets/switchGreeter.png").scaled(58,58));
//        switchLabel->setText(tr("Switch User"));
//        switchWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
//        QVBoxLayout *switchlayout = new QVBoxLayout(switchWidget);
//        switchlayout->addWidget(switchFace);
//        switchlayout->addWidget(switchLabel);
//       // switchWidget->installEventFilter(this);
//    }
//    logoutWidget = new QWidget(this);
//    logoutWidget->setObjectName("logoutWidget");
//    QLabel *logoutFace = new QLabel(this);
//    QLabel *logoutLabel = new QLabel(this);
//    logoutFace->setAlignment(Qt::AlignCenter);
//    logoutFace->setObjectName("logoutFace");
//    logoutFace->installEventFilter(this);
//    logoutLabel->setAlignment(Qt::AlignCenter);
//    logoutFace->setPixmap(QPixmap(":/image/assets/logout.png").scaled(48,48));
//    logoutLabel->setText(tr("Log Out"));
//    logoutWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
//    QVBoxLayout *logoutlayout = new QVBoxLayout(logoutWidget);
//    logoutlayout->addWidget(logoutFace);
//    logoutlayout->addWidget(logoutLabel);
    //logoutWidget->installEventFilter(this);

    rebootWidget = new QWidget(this);
    rebootWidget->setObjectName("rebootWidget");
    QWidget *rbLabelWidget = new QWidget(this);
    rbLabelWidget->setFixedSize(130, 130);
    rebootFace = new QPushButton(rbLabelWidget);
    rebootFace->setObjectName("rebootFace");
    KLabel *rebootLabel = new KLabel(this);
    //rebootFace->setAlignment(Qt::AlignCenter);
    rebootFace->installEventFilter(this);
    rebootLabel->setAlignment(Qt::AlignCenter);
    rebootFace->setIcon(QIcon(":/image/assets/reboot.svg"));
    rebootFace->setIconSize(QSize(48, 48));
    rebootLabel->setFontSize(16);
    rebootLabel->setText(tr("Restart"));
    rebootWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    QVBoxLayout *rebootlayout = new QVBoxLayout(rebootWidget);
    rebootlayout->addWidget(rbLabelWidget);
    rebootlayout->addWidget(rebootLabel);
    //rebootWidget->installEventFilter(this);

    shutdownWidget = new QWidget(this);
    shutdownWidget->setObjectName("shutdownWidget");
    QWidget *shLabelWidget = new QWidget(this);
    shLabelWidget->setFixedSize(130, 130);
    shutdownFace  = new QPushButton(shLabelWidget);
    shutdownFace->setObjectName("shutdownFace");
    KLabel *shutdownLabel = new KLabel(this);
    shutdownLabel->setAlignment(Qt::AlignCenter);
    //shutdownFace->setAlignment(Qt::AlignCenter);
    shutdownFace->installEventFilter(this);
    shutdownFace->setIcon(QIcon(":/image/assets/shutdown.svg"));
    shutdownFace->setIconSize(QSize(48, 48));
    //sysFont.setPointSize(20);
    shutdownLabel->setFontSize(16);
    shutdownLabel->setText(tr("Shut Down"));
    shutdownWidget->setFixedSize(ITEM_WIDTH, ITEM_HEIGHT);
    QVBoxLayout *shutdownlayout = new QVBoxLayout(shutdownWidget);
    shutdownlayout->addWidget(shLabelWidget);
    shutdownlayout->addWidget(shutdownLabel);
    //shutdownWidget->installEventFilter(this);

//    if(userCount>1){
//        QListWidgetItem *item1 = new QListWidgetItem();
//        item1->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
//        insertItem(this->count(), item1);
//        setItemWidget(item1, switchWidget);
//    }
    // hibernateWidget = nullptr;
    // if(canHibernate){
    //     hibernateWidget = new QWidget(this);
    //     hibernateWidget->setObjectName("hibernateWidget");
    //     QWidget *hbLabelWidget = new QWidget(this);
    //     hbLabelWidget->setFixedSize(130, 130);
    //     hibernateFace  = new QPushButton(hbLabelWidget);
    //     hibernateFace->setObjectName("hibernateFace");
    //     KLabel *hibernateLabel = new KLabel(this);
    //     hibernateLabel->setAlignment(Qt::AlignCenter);
    //     //hibernateFace->setAlignment(Qt::AlignCenter);
    //     hibernateFace->installEventFilter(this);
    //     hibernateFace->setIcon(QIcon(":/image/assets/hibernate.svg"));
    //     hibernateFace->setIconSize(QSize(48, 48));
    //     hibernateLabel->setFontSize(16);
    //     hibernateLabel->setText(tr("Hibernate"));
    //     hibernateWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    //     QVBoxLayout *hibernatelayout = new QVBoxLayout(hibernateWidget);
    //     hibernatelayout->addWidget(hbLabelWidget);
    //     hibernatelayout->addWidget(hibernateLabel);
    //     //hibernateWidget->installEventFilter(this);

    //     QListWidgetItem *item5 = new QListWidgetItem();
    //     item5->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
    //     insertItem(this->count(), item5);
    //     setItemWidget(item5, hibernateWidget);
    //     listLabel.append(hibernateFace);
    // }

    // suspendWidget = nullptr;
    // if(canSuspend){
    //     suspendWidget = new QWidget(this);
    //     suspendWidget->setObjectName("suspendWidget");
    //     QWidget *spLabelWidget = new QWidget(this);
    //     spLabelWidget->setFixedSize(130, 130);
    //     suspendFace  = new QPushButton(spLabelWidget);
    //     suspendFace->setObjectName("suspendFace");
    //     KLabel *suspendLabel = new KLabel(this);
    //     suspendLabel->setAlignment(Qt::AlignCenter);
    //     //suspendFace->setAlignment(Qt::AlignCenter);
    //     suspendFace->installEventFilter(this);
    //     suspendFace->setIcon(QIcon(":/image/assets/suspend.svg"));
    //     suspendFace->setIconSize(QSize(48, 48));
    //     suspendLabel->setFontSize(16);
    //     suspendLabel->setText(tr("Suspend"));
    //     suspendWidget->setFixedSize(ITEM_WIDTH,ITEM_HEIGHT);
    //     QVBoxLayout *suspendlayout = new QVBoxLayout(suspendWidget);
    //     suspendlayout->addWidget(spLabelWidget);
    //     suspendlayout->addWidget(suspendLabel);
    //     //suspendWidget->installEventFilter(this);

    //     QListWidgetItem *item4 = new QListWidgetItem();
    //     item4->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
    //     insertItem(this->count(), item4);
    //     setItemWidget(item4, suspendWidget);
    //     listLabel.append(suspendFace);
    // }
    
//    QListWidgetItem *item1 = new QListWidgetItem();
//    item1->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
//    insertItem(this->count(), item1);
//    setItemWidget(item1, logoutWidget);

    QListWidgetItem *item2 = new QListWidgetItem();
    item2->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
    insertItem(this->count(), item2);
    setItemWidget(item2, rebootWidget);
    listLabel.append(rebootFace);

    QListWidgetItem *item3 = new QListWidgetItem();
    item3->setSizeHint(QSize(ITEM_WIDTH, ITEM_HEIGHT));
    insertItem(this->count(), item3);
    setItemWidget(item3, shutdownWidget);
    listLabel.append(shutdownFace);
}
#endif
