/* Copyright (C) 2026 The Qt Company Ltd.
 *
 * SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
*/

#include "archivefactory.h"
#include "libarchivewrapper.h"

#include <QFileInfo>

using namespace QInstaller;

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::ArchiveFactory
    \brief The ArchiveFactory class is used to create archive objects
           based on the suffix of a given filename.

    This class acts as a factory for \c QInstaller::AbstractArchive. You can
    register one or more archive handlers with this factory and create
    registered objects based on the file suffix.

    This class follows the singleton design pattern. Only one instance
    of this class can be created and its reference can be fetched from
    the \c {instance()} method.

    The following archive handler is supported and registered by default:
    \list
        \li LibArchive
    \endlist
*/

/*!
    \fn void QInstaller::ArchiveFactory::registerArchive(const QString &name, const QStringList &types)

    Registers a new archive handler with the factory based on \a name and list
    of supported file suffix \a types.
*/


/*!
    Returns the only instance of this class.
*/
ArchiveFactory &ArchiveFactory::instance()
{
    static ArchiveFactory instance;
    return instance;
}

/*!
    Constructs and returns a pointer to an archive object with \a filename and \a parent.
    If the archive type referenced by \a filename is not registered, a null pointer is
    returned instead.
*/
AbstractArchive *ArchiveFactory::create(const QString &filename, QObject *parent) const
{
    const QString suffix = QFileInfo(filename).completeSuffix();
    QString name;
    for (auto &types : m_supportedTypesHash) {
        QStringList::const_iterator it;
        for (it = types.constBegin(); it != types.constEnd(); ++it) {
            if (suffix.endsWith(*it, Qt::CaseInsensitive)) {
                name = m_supportedTypesHash.key(types);
                break;
            }
        }
    }
    if (name.isEmpty())
        return nullptr;

    AbstractArchive *archive = GenericFactory<AbstractArchive, QString, QString, QObject *>
        ::create(name, filename, parent);

    return archive;
}

/*!
    Returns a list of supported archive types.
*/
QStringList ArchiveFactory::supportedTypes()
{
    QStringList types;
    QHash<QString, QStringList> *const typesHash = &instance().m_supportedTypesHash;
    for (auto &value : *typesHash)
        types.append(value);

    return types;
}

/*!
    Returns \c true if the archive type from \a filename is registered with
    an archive handler.
*/
bool ArchiveFactory::isSupportedType(const QString &filename)
{
    const QString suffix = QFileInfo(filename).completeSuffix();
    QHash<QString, QStringList> *const typesHash = &instance().m_supportedTypesHash;
    for (auto &types : *typesHash) {
        QStringList::const_iterator it;
        for (it = types.constBegin(); it != types.constEnd(); ++it) {
            if (suffix.endsWith(*it, Qt::CaseInsensitive))
                return true;
        }
    }
    return false;
}

/*!
    Private constructor for ArchiveFactory. Registers default archive handlers.
*/
ArchiveFactory::ArchiveFactory()
{
    registerArchive<LibArchiveWrapper>(QLatin1String("LibArchive"), QStringList()
        << QLatin1String("tar") << QLatin1String("tar.gz") << QLatin1String("tar.bz2")
        << QLatin1String("tar.xz") << QLatin1String("zip") << QLatin1String("7z")
        << QLatin1String("qbsp"));
}
