/*
 * This file was generated by qdbusxml2cpp-fix version 0.8
 * Command line was: qdbusxml2cpp-fix -c LangSelector -p generated/com_deepin_daemon_langselector ../xml/com.deepin.daemon.LangSelector.xml
 *
 * qdbusxml2cpp-fix is Copyright (C) 2016 Deepin Technology Co., Ltd.
 *
 * This is an auto-generated file.
 * This file may have been hand-edited. Look for HAND-EDIT comments
 * before re-generating it.
 */

#include "generated/com_deepin_daemon_langselector.h"

/*
 * Implementation of interface class __LangSelector
 */

class __LangSelectorPrivate
{
public:
   __LangSelectorPrivate() = default;

    // begin member variables
    QString CurrentLocale;
    int LocaleState;
    QStringList Locales;

public:
    QMap<QString, QDBusPendingCallWatcher *> m_processingCalls;
    QMap<QString, QList<QVariant>> m_waittingCalls;
};

__LangSelector::__LangSelector(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent)
    : DBusExtendedAbstractInterface(service, path, staticInterfaceName(), connection, parent)
    , d_ptr(new __LangSelectorPrivate)
{
    connect(this, &__LangSelector::propertyChanged, this, &__LangSelector::onPropertyChanged);

    if (QMetaType::type("LocaleList") == QMetaType::UnknownType)
        registerLocaleListMetaType();
}

__LangSelector::~__LangSelector()
{
    qDeleteAll(d_ptr->m_processingCalls.values());
    delete d_ptr;
}

void __LangSelector::onPropertyChanged(const QString &propName, const QVariant &value)
{
    if (propName == QStringLiteral("CurrentLocale"))
    {
        const QString &CurrentLocale = qvariant_cast<QString>(value);
        if (d_ptr->CurrentLocale != CurrentLocale)
        {
            d_ptr->CurrentLocale = CurrentLocale;
            Q_EMIT CurrentLocaleChanged(d_ptr->CurrentLocale);
        }
        return;
    }

    if (propName == QStringLiteral("LocaleState"))
    {
        const int &LocaleState = qvariant_cast<int>(value);
        if (d_ptr->LocaleState != LocaleState)
        {
            d_ptr->LocaleState = LocaleState;
            Q_EMIT LocaleStateChanged(d_ptr->LocaleState);
        }
        return;
    }

    if (propName == QStringLiteral("Locales"))
    {
        const QStringList &Locales = qvariant_cast<QStringList>(value);
        if (d_ptr->Locales != Locales)
        {
            d_ptr->Locales = Locales;
            Q_EMIT LocalesChanged(d_ptr->Locales);
        }
        return;
    }

    qWarning() << "property not handle: " << propName;
    return;
}

QString __LangSelector::currentLocale()
{
    return qvariant_cast<QString>(internalPropGet("CurrentLocale", &d_ptr->CurrentLocale));
}

int __LangSelector::localeState()
{
    return qvariant_cast<int>(internalPropGet("LocaleState", &d_ptr->LocaleState));
}

QStringList __LangSelector::locales()
{
    return qvariant_cast<QStringList>(internalPropGet("Locales", &d_ptr->Locales));
}

void __LangSelector::CallQueued(const QString &callName, const QList<QVariant> &args)
{
    if (d_ptr->m_waittingCalls.contains(callName))
    {
        d_ptr->m_waittingCalls[callName] = args;
        return;
    }
    if (d_ptr->m_processingCalls.contains(callName))
    {
        d_ptr->m_waittingCalls.insert(callName, args);
    } else {
        QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(asyncCallWithArgumentList(callName, args));
        connect(watcher, &QDBusPendingCallWatcher::finished, this, &__LangSelector::onPendingCallFinished);
        d_ptr->m_processingCalls.insert(callName, watcher);
    }
}

void __LangSelector::onPendingCallFinished(QDBusPendingCallWatcher *w)
{
    w->deleteLater();
    const auto callName = d_ptr->m_processingCalls.key(w);
    Q_ASSERT(!callName.isEmpty());
    if (callName.isEmpty())
        return;
    d_ptr->m_processingCalls.remove(callName);
    if (!d_ptr->m_waittingCalls.contains(callName))
        return;
    const auto args = d_ptr->m_waittingCalls.take(callName);
    CallQueued(callName, args);
}
