1 | /* This file is part of the KDE project |
2 | * |
3 | * Copyright (C) 2008 Jakub Stachowski <qbast@go2.pl> |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public License |
16 | * along with this library; see the file COPYING.LIB. If not, write to |
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | * Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #ifndef DNSSDSERVICEMODEL_H |
22 | #define DNSSDSERVICEMODEL_H |
23 | |
24 | #include <QtCore/QAbstractItemModel> |
25 | #include <dnssd/dnssd_export.h> |
26 | #include <dnssd/remoteservice.h> |
27 | |
28 | namespace DNSSD |
29 | { |
30 | |
31 | struct ServiceModelPrivate; |
32 | class ServiceBrowser; |
33 | |
34 | |
35 | /** |
36 | * @class ServiceModel servicemodel.h DNSSD/ServiceModel |
37 | * @short Model for list of Zeroconf services |
38 | * |
39 | * This class provides a Qt Model for ServiceBrowser to allow easy |
40 | * integration of service discovery into a GUI. For example, to |
41 | * show the HTTP servers published on the local network, you can do: |
42 | * @code |
43 | * DNSSD::ServiceModel *serviceModel = new ServiceModel( |
44 | * new DNSSD::ServiceBrowser("_http._tcp") |
45 | * ); |
46 | * QComboBox *serviceCombo = new QComboBox(); |
47 | * serviceCombo->setModel(serviceModel); |
48 | * @endcode |
49 | * |
50 | * After the user makes a selection, the application typically needs |
51 | * to get a pointer to the selected service in order to get the host |
52 | * name and port. A RemoteService::Ptr can be obtained from |
53 | * a QModelIndex using: |
54 | * @code |
55 | * void onSelected(const QModelIndex &selection) { |
56 | * DNSSD::RemoteService::Ptr service = |
57 | * selection.data(DNSSD::ServiceModel::ServicePtrRole) |
58 | * .value<DNSSD::RemoteService::Ptr>(); |
59 | * } |
60 | * @endcode |
61 | * |
62 | * @since 4.1 |
63 | * @author Jakub Stachowski |
64 | */ |
65 | |
66 | class KDNSSD_EXPORT ServiceModel : public QAbstractItemModel |
67 | { |
68 | Q_OBJECT |
69 | |
70 | public: |
71 | |
72 | /** The additional data roles provided by this model */ |
73 | enum AdditionalRoles { |
74 | ServicePtrRole = 0xA06519DE ///< gets a RemoteService::Ptr for the service |
75 | }; |
76 | |
77 | /** |
78 | * The default columns for this model. |
79 | * |
80 | * If service browser is not set to resolve automatically, |
81 | * then the model will only ever have one column (the service name). |
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 | * The model takes ownership of the browser, |
94 | * so there is no need to delete it afterwards. |
95 | * |
96 | * You should @b not call ServiceBrowser::startBrowse() on @p browser |
97 | * before passing it to ServiceModel. |
98 | */ |
99 | explicit ServiceModel(ServiceBrowser* browser, QObject* parent = 0); |
100 | |
101 | virtual ~ServiceModel(); |
102 | |
103 | /** @reimp */ |
104 | virtual int columnCount(const QModelIndex& parent = QModelIndex() ) const; |
105 | /** @reimp */ |
106 | virtual int rowCount(const QModelIndex& parent = QModelIndex() ) const; |
107 | /** @reimp */ |
108 | virtual QModelIndex parent(const QModelIndex& index ) const; |
109 | /** @reimp */ |
110 | virtual QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex() ) const; |
111 | /** @reimp */ |
112 | virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole ) const; |
113 | /** @reimp */ |
114 | virtual QVariant (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; |
115 | /** @reimp */ |
116 | virtual bool hasIndex(int row, int column, const QModelIndex &parent) const; |
117 | |
118 | private: |
119 | ServiceModelPrivate* const d; |
120 | friend struct ServiceModelPrivate; |
121 | |
122 | }; |
123 | |
124 | } |
125 | |
126 | #endif |
127 | |