2
* Copyright (C) 2008 Petri Damsten <damu@iki.fi>
3
* Copyright (C) 2010 Michel Lafon-Puyo <michel.lafonpuyo@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Library General Public License version 2 as
7
* published by the Free Software Foundation
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details
14
* You should have received a copy of the GNU Library General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <Plasma/Theme>
22
#include <Plasma/ToolTipManager>
23
#include <KConfigDialog>
24
#include <QGraphicsLinearLayout>
27
SM::Net::Net(QObject *parent, const QVariantList &args)
28
: SM::Applet(parent, args)
29
, m_rx("^network/interfaces/(\\w+)/transmitter/data$")
31
setHasConfigurationInterface(true);
32
resize(234 + 20 + 23, 135 + 20 + 25);
33
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
34
m_sourceTimer.setSingleShot(true);
35
connect(&m_sourceTimer, SIGNAL(timeout()), this, SLOT(sourcesAdded()));
44
KGlobal::locale()->insertCatalog("plasma_applet_system-monitor");
45
setEngine(dataEngine("systemmonitor"));
46
setTitle(i18n("Network"));
48
connect(engine(), SIGNAL(sourceAdded(const QString&)), this, SLOT(sourceAdded(const QString&)));
49
connect(engine(), SIGNAL(sourceRemoved(const QString&)),
50
this, SLOT(sourceRemoved(const QString&)));
51
foreach (const QString& source, engine()->sources()) {
56
void SM::Net::configChanged()
58
KConfigGroup cg = config();
59
setInterval(cg.readEntry("interval", 2.0) * 1000);
60
setSources(cg.readEntry("interfaces", m_interfaces));
64
void SM::Net::sourceAdded(const QString& name)
66
if (m_rx.indexIn(name) != -1) {
67
//kDebug() << m_rx.cap(1);
68
if (m_rx.cap(1) != "lo") {
70
if (!m_sourceTimer.isActive()) {
71
m_sourceTimer.start(0);
77
void SM::Net::sourcesAdded()
82
void SM::Net::sourceRemoved(const QString& name)
84
m_interfaces.removeAll(name);
87
bool SM::Net::addVisualization(const QString& source)
89
QStringList l = source.split('/');
93
QString interface = l[2];
94
SM::Plotter *plotter = new SM::Plotter(this);
95
plotter->setTitle(interface);
96
plotter->setUnit("KiB/s");
97
plotter->setCustomPlots(QList<QColor>() << QColor("#0099ff") << QColor("#91ff00"));
98
//plotter->setStackPlots(false);
99
appendVisualization(interface, plotter);
100
connectSource("network/interfaces/" + interface + "/receiver/data");
101
setPreferredItemHeight(80);
105
void SM::Net::dataUpdated(const QString& source,
106
const Plasma::DataEngine::Data &data)
108
QStringList splitted = source.split('/');
109
if (splitted.length() < 4) {
112
QString interface = splitted[2];
113
int index = (splitted[3] == "receiver") ? 0 : 1;
114
if (!m_data.contains(interface)) {
115
m_data[interface] = QList<double>() << -1 << -1;
117
m_data[interface][index] = qMax(0.0, data["value"].toDouble());
118
if (!m_data[interface].contains(-1)) {
119
SM::Plotter *plotter = qobject_cast<SM::Plotter*>(visualization(interface));
121
plotter->addSample(m_data[interface]);
122
if (mode() == SM::Applet::Panel) {
123
setToolTip(interface,
124
QString("<tr><td>%1</td><td>in</td><td>%2</td><td>out</td><td>%3</td></tr>")
125
.arg(plotter->title())
126
.arg(m_data[interface][0])
127
.arg(m_data[interface][1]));
130
m_data[interface] = QList<double>() << -1 << -1;
134
void SM::Net::createConfigurationInterface(KConfigDialog *parent)
136
QWidget *widget = new QWidget();
139
m_model.setHorizontalHeaderLabels(QStringList() << i18n("Network Interface"));
140
QStandardItem *parentItem = m_model.invisibleRootItem();
142
foreach (const QString& interface, m_interfaces) {
143
QString ifname = interface.split('/')[2];
144
QStandardItem *item1 = new QStandardItem(ifname);
145
item1->setEditable(false);
146
item1->setCheckable(true);
147
item1->setData(interface);
148
if (sources().contains(interface)) {
149
item1->setCheckState(Qt::Checked);
151
parentItem->appendRow(QList<QStandardItem *>() << item1);
153
ui.treeView->setModel(&m_model);
154
ui.treeView->resizeColumnToContents(0);
155
ui.intervalSpinBox->setValue(interval() / 1000.0);
156
ui.intervalSpinBox->setSuffix(i18nc("second", " s"));
157
parent->addPage(widget, i18n("Interfaces"), "network-workgroup");
159
connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
160
connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
161
connect(ui.treeView, SIGNAL(clicked(QModelIndex)), parent, SLOT(settingsModified()));
162
connect(ui.intervalSpinBox, SIGNAL(valueChanged(QString)), parent, SLOT(settingsModified()));
165
void SM::Net::configAccepted()
167
KConfigGroup cg = config();
168
QStandardItem *parentItem = m_model.invisibleRootItem();
172
for (int i = 0; i < parentItem->rowCount(); ++i) {
173
QStandardItem *item = parentItem->child(i, 0);
175
if (item->checkState() == Qt::Checked) {
176
appendSource(item->data().toString());
180
cg.writeEntry("interfaces", sources());
182
double interval = ui.intervalSpinBox->value();
183
cg.writeEntry("interval", interval);
185
emit configNeedsSaving();