File: lxqtnetworkmonitor.cpp

package info (click to toggle)
lxqt-panel 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 15,748 kB
  • sloc: cpp: 27,548; xml: 798; makefile: 19
file content (214 lines) | stat: -rw-r--r-- 5,975 bytes parent folder | download | duplicates (2)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2012 Razor team
 * Authors:
 *   Alexander Sokoloff <sokoloff.a@gmail.com>
 *
 * This program or library 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.
 *
 * This library 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 this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "lxqtnetworkmonitor.h"
#include "lxqtnetworkmonitorconfiguration.h"
#include "../panel/ilxqtpanelplugin.h"

#include <QEvent>
#include <QPainter>
#include <QPixmap>
#include <QLinearGradient>
#include <QHBoxLayout>

extern "C" {
#include <statgrab.h>
}

#ifdef __sg_public
// since libstatgrab 0.90 this macro is defined, so we use it for version check
#define STATGRAB_NEWER_THAN_0_90 	1
#endif

LXQtNetworkMonitor::LXQtNetworkMonitor(ILXQtPanelPlugin *plugin, QWidget* parent):
    QFrame(parent),
    mPlugin(plugin)
{
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(&m_stuff);
    setLayout(layout);
    /* Initialise statgrab */
#ifdef STATGRAB_NEWER_THAN_0_90
    sg_init(0);
#else
    sg_init();
#endif

    m_iconList << QStringLiteral("modem") << QStringLiteral("monitor")
               << QStringLiteral("network") << QStringLiteral("wireless");

    startTimer(800);

    settingsChanged();
}

LXQtNetworkMonitor::~LXQtNetworkMonitor() = default;

void LXQtNetworkMonitor::resizeEvent(QResizeEvent *)
{
    m_stuff.setMinimumWidth(m_pic.width() + 2);
    m_stuff.setMinimumHeight(m_pic.height() + 2);

    update();
}


void LXQtNetworkMonitor::timerEvent(QTimerEvent * /*event*/)
{
    bool matched = false;

#ifdef STATGRAB_NEWER_THAN_0_90
    size_t num_network_stats;
    size_t x;
#else
    int num_network_stats;
    int x;
#endif
    sg_network_io_stats *network_stats = sg_get_network_io_stats_diff(&num_network_stats);

    for (x = 0; x < num_network_stats; x++)
    {
        if (m_interface == QString::fromLocal8Bit(network_stats->interface_name))
        {
            if (network_stats->rx != 0 && network_stats->tx != 0)
            {
                m_pic.load(iconName(QStringLiteral("transmit-receive")));
            }
            else if (network_stats->rx != 0 && network_stats->tx == 0)
            {
                m_pic.load(iconName(QStringLiteral("receive")));
            }
            else if (network_stats->rx == 0 && network_stats->tx != 0)
            {
                m_pic.load(iconName(QStringLiteral("transmit")));
            }
            else
            {
                m_pic.load(iconName(QStringLiteral("idle")));
            }

            matched = true;

            break;
        }

        network_stats++;
    }

    if (!matched)
    {
        m_pic.load(iconName(QStringLiteral("error")));
    }

    update();
}

void LXQtNetworkMonitor::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    QRectF r = rect();

    int leftOffset = (r.width() - m_pic.width() + 2) / 2;
    int topOffset = (r.height() - m_pic.height() + 2) / 2;

    p.drawPixmap(leftOffset, topOffset, m_pic);
}

bool LXQtNetworkMonitor::event(QEvent *event)
{
    if (event->type() == QEvent::ToolTip)
    {
#ifdef STATGRAB_NEWER_THAN_0_90
        size_t num_network_stats;
        size_t x;
#else
        int num_network_stats;
        int x;
#endif
        sg_network_io_stats *network_stats = sg_get_network_io_stats(&num_network_stats);

        for (x = 0; x < num_network_stats; x++)
        {
            if (m_interface == QString::fromLocal8Bit(network_stats->interface_name))
            {
                setToolTip(tr("Network interface <b>%1</b>").arg(m_interface) + QStringLiteral("<br>")
                           + tr("Transmitted %1").arg(convertUnits(network_stats->tx)) + QStringLiteral("<br>")
                           + tr("Received %1").arg(convertUnits(network_stats->rx))
                          );
            }
            network_stats++;
        }
    }
    return QFrame::event(event);
}

//void LXQtNetworkMonitor::showConfigureDialog()
//{
//    LXQtNetworkMonitorConfiguration *confWindow =
//        this->findChild<LXQtNetworkMonitorConfiguration*>("LXQtNetworkMonitorConfigurationWindow");

//    if (!confWindow)
//    {
//        confWindow = new LXQtNetworkMonitorConfiguration(settings(), this);
//    }

//    confWindow->show();
//    confWindow->raise();
//    confWindow->activateWindow();
//}

void LXQtNetworkMonitor::settingsChanged()
{
    m_iconIndex = mPlugin->settings()->value(QStringLiteral("icon"), 1).toInt();
    m_interface = mPlugin->settings()->value(QStringLiteral("interface")).toString();
    if (m_interface.isEmpty())
    {
#ifdef STATGRAB_NEWER_THAN_0_90
        size_t count;
#else
        int count;
#endif
        sg_network_iface_stats* stats = sg_get_network_iface_stats(&count);
        if (count > 0)
            m_interface = QString(QLatin1String(stats[0].interface_name));
    }

    m_pic.load(iconName(QStringLiteral("error")));
}

QString LXQtNetworkMonitor::convertUnits(double num)
{
    QString unit = tr("B");
    QStringList units = QStringList(tr("KiB")) << tr("MiB") << tr("GiB") << tr("TiB") << tr("PiB");
    for (QStringListIterator iter(units); num >= 1024 && iter.hasNext();)
    {
        num /= 1024;
        unit = iter.next();
    }
    return QString::number(num, 'f', 2) + QLatin1Char(' ') + unit;
}