File indexing completed on 2024-04-28 15:22:07

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2004, 2005 Jakub Stachowski <qbast@go2.pl>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KDNSSDPUBLICSERVICE_H
0010 #define KDNSSDPUBLICSERVICE_H
0011 
0012 #include "servicebase.h"
0013 #include <QObject>
0014 #include <QStringList>
0015 
0016 namespace KDNSSD
0017 {
0018 class PublicServicePrivate;
0019 
0020 /**
0021  * @class PublicService publicservice.h KDNSSD/PublicService
0022  * @short Represents a service to be published
0023  *
0024  * This class allows you to publish the existence of a network
0025  * service provided by your application.
0026  *
0027  * If you are providing a web server and want to advertise it
0028  * on the local network, you might do
0029  * @code
0030  * KDNSSD::PublicService *service = new KDNSSD::PublicService("My files", "_http._tcp", 80);
0031  * bool isOK = service->publish();
0032  * @endcode
0033  *
0034  * In this example publish() is synchronous: it will not return
0035  * until publishing is complete.  This is usually not too long
0036  * but it can freeze an application's GUI for a moment.
0037  * To publish asynchronously instead, do:
0038  * @code
0039  * KDNSSD::PublicService *service = new KDNSSD::PublicService("My files", "_http._tcp", 80);
0040  * connect(service, SIGNAL(published(bool)), this, SLOT(wasPublished(bool)));
0041  * service->publishAsync();
0042  * @endcode
0043  *
0044  * @author Jakub Stachowski
0045  */
0046 
0047 class KDNSSD_EXPORT PublicService : public QObject, public ServiceBase
0048 {
0049     Q_OBJECT
0050 
0051 public:
0052     /**
0053      * Creates a service description that can be published
0054      *
0055      * If no @p name is given, the computer name is used instead.  If there
0056      * is already a service with the same name, type and domain a number will
0057      * be appended to the name to make it unique.
0058      *
0059      * If no @p domain is specified, the service is published on the link-local
0060      * domain (.local).
0061      *
0062      * The subtypes can be used to specify server attributes, such
0063      * as "_anon" for anonymous FTP servers, or can specify a specific protocol
0064      * (such as a web service interface) on top of a generic protocol like SOAP.
0065      *
0066      * There is
0067      * <a href="http://www.dns-sd.org/ServiceTypes.html">a comprehensive list
0068      * of possible types</a> available, but you are largely on your own for
0069      * subtypes.
0070      *
0071      * @param name     a service name to use instead of the computer name
0072      * @param type     service type, in the form _sometype._udp or _sometype._tcp
0073      * @param port     port number, or 0 to "reserve" the service name
0074      * @param domain   the domain to publish the service on (see DomainBrowser)
0075      * @param subtypes optional list of subtypes, each with a leading underscore
0076      *
0077      * @see ServiceBrowser::ServiceBrowser()
0078      */
0079     explicit PublicService(const QString &name = QString(),
0080                            const QString &type = QString(),
0081                            unsigned int port = 0,
0082                            const QString &domain = QString(),
0083                            const QStringList &subtypes = QStringList());
0084 
0085     ~PublicService() override;
0086 
0087     /**
0088      * Stops publishing or aborts an incomplete publish request.
0089      *
0090      * Useful when you want to disable the service for some time.
0091      *
0092      * Note that if you stop providing a service (without exiting the
0093      * application), you should stop publishing it.
0094      */
0095     void stop();
0096 
0097     /**
0098      * Publish the service synchronously
0099      *
0100      * The method will not return (and hence the application interface will
0101      * freeze, since KDElibs code should be executed in the main thread)
0102      * until either the service is published or publishing fails.
0103      *
0104      * published(bool) is emitted before this method returns.
0105      *
0106      * @return @c true if the service was successfully published, @c false otherwise
0107      */
0108     bool publish();
0109 
0110     /**
0111      * Whether the service is currently published
0112      *
0113      * @return @c true if the service is being published to the domain,
0114      *         @c false otherwise
0115      */
0116     bool isPublished() const;
0117 
0118     /**
0119      * Publish the service asynchronously
0120      *
0121      * Returns immediately and emits published(bool) when completed.
0122      * Note that published(bool) may be emitted before this method
0123      * returns when an error is detected immediately.
0124      */
0125     void publishAsync();
0126 
0127     /**
0128      * Sets new text properties
0129      *
0130      * If the service is already published, it will be re-announced with
0131      * the new data.
0132      *
0133      * @param textData the new text properties for the service
0134      *
0135      * @see ServiceBase::textData()
0136      */
0137     void setTextData(const QMap<QString, QByteArray> &textData);
0138 
0139     /**
0140      * Sets the name of the service
0141      *
0142      * If the service is already published, it will be re-announced with
0143      * the new name.
0144      *
0145      * @param serviceName the new name of the service
0146      */
0147     void setServiceName(const QString &serviceName);
0148 
0149     /**
0150      * Sets the service type
0151      *
0152      * If the service is already published, it will be re-announced with
0153      * the new type.
0154      *
0155      * @param type the new type of the service
0156      *
0157      * See PublicService() for details on the format of @p type
0158      */
0159     void setType(const QString &type);
0160 
0161     /**
0162      * Sets the subtypetypes of the service
0163      *
0164      * If the service is already published, it will be re-announced with
0165      * the new subtypes.
0166      *
0167      * The existing list of substypes is replaced, so an empty list will
0168      * cause all existing subtypes to be removed.
0169      *
0170      * @param subtypes the new list of subtypes
0171      */
0172     void setSubTypes(const QStringList &subtypes);
0173 
0174     /**
0175      * Sets the port
0176      *
0177      * If the service is already published, it will be re-announced with
0178      * the new port.
0179      *
0180      * @param port the port of the service, or 0 to simply "reserve" the name
0181      */
0182     void setPort(unsigned short port);
0183 
0184     /**
0185      * Sets the domain where the service is published
0186      *
0187      * "local." means link-local, ie: the IP subnet on the LAN containing
0188      * this computer.
0189      *
0190      * If service is already published, it will be removed from the current
0191      * domain and published on @p domain instead.
0192      *
0193      * @param domain the new domain to publish the service on
0194      */
0195     void setDomain(const QString &domain);
0196 
0197     /**
0198      * The subtypes of service.
0199      *
0200      * @see setSubTypes()
0201      */
0202     QStringList subtypes() const;
0203 
0204 Q_SIGNALS:
0205     /**
0206      * Emitted when publishing is complete
0207      *
0208      * It will also emitted when an already-published service is
0209      * republished after a property of the service (such as the
0210      * name or port) is changed.
0211      */
0212     void published(bool successful);
0213 
0214 protected:
0215     void virtual_hook(int, void *) override;
0216 
0217 private:
0218     friend class PublicServicePrivate;
0219 };
0220 
0221 }
0222 
0223 #endif