2013-01-16 14:56:31 +01:00
|
|
|
#include "avahi.h"
|
|
|
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "dbus/avahientrygroup.h"
|
|
|
|
#include "dbus/avahiserver.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AddService(const QString domain, const QString type, const QByteArray name,
|
|
|
|
quint16 port, QDBusPendingReply<QDBusObjectPath> path_reply);
|
2013-01-16 15:48:04 +01:00
|
|
|
void Commit(OrgFreedesktopAvahiEntryGroupInterface* interface);
|
|
|
|
void LogCommit(QDBusPendingReply<> reply);
|
2013-01-16 14:56:31 +01:00
|
|
|
|
2013-01-16 15:48:04 +01:00
|
|
|
} // namespace
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Avahi::PublishInternal(const QString& domain, const QString& type,
|
|
|
|
const QByteArray& name, quint16 port) {
|
2013-01-16 15:48:04 +01:00
|
|
|
OrgFreedesktopAvahiServerInterface server_interface(
|
2014-02-07 16:34:20 +01:00
|
|
|
"org.freedesktop.Avahi", "/", QDBusConnection::systemBus());
|
2013-01-16 15:48:04 +01:00
|
|
|
QDBusPendingReply<QDBusObjectPath> reply = server_interface.EntryGroupNew();
|
2013-01-16 14:56:31 +01:00
|
|
|
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), &AddService,
|
|
|
|
domain, type, name, port, reply);
|
|
|
|
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), watcher,
|
|
|
|
SLOT(deleteLater()));
|
2013-01-16 14:56:31 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 15:48:04 +01:00
|
|
|
namespace {
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AddService(const QString domain, const QString type, const QByteArray name,
|
|
|
|
quint16 port, QDBusPendingReply<QDBusObjectPath> path_reply) {
|
2013-01-16 14:56:31 +01:00
|
|
|
if (path_reply.isError()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Warning) << "Failed to create Avahi entry group:"
|
|
|
|
<< path_reply.error();
|
|
|
|
qLog(Info) << "This might be because 'disable-user-service-publishing'"
|
|
|
|
<< "is set to 'yes' in avahi-daemon.conf";
|
2013-01-16 14:56:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-05-03 06:44:53 +02:00
|
|
|
if (path_reply.isError()) {
|
|
|
|
qLog(Debug) << path_reply.error();
|
|
|
|
}
|
2013-01-16 14:56:31 +01:00
|
|
|
OrgFreedesktopAvahiEntryGroupInterface* entry_group_interface =
|
2014-02-07 16:34:20 +01:00
|
|
|
new OrgFreedesktopAvahiEntryGroupInterface("org.freedesktop.Avahi",
|
|
|
|
path_reply.value().path(),
|
|
|
|
QDBusConnection::systemBus());
|
2013-01-16 14:56:31 +01:00
|
|
|
QDBusPendingReply<> reply = entry_group_interface->AddService(
|
2014-02-07 16:34:20 +01:00
|
|
|
-1, // Interface (all)
|
|
|
|
-1, // Protocol (v4 & v6)
|
|
|
|
0, // Flags
|
2013-02-22 15:29:00 +01:00
|
|
|
// Service name, eg. Clementine
|
|
|
|
QString::fromUtf8(name.constData(), name.size()),
|
2013-01-16 14:56:31 +01:00
|
|
|
type, // Service type, eg. _clementine._tcp
|
|
|
|
domain, // Domain, eg. local
|
2019-11-09 23:45:28 +01:00
|
|
|
QString(), // Hostname (filled in by Avahi)
|
2013-01-16 14:56:31 +01:00
|
|
|
port, // Port our service is running on
|
|
|
|
QList<QByteArray>()); // TXT record
|
|
|
|
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), &Commit,
|
|
|
|
entry_group_interface);
|
2013-01-16 14:56:31 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), watcher,
|
|
|
|
SLOT(deleteLater()));
|
2013-01-16 14:56:31 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 15:48:04 +01:00
|
|
|
void Commit(OrgFreedesktopAvahiEntryGroupInterface* interface) {
|
|
|
|
QDBusPendingReply<> reply = interface->Commit();
|
2013-01-16 14:56:31 +01:00
|
|
|
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply);
|
2014-02-07 16:34:20 +01:00
|
|
|
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), watcher,
|
|
|
|
SLOT(deleteLater()));
|
|
|
|
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
|
|
|
|
interface, SLOT(deleteLater()));
|
|
|
|
NewClosure(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), &LogCommit,
|
|
|
|
reply);
|
2013-01-16 15:48:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogCommit(QDBusPendingReply<> reply) {
|
2021-05-03 06:44:53 +02:00
|
|
|
if (reply.isError()) {
|
|
|
|
qLog(Debug) << "Commit error:" << reply.error();
|
|
|
|
} else {
|
|
|
|
qLog(Debug) << "Remote interface published on Avahi.";
|
|
|
|
}
|
2013-01-16 14:56:31 +01:00
|
|
|
}
|
2013-01-16 15:48:04 +01:00
|
|
|
|
|
|
|
} // namespace
|