/*
    These are the private helper classes of the Smb4KGlobal namespace.

    SPDX-FileCopyrightText: 2007-2026 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

// application specific includes
#include "smb4kglobal_p.h"
#include "smb4ksettings.h"

// Samba includes
#include <libsmbclient.h>

// Qt includes
#include <QCoreApplication>

Smb4KGlobalPrivate::Smb4KGlobalPrivate()
{
    onlyForeignShares = false;

    //
    // Create and init the SMB context and read the NetBIOS and
    // workgroup name of this machine.
    //
    SMBCCTX *smbContext = smbc_new_context();

    if (smbContext) {
        smbContext = smbc_init_context(smbContext);

        if (!smbContext) {
            smbc_free_context(smbContext, 1);
        } else {
            machineNetbiosName = QString::fromUtf8(smbc_getNetbiosName(smbContext), -1).toUpper();
            machineWorkgroupName = QString::fromUtf8(smbc_getWorkgroup(smbContext), -1).toUpper();
        }
    }

    //
    // Free the SMB context
    //
    smbc_free_context(smbContext, 1);

    //
    // Connections
    //
    connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(slotAboutToQuit()));
}

Smb4KGlobalPrivate::~Smb4KGlobalPrivate()
{
    //
    // Clear the workgroup list
    //
    while (!workgroupsList.isEmpty()) {
        workgroupsList.takeFirst().clear();
    }

    //
    // Clear the host list
    //
    while (!hostsList.isEmpty()) {
        hostsList.takeFirst().clear();
    }

    //
    // Clear the list of mounted shares
    //
    while (!mountedSharesList.isEmpty()) {
        mountedSharesList.takeFirst().clear();
    }

    //
    // Clear the list of shares
    //
    while (!sharesList.isEmpty()) {
        sharesList.takeFirst().clear();
    }
}

void Smb4KGlobalPrivate::slotAboutToQuit()
{
    Smb4KSettings::self()->save();
}
