/*
 * Copyright (C) 2024 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 "greeterconfiginterface.h"
#include <QFile>
#include <QTextStream>
#include <QDBusConnectionInterface>
#include <qdbusconnection.h>
#include <pwd.h>

#define GROUP_FUNCTIONS "Functions"
#define KEY_USER_SWITCH_ENABLE "UserSwitchEnable"
#define KEY_VIRTUAL_KEYBOARD_ENABLE "VirtualKeyBoardEnable"
#define KEY_NETWORK_ENABLE "NetworkEnable"
#define KEY_POWERMANAGER_ENABLE "PowerManagerEnable"
#define KEY_LOCKSCREEN_SHOWDATETIME_ENABLE "LockScreenShowDatetimeEnable"
#define KEY_SCREENSAVER_SHOWDATETIME_ENABLE "ScreensaverShowDatetimeEnable"
#define KEY_LOGIN_DISPLAYED_USER_COUNT "LoginDisplayedUserCount"
#define KEY_SELECT_MANUAL_LOGIN_DEFAULT "SelectManualLoginDefault"
#define GROUP_GREETER "Greeter"

GreeterConfigInterface::GreeterConfigInterface(QObject *parent) : QObject(parent)
{
    m_config = new GreeterConfig(this);
    connect(m_config, &GreeterConfig::changed, this, &GreeterConfigInterface::changed);
    connect(m_config, &GreeterConfig::changedForUser, this, &GreeterConfigInterface::userConfigChanged);
    connect(m_config, &GreeterConfig::changed, this, &GreeterConfigInterface::onChanged);
    connect(m_config, &GreeterConfig::changedForUser, this, &GreeterConfigInterface::onChangedForUser);
}

void GreeterConfigInterface::onChanged(const QString &group, const QString &key)
{
    if (group == GROUP_FUNCTIONS) {
        if (key == KEY_VIRTUAL_KEYBOARD_ENABLE) {
            Q_EMIT virtualKeyboardEnableChanged(getVirtualKeyboardEnable());
        } else if (key == KEY_NETWORK_ENABLE) {
            Q_EMIT networkEnableChanged(getNetworkEnable());
        } else if (key == KEY_USER_SWITCH_ENABLE) {
            Q_EMIT userSwitchEnableChanged(getUserSwitchEnable());
        } else if (key == KEY_POWERMANAGER_ENABLE) {
            Q_EMIT powerManagerEnableChanged(getPowerManagerEnable());
        } else if (key == KEY_LOGIN_DISPLAYED_USER_COUNT) {
            Q_EMIT loginDisplayedUserCountChanged(getLoginDisplayedUserCount());
        }
    }
}

void GreeterConfigInterface::onChangedForUser(const QString &group, const QString &key, const QString &strUserName)
{
    if (group == GROUP_FUNCTIONS) {
        if (key == KEY_LOCKSCREEN_SHOWDATETIME_ENABLE) {
            Q_EMIT lockScreenDatetimeEnableChanged(getLockScreenDatetimeEnable(strUserName), strUserName);
        } else if (key == KEY_SCREENSAVER_SHOWDATETIME_ENABLE) {
            Q_EMIT screensaverDatetimeChanged(getScreensaverDatetimeEnable(strUserName), strUserName);
        }
    }
}

void GreeterConfigInterface::setLoginDisplayedUserCount(int count)
{
    if (!isAllowedCaller()) {
        return;
    }
    qDebug() << "setLoginDisplayedUserCount:" << count;
    m_config->setValue(GROUP_FUNCTIONS, KEY_LOGIN_DISPLAYED_USER_COUNT, count);
}

int GreeterConfigInterface::getLoginDisplayedUserCount()
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_LOGIN_DISPLAYED_USER_COUNT, -1).toInt();
}

bool GreeterConfigInterface::resetLoginDisplayedUserCount()
{
    if (!isAllowedCaller()) {
        return false;
    }
    qDebug() << "resetLoginDisplayedUserCount!";
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_LOGIN_DISPLAYED_USER_COUNT);
}

void GreeterConfigInterface::setVirtualKeyboardEnable(bool isEnable)
{
    if (!isAllowedCaller()) {
        return;
    }
    qDebug() << "setVirtualKeyboardEnable:" << isEnable;
    m_config->setValue(GROUP_FUNCTIONS, KEY_VIRTUAL_KEYBOARD_ENABLE, isEnable);
}

bool GreeterConfigInterface::getVirtualKeyboardEnable()
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_VIRTUAL_KEYBOARD_ENABLE, true).toBool();
}

bool GreeterConfigInterface::resetVirtualKeyboardEnable()
{
    if (!isAllowedCaller()) {
        return false;
    }
    qDebug() << "resetVirtualKeyboardEnable!";
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_VIRTUAL_KEYBOARD_ENABLE);
}

void GreeterConfigInterface::setUserSwitchEnable(bool isEnable)
{
    if (!isAllowedCaller()) {
        return;
    }
    qDebug() << "setUserSwitchEnable:" << isEnable;
    m_config->setValue(GROUP_FUNCTIONS, KEY_USER_SWITCH_ENABLE, isEnable);
}

bool GreeterConfigInterface::getUserSwitchEnable()
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_USER_SWITCH_ENABLE, true).toBool();
}

bool GreeterConfigInterface::resetUserSwitchEnable()
{
    if (!isAllowedCaller()) {
        return false;
    }
    qDebug() << "resetUserSwitchEnable!";
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_USER_SWITCH_ENABLE);
}

void GreeterConfigInterface::setNetworkEnable(bool isEnable)
{
    if (!isAllowedCaller()) {
        return;
    }
    qDebug() << "setNetworkEnable:" << isEnable;
    m_config->setValue(GROUP_FUNCTIONS, KEY_NETWORK_ENABLE, isEnable);
}

bool GreeterConfigInterface::getNetworkEnable()
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_NETWORK_ENABLE, true).toBool();
}

bool GreeterConfigInterface::resetNetworkEnable()
{
    if (!isAllowedCaller()) {
        return false;
    }
    qDebug() << "resetNetworkEnable!";
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_NETWORK_ENABLE);
}

void GreeterConfigInterface::setPowerManagerEnable(bool isEnable)
{
    if (!isAllowedCaller()) {
        return;
    }
    qDebug() << "setPowerManagerEnable:" << isEnable;
    m_config->setValue(GROUP_FUNCTIONS, KEY_POWERMANAGER_ENABLE, isEnable);
}

bool GreeterConfigInterface::getPowerManagerEnable()
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_POWERMANAGER_ENABLE, true).toBool();
}

bool GreeterConfigInterface::resetPowerManagerEnable()
{
    if (!isAllowedCaller()) {
        return false;
    }
    qDebug() << "resetPowerManagerEnable!";
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_POWERMANAGER_ENABLE);
}

// lock screen datetime enable
void GreeterConfigInterface::setLockScreenDatetimeEnable(bool isEnable)
{
    if (!isAllowedCaller()) {
        return;
    }
    QString userName = getUserNameFromCaller();
    qDebug() << "setLockScreenDatetimeEnable:" << isEnable << "," << userName;
    m_config->setValue(GROUP_FUNCTIONS, KEY_LOCKSCREEN_SHOWDATETIME_ENABLE, isEnable, userName);
}

bool GreeterConfigInterface::getLockScreenDatetimeEnable(QString strUserName)
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_LOCKSCREEN_SHOWDATETIME_ENABLE, true, strUserName).toBool();
}

bool GreeterConfigInterface::resetLockScreenDatetimeEnable()
{
    if (!isAllowedCaller()) {
        return false;
    }
    QString userName = getUserNameFromCaller();
    qDebug() << "resetLockScreenDatetimeEnable:" << userName;
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_LOCKSCREEN_SHOWDATETIME_ENABLE, userName);
}

// screensaver datetime enable
void GreeterConfigInterface::setScreensaverDatetimeEnable(bool isEnable)
{
    if (!isAllowedCaller()) {
        return;
    }
    QString userName = getUserNameFromCaller();
    qDebug() << "setScreensaverDatetimeEnable:" << isEnable << "," << userName;
    m_config->setValue(GROUP_FUNCTIONS, KEY_SCREENSAVER_SHOWDATETIME_ENABLE, isEnable, userName);
}

bool GreeterConfigInterface::getScreensaverDatetimeEnable(QString strUserName)
{
    return m_config->getValue(GROUP_FUNCTIONS, KEY_SCREENSAVER_SHOWDATETIME_ENABLE, true, strUserName).toBool();
}

bool GreeterConfigInterface::resetScreensaverDatetimeEnable()
{
    if (!isAllowedCaller()) {
        return false;
    }
    QString userName = getUserNameFromCaller();
    qDebug() << "resetScreensaverDatetimeEnable:" << userName;
    return m_config->resetValue(GROUP_FUNCTIONS, KEY_SCREENSAVER_SHOWDATETIME_ENABLE, userName);
}

bool GreeterConfigInterface::getSelectManualLoginDefault()
{
    return m_config->getValue(GROUP_GREETER, KEY_SELECT_MANUAL_LOGIN_DEFAULT, false).toBool();
}

QString GreeterConfigInterface::printConfig(int nType, QString strUserName)
{
    return m_config->printConfig(nType, strUserName);
}

bool GreeterConfigInterface::checkEnviron(int pid)
{
    QString filePath = QString("/proc/%1/environ").arg(pid);
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "file.open failed" << filePath;
        return false;
    }
    QTextStream in(&file);
    QString data = in.readAll();
    file.close();
    data.replace('\0', '\n');
    QStringList environVars = data.split('\n');
    for (const QString &var : environVars) {
        if (!var.isEmpty()) {
            int pos = var.indexOf('=');
            if (pos != -1) {
                QString key = var.left(pos);
                if (key == "LD_PRELOAD" || key == "LD_LIBRARY_PATH" || key == "LD_AUDIT") {
                    return false;
                }
            }
        }
    }
    return true;
}

bool GreeterConfigInterface::isAllowedCaller()
{
    //    QString clientService = message().service();
    //    QDBusConnectionInterface *intf = connection().interface();
    //    unsigned int pid = intf->servicePid(clientService).value();
    //    /*check caller environ*/
    //    if (!checkEnviron(pid)) {
    //        sendErrorReply(QDBusError::ErrorType::Failed, QString("[%2] dbus method control,env
    //        forbidden").arg((pid))); return false;
    //    }
    //    /*check caller info*/
    //    QFileInfo file(QString("/proc/%1/exe").arg(pid));
    //    QString execPath = "";
    //    if (file.exists()) {
    //        execPath = file.canonicalFilePath();
    //    }
    //    qDebug() << "Caller info(uid,pid,ppath):" << intf->serviceUid(clientService).value() << pid << execPath;
    return true;
}

QString GreeterConfigInterface::getUserNameFromCaller()
{
    QString clientService = message().service();
    QDBusConnectionInterface *intf = connection().interface();
    int uid = intf->serviceUid(clientService).value();
    struct passwd *pwinfo = getpwuid(uid);
    if (pwinfo && pwinfo->pw_name) {
        return QString(pwinfo->pw_name);
    } else {
        return QString("");
    }
}
