//SPDX-FileCopyrightText: 2015 Jolla Ltd. <valerio.valerio@jolla.com>
//SPDX-FileContributor: Andres Gomez
//
//SPDX-License-Identifier: LGPL-2.1-or-later


#include "mpriscontroller_p.h"

#include <QtCore/QtDebug>


/*
 * Implementation of interface class MprisRootInterface
 */

MprisRootInterface::MprisRootInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent)
    : DBusExtendedAbstractInterface(service, path, staticInterfaceName(), connection, parent)
    , m_canQuit(false)
    , m_canRaise(false)
    , m_canSetFullscreen(false)
    , m_fullscreen(false)
    , m_hasTrackList(false)
{
    connect(this, SIGNAL(propertyChanged(QString, QVariant)), this, SLOT(onPropertyChanged(QString, QVariant)));
}

MprisRootInterface::~MprisRootInterface()
{
}

void MprisRootInterface::onPropertyChanged(const QString &propertyName, const QVariant &value)
{
    if (propertyName == QStringLiteral("CanQuit")) {
        bool canQuit = value.toBool();
        if (m_canQuit != canQuit) {
            m_canQuit = canQuit;
            Q_EMIT canQuitChanged(m_canQuit);
        }
    } else if (propertyName == QStringLiteral("CanRaise")) {
        bool canRaise = value.toBool();
        if (m_canRaise != canRaise) {
            m_canRaise = canRaise;
            Q_EMIT canRaiseChanged(m_canRaise);
        }
    } else if (propertyName == QStringLiteral("CanSetFullscreen")) {
        bool canSetFullscreen = value.toBool();
        if (m_canSetFullscreen != canSetFullscreen) {
            m_canSetFullscreen = canSetFullscreen;
            Q_EMIT canSetFullscreenChanged(m_canSetFullscreen);
        }
    } else if (propertyName == QStringLiteral("DesktopEntry")) {
        QString desktopEntry = value.toString();
        if (m_desktopEntry != desktopEntry) {
            m_desktopEntry = desktopEntry;
            Q_EMIT desktopEntryChanged(m_desktopEntry);
        }
    } else if (propertyName == QStringLiteral("Fullscreen")) {
        bool fullscreen = value.toBool();
        if (m_fullscreen != fullscreen) {
            m_fullscreen = fullscreen;
            Q_EMIT fullscreenChanged(m_fullscreen);
        }
    } else if (propertyName == QStringLiteral("HasTrackList")) {
        bool hasTrackList = value.toBool();
        if (m_hasTrackList != hasTrackList) {
            m_hasTrackList = hasTrackList;
            Q_EMIT hasTrackListChanged(m_hasTrackList);
        }
    } else if (propertyName == QStringLiteral("Identity")) {
        QString identity= value.toString();
        if (m_identity != identity) {
            m_identity = identity;
            Q_EMIT identityChanged(m_identity);
        }
    } else if (propertyName == QStringLiteral("SupportedMimeTypes")) {
        QStringList supportedUriSchemes = value.toStringList();
        if (m_supportedUriSchemes != supportedUriSchemes) {
            m_supportedUriSchemes = supportedUriSchemes;
            Q_EMIT supportedMimeTypesChanged(m_supportedUriSchemes);
        }
    } else if (propertyName == QStringLiteral("SupportedUriSchemes")) {
        QStringList supportedMimeTypes = value.toStringList();
        if (m_supportedMimeTypes != supportedMimeTypes) {
            m_supportedMimeTypes = supportedMimeTypes;
            Q_EMIT supportedUriSchemesChanged(m_supportedMimeTypes);
        }
    } else {
        qWarning() << Q_FUNC_INFO
                   << "Received PropertyChanged signal from unknown property: "
                   << propertyName;
    }
}

