qcp/libqtavahi/qtavahi.h
2010-07-25 13:09:05 +02:00

194 lines
5.8 KiB
C++

#ifndef QTAVAHI_H
#define QTAVAHI_H
#include <QObject>
#include "libqtavahi_global.h"
namespace Avahi {
class LIBQTAVAHISHARED_EXPORT Service;
class LIBQTAVAHISHARED_EXPORT ServiceKey;
class LIBQTAVAHISHARED_EXPORT Browse;
class LIBQTAVAHISHARED_EXPORT Announce;
}
/* we need this before #include <QHash> so it finds our qHash function */
inline uint qHash(const Avahi::ServiceKey& k);
#include <QHostAddress>
#include <QAbstractItemModel>
#include <QAtomicInt>
#include <QString>
#include <QHash>
#include "qtavahi_p.h"
namespace Avahi {
typedef Avahi::priv::ServiceWatch ServiceWatch;
class LIBQTAVAHISHARED_EXPORT Service {
public:
inline Service() : m_priv(0) { }
inline Service(QString name, QString hostname, QString domain, QHostAddress address, int interface, int protocol, quint16 port, QHash<QString, QString> txt)
: m_priv(new Avahi::priv::ServiceP(name, hostname, domain, address, interface, protocol, port, txt)) { }
inline Service(const Service &s) : m_priv(0) { assign(s.m_priv); }
inline Service &operator=(const Service &s) { assign(s.m_priv); return *this; }
~Service() { assign(0); }
inline QString name() const { return m_priv ? m_priv->m_name : QString(); }
inline QString hostname() const { return m_priv ? m_priv->m_hostname : QString(); }
inline QString domain() const { return m_priv ? m_priv->m_domain : QString(); }
inline QHostAddress address() const { return m_priv ? m_priv->m_address : QHostAddress(); }
inline quint16 port() const { return m_priv ? m_priv->m_port : 0; }
inline QHash<QString, QString> txt() const { return m_priv ? m_priv->m_txt : QHash<QString, QString>(); }
inline QString txt(QString key) const { return m_priv ? m_priv->m_txt.value(key) : QString(); }
inline bool valid() const { return 0 != m_priv; }
/* save custom data for a service (the service is probably associated with a Browse instance, so all
code using the same Browse instance uses the same private data here */
inline void* data() const { return m_priv ? m_priv->m_data : 0; }
inline void setData(void *data) const { if (m_priv) m_priv->m_data = data; }
inline Avahi::ServiceWatch* watcher() const { return m_priv ? m_priv->watcher() : 0; }
private:
friend class Avahi::ServiceKey;
friend class Avahi::Browse;
inline void assign(Avahi::priv::ServiceP *p) {
if (p == m_priv) return;
if (p) p->ref();
if (m_priv) m_priv->deref();
m_priv = p;
}
Avahi::priv::ServiceP *m_priv;
};
class LIBQTAVAHISHARED_EXPORT ServiceKey {
public:
inline ServiceKey(int interface, int protocol, const char *name, const char *domain)
: interface(interface), protocol(protocol), name(QString::fromUtf8(name)), domain(QString::fromUtf8(domain)) {
}
inline ServiceKey(int interface, int protocol, QString name, QString domain)
: interface(interface), protocol(protocol), name(name), domain(domain) {
}
inline ServiceKey(const Service &s)
: interface(-1), protocol(-1), name(), domain() {
if (s.m_priv) {
interface = s.m_priv->m_interface;
protocol = s.m_priv->m_protocol;
name = s.m_priv->m_name;
domain = s.m_priv->m_domain;
}
}
int interface; /* AvahiIfIndex */
int protocol; /* AvahiProtocol */
QString name;
QString domain;
};
inline bool operator==(const ServiceKey &a, const ServiceKey &b) {
return a.interface == b.interface && a.protocol == b.protocol && a.name == b.name && a.domain == b.domain;
}
inline bool operator<(const ServiceKey &a, const ServiceKey &b) {
if (a.interface < b.interface) return true;
if (a.interface > b.interface) return false;
if (a.protocol < b.protocol) return true;
if (a.protocol > b.protocol) return false;
if (a.name < b.name) return true;
if (a.name > b.name) return false;
return a.domain < b.domain;
}
inline bool operator>(const ServiceKey &a, const ServiceKey &b) { return b < a; }
inline bool operator<=(const ServiceKey &a, const ServiceKey &b) { return !(b < a); }
inline bool operator>=(const ServiceKey &a, const ServiceKey &b) { return !(a < b); }
class LIBQTAVAHISHARED_EXPORT Browse : public QObject {
Q_OBJECT
private:
Q_DISABLE_COPY(Browse);
public:
typedef Avahi::priv::Protocol Protocol;
inline Browse(QString servtype, Protocol prot, QObject *parent = 0) {
Browse(Avahi::priv::serv_prot2str(servtype, prot), parent);
}
Browse(QString serviceType, QObject *parent = 0);
virtual ~Browse();
bool start();
void stop();
QString serviceType() const;
QList<Service> currentList() const;
private:
void clearList();
void foundService(Avahi::Service s);
void lostService(Avahi::Service s);
signals:
void found(Avahi::Service s);
void lost(Avahi::Service s);
void started();
void stopped();
private:
Avahi::priv::BrowseP *m_priv;
friend class Avahi::priv::BrowseP;
};
class LIBQTAVAHISHARED_EXPORT Announce : public QObject {
Q_OBJECT
private:
Q_DISABLE_COPY(Announce);
public:
typedef Avahi::priv::Protocol Protocol;
inline Announce(QString servtype, Protocol prot, QString name, quint16 port, QHash<QString,QString> txt, QObject *parent = 0) {
Announce(Avahi::priv::serv_prot2str(servtype, prot), name, port, txt, parent);
}
Announce(QString serviceType, QString name, quint16 port, QHash<QString,QString> txt, QObject *parent = 0);
virtual ~Announce();
QString serviceType() const;
QString name() const;
quint16 port() const;
QHash<QString, QString> txt() const;
QString txt(QString key) const;
bool start();
void stop();
signals:
void established(); /* the name may have been changed after collisions */
void collision();
void started();
void stopped();
private:
Avahi::priv::AnnounceP *m_priv;
friend class Avahi::priv::AnnounceP;
};
}
QT_BEGIN_NAMESPACE
inline uint qHash(const Avahi::ServiceKey& k) {
return qHash(k.interface ^ k.protocol) ^ qHash(k.name) ^ qHash(k.domain);
}
QT_END_NAMESPACE
#endif // QTAVAHI_H