23 #include <tqstringlist.h>
25 #include "domainbrowser.h"
26 #include "responder.h"
28 #include "servicebrowser.h"
30 #include <avahi-client/client.h>
39 class ServiceBrowserPrivate
42 ServiceBrowserPrivate() : m_running(false)
44 TQValueList<RemoteService::Ptr> m_services;
45 TQValueList<RemoteService::Ptr> m_duringResolve;
47 DomainBrowser* m_domains;
51 TQDict<Query> resolvers;
56 if (domains) init(type,domains,autoResolve ? AutoResolve : 0);
57 else init(type,
new DomainBrowser(
this),autoResolve ? AutoResolve|AutoDelete : AutoDelete);
61 if (domains) init(types,domains,flags);
65 void ServiceBrowser::init(
const TQStringList& type,
DomainBrowser* domains,
int flags)
67 d =
new ServiceBrowserPrivate();
68 d->resolvers.setAutoDelete(
true);
71 d->m_domains = domains;
72 connect(d->m_domains,TQ_SIGNAL(domainAdded(
const TQString& )),
this,TQ_SLOT(
addDomain(
const TQString& )));
73 connect(d->m_domains,TQ_SIGNAL(domainRemoved(
const TQString& )),
this,
78 init(type,
new DomainBrowser(domain,
false,
this),autoResolve ? AutoResolve|AutoDelete : AutoDelete);
82 init(type,
new DomainBrowser(domain,
false,
this),flags | AutoDelete);
88 AvahiClientState s = Responder::self().state();
90 return (s==AVAHI_CLIENT_FAILURE) ? Stopped : Working;
92 return (s==AVAHI_CLIENT_S_INVALID || s==AVAHI_CLIENT_DISCONNECTED) ? Stopped : Working;
98 ServiceBrowser::~ ServiceBrowser()
100 if (d->m_flags & AutoDelete)
delete d->m_domains;
109 void ServiceBrowser::serviceResolved(
bool success)
111 TQObject* sender_obj =
const_cast<TQObject*
>(sender());
113 disconnect(svr,TQ_SIGNAL(resolved(
bool)),
this,TQ_SLOT(serviceResolved(
bool)));
114 TQValueList<RemoteService::Ptr>::Iterator it = d->m_duringResolve.begin();
115 TQValueList<RemoteService::Ptr>::Iterator itEnd = d->m_duringResolve.end();
116 while ( it!= itEnd && svr!= (*it)) ++it;
119 d->m_services+=(*it);
122 d->m_duringResolve.remove(it);
129 if (d->m_running)
return;
132 if (d->m_domains->isRunning()) {
133 TQStringList::const_iterator itEnd = d->m_domains->domains().end();
134 for ( TQStringList::const_iterator it = d->m_domains->domains().begin(); it != itEnd; ++it )
136 }
else d->m_domains->startBrowse();
139 void ServiceBrowser::gotNewService(RemoteService::Ptr svr)
141 if (findDuplicate(svr)==(d->m_services.end())) {
142 if (d->m_flags & AutoResolve) {
143 connect(svr,TQ_SIGNAL(resolved(
bool )),
this,TQ_SLOT(serviceResolved(
bool )));
144 d->m_duringResolve+=svr;
153 void ServiceBrowser::gotRemoveService(RemoteService::Ptr svr)
155 TQValueList<RemoteService::Ptr>::Iterator it = findDuplicate(svr);
156 if (it!=(d->m_services.end())) {
158 d->m_services.remove(it);
165 while (d->resolvers[domain]) d->resolvers.remove(domain);
166 TQValueList<RemoteService::Ptr>::Iterator it = d->m_services.begin();
167 while (it!=d->m_services.end())
169 if ((*it)->domain().section(
'.',0) == domain.section(
'.',0)) {
171 it = d->m_services.remove(it);
177 if (!d->m_running)
return;
178 if (!(d->resolvers[domain])) {
179 TQStringList::ConstIterator itEnd = d->m_types.end();
180 for (TQStringList::ConstIterator it=d->m_types.begin(); it!=itEnd; ++it) {
182 connect(b,TQ_SIGNAL(
serviceAdded(DNSSD::RemoteService::Ptr)),
this,
183 TQ_SLOT(gotNewService(DNSSD::RemoteService::Ptr)));
184 connect(b,TQ_SIGNAL(
serviceRemoved(DNSSD::RemoteService::Ptr )),
this,
185 TQ_SLOT(gotRemoveService(DNSSD::RemoteService::Ptr)));
186 connect(b,TQ_SIGNAL(
finished()),
this,TQ_SLOT(queryFinished()));
188 d->resolvers.insert(domain,b);
193 void ServiceBrowser::queryFinished()
198 bool ServiceBrowser::allFinished()
200 if (d->m_duringResolve.count())
return false;
202 TQDictIterator<Query> it(d->resolvers);
203 for ( ; it.current(); ++it) all&=(*it)->isFinished();
209 return d->m_services;
212 void ServiceBrowser::virtual_hook(
int,
void*)
215 TQValueList<RemoteService::Ptr>::Iterator ServiceBrowser::findDuplicate(RemoteService::Ptr src)
217 TQValueList<RemoteService::Ptr>::Iterator itEnd = d->m_services.end();
218 for (TQValueList<RemoteService::Ptr>::Iterator it = d->m_services.begin(); it!=itEnd; ++it)
219 if ((src->type()==(*it)->type()) && (src->serviceName()==(*it)->serviceName()) &&
220 (src->domain() == (*it)->domain()))
return it;
227 #include "servicebrowser.moc"
Class used to provide current list of domains for browsing.
This class provides way to search for specified service type in one domain.
virtual void startQuery()
Starts query.
RemoteService class allows to resolve service announced on remote machine.
virtual void startBrowse()
Starts browsing for services.
void serviceAdded(DNSSD::RemoteService::Ptr)
Emitted when new service is discovered (or resolved if autoresolve is set.
const TQValueList< RemoteService::Ptr > & services() const
Returns list of services.
void finished()
Emitted when all services has been reported.
void serviceRemoved(DNSSD::RemoteService::Ptr)
Emitted when service is no longer announced.
void addDomain(const TQString &domain)
Add new domain to browse.
void removeDomain(const TQString &domain)
Remove one domain from list of domains to browse.
const DomainBrowser * browsedDomains() const
Return DomainBrowser containing domains being browsed.
static const TQString AllServices
Special service type to search for all available service types.
ServiceBrowser(const TQStringList &types, DomainBrowser *domains, int flags)
ServiceBrowser constructor.
State
Availability of DNS-SD services.
static const State isAvailable()
Checks availability of DNS-SD services (this also covers publishing).