1 | /* |
2 | This file is part of the KDE project |
3 | |
4 | SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef KDNSSDSERVICEMODEL_H |
10 | #define KDNSSDSERVICEMODEL_H |
11 | |
12 | #include "kdnssd_export.h" |
13 | #include "remoteservice.h" |
14 | #include <QAbstractItemModel> |
15 | #include <memory> |
16 | |
17 | namespace KDNSSD |
18 | { |
19 | struct ServiceModelPrivate; |
20 | class ServiceBrowser; |
21 | |
22 | /*! |
23 | * \class KDNSSD::ServiceModel |
24 | * \inmodule KDNSSD |
25 | * \inheaderfile KDNSSD/ServiceModel |
26 | * |
27 | * \brief Model for list of Zeroconf services. |
28 | * |
29 | * This class provides a Qt Model for ServiceBrowser to allow easy |
30 | * integration of service discovery into a GUI. For example, to |
31 | * show the HTTP servers published on the local network, you can do: |
32 | * \code |
33 | * KDNSSD::ServiceModel *serviceModel = new ServiceModel( |
34 | * new KDNSSD::ServiceBrowser("_http._tcp") |
35 | * ); |
36 | * QComboBox *serviceCombo = new QComboBox(); |
37 | * serviceCombo->setModel(serviceModel); |
38 | * \endcode |
39 | * |
40 | * After the user makes a selection, the application typically needs |
41 | * to get a pointer to the selected service in order to get the host |
42 | * name and port. A RemoteService::Ptr can be obtained from |
43 | * a QModelIndex using: |
44 | * \code |
45 | * void onSelected(const QModelIndex &selection) { |
46 | * KDNSSD::RemoteService::Ptr service = |
47 | * selection.data(KDNSSD::ServiceModel::ServicePtrRole) |
48 | * .value<KDNSSD::RemoteService::Ptr>(); |
49 | * } |
50 | * \endcode |
51 | * |
52 | * \since 4.1 |
53 | */ |
54 | class KDNSSD_EXPORT ServiceModel : public QAbstractItemModel |
55 | { |
56 | Q_OBJECT |
57 | |
58 | public: |
59 | /*! \enum KDNSSD::ServiceModel::AdditionalRoles |
60 | * \brief The additional data roles provided by this model. |
61 | * |
62 | * \value ServicePtrRole |
63 | * Gets a RemoteService::Ptr for the service. |
64 | */ |
65 | enum AdditionalRoles { |
66 | ServicePtrRole = 0x7E6519DE, |
67 | }; |
68 | |
69 | /*! |
70 | * \enum KDNSSD::ServiceModel::ModelColumns |
71 | * \brief The default columns for this model. |
72 | * |
73 | * If service browser is not set to resolve automatically, |
74 | * then the model will only ever have one column (the service name). |
75 | * |
76 | * \value ServiceName |
77 | * The name of this service. |
78 | * \value Host |
79 | * The hostname or IP address the service is hosted on. |
80 | * \value Port |
81 | * The TCP or UDP port the service is hosted on. |
82 | */ |
83 | enum ModelColumns { |
84 | ServiceName = 0, |
85 | Host = 1, |
86 | Port = 2, |
87 | }; |
88 | |
89 | /*! |
90 | * Creates a model for the given service browser and starts browsing |
91 | * for services. |
92 | * |
93 | * \note |
94 | * The model takes ownership of the browser, |
95 | * so there is no need to delete it afterwards. |
96 | * |
97 | * \note You should \b not call ServiceBrowser::startBrowse() on \a browser |
98 | * before passing it to ServiceModel. |
99 | */ |
100 | explicit ServiceModel(ServiceBrowser *browser, QObject *parent = nullptr); |
101 | |
102 | ~ServiceModel() override; |
103 | |
104 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
105 | |
106 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
107 | |
108 | QModelIndex parent(const QModelIndex &index) const override; |
109 | |
110 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
111 | |
112 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
113 | |
114 | QVariant (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; |
115 | |
116 | /*! */ |
117 | virtual bool hasIndex(int row, int column, const QModelIndex &parent) const; |
118 | |
119 | private: |
120 | std::unique_ptr<ServiceModelPrivate> const d; |
121 | friend struct ServiceModelPrivate; |
122 | }; |
123 | |
124 | } |
125 | |
126 | #endif |
127 | |