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 ServiceModel servicemodel.h KDNSSD/ServiceModel |
24 | * @short Model for list of Zeroconf services |
25 | * |
26 | * This class provides a Qt Model for ServiceBrowser to allow easy |
27 | * integration of service discovery into a GUI. For example, to |
28 | * show the HTTP servers published on the local network, you can do: |
29 | * @code |
30 | * KDNSSD::ServiceModel *serviceModel = new ServiceModel( |
31 | * new KDNSSD::ServiceBrowser("_http._tcp") |
32 | * ); |
33 | * QComboBox *serviceCombo = new QComboBox(); |
34 | * serviceCombo->setModel(serviceModel); |
35 | * @endcode |
36 | * |
37 | * After the user makes a selection, the application typically needs |
38 | * to get a pointer to the selected service in order to get the host |
39 | * name and port. A RemoteService::Ptr can be obtained from |
40 | * a QModelIndex using: |
41 | * @code |
42 | * void onSelected(const QModelIndex &selection) { |
43 | * KDNSSD::RemoteService::Ptr service = |
44 | * selection.data(KDNSSD::ServiceModel::ServicePtrRole) |
45 | * .value<KDNSSD::RemoteService::Ptr>(); |
46 | * } |
47 | * @endcode |
48 | * |
49 | * @since 4.1 |
50 | * @author Jakub Stachowski |
51 | */ |
52 | |
53 | class KDNSSD_EXPORT ServiceModel : public QAbstractItemModel |
54 | { |
55 | Q_OBJECT |
56 | |
57 | public: |
58 | /** The additional data roles provided by this model */ |
59 | enum AdditionalRoles { |
60 | ServicePtrRole = 0x7E6519DE, ///< gets a RemoteService::Ptr for the service |
61 | }; |
62 | |
63 | /** |
64 | * The default columns for this model. |
65 | * |
66 | * If service browser is not set to resolve automatically, |
67 | * then the model will only ever have one column (the service name). |
68 | */ |
69 | enum ModelColumns { |
70 | ServiceName = 0, |
71 | Host = 1, |
72 | Port = 2, |
73 | }; |
74 | |
75 | /** |
76 | * Creates a model for the given service browser and starts browsing |
77 | * for services. |
78 | * |
79 | * The model takes ownership of the browser, |
80 | * so there is no need to delete it afterwards. |
81 | * |
82 | * You should @b not call ServiceBrowser::startBrowse() on @p browser |
83 | * before passing it to ServiceModel. |
84 | */ |
85 | explicit ServiceModel(ServiceBrowser *browser, QObject *parent = nullptr); |
86 | |
87 | ~ServiceModel() override; |
88 | |
89 | /** @reimp */ |
90 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
91 | /** @reimp */ |
92 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
93 | /** @reimp */ |
94 | QModelIndex parent(const QModelIndex &index) const override; |
95 | /** @reimp */ |
96 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
97 | /** @reimp */ |
98 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
99 | /** @reimp */ |
100 | QVariant (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; |
101 | /** @reimp */ |
102 | virtual bool hasIndex(int row, int column, const QModelIndex &parent) const; |
103 | |
104 | private: |
105 | std::unique_ptr<ServiceModelPrivate> const d; |
106 | friend struct ServiceModelPrivate; |
107 | }; |
108 | |
109 | } |
110 | |
111 | #endif |
112 | |