| 1 | /* |
| 2 | Copyright (c) 2007, Trenton Schulz |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are met: |
| 6 | |
| 7 | 1. Redistributions of source code must retain the above copyright notice, |
| 8 | this list of conditions and the following disclaimer. |
| 9 | |
| 10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | this list of conditions and the following disclaimer in the documentation |
| 12 | and/or other materials provided with the distribution. |
| 13 | |
| 14 | 3. The name of the author may not be used to endorse or promote products |
| 15 | derived from this software without specific prior written permission. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 20 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "bonjourservicebrowser.h" |
| 30 | |
| 31 | #include <QtCore/QSocketNotifier> |
| 32 | |
| 33 | BonjourServiceBrowser::BonjourServiceBrowser(QObject *parent) |
| 34 | : QObject(parent), dnssref(0), bonjourSocket(0) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | BonjourServiceBrowser::~BonjourServiceBrowser() |
| 39 | { |
| 40 | if (dnssref) { |
| 41 | DNSServiceRefDeallocate(dnssref); |
| 42 | dnssref = 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void BonjourServiceBrowser::browseForServiceType(const QString &serviceType) |
| 47 | { |
| 48 | DNSServiceErrorType err = DNSServiceBrowse(&dnssref, 0, 0, serviceType.toUtf8().constData(), 0, |
| 49 | bonjourBrowseReply, this); |
| 50 | if (err != kDNSServiceErr_NoError) { |
| 51 | emit error(err); |
| 52 | } else { |
| 53 | int sockfd = DNSServiceRefSockFD(dnssref); |
| 54 | if (sockfd == -1) { |
| 55 | emit error(kDNSServiceErr_Invalid); |
| 56 | } else { |
| 57 | bonjourSocket = new QSocketNotifier(sockfd, QSocketNotifier::Read, this); |
| 58 | connect(bonjourSocket, SIGNAL(activated(int)), this, SLOT(bonjourSocketReadyRead())); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void BonjourServiceBrowser::bonjourSocketReadyRead() |
| 64 | { |
| 65 | DNSServiceErrorType err = DNSServiceProcessResult(dnssref); |
| 66 | if (err != kDNSServiceErr_NoError) |
| 67 | emit error(err); |
| 68 | } |
| 69 | |
| 70 | void BonjourServiceBrowser::bonjourBrowseReply(DNSServiceRef , DNSServiceFlags flags, |
| 71 | quint32 , DNSServiceErrorType errorCode, |
| 72 | const char *serviceName, const char *regType, |
| 73 | const char *replyDomain, void *context) |
| 74 | { |
| 75 | BonjourServiceBrowser *serviceBrowser = static_cast<BonjourServiceBrowser *>(context); |
| 76 | if (errorCode != kDNSServiceErr_NoError) { |
| 77 | emit serviceBrowser->error(errorCode); |
| 78 | } else { |
| 79 | BonjourRecord bonjourRecord(serviceName, regType, replyDomain); |
| 80 | if (flags & kDNSServiceFlagsAdd) { |
| 81 | if (!serviceBrowser->bonjourRecords.contains(bonjourRecord)) |
| 82 | serviceBrowser->bonjourRecords.append(bonjourRecord); |
| 83 | } else { |
| 84 | serviceBrowser->bonjourRecords.removeAll(bonjourRecord); |
| 85 | } |
| 86 | if (!(flags & kDNSServiceFlagsMoreComing)) { |
| 87 | emit serviceBrowser->currentBonjourRecordsChanged(serviceBrowser->bonjourRecords); |
| 88 | } |
| 89 | } |
| 90 | } |