/****************************************************************************
**
** Copyright (C) 1992-2004 Trolltech AS. All rights reserved.
**
** This file is part of the network module of the Qt Toolkit.
**
** Licensees holding valid Qt Preview licenses may use this file in
** accordance with the Qt Preview License Agreement provided with the
** Software.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QDNS_P_H
#define QDNS_P_H

#include <qcoreapplication.h>
#include "qdns.h"
#include <qmutex.h>
#include <qobject.h>
#include <qpointer.h>

#if !defined QT_NO_THREAD
#include <qthread.h>
#    define QDnsAgentBase QThread
#else
#    define QDnsAgentBase QObject
#endif

struct QDnsQuery
{
    inline QDnsQuery() : member(0) {}

    inline QDnsQuery(const QString &name, QObject *r, const char *m)
        : hostName(name), receiver(r), member(m) {}

    QString hostName;
    QPointer<QObject> receiver;
    const char *member;
};

class QDnsAgent : public QDnsAgentBase
{
    Q_OBJECT
public:
    inline QDnsAgent()
    {
        connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(cleanup()));
    }

    void run();
    static QDnsHostInfo getHostByName(const QString &hostName);

    inline void addHostName(const QString &name,
                            QObject *receiver, const char *member)
    {
        QMutexLocker locker(&mutex);
        queries << QDnsQuery(name, receiver, member);
    }

public slots:
    inline void cleanup()
    {
        {
            QMutexLocker locker(&mutex);
            queries.clear();
        }
        wait();
    }

signals:
    void resultsReady(QDnsHostInfo);

private:
    QList<QDnsQuery> queries;
    QMutex mutex;
};

class QDnsHostInfoPrivate
{
public:
    inline QDnsHostInfoPrivate()
        : err(QDnsHostInfo::NoError),
          errorStr(QT_TRANSLATE_NOOP("QDnsHostInfo", "Unknown error"))
    {
    }

    QDnsHostInfo::Error err;
    QString errorStr;
    QList<QHostAddress> addrs;
    QString hostName;
};

#endif
