Clementine-audio-player-Mac.../src/networkremote/zeroconf.cpp

59 lines
1.2 KiB
C++
Raw Normal View History

#include "zeroconf.h"
#include "config.h"
#ifdef HAVE_DBUS
#include "avahi.h"
#endif
2013-01-16 15:26:35 +01:00
#ifdef Q_OS_DARWIN
#include "bonjour.h"
#endif
2013-01-17 14:11:15 +01:00
#ifdef Q_OS_WIN32
#include "tinysvcmdns.h"
#endif
#include <QTextCodec>
Zeroconf* Zeroconf::sInstance = nullptr;
Zeroconf::~Zeroconf() {}
Zeroconf* Zeroconf::GetZeroconf() {
if (!sInstance) {
#ifdef HAVE_DBUS
sInstance = new Avahi;
#endif // HAVE_DBUS
#ifdef Q_OS_DARWIN
sInstance = new Bonjour;
#endif
#ifdef Q_OS_WIN32
sInstance = new TinySVCMDNS;
#endif
}
return sInstance;
}
QByteArray Zeroconf::TruncateName(const QString& name) {
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
QByteArray truncated_utf8;
for (QChar c : name) {
QByteArray rendered = codec->fromUnicode(&c, 1, nullptr);
if (truncated_utf8.size() + rendered.size() >= 63) {
break;
}
truncated_utf8 += rendered;
}
// NULL-terminate the string.
truncated_utf8.append('\0');
return truncated_utf8;
}
2013-02-22 15:29:00 +01:00
void Zeroconf::Publish(const QString& domain, const QString& type,
const QString& name, quint16 port) {
2013-02-22 15:29:00 +01:00
QByteArray truncated_name = TruncateName(name);
PublishInternal(domain, type, truncated_name, port);
2013-02-22 15:29:00 +01:00
}