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
|
# -*- coding: utf-8 -*-
# Net_monitor plasma interface
#
# Copyright, (C) Eugeni Dodonov <eugeni@mandriva.com>, 2011
#
from PyQt4.QtCore import Qt, QString, pyqtSignature
from PyQt4.QtGui import QGraphicsLinearLayout
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
# localization
import gettext
try:
gettext.install("net_monitor")
except IOError:
_ = str
class NetMonitor(plasmascript.Applet):
def __init__(self,parent,args=None):
plasmascript.Applet.__init__(self,parent)
def init(self):
self.setHasConfigurationInterface(False)
self.setAspectRatioMode(Plasma.Square)
self.theme = Plasma.Svg(self)
self.theme.setImagePath("widgets/background")
self.setBackgroundHints(Plasma.Applet.DefaultBackground)
self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
self.monitoring = {}
self.widgets = {}
for source in self.connectToEngine():
label = Plasma.Label(self.applet)
label.setText("%s:" % source)
self.layout.addItem(label)
self.widgets[str(source)] = label
self.monitoring[str(source)] = {"in": 0, "out": 0}
self.applet.setLayout(self.layout)
def connectToEngine(self):
self.engine = self.dataEngine("net_monitor_data")
self.sources = self.engine.sources()
for source in self.sources:
self.engine.connectSource(source, self, 1000)
return self.sources
@pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
def dataUpdated(self, sourceName, data):
"""Got something from data source"""
iface = str(sourceName)
if iface not in self.widgets:
print("Error: data for %s not available yet" % iface)
return
widget = self.widgets[iface]
data_in = int(data[QString("data_in")])
data_out = int(data[QString("data_out")])
old_data_in = self.monitoring[iface]["in"]
old_data_out = self.monitoring[iface]["out"]
if old_data_in == -1:
speed_in = 0
else:
speed_in = data_in - old_data_in
if old_data_out == -1:
speed_out = 0
else:
speed_out = data_out - old_data_out
self.monitoring[iface]["in"] = data_in
self.monitoring[iface]["out"] = data_out
widget.setText("%s\n\/: %d\n/\: %d" % (sourceName, speed_in, speed_out))
def paintInterface(self, painter, option, rect):
painter.save()
painter.restore()
def CreateApplet(parent):
return NetMonitor(parent)
|