#include "appmanager.h"
#include <QDBusError>
#include <QApplication>

AppManager::AppManager(QObject *parent)
    : QObject(parent)
    , m_appManagerInterface(new AppManagerInterface("com.kylin.AppManager", "/com/kylin/AppManager", QDBusConnection::sessionBus()))
{
    if (!m_appManagerInterface->isValid()) {
        qWarning() << "Connect to com.kylin.AppManager dbus service failed!" << m_appManagerInterface->lastError();
    }
}

void AppManager::runApplication(const QString &desktopFile)
{
    auto reply = m_appManagerInterface->LaunchApp(desktopFile);
    while (!reply.isFinished()) {
        qApp->processEvents();
    }
    if (reply.isError()) {
        qWarning() << "Run Application failed!" << desktopFile << reply.error();
    } else {
        if (!reply.value()) {
            qWarning() << "Run Application failed!" << desktopFile;
        }
    }
}

QList<quint32> AppManager::runningApplications()
{
    QList<quint32> runningAppWids;
    return runningAppWids;
}

void AppManager::runApplicationConfig(const QString &desktopFile, const QStringList &Config)
{
    bool retConfig = m_appManagerInterface->LaunchAppWithArguments(desktopFile, Config);
    qDebug()<<"===retConfig"<<retConfig;
    if (!retConfig) {
        qWarning() << "Run ApplicationConfig failed!" << desktopFile;
    }
}

QString AppManager::AppDesktopFileNameByPid(qlonglong pid)
{
    return m_appManagerInterface->AppDesktopFileNameByPid(pid);
}

QStringList AppManager::getAppWhiteList()
{
    return m_appManagerInterface->AppWhiteList("AppUninstall");
}
