/*
 * Copyright (C) 2023 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 "accountservicehelper.h"
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusConnectionInterface>
#include <QDebug>

const static QString accountsService = QStringLiteral("org.freedesktop.Accounts");
const static QString accountsPath = QStringLiteral("/org/freedesktop/Accounts");
const static QString accountsInterface = QStringLiteral("org.freedesktop.Accounts");
const static QString propertiesInterface = QStringLiteral("org.freedesktop.DBus.Properties");
const static QString accountsUserInterface = QStringLiteral("org.freedesktop.Accounts.User");

AccountServiceHelper::AccountServiceHelper(QObject *parent)
    : QObject(parent)
{
    m_accountsInterface = new QDBusInterface(accountsService,
                                           accountsPath,
                                           accountsInterface,
                                           QDBusConnection::systemBus(),
                                           this);
}

AccountServiceHelper::~AccountServiceHelper()
{

}

QString AccountServiceHelper::getUserLanguageByName(QString strUserName)
{
    QString strLanguage = "";
    if(!m_accountsInterface)
        return strLanguage;
    QDBusReply<QDBusObjectPath> userPath = m_accountsInterface->call("FindUserByName", strUserName);
    if(!userPath.isValid())
        qWarning() << "Get UserPath error:" << userPath.error();
    else {
        QDBusInterface userIface(accountsService, userPath.value().path(),
                                 propertiesInterface, QDBusConnection::systemBus());
        QDBusReply<QDBusVariant> languageReply = userIface.call("Get", accountsUserInterface, "Language");
        if(!languageReply.isValid())
            qWarning() << "Get User's language error" << languageReply.error();
        else {
            strLanguage = languageReply.value().variant().toString();
        }
    }
    return strLanguage;
}

QString AccountServiceHelper::getUserBackgroundByName(QString strUserName)
{
    QString strBackground = "";
    if(!m_accountsInterface)
        return strBackground;
    QDBusReply<QDBusObjectPath> userPath = m_accountsInterface->call("FindUserByName", strUserName);
    if(!userPath.isValid())
        qWarning() << "Get UserPath error:" << userPath.error();
    else {
        QDBusInterface userIface(accountsService, userPath.value().path(),
                                 propertiesInterface, QDBusConnection::systemBus());
        QDBusReply<QDBusVariant> backgroundReply = userIface.call("Get", accountsUserInterface, "BackgroundFile");
        if(!backgroundReply.isValid())
            qWarning() << "Get User's background error" << backgroundReply.error();
        else {
            strBackground = backgroundReply.value().variant().toString();
        }
    }
    return strBackground;
}

QString AccountServiceHelper::getUserSessionByName(QString strUserName)
{
    QString strSession = "";
    if(!m_accountsInterface)
        return strSession;
    QDBusReply<QDBusObjectPath> userPath = m_accountsInterface->call("FindUserByName", strUserName);
    if(!userPath.isValid())
        qWarning() << "Get UserPath error:" << userPath.error();
    else {
        QDBusInterface userIface(accountsService, userPath.value().path(),
                                 propertiesInterface, QDBusConnection::systemBus());
        QDBusReply<QDBusVariant> sessionReply = userIface.call("Get", accountsUserInterface, "XSession");
        if(!sessionReply.isValid())
            qWarning() << "Get User's xsession error" << sessionReply.error();
        else {
            strSession = sessionReply.value().variant().toString();
        }
    }
    return strSession;
}

QString AccountServiceHelper::getAccountBackground(uid_t uid)
{
    QString strBackground = "";
    if(!m_accountsInterface)
        return strBackground;
    QDBusReply<QDBusObjectPath> userPath = m_accountsInterface->call("FindUserById", (qint64)uid);
    if(!userPath.isValid())
        qWarning() << "Get UserPath error:" << userPath.error();
    else {
        QDBusInterface userIface(accountsService, userPath.value().path(),
                                 propertiesInterface, QDBusConnection::systemBus());
        QDBusReply<QDBusVariant> backgroundReply = userIface.call("Get", accountsUserInterface, "BackgroundFile");
        if(!backgroundReply.isValid())
            qWarning() << "Get User's BackgroundFile error" << backgroundReply.error();
        else {
            strBackground = backgroundReply.value().variant().toString();
        }
    }
    return strBackground;
}
