/*
 * SPDX-FileCopyrightText: 2026 Maciej Sopyło <me@klh.io>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "DesktopPortal.h"
#include "PortalInterface.h"

#include <QPluginLoader>
#include <QDBusAbstractAdaptor>
#include <QDebug>

DesktopPortal::DesktopPortal(QObject *parent) : ObjectProxy(parent)
{
    // Portals are static Qt plugins compiled into this binary. staticInstances() returns every
    // statically-linked plugin; each portal plugin already owns its portal object (and its D-Bus
    // adaptor). Reparent the portal plugins onto us so their adaptors become our children - then
    // registration (and handleMessage/introspect) can find them via findChildren.
    for (QObject *plugin : QPluginLoader::staticInstances()) {
        if (qobject_cast<PortalFactory *>(plugin)) {
            plugin->setParent(this);
            qInfo() << "Loaded portal" << plugin->metaObject()->className();
        }
    }

    registerVirtualObject();
    for (auto adaptor : this->findChildren<QDBusAbstractAdaptor *>()) {
        registerAdaptor(adaptor);
    }
}
