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

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KDNSSDSERVICEMODEL_H
0010 #define KDNSSDSERVICEMODEL_H
0011 
0012 #include "kdnssd_export.h"
0013 #include "remoteservice.h"
0014 #include <QAbstractItemModel>
0015 #include <memory>
0016 
0017 namespace KDNSSD
0018 {
0019 struct ServiceModelPrivate;
0020 class ServiceBrowser;
0021 
0022 /**
0023  * @class ServiceModel servicemodel.h KDNSSD/ServiceModel
0024  * @short Model for list of Zeroconf services
0025  *
0026  * This class provides a Qt Model for ServiceBrowser to allow easy
0027  * integration of service discovery into a GUI.  For example, to
0028  * show the HTTP servers published on the local network, you can do:
0029  * @code
0030  * KDNSSD::ServiceModel *serviceModel = new ServiceModel(
0031  *     new KDNSSD::ServiceBrowser("_http._tcp")
0032  *     );
0033  * QComboBox *serviceCombo = new QComboBox();
0034  * serviceCombo->setModel(serviceModel);
0035  * @endcode
0036  *
0037  * After the user makes a selection, the application typically needs
0038  * to get a pointer to the selected service in order to get the host
0039  * name and port.  A RemoteService::Ptr can be obtained from
0040  * a QModelIndex using:
0041  * @code
0042  * void onSelected(const QModelIndex &selection) {
0043  *     KDNSSD::RemoteService::Ptr service =
0044  *         selection.data(KDNSSD::ServiceModel::ServicePtrRole)
0045  *                  .value<KDNSSD::RemoteService::Ptr>();
0046  * }
0047  * @endcode
0048  *
0049  * @since 4.1
0050  * @author Jakub Stachowski
0051  */
0052 
0053 class KDNSSD_EXPORT ServiceModel : public QAbstractItemModel
0054 {
0055     Q_OBJECT
0056 
0057 public:
0058     /** The additional data roles provided by this model */
0059     enum AdditionalRoles {
0060         ServicePtrRole = 0xA06519DE, ///< gets a RemoteService::Ptr for the service
0061     };
0062 
0063     /**
0064      * The default columns for this model.
0065      *
0066      * If service browser is not set to resolve automatically,
0067      * then the model will only ever have one column (the service name).
0068      */
0069     enum ModelColumns {
0070         ServiceName = 0,
0071         Host = 1,
0072         Port = 2,
0073     };
0074 
0075     /**
0076      * Creates a model for the given service browser and starts browsing
0077      * for services.
0078      *
0079      * The model takes ownership of the browser,
0080      * so there is no need to delete it afterwards.
0081      *
0082      * You should @b not call ServiceBrowser::startBrowse() on @p browser
0083      * before passing it to ServiceModel.
0084      */
0085     explicit ServiceModel(ServiceBrowser *browser, QObject *parent = nullptr);
0086 
0087     ~ServiceModel() override;
0088 
0089     /** @reimp */
0090     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0091     /** @reimp */
0092     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0093     /** @reimp */
0094     QModelIndex parent(const QModelIndex &index) const override;
0095     /** @reimp */
0096     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0097     /** @reimp */
0098     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0099     /** @reimp */
0100     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0101     /** @reimp */
0102     virtual bool hasIndex(int row, int column, const QModelIndex &parent) const;
0103 
0104 private:
0105     std::unique_ptr<ServiceModelPrivate> const d;
0106     friend struct ServiceModelPrivate;
0107 };
0108 
0109 }
0110 
0111 #endif