~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/system-monitor/net.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2008 Petri Damsten <damu@iki.fi>
 
3
 *   Copyright (C) 2010 Michel Lafon-Puyo <michel.lafonpuyo@gmail.com>
 
4
 *
 
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
 
8
 *
 
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
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include "net.h"
 
21
#include <Plasma/Theme>
 
22
#include <Plasma/ToolTipManager>
 
23
#include <KConfigDialog>
 
24
#include <QGraphicsLinearLayout>
 
25
#include <plotter.h>
 
26
 
 
27
SM::Net::Net(QObject *parent, const QVariantList &args)
 
28
    : SM::Applet(parent, args)
 
29
    , m_rx("^network/interfaces/(\\w+)/transmitter/data$")
 
30
{
 
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()));
 
36
}
 
37
 
 
38
SM::Net::~Net()
 
39
{
 
40
}
 
41
 
 
42
void SM::Net::init()
 
43
{
 
44
    KGlobal::locale()->insertCatalog("plasma_applet_system-monitor");
 
45
    setEngine(dataEngine("systemmonitor"));
 
46
    setTitle(i18n("Network"));
 
47
 
 
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()) {
 
52
        sourceAdded(source);
 
53
    }
 
54
}
 
55
 
 
56
void SM::Net::configChanged()
 
57
{
 
58
    KConfigGroup cg = config();
 
59
    setInterval(cg.readEntry("interval", 2.0) * 1000);
 
60
    setSources(cg.readEntry("interfaces", m_interfaces));
 
61
    connectToEngine();
 
62
}
 
63
 
 
64
void SM::Net::sourceAdded(const QString& name)
 
65
{
 
66
    if (m_rx.indexIn(name) != -1) {
 
67
        //kDebug() << m_rx.cap(1);
 
68
        if (m_rx.cap(1) != "lo") {
 
69
            m_interfaces << name;
 
70
            if (!m_sourceTimer.isActive()) {
 
71
                m_sourceTimer.start(0);
 
72
            }
 
73
        }
 
74
    }
 
75
}
 
76
 
 
77
void SM::Net::sourcesAdded()
 
78
{
 
79
    configChanged();
 
80
}
 
81
 
 
82
void SM::Net::sourceRemoved(const QString& name)
 
83
{
 
84
    m_interfaces.removeAll(name);
 
85
}
 
86
 
 
87
bool SM::Net::addVisualization(const QString& source)
 
88
{
 
89
    QStringList l = source.split('/');
 
90
    if (l.count() < 3) {
 
91
        return false;
 
92
    }
 
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);
 
102
    return true;
 
103
}
 
104
 
 
105
void SM::Net::dataUpdated(const QString& source,
 
106
                          const Plasma::DataEngine::Data &data)
 
107
{
 
108
    QStringList splitted = source.split('/');
 
109
    if (splitted.length() < 4) {
 
110
        return;
 
111
    }
 
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;
 
116
    }
 
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));
 
120
        if (plotter) {
 
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]));
 
128
            }
 
129
        }
 
130
        m_data[interface] = QList<double>() << -1 << -1;
 
131
    }
 
132
}
 
133
 
 
134
void SM::Net::createConfigurationInterface(KConfigDialog *parent)
 
135
{
 
136
    QWidget *widget = new QWidget();
 
137
    ui.setupUi(widget);
 
138
    m_model.clear();
 
139
    m_model.setHorizontalHeaderLabels(QStringList() << i18n("Network Interface"));
 
140
    QStandardItem *parentItem = m_model.invisibleRootItem();
 
141
 
 
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);
 
150
        }
 
151
        parentItem->appendRow(QList<QStandardItem *>() << item1);
 
152
    }
 
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");
 
158
 
 
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()));
 
163
}
 
164
 
 
165
void SM::Net::configAccepted()
 
166
{
 
167
    KConfigGroup cg = config();
 
168
    QStandardItem *parentItem = m_model.invisibleRootItem();
 
169
 
 
170
    clear();
 
171
 
 
172
    for (int i = 0; i < parentItem->rowCount(); ++i) {
 
173
        QStandardItem *item = parentItem->child(i, 0);
 
174
        if (item) {
 
175
            if (item->checkState() == Qt::Checked) {
 
176
                appendSource(item->data().toString());
 
177
            }
 
178
        }
 
179
    }
 
180
    cg.writeEntry("interfaces", sources());
 
181
 
 
182
    double interval = ui.intervalSpinBox->value();
 
183
    cg.writeEntry("interval", interval);
 
184
 
 
185
    emit configNeedsSaving();
 
186
}
 
187
 
 
188
#include "net.moc"