qcp/libqcp/qcp.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

2010-07-24 16:16:44 +00:00
#include "qcp.h"
#define SERVICETYPE "_qcp._tcp"
namespace QCP {
Browse::Browse(QObject *parent)
: QObject(parent), m_av_browse(new Avahi::Browse(SERVICETYPE, this)) {
connect(m_av_browse, SIGNAL(found(Avahi::Service)), this, SLOT(foundService(Avahi::Service)));
connect(m_av_browse, SIGNAL(lost(Avahi::Service)), this, SLOT(lostService(Avahi::Service)));
connect(m_av_browse, SIGNAL(started()), this, SIGNAL(started()));
connect(m_av_browse, SIGNAL(stopped()), this, SIGNAL(stopped()));
}
WatchShare *Browse::watch(const Avahi::Service &service, QObject *parent) {
ShareData *sd = shareData(service);
WatchShare *ws;
if (!sd) return 0;
sd->watcherList.append(ws = new WatchShare(parent, Share(this, service)));
ws->m_list_it = sd->watcherList.end()-1;
return ws;
}
void Browse::removeWatch(WatcherList::iterator it, const Avahi::Service &service) {
ShareData *sd = shareData(service);
if (!sd || WatcherList::iterator() == it) return;
sd->watcherList.erase(it);
}
void Browse::foundService(Avahi::Service s) {
ShareData *sd = new ShareData();
s.setData(sd);
emit found(Share(this, s));
}
void Browse::lostService(Avahi::Service s) {
emit lost(Share(this, s));
ShareData *sd = shareData(s);
if (0 != sd) {
while (!sd->watcherList.empty()) {
WatchShare *ws = sd->watcherList.first();
emit ws->lost();
if (ws == sd->watcherList.first()) {
ws->m_list_it = WatcherList::iterator();
sd->watcherList.pop_front();
}
}
delete sd;
}
sd->data = 0;
}
WatchShare *Share::watch(QObject *parent) {
return m_browse->watch(m_service, parent);
}
}