-
Notifications
You must be signed in to change notification settings - Fork 8
/
zconfbrowserwidget.cpp
146 lines (132 loc) · 4.37 KB
/
zconfbrowserwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* This file is part of qtzeroconf. (c) 2012 Johannes Hilden
* https://github.com/johanneshilden/qtzeroconf
*
* qtzeroconf is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* qtzeroconf is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with qtzeroconf; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <QDebug>
#include "zconfbrowserwidget.h"
#include "zconfservicebrowser.h"
class ZConfBrowserWidgetPrivate
{
public:
ZConfBrowserWidgetPrivate(ZConfBrowserWidget *widget)
: q(widget),
browser(0)
{
q->setColumnCount(6);
QStringList headers;
q->setHeaderLabels(headers << QObject::tr("Service")
<< QObject::tr("Domain")
<< QObject::tr("Host")
<< QObject::tr("Protocol")
<< QObject::tr("IP")
<< QObject::tr("Port"));
q->setColumnWidth(0, 170);
}
void init(QString serviceType)
{
delete browser;
browser = new ZConfServiceBrowser(q);
type = serviceType;
QObject::connect(browser, SIGNAL(serviceEntryAdded(QString)), q, SLOT(addService(QString)));
QObject::connect(browser, SIGNAL(serviceEntryRemoved(QString)), q, SLOT(removeService(QString)));
q->clear();
browser->browse(type);
}
ZConfBrowserWidget *const q;
ZConfServiceBrowser *browser;
QString type;
};
/*!
\class ZConfBrowserWidget
\brief QTreeWidget-based widget for browsing and displaying Zeroconf
services available on the local network.
*/
/*!
Creates a ZConfBrowserWidget object using the provided service type.
*/
ZConfBrowserWidget::ZConfBrowserWidget(QString serviceType, QWidget *parent)
: QTreeWidget(parent),
d_ptr(new ZConfBrowserWidgetPrivate(this))
{
d_ptr->init(serviceType);
}
/*!
Convenience constructor that uses "_http._tcp" as service type.
*/
ZConfBrowserWidget::ZConfBrowserWidget(QWidget *parent)
: QTreeWidget(parent),
d_ptr(new ZConfBrowserWidgetPrivate(this))
{
d_ptr->init("_http._tcp");
}
/*!
Destroys the ZConfBrowserWidget object.
*/
ZConfBrowserWidget::~ZConfBrowserWidget()
{
delete d_ptr;
}
/*!
Enables/disables condensed list mode. In condensed mode, only service name,
domain and host name is shown in the list.
*/
void ZConfBrowserWidget::setCondensed(bool enabled)
{
for (int i = 3; i < 7; i++) setColumnHidden(i, enabled);
setRootIsDecorated(!enabled);
}
void ZConfBrowserWidget::addService(QString service)
{
QTreeWidgetItem *parent = 0;
for (int i = 0; i < topLevelItemCount(); i++) {
QTreeWidgetItem *item = topLevelItem(i);
if (service == item->text(0)) {
parent = item;
break;
}
}
ZConfServiceEntry serviceEntry = d_ptr->browser->serviceEntry(service);
if (!serviceEntry.isValid())
return; // No service entry with that name found.
if (!parent) {
parent = new QTreeWidgetItem(this);
parent->setText(0, service);
parent->setText(1, serviceEntry.domain);
parent->setText(2, serviceEntry.host);
parent->setText(5, QString::number(serviceEntry.port));
}
QTreeWidgetItem *entry = new QTreeWidgetItem(parent);
entry->setText(0, service);
entry->setText(1, serviceEntry.domain);
entry->setText(3, serviceEntry.protocolName());
entry->setText(4, serviceEntry.ip);
entry->setText(5, QString::number(serviceEntry.port));
}
void ZConfBrowserWidget::removeService(QString service)
{
int count = topLevelItemCount();
int i = 0;
while (i < count) {
QTreeWidgetItem *item = topLevelItem(i);
if (service == item->text(0)) {
delete item;
--count;
} else {
++i;
}
}
}