qcp/libqcp/qcp.h
2010-07-24 18:17:20 +02:00

108 lines
2.4 KiB
C++

#ifndef QCP_H
#define QCP_H
#include "libqcp_global.h"
#include <QObject>
#include <QLinkedList>
#include "../libqtavahi/qtavahi.h"
namespace QCP {
class LIBQCPSHARED_EXPORT Share;
class LIBQCPSHARED_EXPORT WatchShare;
class LIBQCPSHARED_EXPORT Browse;
}
namespace QCP {
/* The private data of the Avahi::Service instances belongs to QCP ! :) */
class LIBQCPSHARED_EXPORT Browse : public QObject {
Q_OBJECT
private:
friend class QCP::Share;
friend class QCP::WatchShare;
typedef QLinkedList<WatchShare*> WatcherList;
struct ShareData {
WatcherList watcherList;
void *data;
};
public:
Browse(QObject *parent = 0);
Avahi::Browse* avahiBrowse() { return m_av_browse; }
inline bool start() { return m_av_browse->start(); }
inline void stop() { m_av_browse->stop(); }
signals:
void found(QCP::Share s);
void lost(QCP::Share s);
void started();
void stopped();
private slots:
void foundService(Avahi::Service s);
void lostService(Avahi::Service s);
private:
WatchShare *watch(const Avahi::Service &service, QObject *parent);
void removeWatch(WatcherList::iterator it, const Avahi::Service &service);
static inline ShareData* shareData(const Avahi::Service &service) {
return static_cast<ShareData*>(service.data());
}
Avahi::Browse *m_av_browse;
};
class LIBQCPSHARED_EXPORT Share {
private:
friend class QCP::Browse;
friend class QCP::WatchShare;
public:
inline Share(QCP::Browse *browse, Avahi::Service service) : m_browse(browse), m_service(service) { }
public:
Avahi::Service avahiService() const { return m_service; }
WatchShare *watch(QObject *parent = 0);
void* data() const { Browse::ShareData *sd = Browse::shareData(m_service); return sd ? sd->data : 0; }
void setData(void *data) const { Browse::ShareData *sd = Browse::shareData(m_service); if (sd) sd->data = data; }
private:
QCP::Browse *m_browse;
Avahi::Service m_service;
};
class LIBQCPSHARED_EXPORT WatchShare : public QObject {
Q_OBJECT
friend class QCP::Share;
friend class QCP::Browse;
private:
inline WatchShare(QObject *parent, Share share) : QObject(parent), m_share(share) { }
public:
virtual ~WatchShare() {
m_share.m_browse->removeWatch(m_list_it, m_share.m_service);
}
inline Share share() { return m_share; }
signals:
void lost();
private:
Share m_share;
Browse::WatcherList::iterator m_list_it;
};
}
#endif // QCP_H