diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f343a360a..baf885bca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -843,6 +843,7 @@ optional_source(APPLE core/mac_startup.mm core/scoped_nsautorelease_pool.mm devices/macdevicelister.mm + networkremote/bonjour.mm ui/globalshortcutgrabber.mm ui/macscreensaver.cpp ui/macsystemtrayicon.mm diff --git a/src/networkremote/bonjour.h b/src/networkremote/bonjour.h new file mode 100644 index 000000000..d6793475c --- /dev/null +++ b/src/networkremote/bonjour.h @@ -0,0 +1,26 @@ +#ifndef BONJOUR_H +#define BONJOUR_H + +#include "zeroconf.h" + +#ifdef __OBJC__ +@class NetServicePublicationDelegate; +#else +class NetServicePublicationDelegate; +#endif // __OBJC__ + +class Bonjour : public Zeroconf { + public: + Bonjour(); + virtual ~Bonjour(); + virtual void Publish( + const QString& domain, + const QString& type, + const QString& name, + quint16 port); + + private: + NetServicePublicationDelegate* delegate_; +}; + +#endif // BONJOUR_H diff --git a/src/networkremote/bonjour.mm b/src/networkremote/bonjour.mm new file mode 100644 index 000000000..8b8d96441 --- /dev/null +++ b/src/networkremote/bonjour.mm @@ -0,0 +1,65 @@ +#include "bonjour.h" + +#import +#import + +#include "core/logging.h" + +@interface NetServicePublicationDelegate : NSObject { +} + +- (void)netServiceWillPublish:(NSNetService*)netService; +- (void)netService:(NSNetService*)netService + didNotPublish:(NSDictionary*)errorDict; +- (void)netServiceDidStop:(NSNetService*)netService; + +@end + +@implementation NetServicePublicationDelegate + +- (void)netServiceWillPublish: (NSNetService*)netService { + qLog(Debug) << "Publishing:" << [[netService name] UTF8String]; +} + +- (void)netService: (NSNetService*)netServie didNotPublish: (NSDictionary*)errorDict { + qLog(Debug) << "Failed to publish remote service with Bonjour"; + NSLog(@"%@", errorDict); +} + +- (void)netServiceDidStop: (NSNetService*)netService { + qLog(Debug) << "Unpublished:" << [[netService name] UTF8String]; +} + +@end + +namespace { + +NSString* NSStringFromQString(const QString& s) { + return [[NSString alloc] initWithUTF8String: s.toUtf8().constData()]; +} + +} + +Bonjour::Bonjour() + : delegate_([[NetServicePublicationDelegate alloc] init]) { +} + +Bonjour::~Bonjour() { + [delegate_ release]; +} + +void Bonjour::Publish( + const QString& domain, + const QString& type, + const QString& name, + quint16 port) { + NSNetService* service = [[NSNetService alloc] + initWithDomain: NSStringFromQString(domain) + type: NSStringFromQString(type) + name: NSStringFromQString(name) + port: port]; + if (service) { + [service setDelegate: delegate_]; + [service publish]; + } +} diff --git a/src/networkremote/zeroconf.cpp b/src/networkremote/zeroconf.cpp index 9acfab86f..33f3b2f4e 100644 --- a/src/networkremote/zeroconf.cpp +++ b/src/networkremote/zeroconf.cpp @@ -6,6 +6,10 @@ #include "avahi.h" #endif +#ifdef Q_OS_DARWIN +#include "bonjour.h" +#endif + Zeroconf* Zeroconf::sInstance = NULL; Zeroconf::~Zeroconf() { @@ -17,6 +21,9 @@ Zeroconf* Zeroconf::GetZeroconf() { #ifdef HAVE_DBUS sInstance = new Avahi; #endif // HAVE_DBUS + #ifdef Q_OS_DARWIN + sInstance = new Bonjour; + #endif } return sInstance;